/* ---------- Tokens ---------- */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;

  /* players */
  --p1: #4a3aa7;          /* X / violet disc */
  --p2: #c98500;          /* O / amber disc  */
  --p2-disc: #eda100;
  --board-c4: #dfe6f0;
  --hole: #fcfcfb;

  /* diverging heat: loss (red) - neutral - win (blue) */
  --heat-neg: #e34948;
  --heat-mid: #f0efec;
  --heat-pos: #2a78d6;
  /* sequential (visits) */
  --seq-lo: #cde2fb;
  --seq-hi: #184f95;

  /* status */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --good-ink: #006300;

  /* categorical series */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948; --s9: #898781;

  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232322;
    --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,0.10);
    --accent: #3987e5;
    --p1: #9085e9; --p2: #e0a526; --p2-disc: #c98500; --board-c4: #26303d; --hole: #1a1a19;
    --heat-neg: #e66767; --heat-mid: #383835; --heat-pos: #3987e5;
    --seq-lo: #1c3a5e; --seq-hi: #86b6ef;
    --good-ink: #0ca30c;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767; --s9: #898781;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232322;
  --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,0.10);
  --accent: #3987e5;
  --p1: #9085e9; --p2: #e0a526; --p2-disc: #c98500; --board-c4: #26303d; --hole: #1a1a19;
  --heat-neg: #e66767; --heat-mid: #383835; --heat-pos: #3987e5;
  --seq-lo: #1c3a5e; --seq-hi: #86b6ef;
  --good-ink: #0ca30c;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767; --s9: #898781;
  --shadow: none;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--page); color: var(--ink); font-size: 14px; line-height: 1.45; }
h2, h3 { margin: 0; font-weight: 650; }
h2 { font-size: 18px; }
h3 { font-size: 14px; margin-bottom: 10px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
button, select, input { font: inherit; color: inherit; }
b { font-weight: 650; }

.topbar {
  display: flex; align-items: center; gap: 16px; padding: 12px 20px;
  border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.topbar .seg { margin-left: auto; }

.layout {
  display: grid; grid-template-columns: minmax(340px, 480px) minmax(0, 1fr);
  gap: 16px; padding: 16px 20px 40px; max-width: 1400px; margin: 0 auto;
}
.col-play { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px 32px; }
  /* single column: board, then the trainer, then setup and review */
  .col-play { display: contents; }
  .board-card { order: 1; }
  .col-trainer { order: 2; }
  .settings { order: 3; }
  .review { order: 4; }
  .topbar { flex-wrap: wrap; padding: 10px 16px; }
}

/* ---------- Controls ---------- */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button {
  border: 0; background: transparent; padding: 6px 12px; border-radius: 7px; cursor: pointer;
  color: var(--ink-2); font-weight: 550; white-space: nowrap;
}
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 0 0 1px var(--border), 0 1px 2px rgba(0,0,0,.08); }
.seg.small button { padding: 5px 10px; font-size: 13px; }
.seg.small { display: flex; flex-wrap: wrap; }
.seg.small button { flex: 1; }

.btn {
  border: 1px solid var(--border); background: var(--surface-2); padding: 6px 12px; border-radius: 8px;
  cursor: pointer; font-weight: 550;
}
.btn:hover { filter: brightness(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn:disabled { opacity: .45; cursor: default; }
.btn-row { display: flex; gap: 8px; }
.icon-btn { border: 1px solid var(--border); background: var(--surface-2); border-radius: 8px; padding: 6px; cursor: pointer; display: grid; place-items: center; }

.set-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.span2 { grid-column: span 2; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--ink-2); }
.field b { color: var(--ink); }
.field select { padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); }
.field input[type=range] { width: 100%; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--ink-2); cursor: pointer; }
.check input { accent-color: var(--accent); }
.hint { font-style: normal; color: var(--muted); font-size: 12px; }
.hidden { display: none !important; }

/* ---------- Board ---------- */
.status-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.status { font-size: 16px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.status .dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }

.board-wrap { display: flex; gap: 12px; align-items: stretch; justify-content: center; }
.board-area { flex: 1; max-width: 400px; min-width: 0; }

.evalbar {
  width: 14px; border-radius: 7px; background: var(--p2-disc); position: relative; overflow: hidden; flex: none;
  border: 1px solid var(--border);
}
.evalbar-fill { position: absolute; left: 0; right: 0; top: 0; background: var(--p1); transition: height .4s ease; height: 50%; }
.evalbar-top, .evalbar-bot { display: none; }

.board { display: grid; gap: 6px; user-select: none; position: relative; }
.board.ttt { aspect-ratio: 1; }
.cell {
  position: relative; border: 0; border-radius: 10px; background: var(--surface-2); cursor: pointer;
  display: grid; place-items: center; padding: 0; overflow: hidden; transition: background .25s, box-shadow .15s;
}
.cell:disabled { cursor: default; }
.cell svg.glyph { width: 62%; height: 62%; }
.cell .heat { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; pointer-events: none; }
.cell .heat .v { font-weight: 700; font-size: 17px; font-variant-numeric: tabular-nums; }
.cell .heat .sub { font-size: 11px; opacity: .85; font-variant-numeric: tabular-nums; }
.cell.best { box-shadow: inset 0 0 0 3px var(--ink); }
.cell.hl { box-shadow: inset 0 0 0 3px var(--accent); }
.cell.win-cell { box-shadow: inset 0 0 0 3px var(--good); }
.cell.last::after { content: ""; position: absolute; top: 6px; right: 6px; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }

/* connect four */
.board.c4 { background: var(--board-c4); padding: 8px; border-radius: 14px; gap: 5px; }
.board.c4 .cell { border-radius: 50%; aspect-ratio: 1; background: var(--hole); }
.board.c4 .cell .disc { width: 100%; height: 100%; border-radius: 50%; }
.board.c4 .cell .heat .v { font-size: 12px; }
.board.c4 .cell .heat .sub { display: none; }
.board.c4.hovering .cell.colhover { filter: brightness(0.96); }
.board.c4 .cell.best { box-shadow: inset 0 0 0 3px var(--ink); }
.board.c4 .cell.last::after { top: 38%; right: 38%; width: 24%; height: 24%; background: rgba(255,255,255,.7); }
.board.c4 .cell.ghost .disc { opacity: .35; }

/* gomoku: a 15x15 grid of intersections */
.board.gomoku { gap: 1px; background: var(--axis); padding: 1px; border-radius: 6px; }
.board.gomoku .cell { border-radius: 0; aspect-ratio: 1; background: var(--surface-2); }
.board.gomoku .cell .disc { width: 84%; height: 84%; border-radius: 50%; }
.board.gomoku .cell .heat .v { font-size: 11px; }
.board.gomoku .cell.best { box-shadow: inset 0 0 0 2px var(--ink); }
.board.gomoku .cell.hl { box-shadow: inset 0 0 0 2px var(--accent); }
.board.gomoku .cell.last::after { top: 50%; right: auto; left: 50%; width: 6px; height: 6px; transform: translate(-50%, -50%); background: rgba(255,255,255,.85); }
.board.gomoku .cell:not(:disabled):hover { background: var(--grid); }
.board-area.wide { max-width: 480px; }

/* ---- game won: line through the winning pieces, everything else steps back ---- */
.board.won .cell:not(.win-cell) > .disc, .board.won .cell:not(.win-cell) > svg.glyph { opacity: .28; transition: opacity .4s; }
.board .cell.win-cell .disc { box-shadow: inset 0 0 0 3px rgba(255,255,255,.9); animation: win-pop .5s ease-out both; }
.board.gomoku .cell.win-cell .disc { box-shadow: inset 0 0 0 2px rgba(255,255,255,.9); }
.board.c4 .cell.win-cell, .board.gomoku .cell.win-cell { box-shadow: none; }
.board .cell.win-cell svg.glyph { animation: win-pop .5s ease-out both; }
.win-line { position: absolute; left: 0; top: 0; pointer-events: none; overflow: visible; }
.win-line line { stroke-linecap: round; stroke-dasharray: var(--len); stroke-dashoffset: var(--len); animation: win-draw .55s .15s ease-out forwards; }
.win-line .halo { stroke: var(--surface); opacity: .9; }
.win-line .stroke { stroke: var(--good); }
@keyframes win-draw { to { stroke-dashoffset: 0; } }
@keyframes win-pop { 0% { transform: scale(.7); } 60% { transform: scale(1.12); } 100% { transform: scale(1); } }
@media (prefers-reduced-motion: reduce) {
  .win-line line { animation: none; stroke-dashoffset: 0; }
  .board .cell.win-cell .disc, .board .cell.win-cell svg.glyph { animation: none; }
}
.status .win-note { font-size: 13px; font-weight: 500; color: var(--ink-2); }

.col-heads { display: grid; gap: 5px; padding: 0 8px; margin-bottom: 6px; }
.col-head {
  border-radius: 6px; text-align: center; font-size: 12px; font-weight: 650; padding: 4px 0;
  font-variant-numeric: tabular-nums; border: 0; cursor: pointer; background: var(--surface-2); color: var(--ink-2);
}
.col-head.best { box-shadow: inset 0 0 0 2px var(--ink); }
.col-head.empty { visibility: hidden; }

.heat-legend { margin-top: 14px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); }
.heat-legend .ramp { height: 10px; width: 150px; border-radius: 5px; }
.heat-legend .ends { display: flex; justify-content: space-between; width: 150px; color: var(--muted); font-size: 11px; }
.heat-legend .metric { margin-left: auto; }

/* ---------- Review ---------- */
.move-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; max-height: 320px; overflow: auto; }
.move-list li { display: grid; grid-template-columns: 28px 1fr auto; gap: 8px; align-items: center; padding: 6px 8px; border-radius: 8px; background: var(--surface-2); font-size: 13px; }
.move-list li .num { color: var(--muted); font-variant-numeric: tabular-nums; }
.move-list li .detail { grid-column: 2 / 4; color: var(--ink-2); font-size: 12px; }
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); white-space: nowrap; }
.badge i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.eval-graph { height: 110px; }
.pchip { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: -1px; margin-right: 5px; }

/* ---------- Trainer ---------- */
.col-trainer { display: flex; flex-direction: column; gap: 14px; }
.trainer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.progress-wrap { min-width: 220px; }
.progress-label { font-size: 12px; color: var(--ink-2); text-align: right; margin-bottom: 4px; font-variant-numeric: tabular-nums; }
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width .15s linear; }

.verdict { border-radius: 10px; padding: 12px 14px; background: var(--surface-2); display: flex; gap: 14px; align-items: center; }
.verdict .big { font-size: 20px; font-weight: 700; }
.verdict .mini { flex: none; }
.verdict p { margin: 2px 0 0; color: var(--ink-2); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tabs button { border: 0; background: none; padding: 8px 12px; cursor: pointer; color: var(--ink-2); font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); }
.tab-panel { display: none; }
.tab-panel.on { display: block; }
.tab-panel h4 { margin: 18px 0 8px; font-size: 13px; font-weight: 650; }
.tab-panel h4:first-child { margin-top: 4px; }
.tab-panel p { margin: 6px 0; }

.tactics { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.tactics li { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border-radius: 8px; background: var(--surface-2); }
.tactics li .ico { flex: none; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 12px; color: #fff; }
.narrative p { color: var(--ink-2); }
.narrative b { color: var(--ink); }

/* move ranking */
.rank { display: flex; flex-direction: column; gap: 3px; }
.rank-row {
  display: grid; grid-template-columns: 22px minmax(92px, 130px) minmax(0, 1fr) 84px minmax(60px, 90px);
  gap: 10px; align-items: center; padding: 5px 8px; border-radius: 8px; cursor: default; font-size: 13px;
}
.rank-row:hover, .rank-row.hl { background: var(--surface-2); }
.rank-row .rk { color: var(--muted); font-variant-numeric: tabular-nums; }
.rank-row .nm { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rank-row .val { font-weight: 650; font-variant-numeric: tabular-nums; text-align: right; }
.rank-row .aux { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; text-align: right; }
.rank-head { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.rank-head:hover { background: none; }
.bar-track { position: relative; height: 12px; background: var(--surface-2); border-radius: 4px; }
.rank-row:hover .bar-track, .rank-row.hl .bar-track { background: var(--page); }
.bar-track .mid { position: absolute; left: 50%; top: -3px; bottom: -3px; width: 1px; background: var(--axis); }
.bar-track .fill { position: absolute; top: 0; bottom: 0; border-radius: 4px; }
.star { color: var(--ink); }

/* tree explorer */
.pv { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; align-items: flex-start; }
.pv .step { display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 11px; color: var(--ink-2); flex: none; text-align: center; }
.pv .arrow { align-self: center; color: var(--muted); flex: none; margin-top: -14px; }
.crumbs { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; font-size: 12px; margin: 4px 0 10px; }
.crumbs button { border: 1px solid var(--border); background: var(--surface-2); border-radius: 6px; padding: 2px 8px; cursor: pointer; }
.crumbs button.cur { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.miller { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 8px; }
.mcol { flex: none; width: 212px; display: flex; flex-direction: column; gap: 6px; }
.mcol-head { font-size: 12px; color: var(--ink-2); min-height: 34px; }
.mcol-head b { color: var(--ink); }
.tnode {
  display: grid; grid-template-columns: auto 1fr; gap: 8px; align-items: center; text-align: left;
  border: 1px solid var(--border); background: var(--surface); border-radius: 9px; padding: 6px; cursor: pointer;
}
.tnode:hover { background: var(--surface-2); }
.tnode.sel { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.tnode.pvnode .tlabel::after { content: " ★"; color: var(--ink); }
.tnode .tlabel { font-weight: 600; font-size: 12px; }
.tnode .tval { font-size: 12px; font-weight: 650; font-variant-numeric: tabular-nums; display: flex; align-items: center; gap: 6px; }
.tnode .tval .sw { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.tnode .tsub { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tnode .nbar { height: 4px; border-radius: 2px; background: var(--surface-2); margin-top: 3px; overflow: hidden; }
.tnode .nbar > div { height: 100%; background: var(--accent); border-radius: 2px; }
.tnote { font-size: 12px; color: var(--muted); padding: 6px 2px; }

/* charts */
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart text.dl { font-weight: 650; fill: var(--ink-2); }
.chart .series { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; transition: opacity .15s; }
.chart .series.dim { opacity: .18; }
.chart .series.focus { stroke-width: 3; }
.chart .xhair { stroke: var(--axis); stroke-width: 1; }
.chart .hit { fill: transparent; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; margin: 6px 0; }
.legend button { border: 0; background: none; padding: 2px 4px; cursor: pointer; color: var(--ink-2); display: inline-flex; align-items: center; gap: 5px; border-radius: 5px; }
.legend button:hover, .legend button.on { background: var(--surface-2); color: var(--ink); }
.legend .sw { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }

.tooltip {
  position: fixed; pointer-events: none; z-index: 20; background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 12px;
  box-shadow: 0 4px 18px rgba(0,0,0,.14); opacity: 0; transition: opacity .1s; max-width: 280px;
}
.tooltip.on { opacity: 1; }
.tooltip table { border-collapse: collapse; }
.tooltip td { padding: 1px 4px; font-variant-numeric: tabular-nums; }
.tooltip td:last-child { text-align: right; font-weight: 600; }

.stat-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin: 8px 0; }
.tile { background: var(--surface-2); border-radius: 9px; padding: 8px 10px; }
.tile .k { font-size: 11px; color: var(--muted); }
.tile .v { font-size: 18px; font-weight: 700; }

/* how it works */
.steps { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 8px; margin: 8px 0 4px; }
.step-card { background: var(--surface-2); border-radius: 9px; padding: 10px; font-size: 12px; color: var(--ink-2); }
.step-card b { display: block; color: var(--ink); font-size: 13px; margin-bottom: 2px; }
.step-card .n { display: inline-grid; place-items: center; width: 18px; height: 18px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); font-size: 11px; font-weight: 700; margin-right: 6px; }
.formula { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; background: var(--surface-2); padding: 8px 10px; border-radius: 8px; font-size: 13px; overflow-wrap: anywhere; }
.ucb-row { display: grid; grid-template-columns: 110px minmax(0, 1fr) 150px; gap: 10px; align-items: center; font-size: 12px; padding: 3px 0; }
.ucb-row .nm { font-weight: 600; }
.ucb-row .num { font-variant-numeric: tabular-nums; color: var(--ink-2); text-align: right; }
.ucb-track { display: flex; height: 12px; gap: 2px; }
.ucb-track > div { height: 100%; }
.ucb-track > div:first-child { border-radius: 4px 0 0 4px; }
.ucb-track > div:last-child { border-radius: 0 4px 4px 0; }
.key { display: inline-flex; align-items: center; gap: 5px; margin-right: 14px; font-size: 12px; color: var(--ink-2); }
.key i { width: 12px; height: 10px; border-radius: 2px; display: inline-block; }
.line-walk { display: flex; flex-direction: column; gap: 6px; }
.line-walk .lw { display: flex; gap: 10px; align-items: center; font-size: 12px; color: var(--ink-2); }
.line-walk .lw b { color: var(--ink); }

@media (max-width: 560px) {
  .rank-row { grid-template-columns: 18px 84px minmax(0, 1fr) 64px; }
  .rank-row .aux { display: none; }
  .ucb-row { grid-template-columns: 70px minmax(0, 1fr); }
  .ucb-row .num { display: none; }
  .progress-wrap { min-width: 0; width: 100%; }
  .progress-label { text-align: left; }
}
