:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #0f172a;
  --border: #334155;
  --accent: #38bdf8;
  --accent-hover: #0284c7;
  --text: #f8fafc;
  --text-dim: #94a3b8;
  --text-muted: #64748b;
  --success: #22c55e;
  --warn: #eab308;
  --danger: #ef4444;
  --board-wood: #d4a055;
  --board-line: #7c4f1a;
  --stone-black-hi: #475569;
  --stone-black-lo: #090d16;
  --stone-white-hi: #ffffff;
  --stone-white-lo: #cbd5e1;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ─── Header ─────────────────────────────────────────────────── */
header {
  width: 100%;
  max-width: 1080px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}

.header-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.3rem;
  font-weight: 700;
}

.badge {
  font-size: 0.7rem;
  background: #075985;
  color: #bae6fd;
  padding: 3px 9px;
  border-radius: 9999px;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.badge.gpu  { background: #065f46; color: #6ee7b7; }
.badge.wasm { background: #44370e; color: #fde68a; }

.header-links { display: flex; gap: 12px; }
.header-links a {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.85rem;
  transition: color 0.2s;
}
.header-links a:hover { color: var(--accent); }

/* ─── Download Banner ────────────────────────────────────────── */
#downloadBanner {
  width: 100%;
  max-width: 1080px;
  margin: 12px 20px 0;
  background: #172554;
  border: 1px solid #1d4ed8;
  border-radius: 10px;
  padding: 14px 18px;
  display: none;
}

#downloadBanner.visible { display: block; }

.banner-title {
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.banner-sub {
  font-size: 0.82rem;
  color: #93c5fd;
  margin-bottom: 10px;
}

.progress-bar-outer {
  height: 8px;
  background: #1e3a8a;
  border-radius: 4px;
  overflow: hidden;
}

.progress-bar-inner {
  height: 100%;
  background: var(--accent);
  border-radius: 4px;
  transition: width 0.3s ease;
  width: 0%;
}

.progress-label {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 5px;
}

/* ─── Status toast ───────────────────────────────────────────── */
#statusToast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 0.85rem;
  color: var(--text-dim);
  display: none;
  z-index: 100;
  max-width: 400px;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}
#statusToast.visible { display: block; }
#statusToast.error { border-color: var(--danger); color: #fca5a5; }
#statusToast.success { border-color: var(--success); color: #86efac; }

/* ─── Main Layout ────────────────────────────────────────────── */
main {
  width: 100%;
  max-width: 1080px;
  padding: 20px;
  display: grid;
  grid-template-columns: auto 320px;
  gap: 20px;
  align-items: start;
}

@media (max-width: 820px) {
  main { grid-template-columns: 1fr; justify-items: center; }
}

/* ─── Board ──────────────────────────────────────────────────── */
.board-wrap {
  background: var(--surface);
  border-radius: 14px;
  padding: 16px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

#goBoard {
  border-radius: 6px;
  cursor: crosshair;
  touch-action: none;
}

.board-info {
  display: flex;
  gap: 20px;
  font-size: 0.88rem;
  color: var(--text-dim);
}
.board-info strong { color: var(--text); }

/* ─── Sidebar ────────────────────────────────────────────────── */
.sidebar { display: flex; flex-direction: column; gap: 16px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}

.panel-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  font-weight: 700;
  margin-bottom: 12px;
}

/* Winrate bar */
.winrate-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  margin-bottom: 5px;
}
.winrate-bar {
  height: 12px;
  background: #e2e8f0;
  border-radius: 6px;
  overflow: hidden;
  display: flex;
}
.winrate-black {
  height: 100%;
  background: #0f172a;
  transition: width 0.4s ease;
}

/* Backend indicator */
.backend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  font-size: 0.82rem;
  color: var(--text-dim);
}
.backend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
}
.backend-dot.gpu  { background: var(--success); }
.backend-dot.wasm { background: var(--warn); }
.backend-dot.loading { background: var(--accent); animation: pulse 1s infinite; }

@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.3} }

/* Controls */
.controls { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }

button {
  background: #334155;
  color: var(--text);
  border: none;
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
}
button:hover:not(:disabled) { background: #475569; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary {
  background: var(--accent);
  color: #0f172a;
  grid-column: span 2;
}
button.primary:hover:not(:disabled) { background: var(--accent-hover); }
button.danger { background: #7f1d1d; }
button.danger:hover:not(:disabled) { background: var(--danger); }

/* Size selector */
.size-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 10px;
}
.size-btn {
  flex: 1;
  min-width: 60px;
  padding: 6px;
  font-size: 0.82rem;
}
.size-btn.active {
  background: #0369a1;
  color: #e0f2fe;
}

/* Toggle Switch */
.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  padding: 8px 12px;
  background: var(--surface2);
  border-radius: 8px;
  border: 1px solid var(--border);
}
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 38px;
  height: 20px;
  flex-shrink: 0;
}
.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background-color: #334155;
  transition: 0.25s ease;
  border-radius: 20px;
}
.toggle-slider:before {
  position: absolute;
  content: "";
  height: 14px;
  width: 14px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: 0.25s ease;
  border-radius: 50%;
}
.toggle-switch input:checked + .toggle-slider {
  background-color: var(--accent);
}
.toggle-switch input:checked + .toggle-slider:before {
  transform: translateX(18px);
  background-color: #0f172a;
}
.toggle-label {
  font-size: 0.83rem;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}

/* Tactics badge */
.tactics-badge {
  display: inline-block;
  background: #713f12;
  color: #fef08a;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 4px;
  margin-top: 8px;
}

/* Candidates */
.candidates-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 220px;
  overflow-y: auto;
}
.candidate-card {
  background: var(--surface2);
  padding: 8px 12px;
  border-radius: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-left: 3px solid var(--accent);
  font-size: 0.85rem;
}
.candidate-card .coord { font-weight: 700; color: var(--accent); }
.candidate-card .visits { font-size: 0.75rem; color: var(--text-muted); }
.candidate-card .winpct { font-weight: 700; color: var(--accent); }

/* Loading spinner overlay */
#loadingOverlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 200;
  gap: 16px;
  display: none;
}
#loadingOverlay.visible { display: flex; }
.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Footer */
footer {
  width: 100%;
  max-width: 1080px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: auto;
}
footer a { color: var(--text-dim); text-decoration: none; }
footer a:hover { color: var(--accent); }
