:root {
  --bg: #101418;
  --panel: #171c22;
  --hover: #1f262e;
  --border: #2a323c;
  --text: #e6e9ec;
  --dim: #8a95a1;
  --accent: #e8a33d;
  --accent-soft: #e8a33d33;
  --win: #4caf50;
  --loss: #d9534f;
  --draw: #8a95a1;
  --light-sq: #e8d8b8;
  --dark-sq: #a8845c;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Segoe UI", system-ui, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}

#topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
#topbar h1 { margin: 0; font-size: 15px; cursor: pointer; }
#voice-toggle { font-size: 13px; padding: 2px 8px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
#voice-toggle {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  padding: 4px 10px;
  cursor: pointer;
}
#voice-toggle.on { background: var(--accent-soft); border-color: var(--accent); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--loss); }
.dot.ok { background: var(--win); }

main { max-width: 1100px; margin: 0 auto; padding: 14px; }

/* ---------- game list ---------- */

#stats-strip {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 10px;
}
.stat-tile {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 14px;
  min-width: 90px;
  text-align: center;
  flex-shrink: 0;
}
.stat-tile .v { font-size: 18px; font-weight: 600; color: var(--accent); }
.stat-tile .l { font-size: 11px; color: var(--dim); }

.game-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 8px;
  cursor: pointer;
}
.game-card:hover { background: var(--hover); }
.result-badge {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
}
.result-badge.win { background: #2c4a2e; color: var(--win); }
.result-badge.loss { background: #4a2c2c; color: var(--loss); }
.result-badge.draw { background: #333a42; color: var(--draw); }
.game-card .info { flex: 1; min-width: 0; }
.game-card .opp { font-weight: 600; }
.game-card .sub {
  color: var(--dim);
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.game-card .right { text-align: right; font-size: 12px; color: var(--dim); flex-shrink: 0; }
.game-card .acpl { color: var(--accent); font-weight: 600; font-size: 14px; }

/* ---------- review ---------- */

/* Compact one-line game header: [←] [badge] vs opp · details [lichess] */
#game-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  min-height: 40px;
}
#back-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 4px 11px;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
}
#gh-content { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.result-badge.sm { width: 26px; height: 26px; font-size: 13px; }
.gh-text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.gh-main { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gh-color { color: var(--dim); font-weight: 400; font-size: 12px; }
.gh-sub { color: var(--dim); font-size: 11.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gh-link { color: var(--accent); text-decoration: none; font-size: 16px; padding: 4px; flex-shrink: 0; }

/* Lichess-like proportions: board dominates, narrow analysis column */
#review-layout { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 22px; }

/* Desktop: app-style fixed layout — board pinned, only the analysis column scrolls */



/* Mobile: app-style fixed layout — board never moves, analysis pane scrolls */


#board-pane { display: grid; grid-template-columns: 56px 14px minmax(0, 1fr); gap: 8px; align-items: start; }

/* Willis's perch: he sits left of the board, slides to the rank of the action */
#willis-perch { grid-row: 2; grid-column: 1; position: relative; height: 100%; }
#willis-perch #willis-watermark {
  position: absolute;
  left: 0;
  width: 100%;
  height: auto;
  transition: top 0.55s cubic-bezier(0.3, 1.25, 0.5, 1), transform 0.4s ease;
}

#eval-bar-wrap {
  /* stretches to the grid row height, i.e. exactly the board's height */
  grid-row: 2;
  grid-column: 2;
  align-self: stretch;
  background: #333;
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  flex-direction: column-reverse;
}
#board { grid-row: 2; grid-column: 3; }

/* Willis: solid black sidekick, one amber eye, watching from his perch */
#willis-watermark { pointer-events: none; z-index: 11; }
#willis-watermark .wcat-body { fill: #14181d; fill-opacity: 0.95; }
#willis-watermark .wcat-scar {
  stroke: #8a95a1; stroke-opacity: 0.55; stroke-width: 3.4;
  stroke-linecap: round; fill: none;
}
#willis-watermark .wcat-eye { fill: #e8a33d; fill-opacity: 0.92; }
#willis-watermark .wcat-pupil { fill: #0a0c0e; fill-opacity: 0.95; }
#willis-watermark .wcat-glint { fill: #fff; fill-opacity: 0.75; }
#willis-watermark .wcat-nose { fill: #05070a; fill-opacity: 0.6; }
#willis-watermark .wcat-mouth,
#willis-watermark .wcat-whiskers path {
  stroke: #3a434e; stroke-opacity: 0.8; stroke-width: 2.4;
  stroke-linecap: round; fill: none;
}

/* eye tracking: quick feline saccades */
#willis-watermark .wcat-pupil,
#willis-watermark .wcat-eye,
#willis-watermark .wcat-glint {
  transition: transform 0.13s cubic-bezier(0.3, 1.4, 0.6, 1);
}

/* best move played: eye pops wide with excitement */
#willis-watermark .wcat-eyegroup {
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 0.25s cubic-bezier(0.3, 1.8, 0.5, 1);
}
#willis-watermark.wcat-happy .wcat-eyegroup { transform: scale(1.3); }
#willis-watermark.wcat-happy .wcat-eye { fill-opacity: 0.65; }
#willis-watermark.wcat-happy .wcat-glint { fill-opacity: 0.7; }

/* the thumbs-up paw */
#willis-watermark .wcat-paw {
  opacity: 0;
  transform: translateY(30px);
}
#willis-watermark .wcat-paw .paw-arm,
#willis-watermark .wcat-paw .paw-fist,
#willis-watermark .wcat-paw .paw-thumb { fill: #14181d; fill-opacity: 0.95; }
#willis-watermark .wcat-paw .paw-toes {
  stroke: #8a95a1; stroke-opacity: 0.7; stroke-width: 2.2;
  stroke-linecap: round; fill: none;
}
#willis-watermark .wcat-paw.paw-show {
  animation: paw-thumbs-up 2.2s cubic-bezier(0.3, 1.4, 0.5, 1) forwards;
}
@keyframes paw-thumbs-up {
  0%   { opacity: 0; transform: translateY(30px) rotate(4deg); }
  18%  { opacity: 1; transform: translateY(-6px) rotate(-3deg); }
  28%  { transform: translateY(0) rotate(0deg); }
  38%  { transform: translateY(-3px) rotate(-2deg); } /* enthusiastic little pump */
  48%  { transform: translateY(0) rotate(0deg); }
  82%  { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(30px); }
}
#eval-bar { background: #eee; width: 100%; height: 50%; transition: height 0.25s; }

#board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr); /* explicit rows: empty ranks must not collapse */
  aspect-ratio: 1;
  justify-self: start;
  /* width comes from the responsive caps in the media queries above */
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border);
  user-select: none;
}
.sq { position: relative; }
.sq.light { background: var(--light-sq); }
.sq.dark { background: var(--dark-sq); }
.sq img { width: 100%; height: 100%; display: block; object-fit: contain; }
.sq.hl-from::after, .sq.hl-to::after {
  content: "";
  position: absolute;
  inset: 0;
  background: #e8a33d55;
}
/* the played move WAS the engine's move — green "that was best" tint */
.sq.hl-played-best::after {
  content: "";
  position: absolute;
  inset: 0;
  background: #4caf5052;
}

#replay-controls {
  grid-column: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 8px;
}
#replay-controls button {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 17px;
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
}
#replay-controls button:active { background: var(--accent-soft); }
#move-indicator { min-width: 88px; text-align: center; color: var(--dim); font-size: 13px; }

#graph-wrap { grid-column: 3; position: relative; }
#eval-graph {
  width: 100%;
  height: 60px;
  margin-top: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}

/* ---------- analysis pane ---------- */

/* Blunder board: aligned rows, severity color-coded, tap to jump */
#key-moments {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 10px;
}
.km-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-left-width: 3px;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
.km-chip .km-mv { font-weight: 600; font-family: Consolas, monospace; white-space: nowrap; }
.km-chip .km-loss { color: var(--dim); font-size: 12px; margin-left: auto; font-family: Consolas, monospace; }
.km-chip .km-cls { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.4px; }
.km-chip.blunder { border-left-color: var(--loss); }
.km-chip.blunder .km-cls { color: var(--loss); }
.km-chip.mistake { border-left-color: var(--accent); }
.km-chip.mistake .km-cls { color: var(--accent); }
.km-chip.active { background: var(--accent-soft); }

#move-comment {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  min-height: 64px;
  font-size: 14px;
  margin-bottom: 12px;
}
#move-comment .cls-blunder { color: var(--loss); font-weight: 700; }
#move-comment .cls-mistake { color: var(--accent); font-weight: 700; }
#move-comment .cls-inaccuracy { color: #d9c34f; font-weight: 600; }
#move-comment .cls-ok { color: var(--win); }

#narrative-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 12px;
  font-size: 14px;
}
#narrative-box summary { cursor: pointer; color: var(--accent); }
#btn-narrate {
  background: var(--accent);
  color: #1a1a1a;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  margin-top: 8px;
}

/* Move list: fully justified rows — number gutter + two equal columns */
#move-list {
  display: grid;
  grid-template-columns: 2.6em 1fr 1fr;
  gap: 0;
  font-size: 13.5px;
  font-family: Consolas, monospace;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
#move-list .num {
  color: var(--dim);
  padding: 5px 6px;
  text-align: right;
  background: var(--panel);
  border-bottom: 1px solid var(--bg);
  font-size: 12px;
}
#move-list .mv {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  padding: 5px 10px;
  border-bottom: 1px solid var(--bg);
  border-left: 1px solid var(--bg);
}
#move-list .mv.empty { cursor: default; }
#move-list .mv:hover { background: var(--hover); }
#move-list .mv.current { background: var(--accent-soft); }
#move-list .mv.blunder .mv-san { color: var(--loss); font-weight: 700; }
#move-list .mv.mistake .mv-san { color: var(--accent); font-weight: 600; }
#move-list .mv.inaccuracy .mv-san { color: #d9c34f; }
#move-list .mv-loss { color: var(--dim); font-size: 11px; }

.loading { color: var(--dim); padding: 30px; text-align: center; }

/* ---------- theme panel ---------- */

#theme-toggle {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  padding: 2px 8px;
  cursor: pointer;
}
#theme-panel {
  position: fixed;
  top: 42px;
  right: 10px;
  z-index: 30;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  width: 250px;
  box-shadow: 0 8px 30px #000a;
}
#theme-panel h3 { margin: 6px 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--dim); }
.theme-row { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.theme-row label { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.theme-row input[type="color"] { width: 34px; height: 26px; border: 1px solid var(--border); border-radius: 6px; background: none; padding: 1px; }
.theme-row select {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 4px 6px; font-size: 13px;
}
.theme-row button {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 5px 12px; font-size: 13px; cursor: pointer;
}
.preset-swatch { width: 30px; height: 30px; border-radius: 6px; border: 1px solid var(--border); cursor: pointer; }

/* ---------- capture shatter (RnR) ---------- */

#board { position: relative; }

/* engine best-move arrow overlay */
.board-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 12;
  pointer-events: none;
}

/* move classification glyph (lichess-style badge on the destination square) */
.glyph-badge {
  position: absolute;
  z-index: 14;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 800;
  font-size: 12px;
  font-family: Georgia, serif;
  box-shadow: 0 1px 4px #0008;
  pointer-events: none;
}

/* king in check */
.sq.in-check {
  background-image: radial-gradient(circle at 50% 50%, #d9534fb0 0%, #d9534f55 55%, transparent 78%);
}

/* board coordinates */
.coord {
  position: absolute;
  font-size: clamp(8px, 1.4vw, 11px);
  font-weight: 600;
  pointer-events: none;
  line-height: 1;
  opacity: 0.85;
}
.coord-file { right: 3px; bottom: 2px; }
.coord-rank { left: 3px; top: 2px; }
.coord.on-light { color: var(--dark-sq); }
.coord.on-dark { color: var(--light-sq); }

.capture-fx { position: absolute; z-index: 20; pointer-events: none; }
.capture-shard {
  position: absolute;
  width: 25%;
  height: 25%;
  background-repeat: no-repeat;
  will-change: transform, opacity;
}

/* ---------- AI analysis ---------- */

#ai-buttons { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
#ai-buttons button {
  background: var(--bg); color: var(--accent); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 12px; font-size: 12.5px; cursor: pointer;
}
#ai-buttons button:disabled { opacity: 0.6; cursor: wait; }
#annot-status { color: var(--dim); font-size: 12px; }
.llm-comment {
  margin-top: 8px;
  padding: 8px 10px;
  background: var(--bg);
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  font-size: 13.5px;
  line-height: 1.45;
}
.llm-comment.openai { border-left-color: #10a37f; }
.llm-comment.anthropic { border-left-color: #d97757; }
.llm-provider {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--accent);
  margin-right: 8px;
  vertical-align: 1px;
}
.llm-comment.openai .llm-provider { color: #10a37f; }
.llm-comment.anthropic .llm-provider { color: #d97757; }

/* hidden views must hide even though ID rules set display */
main[hidden] { display: none !important; }

/* best-move markers (chess.com style) */
.cls-best { color: var(--win); font-weight: 700; }
#move-list .mv-best { color: var(--win); font-weight: 700; font-size: 12px; }
#btn-play.playing { background: var(--accent-soft); border-color: var(--accent); }

/* side-to-move indicator */
#turn-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #f0f0f0;
  border: 2px solid var(--border);
  flex-shrink: 0;
}
#turn-dot.black-turn { background: #1a1d21; border-color: #555e69; }

/* player bars: name + captured pieces, each on their own side of the board */
.player-bar {
  grid-column: 3;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 24px;
  padding: 1px 2px;
}
#player-top { grid-row: 1; }
#player-bottom { grid-row: 3; }
.pb-name { font-size: 13px; font-weight: 600; color: var(--text); white-space: nowrap; }
.pb-name.me { color: var(--accent); }
.captures-row { display: flex; align-items: center; gap: 1px; min-height: 20px; }
.captures-row img { width: 18px; height: 18px; opacity: 0.9; }
.captures-row .mat-diff { color: var(--win); font-size: 12px; font-weight: 700; margin-left: 6px; }

/* Willis rides the eval boundary; the score travels with him */
#willis-eval {
  position: absolute;
  left: 0;
  width: 100%;
  text-align: center;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
  font-family: Consolas, monospace;
  transition: top 0.55s cubic-bezier(0.3, 1.25, 0.5, 1);
  pointer-events: none;
  z-index: 12;
  text-shadow: 0 1px 3px #000c;
}

/* eval graph: timeline cursor + scale */
#graph-wrap #eval-graph { width: 100%; display: block; }
#graph-label {
  position: absolute;
  top: -2px;
  transform: translateX(-50%);
  font-size: 10px;
  font-family: Consolas, monospace;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 4px;
  pointer-events: none;
  white-space: nowrap;
}
.graph-scale {
  position: absolute;
  left: 3px;
  font-size: 9px;
  color: var(--dim);
  pointer-events: none;
  font-family: Consolas, monospace;
}
.gs-top { top: 1px; }
.gs-mid { top: 50%; transform: translateY(-50%); }
.gs-bot { bottom: 1px; }

/* move color legend */
#move-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  font-size: 11px;
  color: var(--dim);
  margin: 6px 0;
  align-items: center;
}
#move-legend span { display: flex; align-items: center; gap: 5px; }
.lg-dot {
  width: 10px; height: 10px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-style: normal; font-size: 8px; font-weight: 800; color: #fff;
}
.lg-best { background: var(--win); width: 12px; height: 12px; }
.lg-ok { background: #e6e9ec; }
.lg-inacc { background: #d9c34f; }
.lg-mist { background: var(--accent); }
.lg-blun { background: var(--loss); }

/* right-panel section titles */
.section-title {
  margin: 14px 0 6px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--dim);
}
#analysis-pane .section-title:first-child { margin-top: 2px; }
.ai-summary { cursor: pointer; color: var(--accent); margin: 0; }
.ai-summary #annot-status { text-transform: none; letter-spacing: 0; }

/* blunder/mistake destination tints (mirror of the green best tint) */
.sq.hl-cls-blunder::after {
  content: ""; position: absolute; inset: 0; background: #d9534f52;
}
.sq.hl-cls-mistake::after {
  content: ""; position: absolute; inset: 0; background: #e8a33d3d;
}

/* ELO trajectory on the list page */
#elo-chart-wrap { margin-bottom: 12px; }
#elo-chart-box {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
}
#elo-chart { width: 100%; height: 90px; display: block; }
.elo-scale {
  position: absolute;
  right: 6px;
  font-size: 9px;
  color: var(--dim);
  font-family: Consolas, monospace;
  pointer-events: none;
}
#elo-hi { top: 3px; }
#elo-lo { bottom: 3px; }
#elo-peakline-label { left: 6px; right: auto; color: var(--accent); }
.elo-tile .v { color: #4a90d9; }

/* ============================================================
   RESPONSIVE OVERRIDES — keep these LAST so they always win.
   (Base rules earlier in this file silently clobbered them twice.)
   ============================================================ */

@media (min-width: 761px) {
  main { max-width: 1280px; }
  #game-view {
    height: calc(100dvh - 40px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  #game-header { flex-shrink: 0; }
  #review-layout { flex: 1; min-height: 0; }
  #board-pane { min-height: 0; }
  #analysis-pane { min-height: 0; overflow-y: auto; padding-right: 4px; }
  /* fill the column but never overflow the viewport vertically */
  #board { max-width: calc(100dvh - 278px); margin: 0 auto; }
}

@media (min-width: 761px) and (max-width: 1080px) {
  #review-layout { grid-template-columns: minmax(0, 1fr) 340px; }
}

@media (max-width: 760px) {
  main { padding: 8px 10px; }
  #game-view {
    height: calc(100dvh - 40px); /* below the slim navbar */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding-bottom: 0;
  }
  #game-header { margin-bottom: 6px; flex-shrink: 0; }
  #review-layout {
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex: 1;
    min-height: 0;
  }
  #board-pane { flex-shrink: 0; grid-template-columns: 34px 12px minmax(0, 1fr); }
  /* keep the board from starving the analysis pane on short screens */
  #board { max-width: 42dvh; margin: 0 auto; }
  #analysis-pane {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: env(safe-area-inset-bottom);
    border-top: 1px solid var(--border);
    padding-top: 8px;
  }
  #eval-graph { height: 40px; }
  #replay-controls { margin-top: 6px; }
  #replay-controls button { padding: 6px 14px; }
}
