:root {
  --mist: #eef2f5;
  --paper: #ffffff;
  --ink: #14263a;
  --slate: #566779;
  --line: #d3dce4;
  --live: #0b7a75;
  --live-soft: #d6ece9;
  --trigger: #e3a21a;
  --weak: #b4bec8;

  --radius-lg: 12px;
  --radius-sm: 6px;
  --font: "Libre Franklin", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.55;
  background: var(--paper);
  color: var(--ink);
}

h1 { font-size: 2.1rem; line-height: 1.1; margin: 0 0 0.6rem; letter-spacing: -0.025em; font-weight: 700; }
h2 { font-size: 1.05rem; font-weight: 700; margin: 2.5rem 0 0.75rem; }
.lede { color: var(--slate); max-width: 60ch; margin: 0 0 1.5rem; }

:focus-visible { outline: 3px solid var(--trigger); outline-offset: 2px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.5rem;
  flex-wrap: wrap;
  padding: 0 2rem;
  min-height: 68px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 0.6rem; color: var(--live); }
.brand-mark { width: 30px; height: 30px; }
.brand-name { font-weight: 700; font-size: 1.3rem; letter-spacing: -0.02em; color: var(--ink); }

.tabs { display: flex; gap: 1.75rem; align-self: stretch; }
.tab {
  font: inherit;
  font-size: 1rem;
  border: 0;
  background: transparent;
  color: var(--slate);
  padding: 0;
  min-height: 44px;
  border-bottom: 3px solid transparent;
  border-top: 3px solid transparent;
  cursor: pointer;
}
.tab:hover { color: var(--ink); }
.tab[aria-current="page"] { color: var(--ink); font-weight: 700; border-bottom-color: var(--ink); }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: clamp(280px, 38vw, 500px) minmax(0, 1fr);
  gap: 3.5rem;
  padding: 2.5rem 2rem 4rem;
  max-width: 1320px;
  margin: 0 auto;
  align-items: start;
}
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; padding: 1.25rem; }
  .topbar { padding: 1rem 1.25rem; }
}

.view { padding: 0.25rem 0 0; }
.view[hidden] { display: none; }

/* ---------- Camera ---------- */
.camera { position: sticky; top: 1.5rem; }
@media (max-width: 960px) { .camera { position: static; } }

.stage {
  position: relative;
  aspect-ratio: 4 / 3;
  background: var(--ink);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.stage video, .stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1);
}
.placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 1.5rem;
  text-align: center;
  color: #c9d5e0;
}
.placeholder[hidden] { display: none; }
.placeholder-title { margin: 0; font-size: 1.25rem; color: #fff; }
.placeholder-note { margin: 0; font-size: 0.9rem; }

.face-warning {
  position: absolute;
  left: 50%;
  bottom: 1rem;
  transform: translateX(-50%);
  margin: 0;
  background: var(--trigger);
  color: var(--ink);
  font-weight: 700;
  padding: 0.4rem 0.9rem;
  border-radius: 999px;
  white-space: nowrap;
}
.face-warning[hidden] { display: none; }

.camera-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.75rem;
  color: var(--slate);
  font-size: 0.95rem;
}
.recording { display: inline-flex; align-items: center; gap: 0.4rem; font-variant-numeric: tabular-nums; }
.recording[hidden] { display: none; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: #d64545; animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }

.camera-controls { display: flex; justify-content: space-between; align-items: center; margin-top: 0.5rem; }
.check { display: inline-flex; align-items: center; gap: 0.5rem; font-size: 0.95rem; color: var(--slate); cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--live); }

/* ---------- Buttons ---------- */
.btn {
  font: inherit;
  font-weight: 700;
  font-size: 1rem;
  min-height: 48px;
  padding: 0.7rem 1.4rem;
  border-radius: 8px;
  border: 1.5px solid transparent;
  cursor: pointer;
}
.btn[hidden] { display: none; }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn-primary { background: var(--live); color: #fff; }
.btn-primary:hover { background: #096762; }
.btn-secondary { background: var(--paper); color: var(--ink); border-color: var(--line); }
.btn-secondary:hover { border-color: var(--slate); }
.btn-quiet { background: transparent; color: var(--slate); padding-inline: 0.6rem; }
.btn-quiet:hover { color: var(--ink); }
.btn-large { font-size: 1.1rem; min-height: 56px; padding-inline: 2.25rem; }
.btn-danger { color: #a93226; }

/* ---------- Meters ---------- */
.meters { display: grid; gap: 0.7rem; }
.meter {
  display: grid;
  grid-template-columns: 9.5rem minmax(0, 1fr) 2.5rem;
  align-items: center;
  gap: 0.9rem;
}
.meter-label { font-size: 1rem; }
.meter-track {
  position: relative;
  height: 12px;
  background: var(--mist);
  border-radius: 6px;
  overflow: hidden;
}
.meter-fill { height: 100%; width: 0; background: var(--live); border-radius: 6px; }
.meter.is-on .meter-fill { background: var(--trigger); }
.meter.is-on .meter-label { font-weight: 700; }
.meter-best { position: absolute; top: 2px; bottom: 2px; width: 2px; background: var(--ink); opacity: 0.45; left: 0; display: none; }
.meter-best.has-value { display: block; }
.meter-threshold { position: absolute; top: 0; bottom: 0; width: 0; border-left: 2px dashed var(--trigger); left: 0; }
.meter-threshold[hidden] { display: none; }
.meter-value { text-align: right; color: var(--slate); font-variant-numeric: tabular-nums; font-size: 0.95rem; }

.meter-centered .meter-fill { position: absolute; top: 0; }
.meter-centered .meter-track::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--weak);
}

.meters-compact .meter { grid-template-columns: 11rem minmax(0, 1fr) 2.5rem; }
.meters-compact .meter-label { font-size: 0.85rem; color: var(--slate); }
.meters-compact .meter-track { height: 10px; }

.raw { margin-top: 2rem; }
.raw summary { cursor: pointer; color: var(--slate); min-height: 44px; display: flex; align-items: center; }
.raw .meters { margin-top: 0.75rem; }

/* ---------- Calibration ---------- */
.tips { color: var(--slate); padding-left: 1.2rem; margin: 0 0 2rem; }
.tips li { margin-bottom: 0.3rem; }

.cal-progress { display: grid; gap: 0.5rem; margin-bottom: 1.5rem; color: var(--slate); }
.track { height: 6px; background: var(--mist); border-radius: 3px; overflow: hidden; }
.track-fill { height: 100%; width: 0; background: var(--ink); }

.cal-stage {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  gap: 2.5rem;
  align-items: center;
  min-height: 260px;
}
@media (max-width: 700px) { .cal-stage { grid-template-columns: 1fr; justify-items: center; text-align: center; } }

.ring { width: 220px; height: 220px; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: var(--mist); stroke-width: 18; }
.ring-fill {
  fill: none;
  stroke: var(--live);
  stroke-width: 18;
  stroke-linecap: round;
  stroke-dasharray: 527.8;
  stroke-dashoffset: 527.8;
}
.ring.is-on .ring-fill { stroke: var(--trigger); }
.ring.is-idle .ring-fill { stroke: var(--weak); }

.cal-phase { margin: 0; font-size: 1.1rem; font-weight: 700; color: var(--live); }
.cal-phase.is-relax { color: var(--slate); }
.cal-prompt { margin: 0.25rem 0 1rem; font-size: clamp(2rem, 4.2vw, 3.4rem); line-height: 1.08; font-weight: 700; letter-spacing: -0.015em; }

.reps { display: flex; gap: 0.6rem; }
@media (max-width: 700px) { .reps { justify-content: center; } }
.rep { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--line); }
.rep.is-current { border-color: var(--ink); }
.rep.is-hit { background: var(--live); border-color: var(--live); }
.rep.is-miss { background: var(--paper); border-color: var(--weak); border-style: dashed; }

.phase-track { height: 4px; background: var(--mist); border-radius: 2px; margin: 2rem 0 1.5rem; overflow: hidden; }
.phase-fill { height: 100%; width: 0; background: var(--slate); }

.cal-actions { display: flex; gap: 0.75rem; flex-wrap: wrap; align-items: center; margin-top: 1.5rem; }

/* results */
.results { list-style: none; padding: 0; margin: 0; display: grid; counter-reset: rank; border-top: 1px solid var(--line); }
.result {
  display: grid;
  grid-template-columns: 2rem 11rem minmax(0, 1fr) 9.5rem;
  gap: 1rem;
  align-items: center;
  padding: 0.9rem 0;
  border-bottom: 1px solid var(--line);
  counter-increment: rank;
}
.result::before { content: counter(rank); color: var(--slate); font-variant-numeric: tabular-nums; }
.result-name { font-weight: 700; }
.result-detail { display: block; font-weight: 400; font-size: 0.85rem; color: var(--slate); }
.result-track { height: 10px; background: var(--mist); border-radius: 5px; overflow: hidden; }
.result-fill { height: 100%; border-radius: 6px; background: var(--live); }
.result.grade-usable .result-fill { background: #6fa9a5; }
.result.grade-weak .result-fill { background: var(--weak); }
.result-grade { text-align: right; font-size: 0.95rem; }
.result.grade-weak { color: var(--slate); }
.result.is-best .result-name { color: var(--live); }
@media (max-width: 700px) {
  .result { grid-template-columns: 1.5rem minmax(0, 1fr) auto; }
  .result-track { grid-column: 2 / -1; }
}

.saved-note { color: var(--slate); font-size: 0.95rem; margin: 1rem 0 0; }

/* ---------- History ---------- */
.trends { display: grid; gap: 0.4rem; }
.trend {
  display: grid;
  grid-template-columns: 8.5rem minmax(80px, 1fr) 6.5rem;
  gap: 1rem;
  align-items: center;
  padding: 0.6rem 0;
  border-bottom: 1px solid var(--line);
}
.trend svg { width: 100%; height: 36px; overflow: visible; }
.trend-line { fill: none; stroke: var(--live); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.trend-dot { fill: var(--live); }
.trend-num { text-align: right; line-height: 1.2; }
.trend-score { display: block; font-weight: 700; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.trend-change { display: block; font-size: 0.85rem; color: var(--slate); font-variant-numeric: tabular-nums; }
.trend-change.is-down { color: #a93226; }
.trend-change.is-up { color: var(--live); }

.sessions { list-style: none; padding: 0; margin: 0; display: grid; border-top: 1px solid var(--line); }
.session {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--line);
  font-size: 0.95rem;
}
.session-type { color: var(--slate); }

@media (prefers-reduced-motion: reduce) {
  .rec-dot { animation: none; }
}

/* topbar on phones */
@media (max-width: 640px) {
  .topbar { flex-direction: column; align-items: flex-start; gap: 0; padding-top: 0.75rem; }
  .tabs { gap: 1.25rem; overflow-x: auto; max-width: 100%; }
}

/* ---------- Practice ---------- */
.settings {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.25rem 2rem;
  align-items: end;
  margin-bottom: 1.25rem;
}
.field { display: grid; gap: 0.4rem; font-size: 0.95rem; color: var(--slate); border: 0; padding: 0; margin: 0; min-width: 0; }
.field legend { padding: 0; margin-bottom: 0.4rem; }
.field output { color: var(--ink); font-weight: 700; font-variant-numeric: tabular-nums; margin-left: 0.3rem; }
.field input[type="range"] { width: 100%; accent-color: var(--live); min-height: 32px; }
.field-actions { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.field[hidden] { display: none; }

.segmented { display: flex; border: 1.5px solid var(--line); border-radius: 8px; overflow: hidden; }
.segmented label { flex: 1; position: relative; }
.segmented input { position: absolute; opacity: 0; }
.segmented span {
  display: flex; align-items: center; justify-content: center; text-align: center;
  min-height: 44px; padding: 0.3rem 0.6rem; color: var(--ink); cursor: pointer; font-size: 0.9rem;
}
.segmented label + label span { border-left: 1.5px solid var(--line); }
.segmented input:checked + span { background: var(--ink); color: #fff; font-weight: 700; }
.segmented input:focus-visible + span { outline: 3px solid var(--trigger); outline-offset: -3px; }
.hint { color: var(--slate); font-size: 0.9rem; margin: -0.5rem 0 1rem; }
.hint[hidden] { display: none; }

.arena {
  position: relative;
  aspect-ratio: 16 / 10;
  background: var(--mist);
  border-radius: var(--radius-lg);
  overflow: hidden;
  cursor: none;
}
.arena-msg {
  position: absolute; inset: 0; margin: 0; display: flex; align-items: center; justify-content: center;
  text-align: center; padding: 2rem; color: var(--slate);
}
.arena-msg[hidden] { display: none; }
.target {
  position: absolute; left: 0; top: 0;
  width: 96px; height: 96px; margin: -48px 0 0 -48px;
  border-radius: 50%;
  border: 3px solid var(--ink);
  background: radial-gradient(circle, var(--ink) 0 7px, transparent 8px);
}
.target[hidden] { display: none; }
.target.is-hit { border-color: var(--live); background: radial-gradient(circle, var(--live) 0 7px, transparent 8px); }
.cursor {
  position: absolute; left: 0; top: 0;
  width: 60px; height: 60px; margin: -30px 0 0 -30px;
  pointer-events: none;
}
.cursor::after {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: 16px; height: 16px; margin: -8px 0 0 -8px;
  border-radius: 50%; background: var(--live); box-shadow: 0 0 0 3px #fff;
}
.cursor svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.cursor-ring { fill: none; stroke: var(--trigger); stroke-width: 5; stroke-linecap: round; stroke-dasharray: 150.8; stroke-dashoffset: 150.8; }
.cursor.is-click::after { background: var(--trigger); }

.practice-bar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1rem; margin-top: 1rem; }
.practice-bar .cal-actions { margin-top: 0; }
.stats { display: flex; gap: 1.5rem; flex-wrap: wrap; color: var(--slate); font-variant-numeric: tabular-nums; }
.stats strong { color: var(--ink); }

/* ---------- Doctor report ---------- */
.report-head { border-bottom: 2px solid var(--ink); padding-bottom: 1.25rem; margin-bottom: 1.5rem; }
.report-brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 700; color: var(--slate); margin-bottom: 0.75rem; }
.report-brand .brand-mark { width: 24px; height: 24px; }
.report-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0.75rem 1.5rem; margin-top: 1rem; }
.report-fields label { display: grid; gap: 0.25rem; font-size: 0.85rem; color: var(--slate); }
.report-fields input {
  font: inherit; font-size: 1rem; color: var(--ink);
  border: 0; border-bottom: 1.5px solid var(--line); padding: 0.35rem 0; background: transparent; min-height: 40px;
}
.report-fields input:focus { outline: none; border-bottom-color: var(--ink); }
.rep-summary { background: var(--mist); border-radius: var(--radius-sm); padding: 1rem 1.25rem; }
.rep-summary p { margin: 0.3rem 0; }
#reportBody h2 { margin-top: 2rem; }
#reportBody h3 { font-size: 1rem; margin: 1.5rem 0 0.15rem; }
#reportBody .muted { color: var(--slate); font-size: 0.9rem; margin: 0 0 0.5rem; max-width: 70ch; }
#reportBody table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
#reportBody th { text-align: left; font-weight: 500; color: var(--slate); font-size: 0.85rem; border-bottom: 1.5px solid var(--ink); padding: 0.4rem 0.5rem 0.4rem 0; }
#reportBody td { border-bottom: 1px solid var(--line); padding: 0.45rem 0.5rem 0.45rem 0; }
#reportBody .num { text-align: right; font-variant-numeric: tabular-nums; }
#reportBody td.down { color: #a93226; font-weight: 700; }
#reportBody td.up { color: var(--live); font-weight: 700; }
#reportBody .disclaimer { margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--line); font-size: 0.85rem; color: var(--slate); }

@media print {
  @page { margin: 16mm; }
  body { font-size: 11pt; background: #fff; }
  .topbar, .camera, .no-print, #reportEmpty { display: none !important; }
  .layout { display: block; padding: 0; max-width: none; }
  .view { display: none !important; }
  #view-report { display: block !important; }
  .report-fields input { border-bottom-color: #999; }
  .report-fields input::placeholder { color: transparent; }
  .rep-region { break-inside: avoid; }
  .rep-summary { border: 1px solid #ccc; }
}
