/* ============================================================
   Chess Coach — dark, dense, no gradients.
   ============================================================ */

:root {
  --bg: #111418;
  --panel: #1c2128;
  --panel-2: #232a33;
  --line: #2c343f;
  --text: #e6e6e6;
  --muted: #8a9199;
  --accent: #4f9cff;

  --sq-light: #f0d9b5;
  --sq-dark: #b58863;

  --c-best: #27ae60;
  --c-good: #8a9199;
  --c-inaccuracy: #e6b800;
  --c-mistake: #e67e22;
  --c-blunder: #e74c3c;

  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0 0 8px; font-weight: 600; line-height: 1.25; }
h1 { font-size: 20px; }
h2 { font-size: 17px; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
h4 { font-size: 14px; }
p { margin: 0 0 8px; }
a { color: var(--accent); }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.center { text-align: center; }

/* ---------------------------------------------------------- boot overlay */
.boot-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
}
.boot-box { text-align: center; max-width: 560px; padding: 24px; }
.boot-logo { font-size: 52px; line-height: 1; color: var(--accent); }
.boot-box h1 { margin: 8px 0 18px; letter-spacing: .02em; }
.boot-spinner {
  width: 26px; height: 26px; margin: 0 auto 12px;
  border: 3px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 800ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#boot-msg { color: var(--muted); margin: 0; }
.boot-error {
  margin-top: 16px; padding: 12px; text-align: left;
  background: #2a1a1a; border: 1px solid var(--c-blunder); border-radius: var(--radius);
  color: #ffbdb5; font-family: var(--mono); font-size: 12px;
  white-space: pre-wrap; word-break: break-word; max-height: 40vh; overflow: auto;
}

/* ---------------------------------------------------------- header + tabs */
.app-header {
  display: flex; align-items: center; gap: 18px;
  padding: 0 16px; height: 52px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 40;
}
.brand { font-weight: 600; letter-spacing: .02em; white-space: nowrap; }
.brand-mark { color: var(--accent); font-size: 18px; }
.tabs { display: flex; gap: 4px; flex: 1; }
.tab {
  background: transparent; border: 0; color: var(--muted);
  padding: 8px 14px; border-radius: var(--radius);
  font: inherit; font-weight: 600; cursor: pointer;
}
.tab:hover { color: var(--text); background: var(--panel-2); }
.tab.is-active { color: var(--text); background: var(--panel-2); box-shadow: inset 0 -2px 0 var(--accent); }
.header-right { display: flex; align-items: center; gap: 8px; }
.pill {
  font-size: 11px; padding: 3px 8px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--panel-2); white-space: nowrap;
}
.pill.ok { color: var(--c-best); border-color: #23483a; }
.pill.busy { color: var(--c-inaccuracy); border-color: #4a4222; }

/* ---------------------------------------------------------- layout */
main { padding: 16px; max-width: 1600px; margin: 0 auto; }
.tab-panel { display: none; }
.tab-panel.is-active { display: block; }

.layout { display: grid; grid-template-columns: 1fr; gap: 16px; align-items: start; }
@media (min-width: 1100px) {
  .layout { grid-template-columns: min(70vh, 640px) minmax(340px, 1fr); }
}
.board-col { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.side-col { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

.card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px;
}
.card.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.tight { gap: 6px; }
.lbl { color: var(--muted); font-size: 12px; }

/* ---------------------------------------------------------- controls */
.btn {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 12px; font: inherit; font-weight: 600; cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: #3d4855; background: #29313b; }
.btn:disabled { opacity: .42; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #08121f; }
.btn.primary:hover:not(:disabled) { background: #6aacff; border-color: #6aacff; }
.btn.accent { border-color: var(--accent); color: var(--accent); }
.btn.danger { border-color: #5a2b28; color: #ff8e83; }
.btn.danger:hover:not(:disabled) { background: #35201e; }
.btn.tiny { padding: 4px 9px; font-size: 12px; }
.btn.link {
  background: none; border: 0; padding: 0; color: var(--accent);
  font-weight: 600; text-decoration: underline; cursor: pointer;
}

input[type="number"], input[type="text"], select, textarea {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 8px; font: inherit;
}
input[type="number"] { width: 88px; font-family: var(--mono); }
textarea { width: 100%; font-family: var(--mono); font-size: 12px; resize: vertical; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; font-size: 13px; }

/* ---------------------------------------------------------- board */
.board-host {
  width: min(70vh, 640px);
  max-width: 100%;
  aspect-ratio: 1 / 1;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  touch-action: none;
  user-select: none;
}
.board-host .board,
.board-host .cc-board,
.board-host .board-grid,
.board-host div:has(> .sq) {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
}
.board-host .sq {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  font-size: 10px;
}
.board-host .sq.light { background: var(--sq-light); }
.board-host .sq.dark  { background: var(--sq-dark); }

.board-host .sq img,
.board-host img.piece {
  width: 100%; height: 100%; display: block;
  -webkit-user-drag: none; user-select: none;
}
.board-host img { -webkit-user-drag: none; }

/* highlight states — tolerant of naming */
.board-host .sq.last-move, .board-host .sq.lastmove, .board-host .sq.last {
  box-shadow: inset 0 0 0 100px rgba(79, 156, 255, .22);
}
.board-host .sq.selected, .board-host .sq.sel, .board-host .sq.active {
  box-shadow: inset 0 0 0 100px rgba(79, 156, 255, .34);
}
.board-host .sq.check, .board-host .sq.in-check, .board-host .sq.checked {
  box-shadow: inset 0 0 0 100px rgba(231, 76, 60, .45);
}
.board-host .sq.hl, .board-host .sq.highlight {
  box-shadow: inset 0 0 0 3px var(--accent);
}
.board-host .sq.target::after,
.board-host .sq.legal::after,
.board-host .sq.dot::after {
  content: ""; position: absolute; width: 26%; height: 26%;
  border-radius: 50%; background: rgba(20, 20, 20, .32);
}
.board-host .sq.target.occupied::after,
.board-host .sq.capture::after {
  width: 84%; height: 84%; background: transparent;
  border: 5px solid rgba(20, 20, 20, .3); border-radius: 50%;
}
.board-host .coord, .board-host .rank, .board-host .file {
  position: absolute; font-size: 9px; font-weight: 700; opacity: .65; color: #5a4632;
}
.board-host svg.arrows, .board-host .arrows {
  position: absolute; inset: 0; pointer-events: none;
}
.board-host .promo, .board-host .promotion {
  position: absolute; z-index: 6; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 8px 26px rgba(0, 0, 0, .55);
}

/* ---------------------------------------------------------- play tab */
.opp-line { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.opp-name { font-weight: 600; }
.opp-elo { font-family: var(--mono); font-size: 18px; color: var(--accent); }

.clocks { display: flex; flex-direction: column; gap: 6px; }
.clocks.flip { flex-direction: column-reverse; }
.clocks.flip #tc-label { order: -1; }
.clock-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 10px; border-radius: 6px;
  background: var(--panel-2); border: 1px solid transparent;
}
.clock-row.active { border-color: var(--accent); background: #21303f; }
.clock-row.low .clock-time { color: var(--c-blunder); }
.clock-side { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.clock-time { font-family: var(--mono); font-size: 24px; font-variant-numeric: tabular-nums; }

.banner {
  padding: 12px; border-radius: var(--radius);
  background: var(--panel-2); border: 1px solid var(--line);
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.banner.win { border-color: #23483a; }
.banner.loss { border-color: #5a2b28; }
.banner-text { font-weight: 600; }

.movelist-card { max-height: 320px; display: flex; flex-direction: column; }
.movelist { overflow-y: auto; max-height: 268px; font-size: 13px; }
.mv-row { display: grid; grid-template-columns: 34px 1fr 1fr; gap: 4px; align-items: start; }
.mv-row:nth-child(odd) { background: rgba(255, 255, 255, .022); }
.mv-num { color: var(--muted); font-family: var(--mono); padding: 3px 0 3px 4px; }
.mv {
  background: none; border: 0; color: var(--text); font: inherit;
  text-align: left; padding: 3px 6px; border-radius: 4px; cursor: pointer;
  display: flex; gap: 6px; align-items: baseline; width: 100%;
}
.mv:hover { background: var(--panel-2); }
.mv.is-current { background: #24405e; box-shadow: inset 0 0 0 1px var(--accent); }
.mv .san { font-weight: 600; }
.mv .badge { font-family: var(--mono); font-weight: 700; font-size: 12px; }
.mv .tt { color: var(--muted); font-size: 11px; margin-left: auto; font-family: var(--mono); }
.mv.empty { visibility: hidden; }

.cls-best .san, .cls-best .badge { color: var(--c-best); }
.cls-good .san { color: var(--text); }
.cls-inaccuracy .san, .cls-inaccuracy .badge { color: var(--c-inaccuracy); }
.cls-mistake .san, .cls-mistake .badge { color: var(--c-mistake); }
.cls-blunder .san, .cls-blunder .badge { color: var(--c-blunder); }

.dot-cls { display: inline-block; width: 8px; height: 8px; border-radius: 50%; }
.dot-best { background: var(--c-best); }
.dot-good { background: var(--c-good); }
.dot-inaccuracy { background: var(--c-inaccuracy); }
.dot-mistake { background: var(--c-mistake); }
.dot-blunder { background: var(--c-blunder); }

/* ---------------------------------------------------------- review */
.empty-state {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 32px; text-align: center; max-width: 720px; margin: 40px auto;
}
.empty-state .row { justify-content: center; margin-top: 14px; }

.progress-wrap {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 24px; max-width: 620px; margin: 60px auto; text-align: center;
}
.progress-label { font-weight: 600; margin-bottom: 10px; }
.progress-bar { height: 8px; background: var(--panel-2); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: var(--accent); transition: width .12s linear; }

.nav-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.caption { color: var(--muted); font-size: 12px; margin-left: 6px; }

.eval-graph { width: 100%; }
.eval-graph svg { width: 100%; height: 150px; display: block; cursor: crosshair; }

.summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; }
.stat { background: var(--panel-2); border-radius: 6px; padding: 8px; }
.stat .k { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
.stat .v { font-family: var(--mono); font-size: 16px; }

.km {
  width: 100%; text-align: left; display: block;
  background: var(--panel-2); border: 1px solid var(--line);
  border-left: 3px solid var(--muted);
  border-radius: 6px; padding: 10px; margin-bottom: 8px;
  color: var(--text); font: inherit; cursor: pointer;
}
.km:hover { border-color: var(--accent); }
.km.headline { border-left-color: var(--accent); background: #1f2937; }
.km-title { font-weight: 700; margin-bottom: 3px; }
.km-meta { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .05em; margin-bottom: 4px; }
.km-text { font-size: 13px; color: #cfd4da; }
.km.kind-blunder { border-left-color: var(--c-blunder); }
.km.kind-missed-win { border-left-color: var(--c-inaccuracy); }
.km.kind-opponent-gift { border-left-color: var(--c-best); }
.km.kind-turning-point { border-left-color: var(--accent); }

.alt-line {
  display: flex; gap: 8px; align-items: baseline;
  padding: 6px 0; border-top: 1px solid var(--line);
}
.alt-san { font-family: var(--mono); font-weight: 700; color: var(--accent); }
.alt-name { flex: 1; font-size: 12px; }
.eco { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.line-sans { font-family: var(--mono); font-size: 12px; color: #cfd4da; word-spacing: 3px; }

.bar { height: 8px; background: var(--panel-2); border-radius: 999px; overflow: hidden; margin: 3px 0 6px; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 999px; }
.bar-row { display: grid; grid-template-columns: 92px 1fr 56px; gap: 8px; align-items: center; margin-bottom: 4px; }
.bar-row .k { color: var(--muted); font-size: 12px; }
.bar-row .v { font-family: var(--mono); font-size: 12px; text-align: right; }

.walk-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ---------------------------------------------------------- drills */
.drill-group h3 { margin-top: 4px; }
.drill-item {
  display: flex; gap: 8px; align-items: center; justify-content: space-between;
  padding: 8px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--panel-2); margin-bottom: 6px;
}
.drill-main { min-width: 0; }
.drill-title { font-weight: 600; font-size: 13px; }
.drill-sub { color: var(--muted); font-size: 11px; }
.tag { font-size: 10px; padding: 2px 7px; border-radius: 999px; border: 1px solid var(--line); text-transform: uppercase; letter-spacing: .05em; }
.tag.new { color: var(--accent); border-color: #2b4a6e; }
.tag.failed { color: var(--c-blunder); border-color: #5a2b28; }
.tag.solved { color: var(--c-best); border-color: #23483a; }
.tag.tactic { color: var(--c-inaccuracy); }
.tag.punish { color: var(--c-mistake); }
.tag.opening { color: var(--accent); }

.drill-prompt { font-size: 15px; font-weight: 600; margin-bottom: 6px; }
.drill-timer { font-family: var(--mono); font-size: 20px; }
.drill-timer.low { color: var(--c-blunder); }
.feedback { padding: 10px; border-radius: 6px; margin-top: 8px; font-size: 13px; }
.feedback.correct { background: #15301f; border: 1px solid #23483a; color: #b6f0cd; }
.feedback.alternate { background: #1a2c3d; border: 1px solid #2b4a6e; color: #cfe4ff; }
.feedback.wrong { background: #331d1b; border: 1px solid #5a2b28; color: #ffc9c2; }
.verdict-mark { font-size: 16px; font-weight: 700; margin-right: 6px; }

/* ---------------------------------------------------------- progress */
.progress-head { display: flex; justify-content: flex-end; margin-bottom: 10px; }
#progress-host { display: block; }
#progress-host h2, #progress-host h3 { margin-top: 0; }
#progress-host table { border-collapse: collapse; width: 100%; font-size: 13px; }
#progress-host th, #progress-host td { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--line); }
#progress-host th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
#progress-host [data-game-id] { cursor: pointer; }
#progress-host [data-game-id]:hover { background: var(--panel-2); }

/* ---------------------------------------------------------- dialogs + toasts */
.dlg {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  padding: 18px; width: min(560px, 92vw);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
}
.dlg::backdrop { background: rgba(0, 0, 0, .6); }
.dlg h3 { color: var(--text); font-size: 16px; text-transform: none; letter-spacing: 0; }
.dlg fieldset {
  border: 1px solid var(--line); border-radius: 6px;
  padding: 10px 12px; margin: 0 0 12px;
  display: flex; gap: 14px; flex-wrap: wrap; align-items: center;
}
.dlg legend { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; padding: 0 4px; }
.dlg menu { display: flex; justify-content: flex-end; gap: 8px; margin: 14px 0 0; padding: 0; }
.error { color: #ffbdb5; background: #2a1a1a; border: 1px solid var(--c-blunder); border-radius: 6px; padding: 8px; font-size: 12px; margin-top: 8px; }

.toast-host {
  position: fixed; right: 16px; bottom: 16px; z-index: 300;
  display: flex; flex-direction: column; gap: 8px; align-items: flex-end;
}
.toast {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-left: 3px solid var(--accent);
  border-radius: 6px; padding: 10px 14px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
  animation: toast-in .14s ease-out;
  max-width: 380px;
}
.toast.error { border-left-color: var(--c-blunder); }
.toast.success { border-left-color: var(--c-best); }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

@media (max-width: 1099px) {
  .board-host { width: min(92vw, 90vh, 640px); }
  main { padding: 12px; }
  .app-header { gap: 10px; padding: 0 10px; }
  .brand { display: none; }
}
@media (max-width: 700px) {
  .header-right { display: none; }
  .tab { padding: 8px 10px; }
}

/* sound toggle */
.sound-toggle { cursor: pointer; border: none; font: inherit; line-height: 1; }
.sound-toggle.off { opacity: 0.45; }
.vol-range { width: 70px; accent-color: var(--c-accent); cursor: pointer; vertical-align: middle; }
.vol-range.off { opacity: 0.35; }
.header-right { display: flex; align-items: center; gap: 8px; }
.vol-range.wide { width: 100%; flex: 1; }
#sound-status.warn { color: var(--c-mistake); }
#sound-card .row.tight { gap: 10px; align-items: center; }
