:root {
  --bg: #161512;
  --surface: #23211d;
  --surface-2: #2e2b26;
  --border: #3a3631;
  --text: #ece8e1;
  --muted: #a39d93;
  --accent: #7fa650;
  --accent-strong: #95bb4a;
  --danger: #d8584a;
  --warn: #e0a94a;

  --sq-light: #eeeed2;
  --sq-dark: #769656;
  --hl-last: rgba(255, 221, 60, 0.42);
  --hl-selected: rgba(20, 85, 30, 0.5);
  --hint: rgba(255, 170, 0, 0.85);

  --board: min(92vw, calc(100vh - 210px), 660px);
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button, select { font: inherit; color: inherit; }

/* ---------- Header ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 1120px;
  margin: 0 auto;
  padding: 18px 16px 6px;
}
.logo { width: 40px; height: 40px; }
.topbar h1 { margin: 0; font-size: 20px; letter-spacing: 0.2px; }
.topbar p { margin: 0; color: var(--muted); font-size: 13px; }

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: var(--board) minmax(280px, 360px);
  gap: 24px;
  justify-content: center;
  max-width: 1120px;
  margin: 0 auto;
  padding: 10px 16px 32px;
  align-items: start;
}

@media (max-width: 900px) {
  :root { --board: min(calc(100vw - 32px), 620px); }
  .layout { grid-template-columns: var(--board); }
}

.board-col { display: flex; flex-direction: column; gap: 8px; }

.player-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 36px;
}
.avatar {
  width: 32px; height: 32px;
  background: var(--surface-2);
  border-radius: 6px;
  padding: 2px;
}
.player-name { font-weight: 600; }
.elo-chip {
  font-size: 12px;
  color: var(--muted);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 2px 9px;
}
.thinking-dots { display: none; gap: 4px; margin-left: 4px; }
.player-bar.thinking .thinking-dots { display: inline-flex; }
.thinking-dots i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
  animation: blink 1s infinite ease-in-out;
}
.thinking-dots i:nth-child(2) { animation-delay: 0.15s; }
.thinking-dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

/* ---------- Board ---------- */

.board-wrap {
  position: relative;
  width: var(--board);
  height: var(--board);
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.sq { position: relative; }
.sq.light { background: var(--sq-light); }
.sq.dark { background: var(--sq-dark); }
.sq.last::before,
.sq.selected::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--hl-last);
}
.sq.selected::before { background: var(--hl-selected); }
.sq.check {
  background-image: radial-gradient(circle, rgba(255, 30, 30, 0.95) 0%, rgba(230, 0, 0, 0.6) 35%, rgba(169, 0, 0, 0) 75%);
}
.sq.target::after {
  content: "";
  position: absolute;
  left: 50%; top: 50%;
  width: 30%; height: 30%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(20, 85, 30, 0.45);
  z-index: 2;
  pointer-events: none;
}
.sq.capture::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  box-shadow: inset 0 0 0 calc(var(--board) / 8 * 0.08) rgba(20, 85, 30, 0.5);
  z-index: 2;
  pointer-events: none;
}

.coord {
  position: absolute;
  font-size: calc(var(--board) / 8 * 0.18);
  font-weight: 700;
  pointer-events: none;
  z-index: 1;
}
.coord.rank { top: 3%; left: 5%; }
.coord.file { bottom: 2%; right: 6%; }
.sq.light .coord { color: var(--sq-dark); }
.sq.dark .coord { color: var(--sq-light); }

.piece {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 3;
  cursor: grab;
  will-change: transform;
}
.piece.dragging { opacity: 0.3; }
.piece.ghost {
  position: fixed;
  inset: auto;
  z-index: 1000;
  pointer-events: none;
  cursor: grabbing;
  filter: drop-shadow(0 6px 8px rgba(0, 0, 0, 0.35));
}

.arrows {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
}
.arrows line { stroke: var(--hint); stroke-width: 0.17; stroke-linecap: round; }
.arrows polygon { fill: var(--hint); }

/* Promotion picker */
.promo {
  position: absolute;
  top: 0;
  width: 12.5%;
  display: flex;
  flex-direction: column;
  z-index: 20;
  background: #fff;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
  border-radius: 0 0 6px 6px;
}
.promo[hidden] { display: none; }
.promo button {
  aspect-ratio: 1;
  border: 0;
  padding: 6%;
  background: #f3f3f3;
  cursor: pointer;
}
.promo button:hover { background: var(--accent-strong); }
.promo img { width: 100%; height: 100%; display: block; }

/* Overlay (loading / results) */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
}
.overlay[hidden] { display: none; }
.overlay.busy { background: rgba(22, 21, 18, 0.55); backdrop-filter: blur(2px); }
.overlay.result { background: rgba(22, 21, 18, 0.35); }
.overlay-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 22px;
  text-align: center;
  max-width: 340px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5);
}
.overlay-card p { margin: 6px 0 0; color: var(--muted); }
.result-title { font-size: 21px; font-weight: 700; }
.overlay.win .result-title { color: var(--accent-strong); }
.overlay.loss .result-title { color: var(--danger); }
.overlay.draw .result-title { color: var(--warn); }
.overlay-actions { display: flex; gap: 8px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }

.spinner {
  width: 30px; height: 30px;
  margin: 0 auto 8px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Panel ---------- */

.panel { display: flex; flex-direction: column; gap: 12px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
.card h2 {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.status-card .eg-name { font-size: 18px; font-weight: 700; }
.status-card .eg-eval { color: var(--muted); font-size: 13px; min-height: 19px; }
.status {
  margin-top: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface-2);
  font-weight: 500;
  min-height: 38px;
}
.status:empty { display: none; }
.status[data-kind="win"] { color: var(--accent-strong); }
.status[data-kind="loss"] { color: var(--danger); }
.status[data-kind="draw"] { color: var(--warn); }
.status[data-kind="thinking"] { color: var(--muted); }

.actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.actions .primary { grid-column: 1 / -1; }

.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 8px;
  padding: 10px 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, transform 0.05s;
}
.btn:hover:not(:disabled) { background: #3a362f; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-strong); }

.bots { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.bots button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 8px 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  cursor: pointer;
}
.bots button:hover { border-color: var(--muted); }
.bots button.active { border-color: var(--accent); background: rgba(127, 166, 80, 0.18); }
.bots .elo { font-weight: 700; font-size: 15px; }
.bots .bot-name { font-size: 11px; color: var(--muted); }
.blurb { margin: 8px 0 0; color: var(--muted); font-size: 13px; }

.select {
  width: 100%;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.moves-scroll { max-height: 220px; overflow-y: auto; }
.moves {
  margin: 0;
  padding-left: 2.2em;
  font-variant-numeric: tabular-nums;
}
.moves li { padding: 2px 0; color: var(--muted); }
.moves li span { display: inline-block; min-width: 4.5em; color: var(--text); font-weight: 500; }
.moves-scroll .blurb { margin: 0; }
