:root {
  --bg: #f6f3ec;
  --surface: #ffffff;
  --surface-2: #efeae0;
  --text: #1d1733;
  --muted: #6b6480;
  --border: #e2dbcd;
  --accent: #4b36a8;
  --accent-soft: #ece8fb;
  --on-accent: #ffffff;
  --gold: #b07d0a;
  --good: #1d7a4b;
  --good-soft: #e3f4ea;
  --bad: #b3261e;
  --bad-soft: #fbe9e7;
  --warn: #8f5500;
  --warn-soft: #fdf0d9;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(29, 23, 51, 0.06), 0 4px 16px rgba(29, 23, 51, 0.05);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14102a;
    --surface: #1f1a3b;
    --surface-2: #2a2450;
    --text: #f1eefb;
    --muted: #a9a3c4;
    --border: #3a3363;
    --accent: #a996ff;
    --accent-soft: #2f2860;
    --on-accent: #14102a;
    --gold: #f2c14e;
    --good: #62d39c;
    --good-soft: #193a2f;
    --bad: #ff8a80;
    --bad-soft: #43202a;
    --warn: #ffc56b;
    --warn-soft: #3d3020;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

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

main {
  max-width: 640px;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; margin-bottom: 12px; }
p { margin: 0; }

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 56px;
  margin-bottom: 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.5rem;
  letter-spacing: 0.02em;
}

.star { width: 1.1em; height: 1.1em; fill: var(--gold); flex: none; }

.round-title { font-size: 1.15rem; font-weight: 600; text-align: center; }
.round-title strong { font-size: 1.4rem; }
.round-title .of { color: var(--muted); font-weight: 400; }

/* ---------- cards & text ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}

.eyebrow {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
}

.hint { color: var(--muted); font-size: 0.85rem; }
.warn { color: var(--warn); }
.good { color: var(--good); }
.bad { color: var(--bad); }
.error { color: var(--bad); font-size: 0.9rem; margin: 8px 0; }
.error:empty { display: none; }

.label-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.label-row h2 { margin-bottom: 0; }
.label { font-weight: 600; }

/* ---------- buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 48px;
  padding: 0 18px;
  border-radius: 12px;
  border: 1px solid transparent;
  font-weight: 600;
  text-decoration: none;
  background: var(--surface-2);
  color: var(--text);
}
.btn.small { min-height: 38px; padding: 0 14px; font-size: 0.9rem; }
.btn.block { display: flex; width: 100%; }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.caution { background: var(--warn-soft); color: var(--warn); border-color: var(--warn); }
.btn.danger { background: transparent; color: var(--bad); border-color: var(--bad); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.button-row { display: flex; gap: 8px; margin: 12px 0; }
.button-row .btn { flex: 1; }
.button-stack { display: grid; gap: 10px; margin: 8px 0 16px; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: none;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-size: 1.5rem;
  line-height: 1;
  text-decoration: none;
}
.icon-btn:disabled { opacity: 0.3; cursor: default; }
.icon-btn.spacer { visibility: hidden; }

.link {
  border: 0;
  background: none;
  padding: 0;
  color: var(--accent);
  text-decoration: underline;
  font-size: inherit;
}

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

.resume { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.resume-line { margin-top: 2px; font-size: 0.95rem; }

.field { margin: 0 0 20px; padding: 0; border: 0; min-width: 0; }
.field legend { padding: 0; margin-bottom: 8px; }

.player-list { list-style: none; margin: 0 0 10px; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; }
.player-list li { display: flex; align-items: center; gap: 4px; }
.player-list input { flex: 1; min-width: 0; margin-left: 6px; }
.player-list .icon-btn { width: 36px; height: 44px; font-size: 1.2rem; color: var(--muted); }
.chevron { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }

input[type="text"], select {
  min-height: 46px;
  padding: 0 12px;
  font-size: 16px; /* stops iOS zooming on focus */
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}
select { width: 100%; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
  min-height: 36px;
  padding: 0 12px;
  border: 1px dashed var(--border);
  border-radius: 999px;
  background: transparent;
  font-size: 0.9rem;
}
.chip::before { content: "+ "; color: var(--muted); }

.radio-card {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 6px;
  cursor: pointer;
}
.radio-card:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.radio-card input { margin: 3px 0 0; accent-color: var(--accent); width: 18px; height: 18px; flex: none; }
.radio-card span { display: grid; gap: 2px; }
.radio-card small { color: var(--muted); font-size: 0.83rem; }

.install-hint { color: var(--muted); font-size: 0.85rem; text-align: center; padding: 0 8px 16px; }

/* ---------- stepper ---------- */

.stepper {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  flex: none;
}
.step {
  width: 44px;
  height: 44px;
  border: 0;
  background: transparent;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--accent);
  touch-action: manipulation;
}
.step:disabled { color: var(--muted); opacity: 0.35; }
.step-value { min-width: 2ch; text-align: center; font-size: 1.2rem; font-weight: 700; }

/* ---------- round ---------- */

.round-meta { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.round-meta .big { font-size: 1.8rem; font-weight: 700; }
.bid-first { color: var(--muted); font-size: 0.88rem; margin-top: 2px; }

.phases { list-style: none; display: flex; gap: 6px; padding: 0; margin: 14px 0 6px; }
.phases li {
  flex: 1;
  text-align: center;
  padding: 6px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  background: var(--surface-2);
}
.phases li.active { background: var(--accent); color: var(--on-accent); }
.phases li.done { color: var(--accent); background: var(--accent-soft); }

.rows { list-style: none; padding: 0; margin: 0; }
.row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.row:last-child { border-bottom: 0; }
.row.is-dealer .seat { background: var(--gold); color: #1d1733; }
.row.invalid .stepper { border-color: var(--bad); background: var(--bad-soft); }

.seat {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  flex: none;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.who { flex: 1; min-width: 0; display: grid; }
.who .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who .sub { font-size: 0.82rem; color: var(--muted); }
.who .sub.warn { color: var(--warn); font-weight: 600; }

.tag {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 1px 6px;
  border-radius: 6px;
  vertical-align: 2px;
  background: var(--warn-soft);
  color: var(--warn);
}

.pts { min-width: 3.2ch; text-align: right; font-weight: 700; }

.tally {
  margin: 12px 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 0.92rem;
  text-align: center;
}
.tally.even { background: var(--good-soft); color: var(--good); }
.tally.warn { background: var(--warn-soft); color: var(--warn); }

/* ---------- standings ---------- */

.standings { list-style: none; padding: 0; margin: 0; }
.standings li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.standings li:last-child { border-bottom: 0; }
.standings .rank {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  flex: none;
  border-radius: 50%;
  background: var(--surface-2);
  font-size: 0.85rem;
  font-weight: 700;
}
.standings .leader .rank { background: var(--gold); color: #1d1733; }
.standings .name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.standings .delta { font-size: 0.85rem; font-weight: 600; }
.standings .total { min-width: 4ch; text-align: right; font-size: 1.15rem; font-weight: 700; }

/* ---------- scoreboard ---------- */

.table-wrap {
  overflow-x: auto;
  margin: 0 -16px;
  padding: 0 16px;
  -webkit-overflow-scrolling: touch;
}

.sheet { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
.sheet th, .sheet td { padding: 6px 4px; text-align: center; border-bottom: 1px solid var(--border); }
.sheet thead th {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  max-width: 5.5em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sheet th:first-child { position: sticky; left: 0; background: var(--surface); width: 2.5em; }
.sheet tbody tr { cursor: pointer; }
.sheet tbody tr:hover td { background: var(--surface-2); }

.rd {
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 8px;
  background: var(--surface-2);
  font-weight: 700;
  font-size: 0.85rem;
}

.sheet td { min-width: 3.8em; }
.cell-total { display: block; font-weight: 700; font-size: 1rem; }
.cell-detail { display: block; font-size: 0.72rem; color: var(--muted); white-space: nowrap; }
.made .cell-delta { color: var(--good); font-weight: 600; }
.missed .cell-delta { color: var(--bad); font-weight: 600; }
.legend { margin-top: 8px; }

/* ---------- results & history ---------- */

.winner { text-align: center; padding: 24px 16px; }
.winner .star { width: 44px; height: 44px; }
.winner-name { font-size: 1.5rem; font-weight: 700; margin-top: 6px; }
.winner-score { font-size: 1.1rem; color: var(--gold); font-weight: 700; margin-bottom: 8px; }

.history { list-style: none; padding: 0; margin: 0; }
.game-item { display: grid; gap: 6px; color: inherit; text-decoration: none; }
.game-date { font-size: 0.85rem; color: var(--muted); }
.game-scores { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.game-scores .leader { color: var(--gold); font-weight: 600; }
.game-scores .leader::before { content: "\2605  "; }
.empty { text-align: center; display: grid; gap: 12px; justify-items: center; }

/* ---------- dialog ---------- */

dialog {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 18px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgba(10, 8, 24, 0.55); }
dialog h2 { margin-bottom: 4px; }
dialog .button-stack { margin: 16px 0 0; }

.edit-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto 3.2ch;
  align-items: center;
  gap: 8px 6px;
  margin-top: 14px;
}
.edit-grid .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.edit-grid .col-head { font-size: 0.75rem; color: var(--muted); text-align: center; font-weight: 600; }
.edit-grid .step { width: 36px; height: 40px; }

@media (max-width: 380px) {
  .edit-grid .step { width: 30px; }
  .step-value { font-size: 1.05rem; }
}
