:root {
  --bg: #f6f6f4;
  --surface: #fcfcfb;
  --ink: #1b1c1e;
  --ink-2: #4a4d52;
  --muted: #7b7f86;
  --line: #e3e3df;
  --grid: #ececea;
  --accent: #2f6fde;
  --accent-ink: #ffffff;
  --bar: #2f6fde;
  --danger: #c8372d;
  --stop: #c8372d;
  --ok: #1f8a4c;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121211;
    --surface: #1a1a19;
    --ink: #f0f0ee;
    --ink-2: #c3c4c6;
    --muted: #8d9096;
    --line: #2c2c2a;
    --grid: #262624;
    --accent: #3b7ff0;
    --bar: #3b7ff0;
    --danger: #e25b50;
    --stop: #d9463b;
    --ok: #3fb26f;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121211; --surface: #1a1a19; --ink: #f0f0ee; --ink-2: #c3c4c6; --muted: #8d9096;
  --line: #2c2c2a; --grid: #262624; --accent: #3b7ff0; --bar: #3b7ff0; --danger: #e25b50;
  --stop: #d9463b; --ok: #3fb26f; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; }
h2 { font-size: 1rem; font-weight: 650; }
.muted { color: var(--muted); font-weight: 400; }
.error { color: var(--danger); margin: 8px 0 0; }

main { max-width: 560px; margin: 0 auto; padding: 0 16px; }

.topbar { display: flex; align-items: center; gap: 8px; padding: 14px 0 10px; }
.topbar h1 { font-size: 1.15rem; flex: 1; letter-spacing: -0.01em; }
.icon-btn { background: none; border: 0; color: var(--ink-2); padding: 8px; border-radius: 10px; cursor: pointer; }
.icon-btn:active { background: var(--line); }
.sync-badge { font-size: 0.75rem; color: var(--muted); }
.sync-badge.pending { color: var(--danger); }

.segmented { display: flex; background: var(--line); padding: 4px; border-radius: 12px; gap: 4px; }
.segmented button {
  flex: 1; border: 0; background: transparent; color: var(--ink-2); font: inherit; font-weight: 600;
  padding: 10px 0; border-radius: 9px; cursor: pointer;
}
.segmented button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.segmented button:disabled { opacity: .5; }

.stage { margin-top: 12px; }
.guide {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; color: var(--ink-2); font-size: 0.94rem;
}
.guide strong { color: var(--ink); }
.guide ul { margin: 6px 0 0; padding-left: 1.1em; }
.stage.recording .guide { display: none; }

.cam-wrap {
  position: relative; border-radius: var(--radius); overflow: hidden; background: #000;
  aspect-ratio: 3 / 4; max-height: 62vh; margin: 0 auto;
}
.cam-wrap video, .cam-wrap canvas {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1);
}
.live {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  padding: 16px; pointer-events: none; color: #fff; text-shadow: 0 2px 10px rgba(0,0,0,.6);
}
.live-count { font-size: clamp(96px, 34vw, 190px); font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.live-count.bump { animation: bump .25s ease-out; }
@keyframes bump { from { transform: scale(1.18); } to { transform: scale(1); } }
.live-status { font-size: 1.05rem; font-weight: 600; margin-top: 6px; background: rgba(0,0,0,.45); padding: 4px 12px; border-radius: 999px; text-shadow: none; }
.live-cadence { font-size: .9rem; margin-top: 6px; min-height: 1.2em; }
.meter { position: relative; width: 70%; height: 8px; border-radius: 4px; background: rgba(255,255,255,.25); margin-top: 10px; }
.meter-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: #fff; border-radius: 4px; }
.meter-mark { position: absolute; top: -4px; bottom: -4px; width: 2px; background: #ffd34d; }
.meter-mark.up { background: rgba(255,255,255,.7); }

.stats { margin-top: 18px; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; }
.tile-label { font-size: .82rem; color: var(--muted); }
.tile-value { font-size: 1.9rem; font-weight: 750; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }

.chart-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 20px; flex-wrap: wrap; }
.chips { display: flex; gap: 6px; }
.chips button {
  border: 1px solid var(--line); background: var(--surface); color: var(--ink-2); font: inherit; font-size: .85rem;
  padding: 4px 12px; border-radius: 999px; cursor: pointer;
}
.chips button[aria-selected="true"] { border-color: var(--ink); color: var(--ink); font-weight: 600; }
.chart-readout { font-size: .85rem; color: var(--muted); margin: 8px 0 4px; min-height: 1.3em; }
.chart-readout b { color: var(--ink); }
.chart { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 6px 4px; }
.chart svg { display: block; width: 100%; height: auto; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis-text { fill: var(--muted); font-size: 10px; }
.chart .bar { fill: var(--bar); }
.chart .bar.dim { opacity: .35; }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .today-dot { fill: var(--ink-2); }
.table-view { margin-top: 8px; font-size: .85rem; color: var(--ink-2); }
.table-view table { width: 100%; border-collapse: collapse; margin-top: 6px; }
.table-view th, .table-view td { text-align: left; padding: 3px 4px; border-bottom: 1px solid var(--line); }

.recent-title { margin-top: 22px; }
.recent { list-style: none; padding: 0; margin: 8px 0 0; }
.recent li {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; margin-bottom: 6px; cursor: pointer;
}
.recent .r-name { font-weight: 600; min-width: 4.2em; }
.recent .r-meta { flex: 1; color: var(--muted); font-size: .85rem; }
.recent .r-reps { font-weight: 750; font-variant-numeric: tabular-nums; }
.recent .r-pending { font-size: .72rem; color: var(--danger); }
.recent .empty { justify-content: center; color: var(--muted); cursor: default; }

.bottom-spacer { height: 110px; }
.bottom-bar {
  position: fixed; left: 0; right: 0; bottom: 0; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
  display: flex; justify-content: center;
}
.btn {
  font: inherit; font-weight: 650; border-radius: 12px; border: 1px solid transparent; padding: 10px 16px; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; color: var(--ink-2); border-color: var(--line); }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--danger); margin-right: auto; }
.btn.main {
  width: 100%; max-width: 528px; font-size: 1.35rem; padding: 18px; border-radius: 18px;
  background: var(--accent); color: #fff; box-shadow: 0 6px 18px rgba(0,0,0,.18);
}
.btn.main.stop { background: var(--stop); }
.btn.main:disabled { opacity: .6; }

dialog { border: 0; border-radius: 18px; padding: 0; background: var(--surface); color: var(--ink); width: min(92vw, 420px); }
dialog::backdrop { background: rgba(0,0,0,.45); }
.dlg { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.dlg label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
.dlg label.check { flex-direction: row; align-items: center; gap: 8px; }
.dlg select, .dlg input[type=number], .pin-card input {
  font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink);
}
.dlg small { font-weight: 400; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

.pin-screen { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.pin-card { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 24px; width: min(100%, 380px); }
.pin-card h1 { font-size: 1.25rem; margin-bottom: 8px; }
.pin-card p { color: var(--ink-2); margin: 0 0 14px; }
.pin-card form { display: flex; gap: 8px; }
.pin-card input { flex: 1; min-width: 0; }

.toast {
  position: fixed; left: 50%; bottom: 110px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: .9rem;
  transition: opacity .2s, transform .2s; pointer-events: none; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (forced-colors: active) {
  .chart .bar { fill: CanvasText; }
}
