/* ============================================================
   Clinicify — screen styles
   Clinical, dense, quiet. 14px base, 8px spacing unit, 32px rows.
   ============================================================ */

:root {
  --unit: 8px;
  --row-h: 38px;
  --nav-h: 54px;
  --control-h: 34px;

  --ink:        #14181d;   /* the one dark text colour */
  --ink-2:      #4a5461;   /* secondary */
  --ink-3:      #79838f;   /* muted */
  --line:       #d9dee4;
  --line-2:     #e9edf1;
  --bg:         #f6f7f9;   /* near-white app background */
  --surface:    #ffffff;
  --surface-2:  #f0f2f5;

  --accent:     #1560c4;   /* active dose dots + primary action ONLY */
  --accent-ink: #ffffff;
  --accent-soft:#e8f0fb;

  --danger:     #b3261e;   /* destructive + validation ONLY */
  --danger-soft:#fceceb;

  --focus:      #1560c4;

  --font-ui: "Inter", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --font-doc: "Source Serif 4", "Iowan Old Style", Georgia, "Times New Roman", serif;

  --radius: 4px;
  --shadow: 0 1px 2px rgba(20, 24, 29, .06), 0 4px 12px rgba(20, 24, 29, .06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;          /* panes scroll, the page does not */
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 20px; letter-spacing: -.01em; }
h2 { font-size: 15px; }
h3 { font-size: 14px; }
p  { margin: 0; }

a { color: var(--accent); }

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

/* Focus is the primary interaction mode — never hide it. */
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
  border-radius: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    transition-duration: .001ms !important;
  }
}

/* ── Boot error ───────────────────────────────────────────── */
.boot-error {
  max-width: 46em; margin: 15vh auto; padding: calc(var(--unit) * 4);
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.boot-error h1 { margin-bottom: var(--unit); }
.boot-error p { margin: var(--unit) 0; color: var(--ink-2); }
.boot-error pre {
  background: var(--surface-2); padding: var(--unit) calc(var(--unit) * 1.5);
  border-radius: var(--radius); overflow-x: auto;
}
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; }

/* ── Shell ────────────────────────────────────────────────── */
/* The shell is exactly one viewport tall. Every screen below fills it,
   so no page ever ends in dead space. */
.app-shell { height: 100%; display: flex; flex-direction: column; }

.nav {
  display: flex; align-items: center; gap: calc(var(--unit) * 3);
  height: var(--nav-h); padding: 0 calc(var(--unit) * 2);
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 600; letter-spacing: -.01em; display: flex; align-items: center; gap: 6px; }
.brand-rx { font-family: var(--font-doc); font-size: 21px; color: var(--accent); }
.nav-links { display: flex; gap: 2px; }
.nav-links a {
  padding: 6px 12px; border-radius: var(--radius); text-decoration: none;
  color: var(--ink-2); font-weight: 500;
}
.nav-links a:hover { background: var(--surface-2); color: var(--ink); }
.nav-links a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }

.main {
  flex: 1; min-height: 0; width: 100%;
  padding: calc(var(--unit) * 2);
  display: flex; flex-direction: column;
}
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.view[hidden] { display: none; }
.view-head { flex: none; }

.view-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: var(--unit); margin-bottom: calc(var(--unit) * 1.5);
}
.view-actions { display: flex; gap: var(--unit); align-items: center; }
.back-link { font-size: 13px; text-decoration: none; }
.back-link:hover { text-decoration: underline; }

/* ── Cards ────────────────────────────────────────────────── */
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: calc(var(--unit) * 2);
  margin-bottom: calc(var(--unit) * 1.5);
}
/* Fills the space under the view header and scrolls its own rows, so the
   card reaches the bottom of the window however few rows it holds.
   overflow-y must stay `auto` (never `hidden`): this element is the
   header's scroll container, and `hidden` would break sticky again. */
.card-flush {
  padding: 0;
  flex: 1; min-height: 0;
  overflow-y: auto;
  margin-bottom: 0;
  display: flex; flex-direction: column;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: calc(var(--unit) * 1.5);
}
.sub-head { margin: calc(var(--unit) * 2) 0 var(--unit); color: var(--ink-3); font-weight: 500; }
.hint { color: var(--ink-3); font-size: 13px; margin-bottom: var(--unit); }

/* ── Buttons ──────────────────────────────────────────────── */
.btn {
  font: inherit; font-weight: 500; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 0 14px; height: var(--control-h);
  cursor: pointer; white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--surface-2); border-color: var(--ink-3); }
.btn:active { background: var(--line-2); }
.btn-sm { height: 30px; padding: 0 11px; font-size: 13.5px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: #0f4fa5; border-color: #0f4fa5; }
.btn-danger { color: var(--danger); border-color: var(--line); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-icon {
  width: 30px; height: 30px; padding: 0; display: inline-flex;
  align-items: center; justify-content: center; line-height: 1;
}
.btn-link {
  background: none; border: none; padding: 0 4px; height: auto;
  color: var(--accent); cursor: pointer; font: inherit; font-size: 13.5px;
}
.btn-link:hover { text-decoration: underline; }
.btn-link.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Fields ───────────────────────────────────────────────── */
.field { margin-bottom: calc(var(--unit) * 1.5); }
.field > label {
  display: block; font-size: 13px; font-weight: 500;
  color: var(--ink-2); margin-bottom: 4px;
}
.field-inline { display: flex; align-items: center; gap: var(--unit); }
.field-inline > label { margin: 0; width: 110px; flex: none; }
.field-inline > input { flex: 1; }

input[type="text"], input[type="tel"], input[type="email"], input[type="number"],
input[type="date"], input[type="search"], select, textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius);
  height: var(--control-h); padding: 0 9px; width: 100%; min-width: 0;
}
textarea { height: auto; padding: 6px 8px; resize: vertical; line-height: 1.45; }
select { padding-right: 4px; }
input:hover, select:hover, textarea:hover { border-color: var(--ink-3); }
input::placeholder, textarea::placeholder { color: var(--ink-3); }
input[aria-invalid="true"], select[aria-invalid="true"] { border-color: var(--danger); }

.field-error { color: var(--danger); font-size: 13px; margin-top: 4px; }
.form-errors {
  border: 1px solid var(--danger); background: var(--danger-soft);
  color: var(--danger); border-radius: var(--radius);
  padding: var(--unit) calc(var(--unit) * 1.5); font-size: 14px;
}
.form-errors ul { margin: 0; padding-left: 18px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 calc(var(--unit) * 2); }
.grid-2 .span-2 { grid-column: 1 / -1; }
.filter-input { width: 280px; }

/* ── Tables ───────────────────────────────────────────────── */
/* Fixed layout keeps the table exactly as wide as its card, so a long
   drug name can never push the action buttons off the screen. Cells
   truncate instead of wrapping, which is what holds the 32px row height. */
/* border-collapse MUST stay `separate` here. With `collapse`, borders and
   backgrounds belong to the table rather than the cell, so a sticky <th>
   fails to occlude the rows scrolling underneath it and the body text
   bleeds through the header. */
.data-table {
  width: 100%; font-size: 14.5px; table-layout: fixed;
  border-collapse: separate; border-spacing: 0;
}
.data-table th, .data-table td {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.data-table th {
  text-align: left; font-size: 12.5px; font-weight: 600; letter-spacing: .03em;
  text-transform: uppercase; color: var(--ink-3);
  padding: 8px calc(var(--unit) * 1.5);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  position: sticky; top: 0; z-index: 3;   /* .card-flush is the scroll container */
}
.data-table td {
  padding: 0 calc(var(--unit) * 1.5); height: var(--row-h);
  border-bottom: 1px solid var(--line-2); vertical-align: middle;
  background: var(--surface);
}
/* Cells carry their own opaque background (see border-collapse note above),
   so row states have to be painted on the cells, not the row. */
.data-table tbody tr:hover td { background: var(--surface-2); }
.data-table .col-actions { text-align: right; white-space: nowrap; overflow: visible; }
.data-table td.col-actions { padding-right: var(--unit); }

/* Column budgets. The action buttons get a fixed slice; everything else
   shares what is left and ellipses when it does not fit. */
#patients-table  th:nth-child(1) { width: 13%; }
#patients-table  th:nth-child(2) { width: 24%; }
#patients-table  th:nth-child(3) { width: 10%; }
#patients-table  th:nth-child(4) { width: 14%; }
#patients-table  th:nth-child(5) { width: 12%; }
#patients-table  th:nth-child(6) { width: 286px; }

#medicines-table th:nth-child(1) { width: 26%; }
#medicines-table th:nth-child(2) { width: 18%; }
#medicines-table th:nth-child(3) { width: 100px; }
#medicines-table th:nth-child(4) { width: 18%; }
#medicines-table th:nth-child(5) { width: 20%; }
#medicines-table th:nth-child(6) { width: 146px; }

#history-body   td:nth-child(1),
#view-history th:nth-child(1) { width: 32%; }
#view-history th:nth-child(3) { width: 20%; }
.mono { font-variant-numeric: tabular-nums; }
.muted { color: var(--ink-3); }
.nowrap { white-space: nowrap; }

.badge {
  display: inline-block; font-size: 12px; line-height: 18px; padding: 0 7px;
  border: 1px solid var(--line); border-radius: 9px; color: var(--ink-2);
  text-transform: capitalize; background: var(--surface);
}

.empty-state {
  padding: calc(var(--unit) * 5) calc(var(--unit) * 2); text-align: center;
  color: var(--ink-3);
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
}
.empty-state[hidden] { display: none; }
.empty-state p { margin-bottom: calc(var(--unit) * 1.5); }

/* ── Combobox (shared search) ─────────────────────────────── */
.combo { position: relative; }
.combo-input { width: 100%; }
.combo-list {
  position: absolute; z-index: 30; top: calc(100% + 2px); left: 0; right: 0;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  max-height: 340px; overflow-y: auto; padding: 3px; margin: 0; list-style: none;
}
.combo-list[hidden] { display: none; }
.combo-list.is-above { top: auto; bottom: calc(100% + 2px); }
.combo-option {
  padding: 7px 9px; border-radius: 3px; cursor: pointer; font-size: 14px;
  display: flex; align-items: baseline; gap: var(--unit);
}
.combo-option[aria-selected="true"] { background: var(--accent-soft); }
.combo-option .opt-main { font-weight: 500; }
.combo-option .opt-sub { color: var(--ink-3); font-size: 13px; margin-left: auto; text-align: right; }
.combo-option mark { background: #fdf2ac; color: inherit; border-radius: 2px; padding: 0 1px; }
.combo-empty { padding: 8px 9px; color: var(--ink-3); font-size: 13.5px; }

/* ── Dose dots — the signature element ────────────────────── */
/* Filled state uses border-width, not background, so it survives
   "background graphics off" when printing. Identical shape on screen. */
.dot {
  display: inline-block; width: 16px; height: 16px; border-radius: 50%;
  border: 1.5px solid var(--ink-3); background: transparent;
  box-sizing: border-box; padding: 0;
}
.dot.is-on { border-width: 8px; border-color: var(--accent); }

button.dot {
  cursor: pointer; transition: transform .1s, border-color .12s;
}
button.dot:hover { border-color: var(--accent); transform: scale(1.12); }
button.dot:active { transform: scale(.94); }

.slot-toggles { display: flex; gap: 10px; align-items: flex-end; }
.slot-toggle { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.slot-toggle .slot-label {
  font-size: 11px; letter-spacing: .02em; color: var(--ink-3);
  max-width: 70px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ── Prescription builder ─────────────────────────────────── */
.builder-layout {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(360px, 34%);
  gap: calc(var(--unit) * 2); align-items: stretch;
  flex: 1; min-height: 0;
}
/* The form column scrolls on its own so the preview stays put beside it. */
.builder {
  min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column;
  padding-right: 2px;                 /* room for the scrollbar */
}
.builder > .card:last-of-type { margin-bottom: 0; }
@media (max-width: 1180px) {
  .builder-layout { grid-template-columns: minmax(0, 1fr); }
  /* Child selector so this still wins over the .preview-pane display
     rule defined later in the file. */
  .builder-layout > .preview-pane { display: none; }
}

.patient-card {
  display: grid; grid-template-columns: 1fr auto; gap: var(--unit);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: calc(var(--unit) * 1.5); background: var(--surface-2);
}
.patient-card[hidden] { display: none; }
.patient-card .pc-name { font-weight: 600; font-size: 16px; }
.patient-card .pc-meta { color: var(--ink-2); font-size: 13.5px; margin-top: 3px; }
.patient-card .pc-history {
  grid-column: 1 / -1; margin-top: var(--unit); padding: var(--unit);
  background: var(--surface); border-left: 2px solid var(--ink-3);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-size: 13.5px; color: var(--ink-2); white-space: pre-wrap;
}
.patient-card .pc-history strong { color: var(--ink); font-weight: 600; }

.rx-items { width: 100%; border-collapse: collapse; font-size: 14px; margin-bottom: var(--unit); }
.rx-items th {
  text-align: left; font-size: 12px; font-weight: 600; letter-spacing: .03em;
  text-transform: uppercase; color: var(--ink-3);
  padding: 6px 6px; border-bottom: 1px solid var(--line);
}
.rx-items td { padding: 5px 6px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
.rx-items .col-med    { width: 26%; }
.rx-items .col-slots  { width: 1%; white-space: nowrap; }
.rx-items .col-qty    { width: 84px; }
.rx-items .col-food   { width: 140px; }
.rx-items .col-days   { width: 74px; }
.rx-items .col-instr  { width: auto; }
.rx-items .col-x      { width: 34px; text-align: right; }
.rx-items input[type="number"] { text-align: right; }

/* Search box and the new-medicine escape hatch share one line, so an
   unfilled row costs no more vertical space than a filled one. */
.med-search { display: flex; align-items: center; gap: 6px; }
.med-search .combo { flex: 1; min-width: 0; }
.med-search .btn-link { flex: none; white-space: nowrap; }

.med-picked { display: flex; flex-direction: column; gap: 2px; }
.med-picked .med-name { font-weight: 500; }
.med-picked .med-sub { color: var(--ink-3); font-size: 12.5px; display: flex; gap: 6px; align-items: center; }
.row-warning { color: var(--danger); font-size: 12.5px; margin-top: 2px; }
.row-actions { margin-bottom: calc(var(--unit) * 2); }
.rx-items tr.is-invalid td { background: var(--danger-soft); }

/* ── Preview pane ─────────────────────────────────────────── */
.preview-pane {
  display: flex; flex-direction: column;
  min-height: 0; min-width: 0;
}
.preview-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: var(--unit); min-height: 30px; flex: none;
}
.preview-meta { font-size: 13px; color: var(--ink-3); }
.preview-scroll {
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: calc(var(--unit) * 1.5);
  flex: 1; min-height: 0; overflow: auto;
}
.paper-scaler { transform-origin: top left; }
.paper-scaler .rx-doc { box-shadow: var(--shadow); }
.preview-placeholder { color: var(--ink-3); font-size: 13.5px; text-align: center; padding: calc(var(--unit) * 4) 0; }

/* ── History ──────────────────────────────────────────────── */
.history-layout {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(380px, 38%);
  gap: calc(var(--unit) * 2); align-items: stretch;
  flex: 1; min-height: 0;
}
.history-doc { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
@media (max-width: 1180px) {
  /* Stacked, but both panes still get a real share of the height. */
  .history-layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(160px, 38%) minmax(0, 1fr);
  }
}
.data-table tr.is-active td { background: var(--accent-soft); }
.data-table tbody tr.clickable { cursor: pointer; }

/* ── Settings ─────────────────────────────────────────────── */
/* Tiles across whatever width is available rather than stranding the
   right half of the window empty. */
.settings-layout {
  flex: 1; min-height: 0; overflow-y: auto;
  display: grid; align-content: start;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: calc(var(--unit) * 2);
  padding-right: 2px;
}
.settings-layout > .card { margin-bottom: 0; }
.letterhead-preview {
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: calc(var(--unit) * 2); background: #fff;
}
.slot-list { list-style: none; margin: 0; padding: 0; }
.slot-row {
  display: flex; align-items: center; gap: var(--unit);
  padding: 5px 0; border-bottom: 1px solid var(--line-2);
}
.slot-row .slot-order { width: 22px; color: var(--ink-3); font-size: 13px; }
.slot-row input[type="text"] { flex: 1; max-width: 260px; }
.slot-row .slot-id { color: var(--ink-3); font-size: 12.5px; font-family: ui-monospace, monospace; }
.slot-row .slot-buttons { margin-left: auto; display: flex; gap: 4px; }

/* ── Modal ────────────────────────────────────────────────── */
.modal-layer {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(20, 24, 29, .38);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 6vh var(--unit) var(--unit); overflow-y: auto;
}
.modal-layer[hidden] { display: none; }
.modal {
  background: var(--surface); border-radius: 6px; box-shadow: var(--shadow);
  width: 100%; max-width: 620px; display: flex; flex-direction: column;
}
.modal.modal-sm { max-width: 420px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--unit) * 1.5) calc(var(--unit) * 2);
  border-bottom: 1px solid var(--line);
}
.modal-body { padding: calc(var(--unit) * 2); }
.modal-body > p { color: var(--ink-2); }
.modal-foot {
  display: flex; justify-content: flex-end; gap: var(--unit);
  padding: calc(var(--unit) * 1.5) calc(var(--unit) * 2);
  border-top: 1px solid var(--line); background: var(--surface-2);
  border-radius: 0 0 6px 6px;
}

/* ── Toasts & banners ─────────────────────────────────────── */
.toast-layer {
  position: fixed; bottom: var(--unit); left: 50%; transform: translateX(-50%);
  z-index: 200; display: flex; flex-direction: column; gap: 6px; align-items: center;
}
.toast {
  background: var(--ink); color: #fff; font-size: 14px;
  padding: 9px 16px; border-radius: 18px; box-shadow: var(--shadow);
}
.toast.is-error { background: var(--danger); }

.banner {
  border: 1px solid var(--danger); background: var(--danger-soft); color: var(--danger);
  padding: var(--unit) calc(var(--unit) * 2); font-size: 14px;
  display: flex; justify-content: space-between; gap: var(--unit); align-items: center;
}

/* The printed document never shows on screen except inside a .paper-scaler */
.print-root { display: none; }
