:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #646b78;
  --line: #dfe2e8;
  --accent: #e8590c;
  --accent-text: #ffffff;
  --left: #1c7ed6;
  --right: #d6336c;
  --both: #7048e8;
  --good: #2b8a3e;
  --chart-grid: #e6e8ee;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111317;
    --surface: #1b1e24;
    --text: #eef0f4;
    --muted: #9aa1ad;
    --line: #2c3139;
    --accent: #ff7a2e;
    --accent-text: #16181d;
    --left: #4dabf7;
    --right: #f06595;
    --both: #9775fa;
    --good: #51cf66;
    --chart-grid: #262a31;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 2;
}
.brand { font-weight: 700; letter-spacing: .02em; }
.status { display: flex; gap: 6px; align-items: center; min-width: 0; }
.chip {
  font-size: 13px; padding: 4px 10px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--bg); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; max-width: 45vw;
}
.chip-btn { cursor: pointer; }
.chip-btn.connected { border-color: var(--good); color: var(--good); }

main { flex: 1; width: 100%; max-width: 560px; margin: 0 auto; padding: 16px 16px 96px; }
.view { display: none; }
.view.active { display: block; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding: 4px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.seg button { border: 0; background: transparent; padding: 10px 0; border-radius: 9px; cursor: pointer; font-weight: 600; color: var(--muted); }
.seg button[aria-checked="true"], .seg button[aria-pressed="true"] { background: var(--bg); color: var(--text); box-shadow: inset 0 0 0 1px var(--line); }
.seg button[data-hand="left"][aria-checked="true"] { color: var(--left); }
.seg button[data-hand="right"][aria-checked="true"] { color: var(--right); }
.seg button[data-hand="both"][aria-checked="true"] { color: var(--both); }
.seg-small button { padding: 6px 0; font-size: 14px; }

.gauge { text-align: center; padding: 20px 0 8px; }
.live { font-variant-numeric: tabular-nums; font-weight: 700; font-size: clamp(64px, 22vw, 112px); line-height: 1; }
.live small { font-size: .28em; font-weight: 600; color: var(--muted); margin-left: 6px; }
.gauge-sub { display: flex; justify-content: center; gap: 18px; color: var(--muted); margin-top: 8px; font-variant-numeric: tabular-nums; flex-wrap: wrap; }
.gauge-sub b { color: var(--text); }

canvas#chart { width: 100%; height: 160px; display: block; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; margin: 12px 0; }

.row { display: flex; gap: 10px; margin: 10px 0; }
.row > * { flex: 1; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; text-align: center;
  padding: 12px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 600; min-height: 48px;
}
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-big { flex: 2; font-size: 18px; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.stop { background: var(--text); color: var(--bg); border-color: var(--text); }

.check { display: flex; gap: 8px; align-items: center; color: var(--muted); font-size: 14px; margin: 8px 0; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 16px; margin: 16px 0; }
.card h2 { font-size: 17px; margin: 0 0 8px; }
.card-head { display: flex; align-items: center; justify-content: space-between; }
.badge { background: var(--good); color: #fff; font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 999px; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 8px 0; }
.stats div { display: flex; flex-direction: column; min-width: 0; }
.stats .k { font-size: 12px; color: var(--muted); }
.stats .v { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stats div:last-child { grid-column: 1 / -1; }
.stats input { width: 100%; font-size: 15px; font-weight: 400; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }

.form label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); margin-bottom: 12px; }
.form label.check { flex-direction: row; }
.form input:not([type="checkbox"]) { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); font-size: 16px; }
.hint { color: var(--muted); font-size: 14px; margin: 6px 0; }

.bests { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.best { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.best .k { font-size: 12px; color: var(--muted); }
.best .v { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.best.left .v { color: var(--left); } .best.right .v { color: var(--right); } .best.both .v { color: var(--both); }
.asym { grid-column: 1 / -1; text-align: center; color: var(--muted); font-size: 14px; }

.list { list-style: none; padding: 0; margin: 12px 0; }
.item { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; margin-bottom: 8px; overflow: hidden; }
.item-main { display: flex; align-items: center; gap: 12px; padding: 12px; cursor: pointer; }
.hand { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 13px; color: #fff; flex: none; }
.hand.left { background: var(--left); } .hand.right { background: var(--right); } .hand.both { background: var(--both); }
.item-body { flex: 1; min-width: 0; }
.item-title { font-weight: 700; font-variant-numeric: tabular-nums; }
.item-sub { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-detail { padding: 0 12px 12px; }
.item-detail canvas { width: 100%; height: 120px; display: block; }
.item-detail .row { margin-bottom: 0; }
.empty { text-align: center; color: var(--muted); margin-top: 32px; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 3;
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button { border: 0; background: none; padding: 14px 0; font-weight: 600; color: var(--muted); cursor: pointer; }
.tabs button[aria-current="page"] { color: var(--accent); }

.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(72px + env(safe-area-inset-bottom)); z-index: 4;
  max-width: 528px; margin: 0 auto; background: var(--text); color: var(--bg);
  padding: 12px 14px; border-radius: 12px; font-size: 14px;
}
