:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #6b7280;
  --border: #e3e5ea;
  --primary: #1d4ed8;
  --primary-text: #ffffff;
  --accent: #f59e0b;
  --danger: #dc2626;
  --success: #15803d;
  --leader-bg: #fef3c7;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #1a1d23;
    --text: #eceef2;
    --muted: #9aa1ad;
    --border: #2c3038;
    --primary: #3b82f6;
    --accent: #fbbf24;
    --danger: #f87171;
    --success: #4ade80;
    --leader-bg: #3a2f10;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* iOS 26.1+ home-screen apps draw an opaque status bar in the body's
   background-color. Make that blue to match the top bar; the image layer
   keeps the page itself the normal colour. */
@media (display-mode: standalone) {
  body {
    background-color: var(--primary);
    background-image: linear-gradient(var(--bg), var(--bg));
  }
}

[hidden] { display: none !important; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--primary);
  color: var(--primary-text);
}
.topbar h1 { margin: 0; font-size: 1.3rem; letter-spacing: .02em; }
.topbar .btn.ghost { color: var(--primary-text); border-color: rgba(255,255,255,.5); }

main {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px;
}

/* Footer: which build this phone is running, and whether games will be saved */
.app-footer {
  max-width: 560px;
  margin: 0 auto;
  padding: 4px 16px max(16px, env(safe-area-inset-bottom));
  color: var(--muted);
  font-size: .8rem;
  text-align: center;
}

h2 { margin: 0 0 8px; font-size: 1.1rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 14px;
}
.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.card-head h2 { margin: 0; }

.row { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; }
.muted { color: var(--muted); font-size: .9rem; }
.hint { color: var(--muted); font-size: .85rem; margin: 8px 0 0; }

input[type="text"], input[type="number"] {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 16px; /* avoid iOS zoom */
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
}
input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.btn {
  font: inherit;
  font-weight: 600;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 34px; padding: 0 10px; font-size: .85rem; }
.btn.block { display: block; width: 100%; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.icon { min-width: 40px; padding: 0; }
.btn.on { background: var(--accent); border-color: var(--accent); color: #1a1300; }
.btn.bust.on { background: var(--danger); border-color: var(--danger); color: #fff; }

.actions { margin-top: 12px; }

/* Setup */
.player-list { list-style: none; margin: 0 0 10px; padding: 0; }
.player-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}
.player-list li .name { flex: 1; font-weight: 500; }
.player-list .empty { color: var(--muted); border: 0; }
.target-row span { flex: 1; font-weight: 600; }
.target-row input { max-width: 110px; text-align: right; }

/* Custom rules */
.rule { display: flex; gap: 12px; align-items: flex-start; padding: 8px 0; cursor: pointer; }
.rule input { width: 22px; height: 22px; margin: 1px 0 0; flex: none; accent-color: var(--primary); }
.rule .hint { display: block; margin-top: 2px; }

/* Scoreboard */
.scoreboard { list-style: none; margin: 0; padding: 0; }
.scoreboard li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: 8px;
}
.scoreboard li + li { border-top: 1px solid var(--border); }
.scoreboard li.leader { background: var(--leader-bg); }
.scoreboard .pos { width: 1.6em; color: var(--muted); font-weight: 600; }
.scoreboard .name { flex: 1; font-weight: 600; overflow-wrap: anywhere; }
.scoreboard .to-go { color: var(--muted); font-size: .8rem; }
.scoreboard .total { font-size: 1.4rem; font-weight: 700; min-width: 3ch; text-align: right; }
.dealer-chip {
  font-size: .7rem;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 99px;
  background: var(--primary);
  color: var(--primary-text);
  margin-left: 6px;
  vertical-align: middle;
}

/* Round entry */
.entry {
  display: grid;
  grid-template-columns: 1fr 88px;
  grid-template-areas: "name input" "quick quick";
  gap: 6px 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.entry:last-child { border-bottom: 0; }
.entry .name { grid-area: name; align-self: center; font-weight: 600; overflow-wrap: anywhere; }
.entry input { grid-area: input; text-align: right; font-size: 1.1rem; font-weight: 600; }
.entry .quick { grid-area: quick; display: flex; gap: 6px; }
.entry .quick .btn { flex: 1; }
.entry.busted input { color: var(--danger); }

/* History */
.table-wrap { overflow-x: auto; margin-top: 8px; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 6px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; color: var(--muted); }
thead th { font-size: .85rem; position: sticky; top: 0; background: var(--surface); }
tbody tr { cursor: pointer; }
tbody tr:active, tbody tr.editing { background: var(--leader-bg); }
td.zero { color: var(--danger); }
td.flip7 { color: var(--success); font-weight: 700; }
tfoot td { font-weight: 700; border-bottom: 0; }

.banner {
  background: var(--accent);
  color: #1a1300;
  font-weight: 600;
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 14px;
}
.install-tip { display: flex; align-items: center; gap: 10px; }
.install-tip span { flex: 1; }
.install-tip .btn { color: #1a1300; border-color: rgba(26,19,0,.35); }
.install-tip .btn:not(.ghost) { background: #fff; }

/* Rules */
.rules summary { font-weight: 600; cursor: pointer; }
.rules ul { margin: 10px 0 0; padding-left: 20px; }
.rules li { margin-bottom: 6px; }

/* Winner dialog */
dialog {
  width: min(92vw, 420px);
  border: 0;
  border-radius: 16px;
  padding: 20px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog h2 { text-align: center; font-size: 1.4rem; margin-bottom: 12px; }
dialog .actions .btn { flex: 1; }
dialog .btn.block { margin-top: 8px; }

/* Bank dialog */
.entry .quick .btn.bank { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
#bank-dialog h2 { margin-bottom: 8px; }
.bank-label { margin: 10px 0 6px; font-size: .85rem; font-weight: 600; color: var(--muted); }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(52px, 1fr)); gap: 6px; }
.btn.playing-card { padding: 0; min-height: 48px; font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.bank-breakdown { margin: 12px 0 0; min-height: 1.4em; color: var(--muted); font-size: .9rem; text-align: center; }
.bank-score { margin: 2px 0 0; text-align: center; font-size: 1rem; color: var(--muted); }
.bank-score span { font-size: 2.2rem; font-weight: 700; color: var(--text); }

/* Tabs */
.tabs { display: flex; gap: 4px; padding: 4px; margin-bottom: 14px; border-radius: 10px; background: var(--border); }
.tab {
  flex: 1;
  font: inherit;
  font-weight: 600;
  font-size: .9rem;
  min-height: 38px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.tab.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.15); }

/* Leaderboard and past games: read-only tables */
table.stats tbody tr { cursor: default; }
table.stats tbody tr:active { background: none; }
table.stats tbody tr.leader { background: var(--leader-bg); }
#leaders td:first-child { color: var(--text); font-weight: 600; }

.past-list { list-style: none; margin: 8px 0 0; padding: 0; }
.past-list li + li { border-top: 1px solid var(--border); }
.past-game {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  padding: 10px 4px;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.past-game:active { background: var(--leader-bg); }
.past-when { color: var(--muted); font-size: .85rem; }
.past-winner { font-weight: 700; }
.past-others { color: var(--muted); font-size: .9rem; overflow-wrap: anywhere; }

#game-dialog { width: min(96vw, 560px); }
#game-dialog h2 { margin-bottom: 4px; }
.game-meta { text-align: center; margin: 0 0 8px; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
#leaders th, #leaders td { padding: 8px 3px; font-size: .85rem; }
#leaders td:first-child { position: sticky; left: 0; background: var(--surface); }
#leaders tr.leader td:first-child { background: var(--leader-bg); }

/* Known-player suggestions at setup, and tappable names on the leaderboard */
.suggest { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.btn.chip { border-radius: 999px; font-weight: 500; }
.link-btn {
  font: inherit;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  text-align: left;
  text-decoration: underline dotted var(--muted);
  text-underline-offset: 3px;
  cursor: pointer;
}

/* ---- Live game: share dialog and the read-only watch view */
.live-qr { margin: 12px auto 0; max-width: 240px; }
.live-qr svg { display: block; width: 100%; height: auto; border-radius: 8px; }
.live-link { margin: 10px 0 0; text-align: center; font-size: .85rem; overflow-wrap: anywhere; }
.live-link a { color: var(--primary); }
#live-dialog .actions { flex-wrap: wrap; }

/* Big screens (a TV) get bigger type so the scores read across the room. */
@media (min-width: 900px) {
  .watching main { max-width: 1000px; }
  .watching { font-size: 1.5rem; }
  .watching h2 { font-size: 2rem; }
  .watching .muted, .watching .hint { font-size: 1.1rem; }
  .watching .scoreboard li { padding: 14px 16px; }
  .watching .scoreboard .to-go { font-size: 1.1rem; }
  .watching .scoreboard .total { font-size: 2.6rem; }
  .watching .banner { font-size: 1.6rem; }
}

/* PIN */
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.pin-reason { text-align: center; }
.pin-input {
  display: block;
  width: 100%;
  margin: 12px 0 8px;
  padding: 10px 12px;
  font: inherit;
  font-size: 1.6rem;
  letter-spacing: .3em;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
}
.pin-error { color: var(--danger); text-align: center; margin: 0; font-size: .9rem; }
.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-8px); } 75% { transform: translateX(8px); } }

/* Short confirmations. A popover, so it shows above open dialogs too. */
.toast {
  position: fixed;
  inset: auto 16px max(24px, env(safe-area-inset-bottom));
  width: fit-content;
  max-width: 480px;
  margin: 0 auto;
  padding: 10px 18px;
  border: 0;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: .95rem;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
  z-index: 20;
}
.toast:not(.show) { display: none; }
.winner-unsaved { text-align: center; margin-top: 8px; }
.winner-unsaved .btn { margin-top: 6px; }
.watching #lock-btn { display: none; }

/* Game versions: With a Vengeance turns the app red */
body.vengeance { --primary: #b91c1c; }
@media (prefers-color-scheme: dark) {
  body.vengeance { --primary: #dc2626; }
}
.topbar { flex-wrap: wrap; }
.topbar h1 { white-space: nowrap; }
.topbar .btn { white-space: nowrap; }
.version-strip {
  flex: 0 0 calc(100% + 32px); /* full width: spans the top bar's side padding */
  margin: 10px -16px -12px;
  padding: 6px 16px;
  background: rgba(0,0,0,.18);
  font-size: .9rem;
  font-weight: 600;
  text-align: center;
}

/* Welcome screen */
.welcome-head { text-align: center; margin: 8px 0 16px; }
.welcome-title { margin: 0; font-size: 2.6rem; font-weight: 800; letter-spacing: .02em; color: var(--primary); }
.welcome-head .muted { margin: 4px 0 0; }
.version-choice { display: grid; gap: 12px; margin-bottom: 14px; }
.version-choice .btn {
  display: flex; flex-direction: column; gap: 4px;
  min-height: 96px; padding: 16px; border-radius: 14px;
  background: var(--surface); font-size: 1.3rem; text-align: center; justify-content: center;
}
.version-choice .btn .hint { margin: 0; font-size: .95rem; font-weight: 500; }
.version-choice .btn.venge { border-color: #dc2626; }
.version-choice .btn.on { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.version-choice .btn.on .hint { color: inherit; opacity: .85; }
#welcome-continue-btn { margin-top: 8px; }
.setup-game { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.tabs.filter { margin: 8px 0 4px; }
.version-tag { margin-left: 6px; color: var(--primary); font-weight: 600; }
.winner-version { text-align: center; margin: -6px 0 10px; }
#bank-hint { margin: 6px 0 0; }
#bank-hint:empty { display: none; }
.card-grid .btn.playing-card[data-kind="zero"], .card-grid .btn.playing-card[data-kind="lucky"] { grid-column: span 2; font-size: 1rem; }
