:root {
  --bg: #f7f4ee;
  --card: #fdfbf7;
  --raised: #efe9df;
  --line: rgba(31, 27, 22, 0.1);
  --line-strong: rgba(31, 27, 22, 0.18);
  --text: #1f1b16;
  --text-2: #5e554a;
  --muted: #776e62;
  --accent: #2c4a7c;          /* fountain-pen blue */
  --accent-ink: #ffffff;
  --accent-soft: rgba(44, 74, 124, 0.1);
  --pos: #1d7a45;
  --neg: #b3261e;
  --suit-red: #b3261e;
  --ease: cubic-bezier(0.2, 0, 0, 1);
  --sans: 'Instrument Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --serif: 'Newsreader', Georgia, 'Times New Roman', serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161513;
    --card: #1c1a18;
    --raised: #262421;
    --line: rgba(239, 233, 222, 0.08);
    --line-strong: rgba(239, 233, 222, 0.16);
    --text: #efe9de;
    --text-2: #aaa398;
    --muted: #8a847a;
    --accent: #e9e2d4;
    --accent-ink: #1c1a18;
    --accent-soft: rgba(143, 175, 224, 0.14);
    --pos: #4cc38a;
    --neg: #e5605a;
    --suit-red: #e5605a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 16px/1.4 var(--sans);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- masthead ---------- */
.masthead {
  max-width: 560px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px 4px;
}
.wordmark {
  margin: 0;
  font: 500 24px/1.2 var(--serif);
  letter-spacing: 0;
}
.wordmark span { color: var(--text-2); font-weight: 400; }

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 8px 16px calc(112px + env(safe-area-inset-bottom));
}
.app[data-view="sheet"] { max-width: none; }

/* ---------- tabs ---------- */
.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin: 4px 0 16px;
  background: var(--raised);
  border-radius: 12px;
  max-width: 528px;
}
.app[data-view="sheet"] .tabs { margin-left: auto; margin-right: auto; }
.tabs button {
  flex: 1;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  font-weight: 500;
  transition: background-color 150ms var(--ease), color 150ms var(--ease);
}
.tabs button[aria-selected="true"] {
  background: var(--card);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 0 0 1px var(--line);
}

/* ---------- panels ---------- */
.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 12px;
}
.panel.tight { padding: 12px 16px 16px; }
.section-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 12px;
  font-size: 15px;
  font-weight: 500;
}
.meta { color: var(--muted); font-size: 13px; font-weight: 400; }
.hint { margin: 8px 0 0; color: var(--muted); font-size: 13px; }

/* ---------- setup ---------- */
.name-list { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 8px; }
.name-row { display: grid; grid-template-columns: 24px 1fr 40px; align-items: center; gap: 8px; }
.seat { color: var(--muted); font-size: 13px; text-align: right; }
.name-input {
  height: 44px;
  width: 100%;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--bg);
  font-size: 16px;
}
.name-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.icon-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border: 0; border-radius: 10px;
  background: transparent; color: var(--muted);
  transition: background-color 150ms var(--ease), color 150ms var(--ease);
}
.icon-btn:hover:not(:disabled) { background: var(--raised); color: var(--text); }
.icon-btn:disabled { opacity: 0.3; }

.segmented { display: flex; gap: 4px; padding: 4px; background: var(--raised); border-radius: 12px; }
.segmented button {
  flex: 1; height: 40px; border: 0; border-radius: 8px; background: transparent;
  color: var(--text-2); font-weight: 500;
  transition: background-color 150ms var(--ease), color 150ms var(--ease);
}
.segmented button[aria-checked="true"] { background: var(--card); color: var(--text); box-shadow: 0 0 0 1px var(--line), 0 1px 2px rgba(0,0,0,.06); }
.segmented button:disabled { opacity: 0.4; }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  height: 36px; padding: 0 14px;
  border: 1px solid var(--line-strong); border-radius: 18px;
  background: transparent; color: var(--text-2);
  transition: background-color 150ms var(--ease), color 150ms var(--ease), border-color 150ms var(--ease);
}
.chip[aria-pressed="true"] { background: var(--text); border-color: var(--text); color: var(--bg); }

.toggle { display: flex; gap: 12px; align-items: flex-start; cursor: pointer; }
.toggle input { width: 20px; height: 20px; margin: 2px 0 0; accent-color: var(--accent); flex: none; }
.toggle-text { display: grid; gap: 0; }
.toggle-text .hint { margin-top: 4px; }

/* ---------- buttons ---------- */
.btn {
  height: 48px;
  padding: 0 20px;
  border-radius: 12px;
  border: 1px solid transparent;
  font-weight: 500;
  transition: background-color 150ms var(--ease), opacity 150ms var(--ease), transform 100ms var(--ease);
}
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:disabled { opacity: 0.45; }
.btn.secondary { background: var(--card); border-color: var(--line-strong); color: var(--text); }
.btn.secondary:disabled { opacity: 0.4; }
.btn.block { width: 100%; }
.btn.grow { flex: 1; }

.action-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  gap: 8px;
  padding: 12px max(16px, calc((100vw - 528px) / 2)) calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
  z-index: 5;
}
.action-bar.quiet { position: static; padding: 16px 0 0; background: none; border: 0; backdrop-filter: none; justify-content: center; flex-wrap: wrap; }

/* ---------- round ---------- */
.round-head {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 4px 16px;
  margin: 4px 0 12px;
}
.round-title { display: flex; align-items: baseline; gap: 8px; }
.round-num { font: 500 32px/1.1 var(--serif); }
.round-of { color: var(--muted); font-size: 15px; }
.hand-size {
  grid-row: span 2;
  display: grid;
  justify-items: center;
  align-content: center;
  min-width: 72px;
  padding: 8px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--card);
  box-shadow: 2px 2px 0 var(--raised), 2px 2px 0 1px var(--line);
}
.hand-count { font: 500 32px/1 var(--serif); }
.hand-label { color: var(--muted); font-size: 12px; margin-top: 2px; }
.dealer-line { margin: 0; color: var(--text-2); font-size: 15px; }

.trump-row {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 0 0 12px;
}
p.trump-row { display: block; }
.trump-label { color: var(--text-2); font-size: 14px; margin-right: auto; }
.suit {
  width: 44px; height: 40px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--card);
  font-size: 20px; line-height: 1;
  color: var(--text);
  transition: background-color 150ms var(--ease), border-color 150ms var(--ease), color 150ms var(--ease);
}
.suit.red { color: var(--suit-red); }
.suit[aria-checked="true"] { border-color: var(--text); box-shadow: inset 0 0 0 1px var(--text); background: var(--raised); }

.player-list { list-style: none; margin: 0; padding: 0; }
.player-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 60px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}
.player-row:first-child { border-top: 0; }
.player-row.blocked .step-val { color: var(--neg); }
.who { flex: 1; min-width: 0; display: grid; }
.who .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who .sub { color: var(--muted); font-size: 13px; }
.dealer-tag {
  display: inline-grid; place-items: center;
  width: 20px; height: 20px; margin-left: 4px;
  border: 1px solid var(--line-strong); border-radius: 50%;
  font-size: 11px; font-weight: 600; color: var(--text-2);
  vertical-align: 1px;
}
.pos { color: var(--pos); }
.neg { color: var(--neg); }

.stepper { display: flex; align-items: center; gap: 4px; flex: none; }
.step {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 1px solid var(--line-strong); border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  transition: background-color 120ms var(--ease), transform 100ms var(--ease);
}
.step:active:not(:disabled) { transform: scale(0.94); background: var(--raised); }
.step:disabled { opacity: 0.3; }
.step-val {
  width: 40px;
  text-align: center;
  font: 500 24px/1 var(--serif);
}

.tally {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--line-strong);
}
.tally-num { font: 500 20px/1 var(--serif); }
.tally-of { font: 400 14px var(--sans); color: var(--muted); }
.tally-note { font-size: 14px; color: var(--text-2); text-align: right; }
.tally-note.even { color: var(--pos); }
.tally-note.warn { color: var(--neg); }

.standings { list-style: none; margin: 0; padding: 0; }
.standings li {
  display: grid;
  grid-template-columns: 24px 1fr auto;
  gap: 8px;
  align-items: baseline;
  padding: 6px 0;
  border-top: 1px solid var(--line);
}
.standings li:first-child { border-top: 0; }
.standings .rank { color: var(--muted); font-size: 13px; }
.standings .total { font-weight: 500; }
.standings.large li { padding: 10px 0; font-size: 17px; }
.standings.large li.top .total, .standings.large li.top .name { font-weight: 600; }

.round-head.single { grid-template-columns: 1fr; }
.results { grid-template-columns: 1fr; text-align: left; margin-bottom: 16px; }
.winner { margin: 4px 0 0; font: 500 32px/1.15 var(--serif); }
.winner-score { margin: 0; color: var(--text-2); }

.edit-pair { display: grid; grid-template-columns: auto auto; align-items: center; gap: 4px 8px; }
.pair-label { color: var(--muted); font-size: 12px; text-align: right; }
.player-row.edit { align-items: center; }

/* ---------- sheet ---------- */
.sheet-wrap {
  overflow-x: auto;
  margin: 0 -16px;
  padding: 0 16px;
  -webkit-overflow-scrolling: touch;
}
.sheet {
  border-collapse: separate;
  border-spacing: 0;
  margin: 0 auto;
  background: var(--card);
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  overflow: hidden;
  min-width: 100%;
}
@media (min-width: 720px) { .sheet { min-width: 0; } }
.sheet th, .sheet td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 0;
}
.sheet tr > :last-child { border-right: 0; }
.sheet thead th {
  height: 40px;
  padding: 0 8px;
  font-weight: 500;
  font-size: 14px;
  background: var(--raised);
  border-bottom: 1px solid var(--line-strong);
  position: sticky; top: 0;
}
.sheet .pcol { min-width: 72px; max-width: 104px; }
.sheet .pcol span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet .rcol {
  width: 36px;
  text-align: center;
  font: 500 16px var(--serif);
  position: sticky; left: 0; z-index: 1;
  background: var(--card);
}
.sheet thead .rcol { background: var(--raised); z-index: 2; }
.sheet .tcol { width: 28px; text-align: center; font-size: 15px; color: var(--text-2); }
.sheet .tcol.red { color: var(--suit-red); }
.sheet tbody tr.editable { cursor: pointer; }
.sheet tbody tr.editable:hover td, .sheet tbody tr.editable:hover th { background: var(--raised); }
.sheet tbody tr.current th, .sheet tbody tr.current td { background: var(--accent-soft); }

/* The paper sheet's cell: score on the left, bid over tricks in a split box on the right */
.cell {
  position: relative;
  height: 44px;
  padding: 0 34px 0 14px !important;
  text-align: left;
  vertical-align: middle;
}
.cell-total { font-size: 16px; font-weight: 500; }
.cell-box {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 26px;
  display: grid;
  grid-template-rows: 1fr 1fr;
  border-left: 1px solid var(--line);
  font-size: 12px;
  color: var(--text-2);
  text-align: center;
}
.cell-box .b { border-bottom: 1px solid var(--line); display: grid; place-items: center; }
.cell-box .a { display: grid; place-items: center; }
.cell.missed .cell-box .a { color: var(--neg); font-weight: 600; }
.cell.missed .cell-total { color: var(--text); }
.cell.dealt::before {
  content: '';
  position: absolute;
  left: 4px; top: calc(50% - 3px);
  width: 6px; height: 6px;
  border: 1px solid var(--muted);
  border-radius: 50%;
}
.sheet tfoot th, .sheet tfoot td {
  height: 44px;
  border-bottom: 0;
  border-top: 1px solid var(--line-strong);
  background: var(--raised);
}
.sheet tfoot th { font: 500 13px var(--sans); color: var(--text-2); }
.sheet .foot { padding: 0 8px; font-weight: 600; font-size: 17px; }
.sheet .foot.lead { color: var(--accent); box-shadow: inset 0 -2px 0 var(--accent); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .sheet .foot.lead { color: var(--text); box-shadow: inset 0 -2px 0 var(--text); } }
.sheet .foot.neg { color: var(--neg); }
.sheet-hint { text-align: center; }

@media (min-width: 600px) {
  .round-num { font-size: 36px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
