:root {
  color-scheme: dark;
  --bg: #0e1014;
  --panel: #14161b;
  --panel2: #1a1d23;
  --grid: #252a32;
  --stroke-strong: #333944;
  --text: #e6e8ec;
  --muted: #a3a9b5;
  --faint: #6f7684;
  --track: #232730;
  --accent: #958df0;
  --ecg: #54d6b7;
  --good: #2ea56a;
  --warn: #d29922;
  --bad: #e5484d;
  --ring-good: #3ddc97;
  --ring-warn: #f5c451;
  --ring-bad: #ff6b6b;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --num: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --panel-radius: 8px;
  --chip-radius: 5px;
  --side-padding: 8px;
  background: var(--bg);
  color: var(--text);
  font: 14px var(--font);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
body, h1, h2, p, dl, dd { margin: 0; }
body:has(#classroom:not([hidden])) {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
}
[hidden] { display: none !important; }
button, select {
  font: inherit;
  color: inherit;
  border: 1px solid var(--grid);
  border-radius: var(--chip-radius);
  background: var(--panel);
  min-height: 26px;
  padding: 2px 8px;
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
  transition: background-color .15s, border-color .15s, color .15s;
}
select { max-width: 190px; text-overflow: ellipsis; }
button:disabled { color: var(--faint); cursor: default; }
button:focus-visible, select:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
@media (hover: hover) {
  button:not(:disabled):hover, select:hover {
    border-color: var(--stroke-strong);
    background: var(--panel2);
  }
  #capture:hover { filter: brightness(1.06); background: var(--accent); }
  #flip:not(:disabled):hover { background: #1a1d23d9; border-color: #ffffff66; }
  summary:hover { color: var(--text); }
}
#classroom {
  margin: auto;
  width: min(100%, 680px);
  height: 100vh;
  height: 100dvh;
  min-height: 300px;
  padding: max(4px, env(safe-area-inset-top)) max(var(--side-padding), env(safe-area-inset-right))
    max(4px, env(safe-area-inset-bottom)) max(var(--side-padding), env(safe-area-inset-left));
  display: grid;
  grid-template-rows: minmax(61px, 7.5%) minmax(0, 45%) minmax(0, 1fr) minmax(19px, 3%);
  gap: max(3px, .35dvh);
}
header { display: grid; grid-template-rows: 1.15fr 1fr; }
.topbar, .controls, .top-actions, h1, .readout-control, .heart-heading, .signal {
  display: flex;
  align-items: center;
}
.topbar, .controls { justify-content: space-between; gap: 6px; }
.topbar { border-bottom: 1px solid var(--grid); }
h1 { font-size: 15px; font-weight: 600; gap: 6px; }
.brand-mark {
  border-radius: var(--chip-radius);
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  background: var(--accent);
  color: var(--bg);
  font-size: 11px;
  font-weight: 700;
}
.top-actions { gap: 6px; font-size: 11px; }
.signal { gap: 4px; white-space: nowrap; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%); }
.dot { flex: none; width: 6px; height: 6px; border-radius: 50%; background: var(--faint); }
.signal[data-level="good"] .dot { background: var(--good); }
.signal[data-level="warn"] .dot { background: var(--warn); }
.signal[data-level="bad"] .dot { background: var(--bad); }
#flip {
  position: absolute;
  inset: 8px 8px auto auto;
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border: 1px solid #ffffff38;
  border-radius: 50%;
  background: #0e1014b8;
  color: #fff;
  padding: 0;
}
#flip svg { width: 24px; height: 24px; }
.camera-badge {
  position: absolute;
  bottom: 6px;
  border-radius: var(--chip-radius);
  background: #0e1014c7;
  color: #fff;
  font: 10px/1.25 var(--num);
  padding: 3px 6px;
  white-space: nowrap;
  pointer-events: none;
}
#preview-info { left: 6px; }
#preview-timing { right: 6px; }
.readout-control { display: flex; align-items: center; font-size: 11px; }
.caption { font-size: 10px; color: var(--muted); margin-right: 5px; }
.segments { display: flex; min-width: 0; }
.segments button { min-height: 23px; color: var(--faint); font-size: 10px; }
.segments button[aria-pressed="true"] {
  color: var(--text);
  background: var(--panel2);
  border-color: var(--stroke-strong);
}
#mode button:first-child { border-radius: var(--chip-radius) 0 0 var(--chip-radius); }
#mode button:last-child { border-radius: 0 var(--chip-radius) var(--chip-radius) 0; border-left: 0; }
.preview {
  position: relative;
  isolation: isolate;
  border: 1px solid var(--grid);
  border-radius: var(--panel-radius);
  overflow: hidden;
  background: #000;
  min-height: 0;
}
video { display: block; width: 100%; height: 100%; object-fit: contain; }
.preview.mirrored video { transform: scaleX(-1); }
#crop { position: absolute; pointer-events: none; background: #958df024; }
#crop i { position: absolute; width: 20%; height: 20%; border: 0 solid var(--accent); }
#crop i:nth-child(1) { top: 0; left: 0; border-width: 2px 0 0 2px; }
#crop i:nth-child(2) { top: 0; right: 0; border-width: 2px 2px 0 0; }
#crop i:nth-child(3) { bottom: 0; right: 0; border-width: 0 2px 2px 0; }
#crop i:nth-child(4) { bottom: 0; left: 0; border-width: 0 0 2px 2px; }
.camera-prompt {
  position: absolute;
  inset: 50% 8px auto;
  transform: translateY(-50%);
  display: grid;
  justify-items: center;
  gap: 8px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  pointer-events: none;
}
#capture {
  background: var(--accent);
  color: var(--bg);
  border-color: transparent;
  font-weight: 650;
  min-height: 42px;
  padding-inline: 16px;
  pointer-events: auto;
}
#camera-status { text-shadow: 0 1px 5px #000; max-width: 34ch; }
#failure { max-width: min(100%, 32rem); pointer-events: auto; }
#processing-fallbacks {
  font-size: 11px;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  max-height: 10rem;
  overflow-y: auto;
  margin: 0;
  padding-inline-start: 1.25rem;
}
#copy-details { min-height: 32px; margin-top: 6px; }
#failure-copy { width: 100%; margin-top: 6px; font: inherit; }
#warning {
  position: absolute;
  top: 0;
  inset-inline: 0;
  padding: 6px;
  text-align: center;
  background: #211907ee;
  color: #f1c574;
  font-size: 11px;
}
.preview:has(#flip:not([hidden])) #warning { right: 62px; }
.readings {
  display: grid;
  min-height: 0;
  grid-template-rows: minmax(96px, 1.46fr) minmax(44px, 1.03fr) minmax(46px, 1fr);
  gap: max(3px, .35dvh);
}
.heart {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: repeat(2, minmax(0, 1fr)) auto;
  align-items: center;
  padding: 4px 6px;
  gap: 2px 8px;
  border: 1px solid var(--grid);
  border-radius: var(--panel-radius);
  background: var(--panel);
  container-type: inline-size;
}
.heart-heading { gap: 6px; }
.metric-values {
  grid-area: 2 / 1 / 3 / 3;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: baseline;
  gap: 3px 8px;
}
.hrv-status { grid-column: 2; min-block-size: 1.2em; color: var(--muted); font-size: 10px; line-height: 1.2; }
h2 { font-size: 12px; font-weight: 600; color: var(--muted); }
.signal {
  border-radius: 20px;
  border: 1px solid var(--grid);
  padding: 2px 6px;
  font-size: 10px;
  line-height: 1.1;
  color: var(--muted);
}
.signal[data-level="good"] { background: #2ea56a18; color: var(--good); }
.signal[data-level="warn"] { background: #d2992218; color: var(--warn); }
.signal[data-level="bad"] { background: #e5484d18; color: var(--bad); }
.value { display: flex; align-items: baseline; gap: 5px; }
.value output { font: 600 clamp(32px, 5.2dvh, 44px)/1 var(--num); font-variant-numeric: tabular-nums; }
.value span { font-size: 11px; color: var(--muted); }
#hrv { font-size: clamp(25px, 4.15dvh, 35px); }
.quality {
  --ring-color: var(--faint);
  grid-column: 3;
  grid-row: 1 / 3;
  justify-self: end;
  display: grid;
  justify-items: center;
  inline-size: max(25ch, clamp(64px, 10.8dvh, 96px));
  font: 9px var(--num);
  margin-inline-end: 2px;
}
.quality:lang(zh) { inline-size: max(19ch, clamp(64px, 10.8dvh, 96px)); }
.quality[data-level="good"] { --ring-color: var(--ring-good); }
.quality[data-level="warn"] { --ring-color: var(--ring-warn); }
.quality[data-level="bad"] { --ring-color: var(--ring-bad); }
.confidence-ring { width: clamp(64px, 10.8dvh, 96px); aspect-ratio: 1; display: grid; place-items: center; }
.confidence-ring > * { grid-area: 1 / 1; }
.confidence-ring svg { width: 100%; transform: rotate(-90deg); }
.ring-bg, .ring-fg { fill: none; stroke-width: 9; }
.ring-bg { stroke: #ffffff10; }
.ring-fg { stroke: var(--ring-color); stroke-linecap: round; stroke-dasharray: 314.159265; stroke-dashoffset: 314.159265; transition: stroke .6s; }
.ring-center { display: grid; justify-items: center; color: var(--muted); font-size: 9px; }
.ring-center output { font: 600 19px/1.15 var(--num); color: var(--ring-color); transition: color .6s; }
#posterior-width { color: var(--faint); font: 9px var(--num); white-space: nowrap; }
.chart {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 5px 10px 4px;
  border-radius: var(--panel-radius);
  border: 1px solid var(--grid);
  background: var(--panel);
}
.chart h2 { font-size: 11px; }
.strip { padding: 4px 8px; }
.wave-lane { display: grid; grid-template-columns: minmax(0, 1fr); grid-auto-rows: minmax(0, 1fr); flex: 1; min-height: 0; }
.ecg-wave { border-top: 1px solid var(--grid); }
.ecg-comparison { grid-column: 1 / -1; color: var(--faint); font-size: 10px; }
#ecg-metrics { display: flex; flex-wrap: wrap; gap: 1px 9px; color: var(--muted); }
#ecg-metrics > div { gap: 3px; font-size: inherit; line-height: 1.3; }
#ecg-metrics dd { color: var(--ecg); }
#ecg-legend { color: var(--ecg); }
.wave-lane canvas { height: 100%; }
.trend-heading, .trend-heading .legend { display: flex; align-items: center; gap: 4px; }
.trend-heading { justify-content: space-between; flex-wrap: wrap; }
.trend-heading .legend i { width: 12px; height: 2px; background: var(--accent); }
.legend { font-size: 9px; color: var(--muted); font-weight: 400; white-space: nowrap; }
.chart canvas { flex: 1 1 0; min-height: 0; width: 100%; display: block; }
footer { position: relative; display: flex; justify-content: space-between; align-items: center; gap: 6px; }
footer p { color: var(--muted); font-size: 10px; line-height: 1.2; }
summary { cursor: pointer; font-size: 10px; color: var(--muted); white-space: nowrap; list-style: none; padding: 2px 4px; border-radius: var(--chip-radius); }
summary::-webkit-details-marker { display: none; }
.diagnostic-panel {
  position: absolute;
  z-index: 5;
  bottom: 100%;
  right: 0;
  margin-bottom: 4px;
  background: var(--panel2);
  border: 1px solid var(--stroke-strong);
  border-radius: var(--panel-radius);
  box-shadow: 0 12px 32px #00000080;
  padding: 8px 10px;
  width: min(320px, 100%);
  max-height: min(56dvh, 28rem);
  overflow-y: auto;
  overscroll-behavior: contain;
}
dl div { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 11px; line-height: 1.75; }
dt { color: var(--muted); }
dd { text-align: right; font-variant-numeric: tabular-nums; }
#stop { margin-top: 6px; font-size: 11px; width: 100%; min-height: 30px; }
#external-browser { min-height: 100dvh; display: grid; place-items: center; padding: 20px; text-align: center; }
.browser-guide {
  position: relative;
  width: min(100%, 400px);
  padding: 56px 22px 22px;
  border: 1px solid var(--grid);
  border-radius: var(--panel-radius);
  background: var(--panel);
}
#external-language { position: absolute; top: 12px; right: 12px; }
.browser-guide h1 { margin: 0; font-size: 22px; justify-content: center; }
.browser-guide p { margin: 10px 0; color: var(--muted); line-height: 1.5; }
.guide-actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; }
.guide-open {
  display: inline-grid;
  place-items: center;
  min-height: 38px;
  padding: 0 14px;
  border-radius: var(--chip-radius);
  background: var(--accent);
  color: var(--bg);
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
}
.guide-open:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
#manual-link {
  width: 100%;
  padding: 8px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--grid);
  border-radius: var(--chip-radius);
}

/* Portrait Tablets & iPads (iPad Mini / Air / Pro, Android Pads) */
@media (orientation: portrait) and (min-width: 700px) {
  #classroom {
    --side-padding: 16px;
    width: min(94vw, 880px);
    grid-template-rows: minmax(68px, 7.2%) minmax(0, 44%) minmax(0, 1fr) minmax(24px, 3%);
    gap: 8px;
  }
  h1 { font-size: 18px; gap: 8px; }
  .brand-mark { width: 26px; height: 26px; font-size: 13px; }
  button, select { min-height: 32px; padding: 4px 12px; font-size: 12px; }
  .segments button { min-height: 30px; padding: 3px 10px; font-size: 11px; }
  .caption { font-size: 12px; margin-right: 6px; }
  #capture { min-height: 46px; padding-inline: 22px; font-size: 14px; }
  .readings { gap: 8px; }
  .heart { padding: 10px 14px; gap: 4px 12px; }
  h2 { font-size: 13px; }
  .chart { padding: 8px 14px 6px; }
  .chart h2 { font-size: 12px; }
  .legend { font-size: 10px; }
  footer p, summary { font-size: 11px; }
  .diagnostic-panel { width: min(380px, 100%); padding: 10px 14px; }
}

/* Landscape across Phones, Tablets/iPads, Laptops & Desktop Monitors */
@media (orientation: landscape) {
  #classroom {
    width: min(100%, 1040px);
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: minmax(38px, auto) minmax(0, 1fr) minmax(18px, auto);
    gap: 5px 8px;
  }
  header, footer { grid-column: 1 / -1; }
  header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 6px 14px;
    border-bottom: 1px solid var(--grid);
    padding-bottom: 4px;
  }
  .topbar { border-bottom: 0; flex: 1 1 auto; }
  .controls { order: -1; }
  .topbar { order: -2; }
  .value output { font-size: clamp(28px, 7.2dvh, 44px); }
}

/* Compact Landscape Phones (e.g. 667x375, 800x360 Honor 70, 844x390, 915x412) */
@media (orientation: landscape) and (max-height: 500px) {
  #classroom {
    grid-template-rows: minmax(34px, auto) minmax(0, 1fr) minmax(16px, auto);
    gap: 4px 8px;
  }
  header { padding-bottom: 2px; }
  button, select { min-height: 24px; padding: 1px 7px; }
  .segments button { min-height: 22px; }
  .readings {
    grid-template-rows: minmax(72px, 1.38fr) minmax(36px, 1fr) minmax(38px, 1fr);
    gap: 4px;
  }
  .heart { padding: 3px 6px; }
  .value output { font-size: clamp(24px, 7.4dvh, 34px); }
  #hrv { font-size: clamp(19px, 6dvh, 28px); }
  .confidence-ring { width: clamp(58px, 14.5dvh, 74px); }
  .ring-center { font-size: 7.5px; line-height: 1.05; }
  .ring-center output { font-size: clamp(14px, 4dvh, 18px); line-height: 1.05; }
}

/* Landscape Tablets/iPads, Laptops & Desktop Displays */
@media (orientation: landscape) and (min-height: 501px) and (min-width: 900px) {
  #classroom {
    --side-padding: 18px;
    width: min(96vw, 1340px);
    height: min(100dvh, 940px);
    padding-block: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-bottom));
    grid-template-columns: minmax(0, 1.06fr) minmax(0, 1fr);
    grid-template-rows: minmax(46px, auto) minmax(0, 1fr) minmax(22px, auto);
    gap: 10px 14px;
  }
  header { padding-bottom: 6px; gap: 8px 18px; }
  h1 { font-size: 17px; gap: 8px; }
  .brand-mark { width: 25px; height: 25px; font-size: 12px; }
  button, select { min-height: 31px; padding: 4px 11px; font-size: 12px; }
  .segments button { min-height: 29px; padding: 3px 10px; font-size: 11px; }
  .caption { font-size: 12px; margin-right: 6px; }
  #capture { min-height: 44px; padding-inline: 20px; font-size: 14px; }
  .readings {
    grid-template-rows: minmax(112px, 1.38fr) minmax(60px, 1.05fr) minmax(64px, 1fr);
    gap: 10px;
  }
  .heart { padding: 10px 14px; gap: 4px 12px; }
  h2 { font-size: 13px; }
  .value output { font-size: clamp(36px, 5.2dvh, 46px); }
  #hrv { font-size: clamp(28px, 4.1dvh, 36px); }
  .confidence-ring { width: clamp(72px, 10dvh, 94px); }
  .chart { padding: 8px 12px 6px; }
  .chart h2 { font-size: 12px; }
  .legend { font-size: 10px; }
  footer p, summary { font-size: 11px; }
  .diagnostic-panel { width: min(380px, 100%); padding: 10px 14px; }
}

@media (orientation: portrait) and (max-height: 690px) {
  #classroom { grid-template-rows: minmax(56px, 8.8%) minmax(0, 40%) minmax(0, 1fr) minmax(19px, 3%); }
  .readings { grid-template-rows: minmax(86px, 1.42fr) minmax(38px, 1.02fr) minmax(40px, 1fr); }
}
@container (max-width: 330px) {
  .quality { inline-size: auto; max-width: 98px; }
  .ring-center { font-size: 8px; line-height: 1.05; }
  .ring-center output { font-size: 16px; line-height: 1.05; }
  #posterior-width { text-align: center; white-space: normal; line-height: 1.1; }
}
@container (max-width: 290px) {
  .quality { inline-size: auto; max-width: 76px; }
  .confidence-ring { width: clamp(56px, 10dvh, 66px); }
  .ring-center { font-size: 7.5px; line-height: 1.05; }
  .ring-center output { font-size: 14px; line-height: 1.05; }
  .value { gap: 3px; }
  .value output { font-size: clamp(21px, 9.2cqi, 26px); }
  #hrv { font-size: clamp(18px, 8cqi, 23px); }
}
@media (orientation: portrait) and (max-width: 365px) {
  #hr { font-size: min(clamp(30px, 5dvh, 42px), 11.8cqi); }
}
@media (orientation: portrait) and (max-width: 330px) {
  .heart:has(#ecg-metrics:not([hidden])) .confidence-ring { width: 52px; }
  .heart:has(#ecg-metrics:not([hidden])) .ring-center output { font-size: 15px; }
}
