/* =========================================================================
   CTAS Assist — single-viewport clinical console.
   The page body never scrolls; only the case list and the panel bodies do.

   Themes are two selected palettes, not one flipped palette. The CTAS level
   colours in particular are re-stepped for the light surface: a naive flip put
   the yellow at 1.77:1 against white and the orange and green under 3:1.
   ========================================================================= */

:root,
:root[data-theme="dark"] {
  --bg:        #0b0d11;
  --panel:     #14181f;
  --panel-2:   #1a1f28;
  --panel-3:   #0e1218;
  --head:      #11151c;
  --line:      #252b36;
  --line-soft: #1e232c;
  --text:      #e7eaf0;
  --muted:     #8b93a4;
  --faint:     #5e6675;
  --accent:    #38bdf8;
  --accent-dim:#0d3d52;
  --accent-bd: #1595c2;
  --accent-tx: #bfe9fb;
  --focus:     rgba(56, 189, 248, .12);

  /* CTAS severity scale for the DARK surface.
     Validated: adjacent normal-vision dE >= 17.0, CVD dE >= 10.5, all >= 3:1
     contrast. Level I is #c81e2b rather than a lighter red because against the
     Level II orange the lighter red separated by only dE 9.8 — and I vs II is
     the worst pair in the set to confuse. */
  --l1: #c81e2b;
  --l2: #f76b15;
  --l3: #e5b800;
  --l4: #46a758;
  --l5: #5b8dd9;
  --mark-ring: rgba(0, 0, 0, .35);

  --ok:      #46a758;
  --ok-bg:   #0f2417;
  --ok-bd:   #1f4a2c;
  --ok-tx:   #7ee2a8;
  --warn:    #e5b800;
  --warn-bg: #221e0d;
  --warn-bd: #4a4118;
  --bad:     #e5484d;
  --bad-bg:  #241113;
  --bad-bd:  #5a2124;
  --ovr-bg:  #3a1f10;
  --ovr-bd:  #6b3a1b;
  --ovr-tx:  #f0ab7a;

  --shadow: none;
}

:root[data-theme="light"] {
  --bg:        #eef1f5;
  --panel:     #ffffff;
  --panel-2:   #f4f6f9;
  --panel-3:   #f7f9fb;
  --head:      #f4f6f9;
  --line:      #d6dbe3;
  --line-soft: #e4e8ee;
  --text:      #131820;
  --muted:     #5a6473;
  --faint:     #8b939f;
  --accent:    #0b74a8;
  --accent-dim:#dff0fa;
  --accent-bd: #0b74a8;
  --accent-tx: #08556f;
  --focus:     rgba(11, 116, 168, .16);

  /* Re-stepped for the light surface, not flipped.
     Validated: adjacent normal-vision dE >= 21.5, CVD dE >= 18.7.
     The Level III amber sits below 3:1 against the surface, which the method
     allows only with relief — every mark carries its Roman numeral and count,
     marks are outlined (--mark-ring), and a table view of the chart exists. */
  --l1: #a8121c;
  --l2: #e2700c;
  --l3: #f5cc2e;
  --l4: #1d7a42;
  --l5: #2158a8;
  --mark-ring: rgba(0, 0, 0, .22);

  --ok:      #1d7a42;
  --ok-bg:   #e6f4ec;
  --ok-bd:   #a9d6bd;
  --ok-tx:   #10603a;
  --warn:    #9a6b00;
  --warn-bg: #fbf2dc;
  --warn-bd: #e0c98a;
  --bad:     #b3161f;
  --bad-bg:  #fbe9ea;
  --bad-bd:  #e5b4b7;
  --ovr-bg:  #fdefe2;
  --ovr-bd:  #eec4a1;
  --ovr-tx:  #9c4a12;

  --shadow: 0 1px 2px rgba(16, 24, 40, .05);
}

:root {
  --r: 10px;
  /* Bahnschrift first: a technical, slightly condensed grotesque that suits an
     instrument panel and fits long clinical labels in narrow columns. Falls
     back cleanly on platforms without it. */
  --font: "Bahnschrift", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --mono: "Consolas", "Cascadia Mono", "SF Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

/* The UA rule for [hidden] is `display: none`, but ANY author rule that sets
   `display` on the same element outranks it — `.result { display: flex }` meant
   an empty result skeleton rendered on a fresh page even though the element
   carried the hidden attribute. Make hidden actually mean hidden. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;               /* the page itself never scrolls */
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  display: grid;
  grid-template-rows: auto 1fr auto;
  -webkit-font-smoothing: antialiased;
  transition: background-color .15s, color .15s;
}

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

/* ── dashboard ─────────────────────────────────────────────────────────── */
.dashboard {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 22px;
  padding: 9px 16px;
  background: var(--head);
  border-bottom: 1px solid var(--line);
}

.kpis { display: flex; gap: 26px; align-items: flex-end; }

.kpi b {
  display: block;
  font-family: var(--mono);
  font-size: 21px;
  font-weight: 600;
  line-height: 1;
  letter-spacing: -.4px;
}

.kpi em {
  display: block;
  font-style: normal;
  font-size: 10.5px;
  color: var(--faint);
  margin-top: 4px;
  letter-spacing: .3px;
  white-space: nowrap;
}

.kpi-good { color: var(--ok); }
.kpi-warn { color: var(--ovr-tx); }

.dash-right { display: flex; align-items: center; gap: 16px; }

.theme-toggle {
  background: var(--panel-3);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 8px;
  width: 34px; height: 34px;
  font-size: 15px;
  cursor: pointer;
  display: grid; place-items: center;
  transition: .12s;
}

.theme-toggle:hover { color: var(--accent); border-color: var(--accent-bd); }

/* level distribution — thin marks, 2px surface gaps, direct numeral labels */
.chart { margin: 0; display: flex; flex-direction: column; gap: 6px; }

.chart figcaption {
  font-size: 9.5px;
  color: var(--faint);
  letter-spacing: .5px;
  text-transform: uppercase;
  font-family: var(--mono);
}

.bars { display: flex; align-items: flex-end; gap: 2px; height: 40px; }

.bar-col {
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  gap: 3px; width: 34px; height: 100%; cursor: default;
}

.bar-val { font-family: var(--mono); font-size: 10px; color: var(--muted); line-height: 1; }

.bar {
  width: 100%;
  min-height: 2px;
  background: var(--lvl, var(--line));
  border: 1px solid var(--mark-ring);      /* relief for the low-contrast amber */
  border-radius: 3px 3px 0 0;
  transition: height .25s ease;
}

.bar-key { font-family: var(--mono); font-size: 9.5px; color: var(--faint); line-height: 1; letter-spacing: .3px; }
.bar-col:hover .bar-val { color: var(--text); }

/* ── layout ────────────────────────────────────────────────────────────── */
.layout {
  display: grid;
  grid-template-columns: 372px minmax(0, 1fr) 296px;
  gap: 12px;
  padding: 12px;
  min-height: 0;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r);
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 0;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.panel-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 13px;
  border-bottom: 1px solid var(--line-soft);
  background: var(--head);
}

.panel-head h2 {
  margin: 0;
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: .5px;
  color: var(--muted);
  text-transform: uppercase;
}

.panel-body { padding: 11px 13px; min-height: 0; overflow-y: auto; }

/* ── form ──────────────────────────────────────────────────────────────── */
.panel-input .panel-form {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  min-height: 0;
}

.panel-input .panel-body { display: flex; flex-direction: column; gap: 9px; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; position: relative; }

.field > span {
  font-size: 11px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 6px;
}

.field em, .vital em {
  font-style: normal;
  font-size: 9.5px;
  color: var(--faint);
  font-family: var(--mono);
}

.field input, .field textarea, .vital input {
  background: var(--panel-3);
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--text);
  padding: 7px 9px;
  font-size: 13px;
  font-family: inherit;
  width: 100%;
  transition: border-color .12s, box-shadow .12s;
}

.field textarea { resize: none; line-height: 1.55; font-size: 12.5px; }

.field input:focus, .field textarea:focus, .vital input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--focus);
}

.field input::placeholder, .field textarea::placeholder { color: var(--faint); opacity: .65; }

.field-row { display: grid; gap: 8px; }
.cols-age { grid-template-columns: 1fr auto auto; }
.cols-2   { grid-template-columns: auto 1fr; }

.field-grow { flex: 0 0 auto; }
.field-grow textarea { height: 62px; }

.expansion {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--faint);
  line-height: 1.5;
  margin-top: 5px;
  padding-left: 9px;
  border-left: 2px solid var(--line);
}

.section-label {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  font-size: 11px;
  color: var(--muted);
  border-top: 1px solid var(--line-soft);
  padding-top: 8px;
}

.section-label em { font-style: normal; font-size: 9.5px; color: var(--faint); }

/* ── option chips (replace dropdowns: one tap, no menu) ────────────────── */
.chips { display: flex; gap: 4px; }
.chips-wrap { flex-wrap: wrap; }

.chip-opt {
  flex: 1; min-width: 0;
  background: var(--panel-3);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 7px;
  padding: 7px 9px;
  font-size: 12px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: .12s;
}

.chips-wrap .chip-opt { flex: 0 1 auto; font-weight: 500; font-size: 11.5px; }
.chip-opt:hover { border-color: var(--accent-bd); color: var(--text); }

.chip-opt[aria-pressed="true"] {
  background: var(--accent-dim);
  border-color: var(--accent-bd);
  color: var(--accent-tx);
}

.chip-lvl { flex: 0 0 auto; min-width: 36px; font-family: var(--mono); }
.chip-lvl[aria-pressed="true"] {
  background: color-mix(in srgb, var(--lvl) 20%, transparent);
  border-color: var(--lvl);
  color: var(--lvl);
}

/* ── vitals: label left, value right — the way the paper chart reads ───── */
.vitals-list { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; }

/* Two columns only. A third column for the unit truncated the labels
   ("Respiration Rate"), and a unit rendered inside a populated field truncated
   the values ("36" for 36.8) — so the unit is the placeholder instead. */
.vital {
  display: grid;
  grid-template-columns: 1fr 78px;
  align-items: center;
  gap: 6px;
}

.vital > span {
  font-size: 10.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -.05px;
}

.vital-in { position: relative; display: block; }

.vital input {
  padding: 5px 16px 5px 7px;
  font-size: 12.5px;
  text-align: right;
  font-family: var(--mono);
}

/* the placeholder carries the unit, so it reads as a hint not as a value */
.vital input::placeholder { font-size: 10px; color: var(--faint); opacity: .7; }

.vital-derived input { color: var(--muted); background: var(--panel-2); cursor: default; }

.flag {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  font-family: var(--mono);
  font-size: 9px;
  font-weight: 700;
  font-style: normal;
  line-height: 1;
  pointer-events: none;
  color: var(--bad);
}

.vital input.abnormal { border-color: var(--bad-bd); }

/* ── buttons ───────────────────────────────────────────────────────────── */
/* Outside the scrolling body as a real panel footer — as a sticky element
   inside the scroll area it overlaid the note box on short viewports. */
.form-actions {
  display: flex; gap: 8px;
  padding: 10px 13px;
  border-top: 1px solid var(--line-soft);
  background: var(--head);
}

.btn {
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 7px;
  padding: 9px 14px;
  font-size: 13px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: .12s;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}

.btn:hover:not(:disabled) { border-color: var(--accent-bd); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn-primary {
  flex: 1;
  background: var(--accent);
  border-color: var(--accent-bd);
  color: #fff;
}

.btn-primary:hover:not(:disabled) { filter: brightness(1.1); }

.btn-ghost { color: var(--muted); }
.btn-accept   { background: var(--ok-bg);  border-color: var(--ok-bd);  color: var(--ok-tx); }
.btn-override { background: var(--ovr-bg); border-color: var(--ovr-bd); color: var(--ovr-tx); }

.spinner {
  width: 13px; height: 13px;
  border: 2px solid rgba(255,255,255,.28);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.link-btn {
  background: none; border: none; color: var(--muted);
  font-size: 15px; cursor: pointer; padding: 0 4px; line-height: 1;
  font-family: inherit;
}

.link-btn:hover { color: var(--accent); }
.link-strong { font-size: 11.5px; font-weight: 650; color: var(--accent); }

/* after a decision is recorded, point at the next step */
.link-strong.nudge {
  background: var(--accent-dim);
  border: 1px solid var(--accent-bd);
  border-radius: 6px;
  padding: 3px 8px;
  animation: nudge 1.4s ease-in-out 3;
}

@keyframes nudge {
  0%, 100% { opacity: 1; }
  50%      { opacity: .45; }
}

@media (prefers-reduced-motion: reduce) {
  .link-strong.nudge { animation: none; }
}

/* ── result ────────────────────────────────────────────────────────────── */
.panel-result .panel-body { display: flex; flex-direction: column; }

.latency { font-family: var(--mono); font-size: 10.5px; color: var(--faint); }
.empty-state { flex: 1; }

.result { display: flex; flex-direction: column; gap: 12px; flex: 1; min-height: 0; }

.level-card {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 14px 16px;
  border-radius: var(--r);
  border: 1px solid var(--lvl, var(--line));
  background:
    linear-gradient(90deg, color-mix(in srgb, var(--lvl, #888) 14%, transparent), transparent 70%),
    var(--panel-2);
}

.level-roman {
  font-size: 42px; font-weight: 700; line-height: 1;
  font-family: var(--mono);
  color: var(--lvl, var(--text));
  min-width: 62px; text-align: center;
}

.level-name { font-size: 18px; font-weight: 650; letter-spacing: .2px; }
.level-sub {
  font-size: 10px; color: var(--faint); font-family: var(--mono);
  margin-top: 3px; letter-spacing: .6px; text-transform: uppercase;
}

.level-targets {
  display: flex; gap: 14px; text-align: center;
  border-left: 1px solid var(--line); padding-left: 14px;
}

.level-targets em { display: block; font-style: normal; font-size: 10px; color: var(--faint); }
.level-targets b  { display: block; font-size: 12px; font-family: var(--mono); margin-top: 3px; white-space: nowrap; }

.output-fields { margin: 0; display: flex; flex-direction: column; gap: 1px; }

.of { background: var(--panel-2); padding: 8px 12px; border-left: 2px solid var(--line); }
.of:first-child { border-radius: 8px 8px 0 0; }
.of:last-child  { border-radius: 0 0 8px 8px; }

.of dt { font-family: var(--mono); font-size: 9.5px; letter-spacing: .6px; color: var(--faint); margin-bottom: 3px; }
.of dd { margin: 0; font-size: 12.5px; line-height: 1.6; word-break: break-word; }

.of-inline { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.of-inline dt { margin: 0; }
.of-meta dd { font-family: var(--mono); font-size: 10.5px; color: var(--faint); }

.of-hit { border-left-color: var(--warn); }
.of-none dd { color: var(--faint); }

.badge { font-family: var(--mono); font-size: 10.5px; padding: 3px 10px; border-radius: 20px; border: 1px solid var(--line); }
.badge-HIGH   { color: var(--ok);   border-color: var(--ok-bd);   background: var(--ok-bg); }
.badge-MEDIUM { color: var(--warn); border-color: var(--warn-bd); background: var(--warn-bg); }
.badge-LOW    { color: var(--bad);  border-color: var(--bad-bd);  background: var(--bad-bg); }

/* decision */
.decision { border-top: 1px solid var(--line-soft); padding-top: 11px; margin-top: auto; }

.decision-row {
  display: grid;
  grid-template-columns: auto auto auto 1fr auto;
  gap: 8px;
  align-items: center;
}

.decision-or { font-size: 11px; color: var(--faint); white-space: nowrap; }

.override-reason {
  background: var(--panel-3); border: 1px solid var(--line); border-radius: 7px;
  color: var(--text); padding: 8px 9px; font-size: 12.5px; font-family: inherit; min-width: 0;
}

.override-reason:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }
.override-reason::placeholder { color: var(--faint); opacity: .65; }

.decision-done {
  margin: 9px 0 0; font-size: 12px; color: var(--ok);
  background: var(--ok-bg); border: 1px solid var(--ok-bd); border-radius: 7px; padding: 7px 11px;
}

.decision.settled .decision-row { opacity: .45; pointer-events: none; }

/* ── case list ─────────────────────────────────────────────────────────── */
.panel-log { grid-template-rows: auto auto 1fr; }

.log-search { padding: 7px 8px; border-bottom: 1px solid var(--line-soft); }

.log-search input {
  width: 100%;
  background: var(--panel-3);
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--text);
  padding: 6px 9px;
  font-size: 12px;
  font-family: inherit;
}

.log-search input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }
.log-search input::placeholder { color: var(--faint); opacity: .7; }

.load-more {
  width: 100%;
  margin-top: 4px;
  background: none;
  border: 1px dashed var(--line);
  border-radius: 7px;
  color: var(--muted);
  padding: 5px;
  font-size: 10.5px;
  font-family: var(--mono);
  cursor: pointer;
}

.load-more:hover { color: var(--accent); border-color: var(--accent-bd); }

.log-scroll { overflow-y: auto; min-height: 0; padding: 6px; }
.log-group + .log-group { margin-top: 10px; }

.log-group-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  margin: 0 0 5px;
  padding: 3px 6px;
  font-size: 9.5px; font-weight: 650; letter-spacing: .6px;
  text-transform: uppercase; font-family: var(--mono);
  border-left: 2px solid var(--line);
}

.log-head-pending { color: var(--warn); border-left-color: var(--warn); }
.log-head-decided { color: var(--faint); }

.log-group-head .count {
  font-size: 9.5px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 1px 7px;
  color: var(--muted);
}

.log-empty { color: var(--faint); font-size: 11.5px; text-align: center; padding: 12px; margin: 0; }

.log-item {
  display: grid; grid-template-columns: 28px 1fr auto;
  gap: 8px; align-items: center;
  padding: 7px 8px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: .12s;
}

.log-item:hover { background: var(--panel-2); border-color: var(--line); }

.log-lvl {
  font-family: var(--mono); font-weight: 700; font-size: 13px; text-align: center;
  border-radius: 6px; padding: 3px 0;
  background: color-mix(in srgb, var(--lvl, #888) 16%, transparent);
  color: var(--lvl, var(--muted));
  border: 1px solid color-mix(in srgb, var(--lvl, #888) 45%, transparent);
}

.log-main { min-width: 0; }
.log-summary { font-size: 11.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.log-sub { font-size: 9.5px; color: var(--faint); font-family: var(--mono); margin-top: 2px; display: flex; gap: 6px; }

.log-mark { font-size: 10.5px; white-space: nowrap; font-family: var(--mono); }
.mark-accepted   { color: var(--ok); }
.mark-overridden { color: var(--ovr-tx); }

/* ── status bar ────────────────────────────────────────────────────────── */
.statusbar {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 5px 16px;
  border-top: 1px solid var(--line);
  background: var(--head);
  font-size: 11px; color: var(--muted);
}

.statusbar-note { color: var(--faint); font-size: 10.5px; }

/* ── scrollbars ────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ── tighter viewports ─────────────────────────────────────────────────── */
@media (max-width: 1280px) {
  .layout { grid-template-columns: 344px minmax(0, 1fr); }
  .panel-log { display: none; }
}

@media (max-height: 850px) {
  .dashboard { padding: 7px 16px; }
  .kpi b { font-size: 18px; }
  .kpis { gap: 20px; }
  .bars { height: 30px; }
  .layout { gap: 10px; padding: 10px; }
  .panel-input .panel-body { gap: 7px; }
  .vitals-list { gap: 3px 10px; }
  .vital > span { font-size: 10.5px; }
  .level-roman { font-size: 34px; }
  .level-card { padding: 10px 14px; }
  .field-grow textarea { height: 52px; }
  .form-actions { padding: 8px 13px; }
}
