:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #293548;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --primary: #2563eb;
  --primary-hi: #3b82f6;
  --accent: #f59e0b;
  --danger: #dc2626;
  --ok: #10b981;
  --line: #334155;
  --radius: 10px;
  --p1: #60a5fa;
  --p2: #f472b6;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}
h1 { font-size: 1.4rem; margin: 0; }
h2 { font-size: 1rem; margin: 0 0 0.5rem; color: var(--muted); font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; }
h3 { margin: 0 0 0.5rem; }
button {
  font: inherit; color: inherit;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.65rem 1rem;
  cursor: pointer;
  min-height: 44px;
}
button:active { transform: scale(0.98); }
button.primary { background: var(--primary); border-color: var(--primary); }
button.primary:hover { background: var(--primary-hi); }
button.danger { background: var(--danger); border-color: var(--danger); }
input[type="text"], input[type="password"], input[type="number"] {
  font: inherit;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.7rem;
  width: 100%;
  min-height: 44px;
}
label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.85rem; color: var(--muted); }

.screen { padding: 1rem; max-width: 720px; margin: 0 auto; }
.screen header {
  display: flex; align-items: center; gap: 0.5rem; justify-content: space-between;
  padding: 0.25rem 0 1rem;
}
.user-chip { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; color: var(--muted); }
.user-chip button { padding: 0.35rem 0.6rem; min-height: 32px; font-size: 0.8rem; }

.login { max-width: 360px; padding-top: 3rem; }
.login form { display: flex; flex-direction: column; gap: 1rem; margin-top: 1.5rem; }
.err { color: var(--danger); min-height: 1em; margin: 0; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
  border: 1px solid var(--line);
}

.start-buttons { display: flex; flex-direction: column; gap: 0.5rem; }
.start-btn { width: 100%; background: var(--primary); border-color: var(--primary); }
.resume-btn { width: 100%; }
.cpu-tag {
  display: inline-block; font-size: 0.65rem; font-weight: 700;
  background: var(--danger); color: white;
  padding: 0.05rem 0.35rem; border-radius: 4px;
  letter-spacing: 0.05em; vertical-align: middle;
  margin-left: 0.25rem;
}

.pairing { padding: 0.5rem 0; border-bottom: 1px solid var(--line); }
.pairing:last-child { border-bottom: none; }
.pairing-head {
  display: flex; gap: 0.4rem; align-items: center;
  font-weight: 600; font-size: 0.95rem; margin-bottom: 0.25rem;
}
.pairing-vs { color: var(--muted); font-weight: 400; font-size: 0.8rem; }
.history-group { padding-top: 0.75rem; }
.history-group + .history-group { border-top: 2px solid var(--line); margin-top: 0.5rem; }
.history-group-head {
  display: flex; align-items: center; gap: 0.4rem;
  font-weight: 700; font-size: 1rem;
  text-transform: uppercase; letter-spacing: 0.04em;
  padding: 0.25rem 0 0.5rem;
  color: var(--text);
}
.history-group-count {
  margin-left: auto;
  background: var(--surface-2); color: var(--muted);
  font-size: 0.75rem; font-weight: 700;
  padding: 0.1rem 0.5rem; border-radius: 999px;
}
.pairing-row {
  display: flex; justify-content: space-between; gap: 0.5rem;
  font-variant-numeric: tabular-nums;
}

.stats-row, .history-row {
  display: flex; justify-content: space-between; gap: 0.5rem;
  padding: 0.5rem 0; border-bottom: 1px solid var(--line);
  align-items: center;
}
.stats-row:last-child, .history-row:last-child { border-bottom: none; }
.stats-name { font-weight: 600; }
.stats-meta, .history-meta { color: var(--muted); font-size: 0.85rem; }
.history-row.win strong { color: var(--accent); }

/* Scorecard */
#scorecard { display: flex; flex-direction: column; gap: 0; }
.sc-row {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) 1fr 1fr;
  align-items: center;
  border-bottom: 1px solid var(--line);
  min-height: 44px;
}
.sc-row.header {
  font-weight: 700; text-transform: uppercase; font-size: 0.75rem;
  color: var(--muted); background: transparent; border-bottom: 2px solid var(--line);
}
.sc-row.section {
  grid-template-columns: 1fr; padding: 0.5rem 0.25rem;
  color: var(--muted); font-size: 0.75rem; letter-spacing: 0.08em;
  text-transform: uppercase; border-bottom: 1px solid var(--line);
  background: transparent; font-weight: 700;
}
.sc-row.total, .sc-row.subtotal, .sc-row.bonus, .sc-row.grand {
  font-weight: 700; background: var(--surface);
}
.sc-row.grand { background: var(--surface-2); font-size: 1.05rem; }
.sc-label {
  padding: 0.5rem 0.5rem 0.5rem 0.25rem;
  font-size: 0.92rem;
}
.sc-label small { color: var(--muted); display: block; font-size: 0.75rem; }
.sc-cell {
  text-align: center;
  padding: 0.5rem;
  border-left: 1px solid var(--line);
  min-height: 44px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  user-select: none;
  font-variant-numeric: tabular-nums;
  font-size: 1rem;
}
.sc-cell.empty { color: var(--muted); }
.sc-cell.crossed { color: var(--danger); text-decoration: line-through; }
.sc-cell.computed { cursor: default; color: var(--text); background: var(--surface); }
.sc-cell.bonus-earned { color: var(--ok); font-weight: 700; }
.sc-cell.grand { font-weight: 800; }

.sc-row .player-col { text-align: center; font-size: 0.85rem; padding: 0.5rem; border-left: 1px solid var(--line); }
.sc-row.header .player-col.p1 { color: var(--p1); }
.sc-row.header .player-col.p2 { color: var(--p2); }

.actions {
  display: flex; gap: 0.5rem; margin-top: 1.2rem;
}
.actions button { flex: 1; }

.back {
  background: transparent; border: none; font-size: 1.4rem; padding: 0.2rem 0.5rem;
  min-height: auto;
}

/* Bottom sheet */
.sheet { position: fixed; inset: 0; z-index: 100; }
.sheet.hidden { display: none; }
.sheet-backdrop {
  position: absolute; inset: 0; background: rgba(0,0,0,0.5);
}
.sheet-body {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: var(--surface);
  padding: 1rem;
  padding-bottom: calc(1rem + env(safe-area-inset-bottom));
  border-top-left-radius: 16px; border-top-right-radius: 16px;
  display: flex; flex-direction: column; gap: 0.75rem;
  max-width: 720px; margin: 0 auto;
}
.hint { margin: 0; color: var(--muted); font-size: 0.85rem; }
.quick { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.quick button { flex: 1 0 4rem; }
.numeric { color: var(--muted); font-size: 0.85rem; }
.sheet-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.sheet-actions button { flex: 1; }

/* Dice tray */
.dice-toggle {
  background: transparent; border: 1px solid var(--line);
  font-size: 1.1rem; padding: 0.25rem 0.55rem; min-height: 36px;
  border-radius: var(--radius); opacity: 0.55;
}
.dice-toggle.on { opacity: 1; border-color: var(--primary); background: rgba(37,99,235,0.15); }
#correct-toggle.on { border-color: var(--accent); background: rgba(245,158,11,0.18); color: var(--accent); }
.correct-banner {
  background: rgba(245,158,11,0.15); color: var(--accent);
  border: 1px solid var(--accent); border-radius: var(--radius);
  padding: 0.5rem 0.75rem; margin-bottom: 0.5rem;
  font-size: 0.85rem; text-align: center; font-weight: 600;
}
.correct-banner.hidden { display: none; }

.dice-tray {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.75rem;
  margin-bottom: 0.75rem;
  display: flex; flex-direction: column; gap: 0.6rem;
}
.dice-tray.hidden { display: none; }
.dice-tray .turn-line {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 0.85rem; color: var(--muted);
}
.dice-tray .turn-line .whose { font-weight: 700; color: var(--text); }
.dice-tray .turn-line .whose.p1 { color: var(--p1); }
.dice-tray .turn-line .whose.p2 { color: var(--p2); }
.dice-row { display: flex; gap: 0.5rem; justify-content: space-between; }
.die {
  flex: 1; aspect-ratio: 1 / 1; max-width: 64px;
  background: var(--surface-2); border: 2px solid var(--line);
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.6rem; font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 0; min-height: 0;
}
.die.empty { color: var(--muted); font-size: 1.1rem; }
.die.held { background: var(--primary); border-color: var(--primary-hi); color: white; }
.die:disabled { cursor: default; opacity: 0.85; }
.dice-actions { display: flex; gap: 0.5rem; }
.dice-actions button { flex: 1; }
.dice-actions .roll { background: var(--primary); border-color: var(--primary); }
.dice-actions .roll:disabled { background: var(--surface-2); border-color: var(--line); opacity: 0.6; }

.cpu-msg {
  text-align: center;
  font-size: 0.95rem;
  font-style: italic;
  color: var(--accent);
  padding: 0.4rem 0;
  min-height: 1.4rem;
}

.sc-row.header .player-col.turn { box-shadow: inset 0 -3px 0 var(--accent); }

.dice-mini {
  display: flex; gap: 0.3rem; padding: 0.3rem 0;
  font-variant-numeric: tabular-nums;
}
.dice-mini .die-mini {
  width: 1.6rem; height: 1.6rem;
  border: 1px solid var(--line); border-radius: 6px;
  background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 0.85rem; font-weight: 700;
}
.dice-mini .die-mini.held { background: var(--primary); border-color: var(--primary-hi); color: white; }

@media (max-width: 420px) {
  .screen { padding: 0.75rem; }
  .sc-label { font-size: 0.85rem; }
  .die { font-size: 1.4rem; }
}
