:root {
  color-scheme: light;
  --bg: #f4f3ef;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e3e2dc;
  --grid: #e8e7e2;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --p1: #2a78d6;
  --p2: #eb6834;
  --p1-soft: #e3eefb;
  --p2-soft: #fde9e0;
  --accent: #0b0b0b;
  --accent-ink: #ffffff;
  --danger: #c62828;
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #2e2e2b;
    --grid: #2a2a27;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --p1: #3987e5;
    --p2: #d95926;
    --p1-soft: #1b2a3d;
    --p2-soft: #3a2218;
    --accent: #ffffff;
    --accent-ink: #0b0b0b;
    --danger: #ef6b6b;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110;
  --surface: #1a1a19;
  --surface-2: #242422;
  --border: #2e2e2b;
  --grid: #2a2a27;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --text-3: #8f8e86;
  --p1: #3987e5;
  --p2: #d95926;
  --p1-soft: #1b2a3d;
  --p2-soft: #3a2218;
  --accent: #ffffff;
  --accent-ink: #0b0b0b;
  --danger: #ef6b6b;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
h1, h2 { margin: 0; }
h2 { font-size: 15px; font-weight: 650; margin-bottom: 12px; }
button, input { font: inherit; color: inherit; }
.muted { color: var(--text-2); }
.small { font-size: 13px; font-weight: 400; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- layout */
.topbar {
  position: sticky; top: 0; z-index: 5;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.topbar h1 { font-size: 20px; font-weight: 700; }
.content {
  max-width: 640px; margin: 0 auto;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 96px);
}
.tab { display: flex; flex-direction: column; gap: 12px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.stack { display: flex; flex-direction: column; gap: 12px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 10px; border: 0; background: none;
  font-size: 12px; color: var(--text-3); cursor: pointer;
}
.tabbar button span { font-size: 20px; filter: grayscale(1); opacity: .7; }
.tabbar button.active { color: var(--text); font-weight: 600; }
.tabbar button.active span { filter: none; opacity: 1; }

/* ---------- controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 0 16px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface-2); cursor: pointer; font-weight: 550;
}
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-ghost { background: none; border: 0; color: var(--text-2); width: 100%; }
.btn:active { transform: scale(.98); }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--text-2); }
.field > span { display: flex; align-items: center; gap: 6px; }
.field input, .inline-form input {
  min-height: 44px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--bg); color: var(--text); font-size: 16px;
}
.inline-form { display: flex; gap: 8px; margin-top: 12px; }
.inline-form input { flex: 1; min-width: 0; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 40px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface-2); cursor: pointer; font-size: 15px;
}
.chip[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.empty { color: var(--text-3); font-size: 14px; margin: 0; }

.winner-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn-winner {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  min-height: 96px; padding: 12px;
  border: 2px solid transparent; border-radius: 14px; cursor: pointer;
  font-size: 17px; font-weight: 650; color: var(--text);
  word-break: break-word;
}
.btn-winner.p1 { background: var(--p1-soft); border-color: var(--p1); }
.btn-winner.p2 { background: var(--p2-soft); border-color: var(--p2); }
.btn-winner:disabled { opacity: .4; cursor: not-allowed; }
.btn-winner:active:not(:disabled) { transform: scale(.97); }
.btn-winner .dot { width: 14px; height: 14px; }
.date-field { margin-top: 14px; }

.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; flex: none; }
.dot.p1, .p1 > .dot { background: var(--p1); }
.dot.p2, .p2 > .dot { background: var(--p2); }

.segmented {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px;
  padding: 4px; background: var(--surface-2); border-radius: 12px;
}
.segmented button {
  min-height: 36px; border: 0; border-radius: 9px; background: none;
  font-size: 13px; color: var(--text-2); cursor: pointer;
}
.segmented button.active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgb(0 0 0 / .12); }

/* ---------- score & split bar */
.scoreline { display: grid; grid-template-columns: 1fr auto 1fr; align-items: end; gap: 8px; }
.scoreline .side { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.scoreline .side.right { align-items: flex-end; text-align: right; }
.scoreline .who { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text-2); overflow-wrap: anywhere; }
.scoreline .big { font-size: 44px; font-weight: 750; line-height: 1; letter-spacing: -0.02em; }
.scoreline .vs { font-size: 20px; color: var(--text-3); padding-bottom: 6px; }
.scoreline .pct { font-size: 13px; color: var(--text-3); }

.split { display: flex; gap: 2px; height: 10px; margin-top: 14px; }
.split.thin { height: 8px; margin-top: 8px; }
.split > div { min-width: 0; border-radius: 4px; }
.split .s1 { background: var(--p1); }
.split .s2 { background: var(--p2); }
.split .s0 { background: var(--grid); flex: 1; }

.verdict { margin: 14px 0 0; font-size: 15px; }
.facts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 14px; }
.fact { background: var(--surface-2); border-radius: 12px; padding: 10px; }
.fact .v { font-size: 20px; font-weight: 700; }
.fact .l { font-size: 12px; color: var(--text-2); }
.fact .v .duo { display: inline-flex; align-items: center; gap: 4px; margin-right: 10px; }
.fact .v .duo:last-child { margin-right: 0; }
.fact .v .duo .dot { width: 8px; height: 8px; }

/* ---------- per game */
.game-row { padding: 12px 0; border-top: 1px solid var(--border); }
.game-row:first-child { border-top: 0; padding-top: 0; }
.game-row .top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.game-row .name { font-weight: 600; overflow-wrap: anywhere; }
.game-row .count { font-size: 13px; color: var(--text-3); white-space: nowrap; }
.game-row .nums { display: flex; justify-content: space-between; font-size: 13px; color: var(--text-2); margin-top: 4px; }
.game-row .nums span { display: inline-flex; align-items: center; gap: 5px; }

/* ---------- chart */
.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 13px; color: var(--text-2); margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; }
.legend i.p1 { background: var(--p1); }
.legend i.p2 { background: var(--p2); }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .axis-label { fill: var(--text-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .bar1 { fill: var(--p1); }
.chart .bar2 { fill: var(--p2); }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .hit.on { fill: var(--text); fill-opacity: .05; }
.tooltip {
  position: fixed; z-index: 20; pointer-events: none;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 6px 20px rgb(0 0 0 / .15);
  padding: 8px 10px; font-size: 13px; min-width: 130px;
}
.tooltip .t { font-weight: 600; margin-bottom: 4px; }
.tooltip .r { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.tooltip .r span { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); }

/* ---------- history */
.history { list-style: none; margin: 0; padding: 0; }
.history li {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-top: 1px solid var(--border);
}
.history li:first-child { border-top: 0; }
.history .main { flex: 1; min-width: 0; }
.history .g { font-weight: 600; overflow-wrap: anywhere; }
.history .meta { font-size: 13px; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.history .del {
  width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 10px; background: none; color: var(--text-3); cursor: pointer; font-size: 18px;
}
.history .del:active { background: var(--surface-2); color: var(--danger); }

/* ---------- login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; text-align: center; }
.login-card .field { text-align: left; }
.logo { font-size: 44px; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

/* ---------- toast */
.toast {
  position: fixed; left: 16px; right: 16px; z-index: 30;
  bottom: calc(env(safe-area-inset-bottom) + 76px);
  max-width: 480px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--accent); color: var(--accent-ink);
  border-radius: 12px; padding: 12px 14px;
  box-shadow: 0 8px 24px rgb(0 0 0 / .2);
  font-size: 15px;
}
.toast button {
  border: 0; background: none; color: var(--accent-ink);
  font-weight: 700; text-decoration: underline; cursor: pointer; padding: 4px;
}
