/* ===== THEME TOKENS ===== */
:root[data-theme="dark"] {
  --bg: #0a0a0a;
  --panel: #141414;
  --panel-2: #1c1c1e;
  --line: #2a2a2c;
  --text: #f2f2f7;
  --muted: #8e8e93;
  --muted-2: #636366;
  --accent: #34C759;
  --white-tint: #e5e5ea;
  --black-tint: #0f0f10;
  --eval-white: #f2f2f7;
  --eval-black: #1c1c1e;
  --good: #34C759;
  --bad: #ff453a;
  --book: #64d2ff;
  --shadow-lg: 0 16px 50px rgba(0,0,0,0.5);
  --shadow-md: 0 12px 40px rgba(0,0,0,0.5);
  --scrim: rgba(0,0,0,0.7);
}
:root[data-theme="light"] {
  --bg: #f2f2f7;
  --panel: #ffffff;
  --panel-2: #f2f2f7;
  --line: #e5e5ea;
  --text: #1c1c1e;
  --muted: #6e6e73;
  --muted-2: #a1a1a6;
  --accent: #34C759;
  --white-tint: #ffffff;
  --black-tint: #1c1c1e;
  --eval-white: #ffffff;
  --eval-black: #1c1c1e;
  --good: #28a745;
  --bad: #e53935;
  --book: #0a84ff;
  --shadow-lg: 0 12px 36px rgba(0,0,0,0.08);
  --shadow-md: 0 8px 24px rgba(0,0,0,0.08);
  --scrim: rgba(0,0,0,0.35);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* Block double-tap-to-zoom on interactive elements — stops browser zooming in when user taps Prev/Next rapidly */
button, a, .ctrl-btn, .move-cell, .menu-btn, .menu-item, .nav-btn, .theme-opt,
.analyse-btn, .modal-close {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: 'Poppins', -apple-system, sans-serif;
  font-size: 15px; line-height: 1.5;
  min-height: 100vh; overflow-x: hidden;
  transition: background 0.2s ease, color 0.2s ease;
}

/* ===== HEADER ===== */
.topbar {
  position: sticky; top: 0; z-index: 60;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1280px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px; gap: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); }
.brand-logo {
  width: 32px; height: 32px; border-radius: 8px;
  object-fit: cover; object-position: center;
  background: var(--panel-2);
  flex-shrink: 0;
}
.brand-name { font-weight: 700; font-size: 16px; letter-spacing: -0.2px; }

.menu-wrap { position: relative; }
.menu-btn {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--panel-2); border: 1px solid var(--line);
  color: var(--text); font-size: 18px; cursor: pointer;
  display: grid; place-items: center;
  transition: background 0.15s, transform 0.1s;
}
.menu-btn:hover { background: color-mix(in srgb, var(--panel-2) 70%, var(--text) 5%); }
.menu-btn:active { transform: scale(0.94); }
.menu-pop {
  position: absolute; top: 44px; right: 0;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 6px;
  min-width: 252px;
  box-shadow: var(--shadow-md);
  opacity: 0; pointer-events: none; transform: translateY(-4px);
  transition: opacity 0.15s, transform 0.15s;
  z-index: 70;
}
.menu-pop.open { opacity: 1; pointer-events: auto; transform: translateY(0); }
.menu-item {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 12px; border-radius: 9px;
  background: transparent; border: none; color: var(--text);
  font-family: inherit; font-size: 13.5px; font-weight: 500;
  cursor: pointer; width: 100%; text-align: left; text-decoration: none;
  transition: background 0.12s;
}
.menu-item:hover { background: var(--panel-2); }
.menu-item svg { width: 16px; height: 16px; flex-shrink: 0; color: var(--muted); }
.menu-divider { height: 1px; background: var(--line); margin: 4px 8px; }

/* ===== GAME NAV ===== */
.game-nav {
  max-width: 1280px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px 0; gap: 10px;
}
.nav-side { display: flex; align-items: center; gap: 6px; }
.nav-btn {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 6px 11px; border-radius: 999px;
  background: var(--panel-2); color: var(--text);
  text-decoration: none; font-size: 12px; font-weight: 500;
  border: 1px solid var(--line);
  transition: background 0.15s, transform 0.1s;
}
.nav-btn:hover { background: color-mix(in srgb, var(--panel-2) 70%, var(--text) 5%); }
.nav-btn:active { transform: scale(0.96); }
.nav-btn.disabled { opacity: 0.35; pointer-events: none; }
.nav-btn svg { width: 12px; height: 12px; }
.game-counter { font-size: 12.5px; font-weight: 500; color: var(--muted); }
.game-counter b { color: var(--text); font-weight: 600; }
.back-all {
  font-size: 12px; color: var(--muted); text-decoration: none;
  padding: 6px 10px; border-radius: 10px;
}
.back-all:hover { color: var(--text); background: var(--panel-2); }

.wrap { max-width: 1280px; margin: 0 auto; padding: 12px 18px 40px; }

/* ===== PLAYERS (compact) ===== */
.players {
  display: grid; grid-template-columns: 1fr auto 1fr;
  align-items: center; gap: 10px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px 12px; margin-bottom: 10px;
}
.player { display: flex; align-items: center; gap: 10px; min-width: 0; }
.player.black { justify-content: flex-end; }
.player-chip {
  width: 26px; height: 26px; border-radius: 7px;
  display: grid; place-items: center; font-size: 15px; flex-shrink: 0;
}
.player-chip.white { background: #ffffff; color: #111; border: 1px solid var(--line); }
.player-chip.black { background: #1c1c1e; color: #fff; border: 1px solid var(--line); }
.player-name { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-side { font-size: 9px; color: var(--muted); letter-spacing: 0.6px; text-transform: uppercase; font-weight: 500; line-height: 1; margin-bottom: 2px; }
.result-badge {
  padding: 4px 11px; border-radius: 999px;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600; font-size: 11.5px; letter-spacing: 0.5px;
  background: var(--panel-2); border: 1px solid var(--line); white-space: nowrap;
}
.result-badge.white-wins { background: #ffffff; color: #111; border-color: var(--line); }
.result-badge.black-wins { background: #1c1c1e; color: #fff; border-color: var(--line); }
.result-badge.draw { background: var(--panel-2); color: var(--muted); }

/* ===== GRID ===== */
/* Three layouts, same HTML:
   - mobile (<768):  single column, natural stack order
                     board → controls → analyse → moves
   - tablet (768-1023):  2 columns. Board top-left, controls and analyse
                         below it, move list fills the right column.
   - laptop+ (≥1024):   2 columns. Board spans the full left column so it
                        can be as large as possible. Controls, analyse,
                        and moves stack in the right column.            */
.game-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
.board-wrap {
  display: flex; gap: 8px; align-items: flex-start;
  justify-content: center;
}
.controls { display: grid; grid-template-columns: repeat(6, 1fr); gap: 5px; }
/* Analyser-style clean eval bar: just the proportional fill, no numeric overlay */
.evalbar {
  width: 14px; background: var(--eval-black);
  position: relative; flex-shrink: 0;
  overflow: hidden; border-radius: 3px;
  border: 1px solid var(--line);
}
.evalbar-fill {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: var(--eval-white);
  transition: height 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  height: 50%;
}
.evalbar-zero {
  position: absolute; left: 0; right: 0; top: 50%;
  height: 1px; background: var(--accent); opacity: 0.55;
}

#board {
  /* Explicit px dimensions set by JS; no aspect-ratio / no flex:1 / no border-radius */
  /* Sharp corners — chessboard.js squares won't be clipped */
  overflow: visible;
  box-shadow: var(--shadow-lg);
  flex-shrink: 0;
}
#board .board-b72b1 { border: none !important; }

.ctrl-btn {
  padding: 0;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  transition: transform 0.1s, background 0.15s, border-color 0.15s;
  display: grid; place-items: center;
  height: 40px;
}
.ctrl-btn svg { width: 18px; height: 18px; stroke-width: 2; }
.ctrl-btn:hover { background: var(--panel-2); }
.ctrl-btn:active { transform: scale(0.94); }
.ctrl-btn.playing { background: var(--accent); color: #0a0a0a; border-color: var(--accent); }

.analyse-btn {
  display: flex; align-items: center; justify-content: center; gap: 9px;
  width: 100%; padding: 12px 18px;
  background: var(--accent); color: #0a0a0a;
  border: none; border-radius: 14px;
  font-family: 'Poppins', sans-serif; font-size: 14px; font-weight: 600;
  letter-spacing: 0.2px; cursor: pointer; text-decoration: none;
  transition: transform 0.1s, background 0.15s, box-shadow 0.2s;
  box-shadow: 0 4px 18px rgba(52, 199, 89, 0.22);
}
.analyse-btn:hover { background: #40d268; box-shadow: 0 6px 24px rgba(52, 199, 89, 0.35); }
.analyse-btn:active { transform: scale(0.98); }
.analyse-btn svg { width: 16px; height: 16px; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.panel-head {
  padding: 10px 16px 6px;
  display: flex; justify-content: space-between; align-items: baseline;
  border-bottom: 1px solid var(--line);
}
.panel-title { font-size: 10px; font-weight: 600; letter-spacing: 1.1px; text-transform: uppercase; color: var(--muted); }
.panel-sub { font-size: 11.5px; color: var(--muted-2); font-family: 'JetBrains Mono', monospace; }

.movelist {
  max-height: 340px; overflow-y: auto; padding: 4px 0;
  scrollbar-width: thin; scrollbar-color: var(--line) transparent;
  overscroll-behavior: contain;   /* prevent scroll chaining to page */
}
.movelist::-webkit-scrollbar { width: 6px; }
.movelist::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }
.move-row {
  display: grid; grid-template-columns: 32px 1fr 1fr;
  align-items: stretch; gap: 2px; padding: 0 6px;
}
.move-num { display: grid; place-items: center; font-family: 'JetBrains Mono', monospace; font-size: 11.5px; color: var(--muted-2); padding: 5px 0; }
.move-cell {
  display: flex; justify-content: space-between; align-items: center;
  padding: 5px 10px; border-radius: 7px; cursor: pointer;
  transition: background 0.1s; gap: 8px; min-height: 30px;
}
.move-cell:hover { background: var(--panel-2); }
.move-cell.active { background: var(--accent); color: #0a0a0a; font-weight: 600; }
.move-cell.active .eval-chip { background: rgba(0,0,0,0.18); color: #0a0a0a; }
.move-cell.empty { cursor: default; }
.move-cell.empty:hover { background: transparent; }
.move-san { font-weight: 500; font-size: 13.5px; }
.eval-chip {
  font-family: 'JetBrains Mono', monospace;
  font-size: 9.5px; font-weight: 600;
  padding: 2px 5px; border-radius: 4px;
  background: var(--panel-2); color: var(--muted);
  white-space: nowrap; flex-shrink: 0;
}
.eval-chip.good { color: var(--good); }
.eval-chip.bad { color: var(--bad); }
.eval-chip.book { color: var(--book); font-style: italic; }

/* ===== MODAL ===== */
.modal-scrim {
  position: fixed; inset: 0;
  background: var(--scrim);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: grid; place-items: center;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s;
  z-index: 120;
  padding: 20px;
}
.modal-scrim.open { opacity: 1; pointer-events: auto; }
.modal {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 0;
  max-width: 440px; width: 100%;
  max-height: 80vh; overflow-y: auto;
  box-shadow: var(--shadow-md);
  transform: translateY(20px) scale(0.96);
  transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.modal-scrim.open .modal { transform: translateY(0) scale(1); }
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--line);
}
.modal-title { font-size: 15px; font-weight: 600; }
.modal-close {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--panel-2); border: 1px solid var(--line);
  color: var(--text); cursor: pointer;
  display: grid; place-items: center;
  transition: background 0.15s;
}
.modal-close:hover { background: color-mix(in srgb, var(--panel-2) 70%, var(--text) 5%); }
.modal-close svg { width: 14px; height: 14px; }
.modal-body { padding: 16px 20px 20px; }
.info-grid {
  display: grid; grid-template-columns: repeat(2, 1fr);
  gap: 14px 20px;
}
.info-item { display: flex; flex-direction: column; gap: 3px; }
.info-label { font-size: 10px; font-weight: 500; color: var(--muted-2); letter-spacing: 0.8px; text-transform: uppercase; }
.info-value { font-size: 13.5px; font-weight: 500; color: var(--text); word-break: break-word; }
.info-value.mono { font-family: 'JetBrains Mono', monospace; }

/* Theme toggle within menu */
.theme-toggle { display: flex; padding: 4px; gap: 4px; background: var(--panel-2); border-radius: 8px; margin: 2px; }
.theme-opt {
  flex: 1; padding: 6px 8px; border-radius: 6px;
  background: transparent; border: none; color: var(--muted);
  font-family: inherit; font-size: 12px; font-weight: 500;
  cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 5px;
  transition: background 0.15s, color 0.15s;
}
.theme-opt.active { background: var(--panel); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,0.15); }
.theme-opt svg { width: 13px; height: 13px; }

/* Toast */
.toast {
  position: fixed; bottom: 24px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--panel); border: 1px solid var(--line);
  color: var(--text); padding: 10px 20px; border-radius: 999px;
  font-size: 12.5px; font-weight: 500;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  box-shadow: var(--shadow-md);
  z-index: 200;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== RESPONSIVE ===== */

/* ---------- Tablet (768-1023px) ---------- */
/* Board top-left; controls + analyse below it on the left; moves fills right. */
@media (min-width: 768px) {
  .topbar-inner { padding: 8px 18px; }
  .brand-logo { width: 28px; height: 28px; }
  .brand-name { font-size: 14.5px; }
  .menu-btn { width: 30px; height: 30px; font-size: 16px; }
  .game-nav { padding: 8px 18px 0; }
  .nav-btn { padding: 5px 10px; font-size: 11.5px; }
  .game-counter { font-size: 12px; }
  .wrap { padding: 10px 18px 28px; }
  .players { padding: 5px 11px; margin-bottom: 8px; }
  .player-chip { width: 24px; height: 24px; font-size: 14px; border-radius: 6px; }
  .player-name { font-size: 12.5px; }
  .result-badge { font-size: 11px; padding: 3px 10px; }

  .game-grid {
    grid-template-columns: minmax(0, 1.35fr) minmax(280px, 1fr);
    grid-template-areas:
      "board     moves"
      "controls  moves"
      "analyse   moves";
    grid-template-rows: auto auto 1fr;
    gap: 12px;
    align-items: start;
  }
  .board-wrap  { grid-area: board; }
  .controls    { grid-area: controls; }
  .analyse-btn { grid-area: analyse; }
  .moves-panel { grid-area: moves; align-self: stretch; }

  .ctrl-btn { height: 34px; }
  .ctrl-btn svg { width: 15px; height: 15px; }
  .analyse-btn { padding: 10px 16px; font-size: 13px; }
  .analyse-btn svg { width: 14px; height: 14px; }
  .panel-head { padding: 8px 14px 5px; }
  .panel-title { font-size: 9.5px; }
  .panel-sub { font-size: 11px; }
  .movelist { max-height: 420px; }
  .move-san { font-size: 13px; }
  .eval-chip { font-size: 9px; padding: 2px 5px; }
  .move-num { font-size: 11px; padding: 4px 0; }
  .move-cell { padding: 4px 9px; min-height: 28px; }
}

/* ---------- Laptop + desktop (≥1024px) ---------- */
/* Board takes the full left column height (bigger board). */
/* Controls + analyse + moves stack in the right column.    */
@media (min-width: 1024px) {
  .wrap { padding: 14px 24px 40px; }
  .game-grid {
    grid-template-columns: minmax(0, 1.9fr) minmax(300px, 1fr);
    grid-template-areas:
      "board  controls"
      "board  analyse"
      "board  moves";
    grid-template-rows: auto auto 1fr;
    gap: 14px;
  }
  .board-wrap  { grid-area: board;    align-self: start; }
  .controls    { grid-area: controls; }
  .analyse-btn { grid-area: analyse; }
  .moves-panel { grid-area: moves;    align-self: stretch; min-height: 0; }
  .evalbar { width: 16px; }
  /* Move list can now stretch to fill remaining right-column height */
  .movelist { max-height: none; height: 100%; }
  .moves-panel { display: flex; flex-direction: column; }
  .moves-panel .movelist { flex: 1; }
}

/* ---------- Mobile (<768px): roomy tap targets ---------- */
@media (max-width: 767px) {
  .wrap { padding: 12px 14px 40px; }
  .ctrl-btn { height: 44px; }
  .ctrl-btn svg { width: 18px; height: 18px; }
  .analyse-btn { padding: 14px 18px; font-size: 14px; }
  .player-side { display: none; }
}
@media (max-width: 480px) {
  .topbar-inner { padding: 9px 12px; }
  .brand-logo { width: 28px; height: 28px; }
  .brand-name { font-size: 14.5px; }
  .game-nav { padding: 9px 12px 0; gap: 6px; }
  .nav-btn { padding: 5px 9px; font-size: 11.5px; }
  .wrap { padding: 10px 12px 32px; }
  .players { padding: 6px 10px; }
  .player-chip { width: 24px; height: 24px; font-size: 14px; }
  .player-name { font-size: 12.5px; }
  .result-badge { font-size: 11px; padding: 3px 10px; }
  .info-grid { grid-template-columns: 1fr 1fr; gap: 12px 14px; }
  .evalbar { width: 12px; }
}

/* ===== MAGNUS ADDITIONS ===== */
.actions { display: contents; }
@media (min-width: 768px) {
  .actions { display: flex; gap: 10px; grid-area: analyse; }
}
.actions > .analyse-btn,
.actions > .practice-btn { flex: 1; width: auto; }

.practice-btn {
  display: flex; align-items: center; justify-content: center; gap: 9px;
  width: 100%; padding: 12px 18px;
  background: #0a84ff; color: #fff;
  border: none; border-radius: 14px;
  font-family: 'Poppins', sans-serif; font-size: 14px; font-weight: 600;
  letter-spacing: 0.2px; cursor: pointer; text-decoration: none;
  transition: transform 0.1s, background 0.15s, box-shadow 0.2s;
  box-shadow: 0 4px 18px rgba(10, 132, 255, 0.22);
}
.practice-btn:hover { background: #1a94ff; box-shadow: 0 6px 24px rgba(10, 132, 255, 0.32); }
.practice-btn:active { transform: scale(0.98); }
.practice-btn svg { width: 16px; height: 16px; }
@media (min-width: 768px) {
  .practice-btn { padding: 10px 16px; font-size: 13px; width: auto; }
  .practice-btn svg { width: 14px; height: 14px; }
}
@media (min-width: 1024px) { .practice-btn { padding: 14px 18px; font-size: 14px; } }