:root {
    --bg: #f3f5f8;
    --surface: #ffffff;
    --text: #1d2733;
    --muted: #66768a;
    --line: #dde3ea;
    --accent: #2f6fd6;
    --accent-text: #ffffff;
    --you: #1f8a4c;
    --warn: #a86400;
    --lost: #c62f2f;
    --button: #eef2f6;
    --button-hover: #e2e8ef;
    --panel-w: 300px;
    --topbar-h: 52px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14181d;
        --surface: #1d232a;
        --text: #e6ebf1;
        --muted: #93a1b2;
        --line: #2c343d;
        --accent: #5b93ec;
        --you: #4cc282;
        --warn: #e0a040;
        --lost: #f07070;
        --button: #262e37;
        --button-hover: #303943;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

/* The attribute wins over display rules such as .buttons { display: flex }. */
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    min-height: 100vh;
    min-height: 100svh;
}

.topbar {
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 max(16px, env(safe-area-inset-left));
    border-bottom: 1px solid var(--line);
    background: var(--surface);
}
.topbar h1 { font-size: 17px; font-weight: 650; margin: 0; letter-spacing: 0.01em; }
.tag {
    font-size: 12px;
    color: var(--muted);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 1px 8px;
}
.topbar h1 a { color: inherit; text-decoration: none; }
.nav { margin-left: auto; }
/* The trainer's rating, right of the title and left of the nav. */
.rating { margin-left: auto; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.rating:not([hidden]) ~ .nav { margin-left: 12px; }
a { color: var(--accent); }

/* Text pages (about.html, roast.html). */
.page { max-width: 680px; margin: 0 auto; padding: 24px max(16px, env(safe-area-inset-left)); }
.page h2 { font-size: 20px; margin: 0 0 12px; }
.page h3 { font-size: 16px; margin: 24px 0 8px; }
.page p, .page ul { margin: 0 0 12px; }

/* Board as large as fits next to the panel and below the top bar. --side: what sits beside
   the board in its column (the trainer's eval bar, .layout.with-bar). */
.layout {
    --gap: 24px;
    --side: 0px;
    --board: min(100vw - var(--panel-w) - 3 * var(--gap) - var(--side), 100vh - var(--topbar-h) - 2 * var(--gap), 760px);
    display: grid;
    grid-template-columns: calc(var(--board) + var(--side)) var(--panel-w);
    gap: var(--gap);
    justify-content: center;
    align-items: start;
    padding: var(--gap);
}
@supports (height: 100svh) {
    .layout { --board: min(100vw - var(--panel-w) - 3 * var(--gap) - var(--side), 100svh - var(--topbar-h) - 2 * var(--gap), 760px); }
}

.board-area { min-width: 0; }

.panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    max-height: var(--board);
    min-height: min(var(--board), 420px);
}

.status {
    font-weight: 600;
    font-size: 16px;
    min-height: 1.45em;
}
.status[data-state="you"] { color: var(--you); }
.status[data-state="them"] { color: var(--muted); }

.buttons { display: flex; gap: 8px; }

button {
    font: inherit;
    font-size: 14px;
    color: var(--text);
    background: var(--button);
    border: 1px solid var(--line);
    border-radius: 7px;
    padding: 7px 12px;
    cursor: pointer;
    flex: 1;
    white-space: nowrap;
}
button:hover { background: var(--button-hover); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.moves {
    flex: 1;
    min-height: 60px;
    overflow-y: auto;
    margin: 0;
    padding: 6px 0;
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    list-style: none;
    counter-reset: none;
    font-variant-numeric: tabular-nums;
}
.moves li {
    display: grid;
    grid-template-columns: 2.6em 1fr 1fr;
    padding: 2px 4px;
    border-radius: 5px;
}
.moves li::before {
    content: attr(value) ".";
    color: var(--muted);
}
.moves li:nth-child(odd) { background: color-mix(in srgb, var(--button) 60%, transparent); }
.move { padding: 0 6px; border-radius: 4px; }
.move[data-ply] { cursor: pointer; }
@media (hover: hover) { .move[data-ply]:hover:not(.current) { background: var(--button-hover); } }
/* The trainee's blunders after a round (OS.MoveList marks); the current move's colours win. */
.move.blunder { color: var(--lost); font-weight: 600; }
.move.slip { color: var(--warn); font-weight: 600; }
.move.current { background: var(--accent); color: var(--accent-text); }
.move.placeholder { color: var(--muted); }

.hint { color: var(--muted); font-size: 13px; margin: 0; }

/* Trainer page (index.html). */
.trainer-info { color: var(--muted); font-size: 14px; margin: 0; }
/* While a round runs, the survival panel (and a review round's controls) replace everything
   else in the panel. */
.panel.playing > :not(.survival, .review) { display: none; }
.survival { display: flex; flex-direction: column; gap: 8px; font-size: 15px; }
.survival p { margin: 0; }
.survival p:first-child { font-weight: 600; font-size: 16px; }
.survival .warn { color: var(--warn); }
.survival .lost { color: var(--lost); font-weight: 600; }
.survival .good { color: var(--you); font-weight: 600; }
.survival .fair { color: var(--warn); font-weight: 600; }
.survival .rated { color: var(--muted); font-size: 14px; }
.review { display: flex; flex-direction: column; gap: 8px; }
.review[hidden] { display: none; }
/* The panel column: the trainer panel, the history panel below it. */
.side { display: flex; flex-direction: column; gap: var(--gap); min-width: 0; }
/* The trainee's rounds (OS.RoundHistory) in their own panel, as tall as its rows; hidden
   while a round runs, as everything but the survival panel. A click on a row brings the
   round on the board. */
.panel.history { min-height: 0; max-height: none; gap: 10px; }
.panel.playing + .history { display: none; }
.history-title { margin: 0; font-size: 16px; font-weight: 650; }
.history-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 14px;
    font-variant-numeric: tabular-nums;
}
.history-table th {
    text-align: left;
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
    padding: 0 6px 4px;
    border-bottom: 1px solid var(--line);
}
.history-table td { padding: 3px 6px; white-space: nowrap; }
.history-table th:last-child, .history-table td:last-child { text-align: right; }
.history-table td:first-child { border-radius: 5px 0 0 5px; color: var(--muted); }
.history-table td:last-child { border-radius: 0 5px 5px 0; }
.history-table tbody tr { cursor: pointer; }
.history-table tbody tr:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.history-table tbody tr:nth-child(odd) td { background: color-mix(in srgb, var(--button) 60%, transparent); }
@media (hover: hover) { .history-table tbody tr:hover td { background: var(--button-hover); } }
/* A rating the round didn't move (a review round): the last rated round's. */
.history-table td.unrated { color: var(--muted); }
.history-table tbody tr.current td { background: var(--accent); color: var(--accent-text); }

/* Eval bar (OS.EvalBar) left of the board: the trainee's share in the panel lines' colours,
   the opponent's gray. The bar stretches to the board's height. */
.layout.with-bar { --bar-w: 14px; --bar-gap: 8px; --side: calc(var(--bar-w) + var(--bar-gap)); }
.layout.with-bar .board-area {
    display: grid;
    grid-template-columns: var(--bar-w) minmax(0, 1fr);
    column-gap: var(--bar-gap);
}
.evalbar {
    --bar-them: #a3adb8;
    position: relative;
    overflow: hidden;
    border-radius: 4px;
    background: var(--bar-them);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.18);
}
/* Hidden while a review round asks its question; keeps its place, so the board stays put. */
.evalbar.off { visibility: hidden; }
.evalbar-fill {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 50%;
    background: var(--muted);
    transition: height 0.4s ease, background-color 0.4s ease;
}
/* The trainee's pieces at the top (a flipped board): their share hangs from the top. */
.evalbar.top .evalbar-fill { top: 0; bottom: auto; }
.evalbar-fill.good { background: var(--you); }
.evalbar-fill.warn { background: var(--warn); }
.evalbar-fill.lost { background: var(--lost); }
/* Even: a tick across the middle. */
.evalbar::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    top: calc(50% - 1px);
    height: 2px;
    background: color-mix(in srgb, var(--surface) 70%, transparent);
}
@media (prefers-color-scheme: dark) {
    .evalbar { --bar-them: #4b5561; }
}

/* ---- narrow screens: board full width on top, panel below */

@media (max-width: 760px) {
    .layout {
        --gap: 12px;
        grid-template-columns: minmax(0, 1fr);
        padding: var(--gap) 0 calc(var(--gap) + env(safe-area-inset-bottom));
    }
    .board-area {
        width: min(100%, 100vh - var(--topbar-h) - 120px);
        justify-self: center;
    }
    .panel {
        margin: 0 16px;
        max-height: none;
        min-height: 0;
    }
    .moves { max-height: 30vh; }
}

/* Phones: edge-to-edge board for the biggest squares. */
@media (max-width: 520px) {
    .layout { padding-top: 0; }
    .layout.with-bar { --bar-w: 10px; --bar-gap: 4px; }
    .board-area { width: 100%; }
    .board-area .osb { border-radius: 0; box-shadow: none; }
    .evalbar { border-radius: 0; box-shadow: none; }
}

/* Socket test page (socket.html). */
.socket-buttons { margin: 16px 0 12px; max-width: 560px; }
button:disabled { opacity: 0.5; cursor: default; }
button:disabled:hover { background: var(--button); }
.socket-log {
    list-style: none;
    margin: 12px 0 0;
    padding: 6px 0;
    border-top: 1px solid var(--line);
    font: 13px/1.5 ui-monospace, "Cascadia Mono", Consolas, monospace;
    overflow-wrap: anywhere;
}
.socket-time { color: var(--muted); }

/* Settings page (settings.html). */
.nav a + a { margin-left: 14px; }
.settings section { margin-bottom: 32px; }
.settings h4 { font-size: 15px; margin: 22px 0 8px; }
.settings .form { display: flex; flex-direction: column; gap: 10px; max-width: 420px; margin: 0 0 12px; }
.settings .form p { margin: 0; }
.settings .buttons { max-width: 420px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.form label.check { flex-direction: row; align-items: center; gap: 8px; }
.form input:not([type="checkbox"]) {
    font: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 7px;
    padding: 7px 10px;
}
.form input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.settings .result { margin: 0 0 12px; font-size: 14px; color: var(--you); }
.settings .result:empty { display: none; }
.settings .result.error { color: var(--lost); }
.settings .warning { color: var(--warn); font-size: 14px; }
.settings details { margin: 16px 0; }
.settings summary { cursor: pointer; color: var(--accent); margin-bottom: 10px; }
button.danger { color: var(--lost); }
