:root {
  color-scheme: light;
  --accent: #1d4ed8;
  --accent-strong: #1e40af;
  --accent-soft: #dbeafe;
  --accent-tint: #eff6ff;
  --ink: #111827;
  --ink-2: #374151;
  --muted: #6b7280;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --up: #15803d;
  --up-soft: #dcfce7;
  --down: #b91c1c;
  --down-soft: #fee2e2;
  --gold: #b45309;
  --gold-soft: #fef3c7;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(17, 24, 39, 0.06), 0 1px 3px rgba(17, 24, 39, 0.08);
  --shadow-lg: 0 -8px 32px rgba(17, 24, 39, 0.18);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --fab-h: 84px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
  padding-bottom: calc(var(--fab-h) + env(safe-area-inset-bottom));
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
button { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.tnum { font-variant-numeric: tabular-nums; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 12px 16px 10px;
}
.top-row { display: flex; align-items: center; gap: 12px; }
.top-row h1 { font-size: 20px; font-weight: 800; letter-spacing: -0.01em; flex: 1; min-width: 0; line-height: 1.15; }

.tabs {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
  margin-top: 10px; padding: 4px; background: var(--bg); border-radius: 10px;
}
.tab {
  border: 0; background: transparent; border-radius: 8px; padding: 0; height: 40px;
  font-weight: 600; font-size: 15px; color: var(--muted); cursor: pointer;
}
.tab[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

/* ---------- main ---------- */
main { max-width: 720px; margin: 0 auto; padding: 12px 16px 24px; }
.view[hidden] { display: none; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.state-box {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 32px 20px; text-align: center; color: var(--muted);
}
.state-box strong { display: block; color: var(--ink); font-size: 18px; margin-bottom: 6px; }
.state-box .btn { margin-top: 16px; }

/* ---------- stat tiles ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; min-width: 0; }
.tile .label { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.tile .value { font-size: 20px; font-weight: 800; letter-spacing: -0.01em; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .value small { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.tile .sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.tile.accent .label, .tile.accent .sub, .tile.accent .value small { color: rgba(255, 255, 255, 0.85); }

/* ---------- leaderboard table ---------- */
.board { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.board-head {
  display: grid; grid-template-columns: 52px 1fr auto; gap: 8px; padding: 8px 12px; font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); border-bottom: 1px solid var(--line); background: var(--bg);
}
.row {
  display: grid; grid-template-columns: 52px 1fr auto; gap: 8px; align-items: center;
  padding: 10px 12px; border-bottom: 1px solid var(--line); min-height: 60px; position: relative;
}
.row:last-child { border-bottom: 0; }
.row.is-record { background: var(--accent-tint); box-shadow: inset 4px 0 0 var(--accent); }
.rank { display: flex; flex-direction: column; align-items: flex-start; line-height: 1.1; }
.rank-n { font-size: 20px; font-weight: 800; color: var(--ink-2); }
.is-record .rank-n { color: var(--accent); }
.delta { font-size: 11px; font-weight: 700; margin-top: 2px; white-space: nowrap; }
.delta.up { color: var(--up); }
.delta.down { color: var(--down); }
.delta.same { color: var(--muted); }
.delta.new { color: var(--accent); }
.who { min-width: 0; }
.name-btn {
  background: none; border: 0; padding: 0; font-size: 17px; font-weight: 700; color: var(--ink); cursor: pointer;
  text-align: left; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block;
  min-height: 24px; text-decoration: none;
}
.name-btn:hover { color: var(--accent); text-decoration: underline; }
.meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta .tag { color: var(--accent); font-weight: 700; }
.result { text-align: right; line-height: 1.1; }
.score { font-size: 24px; font-weight: 800; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.is-record .score { color: var(--accent); }
.time { font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; font-weight: 600; }
.time.none { color: var(--muted); font-weight: 400; }
.row.flash { animation: flash 2.4s ease-out; }
@keyframes flash { 0% { background: #fde68a; } 60% { background: #fde68a; } 100% { background: transparent; } }
.row.is-record.flash { animation: flash-record 2.4s ease-out; }
@keyframes flash-record { 0% { background: #fde68a; } 60% { background: #fde68a; } 100% { background: var(--accent-tint); } }

/* ---------- banner ---------- */
.banner {
  display: none; border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px; border: 1px solid var(--line);
  background: var(--surface); box-shadow: var(--shadow); position: relative; overflow: hidden;
  animation: slide-in 0.3s ease-out;
}
.banner.show { display: flex; gap: 12px; align-items: flex-start; }
.banner .b-title { font-weight: 800; font-size: 16px; }
.banner .b-sub { font-size: 14px; color: var(--ink-2); margin-top: 2px; }
.banner .b-body { flex: 1; min-width: 0; }
.banner .b-close { background: none; border: 0; width: 36px; height: 36px; margin: -6px -8px -6px 0; border-radius: 50%; cursor: pointer; color: var(--muted); font-size: 20px; }
.banner.record { background: var(--accent); border-color: var(--accent); color: #fff; }
.banner.record .b-sub, .banner.record .b-close { color: rgba(255, 255, 255, 0.9); }
.banner.pb { background: var(--accent-tint); border-color: var(--accent-soft); }
.banner.pb .b-title { color: var(--accent-strong); }
.banner.error { background: var(--down-soft); border-color: #fecaca; }
.banner.error .b-title { color: var(--down); }
@keyframes slide-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }
.burst { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.burst i {
  position: absolute; left: 50%; top: 50%; width: 8px; height: 8px; border-radius: 2px; background: var(--c);
  animation: burst 1s ease-out forwards; opacity: 0;
}
@keyframes burst {
  0% { transform: translate(0, 0) rotate(0); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) rotate(540deg); opacity: 0; }
}

/* ---------- fab bar ---------- */
.fab-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, rgba(243, 244, 246, 0));
  pointer-events: none;
}
.fab-bar .btn { pointer-events: auto; max-width: 688px; margin: 0 auto; }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%; min-height: 52px;
  border-radius: var(--radius); border: 1px solid transparent; font-size: 17px; font-weight: 700; cursor: pointer;
  padding: 0 16px; text-decoration: none;
}
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 4px 14px rgba(29, 78, 216, 0.35); }
.btn-primary:hover { background: var(--accent-strong); }
.btn-primary:disabled { opacity: 0.6; cursor: wait; }
.btn-secondary { background: var(--surface); color: var(--ink); border-color: var(--line-strong); }
.btn-secondary:hover { background: var(--bg); }
.btn-sm { min-height: 40px; font-size: 14px; width: auto; padding: 0 14px; border-radius: 10px; }
.btn-link { background: none; border: 0; color: var(--accent); font-weight: 600; padding: 8px; cursor: pointer; min-height: 44px; }

/* ---------- sheets ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; display: none; }
.sheet.open { display: block; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(17, 24, 39, 0.45); animation: fade 0.2s; }
@keyframes fade { from { opacity: 0; } }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 92vh; max-height: 92dvh;
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--surface); border-radius: 20px 20px 0 0; box-shadow: var(--shadow-lg);
  animation: rise 0.28s cubic-bezier(0.2, 0.8, 0.2, 1); transition: transform 0.2s ease-out;
  max-width: 720px; margin: 0 auto; will-change: transform;
}
.sheet-panel.dragging { transition: none; animation: none; }
.sheet-panel.dismissing { transition: transform 0.18s ease-in; }
@keyframes rise { from { transform: translateY(40px); opacity: 0.6; } }
/* Handle = drag area (grabber + header). The body scrolls on its own and is never translated by a drag. */
.sheet-handle { flex: none; padding: 8px 16px 0; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
.sheet-panel.dragging .sheet-handle { cursor: grabbing; }
.sheet-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding: 0 16px calc(20px + env(safe-area-inset-bottom)); margin: 0; }
.grabber { width: 40px; height: 4px; border-radius: 2px; background: var(--line-strong); margin: 4px auto 12px; }
.sheet-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.sheet-head h2 { font-size: 20px; font-weight: 800; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-close { background: var(--bg); border: 0; width: 40px; height: 40px; border-radius: 50%; cursor: pointer; font-size: 22px; color: var(--ink-2); flex: none; }

/* ---------- form ---------- */
.field { margin-bottom: 14px; }
.field label, .field .lbl { display: block; font-size: 13px; font-weight: 700; color: var(--ink-2); margin-bottom: 6px; }
.field .help { font-size: 12px; color: var(--muted); margin-top: 4px; }
.field .err { display: none; font-size: 13px; color: var(--down); font-weight: 600; margin-top: 4px; }
.field.invalid .err { display: block; }
.field.invalid input { border-color: var(--down); }
input[type="text"], input[type="number"], input[type="datetime-local"], select, textarea {
  width: 100%; font: inherit; font-size: 16px; padding: 10px 12px; border: 1px solid var(--line-strong); border-radius: 10px;
  background: var(--surface); color: var(--ink); min-height: 46px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.score-input { font-size: 34px !important; font-weight: 800; text-align: center; min-height: 64px !important; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.counter { float: right; font-weight: 400; color: var(--muted); }
.form-err { display: none; background: var(--down-soft); color: var(--down); border-radius: 10px; padding: 10px 12px; font-size: 14px; font-weight: 600; margin-bottom: 12px; }
.form-err.show { display: block; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 999px; background: var(--bg); color: var(--ink-2); border: 1px solid var(--line); white-space: nowrap; }
.chip.accent { background: var(--accent-soft); color: var(--accent-strong); border-color: transparent; }
.chip.gold { background: var(--gold-soft); color: var(--gold); border-color: transparent; }
.chip.up { background: var(--up-soft); color: var(--up); border-color: transparent; }

/* ---------- contender sheet ---------- */
.c-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.c-stats .tile .value { font-size: 22px; }
.gap { background: var(--bg); border-radius: 10px; padding: 10px 12px; font-size: 14px; margin-bottom: 12px; color: var(--ink-2); }
.gap strong { color: var(--ink); }
.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 16px 0 8px; }
.history { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.history li { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line); min-height: 48px; }
.history li:last-child { border-bottom: 0; }
.history .h-main { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; min-width: 0; }
.history .h-score { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; min-width: 40px; }
.history .h-note { font-size: 13px; color: var(--muted); width: 100%; }
.history .h-when { font-size: 12px; color: var(--muted); text-align: right; white-space: nowrap; }
.history li.pb-row { background: var(--accent-tint); }
.c-actions { display: flex; gap: 8px; margin-top: 16px; }
.c-actions .btn { flex: 1; }
.sparkline { width: 100%; height: 56px; display: block; margin-bottom: 4px; }

/* ---------- activity ---------- */
.feed { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.feed .day { padding: 6px 12px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); background: var(--bg); border-bottom: 1px solid var(--line); border-top: 1px solid var(--line); }
.feed .day:first-child { border-top: 0; }
.act { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line); min-height: 56px; }
.act:last-child { border-bottom: 0; }
.act.is-record-run { background: var(--accent-tint); box-shadow: inset 4px 0 0 var(--accent); }
.act .a-line { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.act .a-note { font-size: 13px; color: var(--muted); margin-top: 2px; overflow-wrap: anywhere; }
.act .a-right { text-align: right; line-height: 1.1; }
.act .a-score { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.act .a-when { font-size: 12px; color: var(--muted); white-space: nowrap; }
.act .name-btn { font-size: 16px; display: inline; }

/* ---------- timeline ---------- */
.chart-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.chart-wrap { position: relative; }
.chart-scroll { overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
.chart-scroll svg { display: block; }
.chart-yaxis { position: absolute; left: 0; top: 0; pointer-events: none; }
.chart-yaxis rect.bg { fill: var(--surface); }
.chart-detail { padding: 10px 12px; border-top: 1px solid var(--line); font-size: 14px; min-height: 48px; display: flex; align-items: center; gap: 10px; }
.chart-detail .d-dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.chart-detail .muted { color: var(--muted); }
.chart-detail .name-btn { display: inline; font-size: 15px; }
.chart-toolbar { display: flex; gap: 8px; align-items: center; justify-content: space-between; padding: 8px 12px; border-bottom: 1px solid var(--line); font-size: 13px; color: var(--muted); }
svg text { font-family: var(--font); }
.ln { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.ln.leader { stroke-width: 3.5; }
.ln.dim, .dot.dim, .lbl.dim { opacity: 0.15; }
.dot { cursor: pointer; }
.dot.faint { opacity: 0.35; }
.dot.active { stroke: var(--ink); stroke-width: 2; }
.lbl { font-size: 12px; font-weight: 600; pointer-events: none; }
.lbl.leader { font-weight: 800; fill: var(--accent); }
.grid { stroke: var(--line); stroke-width: 1; }
.axis-text { font-size: 11px; fill: var(--muted); }
.now-line { stroke: var(--muted); stroke-dasharray: 3 3; }
.legend { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 12px; border-top: 1px solid var(--line); }
.legend .chip { cursor: pointer; }
.legend .chip i { width: 10px; height: 10px; border-radius: 50%; background: var(--c); display: inline-block; }


@media (min-width: 600px) {
  .top { padding: 14px 24px 12px; }
  main { padding: 16px 24px 32px; }
  .tile .value { font-size: 22px; }
  .row { padding: 12px 16px; grid-template-columns: 64px 1fr auto; }
  .board-head { grid-template-columns: 64px 1fr auto; padding: 8px 16px; }
  .score { font-size: 26px; }
}
@media print { .fab-bar, .tabs { display: none; } }
