:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #14181f;
  --muted: #5b6473;
  --border: #d8dde6;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --danger: #d23a3a;
  --success: #2f9e6a;
  --warn: #d18a1a;
  --shadow: 0 6px 18px rgba(20, 24, 31, 0.08);
  --b: #2f6df4;
  --i: #d23a8c;
  --n: #2f9e6a;
  --g: #d18a1a;
  --o: #8a3ad2;
}
[data-theme="dark"] {
  --bg: #10131a;
  --surface: #181d27;
  --surface-2: #222836;
  --text: #e6eaf2;
  --muted: #9aa3b3;
  --border: #2a3142;
  --accent: #7c74ff;
  --accent-text: #0c0f17;
  --danger: #ff6470;
  --success: #4ed39a;
  --warn: #f0b34a;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  --b: #5b8dff;
  --i: #ff6cb3;
  --n: #5fd9a3;
  --g: #f3b35a;
  --o: #b481ff;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { margin: 0 0 0.4em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.15rem; }
p { margin: 0 0 0.6em; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.hidden { display: none !important; }
.center { text-align: center; }

:root { --topbar-h: 56px; --gamebar-h: 116px; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1rem;
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  height: var(--topbar-h);
  box-sizing: border-box;
}
.brand { font-weight: 700; font-size: 1.15rem; }
.brand span { font-weight: 600; }
.topbar-actions { display: flex; align-items: center; gap: 0.5rem; }

.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.35rem 0.55rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
}
.icon-btn:hover { background: var(--surface-2); }

.pill {
  background: var(--surface-2);
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  font-weight: 600;
  letter-spacing: 0.08em;
  border: 1px solid var(--border);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  cursor: pointer;
}

main { max-width: 1100px; margin: 0 auto; padding: 1rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}
.stack > * + * { margin-top: 0.7rem; }

.row { display: flex; align-items: center; }
.row.gap { gap: 0.6rem; }
.row.spread { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.row.center { justify-content: center; }

.field { display: flex; flex-direction: column; gap: 0.35rem; }
.field > span { font-weight: 600; font-size: 0.9rem; }
.field input[type="text"],
.field input[type="number"],
.field select {
  width: 100%;
  padding: 0.6rem 0.7rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1rem;
  outline: none;
}
.field input:focus, .field select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent);
}
fieldset {
  border: none;
  padding: 0;
  margin: 0;
}
.field.row { flex-direction: row; align-items: center; }
.field.row > input[type="checkbox"] { width: 1.1rem; height: 1.1rem; accent-color: var(--accent); }

.btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 0.6rem 1rem;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.05s ease, background 0.15s ease, border-color 0.15s ease;
}
.btn:hover { background: var(--bg); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.05); background: var(--accent); }
.btn.ghost { background: transparent; }
.btn.small { padding: 0.4rem 0.7rem; font-size: 0.9rem; }
.btn[disabled] { opacity: 0.55; cursor: not-allowed; }
.bingo-btn.locked {
  background: var(--surface-2);
  color: var(--danger);
  border-color: var(--danger);
  font-weight: 700;
}

.error { color: var(--danger); font-weight: 600; }

#missed-bingos-section {
  margin-bottom: 1rem;
}

.shutdown-warning {
  background: color-mix(in srgb, var(--warn) 20%, var(--surface));
  border: 1px solid var(--warn);
  color: var(--warn);
  border-radius: 0.5rem;
  padding: 0.6rem 0.9rem;
  margin: 0.4rem 0;
  font-weight: 600;
  text-align: center;
}

.divider {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  color: var(--muted);
  margin: 0.4rem 0;
}
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.code-input {
  text-transform: uppercase;
  letter-spacing: 0.18em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: center;
  font-weight: 700;
}

.icon-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
  gap: 0.4rem;
}
.icon-cell {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 1.5rem;
  width: 100%;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
}
.icon-cell.selected {
  background: color-mix(in srgb, var(--accent) 25%, transparent);
  border-color: var(--accent);
}

.bubble-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.bubble {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  color: var(--text);
  font: inherit;
  cursor: pointer;
  user-select: none;
  transition: background 80ms, border-color 80ms;
}
.bubble:hover:not([disabled]) { border-color: var(--accent); }
.bubble.selected {
  background: color-mix(in srgb, var(--accent) 25%, transparent);
  border-color: var(--accent);
  font-weight: 600;
}
.bubble[disabled] { opacity: 0.55; cursor: not-allowed; }

/* Lobby */
.lobby-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}
@media (min-width: 820px) {
  .lobby-grid { grid-template-columns: 1.2fr 1fr; }
  .lobby-grid > .card { margin-bottom: 0; }
}

.player-list {
  list-style: none;
  padding: 0;
  margin: 0 0 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.player-list.compact { gap: 0.25rem; }
.player {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.7rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.player .icon { font-size: 1.4rem; }
.player .name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player .badge {
  font-size: 0.75rem;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  font-weight: 700;
}
.badge.host { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent); }
.badge.ready { background: color-mix(in srgb, var(--success) 20%, transparent); color: var(--success); }
.badge.bingo { background: color-mix(in srgb, var(--warn) 20%, transparent); color: var(--warn); }
.badge.you { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent); }

/* Game */
.last-drawn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  font-size: 2.2rem;
  font-weight: 800;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 0.04em;
  min-width: 7ch;
}
.draw-meta { text-align: right; }
.drawn-history {
  margin-top: 0.6rem;
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
  max-height: 14rem;
  overflow-y: auto;
}
.tab-bar {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 2px;
  gap: 2px;
}
.tab-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0.35rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  background: var(--accent);
  color: var(--accent-text);
}
.history-pane { margin-top: 0.6rem; }
.drawn-chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.2rem 0.45rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 700;
  font-size: 0.85rem;
}

.cards-area {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fill, minmax(min(280px, 100%), 1fr));
  margin-top: 0.6rem;
}

.bingo-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.bingo-card.hasWin { box-shadow: 0 0 0 3px color-mix(in srgb, var(--success) 70%, transparent); }
.bingo-grid-wrap {
  position: relative;
}
.bingo-strikethrough {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.bingo-card .card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.card-owner {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 1rem;
}
.card-owner .icon { font-size: 1.3rem; }
.card-label { white-space: nowrap; }
.bingo-cell.read-only { cursor: default; }
.bingo-cell.read-only:active { transform: none; }

.bingo-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
}
.bingo-cell {
  touch-action: manipulation;
}
.bingo-header, .bingo-cell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-weight: 800;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: clamp(0.95rem, 3.5vw, 1.35rem);
  user-select: none;
}
.bingo-header { color: var(--accent-text); }
.bingo-header.col-0 { background: var(--b); }
.bingo-header.col-1 { background: var(--i); }
.bingo-header.col-2 { background: var(--n); }
.bingo-header.col-3 { background: var(--g); }
.bingo-header.col-4 { background: var(--o); }

.bingo-cell {
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
  position: relative;
  transition: transform 0.04s ease;
}
.bingo-cell:active { transform: scale(0.96); }
.bingo-cell.marked {
  background: radial-gradient(
    circle at center,
    color-mix(in srgb, var(--dabber, var(--accent)) 75%, transparent) 0%,
    color-mix(in srgb, var(--dabber, var(--accent)) 75%, transparent) 42%,
    var(--surface) 44%
  );
  border-color: var(--dabber, var(--accent));
  color: var(--text);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}
.bingo-cell.free {
  background: color-mix(in srgb, var(--warn) 30%, var(--surface));
  border-color: var(--warn);
  font-size: 0.85rem;
  cursor: default;
}
.bingo-cell.win {
  background: color-mix(in srgb, var(--success) 50%, var(--surface));
  border-color: var(--success);
}
.bingo-cell.drawn-now {
  outline: 3px solid var(--warn);
  outline-offset: -3px;
}

.toast {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.7rem 1rem;
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 50;
  max-width: calc(100vw - 2rem);
}
.toast.error { border-color: var(--danger); color: var(--danger); }
.toast.success { border-color: var(--success); color: var(--success); }

.kick-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 0.15rem 0.4rem;
  font-size: 0.75rem;
  border-radius: 6px;
  cursor: pointer;
}
.kick-btn:hover { color: var(--danger); border-color: var(--danger); }

.spectators-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.4rem;
}
.spectator-chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.2rem 0.5rem;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  cursor: pointer;
  user-select: none;
  font-size: 1rem;
  transition: background 0.15s ease;
}
.spectator-chip:hover, .spectator-chip.revealed {
  background: color-mix(in srgb, var(--accent) 20%, var(--surface-2));
  border-color: var(--accent);
}
.spectator-chip .name-label {
  display: none;
  font-size: 0.85rem;
  font-weight: 600;
  max-width: 14ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
[data-theme="dark"] .spectator-chip .name-label { color: #ffffff; }

/* Game bar (sticky last-drawn + mini chart) */
.game-bar {
  position: sticky;
  top: var(--topbar-h);
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.6rem 1rem;
  margin: -1rem -1rem 1rem -1rem;
  flex-wrap: wrap;
}

#history-card { position: relative; }
#history-card > .row.spread { padding-right: 3rem; }
.pin-btn {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  font-size: 1.05rem;
  z-index: 2;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.pin-btn.active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

/* History card sticky mode — sits below the game-bar */
#history-card.sticky {
  position: sticky;
  top: calc(var(--topbar-h) + var(--gamebar-h));
  z-index: 4;
  max-height: 60vh;
  overflow-y: auto;
}
#history-card.sticky .drawn-history {
  max-height: none;
}
.last-drawn-big {
  font-size: clamp(2rem, 7vw, 3rem);
  font-weight: 900;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 0.02em;
  min-width: 4ch;
  text-align: center;
}
.game-bar-meta {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  flex: 1;
  min-width: 0;
}
.mini-chart-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
  cursor: pointer;
  display: flex;
}
.mini-chart-btn:hover { border-color: var(--accent); }
.mini-chart {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 1px;
  width: 70px;
  height: 84px;
}
.mini-chart-col {
  display: grid;
  grid-template-rows: repeat(15, 1fr);
  gap: 1px;
}
.mini-chart-cell {
  background: var(--border);
  border-radius: 1px;
}
.mini-chart-cell.drawn-0 { background: var(--b); }
.mini-chart-cell.drawn-1 { background: var(--i); }
.mini-chart-cell.drawn-2 { background: var(--n); }
.mini-chart-cell.drawn-3 { background: var(--g); }
.mini-chart-cell.drawn-4 { background: var(--o); }

/* Full-screen chart overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.overlay-inner {
  background: var(--surface);
  border-radius: 14px;
  border: 1px solid var(--border);
  padding: 1rem;
  width: 100%;
  max-width: 720px;
  max-height: 100%;
  overflow: auto;
}
.overlay-header { margin-bottom: 0.6rem; }
.full-chart {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
.full-chart-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.full-chart-numbers {
  display: grid;
  grid-template-rows: repeat(5, 1fr);
  grid-auto-flow: column;
  gap: 3px;
}
.full-chart-header {
  text-align: center;
  font-weight: 800;
  font-size: 1.4rem;
  color: var(--accent-text);
  border-radius: 8px;
  padding: 0.4rem 0;
}
.full-chart-header.col-0 { background: var(--b); }
.full-chart-header.col-1 { background: var(--i); }
.full-chart-header.col-2 { background: var(--n); }
.full-chart-header.col-3 { background: var(--g); }
.full-chart-header.col-4 { background: var(--o); }
.full-chart-cell {
  text-align: center;
  padding: 0.35rem 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 700;
  font-size: 0.85rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
}
.full-chart-cell.drawn {
  color: var(--text);
  background: color-mix(in srgb, var(--accent) 25%, var(--surface-2));
  border-color: var(--accent);
}
.full-chart-cell.last {
  outline: 3px solid var(--warn);
  outline-offset: -3px;
}

/* Alt grid: 5 horizontal rows, letters down the left */
.alt-chart {
  display: grid;
  grid-template-rows: repeat(5, 1fr);
  gap: 4px;
}
.alt-row {
  display: grid;
  grid-template-columns: 1.5rem 1fr;
  gap: 4px;
  align-items: stretch;
}
.alt-letter {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 0.95rem;
  border-radius: 5px;
  color: var(--accent-text);
}
.alt-letter.col-0 { background: var(--b); }
.alt-letter.col-1 { background: var(--i); }
.alt-letter.col-2 { background: var(--n); }
.alt-letter.col-3 { background: var(--g); }
.alt-letter.col-4 { background: var(--o); }
.alt-cells {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  gap: 1px;
}
.alt-cell {
  text-align: center;
  padding: 0.25rem 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 700;
  font-size: 0.7rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--muted);
  min-width: 0;
}
.alt-cell.drawn {
  color: var(--text);
  background: color-mix(in srgb, var(--accent) 25%, var(--surface-2));
  border-color: var(--accent);
}
.alt-cell.last {
  outline: 2px solid var(--warn);
  outline-offset: -2px;
}

/* Dabber picker */
.dabber-picker {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.dabber-swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid var(--border);
  cursor: pointer;
  padding: 0;
}
.dabber-swatch.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent);
}

/* Gear menu */
.gear-wrapper { position: relative; }
.gear-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.7rem;
  min-width: 240px;
  box-shadow: var(--shadow);
  z-index: 20;
}
.gear-item { padding: 0.3rem 0; }
.gear-menu .btn { width: 100%; margin: 0.4rem 0; }
.gear-menu code {
  background: var(--surface-2);
  padding: 0.05rem 0.25rem;
  border-radius: 4px;
  font-size: 0.78rem;
}

/* Player dabber pip in lists */
.dabber-pip {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1px solid var(--border);
  display: inline-block;
}
.spectator-chip:hover .name-label,
.spectator-chip.revealed .name-label { display: inline; }

.pill.locked {
  border-color: var(--warn);
  color: var(--warn);
}

.leaderboard-medal {
  font-size: 1.2rem;
  margin-right: 0.3rem;
}

#end-host-hint { margin-top: 0.8rem; margin-bottom: -0.4rem; }
.leaderboard-wins {
  font-weight: 700;
  color: var(--warn);
}
