:root {
  --bg: #f6f7f8;
  --surface: #ffffff;
  --surface-2: #eef1f3;
  --text: #15191d;
  --muted: #5d6873;
  --border: #dde2e6;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --good: #15803d;
  --fair: #b45309;
  --poor: #b91c1c;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 16px rgba(0, 0, 0, .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1113;
    --surface: #171b1e;
    --surface-2: #20262a;
    --text: #e8ecef;
    --muted: #94a0aa;
    --border: #2b3237;
    --accent: #2dd4bf;
    --accent-text: #04201d;
    --good: #4ade80;
    --fair: #fbbf24;
    --poor: #f87171;
    --warn-bg: #3b2a0a;
    --warn-text: #fcd34d;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1113; --surface: #171b1e; --surface-2: #20262a; --text: #e8ecef; --muted: #94a0aa;
  --border: #2b3237; --accent: #2dd4bf; --accent-text: #04201d; --good: #4ade80; --fair: #fbbf24;
  --poor: #f87171; --warn-bg: #3b2a0a; --warn-text: #fcd34d; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
main { width: 100%; max-width: 760px; margin: 0 auto; padding: 0 16px 24px; flex: 1; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  max-width: 760px; width: 100%; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 17px; }
.topbar-actions { display: flex; gap: 8px; align-items: center; min-width: 0; }
.topbar select { max-width: 46vw; }

select, input {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; width: 100%;
}
.topbar select { width: auto; }
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.tabs {
  display: flex; gap: 4px; max-width: 760px; width: calc(100% - 32px); margin: 0 auto 14px;
  background: var(--surface-2); padding: 4px; border-radius: 12px;
}
.tabs button {
  flex: 1; border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 550;
  padding: 8px 6px; border-radius: 9px; cursor: pointer;
}
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.view { display: none; }
.view.active { display: block; }

.btn {
  font: inherit; font-weight: 600; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 10px 16px; border-radius: 11px; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--poor); }
.btn.big { width: 100%; padding: 14px; font-size: 16px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: 0; background: transparent; color: var(--muted); padding: 6px; cursor: pointer; border-radius: 8px; }

.camera {
  position: relative; aspect-ratio: 3 / 4; max-height: 62dvh; width: 100%; margin: 0 auto;
  border-radius: var(--radius); overflow: hidden; background: #000;
}
@media (min-width: 640px) { .camera { aspect-ratio: 4 / 3; } }
.camera video { width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); }
.guide { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.guide.recording #guideRing { stroke: #2dd4bf; stroke-dasharray: none; stroke-width: .8; }
.camera-status { position: absolute; top: 10px; left: 10px; right: 10px; display: flex; gap: 6px; flex-wrap: wrap; }
.chip { background: rgba(0, 0, 0, .55); color: #fff; font-size: 13px; padding: 4px 10px; border-radius: 999px; }
.chip.bad { background: rgba(185, 28, 28, .85); }
.live-hr {
  position: absolute; top: 44px; right: 12px; color: #fff; font-size: 26px; font-weight: 700;
  font-variant-numeric: tabular-nums; text-shadow: 0 2px 8px rgba(0,0,0,.6);
}
.live-hr[hidden], .chip[hidden] { display: none; }
.countdown {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  color: #fff; font-size: 34px; font-weight: 700; font-variant-numeric: tabular-nums; text-shadow: 0 2px 8px rgba(0,0,0,.6);
}
.camera-empty {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; color: #fff; text-align: center; padding: 24px; background: #111;
}
.camera-empty[hidden] { display: none; }

.progress { height: 6px; background: var(--surface-2); border-radius: 99px; margin: 12px 0 0; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .3s linear; }
.hint { color: var(--muted); margin: 12px 0; }
.controls { display: grid; gap: 12px; }

.segmented { display: flex; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.segmented label { flex: 1; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; width: auto; }
.segmented span { display: block; text-align: center; padding: 8px; border-radius: 9px; cursor: pointer; color: var(--muted); font-weight: 550; }
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

.result-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow); min-width: 0;
}
.card.hero { grid-column: 1 / -1; }
.card h3 { margin: 0 0 4px; font-size: 13px; font-weight: 600; color: var(--muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.value { display: flex; align-items: baseline; gap: 6px; }
.value span { font-size: 34px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.card.hero .value span { font-size: 52px; }
.value small { color: var(--muted); font-size: 13px; }
.sub { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.tag { font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--muted); }
.tag.warn { background: var(--warn-bg); color: var(--warn-text); }
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); }
.badge.good { color: var(--good); }
.badge.fair { color: var(--fair); }
.badge.poor { color: var(--poor); }
#waveform { width: 100%; display: block; margin-top: 10px; }

.quality { margin-bottom: 12px; }
.quality .warning { background: var(--warn-bg); color: var(--warn-text); padding: 8px 12px; border-radius: 10px; margin-bottom: 6px; font-size: 14px; }
.actions { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; }
.actions .btn { flex: 1 1 200px; }

.history { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.history li button {
  width: 100%; text-align: left; font: inherit; color: inherit; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px;
  display: flex; justify-content: space-between; gap: 12px; align-items: center;
}
.history .when { color: var(--muted); font-size: 13px; }
.history .vals { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.stats { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 14px; }
.stats th, .stats td { padding: 10px 12px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
.stats th:first-child, .stats td:first-child { text-align: left; }
.stats thead th { color: var(--muted); font-weight: 600; font-size: 12px; }
.stats tr:last-child td { border-bottom: 0; }
.better { color: var(--good); font-weight: 650; }
.worse { color: var(--poor); font-weight: 650; }

dialog {
  border: 1px solid var(--border); border-radius: 16px; background: var(--surface); color: var(--text);
  padding: 20px; width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin: 0 0 8px; font-size: 18px; }
dialog form { display: grid; gap: 10px; }
dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.dialog-actions.spread { justify-content: space-between; flex-wrap: wrap; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.disclaimer { text-align: center; color: var(--muted); font-size: 12px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); }
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; max-width: calc(100vw - 32px); z-index: 10;
}
.toast[hidden] { display: none; }
@media (max-width: 380px) { .cards { grid-template-columns: 1fr; } }

.devices { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 10px; }
.devices .btn { flex: 1 1 160px; }
.devices .btn.on { border-color: var(--accent); color: var(--accent); }
.auto-cal { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.auto-cal[hidden] { display: none; }
