:root {
  --bg: #fafafa;
  --surface: #fff;
  --border: #e0e0e0;
  --ink: #1a1a1a;
  --muted: #777;
  --accent: #1a6b5a;
  --accent-light: #e8f5f1;
  --danger: #c0392b;
  --token-x: #c0392b;
  --token-o: #2c6fbb;
  --board-bg: #f5f3ef;
  --board-selected: rgba(26, 107, 90, 0.22);
  --board-recent: rgba(200, 140, 30, 0.25);
  --board-winning: rgba(46, 160, 80, 0.28);
  --board-x-fill: rgba(192, 57, 43, 0.10);
  --board-x-stroke: rgba(192, 57, 43, 0.25);
  --board-o-fill: rgba(44, 111, 187, 0.10);
  --board-o-stroke: rgba(44, 111, 187, 0.25);
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.5;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ── Top bar ── */

.topbar {
  display: flex;
  align-items: baseline;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.topbar h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.game-info {
  font-size: 0.85rem;
  color: var(--muted);
}

/* ── Mode nav ── */

.modes {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}

.mode-group {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 0;
}

.mode-group + .mode-group {
  padding-left: 6px;
  border-left: 1px solid var(--border);
}

.mode-group.is-active {
  background: var(--accent-light);
  border-radius: 6px;
  padding: 2px 6px;
}

.mode-name {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.code-input {
  width: 70px;
  padding: 4px 8px;
  font: inherit;
  font-size: 0.82rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  text-transform: uppercase;
}

.code-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.strength-select {
  padding: 4px 8px;
  font: inherit;
  font-size: 0.82rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  cursor: pointer;
}

.strength-select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.invite-status {
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: 500;
}

/* ── Buttons ── */

.btn {
  padding: 5px 12px;
  font: inherit;
  font-size: 0.82rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover {
  background: #f0f0f0;
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.btn.primary:hover {
  background: #15594b;
}

.btn.primary:disabled {
  background: var(--accent);
}

.btn.sm {
  padding: 3px 8px;
  font-size: 0.78rem;
}

/* ── Game layout ── */

.game {
  display: grid;
  grid-template-columns: 1fr 260px;
  gap: 0;
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
}

/* ── Board ── */

.board-area {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.board-header {
  margin-bottom: 8px;
}

.board-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.subtitle {
  font-size: 0.82rem;
  color: var(--muted);
}

#board {
  width: 100%;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--board-bg);
  touch-action: none;
  cursor: grab;
}

#board:active {
  cursor: grabbing;
}

.board-wrap {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  display: flex;
}

.board-wrap #board {
  flex: 1 1 0;
}

.minimap {
  width: 100%;
  height: 140px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  cursor: pointer;
}

/* ── Side panel ── */

.panel {
  padding: 12px;
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
}

.status-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat .label, .stat-row .label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.stat strong, .stat-row strong {
  font-size: 0.88rem;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.stat-row strong {
  text-align: right;
  font-size: 0.82rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.divider {
  height: 1px;
  background: var(--border);
}

/* ── Selection pills ── */

.pill-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 30px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 0.8rem;
  background: var(--accent-light);
  color: var(--accent);
}

.pill.subtle {
  background: #f0f0f0;
  color: var(--muted);
}

.pill button {
  width: 18px;
  height: 18px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.1);
  cursor: pointer;
  font-size: 0.7rem;
  line-height: 1;
}

/* ── Actions & zoom ── */

.actions {
  display: flex;
  gap: 6px;
}

.zoom-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.zoom-row strong {
  min-width: 40px;
  text-align: center;
  font-size: 0.82rem;
}

.helper {
  font-size: 0.78rem;
  color: var(--muted);
}

/* ── Flash message ── */

.flash {
  position: fixed;
  right: 16px;
  bottom: 16px;
  max-width: 380px;
  padding: 10px 14px;
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
  font-size: 0.85rem;
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 150ms, transform 150ms;
}

.flash.visible {
  opacity: 1;
  transform: translateY(0);
}

/* ── Hex cells ── */

.hex {
  cursor: pointer;
}

.hex.empty polygon {
  fill: rgba(255, 255, 255, 0.02);
  stroke: rgba(0, 0, 0, 0.10);
  stroke-width: 1.4;
}

.hex.x polygon {
  fill: var(--board-x-fill);
  stroke: var(--board-x-stroke);
  stroke-width: 1.6;
}

.hex.o polygon {
  fill: var(--board-o-fill);
  stroke: var(--board-o-stroke);
  stroke-width: 1.6;
}

.hex.selected polygon {
  fill: var(--board-selected);
}

.hex.recent polygon {
  fill: var(--board-recent);
}

.hex.winning polygon {
  fill: rgba(46, 160, 80, 0.45);
  stroke: #2ea050;
  stroke-width: 3;
  animation: winPulse 1.2s ease-in-out infinite;
}

@keyframes winPulse {
  0%, 100% { fill: rgba(46, 160, 80, 0.45); }
  50% { fill: rgba(46, 160, 80, 0.7); }
}

.token-x {
  stroke: var(--token-x);
  stroke-width: 4.75;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
}

.token-o {
  fill: none;
  stroke: var(--token-o);
  stroke-width: 4.5;
}

/* ── Topbar right ── */

.topbar-right {
  margin-left: auto;
}

/* ── Status bar ── */

.status-bar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.turn-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 28px;
  padding: 0 8px;
  border-radius: 4px;
  font-size: 0.82rem;
  font-weight: 700;
  background: var(--accent-light);
  color: var(--accent);
}

.status-text {
  font-size: 0.82rem;
  color: var(--muted);
}

/* ── Move history ── */

.move-history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.history-nav {
  display: flex;
  gap: 2px;
}

.history-nav .btn {
  min-width: 28px;
  text-align: center;
  padding: 2px 6px;
}

.move-history {
  max-height: 180px;
  overflow-y: auto;
  font-size: 0.78rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
}

.move-history table {
  width: 100%;
  border-collapse: collapse;
}

.move-history th {
  position: sticky;
  top: 0;
  background: var(--surface);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding: 4px 6px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.move-history td {
  padding: 3px 6px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.04);
}

.move-history tr.is-current {
  background: var(--accent-light);
}

.move-history tr:hover {
  background: rgba(0, 0, 0, 0.03);
}

.move-history .stone-x {
  color: var(--token-x);
  font-weight: 600;
}

.move-history .stone-o {
  color: var(--token-o);
  font-weight: 600;
}

/* ── Rules modal ── */

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 150ms ease;
}

.modal {
  background: var(--surface);
  border-radius: 10px;
  width: 90%;
  max-width: 440px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.2);
  animation: slideUp 200ms ease;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  font-size: 1.1rem;
  font-weight: 700;
}

.modal-body {
  padding: 16px 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.rule-step {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.rule-step strong {
  font-size: 0.88rem;
  color: var(--ink);
}

.rule-step p {
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.5;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Eval bar ── */

.eval-bar {
  position: relative;
  width: 24px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.eval-bar-x {
  background: var(--token-x);
  transition: flex 0.6s ease;
}

.eval-bar-o {
  background: var(--token-o);
  transition: flex 0.6s ease;
}

.eval-label {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-90deg);
  font-size: 0.62rem;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}

/* ── Thinking indicator ── */

#board.thinking {
  border-color: var(--accent);
  animation: thinkPulse 1.4s ease-in-out infinite;
}

@keyframes thinkPulse {
  0%, 100% { border-color: var(--accent); box-shadow: 0 0 0 0 rgba(26, 107, 90, 0); }
  50% { border-color: #2ea050; box-shadow: 0 0 12px 2px rgba(26, 107, 90, 0.25); }
}

.thinking-text {
  animation: ellipsis 1.5s steps(4, end) infinite;
}

/* ── Win banner ── */

.win-banner {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 56px;
  font-weight: 900;
  text-anchor: middle;
  dominant-baseline: central;
  paint-order: stroke fill;
  stroke-width: 6;
  stroke-linejoin: round;
}

.win-banner-bg {
  fill: rgba(0, 0, 0, 0.35);
  rx: 16;
}

/* ── Rule demo cells (kept for rule_demos.js compatibility) ── */

.demo-cell polygon { fill: rgba(255,255,255,0.05); stroke: rgba(0,0,0,0.1); stroke-width: 1.3; }
.demo-cell.is-focus polygon { fill: rgba(26,107,90,0.12); stroke: rgba(26,107,90,0.3); }
.demo-cell.is-recent polygon { fill: rgba(200,140,30,0.14); }
.demo-cell.is-winning polygon { fill: rgba(46,160,80,0.18); }
.demo-cell.is-ghost polygon { fill: rgba(0,0,0,0.04); stroke-dasharray: 3 4; }
.demo-cell.is-muted polygon { opacity: 0.45; }
.demo-token-x { stroke: var(--token-x); stroke-width: 3.2; stroke-linecap: round; }
.demo-token-o { fill: none; stroke: var(--token-o); stroke-width: 3.2; }
.demo-overlay { font-family: inherit; font-size: 0.76rem; fill: rgba(0,0,0,0.6); }

/* ── Responsive ── */

@media (max-width: 800px) {
  .game {
    grid-template-columns: 1fr;
  }

  .panel {
    border-left: none;
    border-top: 1px solid var(--border);
  }

  #board {
    min-height: 400px;
  }

  .topbar {
    flex-direction: column;
    gap: 4px;
  }
}
