:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1f2421;
  --muted: #6b716c;
  --line: #dedcd3;
  --accent: #3d7a4f;
  --win: #2f7d46;
  --loss: #b3412f;
  --draw: #8a7a3a;
  --inaccuracy: #a8861c;
  --mistake: #c96a1b;
  --blunder: #b3412f;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161917;
    --surface: #1f2421;
    --text: #e8e9e4;
    --muted: #9aa19b;
    --line: #333a35;
    --accent: #7fbf8f;
    --win: #7fbf8f;
    --loss: #e08a7a;
    --draw: #cdbb72;
    --inaccuracy: #d9bd5c;
    --mistake: #eb9a55;
    --blunder: #e9806f;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px 24px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
header h1 { margin: 0; font-size: 18px; }
nav { display: flex; gap: 16px; }
nav a { color: var(--muted); text-decoration: none; padding: 2px 0; }
nav a.active { color: var(--text); border-bottom: 2px solid var(--accent); }
main { max-width: 1100px; margin: 0 auto; padding: 16px; }
.summary { color: var(--muted); margin: 0 0 12px; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
select, button {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
}
button { cursor: pointer; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; white-space: nowrap; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
td.num { font-variant-numeric: tabular-nums; }
td.opening { white-space: normal; min-width: 180px; color: var(--muted); }
.win { color: var(--win); font-weight: 600; }
.loss { color: var(--loss); font-weight: 600; }
.draw { color: var(--draw); font-weight: 600; }
.piece { display: inline-block; width: 11px; height: 11px; border-radius: 50%; border: 1px solid var(--muted); margin-right: 6px; vertical-align: -1px; }
.piece.white { background: #fff; }
.piece.black { background: #222; }
td a { color: var(--accent); }
#more { display: block; margin: 12px auto; }
.empty { padding: 32px 16px; color: var(--muted); text-align: center; }

.muted { color: var(--muted); }
.inaccuracy { color: var(--inaccuracy); }
.mistake { color: var(--mistake); }
.blunder { color: var(--blunder); }
.count { display: inline-block; min-width: 22px; font-weight: 600; }

/* Review page */
.game-head h2 { margin: 0 0 2px; font-size: 17px; font-weight: 600; }
.game-head a, .neighbours a { color: var(--accent); }
.review { display: flex; flex-wrap: wrap; gap: 20px; align-items: flex-start; }
.board-col { --size: min(calc(100vw - 32px - 26px), 560px); width: calc(var(--size) + 26px); }
.board-row { display: flex; gap: 8px; }
#board { width: var(--size); height: var(--size); }
#bar { width: 18px; height: var(--size); background: #2b2b2b; border-radius: 4px; overflow: hidden; display: flex; flex-direction: column-reverse; border: 1px solid var(--line); }
#bar.flipped { flex-direction: column; }
#bar-white { background: #f2f2ee; height: 50%; transition: height 0.2s; }
#graph { display: block; width: var(--size); height: 64px; margin: 10px 0 0 26px; border-radius: 4px; border: 1px solid var(--line); cursor: pointer; }
#graph line { vector-effect: non-scaling-stroke; }
.g-black { fill: #2b2b2b; }
.g-white { fill: #f2f2ee; }
.g-mid { stroke: #888; stroke-width: 1; stroke-dasharray: 3 3; }
.g-mark { stroke-width: 2; opacity: 0.85; }
.g-mark.mistake { stroke: #e08a2e; }
.g-mark.blunder { stroke: #d6452f; }
.g-cursor { stroke: #3d8bd6; stroke-width: 2; }
.side { flex: 1 1 300px; min-width: 0; max-width: 460px; }
.eval-line { margin-bottom: 6px; }
#eval-text { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
#info { min-height: 76px; }
#info p { margin: 0 0 4px; }
.tag { font-weight: 600; }
.controls { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 12px; }
.controls button { min-width: 44px; min-height: 38px; }
#moves {
  display: grid;
  grid-template-columns: 40px 1fr 1fr;
  gap: 2px 4px;
  max-height: 340px;
  overflow-y: auto;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  position: relative;
}
#moves .num { color: var(--muted); padding: 4px 0; font-variant-numeric: tabular-nums; }
#moves button { border: 0; background: none; text-align: left; padding: 4px 8px; border-radius: 4px; font-weight: 500; }
#moves button.inaccuracy, #moves button.mistake, #moves button.blunder { font-weight: 700; }
#moves button:hover { background: var(--bg); }
#moves button.active { background: var(--accent); color: var(--surface); }
.neighbours { display: flex; justify-content: space-between; margin-top: 12px; }

/* Train page */
.train-col { --size: min(calc(100vw - 32px), 560px); width: var(--size); }
#prompt { font-size: 17px; font-weight: 600; margin: 0 0 4px; }
#result { min-height: 48px; margin-bottom: 4px; }
#result p { margin: 0 0 4px; }
#result .solved { color: var(--win); font-weight: 600; }
#result .failed { color: var(--loss); font-weight: 600; }
.train-done { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; margin-bottom: 16px; }

/* Stats page */
.weak { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 12px 16px; margin-bottom: 16px; }
.weak h2, .chart-card h2 { font-size: 16px; margin: 0 0 8px; }
.weak ul { margin: 0 0 8px; padding-left: 20px; }
.weak li { margin-bottom: 4px; }
.weak p { margin: 0; font-size: 14px; }
.chart-grid { display: grid; grid-template-columns: 1fr; gap: 16px; }
.chart-card { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 12px 16px; min-width: 0; }
.chart-card.wide { grid-column: 1 / -1; }
.chart-card .muted { margin: 0 0 8px; font-size: 14px; }
.chart-box { position: relative; width: 100%; }
@media (min-width: 800px) {
  .chart-grid { grid-template-columns: 1fr 1fr; }
}
