:root {
  --bg: #0f1115;
  --panel: #171a21;
  --line: #262b36;
  --text: #e8eaef;
  --muted: #8b93a5;
  --accent: #ef4444;
  --good: #22c55e;
  --fair: #eab308;
  --bad: #ef4444;
  --forehead: #3b82f6;
  --left: #f97316;
  --right: #22c55e;
  --full: #a855f7;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; background: var(--bg); color: var(--text); }

body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  max-width: 560px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 16px calc(16px + env(safe-area-inset-bottom));
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
}

h1 { font-size: 18px; margin: 0; letter-spacing: .2px; }

.seg { display: inline-flex; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.seg button {
  border: 0; background: transparent; color: var(--muted);
  font: inherit; padding: 6px 14px; border-radius: 7px; cursor: pointer;
}
.seg button.on { background: var(--line); color: var(--text); }

.stage {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  max-height: 52vh;
  margin: 0 auto;
  background: #000;
  border-radius: 14px;
  overflow: hidden;
}
.stage video, .stage canvas#overlay {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain;
}
.stage.mirror video, .stage.mirror canvas#overlay { transform: scaleX(-1); }

.guide {
  position: absolute;
  left: 22%; right: 22%; top: 12%; bottom: 20%;
  border: 2px dashed rgba(255, 255, 255, .35);
  border-radius: 48% / 42%;
  pointer-events: none;
}
.stage.finger .guide { left: 30%; right: 30%; top: 32%; bottom: 32%; border-radius: 50%; }
.stage.running .guide { border-color: rgba(255, 255, 255, .18); }

.placeholder {
  position: absolute; inset: 0; display: grid; place-items: center; padding: 24px;
  text-align: center; color: var(--muted); background: #0b0d11;
}
.placeholder[hidden] { display: none; }

.flip {
  position: absolute; right: 10px; bottom: 10px; z-index: 2;
  border: 1px solid rgba(255, 255, 255, .25); border-radius: 999px;
  background: rgba(15, 17, 21, .72); color: var(--text);
  padding: 8px 14px; min-height: 40px; cursor: pointer;
  backdrop-filter: blur(4px);
}
.flip:disabled { opacity: .5; cursor: default; }
.stage.finger .flip { display: none; }

.readout {
  display: grid; grid-template-columns: auto 1fr; gap: 16px; align-items: center;
  margin: 14px 0 10px;
}
.hr { display: flex; align-items: baseline; gap: 6px; }
.hr span { font-size: 64px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--accent); line-height: 1; min-width: 2.2ch; }
.hr small { color: var(--muted); font-size: 14px; }

.meta dl { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 8px 0 0; }
.meta dl div { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }
.meta dt { color: var(--muted); font-size: 11px; }
.meta dd { margin: 0; font-variant-numeric: tabular-nums; font-size: 14px; }

.status { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); display: inline-block; flex: none; }
.status.good .dot { background: var(--good); }
.status.fair .dot { background: var(--fair); }
.status.bad .dot { background: var(--bad); }

.progress { height: 4px; background: var(--line); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width .3s; }

button { font: inherit; }
.primary {
  width: 100%; padding: 14px; border: 0; border-radius: 12px;
  background: var(--accent); color: #fff; font-weight: 600; font-size: 16px; cursor: pointer;
}
.primary.stop { background: var(--line); }
.starts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.ref { margin-top: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.ref label { color: var(--muted); font-size: 13px; }
.ref-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; margin-top: 6px; }
.ref-row input { font: inherit; font-size: 18px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line);
  background: transparent; color: inherit; min-width: 0; }
.ref-row button { font: inherit; padding: 8px 14px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; }
.ref-stats { margin: 8px 0 0; font-size: 13px; color: var(--muted); }
.primary.quick { background: #0f766e; }
.primary.rec { grid-column: 1 / -1; background: var(--panel); border: 1px solid var(--line); font-size: 14px; padding-top: 10px; padding-bottom: 10px; }
.history { margin-top: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.history summary { cursor: pointer; color: var(--muted); font-size: 14px; }
.history ol { margin: 8px 0; padding-left: 0; list-style: none; font-size: 13px; font-variant-numeric: tabular-nums; }
.history li { display: flex; justify-content: space-between; gap: 8px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.history li b { color: var(--text); }
.history li span { color: var(--muted); }
.history button { font: inherit; font-size: 13px; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); background: transparent; color: var(--muted); }
.primary:disabled { opacity: .45; cursor: default; }

.plots { display: grid; gap: 10px; margin-top: 14px; }
figure { margin: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
figcaption { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
figure canvas { width: 100%; height: 86px; display: block; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.actions button {
  padding: 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); cursor: pointer;
}
.actions button:disabled { opacity: .45; cursor: default; }

.help { margin-top: 16px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; }
.help summary { cursor: pointer; font-weight: 600; }
.help h3 { font-size: 14px; margin: 12px 0 4px; }
.help ul { margin: 0; padding-left: 18px; color: #c9cedb; }
.note { color: var(--muted); font-size: 12px; }

footer { color: var(--muted); font-size: 12px; text-align: center; margin-top: 18px; }
footer a { color: var(--muted); }
