/* cad-ems console.
 *
 * Hand-written, no framework, no web fonts. Two reasons beyond taste: this repo has no
 * node_modules and adding a build step means the console stops rendering for whoever has
 * to fix something at 03:00, and a stylesheet fetched from a CDN is a third party who can
 * change what a dispatch screen looks like -- or fail to load, on the night the network is
 * the thing that is broken.
 *
 * Dark by default because this runs on a wall or a desk in a dim room for twelve hours.
 * Colour is never the only carrier of meaning: priority is a number as well as a hue, and
 * status is a word as well as a dot, for the ~8% of men with a colour vision deficiency
 * who work dispatch like everyone else.
 */

:root {
  --bg: #0d1117;
  --bg-raised: #151b23;
  --bg-inset: #0a0e14;
  --line: #232b36;
  --line-bright: #323d4d;

  --ink: #e6edf3;
  --ink-dim: #9aa7b4;
  --ink-faint: #6b7785;

  --accent: #4cc2ff;
  --accent-dim: #1b4d66;

  /* Priority. 1 reads as alarm, 5 as routine, and the steps between are distinguishable
   * in greyscale as well as in hue. */
  --p1: #ff4d4f;
  --p2: #ff8c42;
  --p3: #f5c542;
  --p4: #58a6ff;
  --p5: #8b949e;

  --good: #3fb950;
  --warn: #d29922;
  --bad: #f85149;

  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --radius: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .25);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

code, .mono { font-family: var(--mono); font-size: .92em; }
code { background: var(--bg-inset); padding: .1em .35em; border-radius: 4px; color: #c9d6e4; }

h1 { font-size: 20px; margin: 0 0 4px; font-weight: 650; letter-spacing: -.01em; }
h2 {
  font-size: 12px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--ink-dim); margin: 0 0 12px; font-weight: 600;
  display: flex; align-items: baseline; gap: 8px;
}
.hint { text-transform: none; letter-spacing: 0; color: var(--ink-faint); font-weight: 400; }

.muted { color: var(--ink-dim); margin: 0; }
.fineprint { color: var(--ink-faint); font-size: 12px; margin: 16px 0 0; max-width: 78ch; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------------ chrome */

.topbar {
  display: flex; align-items: center; gap: 20px;
  padding: 0 20px; height: 52px;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 40;
}

.brand { display: flex; align-items: baseline; gap: 8px; }
.brand:hover { text-decoration: none; }
.brand-mark {
  font-family: var(--mono); font-weight: 700; letter-spacing: .06em;
  color: var(--bg); background: var(--accent);
  padding: 2px 7px; border-radius: 4px; font-size: 13px;
}
.brand-mark.big { font-size: 18px; padding: 4px 10px; }
.brand-sub { color: var(--ink-faint); font-size: 12px; }

.nav { display: flex; gap: 2px; margin-left: 8px; }
.nav a {
  padding: 6px 12px; border-radius: 6px; color: var(--ink-dim); font-weight: 500;
}
.nav a:hover { background: #1c2530; color: var(--ink); text-decoration: none; }
.nav a.on { background: var(--accent-dim); color: #cdeaff; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }

.link-state { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-faint); }
.link-state .dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--ink-faint);
  box-shadow: 0 0 0 3px transparent;
}
.link-state[data-state="live"] .dot { background: var(--good); box-shadow: 0 0 0 3px rgba(63,185,80,.15); }
.link-state[data-state="connecting"] .dot { background: var(--warn); animation: pulse 1.4s infinite; }
.link-state[data-state="down"] .dot { background: var(--bad); box-shadow: 0 0 0 3px rgba(248,81,73,.15); }
.link-state[data-state="down"] { color: var(--bad); }
@keyframes pulse { 50% { opacity: .3; } }

.whoami { display: flex; align-items: center; gap: 8px; }
.whoami:hover { text-decoration: none; }
.whoami-name { color: var(--ink); font-weight: 500; }

.role {
  font-size: 10px; text-transform: uppercase; letter-spacing: .06em; font-weight: 700;
  padding: 2px 6px; border-radius: 4px; border: 1px solid;
}
.role-call_taker { color: #9aa7b4; border-color: #37414f; background: #1a212b; }
.role-supervisor { color: #ffd479; border-color: #6b5320; background: #2a2213; }
.role-admin      { color: #ff9c9c; border-color: #6e2b2b; background: #2b1717; }

.logout-form { margin: 0; }

/* ------------------------------------------------------------------ layout */

.wrap { padding: 20px; max-width: 1600px; margin: 0 auto; }
.wrap.narrow { max-width: 780px; }
.centre { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }

.page-head {
  display: flex; align-items: flex-start; gap: 24px;
  margin-bottom: 18px; flex-wrap: wrap;
}
.page-head .muted { max-width: 88ch; font-size: 13px; }
.page-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.card {
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.card-flat { background: transparent; }
.card-danger { border-color: #5a2222; background: #1a1214; }

.detail-cols { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(320px, .9fr); gap: 16px; }
.col-main, .col-side { min-width: 0; }
@media (max-width: 1080px) { .detail-cols { grid-template-columns: 1fr; } }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; align-items: start; }
.form-grid > .card-flat { grid-column: 1 / -1; }

.two-up { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stack { display: grid; gap: 12px; }

/* ------------------------------------------------------------------ forms */

label { display: block; }
label > span:first-child {
  display: block; font-size: 12px; font-weight: 600; color: var(--ink-dim);
  margin-bottom: 5px;
}
label + label, .card > label { margin-top: 14px; }
.req { color: var(--p2); font-style: normal; font-size: 10px; text-transform: uppercase; letter-spacing: .05em; }

input, select, textarea {
  width: 100%; padding: 8px 10px;
  background: var(--bg-inset); color: var(--ink);
  border: 1px solid var(--line-bright); border-radius: 6px;
  font: inherit;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent);
}
textarea { resize: vertical; }
input[type=checkbox] { width: auto; }
label small {
  display: block; color: var(--ink-faint); font-size: 12px; margin-top: 5px; max-width: 70ch;
}

fieldset { border: 0; padding: 0; margin: 14px 0 0; }
legend { font-size: 12px; font-weight: 600; color: var(--ink-dim); padding: 0; margin-bottom: 5px; }

.pri-row { display: flex; gap: 6px; }
.pri { flex: 1; }
.pri input { position: absolute; opacity: 0; pointer-events: none; }
.pri span {
  display: grid; place-items: center; height: 40px;
  border: 1px solid var(--line-bright); border-radius: 6px;
  font-family: var(--mono); font-size: 16px; font-weight: 700;
  background: var(--bg-inset); cursor: pointer;
}
.pri input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }
.pri-1 input:checked + span { background: var(--p1); border-color: var(--p1); color: #2b0000; }
.pri-2 input:checked + span { background: var(--p2); border-color: var(--p2); color: #2b1300; }
.pri-3 input:checked + span { background: var(--p3); border-color: var(--p3); color: #2b2200; }
.pri-4 input:checked + span { background: var(--p4); border-color: var(--p4); color: #001c3b; }
.pri-5 input:checked + span { background: var(--p5); border-color: var(--p5); color: #14181d; }

.form-actions {
  display: flex; align-items: center; gap: 12px; margin-top: 14px;
  flex-wrap: wrap;
}
.attribution { color: var(--ink-faint); font-size: 12px; margin-right: auto; }
.attribution strong { font-family: var(--mono); color: var(--ink-dim); font-weight: 600; }

.btn {
  padding: 8px 14px; border-radius: 6px; cursor: pointer;
  background: #202836; color: var(--ink);
  border: 1px solid var(--line-bright);
  font: inherit; font-weight: 500;
}
.btn:hover:not(:disabled) { background: #29323f; border-color: #445164; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #04121c; font-weight: 650; }
.btn-primary:hover:not(:disabled) { background: #6dcfff; border-color: #6dcfff; }
.btn-danger { background: #7d2020; border-color: #a02c2c; color: #ffdede; }
.btn-danger:hover:not(:disabled) { background: #a02c2c; }
.btn-quiet { background: transparent; border-color: transparent; color: var(--ink-dim); }
.btn-quiet:hover { background: #1c2530; border-color: var(--line-bright); }
.btn-wide { width: 100%; justify-self: stretch; }
.btn-tiny { padding: 3px 8px; font-size: 12px; }

/* ------------------------------------------------------------------ login */

.login { width: 100%; max-width: 400px; display: grid; gap: 14px; margin: 0; }
.login-head { text-align: center; display: grid; gap: 8px; justify-items: center; }
.login-head h1 { margin: 0; }
.login-head .muted { font-size: 12px; text-align: left; }

.banner { padding: 9px 12px; border-radius: 6px; font-size: 13px; margin: 0; border: 1px solid; }
.banner-bad { background: #2b1517; border-color: #6e2b2b; color: #ffb4b4; }
.banner-warn { background: #2a2213; border-color: #6b5320; color: #ffd479; }

/* ------------------------------------------------------------------ tables */

.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg-raised); }
.table-scroll.tall { max-height: 62vh; overflow-y: auto; }

table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
table.grid th {
  text-align: left; padding: 9px 12px; position: sticky; top: 0; z-index: 2;
  background: #1a212b; color: var(--ink-dim);
  font-size: 11px; text-transform: uppercase; letter-spacing: .05em; font-weight: 600;
  border-bottom: 1px solid var(--line-bright); white-space: nowrap;
}
table.grid td { padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.grid.tight td, table.grid.tight th { padding: 5px 10px; }
table.grid tbody tr:hover { background: #1a222d; }
table.grid tbody tr.clickable { cursor: pointer; }
table.grid tr.empty td { color: var(--ink-faint); text-align: center; padding: 28px; }
table.grid tr.empty:hover { background: transparent; }

.pri-cell {
  display: inline-grid; place-items: center; width: 24px; height: 24px;
  border-radius: 5px; font-family: var(--mono); font-weight: 700; font-size: 13px;
}
.pri-cell-1 { background: var(--p1); color: #2b0000; }
.pri-cell-2 { background: var(--p2); color: #2b1300; }
.pri-cell-3 { background: var(--p3); color: #2b2200; }
.pri-cell-4 { background: var(--p4); color: #001c3b; }
.pri-cell-5 { background: var(--p5); color: #14181d; }

.pill {
  display: inline-block; padding: 2px 7px; border-radius: 999px;
  font-size: 11px; font-weight: 600; border: 1px solid; white-space: nowrap;
}
.pill-open      { color: #a8e6b0; border-color: #2b5c33; background: #14251a; }
.pill-closed    { color: var(--ink-dim); border-color: #37414f; background: #1a212b; }
.pill-cancelled { color: #ffb4b4; border-color: #6e2b2b; background: #2b1717; }
.pill-sent      { color: #a8e6b0; border-color: #2b5c33; background: #14251a; }
.pill-pending   { color: #ffd479; border-color: #6b5320; background: #2a2213; }
.pill-failed    { color: #ffb4b4; border-color: #6e2b2b; background: #2b1717; }
.pill-applied   { color: #a8e6b0; border-color: #2b5c33; background: #14251a; }
.pill-rejected  { color: #ffb4b4; border-color: #6e2b2b; background: #2b1717; }
.pill-dropped_by_chaos { color: #e0b0ff; border-color: #58306e; background: #211628; }
.pill-diverting { color: #ffb4b4; border-color: #6e2b2b; background: #2b1717; }
.pill-accepting { color: #a8e6b0; border-color: #2b5c33; background: #14251a; }

.dir { font-family: var(--mono); font-size: 11px; font-weight: 700; }
.dir-out { color: var(--accent); }
.dir-in { color: #c58af9; }

.age { color: var(--ink-dim); font-variant-numeric: tabular-nums; white-space: nowrap; }
.age-stale { color: var(--warn); }

.confidence {
  font-size: 10px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--ink-faint); border: 1px dashed var(--line-bright);
  padding: 1px 5px; border-radius: 4px;
}
.confidence-gps, .confidence-phase_2 { color: #a8e6b0; border-color: #2b5c33; border-style: solid; }
.confidence-caller_report { color: var(--warn); border-color: #6b5320; }

/* No position at all. Deliberately the loudest of the four: this call cannot be offered
   until somebody places it, and the states that CAN be dispatched must not be shouting
   alongside it. Not colour alone -- it carries the words "not located" too. */
.confidence-unlocated {
  color: var(--danger, #ff8f80); border-color: currentColor; border-style: solid;
  font-weight: 700;
}
td.unlocated, .unlocated { color: var(--danger, #ff8f80); }

/* "Nobody has been told about this call." The loudest thing on the incident page,
   because it is the one state where the dispatcher's mental model and reality differ. */
.offer-blocked {
  margin: 0; padding: 8px 10px; font-size: 12px; line-height: 1.45;
  color: var(--danger, #ff8f80);
  background: rgba(255, 143, 128, .07);
  border: 1px solid currentColor; border-radius: 4px;
}
.offer-blocked strong { display: block; margin-bottom: 2px; }

/* ------------------------------------------------------------------ filters */

.filters { display: flex; align-items: center; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.filters-spacer { flex: 1; }
.chip {
  padding: 5px 12px; border-radius: 999px; cursor: pointer;
  background: transparent; border: 1px solid var(--line-bright); color: var(--ink-dim);
  font: inherit; font-size: 13px;
}
.chip:hover { background: #1c2530; color: var(--ink); }
.chip.on { background: var(--accent-dim); border-color: #2f7391; color: #cdeaff; }
.inline-check { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-dim); }
.inline-check input { margin: 0; }
.count { color: var(--ink-faint); font-size: 12px; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------------ detail */

.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; }
.summary .field span { display: block; font-size: 11px; color: var(--ink-faint); text-transform: uppercase; letter-spacing: .05em; }
.summary .field strong { font-weight: 600; font-size: 14px; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 8px 20px; margin: 0; }
.kv dt { color: var(--ink-faint); font-size: 12px; }
.kv dd { margin: 0; }

.notes { list-style: none; padding: 0; margin: 0 0 16px; display: grid; gap: 8px; }
.notes li {
  background: var(--bg-inset); border-left: 2px solid var(--line-bright);
  border-radius: 0 6px 6px 0; padding: 8px 12px;
}
.notes .note-meta { font-size: 11px; color: var(--ink-faint); display: flex; gap: 10px; margin-top: 4px; }
.notes .note-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.notes .seq { font-family: var(--mono); color: var(--accent); }

.assignments { margin-top: 14px; display: grid; gap: 6px; }
.assign-row {
  display: flex; align-items: center; gap: 8px; font-size: 13px;
  padding: 6px 10px; background: var(--bg-inset); border-radius: 6px;
}
.assign-row .slot { font-family: var(--mono); color: var(--ink-faint); }
.assign-row .who { flex: 1; overflow-wrap: anywhere; }

.trip { border: 1px solid var(--line); border-radius: 6px; padding: 10px 12px; margin-bottom: 8px; background: var(--bg-inset); }
.trip-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.trip-head .call-sign { font-family: var(--mono); font-weight: 700; font-size: 15px; }

/* The ladder. Two gaps must never close: at_origin -> patient_contact is wheels-stopped
 * versus hands-on-patient, and at_destination -> transfer_of_care IS APOT. Rendering them
 * as one step would make the timeline unsupportable, so each rung is drawn. */
.ladder { display: flex; gap: 2px; flex-wrap: wrap; }
.rung {
  font-size: 10px; padding: 2px 6px; border-radius: 3px;
  background: #1a212b; color: var(--ink-faint); border: 1px solid var(--line);
  white-space: nowrap;
}
.rung.done { background: #14251a; color: #a8e6b0; border-color: #2b5c33; }
.rung.at { background: var(--accent); color: #04121c; border-color: var(--accent); font-weight: 700; }
.rung.apot-gap { border-left-width: 3px; border-left-color: var(--p2); }

.eta { font-size: 12px; color: var(--ink-dim); }
.eta .stale { color: var(--warn); }

.staging, .backup {
  font-size: 12px; padding: 6px 10px; border-radius: 5px; margin-top: 6px;
  border: 1px solid #6b5320; background: #2a2213; color: #ffd479;
}
.backup.unack { border-color: var(--bad); background: #2b1717; color: #ffb4b4; font-weight: 600; }

.map-holder { min-height: 120px; }
.map-canvas { height: 340px; border-radius: 6px; }
.coord-table { width: 100%; font-size: 13px; border-collapse: collapse; }
.coord-table th { text-align: left; color: var(--ink-faint); font-weight: 500; padding: 4px 10px 4px 0; font-size: 12px; }
.coord-table td { font-family: var(--mono); padding: 4px 0; }
.no-map {
  border: 1px dashed var(--line-bright); border-radius: 6px; padding: 12px;
  color: var(--ink-faint); font-size: 12px; margin-top: 10px;
}

/* ------------------------------------------------------------------ hospitals */

.hospital-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 16px; }
.hospital { background: var(--bg-raised); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.hospital.diverting { border-color: #6e2b2b; }
.hospital-head { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 10px; }
.hospital-head h3 { margin: 0; font-size: 15px; flex: 1; }
.caps { display: flex; gap: 4px; flex-wrap: wrap; margin: 10px 0; }
.cap { font-size: 10px; padding: 2px 6px; border-radius: 3px; background: #1a212b; color: var(--ink-dim); border: 1px solid var(--line); }
.diversion-note { font-size: 12px; color: #ffb4b4; margin: 8px 0; }
.expired-flag { color: var(--warn); font-size: 12px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--bg-raised); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat .n { font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; display: block; line-height: 1.2; }
.stat .l { font-size: 11px; color: var(--ink-faint); text-transform: uppercase; letter-spacing: .05em; }
.stat.bad .n { color: var(--bad); }
.stat.warn .n { color: var(--warn); }
.stat.good .n { color: var(--good); }

/* ------------------------------------------------------------------ chaos */

.toggle-row {
  display: flex; align-items: center; gap: 12px; padding: 8px 0;
  border-bottom: 1px solid var(--line);
}
.toggle-row:last-child { border-bottom: 0; }
.toggle-row .tname { font-family: var(--mono); font-size: 13px; flex: 1; }
.toggle-row .on-flag { color: var(--bad); font-size: 11px; font-weight: 700; text-transform: uppercase; }
.chaos-active { border: 1px solid var(--bad); border-radius: 6px; padding: 8px 12px; margin-bottom: 12px; color: #ffb4b4; font-size: 13px; }

.warn-inline { color: var(--warn); font-size: 12px; margin: 8px 0 0; max-width: 88ch; }
.phi-warning {
  border: 1px solid #6b5320; background: #2a2213; color: #ffd479;
  padding: 10px 12px; border-radius: 6px; font-size: 12px; margin: 0; max-width: none;
}

.perms td { font-size: 13px; }
.perms .yes { color: var(--good); font-weight: 700; }
.perms .no { color: var(--ink-faint); }

/* ------------------------------------------------------------------ toasts */

.toasts {
  position: fixed; bottom: 16px; right: 16px; z-index: 90;
  display: grid; gap: 8px; max-width: min(460px, calc(100vw - 32px));
}
.toast {
  background: var(--bg-raised); border: 1px solid var(--line-bright);
  border-left: 3px solid var(--accent);
  border-radius: 6px; padding: 10px 14px; box-shadow: var(--shadow);
  font-size: 13px; overflow-wrap: anywhere;
}
.toast-bad { border-left-color: var(--bad); }
.toast-good { border-left-color: var(--good); }
.toast .toast-detail { color: var(--ink-dim); font-size: 12px; margin-top: 3px; }

@media (prefers-reduced-motion: no-preference) {
  .toast { animation: slide-in .16s ease-out; }
  @keyframes slide-in { from { opacity: 0; transform: translateY(6px); } }
}

/* A row that just changed. Brief, and never the only signal -- the text changed too. */
@media (prefers-reduced-motion: no-preference) {
  tr.flash { animation: flash 1s ease-out; }
  @keyframes flash { from { background: var(--accent-dim); } }
}
