/* Branding colours arrive as CSS custom properties from the tenant's settings
   (see views/layout.ejs); nothing here hardcodes a practice's palette. */

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

:root {
  --ink: #1c2430;
  --ink-muted: #5b6875;
  --line: #dfe4ea;
  --surface: #ffffff;
  --canvas: #f4f6f9;
  --danger: #b42318;
  --danger-bg: #fef3f2;
  --success: #067647;
  --success-bg: #ecfdf3;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .06);
}

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;
}

a { color: var(--brand-primary); }

.page { flex: 1; width: 100%; max-width: 1100px; margin: 0 auto; padding: 28px 20px; }
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: 24px; letter-spacing: -0.01em; }
.page__subtitle, .muted { margin: 0; color: var(--ink-muted); }

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

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.topbar__brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; }
.topbar__logo { height: 34px; width: auto; }
.topbar__mark {
  display: grid; place-items: center;
  height: 34px; width: 34px; border-radius: 8px;
  background: var(--brand-primary); color: #fff; font-weight: 700;
}
.topbar__names { display: flex; flex-direction: column; line-height: 1.2; }
.topbar__practice { font-weight: 600; }
.topbar__doctor { font-size: 12px; color: var(--ink-muted); }

.topbar__nav { display: flex; gap: 4px; margin-left: auto; }
.topbar__link {
  padding: 7px 12px; border-radius: 8px;
  text-decoration: none; color: var(--ink-muted); font-weight: 500;
}
.topbar__link:hover { background: var(--canvas); color: var(--ink); }
.topbar__link.is-active { background: var(--brand-primary); color: #fff; }

.topbar__user { display: flex; align-items: center; gap: 10px; }
.topbar__username { font-weight: 500; }
.topbar__logout { margin: 0; }

.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 8px; font-size: 17px; }

.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: 8px;
  font: inherit;
  background: #fff;
}
.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: 8px;
  font: inherit; font-weight: 600;
  cursor: pointer;
}
.btn--primary { background: var(--brand-primary); color: #fff; }
.btn--primary:hover { filter: brightness(.94); }
.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: #fecdca; color: var(--danger); }
.flash--success { background: var(--success-bg); border-color: #abefc6; color: var(--success); }
.flash--info { background: #e3f2fd; border-color: #b6d9f7; color: #1565c0; }

.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;
  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(--brand-primary); }
.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: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-muted); background: var(--canvas); }
.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; }

.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(--brand-primary); border-color: var(--brand-primary); color: #fff; }

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

.badge--muted { background: var(--canvas); color: var(--ink-muted); border: 1px solid var(--line); }
.badge--num { background: var(--brand-primary); 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: #f5f5f5; color: #666; }
.status--reviewed { background: #e3f2fd; color: #1565c0; }
.status--approved { background: #e8f5e9; color: #2e7d32; }
.status--sent     { background: #f3e5f5; color: #6a1b9a; }

/* ------------------------------- 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(--brand-primary); color: var(--brand-primary); }
.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: #f0b0b0; }
.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: 8px; background: var(--surface); font: inherit; cursor: pointer; }
.segmented__btn.is-active { background: var(--brand-primary); border-color: var(--brand-primary); 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; }
.saved { font-size: 12px; color: var(--success); font-weight: 600; }

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

.generator { display: grid; grid-template-columns: minmax(280px, 360px) 1fr; gap: 20px; align-items: start; }
@media (max-width: 820px) { .generator { grid-template-columns: 1fr; } }

.assessment { min-height: 340px; font-size: 14px; line-height: 1.6; }
.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; }

.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(--brand-primary); }
.selected-template[hidden] { display: none; }

.edit-toolbar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; padding: 8px 14px; background: #fff8e1; border: 1px solid #f0e0a0; border-radius: 8px; 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 ----------------------------- */

.wa-title { color: #25d366; }
.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: #25d366; background: #f6fef9; }
.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: #b58900; }
.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: #25d366; background: #f6fef9; }
.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: #b58900; white-space: nowrap; }
.wa-result__empty { padding: 8px; font-size: 12px; color: var(--ink-muted); }

.wa-tip {
  margin: 12px 0 0; padding: 8px 12px;
  background: #fff8e1; border-left: 3px solid #f59e0b; border-radius: 0 5px 5px 0;
  font-size: 11px; color: #78350f;
}

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

.sort-link { color: inherit; text-decoration: none; white-space: nowrap; }
.sort-link:hover { color: var(--brand-primary); 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(--canvas); border-radius: 8px; border-left: 3px solid var(--brand-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; }

/* ----------------------------- 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: 10px; font-size: 12px; font-weight: 600; }
.tl-appt .timeline-card { background: #f7fbff; border-left: 3px solid #1565c0; }

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

.activity-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 4px; 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; }
