:root {
    color-scheme: light;

    /* Core palette */
    --bg: #f4f1ea;
    --paper: #fffdf8;
    --ink: #1d1b19;
    --muted: #7a746b;
    --line: #d9d2c5;
    --accent: #6b3fa0;
    --accent-ink: #fff;
    --danger: #b3261e;
    --danger-ink: #fff;
    --ok: #2b7630;

    /* Surfaces */
    --field: #fff;                 /* inputs, buttons, scene log */
    --header-bg: var(--ink);
    --header-ink: #fff;
    --subtle: #ece6dc;             /* tags, table headers */
    --stripe: #faf7f1;             /* alternate table rows */
    --timeline-bg: #e9e3d7;
    --code-bg: #23201d;
    --code-ink: #f4f1ea;

    /* Accent tints */
    --accent-tint: #efe6fa;        /* info banner, quotes */
    --accent-wash: #faf6ff;        /* "your turn" panel */
    --accent-soft: #f1e9fb;        /* current player row */
    --slot-bg: rgba(107, 63, 160, .07);
    --slot-bg-hover: rgba(107, 63, 160, .18);

    /* Status */
    --warn-bg: #fff3cd;
    --notice-bg: #fff8e1;          /* end vote, open push */
    --notice-line: #f0d98c;
    --ok-bg: #e3f3e4;
    --danger-bg: #fbe4e2;
    --online: #4caf50;
    --offline: #ccc;

    /* Timeline cards: Light and Dark tones must stay clearly distinct in every theme */
    --card-border: #333;
    --light-card: #ffffff;
    --light-card-ink: var(--ink);
    --dark-card: #23201d;
    --dark-card-ink: #f4f1ea;
    --dark-card-border: #000;
    --pending-a: #fff;
    --pending-b: #ddd;

    /* Scene and story text */
    --quote-ink: #4a443d;          /* big picture */
    --thought-ink: #5a4b78;
    --narration-ink: #444;

    /* Type and shape */
    --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --font-heading: Georgia, "Times New Roman", serif;
    --font-mono: ui-monospace, monospace;
    --radius: 8px;
    --radius-sm: 6px;
    --shadow: 0 1px 3px rgba(0, 0, 0, .12);

    font-family: var(--font-body);
    color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); line-height: 1.45; }
a { color: var(--accent); }
h1, h2, h3 { font-family: var(--font-heading); margin: .2em 0 .4em; }
h1 { font-size: 1.7rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
[hidden] { display: none !important; }

.site-header { display: flex; justify-content: space-between; align-items: center; padding: .6rem 1.2rem; background: var(--header-bg); color: var(--header-ink); }
.site-header a { color: var(--header-ink); text-decoration: none; margin-left: 1rem; }
.site-header .brand { font-weight: 700; margin-left: 0; font-family: var(--font-heading); font-size: 1.2rem; }
.container { max-width: 1100px; margin: 1.5rem auto; padding: 0 1rem; }
.board-main { padding: 1rem; }

.banner { padding: .6rem 1.2rem; background: var(--accent-tint); border-bottom: 1px solid var(--line); }
.banner.warn { background: var(--warn-bg); }
.flash { padding: .6rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; }
.flash-success { background: var(--ok-bg); color: var(--ok); }
.flash-error { background: var(--danger-bg); color: var(--danger); }
.error { color: var(--danger); margin-top: .5rem; }

.card { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.2rem; margin-bottom: 1rem; box-shadow: var(--shadow); }
.card.narrow { max-width: 440px; margin: 2rem auto; }

form label { display: block; margin-bottom: .7rem; font-weight: 500; }
form label.check { font-weight: normal; }
input, select, textarea { font: inherit; padding: .45rem .55rem; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--field); width: 100%; margin-top: .2rem; }
input[type=checkbox], input[type=radio] { width: auto; margin: 0 .3rem 0 0; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); margin: 0 0 .8rem; padding: .6rem .8rem; }
form.inline { display: inline; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.row input { flex: 1; min-width: 12rem; margin: 0; }
.row select { width: auto; margin: 0; }
.stack > * { margin-bottom: .6rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
@media (max-width: 700px) { .grid2 { grid-template-columns: 1fr; } }

.btn { display: inline-block; font: inherit; padding: .45rem .9rem; border-radius: var(--radius-sm); border: 1px solid var(--line); background: var(--field); cursor: pointer; color: var(--ink); }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.small { padding: .2rem .6rem; font-size: .85rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
button.link { background: none; border: none; color: var(--accent); cursor: pointer; text-decoration: underline; font: inherit; }

.tag { display: inline-block; font-size: .75rem; padding: .05rem .45rem; border-radius: 999px; background: var(--subtle); color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.tag.your-turn { background: var(--accent); color: var(--accent-ink); }
.badge { background: var(--danger); color: var(--danger-ink); border-radius: 999px; padding: 0 .4rem; font-size: .7rem; }

/* Lobby */
.lobby { display: grid; grid-template-columns: 1fr 320px; gap: 1.5rem; }
@media (max-width: 800px) { .lobby { grid-template-columns: 1fr; } }
.game-list { list-style: none; padding: 0; }
.game-list li { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: .7rem 1rem; margin-bottom: .5rem; display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.code-input, .code { text-transform: uppercase; letter-spacing: .15em; font-family: var(--font-mono); }
.rules-mini p { font-size: .9rem; }

/* Board */
.board-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; }
.big-picture { font-family: var(--font-heading); font-style: italic; font-size: 1.1rem; margin: 0; color: var(--quote-ink); }
.head-meta { display: flex; gap: .4rem; flex-wrap: wrap; justify-content: flex-end; align-items: center; }
.turn-panel { background: var(--paper); border: 1px solid var(--line); border-left: 5px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; margin: .8rem 0; }
.turn-panel:has(.prompt.my-turn) { border-left-color: var(--accent); background: var(--accent-wash); }
.prompt { font-weight: 600; }
.prompt.my-turn::before { content: "Your turn — "; color: var(--accent); }
.focus-line { display: flex; gap: 1.5rem; color: var(--muted); font-size: .9rem; margin-bottom: .3rem; }
.focus-line .focus { color: var(--ink); font-family: var(--font-heading); font-size: 1.05rem; }
.action { margin-top: .6rem; }
.action .hint { margin: .3rem 0; }
.slot-sample { display: inline-block; border: 1px dashed var(--accent); color: var(--accent); border-radius: 4px; padding: 0 .35rem; font-weight: 700; }
.end-vote { background: var(--notice-bg); border: 1px solid var(--notice-line); padding: .5rem .8rem; border-radius: var(--radius); margin-bottom: .6rem; }
.invite-link { width: auto; min-width: 22rem; display: inline-block; }
.lobby-leave { margin-top: .5rem; display: inline-block; margin-right: .5rem; }
.tone-pick { display: inline-flex; gap: .8rem; margin-left: .5rem; }
.tone-pick label { display: inline-flex; align-items: center; margin: .3rem 0; font-weight: normal; }

.board-body { display: grid; grid-template-columns: 1fr 320px; gap: 1rem; align-items: start; }
@media (max-width: 1000px) { .board-body { grid-template-columns: 1fr; } }

/* Timeline */
.timeline-wrap { background: var(--timeline-bg); border-radius: var(--radius); padding: .8rem; min-height: 300px; overflow: hidden; }
.timeline { display: flex; align-items: flex-start; overflow-x: auto; padding-bottom: .8rem; gap: 0; }
.timeline .empty { padding: 2rem; }
.period-col { flex: 0 0 210px; display: flex; flex-direction: column; gap: .4rem; }
.period-gap { flex: 0 0 auto; display: flex; align-items: flex-start; padding: 0 .3rem; min-width: .8rem; }
.events, .scenes { display: flex; flex-direction: column; gap: .35rem; }
.event-wrap { display: flex; flex-direction: column; gap: .3rem; }
.scenes { margin-left: 1rem; }

/* Tiered timeline style (html[data-timeline="tiered"], chosen per game or per player):
   rails so Events hang off their Period and Scenes off their Event. */
[data-timeline="tiered"] .events { margin-left: .9rem; padding-left: .6rem; border-left: 2px solid var(--muted); }
[data-timeline="tiered"] .scenes { margin-left: .7rem; padding-left: .5rem; border-left: 1px dashed var(--muted); }
[data-timeline="tiered"] .events:empty, [data-timeline="tiered"] .scenes:empty { display: none; }

/* Layout toggle (per viewer). */
.timeline-bar { display: flex; align-items: center; justify-content: flex-end; gap: .3rem; margin-bottom: .6rem; }
.timeline-bar .btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* Vertical layout (html[data-timeline-axis="vertical"]): Periods run top to bottom as rows,
   each Period's Events run left to right beside it, and Scenes stack under their Event. */
[data-timeline-axis="vertical"] .timeline { flex-direction: column; align-items: stretch; overflow-x: visible; padding-bottom: 0; }
[data-timeline-axis="vertical"] .period-col { flex: 0 0 auto; flex-direction: row; align-items: flex-start; gap: .6rem; }
[data-timeline-axis="vertical"] .period-col > .card-item.period { flex: 0 0 210px; }
[data-timeline-axis="vertical"] .events { flex: 1 1 auto; flex-direction: row; flex-wrap: wrap; align-items: flex-start; min-width: 0; }
[data-timeline-axis="vertical"] .event-wrap { flex: 0 0 180px; }
[data-timeline-axis="vertical"] .slot-event { writing-mode: vertical-rl; padding: .6rem .15rem; min-height: 70px; align-self: stretch; }
[data-timeline-axis="vertical"] .period-gap { padding: .3rem 0; min-width: 0; min-height: .8rem; }
[data-timeline-axis="vertical"] .slot-period { writing-mode: horizontal-tb; min-height: 0; width: 100%; padding: .2rem .4rem; }
[data-timeline-axis="vertical"][data-timeline="tiered"] .events { margin-left: 0; padding-left: .6rem; }
[data-timeline-axis="vertical"][data-timeline="tiered"] .scenes { margin-left: .4rem; }
@media (max-width: 600px) {
    [data-timeline-axis="vertical"] .period-col { flex-direction: column; align-items: stretch; }
    [data-timeline-axis="vertical"] .period-col > .card-item.period { flex-basis: auto; }
}

.card-item { border-radius: var(--radius-sm); padding: .5rem .6rem; box-shadow: var(--shadow); cursor: pointer; font-size: .9rem; position: relative; border: 2px solid var(--card-border); transition: transform .08s; }
.card-item:hover { transform: translateY(-1px); }
.card-item .label { display: block; font-size: .65rem; text-transform: uppercase; letter-spacing: .08em; opacity: .65; margin-bottom: .15rem; }
.card-item .by { display: block; font-size: .7rem; opacity: .6; margin-top: .25rem; }
.card-item .text { display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; }
.card-item.period { min-height: 110px; font-family: var(--font-heading); font-size: 1rem; }
.card-item.event { min-height: 70px; }
/* Tiered: Periods are chapter headings (heavy border, large title, ruled label); Events are lighter entries. */
[data-timeline="tiered"] .card-item.period { font-size: 1.15rem; font-weight: 600; line-height: 1.3; border-width: 3px; padding: .6rem .7rem; }
[data-timeline="tiered"] .card-item.period .label { font-size: .7rem; font-weight: 700; opacity: .8; border-bottom: 1px solid currentColor; padding-bottom: .2rem; margin-bottom: .35rem; }
[data-timeline="tiered"] .card-item.period .by { font-weight: normal; }
[data-timeline="tiered"] .card-item.event { min-height: 0; border-width: 1px; font-size: .86rem; padding: .4rem .55rem; }
.card-item.scene { border-style: dashed; font-style: italic; }
.tone-light { background: var(--light-card); color: var(--light-card-ink); }
.tone-dark { background: var(--dark-card); color: var(--dark-card-ink); border-color: var(--dark-card-border); }
.tone-pending { background: repeating-linear-gradient(45deg, var(--pending-a), var(--pending-a) 6px, var(--pending-b) 6px, var(--pending-b) 12px); color: var(--ink); }
.tone-chip { font-size: .75rem; padding: .1rem .5rem; border-radius: 999px; border: 1px solid var(--card-border); vertical-align: middle; font-family: var(--font-body); }

.slot { border: 1px dashed var(--accent); background: var(--slot-bg); color: var(--accent); border-radius: var(--radius-sm); cursor: pointer; font-size: .75rem; padding: .2rem .4rem; text-align: center; }
.slot:hover { background: var(--slot-bg-hover); }
.slot-period { writing-mode: vertical-rl; padding: .8rem .15rem; min-height: 110px; }

/* Sidebar */
.sidebar { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); }
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; background: none; border: none; padding: .55rem .3rem; cursor: pointer; font: inherit; font-size: .9rem; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--ink); border-bottom-color: var(--accent); }
.tab-body { padding: .8rem 1rem; max-height: 65vh; overflow-y: auto; }
.players, .legacies, .palette, .chat, .turn-log { list-style: none; padding: 0; margin: 0 0 .8rem; }
.players li { padding: .3rem .4rem; border-radius: var(--radius-sm); }
.players li.turn { background: var(--accent-soft); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--offline); margin-right: .3rem; }
.dot.on { background: var(--online); }
.legacies li, .palette li { padding: .2rem 0; border-bottom: 1px dotted var(--line); }
.palette.yes li::before { content: "✓ "; color: var(--ok); }
.palette.no li::before { content: "✗ "; color: var(--danger); }
.turn-log li { font-size: .85rem; padding: .25rem 0; border-bottom: 1px dotted var(--line); }
.turn-log .round-sep { font-weight: 700; font-family: var(--font-heading); border-bottom: 1px solid var(--line); margin-top: .5rem; }
.when { color: var(--muted); font-size: .72rem; }
.chat { max-height: 45vh; overflow-y: auto; }
.chat li { font-size: .9rem; padding: .2rem 0; }
.chat-form { display: flex; gap: .4rem; }
.chat-form input { margin: 0; }

/* Played scene */
.scene-panel { border: 2px solid var(--accent); }
.scene-grid { display: grid; grid-template-columns: 220px 1fr; gap: 1rem; }
@media (max-width: 800px) { .scene-grid { grid-template-columns: 1fr; } }
.scene-characters { list-style: none; padding: 0; }
.scene-characters li { padding: .3rem 0; border-bottom: 1px dotted var(--line); }
.scene-log { height: 300px; overflow-y: auto; background: var(--field); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: .6rem; margin-bottom: .6rem; }
.msg { padding: .25rem 0; }
.msg.system { color: var(--muted); font-size: .85rem; font-style: italic; text-align: center; }
.msg.thought { color: var(--thought-ink); }
.msg.narration { color: var(--narration-ink); }
.push { margin-top: .6rem; }
.push.open { background: var(--notice-bg); border: 1px solid var(--notice-line); border-radius: var(--radius-sm); padding: .5rem .8rem; }
.push.open ul { padding-left: 1rem; }
.complete-scene { margin-top: .8rem; }
.complete-scene summary { cursor: pointer; color: var(--accent); font-weight: 600; }

/* Dialogs */
.dialog { border: none; border-radius: 10px; padding: 1.2rem 1.4rem; width: min(560px, 95vw); box-shadow: 0 10px 40px rgba(0,0,0,.3); background: var(--paper); }
.dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dialog-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; }
.item-fields.nested { border-top: 1px solid var(--line); margin-top: .8rem; padding-top: .6rem; }
.scene-type { margin-bottom: .6rem; }

/* How To Play guide */
.guide { max-width: 860px; margin: 0 auto; padding: 1.5rem 2rem; }
.guide h1 { font-size: 2rem; }
.guide h2 { margin-top: 2rem; padding-top: .5rem; border-top: 1px solid var(--line); scroll-margin-top: 1rem; }
.guide h3 { margin-top: 1.4rem; scroll-margin-top: 1rem; }
.guide .heading-permalink { margin-left: .4rem; color: var(--line); text-decoration: none; font-size: .8em; }
.guide h2:hover .heading-permalink, .guide h3:hover .heading-permalink { color: var(--accent); }
.guide table { border-collapse: collapse; width: 100%; margin: .8rem 0; font-size: .92rem; }
.guide th, .guide td { border: 1px solid var(--line); padding: .4rem .6rem; text-align: left; vertical-align: top; }
.guide th { background: var(--subtle); }
.guide tr:nth-child(even) td { background: var(--stripe); }
.guide pre { background: var(--code-bg); color: var(--code-ink); padding: .9rem 1rem; border-radius: var(--radius); overflow-x: auto; font-size: .8rem; line-height: 1.3; }
.guide code { font-family: var(--font-mono); }
.guide blockquote { margin: 1rem 0; padding: .6rem 1rem; background: var(--accent-tint); border-left: 4px solid var(--accent); border-radius: 0 var(--radius) var(--radius) 0; }
.guide blockquote p { margin: 0; }
.guide hr { border: none; margin: 1rem 0; }
.guide li { margin-bottom: .25rem; }
.guide-top { text-align: right; font-size: .9rem; }
@media (max-width: 600px) { .guide { padding: 1rem; } .guide table { display: block; overflow-x: auto; } }

/* Theme picker (host, on the board) */
.theme-form { display: flex; align-items: center; gap: .3rem; }
.theme-form label { margin: 0; font-weight: normal; }
.theme-form select { width: auto; margin: 0; padding: .2rem .4rem; font-size: .85rem; }
.theme-form.js-autosubmit button { display: none; }

/* Pause and archive: host bar, and hide move controls the server would refuse. */
.is-frozen #scene-panel form,
.is-frozen #scene-panel [data-vote],
.is-frozen [data-end-vote] { display: none !important; }

/* Host panel */
.host-panel summary { cursor: pointer; display: flex; align-items: baseline; gap: .6rem; flex-wrap: wrap; }
.host-panel-title { font-family: var(--font-heading); font-size: 1.1rem; font-weight: 600; }
.host-panel[open] > summary { margin-bottom: .5rem; }
.host-archive { margin-top: .6rem; }
.host-archive > summary { cursor: pointer; color: var(--danger, #a33); }
.host-section { border-top: 1px solid var(--line); padding-top: .6rem; margin-top: .6rem; }
.host-section h3 { margin: 0 0 .4rem; font-size: .95rem; }
.host-section p { margin: .3rem 0; }
.host-players { list-style: none; padding: 0; margin: 0; }
.host-players li { display: flex; align-items: center; gap: .4rem; padding: .25rem 0; flex-wrap: wrap; }
.host-player-actions { margin-left: auto; display: flex; gap: .4rem; }
.host-form input[name=reason] { flex: 1 1 14rem; min-width: 0; }
