/* =============================================================================
   neo Chess — Analysis Notebook
   The board is the hero; the frame borrows the chess world's vernacular — a
   monospace scoresheet, slate-and-stone squares, and colour used only to mean
   something (teal = you, amber = last, red = check). Dark by default.

   Theme model: dark is the base (:root). [data-theme="light"] overrides the
   *chrome only*. "Auto" is resolved to a concrete light/dark value in JS before
   paint, so CSS never has to guess from a system media query — that ambiguity
   was the old dark-mode bug. The BOARD and PIECES are constants: they never
   change with the theme, so the playing surface always looks the same.
   ============================================================================= */

:root {
  /* chrome — dark (default) */
  --ink:      #e7ecf2;
  --paper:    #12161d;
  --panel:    #1b222d;
  --panel-2:  #232c39;
  --line:     #2f3a48;
  --muted:    #93a0b0;
  --edge:     rgba(255,255,255,.04);

  /* meaning — constant across themes */
  --teal:     #35b0a7;   /* selection + legal moves + you   */
  --amber:    #e0a83e;   /* last move                       */
  --danger:   #d15b45;   /* check / danger                  */

  /* board + pieces — the classic look, constant across themes */
  --sq-light:     #dfe3e8;
  --sq-dark:      #5b6b7f;
  --piece-w:      #f6f8fa;
  --piece-w-line: #10141b;
  --piece-b:      #26303e;
  --piece-b-line: #cfd6de;

  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "JetBrains Mono", Menlo, Consolas, monospace;

  /* Board fills close to the screen width on a phone (width term wins), capping
     at 560px on desktop. The height term is a generous safety cap so a very
     short viewport shrinks the board instead of forcing it past the fold. */
  --board: min(100vw - 12px, calc(100dvh - 210px), 560px);
  --radius: 10px;
}

/* Explicit Light — chrome only. Board/piece tokens are deliberately absent. */
[data-theme="light"] {
  --ink:      #1b2330;
  --paper:    #eef1f4;
  --panel:    #ffffff;
  --panel-2:  #f7f9fb;
  --line:     #d5dbe2;
  --muted:    #6b7683;
  --edge:     rgba(255,255,255,.9);
  --teal:     #2f9c95;
  --danger:   #c8503a;
}

* { box-sizing: border-box; }
html, body { margin: 0; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}

/* --- App bar (sticky top; three rows) -------------------------------------- */
.appbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: calc(10px + env(safe-area-inset-top)) clamp(12px, 4vw, 22px) 10px;
}
.appbar__row {
  display: flex; align-items: center; gap: 10px;
  max-width: 600px; margin: 0 auto;
}
.appbar__brandrow  { justify-content: space-between; }
.appbar__controls  { flex-wrap: wrap; margin-top: 10px; }
.appbar__statusrow { justify-content: space-between; flex-wrap: wrap; gap: 6px 12px; margin-top: 10px; }

.appbar__divider {
  border: 0; border-top: 1px solid var(--line);
  max-width: 600px; margin: 10px auto 0;
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand__logo {
  display: grid; place-items: center;
  width: 44px; height: 44px; border-radius: 11px;
  background: var(--teal); color: #fff; font-size: 29px; line-height: 1; flex: none;
}
.brand__text  { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.brand__title { display: flex; align-items: baseline; gap: 7px; }
.brand__name  { font-family: var(--mono); font-weight: 700; font-size: 19px; letter-spacing: .01em; }
.brand__ver   { font-family: var(--mono); font-style: italic; font-size: 12px; color: var(--muted); letter-spacing: .02em; }
.brand__site  { font-family: var(--mono); font-style: italic; font-size: 12px; color: var(--muted); letter-spacing: .02em; }

/* Controls: three selects, wrapping gracefully on narrow phones. */
.ctl {
  font-family: var(--mono); font-size: 13px;
  color: var(--ink); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; cursor: pointer;
  flex: 1 1 auto; min-width: 0;
}
.ctl:disabled { opacity: .4; cursor: default; }

/* Right end of the brand row: Install pill, then the info button. */
.brandrow__end { display: flex; align-items: center; gap: 8px; flex: none; }

.iconbtn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; flex: none;
  color: var(--ink); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 999px; cursor: pointer;
}
.iconbtn:hover, .iconbtn:focus-visible { border-color: var(--teal); outline: none; }
.iconbtn svg { width: 20px; height: 20px; }
.iconbtn[hidden] { display: none; }

/* NOTE: the Install pill (.pill) is intentionally styled INLINE in index.html's
   <head>, not here. The CSS file has repeatedly reached devices stale while the
   HTML (network-first) always lands, leaving the pill unstyled. Inlining it in
   the reliable channel — with a hardcoded teal, independent of these tokens —
   guarantees it renders. If you restyle the pill, edit index.html, not this. */

.status {
  font-family: var(--mono); font-size: 14px; color: var(--ink); min-width: 0;
}
.status--check    { color: var(--danger); font-weight: 600; }
.status--over     { color: var(--ink); font-weight: 700; }
.status--thinking { color: var(--muted); font-style: italic; }

.tally {
  font-family: var(--mono); font-size: 12px; color: var(--muted); white-space: nowrap;
}
.tally:empty { display: none; }

/* --- Stage ----------------------------------------------------------------- */
.stage {
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  max-width: 600px; margin: 0 auto;
  padding: 12px clamp(4px, 1.5vw, 16px);
}

/* --- Board (theme-independent) --------------------------------------------- */
.board-wrap {
  width: var(--board);
  padding: 5px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 1px 0 var(--edge) inset, 0 10px 30px -20px rgba(0, 0, 0, .6);
}
.board {
  width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid rgba(0, 0, 0, .25);
}
.square {
  position: relative; border: 0; margin: 0; padding: 0; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--mono);
  -webkit-tap-highlight-color: transparent;
}
.square--light { background: var(--sq-light); }
.square--dark  { background: var(--sq-dark); }
.square:focus-visible { outline: 3px solid var(--teal); outline-offset: -3px; z-index: 3; }

.piece {
  font-size: calc(var(--board) / 8 * 0.82);
  line-height: 1; pointer-events: none;
  paint-order: stroke fill;
  -webkit-text-stroke: 1.2px var(--piece-w-line);
  text-shadow: 0 1px 1px rgba(0, 0, 0, .32);
}
.piece--w { color: var(--piece-w); }
.piece--b { color: var(--piece-b); -webkit-text-stroke-color: var(--piece-b-line); }

.coord {
  position: absolute;
  font-size: calc(var(--board) / 8 * 0.17);
  font-weight: 600; pointer-events: none; user-select: none;
}
.square--light .coord { color: rgba(0, 0, 0, .45); }
.square--dark  .coord { color: rgba(255, 255, 255, .7); }
.coord--rank { top: 2px; left: 3px; }
.coord--file { bottom: 1px; right: 3px; }

/* Highlights — each colour means one thing. */
.square.is-last::after {
  content: ""; position: absolute; inset: 0;
  background: var(--amber); opacity: .30; pointer-events: none;
}
.square.is-selected { box-shadow: inset 0 0 0 3px var(--teal); }
.square.is-check::after {
  content: ""; position: absolute; inset: 0;
  background: radial-gradient(circle at center, rgba(209, 91, 69, .85), rgba(209, 91, 69, 0) 70%);
  pointer-events: none;
}
.square.is-target::before {
  content: ""; position: absolute;
  width: 30%; height: 30%; border-radius: 50%;
  background: rgba(53, 176, 167, .55); pointer-events: none;
}
.square.is-capture::before {
  content: ""; position: absolute; inset: 8%;
  border-radius: 50%; border: 4px solid rgba(53, 176, 167, .7); pointer-events: none;
}
.square.is-hint { outline: 3px dashed var(--teal); outline-offset: -3px; z-index: 1; }

/* --- Action toolbar (under the board; scrolls with content, never clipped) --
   Reads as a clean nav strip, not a boxed panel: no container border or fill,
   the buttons themselves are the UI. Icon-only — each button is a generous tap
   target with a subtle pill on hover/press. Kept in normal flow (never pinned
   to the viewport bottom — that was what Brave's mobile chrome clipped).
   Five buttons (New·Hint·Undo·Flip·Coach), each flex:1 so they space evenly. */
.actions {
  width: var(--board);
  display: flex; justify-content: space-between; align-items: stretch; gap: 4px;
  background: none; border: 0; padding: 2px 0;
}
.actbtn {
  flex: 1 1 0; min-width: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 12px 4px;
  background: none; border: 0; border-radius: 12px; cursor: pointer;
  color: var(--muted);
  transition: color .12s ease, background .12s ease, transform .06s ease;
}
.actbtn svg { width: 26px; height: 26px; }
.actbtn:hover, .actbtn:focus-visible { color: var(--ink); background: var(--panel-2); outline: none; }
.actbtn:active { transform: translateY(1px); }
.actbtn.is-active { color: var(--teal); }
.actbtn:disabled { opacity: .34; cursor: default; }
.actbtn:disabled:hover { background: none; color: var(--muted); }

/* --- Coach box ------------------------------------------------------------- */
.coach {
  width: var(--board);
  border-left: 3px solid var(--line);
  background: var(--panel);
  border-radius: 8px; padding: 10px 12px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .12);
}
.coach[hidden] { display: none; }
.coach__tag {
  display: inline-block; font-family: var(--mono); font-size: 10px;
  letter-spacing: .09em; text-transform: uppercase; color: var(--muted); margin-bottom: 4px;
}
.coach__tag:empty { display: none; }
.coach__headline { margin: 0; font-size: 14px; line-height: 1.4; color: var(--ink); }
.coach__detail { margin: 4px 0 0; font-size: 13px; line-height: 1.4; color: var(--muted); }
.coach__detail:empty { display: none; }
.coach--blunder    { border-left-color: var(--danger); }
.coach--blunder    .coach__tag { color: var(--danger); }
.coach--mistake    { border-left-color: var(--amber); }
.coach--mistake    .coach__tag { color: var(--amber); }
.coach--inaccuracy { border-left-color: var(--muted); }
.coach--good,
.coach--great      { border-left-color: var(--teal); }
.coach--good  .coach__tag,
.coach--great .coach__tag { color: var(--teal); }
.coach--ok         { border-left-color: var(--line); }

/* --- Scoresheet ------------------------------------------------------------ */
.scoresheet {
  width: var(--board);
  background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px;
  overflow: hidden;
}
.scoresheet__empty { color: var(--muted); font-size: 13px; padding: 12px 16px; margin: 0; }
.scoresheet__table { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 14px; }
.scoresheet__no { width: 2.5em; text-align: right; padding: 3px 10px 3px 12px; color: var(--muted); }
.scoresheet__ply { padding: 3px 10px; }
.scoresheet__table tr:nth-child(odd) { background: var(--panel-2); }

/* --- About modal ----------------------------------------------------------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, .55);
}
.modal[hidden] { display: none; }
.modal__panel {
  position: relative;
  width: 100%; max-width: 440px; max-height: 100%;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 14px;
  padding: 22px 22px 24px;
  box-shadow: 0 24px 70px -24px rgba(0, 0, 0, .8);
}
.modal__close {
  position: absolute; top: 12px; right: 12px;
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px;
  color: var(--muted); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 9px; cursor: pointer;
}
.modal__close:hover, .modal__close:focus-visible { color: var(--ink); border-color: var(--teal); outline: none; }
.modal__close svg { width: 18px; height: 18px; }
.modal__section { margin: 0; }
.modal__divider { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.modal__site { margin: 0; font-family: var(--mono); font-size: 12px; color: var(--muted); letter-spacing: .04em; }
.modal__title { margin: 4px 0 0; font-family: var(--mono); font-size: 20px; font-weight: 700; }
.modal__ver { font-size: 13px; font-weight: 400; color: var(--muted); }
.modal__h { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .09em; color: var(--teal); font-family: var(--mono); }
.modal__section p { margin: 0 0 8px; font-size: 14px; line-height: 1.5; color: var(--ink); }
.modal__section p:last-child { margin-bottom: 0; }
.modal__features { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; }
.modal__features dt { font-family: var(--mono); font-size: 13px; color: var(--teal); }
.modal__features dd { margin: 0; font-size: 13px; line-height: 1.45; color: var(--ink); }

/* --- Promotion picker ------------------------------------------------------ */
.promotion {
  position: fixed; inset: 0; z-index: 60;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .55);
}
.promotion[hidden] { display: none; }
.promotion__panel {
  background: var(--panel); border-radius: 12px; padding: 18px 20px 20px;
  box-shadow: 0 20px 60px -20px rgba(0, 0, 0, .8); text-align: center;
}
.promotion__title { margin: 0 0 12px; font-family: var(--mono); font-size: 13px; color: var(--muted); letter-spacing: .04em; }
.promotion__choices { display: flex; gap: 10px; }
.promotion__btn {
  width: 60px; height: 60px; font-size: 40px; line-height: 1;
  color: var(--piece-w);
  background: var(--sq-dark);
  border: 1px solid var(--line); border-radius: 10px; cursor: pointer;
  -webkit-text-stroke: 1.2px var(--piece-w-line); paint-order: stroke fill;
  transition: transform .06s ease, border-color .12s ease;
}
.promotion__btn:hover { border-color: var(--teal); }
.promotion__btn:active { transform: translateY(1px); }
.promotion[data-color="b"] .promotion__btn { color: var(--piece-b); -webkit-text-stroke-color: var(--piece-b-line); }

/* --- Motion preference ----------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
