:root {
  --buzz-red: #E8003D;
  --buzz-blue: #1565c0;
  --buzz-orange: #FF7200;
  --buzz-yellow: #FFD500;
  --buzz-green: #2e7d32;
  --bg: #1a1a1a;
  --surface: #2a2a2a;
  --text: #f0f0f0;
  --text-muted: #888;
  --radius: 12px;

  /* Host-page palette */
  --host-bg: #0D0D1A;
  --host-card: #1A1A35;
  --host-surface: #22224A;

  /* Player strip colours */
  --p1-colour: #FFD500;
  --p2-colour: #3B9EFF;
  --p3-colour: #4CAF50;
  --p4-colour: #C850F8;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ── Corner setup button ────────────────────────────────────────────────────── */

.corner-btn {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 50;
  padding: 7px 12px;
  border-radius: 20px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  text-decoration: none;
  letter-spacing: 0.04em;
  transition: color 0.15s, background 0.15s;
}

.corner-btn:hover { color: var(--text); background: #383838; }

/* ── Layout helpers ─────────────────────────────────────────────────────────── */

.screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px 16px;
  gap: 16px;
}

.hidden { display: none !important; }

/* ── Join screen ────────────────────────────────────────────────────────────── */

.join-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px 24px;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.join-card h1 {
  font-size: 2rem;
  font-weight: 900;
  letter-spacing: -1px;
  color: var(--buzz-red);
  text-align: center;
}

.join-card label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  display: block;
  margin-bottom: 6px;
}

.join-card input {
  width: 100%;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 2px solid #444;
  background: var(--bg);
  color: var(--text);
  font-size: 1.1rem;
  outline: none;
  transition: border-color 0.2s;
}

.join-card input:focus {
  border-color: var(--buzz-red);
}

.join-card input.error {
  border-color: var(--buzz-red);
  animation: shake 0.3s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

.btn-join {
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: var(--radius);
  background: var(--buzz-red);
  color: #fff;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}

.btn-join:active { transform: scale(0.97); opacity: 0.85; }
.btn-join:disabled { opacity: 0.4; cursor: not-allowed; }

.status-msg {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  min-height: 1.2em;
}

.status-msg.error { color: #ff6b6b; }
.status-msg.ok { color: #6bff8e; }

/* ── Controller screen ──────────────────────────────────────────────────────── */

#controller-screen {
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 20px 16px 32px;
  user-select: none;
  -webkit-user-select: none;
}

.player-info {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
}

.player-info strong {
  color: var(--text);
}

/* BUZZ button */
.buzz-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.btn-buzz {
  width: min(70vw, 260px);
  height: min(70vw, 260px);
  border-radius: 50%;
  border: none;
  background: var(--buzz-red);
  color: #fff;
  font-size: clamp(1.6rem, 8vw, 2.4rem);
  font-weight: 900;
  letter-spacing: 0.05em;
  cursor: pointer;
  box-shadow: 0 8px 32px rgba(227, 30, 36, 0.45), 0 0 0 8px rgba(227, 30, 36, 0.15);
  touch-action: none;
  transition: transform 0.08s, box-shadow 0.08s;
  -webkit-tap-highlight-color: transparent;
}

.btn-buzz:active,
.btn-buzz.pressed {
  transform: scale(0.93);
  box-shadow: 0 2px 12px rgba(227, 30, 36, 0.35), 0 0 0 4px rgba(227, 30, 36, 0.1);
}

/* Answer buttons grid */
.answer-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  width: 100%;
  max-width: 360px;
}

.btn-answer {
  height: clamp(70px, 18vw, 100px);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: clamp(0.9rem, 4vw, 1.1rem);
  font-weight: 700;
  cursor: pointer;
  touch-action: none;
  transition: transform 0.08s, opacity 0.08s;
  -webkit-tap-highlight-color: transparent;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.btn-answer:active,
.btn-answer.pressed {
  transform: scale(0.93);
  opacity: 0.8;
}

.btn-answer[data-button="blue"]   { background: var(--buzz-blue); }
.btn-answer[data-button="orange"] { background: var(--buzz-orange); }
.btn-answer[data-button="green"]  { background: var(--buzz-green); }
.btn-answer[data-button="yellow"] { background: var(--buzz-yellow); color: #111; }

/* ── Dev mode panel ─────────────────────────────────────────────────────────── */

#controller-screen.dev-mode {
  height: auto;
  min-height: 100dvh;
  overflow-y: auto;
  justify-content: flex-start;
  gap: 16px;
  padding-bottom: 24px;
}

#controller-screen.dev-mode .buzz-wrap { flex: none; }

#controller-screen.dev-mode .btn-buzz {
  width: min(52vw, 200px);
  height: min(52vw, 200px);
}

#dev-panel {
  width: 100%;
  max-width: 360px;
  padding: 14px;
  background: #111;
  border: 1px solid #333;
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.dev-header {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: #666;
  text-align: center;
}

.dev-slot-row {
  display: flex;
  gap: 8px;
}

.dev-slot-btn {
  flex: 1;
  padding: 10px 4px;
  border: 2px solid #333;
  border-radius: 8px;
  background: transparent;
  color: #666;
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}

.dev-slot-btn.active {
  border-color: var(--buzz-red);
  color: var(--text);
  background: rgba(227, 30, 36, 0.15);
}

.dev-btn-row {
  display: flex;
  gap: 6px;
}

.dev-action-btn {
  flex: 1;
  height: 46px;
  border: none;
  border-radius: 8px;
  color: #fff;
  font-size: 0.68rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: none;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  transition: transform 0.08s, opacity 0.08s;
  -webkit-tap-highlight-color: transparent;
}

.dev-action-btn:active,
.dev-action-btn.pressed { transform: scale(0.93); opacity: 0.8; }

.dev-action-btn[data-button="buzz"]   { background: var(--buzz-red); }
.dev-action-btn[data-button="blue"]   { background: var(--buzz-blue); }
.dev-action-btn[data-button="orange"] { background: var(--buzz-orange); }
.dev-action-btn[data-button="green"]  { background: var(--buzz-green); }
.dev-action-btn[data-button="yellow"] { background: var(--buzz-yellow); color: #111; }

/* ── Reconnect overlay ──────────────────────────────────────────────────────── */

#reconnect-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: 1rem;
  color: var(--text-muted);
  z-index: 100;
}

#reconnect-overlay strong {
  font-size: 1.3rem;
  color: var(--text);
}

/* ── Host page ──────────────────────────────────────────────────────────────── */

/* Host page fills the viewport with the Buzz! deep-navy background */
body:has(.host-page) {
  background: var(--host-bg);
}

.host-page {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: clamp(16px, 3vw, 40px) clamp(16px, 4vw, 48px);
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-width: 0;
}

.host-title {
  font-family: 'Fredoka One', system-ui, sans-serif;
  font-size: clamp(2.2rem, 6vw, 3.4rem);
  font-weight: 400; /* Fredoka One is already heavy */
  color: var(--buzz-red);
  letter-spacing: 0.05em;
  text-shadow:
    0 0 24px rgba(232, 0, 61, 0.55),
    0 2px 0 rgba(0,0,0,0.5);
  line-height: 1;
}

.host-setup {
  max-width: 560px;
}

/* Two-column dashboard */
.host-dash {
  display: grid;
  grid-template-columns: minmax(0, 320px) minmax(0, 1fr);
  gap: 20px;
  align-items: start;
  min-width: 0;
}

@media (max-width: 650px) {
  .host-dash {
    grid-template-columns: 1fr;
  }
}

.dash-left {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.dash-left-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.dash-right {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.host-room-row {
  display: flex;
  align-items: center;
  gap: 16px;
}

.host-room-label {
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* Shared h2 for host cards */
.host-page h2, .host-dash h2 {
  font-family: 'Fredoka One', system-ui, sans-serif;
  font-size: 1rem;
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--buzz-orange);
  margin-bottom: 10px;
}

.card {
  background: var(--host-card);
  border-radius: var(--radius);
  padding: 20px;
  min-width: 0;
}

/* QR card gets a subtle glow border */
#qr-container.card {
  border: 1px solid rgba(255, 114, 0, 0.35);
  box-shadow: 0 0 20px rgba(255, 114, 0, 0.15), 0 4px 16px rgba(0, 0, 0, 0.4);
}

.room-code {
  font-family: 'Fredoka One', monospace;
  font-size: clamp(2rem, 5vw, 3rem);
  font-weight: 400;
  letter-spacing: 0.2em;
  color: var(--buzz-yellow);
  text-shadow:
    0 0 20px rgba(255, 213, 0, 0.6),
    0 0 40px rgba(255, 213, 0, 0.3);
}

.player-slots {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player-slot {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 8px;
  background: var(--host-surface);
  font-size: 0.95rem;
  border-left: 4px solid transparent;
  min-width: 0;
}

/* Player strip colours by slot index */
.player-slot:nth-child(1) { border-left-color: var(--p1-colour); }
.player-slot:nth-child(2) { border-left-color: var(--p2-colour); }
.player-slot:nth-child(3) { border-left-color: var(--p3-colour); }
.player-slot:nth-child(4) { border-left-color: var(--p4-colour); }

.slot-num {
  font-weight: 700;
  color: var(--text-muted);
  width: 24px;
  flex-shrink: 0;
}

.slot-name {
  flex: 1;
  color: var(--text);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.slot-empty {
  color: var(--text-muted);
  font-style: italic;
}

.slot-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #444;
  flex-shrink: 0;
}

.slot-dot.connected { background: #6bff8e; }

.ngrok-form {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.ngrok-form input {
  flex: 1;
  min-width: 200px;
  padding: 12px 14px;
  border-radius: 8px;
  border: 2px solid #3a3a5c;
  background: var(--host-bg);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
}

.ngrok-form input:focus { border-color: var(--buzz-orange); }

.btn-connect {
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--buzz-orange) 0%, var(--buzz-red) 100%);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.15s, transform 0.1s;
}

.btn-connect:hover { opacity: 0.9; }
.btn-connect:active { transform: scale(0.97); }
.btn-connect:disabled { opacity: 0.4; cursor: not-allowed; }

#qr-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

#qr-canvas {
  border-radius: 8px;
  background: #fff;
  padding: 12px;
  width: 100%;
  max-width: 280px;
}

#qr-canvas canvas,
#qr-canvas img {
  width: 100% !important;
  height: auto !important;
}

.join-url-box {
  font-size: 0.72rem;
  color: var(--text-muted);
  word-break: break-all;
  text-align: center;
}

.btn-copy {
  padding: 10px 18px;
  border: 2px solid #3a3a5c;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s;
}

.btn-copy:hover { border-color: var(--buzz-orange); }

/* ── Start Game button ──────────────────────────────────────────────────────── */

.btn-start-game {
  width: 100%;
  padding: 18px;
  border: none;
  border-radius: var(--radius);
  background: linear-gradient(135deg, var(--buzz-orange) 0%, var(--buzz-red) 100%);
  color: #fff;
  font-family: 'Fredoka One', system-ui, sans-serif;
  font-size: 1.4rem;
  font-weight: 400;
  cursor: pointer;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  transition: opacity 0.15s, transform 0.1s;
  box-shadow: 0 4px 20px rgba(232, 0, 61, 0.4);
}

.btn-start-game:hover { opacity: 0.92; }
.btn-start-game:active { transform: scale(0.97); opacity: 0.85; }
.btn-start-game:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }

/* ── Lobby screen ───────────────────────────────────────────────────────────── */

.lobby-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px 24px;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  text-align: center;
}

.lobby-card h1 {
  font-size: 2rem;
  font-weight: 900;
  letter-spacing: -1px;
  color: var(--buzz-red);
}

.lobby-subtitle {
  font-size: 0.9rem;
  color: var(--text-muted);
}

.lobby-player-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.lobby-player-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--bg);
  font-size: 0.95rem;
}

/* ── Game Picker screen ──────────────────────────────────────────────────────── */

#picker-screen {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

.picker-header {
  padding: 20px 16px 10px;
  text-align: center;
  flex-shrink: 0;
}

.picker-header h2 {
  font-size: 1.4rem;
  font-weight: 900;
  color: var(--buzz-red);
  letter-spacing: -0.5px;
}

.game-grid {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  align-content: start;
  -webkit-overflow-scrolling: touch;
}

.picker-empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
  padding: 40px 0;
  line-height: 1.7;
}

.picker-empty code {
  background: var(--surface);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.85em;
}

.game-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 2px solid transparent;
  border-radius: var(--radius);
  padding-bottom: 10px;
  overflow: hidden;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: border-color 0.15s, transform 0.1s;
  text-align: center;
}

.game-card.selected {
  border-color: var(--buzz-red);
  transform: scale(0.97);
}

.game-cover {
  width: 100%;
  aspect-ratio: 2 / 3;
  background: var(--bg);
  position: relative;
  overflow: hidden;
}

.game-cover-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
  font-weight: 900;
  color: #444;
}

.game-cover img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.game-name {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text);
  padding: 0 8px;
  line-height: 1.3;
}

.picker-footer {
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid #333;
  flex-shrink: 0;
}
