:root {
  --bg:#f4f5f7; --card:#fff; --ink:#1f2933; --muted:#6b7280; --line:#e4e7eb;
  --brand:#2563eb; --ok:#059669; --warn:#d97706; --bad:#dc2626;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  margin:0; background:var(--bg); color:var(--ink); font-size:14px; line-height:1.5;
}

/* ---- nav ---- */
nav {
  background:#111827; color:#fff; padding:0 20px; display:flex; gap:4px;
  align-items:center; height:48px; position:sticky; top:0; z-index:50;
}
nav strong { margin-right:20px; font-size:15px; letter-spacing:.2px; }
nav a {
  color:#9ca3af; text-decoration:none; font-size:13px; font-weight:500;
  padding:6px 12px; border-radius:6px;
}
nav a:hover { color:#fff; background:rgba(255,255,255,.08); }
nav a.active { color:#fff; background:rgba(255,255,255,.14); }
nav .status { margin-left:auto; font-size:11px; display:flex; gap:10px; }
nav .status .ok { color:#34d399; } nav .status .bad { color:#f87171; }

main { padding:20px; max-width:1500px; margin:0 auto; }
h1 { font-size:20px; margin:0 0 16px; }
h2 { font-size:13px; text-transform:uppercase; letter-spacing:.6px; color:var(--muted); margin:0; }
h3 { font-size:12px; text-transform:uppercase; letter-spacing:.5px; color:var(--muted); margin:16px 0 6px; }

/* ---- cards ---- */
.card {
  background:var(--card); border:1px solid var(--line); border-radius:10px;
  padding:16px; margin-bottom:14px;
}
.card-head {
  display:flex; align-items:center; gap:10px; margin-bottom:12px;
  padding-bottom:10px; border-bottom:1px solid var(--line);
}
.card-head .actions { margin-left:auto; display:flex; gap:8px; }

/* Nested groups get a coloured spine so depth reads at a glance. */
.card.group { border-left:3px solid var(--brand); }
.card.group.depth2 { border-left-color:#8b5cf6; margin-left:18px; background:#fbfaff; }
.card.group .card-head h2 { color:var(--ink); font-size:13px; }

/* ---- context strip ---- */
.context { padding:14px 16px; }
.ctx-grid { display:grid; grid-template-columns:repeat(auto-fit,minmax(190px,1fr)); gap:12px; }
.ctx-grid label {
  display:block; font-size:11px; text-transform:uppercase; letter-spacing:.5px;
  color:var(--muted); margin-bottom:4px; font-weight:600;
}

/* ---- layout ---- */
.split { display:grid; grid-template-columns:minmax(0,1fr) minmax(0,1fr); gap:16px; align-items:start; }
@media (max-width:1100px) { .split { grid-template-columns:1fr; } }
.output-card { position:sticky; top:68px; }

/* ---- fields ---- */
.fields { display:grid; gap:10px; }
.field label {
  display:flex; align-items:baseline; gap:6px; font-size:12px; font-weight:600;
  color:#374151; margin-bottom:3px;
}
.field .fkey {
  margin-left:auto; font-family:var(--mono); font-size:10px; color:#9ca3af; font-weight:400;
}
.req-dot { color:var(--bad); }
input, select, textarea {
  width:100%; padding:7px 9px; border:1px solid #d2d6dc; border-radius:6px;
  font:inherit; font-size:13px; background:#fff; color:var(--ink);
}
input:focus, select:focus { outline:none; border-color:var(--brand); box-shadow:0 0 0 3px rgba(37,99,235,.12); }
input.locked, input:disabled { background:#f3f4f6; color:#9ca3af; cursor:not-allowed; }
.field.needs input:not(.locked), .field.needs select { border-color:var(--warn); background:#fffbeb; }
.field.needs.filled input:not(.locked), .field.needs.filled select { border-color:var(--ok); background:#f0fdf4; }

/* ---- buttons ---- */
button {
  background:var(--brand); color:#fff; border:0; padding:8px 14px; border-radius:6px;
  cursor:pointer; font:inherit; font-size:13px; font-weight:600;
}
button:hover { filter:brightness(1.08); }
button.secondary { background:#e5e7eb; color:#374151; }
button.danger { background:var(--bad); }
button.link-danger {
  margin-left:auto; background:none; color:var(--bad); padding:2px 6px; font-size:12px; font-weight:500;
}
button.link-danger:hover { background:#fee2e2; }
button.add {
  display:block; width:100%; background:none; color:var(--brand);
  border:1px dashed #c7d2fe; padding:9px; margin-bottom:14px; font-weight:600;
}
button.add:hover { background:#eff6ff; filter:none; }
button.add.depth2 { margin-left:18px; width:calc(100% - 18px); border-color:#ddd6fe; color:#7c3aed; }
button.add.depth2:hover { background:#f5f3ff; }

/* ---- output ---- */
pre {
  background:#0f172a; color:#cbd5e1; padding:12px; border-radius:8px; font-family:var(--mono);
  font-size:11.5px; line-height:1.55; overflow-x:auto; white-space:pre-wrap;
  word-break:break-all; max-height:360px; overflow-y:auto; margin:0 0 10px;
}
pre.hl7 { color:#e2e8f0; }
pre.errs { background:#450a0a; color:#fecaca; }
#output.empty { color:var(--muted); font-size:13px; padding:20px 0; text-align:center; }
.ack-row { display:flex; gap:8px; flex-wrap:wrap; margin-bottom:12px; }
.seg-count { font-size:11px; color:var(--muted); margin-bottom:6px; }

/* ---- misc ---- */
.tag { display:inline-block; padding:2px 8px; border-radius:999px; font-size:11px; font-weight:700; }
.tag.ok { background:#d1fae5; color:#065f46; }
.tag.bad { background:#fee2e2; color:#991b1b; }
.tag.info { background:#e0e7ff; color:#3730a3; }
.help { font-size:11.5px; color:var(--muted); margin-top:3px; }
.note {
  background:#eff6ff; border:1px solid #bfdbfe; color:#1e40af;
  padding:9px 12px; border-radius:8px; font-size:12px; margin-bottom:14px;
}
details { margin-top:12px; border-top:1px solid var(--line); padding-top:10px; }
details summary {
  cursor:pointer; font-size:12px; font-weight:600; color:var(--muted);
  display:flex; align-items:center; gap:6px;
}
details summary:hover { color:var(--ink); }
details[open] summary { margin-bottom:10px; }

table { width:100%; border-collapse:collapse; margin-top:8px; font-size:12.5px; }
th, td { text-align:left; padding:7px 9px; border-bottom:1px solid var(--line); }
th { background:#f9fafb; font-weight:600; color:var(--muted); font-size:11px;
     text-transform:uppercase; letter-spacing:.4px; }
tr:hover td { background:#fafbfc; }

/* ---- type-ahead ---- */
.nomen { position:relative; }
.nomen-menu {
  display:none; position:absolute; z-index:40; left:0; right:0; top:100%;
  background:#fff; border:1px solid #d2d6dc; border-radius:8px; margin-top:3px;
  max-height:280px; overflow-y:auto; box-shadow:0 8px 24px rgba(0,0,0,.14);
}
.nomen-item { padding:7px 10px; font-size:12px; cursor:pointer; border-bottom:1px solid #f3f4f6; }
.nomen-item:last-child { border-bottom:0; }
.nomen-item:hover { background:#eff6ff; }

/* ---- pki cards ---- */
.pki-card { border:1px solid var(--line); border-radius:8px; padding:12px; margin-bottom:10px; }
.pki-card.active { border-color:var(--brand); background:#eff6ff; }
.pki-card h4 { margin:0 0 4px; font-size:14px; }
.chip-list { display:flex; flex-wrap:wrap; gap:6px; margin:4px 0 8px; }
.chip {
  display:inline-flex; align-items:center; gap:2px; background:#eef2ff; color:#3730a3;
  border-radius:999px; padding:2px 6px 2px 10px; font-size:12px; white-space:nowrap;
}
.chip .x { cursor:pointer; opacity:.6; padding:0 4px; }
.chip .x:hover { opacity:1; }
.mono { font-family:var(--mono); font-size:10.5px; color:var(--muted); word-break:break-all; }
.grid2 { display:grid; grid-template-columns:1fr 1fr; gap:16px; align-items:start; }
.grid3 { display:grid; grid-template-columns:repeat(3,1fr); gap:16px; align-items:start; }
@media (max-width:1000px) { .grid2, .grid3 { grid-template-columns:1fr; } }
.panel { background:var(--card); border:1px solid var(--line); border-radius:10px; padding:16px; }
.panel h2 { margin-bottom:10px; }
form input { margin-bottom:8px; }

/* ---- reference data ---- */
.refdata-layout { display:grid; grid-template-columns:220px minmax(0,1fr); gap:16px; align-items:start; }
@media (max-width:860px) { .refdata-layout { grid-template-columns:1fr; } }
.refdata-sidebar { background:var(--card); border:1px solid var(--line); border-radius:10px; padding:10px; }
.refdata-group-label {
  font-size:11px; text-transform:uppercase; letter-spacing:.5px; color:var(--muted);
  font-weight:700; margin:12px 6px 4px;
}
.refdata-group-label:first-child { margin-top:2px; }
.refdata-set-btn {
  display:flex; justify-content:space-between; align-items:center; width:100%;
  background:none; color:var(--ink); font-weight:500; text-align:left;
  padding:8px 10px; border-radius:6px; margin-bottom:2px;
}
.refdata-set-btn:hover { background:#f3f4f6; filter:none; }
.refdata-set-btn.active { background:#eff6ff; color:var(--brand); font-weight:700; }
.refdata-toolbar { display:flex; gap:10px; margin-bottom:12px; }
.refdata-toolbar input { flex:1 1 auto; }
.refdata-toolbar button { flex:0 0 auto; }
.refdata-list { max-height:65vh; overflow-y:auto; display:flex; flex-direction:column; gap:8px; padding-right:4px; }
.refdata-card {
  border:1px solid var(--line); border-radius:8px; padding:10px 12px;
  display:flex; justify-content:space-between; align-items:flex-start; gap:10px;
}
.refdata-card-title { font-weight:600; font-size:13px; }
.refdata-card-actions { display:flex; gap:6px; flex:0 0 auto; }
.refdata-card-actions button { padding:5px 10px; font-size:12px; min-height:0; }
.refdata-form { border:1px solid var(--line); border-radius:8px; padding:12px; margin-bottom:12px; background:#fafbfc; }
.refdata-form .fields { grid-template-columns:repeat(auto-fit,minmax(160px,1fr)); margin-bottom:10px; }
.refdata-form-actions { display:flex; gap:8px; }
@media (max-width:560px) {
  .refdata-card { flex-direction:column; }
  .refdata-card-actions { width:100%; }
  .refdata-card-actions button { flex:1 1 0; }
}

/* ===========================================================================
   Responsive / touch
   =========================================================================== */

/* Tables are the worst offender on narrow screens — wrap them so they scroll
   horizontally instead of blowing out the page width. */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-wrap table { min-width: 460px; }

/* Tablet and below: nav becomes a horizontal scroller rather than wrapping
   into two rows and pushing content down. */
@media (max-width: 860px) {
  nav {
    padding: 0 12px; gap: 2px;
    overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  nav::-webkit-scrollbar { display: none; }
  nav strong { margin-right: 10px; flex: 0 0 auto; }
  nav a { flex: 0 0 auto; padding: 6px 9px; }
  nav .status { display: none; }   /* cert badges are on the PKI page anyway */

  main { padding: 12px; }
  .card { padding: 13px; border-radius: 9px; margin-bottom: 11px; }
  .ctx-grid { grid-template-columns: 1fr 1fr; gap: 10px; }

  /* Stop the output panel pinning itself over a small viewport. */
  .output-card { position: static; }
  pre { max-height: 280px; font-size: 11px; }
}

/* Phones */
@media (max-width: 560px) {
  .ctx-grid { grid-template-columns: 1fr; }
  h1 { font-size: 18px; }

  /* Nested groups: trade the indent for screen width, keep the colour spine
     as the only depth cue. */
  .card.group.depth2 { margin-left: 0; border-left-width: 4px; }
  button.add.depth2 { margin-left: 0; width: 100%; }

  /* 16px is the threshold below which iOS Safari zooms in on focus. */
  input, select, textarea { font-size: 16px; padding: 10px; }

  /* Comfortable tap targets (Apple/Android guidance is ~44px). */
  button { min-height: 44px; padding: 11px 16px; }
  button.link-danger { min-height: 32px; }
  .nomen-item { padding: 11px 12px; font-size: 13px; }
  .nomen-menu { max-height: 50vh; }

  /* Field key drops below the label instead of fighting it for space. */
  .field label { flex-wrap: wrap; gap: 3px; }
  .field .fkey { margin-left: 0; width: 100%; }

  /* Action buttons go full width and stack under the heading. */
  .card-head { flex-wrap: wrap; }
  .card-head .actions { margin-left: 0; width: 100%; display: grid;
                        grid-template-columns: 1fr 1fr; gap: 8px; }
  .card-head .actions button { width: 100%; }

  pre { font-size: 10.5px; padding: 10px; }
  th, td { padding: 8px 7px; }
  .ack-row { gap: 6px; }
}

/* Respect a user's reduced-motion preference for the sticky/scroll behaviour. */
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}

/* query parameters + local validation */
.param-line { display:flex; gap:8px; align-items:center; }
.param-line input { flex:1 1 auto; }
.param-line button { flex:0 0 auto; min-height:0; padding:5px 10px; font-size:12px; }
.field.param label { margin-bottom:3px; }
.validation {
  background:#fef2f2; border:1px solid #fecaca; border-radius:8px;
  padding:10px 12px; margin-bottom:12px;
}
.validation table { margin-top:8px; font-size:11.5px; }
.validation th { background:transparent; }
.validation .mono { font-size:10.5px; }
@media (max-width:560px) {
  .param-line { flex-wrap:wrap; }
  .param-line button { width:100%; min-height:40px; }
}

/* complex query parameter blocks */
.param-group {
  border:1px solid var(--line); border-radius:8px; padding:10px 12px;
  background:#fafbfc;
}
.param-group.off { opacity:.55; }
.param-group-head {
  display:flex; align-items:center; gap:8px; margin-bottom:8px;
  padding-bottom:7px; border-bottom:1px solid var(--line);
}
.param-group-head strong { font-size:12.5px; }
.param-group-head .fkey { margin-left:auto; }
.param-group-head button { flex:0 0 auto; min-height:0; padding:4px 9px; font-size:11.5px; }
.param-group .fields { gap:8px; }
@media (max-width:560px) {
  .param-group-head { flex-wrap:wrap; }
  .param-group-head .fkey { margin-left:0; width:100%; }
  .param-group-head button { width:100%; min-height:38px; }
}
.tag.warn { background:#fef3c7; color:#92400e; }

/* ---- tabs ---- */
.tabs { margin-bottom: 12px; }
.tab-bar { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 10px; }
button.tab {
  background: none; color: var(--muted); border: 0; border-bottom: 2px solid transparent;
  border-radius: 0; padding: 7px 12px; font-size: 12.5px; font-weight: 600; min-height: 0;
}
button.tab:hover { color: var(--ink); background: #f9fafb; filter: none; }
button.tab.active { color: var(--brand); border-bottom-color: var(--brand); }

/* ---- lab report ---- */
.patient-banner {
  background: #f8fafc; border: 1px solid var(--line); border-left: 3px solid var(--brand);
  border-radius: 8px; padding: 10px 12px; margin-bottom: 12px;
}
.pb-name { font-size: 15px; font-weight: 700; }
.pb-meta { font-size: 12px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.pb-meta .sep { opacity: .5; }

.report { border: 1px solid var(--line); border-radius: 8px; padding: 12px; margin-bottom: 10px; }
.report-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.report-head h4 { margin: 0; font-size: 13.5px; }
.report-meta {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 4px 14px; margin-bottom: 10px;
}
.kv { font-size: 12px; display: flex; gap: 6px; }
.kv-k { color: var(--muted); flex: 0 0 auto; }
.kv-v { color: var(--ink); word-break: break-word; }

table.results td.val { font-weight: 700; font-variant-numeric: tabular-nums; }
table.results tr.hi   td.val { color: #b45309; }
table.results tr.lo   td.val { color: #1d4ed8; }
table.results tr.abn  td.val { color: #b45309; }
table.results tr.crit td.val { color: var(--bad); }
table.results tr.crit { background: #fef2f2; }

@media (max-width: 560px) {
  .report-meta { grid-template-columns: 1fr; }
  .tab-bar { overflow-x: auto; }
  button.tab { flex: 0 0 auto; min-height: 40px; }
}
pre.warns { background:#422006; color:#fde68a; }
/* Editable but builder-populated: greyed text, still typeable. */
input.locked-soft { color:#6b7280; background:#fcfcfd; }
input.locked-soft:focus { color:var(--ink); background:#fff; }

/* timestamp range: two inputs that the builder joins with '&' */
.range { display:flex; gap:8px; flex:1 1 auto; }
.range-part { display:flex; flex-direction:column; gap:2px; flex:1 1 0; min-width:0; }
.range-lab { font-size:10.5px; color:var(--muted); text-transform:uppercase; letter-spacing:.4px; }
.range-preview { font-family:var(--mono); font-size:10.5px; color:var(--muted); }
@media (max-width:560px) { .range { flex-direction:column; } }

/* ----- authentication ------------------------------------------------------ */

.login-page { display: flex; min-height: 100vh; align-items: center; justify-content: center; }
.login-wrap { width: 100%; max-width: 380px; padding: 20px; }
.login-card { display: flex; flex-direction: column; gap: 6px; }
.login-card h1 { margin: 0 0 2px; font-size: 20px; }
.login-card label { margin-top: 10px; }
.login-card button { margin-top: 16px; }
.login-err {
  margin-top: 12px; padding: 8px 10px; border-radius: 6px;
  background: #7f1d1d; color: #fee2e2; font-size: 13px;
}

/* Identity chip sits at the far right of the nav, after the status flags. */
.whoami {
  display: flex; align-items: center; gap: 10px;
  margin-left: 12px; font-size: 12px; white-space: nowrap;
}
.whoami-name { color: #9ca3af; }
.signout { color: #93c5fd; text-decoration: none; }
.signout:hover { text-decoration: underline; }

/* Disabled accounts stay visible but read as inactive. */
.row-off { opacity: .45; }

@media (max-width: 640px) {
  .whoami { width: 100%; margin: 6px 0 0; justify-content: space-between; }
}
