/* ============================================================
   Chess — Two Player + Stockfish
   Dark UI, classic board. No external assets.
   ============================================================ */

:root {
  --bg: #151821;
  --bg-grad: radial-gradient(1200px 700px at 20% -10%, #1d2333 0%, #151821 55%);
  --panel: #1d222d;
  --panel-2: #232a38;
  --line: #2e3648;
  --text: #e8ebf2;
  --text-dim: #9aa4b8;
  --accent: #4f9cf7;
  --accent-2: #3b82d6;
  --gold: #e5c15c;
  --danger: #e5533d;
  --ok: #43a564;

  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
  --sq-hint: rgba(20, 85, 20, 0.55);
  --sq-last: rgba(155, 199, 0, 0.42);
  --sq-sel: rgba(20, 85, 20, 0.75);
  --sq-check: rgba(229, 83, 61, 0.75);

  --radius: 10px;
  /* NOTE: no percentage terms here — a % inside this min() resolves
     differently for width (parent's width) vs height/font-size and makes
     the board collapse. Container clamping happens on .board instead. */
  --board-size: min(78vh, 680px);
}

* { box-sizing: border-box; }

/* Monochrome chess glyphs (Noto Sans Symbols 2 subset to U+2654-265F, OFL).
   Without this, phones route ♟ (U+265F — the only chess char with default
   emoji presentation) to the color-emoji font, so pawns render as uncolorable
   black emoji regardless of the .w/.b CSS color. */
@font-face {
  font-family: "ChessGlyphs";
  src: url("vendor/fonts/chess-symbols.woff") format("woff");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
  unicode-range: U+2654-265F;
}

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg) var(--bg-grad) no-repeat fixed;
  color: var(--text);
  font: 15px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(21, 24, 33, 0.85);
  backdrop-filter: blur(6px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: baseline; gap: 10px; }
.brand-piece { font-size: 26px; color: var(--gold); }
.brand h1 { font-size: 20px; margin: 0; letter-spacing: 0.5px; }
.brand-sub { color: var(--text-dim); font-size: 12.5px; }

/* ---------- Layout ---------- */

.layout {
  display: flex;
  gap: 20px;
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  align-items: flex-start;
  flex-wrap: wrap;
}

.board-wrap {
  position: relative;
  flex: 1 1 480px;
  min-width: 300px;
  display: flex;
  justify-content: center;
  /* query container so the board can clamp to its own column width (100cqw) */
  container-type: inline-size;
}

.panel {
  flex: 1 1 300px;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 280px;
}

/* ---------- Board ---------- */

.board {
  --sq: calc(min(var(--board-size), 100cqw) / 8);
  width: var(--board-size);
  width: min(var(--board-size), 100cqw);
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.55), 0 0 0 1px #000;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  position: relative;
}

.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.sq.light { background: var(--sq-light); }
.sq.dark  { background: var(--sq-dark); }

.sq.lastmove::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--sq-last);
  pointer-events: none;
}

.sq.selected::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--sq-sel);
  pointer-events: none;
}

.sq.check::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 50%, var(--sq-check) 0%, rgba(229, 83, 61, 0.25) 62%, transparent 75%);
  pointer-events: none;
}

.sq .coord {
  position: absolute;
  font-size: calc(var(--sq) * 0.16);
  font-weight: 600;
  pointer-events: none;
  opacity: 0.85;
}
.sq .coord.file { right: 4%; bottom: 2%; }
.sq .coord.rank { left: 5%; top: 3%; }
.sq.light .coord { color: var(--sq-dark); }
.sq.dark  .coord { color: var(--sq-light); }

/* Move hints */
.sq.hint::after {
  content: "";
  position: absolute;
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgba(30, 60, 20, 0.5);
  pointer-events: none;
}
.sq.hint-capture::after {
  content: "";
  position: absolute;
  inset: 6%;
  border-radius: 50%;
  border: calc(var(--sq) * 0.07) solid rgba(30, 60, 20, 0.55);
  background: transparent;
  pointer-events: none;
}
.sq.drag-over { box-shadow: inset 0 0 0 3px rgba(255, 220, 100, 0.9); }

/* Pieces */
.piece {
  position: relative;
  z-index: 2;
  font-family: "ChessGlyphs", "Noto Sans Symbols 2", "Segoe UI Symbol", "DejaVu Sans", "Arial Unicode MS", sans-serif;
  font-size: calc(var(--sq) * 0.74);
  line-height: 1;
  pointer-events: none;
  transition: transform 0.08s ease-out;
}

.piece.w {
  color: #f9f6ef;
  text-shadow:
    0 1px 0 rgba(0, 0, 0, 0.55),
    0 0 3px rgba(0, 0, 0, 0.65),
    0 0 1px rgba(0, 0, 0, 0.9);
}

.piece.b {
  color: #1d1a17;
  text-shadow:
    0 1px 0 rgba(255, 255, 255, 0.18),
    0 0 2px rgba(0, 0, 0, 0.5);
}

.sq.drag-src .piece { visibility: hidden; }

/* Floating drag ghost */
.ghost-piece {
  position: fixed;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  font-family: "ChessGlyphs", "Noto Sans Symbols 2", "Segoe UI Symbol", "DejaVu Sans", "Arial Unicode MS", sans-serif;
  transform: translate(-50%, -58%);
}
.ghost-piece .piece { font-size: 100%; }

/* ---------- Overlays ---------- */

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 12, 18, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  border-radius: 6px;
}
.overlay.hidden { display: none; }

.overlay-card {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 26px 34px;
  text-align: center;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6);
  max-width: 86%;
}
.overlay-icon { font-size: 46px; margin-bottom: 6px; }
.overlay-card h2 { margin: 0 0 4px; font-size: 26px; }
.overlay-card p { margin: 0 0 18px; color: var(--text-dim); }
.overlay-actions { display: flex; gap: 10px; justify-content: center; }

/* Promotion */
.promo-card {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6);
}
.promo-pieces { display: flex; gap: 10px; }
.promo-pieces button {
  width: 64px;
  height: 64px;
  font-size: 44px;
  line-height: 1;
  pointer-events: auto; /* .piece sets pointer-events:none — must override or buttons are unclickable */
  background: var(--sq-light);
  border: 2px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  font-family: "ChessGlyphs", "Noto Sans Symbols 2", "Segoe UI Symbol", "DejaVu Sans", "Arial Unicode MS", sans-serif;
  transition: transform 0.06s ease-out, border-color 0.06s;
}
.promo-pieces button:hover { transform: scale(1.07); border-color: var(--accent); }

/* ---------- Status bar ---------- */

.status {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-weight: 600;
}

.turn-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: #f4f1e8;
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.4);
}
.turn-dot.b { background: #23201d; box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.25); }
.turn-dot.pulse { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.25); } }

/* ---------- Engine box ---------- */

.engine-box {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.engine-row { display: flex; align-items: center; gap: 10px; }
.engine-row label { width: 78px; color: var(--text-dim); font-size: 13px; flex: 0 0 auto; }

.seg {
  display: flex;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  flex: 1;
}
.seg button {
  flex: 1;
  padding: 7px 6px;
  background: transparent;
  color: var(--text-dim);
  border: 0;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.seg button + button { border-left: 1px solid var(--line); }
.seg button:hover { color: var(--text); }
.seg button.active { background: var(--accent-2); color: #fff; font-weight: 600; }

.engine-box.dimmed .seg button[data-mode]:not([data-mode="pp"]),
.engine-box.dimmed #diffSeg { opacity: 0.45; }

.engine-status {
  font-size: 12.5px;
  color: var(--text-dim);
  min-height: 16px;
  border-top: 1px dashed var(--line);
  padding-top: 8px;
}
.engine-status.busy { color: var(--gold); }
.engine-status.error { color: var(--danger); }

/* ---------- Players ---------- */

.players {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 12px;
  transition: border-color 0.15s;
}
.player.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent-2); }

.player-name {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 14px;
}

.dot { width: 12px; height: 12px; border-radius: 50%; flex: 0 0 auto; }
.dot.w { background: #f4f1e8; box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.45); }
.dot.b { background: #23201d; box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.25); }

.captured {
  min-height: 20px;
  font-size: 17px;
  letter-spacing: 1px;
  color: var(--text-dim);
  font-family: "ChessGlyphs", "Noto Sans Symbols 2", "Segoe UI Symbol", "DejaVu Sans", "Arial Unicode MS", sans-serif;
}
.captured .mat { font-size: 12px; color: var(--gold); font-weight: 700; margin-left: 6px; letter-spacing: 0; }

/* ---------- Move list ---------- */

.moves-wrap {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.moves-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  font-weight: 700;
  font-size: 13px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.moves-nav { display: flex; gap: 4px; align-items: center; }

.live-badge {
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 1px;
  color: var(--ok);
  border: 1px solid var(--ok);
  border-radius: 5px;
  padding: 2px 6px;
}
.live-badge.hidden { display: none; }

.move-list {
  list-style: none;
  margin: 0;
  padding: 6px 8px;
  overflow-y: auto;
  max-height: 220px;
  min-height: 44px;
  font-variant-numeric: tabular-nums;
}
.move-list:empty::after {
  content: "No moves yet — White starts.";
  color: var(--text-dim);
  font-size: 13px;
  padding: 6px;
}

.mv-row { display: flex; align-items: center; gap: 4px; }
.mv-row:nth-child(odd) { background: rgba(255, 255, 255, 0.03); }

.mv-num {
  width: 30px;
  flex: 0 0 auto;
  color: var(--text-dim);
  font-size: 13px;
  text-align: right;
  padding-right: 4px;
}

.mv {
  flex: 1;
  background: transparent;
  border: 0;
  color: var(--text);
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
}
.mv:hover { background: var(--panel-2); }
.mv.active { background: var(--accent-2); color: #fff; font-weight: 700; }
.mv:disabled { opacity: 0.35; cursor: default; }

/* ---------- Buttons ---------- */

.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.12s, transform 0.05s, border-color 0.12s;
  font-family: inherit;
}
.btn:hover { background: #2b3345; border-color: #3b4666; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent); }

.btn.small { padding: 6px 10px; font-size: 13px; }

.btn.mini {
  padding: 3px 8px;
  font-size: 12px;
  border-radius: 6px;
}

.btn.icon { font-size: 17px; padding: 6px 10px; }

.controls { display: flex; gap: 8px; flex-wrap: wrap; }
.controls .btn { flex: 1; }

/* ---------- Load FEN ---------- */

.loadbox {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 12px;
}
.loadbox summary { cursor: pointer; color: var(--text-dim); font-size: 13px; font-weight: 600; }
.loadbox textarea {
  width: 100%;
  margin-top: 8px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 12px;
  resize: vertical;
}
.loadbox .controls { margin-top: 8px; }
.loadbox .controls .btn { flex: 0 0 auto; }

.msg { font-size: 12.5px; color: var(--danger); align-self: center; }
.msg.ok { color: var(--ok); }

/* ---------- Footnote ---------- */

.footnote {
  color: var(--text-dim);
  font-size: 12px;
  margin: 2px 4px 8px;
}

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  :root { --board-size: min(62vh, 92vw); }
  .layout { padding: 12px; gap: 14px; }
  .panel { max-width: none; width: 100%; }
  .brand-sub { display: none; }
}
