:root {
  --bg: #f4f6f6;
  --panel: #ffffff;
  --ink: #17201f;
  --muted: #5d6b69;
  --line: #dde3e2;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e3f2f0;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn-soft: #fff4d6;
  --warn-ink: #7a4b00;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 5%);
  --radius: 10px;
  --st-booked: #2563eb;
  --st-arrived: #b45309;
  --st-completed: #15803d;
  --st-cancelled: #8a9391;
  --st-no-show: #b42318;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111615;
    --panel: #1a2120;
    --ink: #e6ecea;
    --muted: #9aa7a4;
    --line: #2c3634;
    --accent: #2bb3a5;
    --accent-ink: #062320;
    --accent-soft: #163330;
    --danger: #f2765f;
    --danger-soft: #3a1d19;
    --warn-soft: #3a2f12;
    --warn-ink: #f3cf7a;
    --shadow: none;
    --st-booked: #6b9cff;
    --st-arrived: #f0a34a;
    --st-completed: #4fc47a;
    --st-cancelled: #7b8684;
    --st-no-show: #f2765f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2, h3 { margin: 0 0 .4em; line-height: 1.2; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.1rem; }
a { color: var(--accent); }
.loading { padding: 40px; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side {
  background: var(--panel); border-right: 1px solid var(--line);
  padding: 18px 12px; display: flex; flex-direction: column; gap: 4px;
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; padding: 4px 8px 16px; }
.brand img { width: 28px; height: 28px; }
.side a {
  display: block; padding: 8px 10px; border-radius: 8px; color: var(--ink); text-decoration: none;
}
.side a:hover { background: var(--accent-soft); }
.side a.active { background: var(--accent); color: var(--accent-ink); }
.side .sep { margin: 10px 10px 4px; font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.side .me { margin-top: auto; padding: 10px; border-top: 1px solid var(--line); font-size: .9rem; }
.main { padding: 24px; min-width: 0; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-bottom: 18px; }
.page-head h1 { margin: 0; margin-right: auto; }
.page-head select, .page-head input { width: auto; }

@media (max-width: 800px) {
  .shell { grid-template-columns: 1fr; }
  .side {
    position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center;
    padding: 10px 16px; border-right: 0; border-bottom: 1px solid var(--line);
  }
  .brand { padding: 4px 8px 4px 0; width: 100%; }
  .side .sep { display: none; }
  .side .me { margin: 0; border: 0; padding: 6px 0 0; width: 100%; }
  .main { padding: 16px; }
}

/* ---------- pieces ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 16px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat b { display: block; font-size: 1.6rem; }

button, .btn {
  font: inherit; cursor: pointer; border-radius: 8px; padding: 8px 14px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink); text-decoration: none; display: inline-block;
}
button:hover, .btn:hover { border-color: var(--accent); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.danger { color: var(--danger); }
button.small, .btn.small { padding: 4px 10px; font-size: .85rem; }
button:disabled { opacity: .5; cursor: default; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.form-actions { margin: 16px 0; }
#history { margin-top: 16px; }
details { margin-top: 8px; }

label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  font: inherit; width: 100%; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink);
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { min-height: 90px; resize: vertical; }
.field { margin-bottom: 12px; }
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 14px; }
.error { color: var(--danger); background: var(--danger-soft); padding: 8px 12px; border-radius: 8px; margin-bottom: 12px; }
.note { background: var(--accent-soft); padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; }
.locked { background: var(--warn-soft); color: var(--warn-ink); padding: 10px 12px; border-radius: 8px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--accent-soft); }
.table-wrap { overflow-x: auto; }

.badge { display: inline-block; font-size: .75rem; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--ink); }
.badge.warn { background: var(--warn-soft); color: var(--warn-ink); }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: .85rem; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.st-booked { color: var(--st-booked); }
.st-arrived { color: var(--st-arrived); }
.st-completed { color: var(--st-completed); }
.st-cancelled { color: var(--st-cancelled); }
.st-no-show { color: var(--st-no-show); }

/* ---------- login ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: 100%; max-width: 380px; }
.auth .brand { padding: 0 0 12px; font-size: 1.1rem; }

/* ---------- today list ---------- */
.appt-list { list-style: none; margin: 0; padding: 0; }
.appt-list li { display: grid; grid-template-columns: 70px 1fr auto; gap: 12px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); }
.appt-list li:last-child { border-bottom: 0; }
.appt-list .time { font-weight: 700; font-variant-numeric: tabular-nums; }
@media (max-width: 600px) { .appt-list li { grid-template-columns: 60px 1fr; } .appt-list li .row { grid-column: 1 / -1; } }

/* ---------- week calendar ---------- */
.cal-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.cal { display: grid; grid-template-columns: 56px repeat(7, minmax(120px, 1fr)); min-width: 900px; }
.cal-head { position: sticky; top: 0; background: var(--panel); border-bottom: 1px solid var(--line); padding: 8px; font-size: .85rem; text-align: center; z-index: 2; }
.cal-head.today { color: var(--accent); font-weight: 700; }
.cal-times { position: relative; }
.cal-times div { height: 60px; font-size: .75rem; color: var(--muted); text-align: right; padding-right: 6px; transform: translateY(-.6em); }
.cal-times div:first-child { transform: none; }
.cal-day { position: relative; border-left: 1px solid var(--line); cursor: copy; }
.cal-day .hour { height: 60px; border-bottom: 1px dashed var(--line); }
.cal-day.today { background: var(--accent-soft); }
.ev {
  position: absolute; left: 3px; right: 3px; border-radius: 6px; padding: 3px 6px; font-size: .78rem; overflow: hidden;
  background: var(--panel); border: 1px solid var(--line); border-left: 4px solid currentColor; cursor: pointer; line-height: 1.25;
}
.ev b { color: var(--ink); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev span { color: var(--muted); }
.ev.st-cancelled, .ev.st-no-show { opacity: .55; }

/* ---------- patient picker ---------- */
.picker { position: relative; }
.quick { margin-top: 6px; }
.picker ul {
  position: absolute; z-index: 5; left: 0; right: 0; top: 100%; margin: 2px 0 0; padding: 4px; list-style: none;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); max-height: 220px; overflow: auto;
}
.picker li { padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.picker li:hover, .picker li.on { background: var(--accent-soft); }

/* ---------- dialog & toast ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 32px));
  background: var(--panel); color: var(--ink); box-shadow: 0 20px 60px rgb(0 0 0 / 25%);
}
dialog::backdrop { background: rgb(0 0 0 / 40%); }
dialog form { padding: 20px; }
dialog .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; flex-wrap: wrap; }
dialog .actions .left { margin-right: auto; }
#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; }

/* ---------- Phase 1 additions ---------- */
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.dim { opacity: .55; }
.space { justify-content: space-between; }
.block { display: block; text-decoration: none; color: var(--ink); }
.plain { list-style: none; margin: 0; padding: 0; }
.plain li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.plain li:last-child { border-bottom: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 14px; }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 99px; padding: 3px 10px; font-size: .85rem; }
.count { background: var(--danger); color: #fff; border-radius: 99px; font-size: .72rem; padding: 1px 7px; margin-left: 6px; }
.side a.active .count { background: var(--accent-ink); color: var(--accent); }
.badge.new { background: var(--accent); color: var(--accent-ink); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.done, .badge.accepted { background: #dcf3e3; color: #12632f; }
.badge.declined, .badge.cancelled { background: var(--line); color: var(--muted); }
.badge.open, .badge.proposed { background: var(--warn-soft); color: var(--warn-ink); }
.badge.booked { background: #dfe9ff; color: #1d4ed8; }
@media (prefers-color-scheme: dark) {
  .badge.done, .badge.accepted { background: #173a24; color: #7ee0a0; }
  .badge.booked { background: #1c2a4a; color: #9fbcff; }
}
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin: 0 0 12px; }
legend { font-size: .85rem; color: var(--muted); padding: 0 4px; }
label:has(> input[type=checkbox]) { display: flex; gap: 8px; align-items: center; color: var(--ink); font-size: .95rem; margin-bottom: 8px; }
input[type=checkbox] { width: auto; }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 0 12px; }
.file-btn { cursor: pointer; }
dialog form.wide, dialog:has(form.wide) { width: min(760px, calc(100vw - 32px)); }
textarea.tall { min-height: 260px; }
.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 10px; background: var(--accent-soft); }
.alert.warn { background: var(--warn-soft); color: var(--warn-ink); }
.alert.danger { background: var(--danger-soft); color: var(--danger); }

.avatar { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; flex: none; display: inline-grid; place-items: center; background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.avatar.xs { width: 30px; height: 30px; font-size: .75rem; }
.avatar.sm { width: 40px; height: 40px; font-size: .85rem; }
.avatar.lg { width: 96px; height: 96px; font-size: 1.6rem; }
.avatar.xl { width: 128px; height: 128px; font-size: 2rem; border-radius: 16px; }

.patient-head { display: flex; gap: 18px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.patient-head h1 { margin: 0 0 4px; }
.ph-main { flex: 1; min-width: 220px; }
.ph-actions { margin-top: 10px; }
.photo-btn { border: 0; background: none; padding: 0; display: grid; justify-items: center; gap: 4px; font-size: .8rem; color: var(--accent); }
.stat.due b { color: var(--danger); }
.with-aside { display: grid; grid-template-columns: 1fr 300px; gap: 18px; align-items: start; }
.aside { position: sticky; top: 16px; }
@media (max-width: 1100px) {
  .with-aside { grid-template-columns: 1fr; }
  .aside { position: static; order: -1; }
  .aside .sticky-note textarea { min-height: 80px; }
}
.sticky-note { background: #fff7c2; color: #3d3400; border-radius: 10px; padding: 14px; box-shadow: var(--shadow); }
.sticky-note h2, .sticky-note h3 { margin-bottom: 4px; }
.sticky-note .muted { color: #6b5f1f; }
.sticky-note textarea { background: #fffbe0; border-color: #e8da84; color: #3d3400; min-height: 200px; }
.sticky-note p { white-space: pre-wrap; margin: 0; }
@media (prefers-color-scheme: dark) {
  .sticky-note { background: #3a3415; color: #f4e9b0; }
  .sticky-note .muted { color: #cfc185; }
  .sticky-note textarea { background: #2d2810; border-color: #5c5222; color: #f4e9b0; }
}
.tabs { display: flex; flex-wrap: wrap; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.tabs a { padding: 8px 12px; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.visit-list { display: grid; gap: 10px; margin-top: 8px; }
.visit { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }
.visit > div { margin-top: 4px; white-space: pre-wrap; }
.v-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 0 !important; white-space: normal !important; }
.v-money { margin-left: auto; font-weight: 600; }
.lbl { display: inline-block; min-width: 86px; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
tfoot th, tfoot td { border-bottom: 0; }

.upload input[name=note] { flex: 1; min-width: 180px; }
.upload input[type=file] { width: auto; }
.upload select { width: auto; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; margin-top: 12px; }
.thumbs figure { margin: 0; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--bg); }
.thumbs img { width: 100%; height: 130px; object-fit: cover; display: block; background: #000; }
.thumbs .pdf { display: grid; place-items: center; height: 130px; font-weight: 700; color: var(--danger); }
.thumbs figcaption { padding: 6px 8px; font-size: .85rem; }

.appt-list .who { display: flex; gap: 10px; align-items: flex-start; }
.appt-list li.dim { opacity: .55; }

.camera { background: #000; border-radius: 10px; min-height: 200px; display: grid; place-items: center; overflow: hidden; margin-bottom: 8px; }
.camera video { width: 100%; max-height: 360px; }
.camera .muted { color: #ccc; padding: 20px; text-align: center; }

.msg { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-top: 10px; display: grid; gap: 8px; }
.msg.sent { opacity: .6; }
.msg-body { min-height: 120px; font-size: .9rem; }

.doctor-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; margin-top: 16px; }
.doctor-cards .card + .card { margin-top: 0; }
.logo-row { margin: 8px 0; }
.logo-preview { max-height: 60px; max-width: 200px; }

/* Morning brief */
.brief .today-line { margin: 8px 0 0; font-size: 1.05rem; }
.brief-grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; margin-top: 12px; }
@media (max-width: 800px) { .brief-grid { grid-template-columns: 1fr; } }
.brief-block { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 12px; }
.brief-block h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 4px; }
.brief-block p { margin: 0; white-space: pre-wrap; }
.brief-block ul { margin: 0; padding-left: 18px; }
.brief .sticky-note { margin-bottom: 12px; }

/* Letterhead documents */
.paper { background: #fff; color: #111; max-width: 800px; margin: 0 auto; padding: 36px 40px; border: 1px solid var(--line); border-radius: 6px; }
.letterhead { display: flex; gap: 16px; align-items: center; border-bottom: 3px solid #0f766e; padding-bottom: 12px; margin-bottom: 18px; }
.letterhead img { max-height: 70px; max-width: 160px; }
.letterhead h1 { color: #0f766e; margin: 0 0 2px; font-size: 1.6rem; }
.letterhead p { margin: 0; font-size: .9rem; color: #444; }
.doc-title { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.doc-title h2 { text-transform: uppercase; letter-spacing: .08em; font-size: 1.15rem; }
.doc-title > div { text-align: right; }
.bill-to { margin-bottom: 14px; }
.lines th, .lines td { border-bottom: 1px solid #ddd; color: #111; }
.lines th { color: #444; }
.lines .num { text-align: right; white-space: nowrap; }
.regs { margin-top: 24px; color: #444; }
.sign { margin: 48px 0 0 auto; width: 220px; border-top: 1px solid #999; text-align: center; padding-top: 4px; font-size: .9rem; color: #444; }
.paper footer { margin-top: 24px; text-align: center; font-size: .85rem; color: #555; }

/* ---------- Version 3 additions ---------- */
.wide-card { max-width: 640px !important; }
.who-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.who-btn { display: flex; align-items: center; gap: 10px; padding: 12px; text-align: left; font-size: 1rem; }
.side .me label { margin-bottom: 4px; }
.side .me select { padding: 6px 8px; }
.cal-day.view { cursor: default; }
.chosen { display: flex; gap: 10px; align-items: center; border: 1px solid var(--accent); background: var(--accent-soft); border-radius: 8px; padding: 8px 10px; }
.chosen > div { flex: 1; }
.picker li { display: flex; gap: 8px; align-items: center; }
.picker li.add-new { color: var(--accent); font-weight: 600; border-top: 1px solid var(--line); margin-top: 4px; }
.new-patient { border: 1px dashed var(--accent); border-radius: 8px; padding: 10px 12px; margin-top: 8px; }
.nowrap { flex-wrap: nowrap; }
.grid3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; margin-bottom: 16px; }
.grid3 .card + .card, .grid2 .card + .card { margin-top: 0; }
.grid2 { margin-top: 16px; }
td.num, th.num { text-align: right; white-space: nowrap; }
label.consent { display: flex; gap: 8px; align-items: center; font-size: 1rem; color: var(--ink); background: var(--warn-soft); padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; }
label.apply { display: flex; gap: 10px; align-items: flex-start; color: var(--ink); font-size: .95rem; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 8px; }
label.apply input { margin-top: 4px; }
label.apply select { margin-top: 6px; width: auto; }
label.inline { display: inline-flex; gap: 6px; align-items: center; margin: 0; }
label.inline input { width: auto; }
.pre { white-space: pre-wrap; }
.consult audio { width: 100%; margin: 6px 0; }
.consult details { margin: 6px 0 10px; }
.summary .note { margin-top: 8px; }
.facts { margin-bottom: 12px; }
.facts th { width: 18%; color: #444; text-transform: none; letter-spacing: 0; font-size: .85rem; }
.facts th, .facts td { border-bottom: 1px solid #ddd; color: #111; padding: 6px 8px; }
.paper h3 { font-size: .95rem; margin: 16px 0 4px; color: #0f766e; }
.paper p { margin: 0 0 6px; white-space: pre-wrap; }
code { background: var(--accent-soft); padding: 1px 5px; border-radius: 4px; font-size: .85em; }

/* ---------- Treatments & payments ---------- */
.tlist { display: grid; gap: 10px; }
.tcard { border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 8px; padding: 10px 12px; display: grid; gap: 8px; }
.tcard.done { border-left-color: var(--st-completed); }
.tcard.proposed { border-left-color: var(--line); }
.money-row { display: flex; flex-wrap: wrap; gap: 6px 18px; }
.money-row .owe { color: var(--danger); }
.money-row .clear { color: var(--st-completed); font-weight: 600; }
.bar { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--st-completed); width: 0; }
.badge.in_progress { background: #dfe9ff; color: #1d4ed8; }
@media (prefers-color-scheme: dark) { .badge.in_progress { background: #1c2a4a; color: #9fbcff; } }

@media print {
  body { background: #fff; }
  .no-print, .side, #toast { display: none !important; }
  .shell { display: block; }
  .main { padding: 0; }
  .paper { border: 0; padding: 0; max-width: none; }
  .brief-block, .card { break-inside: avoid; box-shadow: none; }
  a { color: inherit; text-decoration: none; }
}
