/* ═══════════════════════════════════════════════════════════════════════════
   Chess — black and green chrome, with the board doing the talking.

   The board is DOM (not canvas) because the E2E suite asserts against it AND
   because chess needs pieces to be ELEMENTS for drag and drop to work at all.
   Squares carry data-square / data-file / data-rank (always the LOGICAL square,
   so a flipped board does not lie about where a piece is); the piece is a child
   element with data-piece / data-colour / data-role.

   Coordinates are two strips beside and below the grid, rebuilt on flip, which
   is what makes "the pieces stay but the numbers turn over" assertable.
   ═══════════════════════════════════════════════════════════════════════════ */

:root {
  --font-display: 'Bebas Neue', 'DM Sans', system-ui, sans-serif;
  --font-body: 'DM Sans', system-ui, -apple-system, sans-serif;
  --font-mono: 'DM Mono', ui-monospace, 'SF Mono', Menlo, monospace;

  --bg: #000000;
  --surface: #0b0b0b;
  --surface2: #131313;
  --surface3: #1c1c1c;
  --border: #232323;
  --border2: #2f2f2f;

  --text: #f5f5f5;
  --muted: #8a8a8a;

  --green: #1db954;
  --green-bright: #1ed760;
  --green-dim: #0f5c2a;
  --red: #e91429;
  --amber: #f5c518;

  /* The board: light squares are the "paper", dark squares are the dark green of
     the chrome. The two piece colours deliberately do NOT reuse the accent green
     for both sides — a green piece and a green dot would be one green too many. */
  --sq-light: #dfe4da;
  --sq-dark: #1c2b21;
  --sq-light-hover: #eef2e8;
  --sq-dark-hover: #2a4031;
  --piece-white: #f7f9f4;
  --piece-white-edge: #8b948b;
  --piece-black: #14181a;
  --piece-black-edge: #5a6366;
  --target: #1db954;
  --target-ring: rgba(30, 215, 96, 0.9);
  --last-move: #f5c518;
  --check: #e91429;
  --select: #1ed760;
}

html[data-theme='light'] {
  --bg: #f4f6f4;
  --surface: #ffffff;
  --surface2: #f0f3f0;
  --surface3: #e6eae6;
  --border: #d8ded8;
  --border2: #c6cec6;
  --text: #101410;
  --muted: #5c665c;
  --sq-light: #f0f2ec;
  --sq-dark: #8aa892;
  --sq-light-hover: #fafbf7;
  --sq-dark-hover: #7a9a83;
  --piece-white: #ffffff;
  --piece-white-edge: #6d766d;
  --piece-black: #20242a;
  --piece-black-edge: #59606a;
}

/* MANDATORY: an overlay that is only visually hidden still swallows clicks. */
[hidden] { display: none !important; }

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

.shell { max-width: 1120px; margin: 0 auto; padding: 14px 16px 44px; }

/* ── Header ────────────────────────────────────────────────────────────── */

.header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--border);
}
.home-btn {
  display: inline-flex; align-items: center; gap: 5px;
  font-family: var(--font-mono); font-size: 12px; letter-spacing: .08em;
  color: var(--muted); text-decoration: none; padding: 6px 10px;
  border: 1px solid var(--border); border-radius: 8px;
  transition: color .15s, border-color .15s;
}
.home-btn svg { width: 14px; height: 14px; }
.home-btn:hover { color: var(--green); border-color: var(--green-dim); }
.brand { display: flex; align-items: center; gap: 8px; }
.brand-mark { font-size: 22px; color: var(--green); line-height: 1; }
.brand-text { font-family: var(--font-display); font-size: 22px; letter-spacing: .14em; }
.header-right { display: flex; align-items: center; gap: 8px; }
.user-badge { font-family: var(--font-mono); font-size: 11px; color: var(--muted); }
.icon-btn {
  width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface2); color: var(--muted); cursor: pointer;
  display: flex; align-items: center; justify-content: center; padding: 0;
  transition: color .15s, border-color .15s;
}
.icon-btn svg { width: 17px; height: 17px; }
.icon-btn:hover { color: var(--green); border-color: var(--green-dim); }

/* ── Screens ───────────────────────────────────────────────────────────── */

.screen { display: none; padding-top: 18px; }
.screen.active { display: block; }

.screen-title {
  font-family: var(--font-display); font-size: 40px; letter-spacing: .04em;
  margin: 0 0 6px;
}
.screen-sub { color: var(--muted); font-size: 14px; line-height: 1.55; margin: 0 0 18px; max-width: 78ch; }

.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row.between { justify-content: space-between; align-items: flex-start; }

/* ── Buttons and fields ────────────────────────────────────────────────── */

.btn {
  font-family: var(--font-display); font-size: 16px; letter-spacing: .08em;
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border2); border-radius: 10px;
  padding: 9px 16px; cursor: pointer;
  transition: background .15s, border-color .15s, color .15s, transform .1s;
}
.btn:hover { border-color: var(--green); color: var(--green); }
.btn:active { transform: translateY(1px); }
.btn:disabled, .btn[aria-disabled='true'] { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--green); border-color: var(--green); color: #04140a; font-weight: 700; }
.btn-primary:hover { background: var(--green-bright); color: #04140a; }
.btn-sm { font-size: 13px; padding: 6px 11px; border-radius: 8px; }
.btn-danger:hover { border-color: var(--red); color: var(--red); }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 10px; }
.field-label, .field label {
  font-family: var(--font-mono); font-size: 10.5px; letter-spacing: .06em;
  color: var(--muted); text-transform: uppercase;
}
.field input, .field select, select, input[type="text"], input[type="number"], textarea {
  background: var(--surface2); border: 1px solid var(--border2); border-radius: 8px;
  color: var(--text); padding: 8px 10px; font-family: var(--font-body); font-size: 14px;
}
textarea { font-family: var(--font-mono); font-size: 12px; resize: vertical; width: 100%; }
input[type="number"] { width: 92px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.checkbox-row input { width: 16px; height: 16px; accent-color: var(--green); }
.checkbox-row label { font-size: 13.5px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: var(--font-mono); }
.hidden { display: none !important; }

/* ── Menu ──────────────────────────────────────────────────────────────── */

.menu-grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}
.mode-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px; display: flex; flex-direction: column; gap: 8px;
  transition: border-color .15s, transform .15s;
}
.mode-card:hover { border-color: var(--green-dim); transform: translateY(-2px); }
.mode-icon { font-size: 26px; line-height: 1; }
.mode-name { font-family: var(--font-display); font-size: 22px; letter-spacing: .06em; }
.mode-desc { color: var(--muted); font-size: 13px; line-height: 1.5; flex: 1; }
.mode-form { display: flex; flex-direction: column; gap: 8px; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; min-width: 0; font-family: var(--font-mono); letter-spacing: .2em; text-align: center; }

.banner {
  margin-top: 16px; padding: 10px 14px; border-radius: 10px;
  background: var(--surface2); border: 1px solid var(--border2);
  font-size: 13.5px;
}
.banner.error { border-color: var(--red); color: #ff9aa5; }
.banner.good { border-color: var(--green-dim); color: var(--green); }

/* ── HUD ───────────────────────────────────────────────────────────────── */

.hud {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: center;
  justify-content: space-between; padding: 10px 0 12px;
  border-bottom: 1px solid var(--border); margin-bottom: 12px;
}
.hud-left { display: flex; align-items: center; gap: 10px; }
.turn-dot {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--border2); background: var(--piece-white);
  transition: background .2s;
}
.turn-dot.black { background: var(--piece-black); }
.hud-turns { display: flex; flex-direction: column; }
.hud-label { font-family: var(--font-mono); font-size: 10px; letter-spacing: .08em; color: var(--muted); text-transform: uppercase; }
.hud-value { font-family: var(--font-display); font-size: 20px; letter-spacing: .04em; }
.hud-stats { display: flex; gap: 16px; flex-wrap: wrap; }
.hud-stat { display: flex; flex-direction: column; }
.hud-mode { font-family: var(--font-mono); font-size: 13px; }
.hud-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.online-strip {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  background: var(--surface2); border: 1px solid var(--border2); border-radius: 10px;
  padding: 8px 12px; margin-bottom: 10px; font-size: 13px;
}
.online-strip strong { font-family: var(--font-mono); font-size: 18px; letter-spacing: .18em; color: var(--green); }

.check-banner {
  display: flex; gap: 8px; align-items: center;
  background: rgba(233, 20, 41, .12); border: 1px solid var(--red); color: #ff9aa5;
  border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; font-size: 13.5px;
}
.check-icon { font-size: 16px; }

/* ── Board ─────────────────────────────────────────────────────────────── */

.board-wrap {
  display: grid; gap: 18px;
  grid-template-columns: minmax(0, 1fr) 260px;
  align-items: start;
}
@media (max-width: 780px) { .board-wrap { grid-template-columns: 1fr; } }

/* rank strip on the left, file strip underneath, board in the corner cell */
.board-area {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) 18px;
  gap: 3px;
  width: 100%;
  max-width: min(88vw, 620px);
}
.rank-strip {
  grid-row: 1; grid-column: 1;
  display: grid; grid-template-rows: repeat(8, 1fr);
  font-family: var(--font-mono); font-size: 10px; color: var(--muted);
}
.rank-strip span { display: flex; align-items: center; justify-content: center; }
.file-strip {
  grid-row: 2; grid-column: 2;
  display: grid; grid-template-columns: repeat(8, 1fr);
  font-family: var(--font-mono); font-size: 10px; color: var(--muted);
  text-align: center;
}
.rank-strip.hidden, .file-strip.hidden { visibility: hidden; }

.board {
  grid-row: 1; grid-column: 2;
  display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1 / 1; width: 100%;
  border: 2px solid var(--border2); border-radius: 8px; overflow: hidden;
  background: var(--sq-dark);
}
.square {
  position: relative; display: flex; align-items: center; justify-content: center;
  cursor: pointer; user-select: none;
}
.square.light { background: var(--sq-light); }
.square.dark { background: var(--sq-dark); }
.square.light:hover { background: var(--sq-light-hover); }
.square.dark:hover { background: var(--sq-dark-hover); }
.square.selected { box-shadow: inset 0 0 0 3px var(--select); }
.square.last-move::before {
  content: ''; position: absolute; inset: 0; background: var(--last-move); opacity: .22;
}
.square.in-check::before {
  content: ''; position: absolute; inset: 0;
  background: radial-gradient(circle, rgba(233, 20, 41, .75) 0%, rgba(233, 20, 41, 0) 72%);
}
.square .dot {
  position: absolute; width: 26%; height: 26%; border-radius: 50%;
  background: var(--target); opacity: .55; pointer-events: none;
}
.square.target-capture .dot {
  width: 88%; height: 88%; background: transparent;
  border: 4px solid var(--target-ring); opacity: .9;
}

/* A piece is an ELEMENT so drag and drop works. The glyph is set in JS. */
.piece {
  position: relative; z-index: 2;
  font-size: min(7.4vw, 52px); line-height: 1;
  cursor: grab; user-select: none;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, .5));
  transition: transform .08s;
}
.piece[data-colour='white'] { color: var(--piece-white); text-shadow: 0 0 1px var(--piece-white-edge), 0 1px 2px rgba(0,0,0,.45); }
.piece[data-colour='black'] { color: var(--piece-black); text-shadow: 0 0 1px var(--piece-black-edge), 0 1px 2px rgba(255,255,255,.12); }
.piece.dragging { opacity: .5; }
.square.drop-hover { box-shadow: inset 0 0 0 3px var(--green-bright); }

@media (max-width: 780px) { .piece { font-size: min(10vw, 46px); } }

/* ── Side panel ────────────────────────────────────────────────────────── */

.side-panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 14px;
}
.panel-title { font-family: var(--font-display); font-size: 20px; letter-spacing: .06em; margin: 0 0 10px; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px; }
.move-list {
  margin: 0 0 12px; padding-left: 20px; max-height: 320px; overflow-y: auto;
  font-family: var(--font-mono); font-size: 13px; line-height: 1.7;
}
.move-list.two-col { columns: 2; column-gap: 14px; }
.move-list li { break-inside: avoid; }
.move-empty { list-style: none; margin-left: -20px; color: var(--muted); }
.captured { border-top: 1px solid var(--border); padding-top: 10px; margin-top: 4px; }

/* ── I/O panel ─────────────────────────────────────────────────────────── */

.io-panel {
  margin-top: 16px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 14px;
}
.io-panel summary { cursor: pointer; font-family: var(--font-display); font-size: 18px; letter-spacing: .06em; }
.io-grid { display: grid; gap: 16px; grid-template-columns: 1fr 1fr; margin-top: 12px; }
@media (max-width: 780px) { .io-grid { grid-template-columns: 1fr; } }

/* ── Puzzles & settings grids ──────────────────────────────────────────── */

.puzzle-list { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); margin-bottom: 16px; }
.puzzle-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 14px; display: flex; flex-direction: column; gap: 6px; text-align: left;
}
.puzzle-card:hover { border-color: var(--green-dim); }
.puzzle-card.solved { border-color: var(--green); }
.puzzle-name { font-family: var(--font-display); font-size: 20px; letter-spacing: .05em; }
.puzzle-blurb { color: var(--muted); font-size: 12.5px; line-height: 1.5; flex: 1; }
.puzzle-meta { font-family: var(--font-mono); font-size: 11px; color: var(--green); }
.puzzle-tick { font-size: 15px; }

.settings-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }

/* ── Overlays ──────────────────────────────────────────────────────────── */

.overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .72);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 18px;
}
.overlay-card {
  background: var(--surface); border: 1px solid var(--border2); border-radius: 16px;
  padding: 20px; max-width: 420px; width: 100%; text-align: center;
}
.overlay-title { font-family: var(--font-display); font-size: 30px; letter-spacing: .05em; margin: 0 0 8px; }
.overlay-body { color: var(--muted); font-size: 13.5px; line-height: 1.5; }
.overlay-actions { display: flex; gap: 10px; justify-content: center; margin-top: 14px; }
.promotion-row { display: flex; gap: 10px; justify-content: center; margin: 12px 0; }
.promo-btn {
  font-size: 30px; line-height: 1; padding: 8px 12px; border-radius: 10px;
  background: var(--surface2); border: 1px solid var(--border2); color: var(--text);
  cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.promo-btn span { font-family: var(--font-mono); font-size: 10px; color: var(--muted); }
.promo-btn:hover { border-color: var(--green); color: var(--green); }
