/* ── Variables (same system as connect4/sushi) ─────────────────────────────── */
:root {
  --bg:        #0f1117;
  --surface:   #1a1d2e;
  --surface2:  #232640;
  --border:    #2e3255;
  --text:      #e8eaf6;
  --muted:     #7b80a8;
  --purple:    #8b5cf6;
  --purple2:   #a78bfa;
  --red:       #f87171;
  --cell-size: 48px;
  --cell-gap:  4px;
}
body.light {
  --bg:       #eef0f7;
  --surface:  #ffffff;
  --surface2: #f0f2fb;
  --border:   #dde0ef;
  --text:     #1a1b2e;
  --muted:    #6b6d8e;
  --purple:   #6d28d9;
  --purple2:  #7c3aed;
}

/* ── Reset / base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  background-image:
    radial-gradient(circle at 20% 20%, rgba(139,92,246,0.06) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(167,139,250,0.04) 0%, transparent 50%);
  transition: background 0.3s, color 0.3s;
}
body.light {
  background-image:
    radial-gradient(circle at 20% 20%, rgba(109,40,217,0.06) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(124,58,237,0.04) 0%, transparent 50%);
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  outline: none;
}
input {
  font-family: inherit;
  outline: none;
}

/* ── Screens ─────────────────────────────────────────────────────────────── */
.screen { display: none; }
.screen.active { display: flex; flex-direction: column; align-items: center; }

/* ── Top bar (settings / auth) ───────────────────────────────────────────── */
#top-bar {
  position: fixed;
  top: 12px; right: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 100;
}
#theme-toggle {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 16px;
  color: var(--text);
  cursor: pointer;
  transition: background 0.2s;
}
#theme-toggle:hover { background: var(--border); }

/* ── Lobby screen ─────────────────────────────────────────────────────────── */
#screen-lobby {
  min-height: 100vh;
  justify-content: center;
  padding: 60px 16px 40px;
}
.lobby-inner {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.lobby-title {
  text-align: center;
  margin-bottom: 8px;
}
.lobby-title h1 {
  font-size: 2rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--purple2), #c084fc);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.lobby-title p {
  color: var(--muted);
  margin-top: 4px;
  font-size: 0.9rem;
}

.lobby-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.lobby-card h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.name-input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 1rem;
  color: var(--text);
  width: 100%;
  transition: border-color 0.2s;
}
.name-input:focus { border-color: var(--purple); }

.time-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.time-btn {
  flex: 1;
  min-width: 60px;
  padding: 8px 6px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.85rem;
  transition: all 0.15s;
}
.time-btn.active, .time-btn:hover {
  background: var(--purple);
  border-color: var(--purple);
  color: #fff;
}

.btn-primary {
  background: linear-gradient(135deg, var(--purple), var(--purple2));
  color: #fff;
  padding: 12px 20px;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  width: 100%;
  transition: opacity 0.15s, transform 0.1s;
}
.btn-primary:hover { opacity: 0.9; transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 11px 20px;
  border-radius: 12px;
  font-size: 0.95rem;
  font-weight: 500;
  width: 100%;
  transition: background 0.15s;
}
.btn-secondary:hover { background: var(--border); }

.room-code-row {
  display: flex;
  gap: 8px;
}
.room-code-input {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 1rem;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
.room-code-input:focus { border-color: var(--purple); }
.btn-join {
  background: var(--purple);
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  transition: opacity 0.15s;
  white-space: nowrap;
}
.btn-join:hover { opacity: 0.85; }

.diff-row {
  display: flex;
  gap: 8px;
}
.diff-btn {
  flex: 1;
  padding: 8px 4px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.85rem;
  transition: all 0.15s;
}
.diff-btn.active, .diff-btn:hover {
  background: var(--purple);
  border-color: var(--purple);
  color: #fff;
}

#lobby-error {
  color: var(--red);
  font-size: 0.85rem;
  text-align: center;
  min-height: 20px;
}

/* ── Waiting screen ───────────────────────────────────────────────────────── */
#screen-waiting {
  min-height: 100vh;
  justify-content: center;
  gap: 24px;
  padding: 40px 16px;
}
.waiting-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 40px 32px;
  text-align: center;
  max-width: 360px;
  width: 100%;
}
.waiting-box h2 { font-size: 1.3rem; margin-bottom: 8px; }
.waiting-box p  { color: var(--muted); font-size: 0.9rem; margin-bottom: 20px; }

.room-code-display {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 24px;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  color: var(--purple2);
  margin-bottom: 12px;
  display: inline-block;
}
.room-share-hint { color: var(--muted); font-size: 0.8rem; margin-bottom: 20px; }

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

/* ── Game screen ─────────────────────────────────────────────────────────── */
#screen-game {
  flex-direction: column;
  align-items: center;
  padding: 8px 12px 24px;
  min-height: 100vh;
}

/* ── HUD ──────────────────────────────────────────────────────────────────── */
.ab-hud {
  width: 100%;
  max-width: 560px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 6px 0 10px;
}
.ab-hud-player {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}
.ab-hud-player.right { align-items: flex-end; }
.ab-hud-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  max-width: 130px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ab-hud-cap {
  display: flex;
  gap: 4px;
  align-items: center;
}
.ab-hud-marble {
  width: 12px; height: 12px;
  border-radius: 50%;
  display: inline-block;
}
.ab-hud-marble.black { background: #1a1a2e; border: 1.5px solid #555; }
.ab-hud-marble.white { background: #e0e0e0; border: 1.5px solid #aaa; }
.ab-hud-cap-count { font-size: 0.8rem; color: var(--muted); }
.ab-hud-score { font-size: 1rem; font-weight: 700; color: var(--purple2); }

.ab-hud-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.ab-turn-indicator {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.ab-timer-pair {
  display: flex;
  gap: 6px;
}
.ab-timer {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 0.85rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  min-width: 52px;
  text-align: center;
  transition: border-color 0.3s, color 0.3s;
}
.ab-timer.active { border-color: var(--purple); color: var(--purple2); }
.ab-timer.low    { border-color: var(--red); color: var(--red); }
.ab-timer.hidden { display: none; }

#ab-resign-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  font-size: 0.75rem;
  padding: 4px 10px;
  cursor: pointer;
  transition: all 0.15s;
}
#ab-resign-btn:hover { border-color: var(--red); color: var(--red); }

/* ── Board ─────────────────────────────────────────────────────────────────── */
.ab-board-wrap {
  position: relative;
  margin: 4px 0 12px;
  display: flex;
  justify-content: center;
}
#ab-board {
  position: relative;
  touch-action: none;  /* let JS handle all touch events; prevents scroll/zoom on the board */
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.3s ease;
}
/* When player is White (playerNum 2), flip the board 180° so their marbles are at the bottom */
#ab-board.flipped {
  transform: rotate(180deg);
}
/* Marble cells are symmetric circles — no need to counter-rotate them */
.ab-cell {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface2);
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.1s, border-color 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ab-cell:hover:not(.no-action) {
  border-color: var(--purple);
  transform: scale(1.06);
}

/* Marble styles */
.ab-cell.black {
  background: radial-gradient(circle at 35% 35%, #444, #111);
  border-color: #333;
  box-shadow: inset 0 -3px 6px rgba(255,255,255,0.08), 0 2px 6px rgba(0,0,0,0.5);
}
.ab-cell.white {
  background: radial-gradient(circle at 35% 35%, #f5f5f5, #c0c0c0);
  border-color: #aaa;
  box-shadow: inset 0 -3px 6px rgba(0,0,0,0.12), 0 2px 6px rgba(0,0,0,0.2);
}
body.light .ab-cell {
  background: var(--surface);
  border-color: var(--border);
}
body.light .ab-cell.black {
  background: radial-gradient(circle at 35% 35%, #444, #111);
  border-color: #333;
}
body.light .ab-cell.white {
  background: radial-gradient(circle at 35% 35%, #fafafa, #c8c8c8);
  border-color: #aaa;
}

/* Selection and highlights */
.ab-cell.selected {
  outline: 3px solid var(--purple2);
  outline-offset: 1px;
  transform: scale(1.1);
  z-index: 2;
}
.ab-cell.valid-target {
  outline: 2px dashed var(--purple);
  outline-offset: 2px;
  opacity: 0.85;
  z-index: 1;
}
.ab-cell.valid-target:hover {
  opacity: 1;
  transform: scale(1.06);
}
.ab-cell.last-moved {
  outline: 2px solid rgba(245,197,24,0.7);
  outline-offset: 1px;
}

/* ── Result overlay ────────────────────────────────────────────────────────── */
#ab-result {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  align-items: center;
  justify-content: center;
  z-index: 200;
}
#ab-result.visible { display: flex; }
.ab-result-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 40px;
  text-align: center;
  max-width: 340px;
  width: 90%;
  animation: pop-in 0.25s ease;
}
@keyframes pop-in {
  from { transform: scale(0.85); opacity: 0; }
  to   { transform: scale(1);    opacity: 1; }
}
.ab-result-title {
  font-size: 1.6rem;
  font-weight: 700;
  margin-bottom: 6px;
}
.ab-result-sub {
  color: var(--muted);
  margin-bottom: 24px;
  font-size: 0.9rem;
}
.ab-result-btns {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ── Chat ─────────────────────────────────────────────────────────────────── */
.ab-chat {
  width: 100%;
  max-width: 560px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  margin-top: 4px;
}
.ab-chat-messages {
  height: 80px;
  overflow-y: auto;
  padding: 8px 12px;
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.ab-chat-msg { color: var(--text); }
.ab-chat-msg span.sender { color: var(--purple2); font-weight: 600; }
.ab-chat-row {
  display: flex;
  border-top: 1px solid var(--border);
}
.ab-chat-input {
  flex: 1;
  background: transparent;
  border: none;
  padding: 8px 12px;
  font-size: 0.85rem;
  color: var(--text);
}
.ab-chat-send {
  background: none;
  border: none;
  border-left: 1px solid var(--border);
  padding: 6px 12px;
  color: var(--purple2);
  font-size: 0.8rem;
  cursor: pointer;
}
.ab-chat-send:hover { background: var(--surface2); }

/* ── History button (back to lobby) ────────────────────────────────────────── */
.ab-lobby-link {
  font-size: 0.8rem;
  color: var(--muted);
  text-decoration: none;
  margin-top: 8px;
  transition: color 0.15s;
}
.ab-lobby-link:hover { color: var(--purple2); }

/* ── Responsive ─────────────────────────────────────────────────────────────── */
@media (max-width: 520px) {
  :root {
    --cell-size: 36px;
    --cell-gap: 3px;
  }
  .ab-hud-name { max-width: 90px; font-size: 0.78rem; }
  .ab-result-box { padding: 24px 20px; }
}
@media (max-width: 360px) {
  :root { --cell-size: 30px; --cell-gap: 2px; }
}
