/* Electrocardiography Lab: application shell. Tokens live in tokens.css. */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body.ecg-app {
  margin: 0;
  overflow-x: hidden;
  min-width: 320px;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.5; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  box-shadow: 0 0 0 6px color-mix(in srgb, var(--primary-strong) 55%, transparent);
  border-radius: 6px;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.ecg-skip {
  position: fixed; z-index: 100; top: 10px; left: 10px; padding: 8px 12px; border-radius: 8px;
  background: var(--ink); color: var(--surface); font-weight: 700; transform: translateY(-160%);
}
.ecg-skip:focus { transform: none; }

/* --- Top bar ------------------------------------------------------------- */

.ecg-top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 8px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  padding-top: max(8px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.ecg-brand { display: inline-flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; min-width: 0; }
.ecg-brand img { display: block; }
.ecg-brand-name { font-weight: 800; letter-spacing: -0.02em; font-size: 17px; white-space: nowrap; }
.ecg-modes { margin-inline: auto; }
.ecg-top-actions { display: flex; gap: 4px; }
body.is-embed .ecg-brand { display: none; }
body.is-embed .ecg-modes { margin-inline: 0 auto; }

.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px; padding: 0;
  border: 1px solid transparent; border-radius: 10px; background: transparent; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn svg, .play-btn svg, .pick svg, .btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Segmented control */
.seg { display: inline-flex; padding: 3px; gap: 2px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--line); }
.seg > button {
  min-height: 34px; padding: 4px 12px; border: 0; border-radius: 9px; background: transparent;
  color: var(--muted); font-weight: 650; font-size: 14px; white-space: nowrap;
}
.seg > button:hover { color: var(--ink); }
.seg > button[aria-pressed="true"], .seg > button[aria-selected="true"] {
  background: var(--surface); color: var(--primary-strong);
  box-shadow: 0 1px 2px rgba(8, 54, 120, 0.18), 0 0 0 1px var(--line);
}

/* --- Layout -------------------------------------------------------------- */

.ecg-main {
  width: min(1560px, 100%); margin-inline: auto;
  padding: 14px max(16px, env(safe-area-inset-right)) 40px max(16px, env(safe-area-inset-left));
  display: grid; gap: 14px;
}
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }

.ecg-scenario { display: grid; grid-template-columns: auto repeat(3, minmax(0, 1fr)) minmax(190px, 0.8fr); gap: 10px; }
.pick-cases { background: var(--primary-soft); border-color: color-mix(in srgb, var(--primary) 40%, var(--line)); }
.pick-cases .pick-value { color: var(--primary-strong); }
.pick, .rate {
  display: grid; gap: 1px; text-align: left; min-height: 60px; padding: 9px 38px 9px 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; position: relative;
}
.pick:hover { border-color: var(--primary); }
.pick::after {
  content: ""; position: absolute; right: 16px; top: 50%; width: 8px; height: 8px; margin-top: -6px;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg);
}
.pick-label { font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.pick-value { font-weight: 700; font-size: 15.5px; line-height: 1.25; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pick.is-set { border-color: color-mix(in srgb, var(--primary) 55%, var(--line)); background: color-mix(in srgb, var(--primary-soft) 55%, var(--surface)); }
.rate { padding-right: 14px; }
.pick, .rate { min-width: 0; }
.rate-row { min-width: 0; }
.rate-row { display: flex; align-items: center; gap: 10px; }
.rate-row input { flex: 1; min-width: 0; accent-color: var(--primary); height: 24px; }
.rate-row output { font-family: var(--mono); font-weight: 700; font-size: 15px; min-width: 7.5ch; text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

.ecg-stage { display: grid; grid-template-columns: minmax(0, 1fr) minmax(340px, 440px); gap: 14px; align-items: start; }
.ecg-stage.no-side { grid-template-columns: minmax(0, 1fr); }

/* --- Monitor -------------------------------------------------------------- */

.monitor { overflow: hidden; }
.monitor-bar, .side-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px 12px; }
.monitor-spacer { flex: 1; }
.monitor-ctrls { display: flex; align-items: center; gap: inherit; margin-left: auto; }
.chip-btn {
  min-height: 36px; padding: 4px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  color: var(--muted); font-weight: 650; font-size: 14px; list-style: none;
}
.chip-btn::-webkit-details-marker { display: none; }
.chip-btn:hover { color: var(--ink); border-color: var(--muted); }
.chip-btn[aria-pressed="true"] { color: var(--primary-strong); border-color: var(--primary); background: var(--primary-soft); }
#btn-speed { min-width: 92px; font-variant-numeric: tabular-nums; }
.ap-root { display: grid; gap: 8px; }
.ap-seg { display: flex; flex-wrap: wrap; }
.ap-seg > button { flex: 1 1 auto; padding-inline: 8px; font-size: 13px; }
.ap-canvas { display: block; width: 100%; border: 1px solid var(--line); border-radius: 12px; cursor: pointer; }
.ap-caption { margin: 0; font-size: 14px; line-height: 1.45; min-height: 4.35em; }
.ap-caption strong { color: var(--primary-strong); }
.ap-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.ap-play { display: inline-flex; align-items: center; gap: 6px; color: var(--primary-strong); border-color: var(--primary); }
.ap-play svg { width: 16px; height: 16px; fill: currentColor; stroke: none; }
.ap-speed { display: inline-flex; align-items: center; gap: 10px; min-height: 40px; padding: 4px 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface, transparent); font-size: 14px; font-weight: 600; color: var(--primary-strong); }
.ap-speed-name { color: var(--muted); font-weight: 600; }
.ap-speed-range { width: 128px; margin: 0; accent-color: var(--primary); cursor: pointer; touch-action: pan-y; }
.ap-speed-range:focus-visible { outline: 2px solid var(--primary); outline-offset: 3px; border-radius: 4px; }
.ap-speed-value { min-width: 3.2em; text-align: right; font-variant-numeric: tabular-nums; }
.ap-tip { margin: 0; font-size: 12.5px; color: var(--muted); flex: 1 1 140px; }
.sound-btn { display: inline-flex; align-items: center; gap: 6px; }
.sound-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.play-btn {
  display: inline-flex; align-items: center; gap: 8px; min-height: 38px; padding: 4px 16px 4px 12px;
  border: 0; border-radius: 11px; background: var(--primary); color: var(--on-primary); font-weight: 700;
}
.play-btn:hover { background: var(--primary-strong); }
.play-btn svg { fill: currentColor; stroke: none; }
.select select {
  min-height: 36px; padding: 4px 30px 4px 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 650; font-size: 14px; color: var(--ink);
}
.pop { position: relative; }
.pop > summary { list-style: none; cursor: pointer; }
.pop > summary::-webkit-details-marker { display: none; }
.pop[hidden] { display: none; }
.more-btn { border-color: var(--line); background: var(--surface); }
.more-btn svg { stroke-width: 3.2; }
.pop[open] > .more-btn { background: var(--primary-soft); color: var(--primary-strong); border-color: var(--primary); }
.pop[open] > .pop-body { animation: pop-in 0.16s cubic-bezier(.2, .8, .2, 1); transform-origin: 100% 0; }
@keyframes pop-in { from { opacity: 0; transform: translateY(-6px) scale(0.97); } to { opacity: 1; transform: none; } }
.menu-rows { display: grid; gap: 2px; margin: -6px -6px 0; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.menu-rows:last-child { margin-bottom: -6px; padding-bottom: 0; border-bottom: 0; }
.menu-row { display: flex; align-items: center; gap: 10px; width: 100%; min-width: 190px; min-height: 40px; padding: 6px 10px; border: 0; border-radius: 9px; background: transparent; color: var(--ink); font: inherit; font-weight: 600; font-size: 14.5px; text-align: left; cursor: pointer; }
.menu-row:hover { background: var(--surface-2); }
.menu-row[hidden] { display: none; }
.menu-row svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.menu-row[aria-pressed]::after { content: ""; margin-left: auto; width: 34px; height: 20px; border-radius: 999px; background-color: var(--line); background-image: radial-gradient(circle at 10px 10px, #fff 7px, transparent 7.5px); background-repeat: no-repeat; transition: background-color 0.18s ease, background-position 0.18s ease; }
.menu-row[aria-pressed="true"]::after { background-color: var(--primary); background-position: 14px 0; }
@media (prefers-reduced-motion: reduce) { .pop[open] > .pop-body { animation: none; } .menu-row[aria-pressed]::after { transition: none; } }
@media (max-width: 640px) {
  /* Two rows on phones: the view switch across the top, then the 12-lead chest
     lead set (when shown), speed, options and play. */
  .monitor-bar { flex-wrap: wrap; gap: 8px 6px; }
  .monitor-bar #view-seg { flex: 1 0 100%; }
  .monitor-bar #view-seg > button { flex: 1 1 0; min-width: 0; padding-inline: 6px; white-space: nowrap; }
  /* The lead set shares the second row when its longest name fits, and takes
     a row of its own on narrower phones; the controls stay right-aligned. */
  .monitor-bar #leadset-wrap { flex: 1 1 172px; }
  .monitor-bar #leadset-wrap select { width: 100%; }
  .monitor-bar .monitor-spacer { display: none; }
  .monitor-bar #btn-speed { font-size: 13px; }
  /* Anchor the options menu to the right end of the controls so it opens
     fully on screen, even on the narrowest phones. */
  .monitor-bar .monitor-ctrls { position: relative; }
  .monitor-bar #display-pop { position: static; }
  .monitor-bar #display-pop > .pop-body { max-width: calc(100vw - 24px); }
  .monitor-bar #btn-speed { padding-inline: 8px; white-space: nowrap; }
  .monitor-bar .play-btn { padding-inline: 12px; }
  .monitor-bar .play-btn .play-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .side-bar { flex-wrap: nowrap; }
  .side-bar #side-tabs { flex: 1 1 auto; min-width: 0; overflow-x: auto; scrollbar-width: none; }
  .side-bar .monitor-spacer { display: none; }
}
.pop-body {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 15; display: grid; gap: 12px; padding: 14px; width: max-content;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
}
.pop-body fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 6px; }
.pop-body legend { padding: 0; font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }

.readouts { display: flex; flex-wrap: wrap; gap: 6px 22px; margin: 0; padding: 2px 14px 10px; }
.readouts > div { display: flex; align-items: baseline; gap: 6px; }
.readouts dt { font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.readouts dd { margin: 0; font-family: var(--mono); font-weight: 700; font-size: 16px; font-variant-numeric: tabular-nums; }
.readouts dd small { font-family: var(--font); font-weight: 600; font-size: 12px; color: var(--muted); margin-left: 3px; }
.readouts .hr dd { font-size: 22px; color: var(--primary-strong); line-height: 1; }
.readouts .axis-chip { font-family: var(--font); font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); margin-left: 6px; }
:root:not([data-theme="dark"]) .readouts .axis-chip:not(.is-abnormal) { color: #066b72; } /* 5.6:1 on the soft accent */
.readouts .axis-chip.is-abnormal { background: var(--danger-soft); color: var(--danger); }
.link-btn { border: 0; background: none; padding: 0 2px; color: var(--primary); font-weight: 700; font-size: 12.5px; text-decoration: underline; }

.paper-wrap { position: relative; background: var(--paper); border-block: 1px solid var(--line); overflow: hidden; touch-action: pan-y; }
#paper { display: block; width: 100%; }
.lead-tags { position: absolute; inset: 0; pointer-events: none; }
.lead-tag {
  position: absolute; pointer-events: auto; min-width: 34px; min-height: 24px; padding: 0 7px;
  border: 1px solid transparent; border-radius: 7px; background: color-mix(in srgb, var(--paper) 78%, transparent);
  color: var(--ink); font-weight: 750; font-size: 13px; line-height: 22px;
}
.lead-tag:hover { border-color: var(--primary); color: var(--primary-strong); background: var(--surface); }
.lead-tag::after { content: "▾"; margin-left: 3px; font-size: 10px; color: var(--muted); }
.lead-tags.is-dense .lead-tag { min-width: 0; min-height: 20px; padding: 0 4px; font-size: 11px; line-height: 18px; }
.lead-tags.is-dense .lead-tag::after { content: none; }
.paused-hint { color: var(--primary-strong); font-weight: 650; }
.lead-menu {
  position: absolute; z-index: 16; display: grid; gap: 8px; padding: 10px; width: 232px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
}
.lead-menu h3 { margin: 0; font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.lead-menu .grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; }
.lead-menu button { min-height: 32px; padding: 0; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); font-weight: 700; font-size: 12px; }
.lead-menu button:hover { border-color: var(--primary); }
.lead-menu button[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.monitor-foot { margin: 0; padding: 8px 14px 10px; font-size: 12.5px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 14px; }
.monitor-foot strong { color: var(--ink); }

/* --- ST deviation panel ------------------------------------------------------ */

.stage-main { display: grid; gap: 14px; min-width: 0; align-content: start; }
.stj { padding: 12px 14px 12px; display: grid; gap: 10px; }
.stj-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; }
.stj-head h2 { margin: 0; flex: 1; font-size: 16px; letter-spacing: -0.01em; }
.stj-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 6px; }
.stj-cell {
  display: grid; gap: 0; padding: 6px 4px 5px; text-align: center; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface-2);
}
.stj-cell b { font-size: 12px; font-weight: 700; color: var(--muted); }
.stj-cell span { font-family: var(--mono); font-weight: 700; font-size: 15px; font-variant-numeric: tabular-nums; }
.stj-cell.is-up { border-color: color-mix(in srgb, var(--danger) 55%, var(--line)); background: var(--danger-soft); }
.stj-cell.is-up span { color: var(--danger); }
.stj-cell.is-down { border-color: color-mix(in srgb, var(--primary) 55%, var(--line)); background: var(--primary-soft); }
.stj-cell.is-down span { color: var(--primary-strong); }
.stj-cell.is-extra { border-style: dashed; }
.stj-gap { grid-column: 1 / -1; height: 0; }
.stj-note { margin: 0; font-size: 12.5px; color: var(--muted); }
.stj-note strong { color: var(--ink); }
.stj-empty { grid-column: 1 / -1; margin: 0; padding: 10px; text-align: center; color: var(--muted); }

/* --- Side panel ------------------------------------------------------------ */

.side { overflow: hidden; }
.side-view { padding: 0 10px 10px; }
.flow-actions { display: grid; gap: 6px; padding: 4px 14px 14px; }
.flow-note { margin: 0; font-size: 13px; color: var(--muted); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 8px 18px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); font-weight: 700;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-strong); border-color: var(--primary-strong); }
.btn.ghost { border-color: transparent; color: var(--muted); }

/* --- Info panel ------------------------------------------------------------- */

.info summary {
  display: flex; align-items: center; gap: 10px; padding: 14px 16px; font-weight: 800; font-size: 16px; letter-spacing: -0.01em;
  cursor: pointer; list-style: none; border-radius: var(--radius);
}
.info summary::-webkit-details-marker { display: none; }
.info summary::after {
  content: ""; margin-left: auto; width: 9px; height: 9px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: rotate(45deg); transition: transform 0.15s;
}
.info details[open] summary::after { transform: rotate(-135deg); }
.info-body { padding: 0 16px 16px; display: grid; gap: 18px; }
.info-entry { display: grid; gap: 12px; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); align-items: start; }
.info-entry + .info-entry { padding-top: 18px; border-top: 1px solid var(--line); }
.info-entry h3 { margin: 0 0 4px; font-size: 17px; letter-spacing: -0.01em; }
.info-entry .kind { font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--accent); }
.info-entry p { margin: 0 0 8px; }
.info-entry h4 { margin: 10px 0 4px; font-size: 11.5px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.info-entry ul { margin: 0; padding-left: 18px; }
.info-entry li { margin: 2px 0; }
.criteria { margin: 0; display: grid; grid-template-columns: max-content 1fr; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.criteria dt, .criteria dd { margin: 0; padding: 7px 12px; border-top: 1px solid var(--line); }
.criteria dt:first-of-type, .criteria dt:first-of-type + dd { border-top: 0; }
.criteria dt { font-weight: 700; color: var(--muted); font-size: 13px; background: var(--surface-2); }
.criteria dd { font-weight: 600; }
.causes { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 !important; list-style: none; }
.causes li { margin: 0; padding: 2px 10px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); font-size: 13px; }

.ecg-disclaimer { margin: 0; text-align: center; font-size: 12.5px; color: var(--muted); }
.ecg-noscript { padding: 24px; text-align: center; }

/* --- Sheets (picker, export) -------------------------------------------------- */

dialog.sheet {
  width: min(560px, calc(100vw - 24px)); max-height: min(720px, calc(100dvh - 24px)); padding: 0; border: 1px solid var(--line);
  border-radius: 20px; background: var(--surface); color: var(--ink); box-shadow: 0 30px 80px rgba(3, 14, 32, 0.35); overflow: hidden;
}
dialog.sheet[open] { display: flex; flex-direction: column; }
dialog.sheet::backdrop { background: rgba(5, 16, 34, 0.5); backdrop-filter: blur(2px); }
.sheet-head { display: flex; align-items: center; gap: 10px; padding: 14px 14px 10px 18px; }
.sheet-head h2 { margin: 0; font-size: 18px; letter-spacing: -0.01em; flex: 1; }
.sheet-search { margin: 0 14px 8px; position: relative; }
.sheet-search input {
  width: 100%; min-height: 44px; padding: 8px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2); font-size: 16px;
}
.sheet-list { overflow-y: auto; padding: 0 8px 10px; overscroll-behavior: contain; }
.sheet-group { margin: 0; padding: 12px 10px 4px; font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.sheet-group small { text-transform: none; letter-spacing: 0; font-weight: 600; margin-left: 6px; }
.sheet-item {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 44px; padding: 7px 10px; text-align: left;
  border: 0; border-radius: 10px; background: transparent; font-weight: 600;
}
.sheet-item:hover { background: var(--surface-2); }
.sheet-item .mark { flex: none; width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--line); display: grid; place-items: center; }
.sheet-item.is-multi .mark { border-radius: 6px; }
.sheet-item[aria-pressed="true"], .sheet-item[aria-checked="true"] { background: var(--primary-soft); color: var(--primary-strong); }
.sheet-item[aria-pressed="true"] .mark, .sheet-item[aria-checked="true"] .mark { border-color: var(--primary); background: var(--primary); box-shadow: inset 0 0 0 3px var(--primary-soft); }
.sheet-item small { display: block; font-weight: 500; color: var(--muted); font-size: 12.5px; }
.sheet-empty { padding: 24px; text-align: center; color: var(--muted); }
.sheet-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 10px 14px 14px; border-top: 1px solid var(--line); }
.sheet-note { margin: 0 18px 8px; font-size: 13px; color: var(--muted); }

.export-body { padding: 0 18px 6px; display: grid; gap: 16px; overflow-y: auto; }
.export-body fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 6px; }
.export-body legend { padding: 0 0 4px; font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.check { display: flex; align-items: center; gap: 10px; min-height: 36px; font-weight: 600; }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }
.check.is-off { color: var(--muted); }
.format-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(148px, 1fr)); gap: 8px; }
.format-grid label { display: grid; gap: 1px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 12px; cursor: pointer; }
.format-grid label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.format-grid input { position: absolute; opacity: 0; }
.format-grid label:has(input:focus-visible) { outline: 3px solid var(--focus); outline-offset: 2px; }
.format-grid strong { font-size: 14.5px; }
.format-grid span { font-size: 12.5px; color: var(--muted); }
.export-body fieldset { align-content: start; }
.export-parts { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-rows: repeat(3, auto); grid-auto-flow: column; column-gap: 14px; row-gap: 2px; }
.export-parts .check { min-height: 34px; }
.export-parts .export-note { grid-row: 4; }
@media (max-width: 520px) { .export-parts { grid-template-columns: minmax(0, 1fr); grid-template-rows: none; grid-auto-flow: row; } .export-parts .export-note { grid-row: auto; } }
.check.is-sub { padding-left: 28px; font-weight: 500; font-size: 14px; }
.export-note { grid-column: 1 / -1; margin: 0; font-size: 12.5px; color: var(--muted); }
.export-note a { color: var(--primary); font-weight: 700; }
.export-row { display: flex; flex-wrap: wrap; gap: 16px 22px; }
.export-row > fieldset { flex: 1 1 220px; }
.export-row > fieldset[hidden] { display: none; }
.export-row .seg { display: flex; }
.export-row .seg > button { flex: 1 1 auto; }
.export-mark { padding: 10px 12px 12px !important; border: 1px solid var(--line) !important; border-radius: 12px; background: var(--surface-2); }
.export-mark[hidden] { display: none; }
.export-mark legend { float: left; width: 100%; display: flex; align-items: center; gap: 8px; padding: 0; }
.export-mark legend + * { clear: both; }
.export-badge { padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-size: 10.5px; letter-spacing: 0.04em; }

/* --- Quiz ---------------------------------------------------------------------- */

.quiz-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; display: grid; gap: 12px; }
.quiz-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 16px; }
.quiz-head h2 { margin: 0; font-size: 19px; letter-spacing: -0.015em; flex: 1; min-width: 200px; }
.quiz-streak { font-weight: 750; color: var(--accent); white-space: nowrap; }
.quiz-meta { font-family: var(--mono); font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums; }
.quiz-progress { height: 5px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.quiz-progress i { display: block; height: 100%; background: var(--primary); border-radius: inherit; transition: width 0.25s; }
.quiz-options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.quiz-option {
  min-height: 52px; padding: 10px 14px; text-align: left; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface); font-weight: 650;
}
.quiz-option:hover:not(:disabled) { border-color: var(--primary); }
.quiz-option:disabled { opacity: 1; cursor: default; }
.quiz-option.is-correct { border-color: var(--ok); background: var(--ok-soft); color: var(--ok); }
.quiz-option.is-wrong { border-color: var(--danger); background: var(--danger-soft); color: var(--danger); }
.quiz-feedback { padding: 12px 14px; border-radius: 12px; background: var(--surface-2); display: grid; gap: 4px; }
.quiz-feedback strong.ok { color: var(--ok); }
.quiz-feedback strong.no { color: var(--danger); }
.quiz-feedback p { margin: 0; }
.quiz-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.quiz-score { font-size: 44px; font-weight: 800; letter-spacing: -0.03em; line-height: 1; color: var(--primary-strong); }
.quiz-missed { margin: 0; padding-left: 18px; }
.quiz-hint { color: var(--muted); font-size: 13.5px; }
.quiz-intro p { margin: 0; color: var(--muted); max-width: 62ch; }

/* --- Tour ------------------------------------------------------------------------ */

.tour { position: fixed; inset: 0; z-index: 60; }
.tour-ring {
  position: fixed; border-radius: 16px; pointer-events: none;
  box-shadow: 0 0 0 3px var(--focus), 0 0 0 9999px rgba(4, 14, 30, 0.58); transition: all 0.25s ease;
}
.tour-card {
  position: fixed; width: min(340px, calc(100vw - 24px)); padding: 16px; display: grid; gap: 10px;
  background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 24px 60px rgba(3, 14, 32, 0.4);
}
.tour-card h2 { margin: 0; font-size: 17px; letter-spacing: -0.01em; }
.tour-card p { margin: 0; color: var(--muted); }
.tour-row { display: flex; align-items: center; gap: 8px; }
.tour-dots { display: flex; gap: 5px; margin-right: auto; }
.tour-dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--line); }
.tour-dots i.on { background: var(--primary); }

.toast {
  position: fixed; left: 50%; bottom: max(18px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 70;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px; background: var(--ink); color: var(--surface); font-weight: 600; box-shadow: var(--shadow);
}

/* --- Responsive -------------------------------------------------------------------- */

@media (max-width: 1080px) {
  .ecg-stage { grid-template-columns: minmax(0, 1fr); }
  .side-view { max-width: 520px; margin-inline: auto; }
}
@media (max-width: 860px) {
  .ecg-scenario { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .pick-cases { grid-column: 1 / -1; min-height: 0; grid-auto-flow: column; justify-content: start; align-items: center; align-content: center; gap: 8px; }
  .info-entry { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 640px) {
  body.ecg-app { font-size: 15px; }
  .stj-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .stage-main { gap: 10px; }
  .ecg-top { flex-wrap: wrap; row-gap: 6px; }
  /* Stack the wordmark so the brand and the three actions share one row. */
  .ecg-brand-name { font-size: 16px; line-height: 1.05; }
  .ecg-brand-sub { display: block; font-size: 10.5px; font-weight: 800; letter-spacing: 0.24em; text-transform: uppercase; color: var(--accent); margin-top: 2px; }
  .ecg-modes { order: 3; width: 100%; margin: 0; }
  .ecg-modes .seg { display: flex; }
  .ecg-modes .seg > button { flex: 1; padding-inline: 6px; }
  .ecg-top-actions { margin-left: auto; }
  .ecg-main { padding-top: 10px; gap: 10px; }
  .ecg-scenario { gap: 8px; }
  .pick, .rate { min-height: 54px; padding: 7px 30px 7px 12px; }
  .rate { padding-right: 12px; }
  .pick::after { right: 12px; }
  .monitor-bar { gap: 6px; padding: 8px; }
  .monitor-bar .seg > button { padding-inline: 9px; }
  .quiz-options { grid-template-columns: minmax(0, 1fr); }
  dialog.sheet { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 20px 20px 0 0; max-height: 88dvh; border-bottom: 0; }
  .sheet-foot { padding-bottom: max(14px, env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}

/* --- Tooltips ---------------------------------------------------------------------- */

.tip {
  position: fixed; z-index: 90; max-width: min(280px, calc(100vw - 12px)); padding: 7px 10px; pointer-events: none;
  display: flex; align-items: baseline; gap: 8px;
  border-radius: 9px; background: var(--ink); color: var(--surface); font-size: 13px; font-weight: 550; line-height: 1.35;
  box-shadow: 0 8px 24px rgba(3, 14, 32, 0.28);
}
.tip::after {
  content: ""; position: absolute; left: var(--tip-arrow, 50%); top: 100%; margin-left: -5px;
  border: 5px solid transparent; border-top-color: var(--ink);
}
.tip.is-below::after { top: auto; bottom: 100%; border-top-color: transparent; border-bottom-color: var(--ink); }
.tip kbd {
  flex: none; padding: 0 6px; border-radius: 5px; font-family: var(--mono); font-size: 11.5px; font-weight: 700;
  background: color-mix(in srgb, var(--surface) 22%, transparent); border: 1px solid color-mix(in srgb, var(--surface) 35%, transparent);
}
.readouts > div[data-tip], .stj-cell[data-tip] { cursor: help; }

/* --- Touch targets -------------------------------------------------------------------- */

@media (pointer: coarse) {
  .seg > button, .chip-btn, .select select { min-height: 44px; }
  .play-btn { min-height: 44px; }
  .icon-btn { width: 44px; height: 44px; }
  .lead-tag { min-height: 32px; min-width: 40px; line-height: 30px; }
  .lead-tags.is-dense .lead-tag { min-height: 26px; line-height: 24px; padding: 0 6px; }
  .lead-menu button { min-height: 40px; }
  .link-btn { padding: 8px 6px; }
}

/* A control that cannot be used says why instead of only fading. */
.pick:disabled { cursor: not-allowed; opacity: 1; background: var(--surface-2); }
.pick:disabled .pick-value { color: var(--muted); font-weight: 600; }
.pick:disabled::after { opacity: 0.35; }

.quiz-patient { font-size: 13.5px; color: var(--muted); }
.quiz-patient strong { color: var(--ink); }

/* Paused paper can be measured by dragging. */
.paper-wrap.is-measurable #paper { cursor: crosshair; touch-action: none; }
@media (max-width: 370px) {
  .ecg-top { column-gap: 6px; }
  .ecg-brand { gap: 8px; }
  .ecg-brand img { width: 26px; height: 26px; }
  .ecg-brand-name { font-size: 14px; }
  .ecg-top-actions { gap: 0; }
  .ecg-top-actions .icon-btn { width: 36px; }
}

/* Keep an empty picker value one line tall so the toolbar does not grow when it fills. */
.pick-value:empty::before { content: "\00a0"; }

/* Until app.mjs has built the lab, keep the main area laid out but unseen, so
   panels do not jump as the readouts, ST table and canvases fill in. Without
   scripting this never applies. */
@media (scripting: enabled) { html:not(.ecg-ready) .ecg-main { visibility: hidden; } }

/* Readouts sit on a grid, so how many rows they take depends on the width only,
   not on the values, and the strip below never moves while the rhythm plays. */
.readouts { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.readouts > div { min-width: 0; white-space: nowrap; }
.readouts > div dd { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
/* The axis row carries its classification (and Reset), so it takes two
   columns and the label is never cut short. */
.readouts > .axis { grid-column: span 2; }
@media (max-width: 400px) { .readouts > .axis { grid-column: 1 / -1; } }

/* --- Rules: disabled picker items, notices, calculators ------------------------ */

.sheet-item.is-disabled { cursor: not-allowed; color: var(--muted); }
.sheet-item.is-disabled:hover { background: transparent; }
.sheet-item.is-disabled .mark { opacity: 0.45; }
.sheet-item .sheet-item-note { color: var(--warn); font-weight: 600; margin-top: 2px; }

.menu-tools { margin: 0 -6px; padding-bottom: 0; border-bottom: 0; }
.menu-tools .menu-row { min-width: 0; }

.stj-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; padding-top: 10px; border-top: 1px solid var(--line); }
.stj-tools-label { font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); margin-right: 2px; }

.info-notice { padding: 10px 14px; border-radius: 12px; border: 1px solid color-mix(in srgb, var(--warn) 50%, var(--line)); background: color-mix(in srgb, var(--warn) 10%, var(--surface)); }
.info-notice strong { display: block; margin-bottom: 4px; font-size: 13px; letter-spacing: 0.02em; }
.info-notice p { margin: 0; }
.info-notice p + p { margin-top: 6px; }

.rule-verdict { padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2); }
.rule-verdict.is-danger { border-color: color-mix(in srgb, var(--danger) 55%, var(--line)); background: var(--danger-soft); }
.rule-verdict.is-ok { border-color: color-mix(in srgb, var(--ok) 55%, var(--line)); background: var(--ok-soft); }
.rule-history { color: var(--muted); font-size: 13.5px; margin-top: 8px !important; }
.rule-open { margin-top: 12px; }
.calc-summary { margin-top: 10px; }

dialog.sheet.calc { width: min(640px, calc(100vw - 24px)); max-height: min(860px, calc(100dvh - 24px)); }
.calc-body { padding: 0 18px 14px; display: flex; flex-direction: column; gap: 14px; overflow-y: auto; overscroll-behavior: contain; min-height: 0; }
.calc-body > * { flex: none; }
.calc-intro { margin: 0; color: var(--muted); }
.calc-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.calc-status { margin: 0; font-size: 13px; color: var(--muted); }
.calc-status:empty { display: none; }
.calc-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.calc-field { display: grid; gap: 4px; align-content: start; }
.calc-field label { font-weight: 700; font-size: 14px; }
.calc-field small { color: var(--muted); font-size: 12.5px; }
.calc-input { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.calc-input:focus-within { border-color: var(--primary); outline: 3px solid var(--focus); outline-offset: 1px; }
.calc-input input { flex: 1; min-width: 0; min-height: 42px; padding: 6px 10px; border: 0; background: transparent; color: var(--ink); font: inherit; font-family: var(--mono); font-weight: 700; font-size: 16px; outline: none; }
.calc-unit { padding: 0 12px 0 4px; color: var(--muted); font-weight: 650; font-size: 13px; }
.calc-formula { margin: 0; font-family: var(--mono); font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.calc-result { padding: 12px 14px; border-radius: 14px; border: 1px solid var(--line); background: var(--surface-2); display: grid; gap: 6px; }
.calc-result.is-danger { border-color: color-mix(in srgb, var(--danger) 55%, var(--line)); background: var(--danger-soft); }
.calc-result.is-ok { border-color: color-mix(in srgb, var(--ok) 55%, var(--line)); background: var(--ok-soft); }
.calc-result.is-void { border-style: dashed; }
.calc-result.is-void strong { color: var(--muted); }
.calc-empty { margin: 0; color: var(--muted); }
.calc-score { display: grid; grid-template-columns: auto 1fr; align-items: baseline; gap: 2px 14px; }
.calc-score-label { grid-column: 1 / -1; font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.calc-score strong { font-family: var(--mono); font-size: 28px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.calc-verdict { font-weight: 700; }
.calc-second { margin: 0; font-size: 13.5px; color: var(--muted); }
.calc-scope { margin: 0; font-size: 12.5px; color: var(--muted); }
.calc-warning { margin: 4px 0 0; padding: 8px 10px; border-radius: 10px; background: var(--surface); border: 1px solid color-mix(in srgb, var(--warn) 55%, var(--line)); font-size: 13.5px; }
.calc-warning.is-mild { border-color: var(--line); }
.calc-caveat { padding: 12px 14px; border-radius: 14px; border: 1px solid var(--line); }
.calc-caveat h3 { margin: 0 0 6px; font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.calc-caveat ul { margin: 0; padding-left: 18px; display: grid; gap: 4px; font-size: 14px; }
.calc-credit { margin: 10px 0 0; font-size: 12.5px; color: var(--muted); }
.calc-yn, .calc-helper { border: 1px solid var(--line); border-radius: 14px; margin: 0; padding: 10px 14px 12px; display: grid; gap: 6px; }
.calc-yn legend, .calc-helper legend { padding: 0 4px; font-weight: 750; font-size: 14.5px; }
.calc-yn p { margin: 0; font-size: 13.5px; color: var(--muted); }
.calc-yn-row { display: flex; gap: 8px; }
.calc-yn-row label { position: relative; display: inline-flex; align-items: center; justify-content: center; min-width: 76px; min-height: 40px; padding: 6px 14px; border: 1px solid var(--line); border-radius: 10px; font-weight: 700; cursor: pointer; }
.calc-yn-row input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.calc-yn-row label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); color: var(--primary-strong); }
.calc-yn-row label:has(input:focus-visible) { outline: 3px solid var(--focus); outline-offset: 2px; }
.calc-ratio { display: block; font-weight: 700; font-size: 14px; }
.calc-ratio.is-danger { color: var(--danger); }
.calc-leads:empty { display: none; }
@media (max-width: 520px) {
  .calc-grid { grid-template-columns: minmax(0, 1fr); }
  .calc-body { padding: 0 14px 12px; }
}
