/* The practice sets three colours per theme — primary, secondary and a
   background base — and views/layout.ejs emits them as --primary, --secondary
   and --bg-base. Every token below is mixed from those three, so the palette
   stays balanced whatever the practice picks and nothing here hardcodes it.

   The legacy names (--ink, --line, --canvas…) are kept as aliases rather than
   replaced: they are what the rest of this stylesheet already asks for, and as
   aliases they follow the active theme for free. */

*, *::before, *::after { box-sizing: border-box; }

/* The `hidden` attribute is how every script in this app shows and hides
   things, and the browser only implements it as `[hidden] { display: none }` —
   which any class of ours that sets `display` then outranks. `.btn` sets
   `display: inline-block`, so every hidden button in the app was on screen:
   the report modal showed Save Changes and Cancel next to Edit, Print / PDF
   appeared before there was anything to print, and the generator's mic showed
   in browsers that cannot dictate. Stated once, with `!important`, so hiding
   works everywhere rather than needing a per-class rule each time. */
[hidden] { display: none !important; }

/* Form controls do not inherit the page's colour: the browser paints them in
   its own `buttontext`, which is black whatever the theme is doing. That is
   how the generator's template groups — plain buttons with no colour of their
   own — came out black on a dark card. Everything with a colour of its own
   still wins on specificity; this only stops "no colour" meaning "black". */
button, input, select, textarea, optgroup { color: inherit; }

:root {
  /* ---- derived: light ---- */
  --bg: var(--bg-base);
  --surface: #ffffff;
  --surface-2: color-mix(in srgb, var(--primary) 4%, #ffffff);
  --text: #14161a;
  --text-dim: #5b616e;
  --border: color-mix(in srgb, var(--primary) 10%, #e3e6ec);

  /* The primary as *text*. Dark mode lifts it so a deep brand colour stays
     legible on a dark ground; light mode uses it as given. */
  --primary-ink: var(--primary);
  --on-primary: #ffffff;

  --grad: linear-gradient(135deg, var(--primary), var(--secondary));
  --grad-soft: linear-gradient(135deg,
      color-mix(in srgb, var(--primary) 14%, transparent),
      color-mix(in srgb, var(--secondary) 14%, transparent));

  --danger: #b42318;
  --danger-bg: #fef3f2;
  --danger-line: #fecdca;
  --success: #067647;
  --success-bg: #ecfdf3;
  --success-line: #abefc6;
  --info: #1565c0;
  --info-bg: #e3f2fd;
  --info-line: #b6d9f7;
  /* Deepened from #b8860b, which read at 3.04:1 on --warn-bg. */
  --warn: #8a6508;
  --warn-bg: #fff7e0;
  --warn-line: #f2d98a;
  /* The fourth state live paints in violet: a report already sent, a
     developer account. Distinct from success/info/warn on purpose. */
  --accent-ink: #6a1b9a;
  --accent-bg: #f3e5f5;
  /* Deepened from #1a8a4a so white sits on it at 4.8:1 rather than 4.39, and
     so it reads as text on white at the same. Still WhatsApp's green. */
  --whatsapp: #188343;
  /* What reads on top of it. Light's green is deep enough for white; dark's is
     a bright green that needs dark text — white on it is 1.85:1, which is not
     text anybody should have to read. */
  --on-whatsapp: #ffffff;

  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .05);

  /* ---- aliases the rest of this file already uses ---- */
  --ink: var(--text);
  --ink-muted: var(--text-dim);
  --line: var(--border);
  --canvas: var(--bg);
}

:root[data-theme="dark"] {
  --bg: var(--bg-base);
  --surface: color-mix(in srgb, var(--secondary) 9%, #17161d);
  --surface-2: color-mix(in srgb, var(--primary) 7%, #1d1c25);
  --text: #f3f2fb;
  --text-dim: #a8a4bd;
  --border: color-mix(in srgb, var(--primary) 16%, #2f2e39);

  /* A deep brand red is unreadable as text on near-black, so the derivation
     lifts it towards white. Backgrounds and gradients still use the raw
     colour — this is only the text/heading form. */
  --primary-ink: color-mix(in srgb, var(--primary) 62%, #ffffff);

  --danger: #ff8a80;
  --danger-bg: color-mix(in srgb, #ff5449 16%, var(--surface));
  --danger-line: color-mix(in srgb, #ff5449 38%, transparent);
  --success: #4bd07f;
  --success-bg: color-mix(in srgb, #4bd07f 14%, var(--surface));
  --success-line: color-mix(in srgb, #4bd07f 34%, transparent);
  --info: #7fb4f5;
  --info-bg: color-mix(in srgb, #4a90e2 16%, var(--surface));
  --info-line: color-mix(in srgb, #4a90e2 36%, transparent);
  --warn: #ffcf4d;
  --warn-bg: color-mix(in srgb, #f2b100 15%, var(--surface));
  --warn-line: color-mix(in srgb, #f2b100 38%, transparent);
  --accent-ink: #d9a6f5;
  --accent-bg: color-mix(in srgb, #a855f7 18%, var(--surface));
  --whatsapp: #34d97e;
  --on-whatsapp: #06281a;

  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 12px 40px rgba(0, 0, 0, .45);
}

body {
  margin: 0;
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--canvas);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* `--primary-ink` rather than the raw brand colour: it is the practice's
   primary *as text*, which dark mode lifts so a deep house colour stays
   legible on a dark ground. A maroon practice colour reads at 3.2:1 on the
   dark canvas unlifted. The rule the rest of the stylesheet already follows. */
a { color: var(--primary-ink); }

.page { flex: 1; width: 100%; max-width: 1280px; margin: 0 auto; padding: 26px 22px 56px; }
body.is-signed-out .page { display: grid; place-items: center; }

.stack { display: flex; flex-direction: column; gap: 20px; }
.page__head { display: flex; flex-direction: column; gap: 4px; }
.page__title { margin: 0; font-size: 25px; font-weight: 700; letter-spacing: -0.02em; color: var(--primary-ink); }
.page__subtitle, .muted { margin: 0; color: var(--ink-muted); }

/* ------------------------------- top bar -------------------------------- */

/* The gradient bar. The nav must hold every item on one line at desktop
   widths — it used to wrap "Referring Doctors" and "Email Log" onto a second
   row — so the type and padding here are deliberately tight. */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  min-height: 62px;
  background: var(--grad);
  color: #fff;
  position: sticky; top: 0; z-index: 50;
  box-shadow: 0 2px 18px color-mix(in srgb, var(--secondary) 45%, transparent);
}

.topbar__brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; flex-shrink: 0; }
.topbar__logo { height: 34px; width: auto; }
.topbar__mark {
  display: grid; place-items: center;
  height: 36px; width: 36px; border-radius: 10px;
  background: rgba(255, 255, 255, .18); color: #fff; font-weight: 800;
}
.topbar__names { display: flex; flex-direction: column; line-height: 1.2; }
.topbar__practice { font-weight: 600; font-size: 14px; }
.topbar__doctor { font-size: 11px; opacity: .82; }

.topbar__nav { display: flex; align-items: center; gap: 2px; margin-left: auto; flex-wrap: nowrap; }
.topbar__link {
  padding: 7px 8px; border-radius: 8px; white-space: nowrap;
  text-decoration: none; color: #fff; font-weight: 500; font-size: 13px;
  opacity: .88;
}
.topbar__link:hover { background: rgba(255, 255, 255, .14); opacity: 1; }
.topbar__link.is-active { background: rgba(255, 255, 255, .22); opacity: 1; font-weight: 600; }

.topbar__user { display: flex; align-items: center; gap: 9px; flex-shrink: 0; }
.topbar__username { font-weight: 500; font-size: 13px; white-space: nowrap; }
/* The role badge already says who you are signed in as; the name is the first
   thing to go when the bar runs out of room, rather than the nav wrapping. */
@media (max-width: 1500px) { .topbar__username { display: none; } }
.topbar__logout { margin: 0; }
.topbar .badge--role { background: rgba(255, 255, 255, .18); color: #fff; border: none; }
.topbar .btn--link { color: #fff; opacity: .85; font-size: 13px; }
.topbar .btn--link:hover { color: #fff; opacity: 1; }

/* Light / Dark switch */
.theme-toggle { display: flex; align-items: center; background: rgba(255, 255, 255, .16); border-radius: 999px; padding: 3px; flex-shrink: 0; }
.theme-toggle__btn {
  border: 0; background: transparent; color: #fff; cursor: pointer;
  font: inherit; font-size: 12px; font-weight: 600;
  padding: 4px 10px; border-radius: 999px; opacity: .75;
  white-space: nowrap;
}
.theme-toggle__btn.is-on { background: rgba(255, 255, 255, .92); color: var(--secondary); opacity: 1; }

/* Below this width the links stop fitting; they collapse behind the burger. */
.topbar__burger {
  display: none; flex-shrink: 0;
  border: 0; background: rgba(255, 255, 255, .16); color: #fff;
  font: inherit; font-size: 18px; line-height: 1;
  padding: 8px 12px; border-radius: 10px; cursor: pointer;
}

/* Where the links genuinely stop fitting. Measured, not guessed: with the
   Dispensary in the bar the single line runs out at 1390px, so the burger
   takes over from 1394px down — the redesign's own 1340px left the bar
   overflowing sideways between the two. */
@media (max-width: 1394px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; }
  .topbar__burger { display: block; margin-left: auto; order: 2; }
  .topbar__user { order: 3; }
  .topbar__nav {
    order: 4; margin-left: 0; flex-basis: 100%;
    flex-wrap: wrap; gap: 4px; padding-bottom: 8px;
  }
  .topbar__nav[hidden] { display: none; }
}

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600;
}
.badge--role { background: var(--canvas); color: var(--ink-muted); border: 1px solid var(--line); }

/* -------------------------------- cards --------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
}
.card--centred { text-align: center; max-width: 520px; margin: 0 auto; }
.card__title { margin: 0 0 12px; font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--primary-ink); }
/* Composer and modal headings stay sentence case — they are titles, not
   section labels. */
.card__title--plain { font-size: 16px; text-transform: none; letter-spacing: normal; }

.checklist { margin: 12px 0; padding-left: 18px; color: var(--ink-muted); }
.checklist li { margin: 4px 0; }

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

.auth { width: 100%; }
.auth__card { width: min(400px, 100%); }
.auth__head { text-align: center; margin-bottom: 20px; }
.auth__logo { max-height: 56px; width: auto; margin-bottom: 12px; }
.auth__title { margin: 0; font-size: 20px; }
.auth__subtitle { margin: 4px 0 0; color: var(--ink-muted); font-size: 14px; }

.form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field__label { font-size: 13px; font-weight: 600; color: var(--ink-muted); }
.field__input {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font: inherit;
  background: var(--surface-2);
  color: var(--text);
}
.field__input:focus {
  outline: none;
  border-color: var(--brand-primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand-primary) 18%, transparent);
}

.btn {
  padding: 10px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font: inherit; font-weight: 600;
  cursor: pointer;
  /* .btn is worn by <a> as often as by <button>; without this the global
     link rule underlines every one of them. */
  text-decoration: none;
  display: inline-block;
  transition: filter .15s, border-color .15s, background .15s;
}
a.btn { color: var(--text); }
a.btn--primary, a.btn--danger { color: #fff; }
a.btn--whatsapp, a.btn--send { color: var(--on-whatsapp); }
.btn--primary { background: var(--grad); color: #fff; }
.btn--primary:hover { filter: brightness(1.06); }
.btn--block { width: 100%; }
.btn--link {
  background: none; border: none; padding: 0;
  color: var(--ink-muted); font-weight: 500; text-decoration: underline;
}
.btn--link:hover { color: var(--ink); }

/* ------------------------------- messages ------------------------------- */

.flash {
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  margin-bottom: 14px;
  font-size: 14px;
}
.flash--error { background: var(--danger-bg); border-color: var(--danger-line); color: var(--danger); }
.flash--success { background: var(--success-bg); border-color: var(--success-line); color: var(--success); }
.flash--info { background: var(--info-bg); border-color: var(--info-line); color: var(--info); }

.error__detail {
  text-align: left; overflow-x: auto; padding: 12px;
  background: var(--canvas); border-radius: 8px;
  font-size: 12px; color: var(--ink-muted);
}

/* -------------------------------- footer -------------------------------- */

.footer {
  padding: 16px 20px;
  margin-top: auto;
  border-top: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-muted);
  font-size: 13px;
  text-align: center;
}

/* ============================== Phase 1 UI ============================== */

.page__head--row { flex-direction: row; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.page__head-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.page__head-actions form { margin: 0; }

.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-muted); }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 20px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; }
.tile {
  display: flex; flex-direction: column; gap: 4px; padding: 18px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); text-decoration: none; color: inherit;
}
.tile:hover { border-color: var(--brand-primary); }
.tile__title { font-weight: 600; color: var(--primary-ink); }
.tile__text { font-size: 13px; color: var(--ink-muted); }

.card--flush { padding: 0; overflow: hidden; }
.card__title--padded { padding: 20px 20px 0; }

.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar .field__input { flex: 1; min-width: 200px; }
.field__input--narrow { flex: 0 0 auto; min-width: 150px; }

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

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-dim); background: var(--surface-2); font-weight: 700; }
.table tbody tr:last-child td { border-bottom: none; }
.table td.num, .table th.num, .table td.right, .table th.right { text-align: right; }
.table__empty { color: var(--ink-muted); text-align: center; padding: 28px; }
.table tr.is-inactive { opacity: .55; }
.table tbody tr:hover { background: var(--surface-2); }

.deflist { display: grid; grid-template-columns: minmax(90px, auto) 1fr; gap: 6px 14px; margin: 0; font-size: 14px; }
.deflist dt { color: var(--ink-muted); }
.deflist dd { margin: 0; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 20px; border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: none; }
.row__main { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row__name { font-weight: 500; }

.pager { display: flex; gap: 4px; flex-wrap: wrap; }
.pager__link { padding: 6px 11px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); text-decoration: none; color: var(--ink-muted); font-size: 13px; }
.pager__link.is-active { background: var(--grad); border-color: transparent; color: #fff; }

/* ------------------------------- badges --------------------------------- */

.badge--muted { background: var(--canvas); color: var(--ink-muted); border: 1px solid var(--line); }
.badge--num { background: var(--grad); color: #fff; font-variant-numeric: tabular-nums; }

.status { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.status--draft    { background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border); }
.status--reviewed { background: var(--info-bg); color: var(--info); }
.status--approved { background: var(--success-bg); color: var(--success); }
.status--sent     { background: var(--accent-bg); color: var(--accent-ink); }

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

.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; }
.field__note { font-weight: 400; text-transform: none; letter-spacing: 0; color: var(--ink-muted); }
.field__input--upper { text-transform: uppercase; }
.field__input--code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; line-height: 1.6; }
textarea.field__input { resize: vertical; }

.button-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.button-row form { margin: 0; }
.button-row--tight { gap: 6px; justify-content: flex-end; }
.reset-form { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }

.btn--sm { padding: 5px 11px; font-size: 13px; }
.btn--outline { background: var(--surface); border-color: var(--line); color: var(--ink); }
.btn--outline:hover { border-color: var(--primary); color: var(--primary-ink); background: var(--grad-soft); }
.btn--danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn--danger:hover { filter: brightness(.94); }
.btn--danger-outline { background: var(--surface); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.btn--danger-outline:hover { border-color: var(--danger); }

.segmented { display: flex; gap: 8px; }
.segmented__btn { flex: 1; padding: 8px; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface); color: var(--text); font: inherit; cursor: pointer; }
.segmented__btn.is-active { background: var(--grad); border-color: transparent; color: #fff; font-weight: 600; }

/* ------------------------------- modals --------------------------------- */

.modal { position: fixed; inset: 0; background: rgba(16, 24, 40, .55); display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 200; }
.modal[hidden] { display: none; }
.modal__box { background: var(--surface); border-radius: var(--radius); width: min(520px, 100%); max-height: 90vh; display: flex; flex-direction: column; box-shadow: 0 20px 40px rgba(16, 24, 40, .2); }
.modal__box--large { width: min(900px, 100%); }
.modal__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal__title { margin: 0; font-size: 16px; }
.modal__actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.modal__body { padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
/* The body is the scrolling part of a modal, so its children must keep their
   own height. A child that sets `overflow` (the generated report's paper does,
   for its rounded letterhead) may otherwise be shrunk by the flex layout to
   whatever height is left and quietly clip the rest of itself — which is what
   made a long report end mid-sentence with nothing to scroll. */
.modal__body > * { flex: 0 0 auto; }
.saved { font-size: 12px; color: var(--success); font-weight: 600; }

/* ------------------------------ generator ------------------------------- */

.generator { display: grid; grid-template-columns: 1fr 1.4fr; gap: 20px; align-items: start; }
@media (max-width: 900px) { .generator { grid-template-columns: 1fr; } }

/* Spec §5: the assessment box fills its column. A bare <textarea> falls back
   to its `cols` width — about half of it — which is what it was doing. */
.assessment-wrap { position: relative; }
.assessment { display: block; width: 100%; min-height: 360px; font-size: 14px; line-height: 1.6; }

.mic {
  position: absolute; top: 10px; right: 10px;
  width: 38px; height: 38px; border-radius: 50%;
  border: 1px solid var(--primary); background: var(--surface);
  color: var(--primary-ink); font-size: 16px; cursor: pointer;
  display: grid; place-items: center;
}
.mic:hover { background: var(--grad-soft); }
.mic.is-recording { background: var(--danger); border-color: var(--danger); color: #fff; animation: mic-pulse 1.4s ease-in-out infinite; }
@keyframes mic-pulse { 50% { opacity: .55; } }
@media (prefers-reduced-motion: reduce) { .mic.is-recording { animation: none; } }

.dictation-status { margin-top: 8px; font-size: 12px; color: var(--text-dim); }
.dictation-status--error { color: var(--danger); }

.quick-add { margin: 10px 0 0; }
.word-count { font-size: 12px; color: var(--ink-muted); text-align: right; margin: 6px 0 12px; }

.search-results { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.search-result { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; font-size: 13px; background: var(--surface); }
.search-result:hover { border-color: var(--brand-primary); background: var(--canvas); }
.search-result__meta { color: var(--ink-muted); font-size: 11px; }
/* The "nobody by that name" row, with quick-add on it as live has. */
.search-result__none {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 10px; font-size: 12px; color: var(--ink-muted);
}

.selected-patient { padding: 12px; border: 1px solid var(--brand-primary); border-radius: 8px; background: var(--canvas); margin-bottom: 8px; }
.selected-patient__name { font-weight: 600; }
.selected-patient__meta { font-size: 12px; color: var(--ink-muted); }

.template-group { border: 1px solid var(--line); border-radius: 8px; margin-bottom: 8px; overflow: hidden; }
.template-group__toggle { width: 100%; display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; background: var(--surface); border: none; font: inherit; font-weight: 500; cursor: pointer; text-align: left; }
.template-group__toggle:hover { background: var(--canvas); }
.template-group__list { border-top: 1px solid var(--line); }
.template-group__list[hidden] { display: none; }
.template-item { display: flex; align-items: center; gap: 8px; padding: 8px 12px; cursor: pointer; font-size: 13px; }
.template-item:hover { background: var(--canvas); }
.template-item.is-selected { background: var(--canvas); font-weight: 600; }
.selected-template { margin-top: 10px; padding: 8px 12px; border-radius: 8px; background: var(--canvas); font-size: 13px; font-weight: 600; color: var(--primary-ink); }
.selected-template[hidden] { display: none; }

.edit-toolbar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; padding: 8px 14px; background: var(--warn-bg); border: 1px solid var(--warn-line); border-radius: var(--radius-sm); font-size: 12px; }
.edit-toolbar[hidden] { display: none; }

/* ============================= Phase 2 sharing ========================== */

.recipients { border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; margin: 0; }
.recipients legend { padding: 0 6px; }
.recipient { display: flex; align-items: flex-start; gap: 10px; padding: 6px 0; cursor: pointer; }
.recipient__name { display: block; font-weight: 500; }
.recipient__meta { display: block; font-size: 12px; color: var(--ink-muted); }

/* ------------------------- WhatsApp chooser ----------------------------- */

/* WhatsApp's own brand green (#25d366) fails as text on white — 1.98:1. The
   theme's green is the same colour tuned per theme for exactly this, as every
   other status colour here is, so the title uses that. */
.wa-title { color: var(--whatsapp); }
.wa-options { display: flex; flex-direction: column; gap: 10px; }

.wa-opt {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--line); border-radius: 8px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.wa-opt:hover { border-color: var(--whatsapp); background: color-mix(in srgb, var(--whatsapp) 10%, var(--surface)); }
.wa-opt.is-disabled { cursor: not-allowed; background: var(--canvas); }
.wa-opt.is-disabled:hover { border-color: var(--line); background: var(--canvas); }
.wa-opt.is-disabled .wa-opt__title { color: var(--ink-muted); }
.wa-opt.is-disabled .wa-opt__sub { color: var(--warn); }
.wa-opt__icon { font-size: 20px; width: 24px; text-align: center; flex-shrink: 0; }
.wa-opt__title { display: block; font-size: 14px; font-weight: 600; }
.wa-opt__sub { display: block; font-size: 12px; color: var(--ink-muted); }

.wa-sub { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }
.wa-sub[hidden] { display: none; }
.wa-row { display: flex; gap: 8px; }
.wa-row .field__input { flex: 1; }

.wa-results { margin-top: 8px; max-height: 190px; overflow-y: auto; }
.wa-result {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 9px 10px; margin-bottom: 6px;
  border: 1px solid var(--line); border-radius: 6px;
  font-size: 13px; cursor: pointer;
}
.wa-result:hover { border-color: var(--whatsapp); background: color-mix(in srgb, var(--whatsapp) 10%, var(--surface)); }
.wa-result.is-disabled { cursor: not-allowed; background: var(--canvas); }
.wa-result.is-disabled:hover { border-color: var(--line); background: var(--canvas); }
.wa-result__num { font-size: 11px; color: var(--success); white-space: nowrap; }
.wa-result__none { font-size: 11px; color: var(--warn); white-space: nowrap; }
.wa-result__empty { padding: 8px; font-size: 12px; color: var(--ink-muted); }

/* The amber aside live uses for "this is what will happen" notes. Named for
   what it is rather than for the WhatsApp chooser it started in. */
.tip, .wa-tip {
  margin: 12px 0 0; padding: 8px 12px;
  background: var(--warn-bg); border-left: 3px solid var(--warn-line); border-radius: 0 5px 5px 0;
  font-size: 11px; color: var(--warn);
}

/* ========================== Phase 3 patient record ====================== */

.sort-link { color: inherit; text-decoration: none; white-space: nowrap; }
.sort-link:hover { color: var(--primary-ink); text-decoration: underline; }

.card__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-right: 20px; }

.entries { list-style: none; margin: 0 0 12px; padding: 0; font-size: 14px; }
.entry { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.entry:last-child { border-bottom: none; }
.entry .muted { font-size: 12px; margin-left: 6px; }

.entry-add { display: flex; gap: 8px; align-items: flex-start; margin-top: 10px; }
.entry-add .field__input { flex: 1; }

.notes { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.note { padding: 10px 12px; background: var(--surface-2); border-radius: var(--radius-sm); border-left: 3px solid var(--primary); }
.note__meta { font-size: 11px; color: var(--ink-muted); margin-bottom: 4px; }
.note__text { font-size: 14px; white-space: pre-wrap; }

.upload-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: 14px 20px; border-top: 1px solid var(--line); }
.upload-row .field__input { flex: 1; min-width: 140px; }

.checkbox { display: flex; align-items: center; gap: 8px; margin: 14px 0; font-size: 14px; cursor: pointer; }

/* ----------------------------- prescriptions ---------------------------- */

.rx-canvas-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  overflow: hidden;
  margin-top: 6px;
}
#rxCanvas { display: block; width: 100%; height: auto; cursor: crosshair; }

.rx-text { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; line-height: 1.7; white-space: pre-wrap; margin: 0; }
.rx-image { max-width: 100%; height: auto; display: block; }

/* A script's lines in the composer: the medicine, then the three things the
   doctor sets on it. */
.rx-lines { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }
.rx-line {
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); padding: 10px 12px;
}
.rx-line__head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.rx-line__no {
  width: 22px; height: 22px; border-radius: 50%; flex-shrink: 0;
  background: var(--grad); color: #fff;
  display: grid; place-items: center; font-size: 11px; font-weight: 700;
}
.rx-line__name { font-size: 14px; }
.rx-line__remove { margin-left: auto; }
.rx-line__fields { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 10px; margin-top: 8px; }
@media (max-width: 700px) { .rx-line__fields { grid-template-columns: 1fr; } }

/* ----------------------------- patient timeline -------------------------- */
/* Ported from .timeline-* in core/css/style.css and the tl-appt rule in
   patient.php. Reports and appointments share one date-sorted spine. */

.timeline { position: relative; padding-left: 24px; margin-top: 14px; }
.timeline::before { content: ''; position: absolute; left: 7px; top: 0; bottom: 0; width: 2px; background: var(--line); }
.timeline__empty { color: var(--ink-muted); }
.timeline-item { position: relative; margin-bottom: 14px; }
.timeline-dot { position: absolute; left: -20px; top: 16px; width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--surface); box-shadow: 0 0 0 2px var(--line); }
.timeline-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 13px; transition: border-color .15s; }
.timeline-card:hover { border-color: var(--brand-primary); }
.timeline-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; flex-wrap: wrap; margin-bottom: 7px; }
.timeline-left { display: flex; flex-direction: column; gap: 2px; }
.timeline-date { font-weight: 600; font-size: 13px; }
.timeline-template { font-size: 12px; color: var(--ink-muted); }
.timeline-right { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.timeline-meta { font-size: 12px; color: var(--ink-muted); margin-bottom: 7px; }
.timeline-actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.status-badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
/* Report and appointment states. Live inlined these as hex; as classes they
   follow the theme instead of fighting it. */
.status-badge--draft     { background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border); }
.status-badge--reviewed  { background: var(--info-bg); color: var(--info); }
.status-badge--approved  { background: var(--success-bg); color: var(--success); }
.status-badge--sent      { background: var(--accent-bg); color: var(--accent-ink); }
.status-badge--arrived   { background: var(--accent-bg); color: var(--accent-ink); }
.status-badge--overdue   { background: var(--danger-bg); color: var(--danger); }
.status-badge--today     { background: var(--warn-bg); color: var(--warn); }
.tl-appt .timeline-card { background: var(--info-bg); border-left: 3px solid var(--info); }

/* ------------------------------- dashboard ------------------------------- */
/* Ported from the <style> block in core/dashboard.php, plus
   .unallocated-badge from core/css/style.css. */

/* Eight tiles across on a desktop, folding to four then two. */
.dash-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 11px;
  margin-bottom: 20px;
}
@media (max-width: 1150px) { .dash-grid { grid-template-columns: repeat(4, 1fr); } }
@media (max-width: 620px)  { .dash-grid { grid-template-columns: repeat(2, 1fr); } }
.dash-stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
  text-decoration: none;
  color: inherit;
  display: block;
  position: relative;
  overflow: hidden;
}
/* The gradient edge — the redesign's accent, used once per tile. */
.dash-stat::before { content: ''; position: absolute; inset: 0 auto 0 0; width: 3px; background: var(--grad); }
.dash-stat:hover { border-color: var(--primary); }
.dash-stat__num { display: block; font-size: 25px; font-weight: 800; line-height: 1; color: var(--primary-ink); font-variant-numeric: tabular-nums; }
.dash-stat__lbl { display: block; font-size: 11.5px; color: var(--text-dim); margin-top: 6px; }
/* Live's amber highlight: these counts mean somebody has work waiting. */
.dash-stat--attention { background: var(--warn-bg); border-color: var(--warn-line); }
.dash-stat--attention .dash-stat__num { color: var(--warn); }

/* Spec §3: both dashboard rows are an even 50/50. */
.dash-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; margin-bottom: 18px; }
.dash-cols:last-child { margin-bottom: 0; }
@media (max-width: 900px) { .dash-cols { grid-template-columns: 1fr; } }

.dash-more { margin-top: 12px; }

.qa { display: flex; flex-direction: column; gap: 9px; }
.qa-btn { display: block; width: 100%; text-align: left; }

/* ------------------------------ time tracker ----------------------------- */

.tt-totals { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.tt-totals--wide { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
.tt-cell {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 14px; text-align: center;
}
.tt-cell__n {
  font-size: 21px; font-weight: 800; font-variant-numeric: tabular-nums;
  color: var(--primary-ink);
}
.tt-cell__l { font-size: 11px; color: var(--text-dim); margin-top: 4px; }
.tt-bar { height: 7px; border-radius: 999px; background: var(--grad); margin-top: 8px; opacity: .85; }
.tt-bar--row { margin-top: 0; max-width: 180px; flex-shrink: 0; }

.unallocated-badge {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--warn-bg); color: var(--warn); border: 1px solid var(--warn-line);
  border-radius: 4px; padding: 2px 8px; font-size: 11px; font-weight: 600;
}

.share-panel { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 14px; margin-top: 12px; }
.share-panel .link-row { display: flex; gap: 8px; margin: 8px 0; flex-wrap: wrap; align-items: center; }
.share-panel .link-row .field__input { flex: 1; min-width: 180px; }

.btn--whatsapp { background: var(--whatsapp); color: var(--on-whatsapp); border-color: var(--whatsapp); }
.btn--whatsapp:hover { filter: brightness(.92); }

/* ------------------------- dashboard activity rows ----------------------- */
/* Ported from the .activity-* rules in core/dashboard.php. */

.activity-row { display: flex; justify-content: space-between; align-items: center; gap: 11px; padding: 11px 2px; border-bottom: 1px solid var(--line); }
.activity-row:last-child { border-bottom: none; }
.activity-main { min-width: 0; }
.activity-main a { color: var(--ink); text-decoration: none; font-weight: 600; }
.activity-main a:hover { text-decoration: underline; }
.activity-sub { font-size: 12px; color: var(--ink-muted); margin-top: 2px; }
.activity-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

/* ----------------------------- user management --------------------------- */
/* Ported from the .user-* and .badge-<role> rules in core/css/style.css. */

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
@media (max-width: 700px) { .two-col { grid-template-columns: 1fr; } }

.stack-tight { display: flex; flex-direction: column; gap: 12px; }

.user-row { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); gap: 8px; flex-wrap: wrap; }
.user-row:last-child { border-bottom: none; }
.user-info { min-width: 0; }
.user-name { font-weight: 600; font-size: 13px; }
.user-meta { font-size: 12px; color: var(--ink-muted); margin-top: 2px; display: flex; align-items: center; gap: 5px; flex-wrap: wrap; }
.user-last-login { font-size: 11px; color: var(--ink-muted); margin-top: 1px; }
.user-actions { display: flex; gap: 5px; flex-shrink: 0; align-items: center; }

.badge--role-doctor { background: var(--success-bg); color: var(--success); }
.badge--role-admin { background: var(--info-bg); color: var(--info); }
.badge--role-developer { background: var(--accent-bg); color: var(--accent-ink); }
.badge--disabled { background: var(--canvas); color: var(--ink-muted); }

/* ========================= redesign: shared pieces ======================= */
/* AMIYAMED_UIUX_REDESIGN_SPEC.md §11 — one chip vocabulary across Patients,
   Referring Doctors and Prescriptions, so the same fact looks the same
   wherever it appears. Backgrounds are mixed from theme tokens rather than
   hardcoded, because a green pill baked for white breaks on a dark ground. */

.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 9px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; white-space: nowrap;
  text-decoration: none; border: 1px solid transparent;
}
.chip--phone { background: var(--success-bg); color: var(--success); border-color: var(--success-line); }
.chip--email { background: var(--info-bg); color: var(--info); border-color: var(--info-line); }
.chip--code  { background: var(--info-bg); color: var(--info); border-color: var(--info-line); font-variant-numeric: tabular-nums; }
.chip--muted { background: var(--surface-2); color: var(--text-dim); border-color: var(--border); }
.chip--warn  { background: var(--warn-bg); color: var(--warn); border-color: var(--warn-line); }
a.chip:hover { filter: brightness(1.06); text-decoration: underline; }

/* Copy-to-clipboard chips report success in place rather than with an alert. */
.chip.is-copied { background: var(--success-bg); color: var(--success); }

/* Initials disc. The gradient is the redesign's one repeated accent. */
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  background: var(--grad); color: #fff;
  display: grid; place-items: center;
  font-weight: 700; font-size: 13px; letter-spacing: .02em;
}
.avatar--sm { width: 32px; height: 32px; font-size: 11px; }
.avatar--lg { width: 54px; height: 54px; font-size: 17px; }
.avatar--muted { background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border); }

/* ------------------------- the patient header card ---------------------- */
/* live patient.php opens with one identity card — back link, initials disc,
   name, the identifiers on one line and the contact details as chips on the
   next, with every action for the patient on the right. */

.pcard { display: flex; flex-direction: column; gap: 12px; }
.pcard__back { font-size: 12.5px; text-decoration: none; }
.pcard__back:hover { text-decoration: underline; }
.pcard__row { display: flex; align-items: flex-start; gap: 14px; flex-wrap: wrap; }
.pcard__main { flex: 1; min-width: 260px; }
.pcard__name {
  margin: 0; font-size: 21px; font-weight: 700; letter-spacing: -0.01em;
  color: var(--primary-ink);
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.pcard__meta { margin-top: 4px; font-size: 12.5px; color: var(--text-dim); }
.pcard__chips { margin-top: 8px; display: flex; gap: 6px; flex-wrap: wrap; }
.pcard__actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-start; }

/* ------------------------------- tab strips ----------------------------- */
/* The patient page's five tabs. A strip of buttons over a rule, the active one
   underlined in the practice's colour — live's own shape. Panels are switched
   with the `hidden` attribute, so a panel is genuinely not there for a screen
   reader rather than merely invisible. */

.tabs {
  display: flex; gap: 2px; flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}
.tab {
  padding: 10px 16px;
  border: none; border-bottom: 3px solid transparent;
  background: none; color: var(--text-dim);
  font: inherit; font-weight: 600; font-size: 13.5px;
  cursor: pointer; border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}
.tab:hover { color: var(--primary-ink); background: var(--grad-soft); }
.tab.is-active { color: var(--primary-ink); border-bottom-color: var(--primary); }
.tab__count {
  display: inline-block; margin-left: 6px; padding: 0 6px;
  border-radius: 9px; background: var(--surface-2); border: 1px solid var(--border);
  font-size: 11px; font-weight: 700; color: var(--text-dim);
}
.tab.is-active .tab__count { background: var(--grad-soft); color: var(--primary-ink); }
.tab-panel { display: flex; flex-direction: column; gap: 16px; }
/* A heading straight in a panel labels the list under it rather than being a
   card of its own, so the panel's own gap does the spacing. */
.tab-panel > .card__title { margin: 4px 2px 0; }

/* The read-only allergy and medication summary at the top of live's Reports
   tab, which points at the Clinical tab for editing rather than repeating the
   form. */
.summary-line { margin: 0 0 6px; font-size: 13.5px; }
.summary-line__label { font-weight: 700; color: var(--primary-ink); }

/* One bordered card per record — the layout Patients, Referring Doctors,
   Prescriptions and Forms all share. */
.ecards { display: flex; flex-direction: column; gap: 10px; }
.ecard {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 14px 16px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); box-shadow: var(--shadow);
}
.ecard:hover { border-color: color-mix(in srgb, var(--primary) 35%, var(--border)); }
.ecard.is-inactive { opacity: .6; }
.ecard__main { flex: 1; min-width: 0; }
.ecard__name { font-size: 14.5px; font-weight: 600; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.ecard__name a { color: inherit; text-decoration: none; }
.ecard__name a:hover { text-decoration: underline; }
.ecard__sub { font-size: 12.5px; color: var(--text-dim); margin-top: 3px; }
.ecard__chips { margin-top: 7px; }
.ecard__side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; flex-shrink: 0; }
.ecard__actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.ecard__count { text-align: right; }
.ecard__count b { display: block; font-size: 19px; color: var(--primary-ink); line-height: 1.1; }
.ecard__count span { font-size: 11px; color: var(--text-dim); }
.ecard__empty { padding: 30px; text-align: center; color: var(--text-dim); }

/* Filter and sort chip rows (Patients "Show:"/"Sort:", Forms status tabs). */
.filterbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 4px; }
.filterbar__label { font-size: 12px; font-weight: 600; color: var(--text-dim); }
.fchip {
  font-size: 12.5px; font-weight: 600;
  padding: 6px 13px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-dim);
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.fchip:hover { border-color: var(--primary); color: var(--primary-ink); }
.fchip.is-on { background: var(--grad); border-color: transparent; color: #fff; }
.fchip__n { opacity: .75; font-variant-numeric: tabular-nums; }

/* The T-code badge that sits inline before a template name in the Archive. */
.tcode {
  display: inline-block; margin-right: 6px;
  padding: 2px 8px; border-radius: 6px;
  background: var(--grad); color: #fff;
  font-size: 11px; font-weight: 700; letter-spacing: .02em;
  font-variant-numeric: tabular-nums;
}

/* Danger-tinted small action ("Delete", "Discard", "✕"). */
.btn--danger-ghost { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.btn--danger-ghost:hover { background: var(--danger-bg); border-color: var(--danger); }
.btn--send { background: var(--whatsapp); color: var(--on-whatsapp); border-color: transparent; }
.btn--send:hover { filter: brightness(.93); }

/* Wide tables scroll inside their card rather than pushing the page sideways —
   the Archive carries eight columns. */
.table-scroll { overflow-x: auto; }


/* --- Template Manager: reading a template's instructions ------------------
   The manager is read-only since template sets (docs/TEMPLATE-SETS.md), so the
   instructions are shown rather than edited. `pre` keeps the practice's own
   line breaks, which carry meaning in a report structure. */
.row--detail {
  display: block;
  background: var(--bg-base);
  padding: 14px 20px 18px;
}
.stack--tight { display: flex; flex-direction: column; gap: 6px; }
.instructions {
  margin: 0;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font: 13px/1.65 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* --- An informational note, not a warning --------------------------------
   Used by the Template Manager to say what a save will do. Deliberately NOT
   mixed from --primary: the practice's primary is Dr Moosa's red, and a note
   tinted with it reads as an alarm when nothing is wrong. */
.flash--info {
  background: color-mix(in srgb, var(--text) 4%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--text) 18%, var(--surface));
  color: var(--text);
}
