:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --surface: #f8fafc;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --card-text: #0f172a;

  --blue: #2563eb;
  --blue-soft: #bfdbfe;
  --red: #dc2626;
  --red-soft: #fecaca;
  --yellow: #facc15;
  --yellow-soft: #fef9c3;
  --black: #111111;

  --radius: 14px;
  --gap: clamp(6px, 1vw, 14px);
  --font: 'Rubik', system-ui, -apple-system, 'Segoe UI', Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
}

body {
  background:
    radial-gradient(circle at 15% 0%, rgba(37, 99, 235, 0.18), transparent 45%),
    radial-gradient(circle at 85% 100%, rgba(220, 38, 38, 0.16), transparent 45%),
    var(--bg);
  min-height: 100dvh;
}

[hidden] { display: none !important; }

a { color: inherit; }

/* ---------- shared UI ---------- */

.logo {
  margin: 0;
  font-weight: 800;
  font-size: clamp(2.2rem, 6vw, 3.5rem);
  letter-spacing: -0.02em;
}
.logo span { color: var(--red); }
.logo-hero { font-size: clamp(3rem, 12vw, 6rem); }

.btn {
  font: inherit;
  font-weight: 600;
  font-size: 1rem;
  padding: 0.75em 1.4em;
  border-radius: 999px;
  border: 0;
  cursor: pointer;
  background: var(--surface);
  color: var(--card-text);
  transition: transform 0.1s ease, opacity 0.2s ease;
}
.btn:active { transform: scale(0.96); }
.btn:focus-visible, .card:focus-visible, .home-card:focus-visible, .team-btn:focus-visible {
  outline: 3px solid var(--yellow);
  outline-offset: 3px;
}
.btn-primary { background: var(--blue); color: #fff; }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid rgba(255, 255, 255, 0.25); }
.btn-small { padding: 0.45em 1em; font-size: 0.85rem; }

.muted { color: var(--text-muted); }
.error { color: #fca5a5; min-height: 1.2em; margin: 0; }

.loading {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 1.4rem;
  color: var(--text-muted);
  padding: 16px;
  text-align: center;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  color: var(--card-text);
  padding: 0.7em 1.4em;
  border-radius: 999px;
  font-weight: 600;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  z-index: 50;
}

/* ---------- home ---------- */

.home {
  max-width: 760px;
  margin: 0 auto;
  padding: 8vh 16px 48px;
  text-align: center;
}
.tagline { color: var(--text-muted); font-size: 1.2rem; margin: 0.5em 0 2.5em; }

.home-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}
.home-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 28px 20px;
  border-radius: var(--radius);
  background: var(--bg-soft);
  border: 1px solid rgba(255, 255, 255, 0.08);
  text-decoration: none;
  transition: transform 0.15s ease, border-color 0.15s ease;
}
.home-card:hover { transform: translateY(-3px); border-color: rgba(255, 255, 255, 0.3); }
.home-icon { font-size: 2.6rem; }
.home-card strong { font-size: 1.4rem; }
.home-card small { color: var(--text-muted); }

.rules {
  margin-top: 40px;
  text-align: right;
  background: var(--bg-soft);
  border-radius: var(--radius);
  padding: 16px 20px;
}
.rules summary { cursor: pointer; font-weight: 600; font-size: 1.1rem; }
.rules li { margin: 0.6em 0; line-height: 1.6; }
.c-blue { color: #60a5fa; }
.c-red { color: #f87171; }
.c-yellow { color: var(--yellow); }

/* ---------- host lobby ---------- */

.lobby {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px 16px;
  text-align: center;
}
.lobby-hint { font-size: 1.3rem; color: var(--text-muted); margin: 0; }
.code-big {
  font-size: clamp(3.5rem, 12vw, 7rem);
  font-weight: 800;
  letter-spacing: 0.15em;
  direction: ltr;
  background: var(--surface);
  color: var(--card-text);
  padding: 0.05em 0.4em 0.05em 0.55em;
  border-radius: var(--radius);
}
.qr { width: 180px; height: 180px; background: #fff; border-radius: 10px; overflow: hidden; }
.qr:empty { display: none; }
.qr svg { width: 100%; height: 100%; display: block; }
.join-link { margin: 0; direction: ltr; color: var(--text-muted); word-break: break-all; }
.lobby-teams { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.lobby-team {
  padding: 0.6em 1.2em;
  border-radius: 999px;
  font-weight: 600;
  background: var(--bg-soft);
  border: 2px solid transparent;
  opacity: 0.7;
}
.lobby-team.connected { opacity: 1; }
.lobby-team.team-blue.connected { border-color: var(--blue); }
.lobby-team.team-red.connected { border-color: var(--red); }

/* ---------- game layout ---------- */

.game, .phone-game {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: var(--gap) clamp(8px, 1.5vw, 24px);
}

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
}
.topbar-center { text-align: center; display: grid; gap: 4px; justify-items: center; }
.topbar-code { color: var(--text-muted); font-size: 0.95rem; }
.topbar-code strong { color: var(--text); letter-spacing: 0.1em; }

.score {
  display: grid;
  justify-items: center;
  min-width: 70px;
  padding: 6px 14px;
  border-radius: var(--radius);
  font-weight: 800;
  color: #fff;
}
.score span { font-size: clamp(1.6rem, 3vw, 2.4rem); line-height: 1.1; }
.score small { font-weight: 600; opacity: 0.85; }
.score .score-sub { font-weight: 400; font-size: 0.75rem; opacity: 0.8; }
.score-blue { background: var(--blue); }
.score-red { background: var(--red); }

.turn {
  font-weight: 800;
  font-size: clamp(1.1rem, 2.4vw, 1.8rem);
  padding: 0.25em 1em;
  border-radius: 999px;
  color: #fff;
  transition: background 0.3s ease;
}
.turn-blue { background: var(--blue); }
.turn-red { background: var(--red); }

.toolbar {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}
.toolbar-compact { justify-content: space-between; align-items: center; }

/* ---------- board & cards ---------- */

.board {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-template-rows: repeat(5, minmax(0, 1fr));
  gap: var(--gap);
  flex: 1 1 auto;
  min-height: 0;
}

.card {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 4px;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--card-text);
  font: inherit;
  font-weight: 800;
  font-size: clamp(0.9rem, 2.3vw, 2.4rem);
  line-height: 1.15;
  text-align: center;
  cursor: pointer;
  box-shadow: 0 4px 0 #cbd5e1, 0 8px 18px rgba(0, 0, 0, 0.25);
  transition: transform 0.12s ease, background 0.3s ease, box-shadow 0.3s ease;
}
.card:not(:disabled):hover { transform: translateY(-2px); }
.card:disabled { cursor: default; }
/* Unrevealed cards look muted while guessing isn't allowed (no clue yet / not your turn). */
.board:not(.clickable) .card:not(.revealed):not(.key) { opacity: 0.8; }
.card-word { overflow-wrap: anywhere; }

/* Revealed cards (host + phones) */
.card.revealed { color: #fff; box-shadow: 0 8px 18px rgba(0, 0, 0, 0.25); }
.card.revealed.color-blue { background: var(--blue); }
.card.revealed.color-red { background: var(--red); }
.card.revealed.color-neutral { background: var(--yellow); color: var(--card-text); }
.card.revealed.color-black { background: var(--black); outline: 2px solid #444; }

.card.flip { animation: flip 0.6s ease; }
@keyframes flip {
  0% { transform: rotateY(0) scale(1); }
  50% { transform: rotateY(90deg) scale(1.05); }
  100% { transform: rotateY(0) scale(1); }
}

/* Phone boards: compact */
.board-phone { gap: 6px; }
.board-phone .card {
  font-size: clamp(0.72rem, 3.6vw, 1.5rem);
  border-radius: 10px;
  box-shadow: 0 3px 0 #cbd5e1;
}
.board-phone .card.revealed, .board-key .card { box-shadow: none; }

/* Spymaster key */
.card.key { border: 3px solid transparent; }
.card.key.color-blue { background: var(--blue); color: #fff; }
.card.key.color-red { background: var(--red); color: #fff; }
.card.key.color-neutral { background: var(--yellow-soft); color: var(--card-text); }
.card.key.color-black { background: var(--black); color: #fff; }

/* On phones, revealed cards fade out so the spymaster sees what is left. */
.board-key .card.revealed { opacity: 0.35; filter: saturate(0.6); }
.board-key .card.revealed .card-word { text-decoration: line-through; }
.board-key .card.revealed::after {
  content: '✓';
  position: absolute;
  top: 2px;
  left: 6px;
  font-size: 0.8em;
}

/* Emphasize my own team's words */
body[data-team='blue'] .card.key.color-blue,
body[data-team='red'] .card.key.color-red {
  border-color: #fff;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.25);
}

.legend { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; font-size: 0.8rem; color: var(--text-muted); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-inline-start: 6px; }
.dot-blue { background: var(--blue); }
.dot-red { background: var(--red); }
.dot-neutral { background: var(--yellow-soft); }
.dot-black { background: var(--black); border: 1px solid #555; }

/* ---------- phone panels ---------- */

.page-play { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }

.panel {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 24px 16px;
  text-align: center;
}
.panel h2 { margin: 0; font-size: 1.8rem; }

.join-form { display: grid; gap: 12px; width: min(320px, 100%); }
.join-form label { color: var(--text-muted); }
.code-input {
  font: inherit;
  font-size: 2.4rem;
  font-weight: 800;
  text-align: center;
  letter-spacing: 0.3em;
  direction: ltr;
  padding: 0.3em 0.2em 0.3em 0.5em;
  border-radius: var(--radius);
  border: 2px solid transparent;
  background: var(--surface);
  color: var(--card-text);
  width: 100%;
}
.code-input:focus { outline: none; border-color: var(--blue); }

.team-picker { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; width: min(420px, 100%); }
.team-btn {
  font: inherit;
  font-size: 1.8rem;
  font-weight: 800;
  color: #fff;
  border: 0;
  border-radius: var(--radius);
  padding: 32px 12px;
  display: grid;
  gap: 6px;
  cursor: pointer;
}
.team-btn small { font-size: 0.9rem; font-weight: 400; opacity: 0.9; }
.team-btn .taken { font-size: 0.8rem; font-style: normal; background: rgba(0, 0, 0, 0.3); border-radius: 999px; padding: 2px 8px; }
.team-btn.team-blue { background: var(--blue); }
.team-btn.team-red { background: var(--red); }

.topbar-compact { gap: 8px; }
.topbar-compact .score { min-width: 54px; padding: 4px 8px; }
.topbar-compact .turn { font-size: 0.85rem; white-space: nowrap; padding: 0.3em 0.8em; }
.identity { font-size: 0.85rem; font-weight: 600; }
.identity-blue { color: #93c5fd; }
.identity-red { color: #fca5a5; }

.winner-banner {
  text-align: center;
  font-weight: 700;
  padding: 10px;
  border-radius: var(--radius);
  color: #fff;
}
.winner-banner-win { background: #16a34a; }
.winner-banner-loss { background: #374151; }

/* ---------- winner overlay (host) ---------- */

.winner-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(2, 6, 23, 0.75);
  backdrop-filter: blur(4px);
  z-index: 40;
  padding: 16px;
}
.winner-box {
  background: var(--bg-soft);
  border-radius: 20px;
  padding: 36px 28px;
  text-align: center;
  max-width: 560px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: pop 0.35s ease;
}
.winner-box h2 { font-size: clamp(2rem, 5vw, 3.2rem); margin: 0 0 12px; }
.winner-box p { font-size: 1.2rem; color: var(--text-muted); margin: 0 0 24px; }
.winner-title-blue { color: #60a5fa; }
.winner-title-red { color: #f87171; }
.winner-title-loss { color: #f8fafc; }
.winner-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

@keyframes pop {
  from { transform: scale(0.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Phones in portrait: a 5x5 grid of words is tight, so keep cards compact */
@media (max-width: 480px) {
  .game .card { font-size: 0.8rem; border-radius: 8px; }
}

/* Host: after the game ends, show the remaining map faded */
.page-host .card.key { opacity: 0.45; }

/* Home: join-with-code card */
.home-join { cursor: default; }
.home-join:hover { transform: none; }
.home-join .code-input { font-size: 1.8rem; margin-top: 8px; }
.home-join .btn { width: 100%; }
.home-card-btn { width: 100%; margin-top: 16px; }
@media (min-width: 560px) { .home-card-btn { margin-top: auto; } } /* align with the join button */

/* ---------- mode & role pickers ---------- */

.mode-options, .role-picker {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
  width: min(760px, 100%);
}
.mode-card, .role-btn {
  font: inherit;
  color: var(--text);
  cursor: pointer;
  text-align: center;
}
.mode-card small { line-height: 1.5; }
.role-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 24px 16px;
  border-radius: var(--radius);
  background: var(--bg-soft);
  border: 2px solid rgba(255, 255, 255, 0.1);
}
.role-btn strong { font-size: 1.4rem; }
.role-btn small { color: var(--text-muted); }
.role-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.role-btn .taken, .team-btn .taken {
  font-size: 0.8rem;
  font-style: normal;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 999px;
  padding: 2px 10px;
}

/* ---------- clue ---------- */

.clue-bar {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.5em;
  padding: 0.3em 1em;
  border-radius: 999px;
  background: var(--surface);
  color: var(--card-text);
  border: 3px solid var(--blue);
  font-size: clamp(1rem, 2.2vw, 1.8rem);
}
.clue-bar.team-red { border-color: var(--red); }
.clue-bar.waiting {
  background: transparent;
  color: var(--text-muted);
  border-style: dashed;
  font-size: clamp(0.85rem, 1.6vw, 1.1rem);
}
.clue-label { font-weight: 600; }
.clue-word { font-weight: 800; font-size: 1.3em; }
.clue-count {
  font-weight: 800;
  background: var(--card-text);
  color: #fff;
  border-radius: 999px;
  min-width: 1.6em;
  padding: 0 0.4em;
  text-align: center;
}
.phone-game .clue-bar { align-self: center; max-width: 100%; }

.clue-form {
  display: grid;
  gap: 8px;
  background: var(--bg-soft);
  border-radius: var(--radius);
  padding: 10px;
}
.clue-input {
  font: inherit;
  font-size: 1.2rem;
  font-weight: 700;
  padding: 0.5em 0.8em;
  border-radius: 10px;
  border: 2px solid transparent;
  background: var(--surface);
  color: var(--card-text);
}
.clue-input:focus { outline: none; border-color: var(--yellow); }
.clue-numbers { display: grid; grid-template-columns: repeat(9, 1fr); gap: 4px; }
.num-btn {
  font: inherit;
  font-weight: 800;
  padding: 0.45em 0;
  border-radius: 8px;
  border: 0;
  background: rgba(255, 255, 255, 0.12);
  color: var(--text);
  cursor: pointer;
}
.num-btn.active { background: var(--yellow); color: var(--card-text); }
.clue-form .error:empty { display: none; }

.hint { margin: 0; text-align: center; color: var(--text-muted); font-weight: 600; }
body[data-role] .hint { color: var(--text); }
.next-turn { align-self: center; }
.toolbar-buttons { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }

.code-medium { font-size: 3rem; margin: 8px 0 16px; }
.winner-box .qr { margin: 0 auto 20px; }

/* Win tally across games */
.wins { font-size: 0.8rem; color: var(--text-muted); white-space: nowrap; }
.wins b { font-weight: 700; }
.page-host .wins { font-size: 0.95rem; }

/* Home: other games */
.home-section-title { margin: 40px 0 14px; font-size: 1.2rem; color: var(--text-muted); font-weight: 600; }
.home-card-wide { flex-direction: row; gap: 16px; text-align: right; }
.home-card-wide .home-card-text { display: grid; gap: 4px; flex: 1; }
.home-card-wide .home-card-btn { width: auto; margin: 0; white-space: nowrap; }
.alias-card { background: linear-gradient(135deg, #b91c1c, #7f1d1d); border-color: rgba(255, 255, 255, 0.15); }
.alias-card small { color: rgba(255, 255, 255, 0.85); }
@media (max-width: 560px) {
  .home-card-wide { flex-direction: column; text-align: center; }
  .home-card-wide .home-card-btn { width: 100%; }
}
