:root {
  --bg: #f3f6f6; --surface: #ffffff; --surface-2: #f8fafa; --text: #14201f; --muted: #5d6b6a; --line: #dfe6e5;
  --primary: #0f766e; --primary-ink: #ffffff; --primary-soft: #e3f3f1;
  --red: #b42318; --red-soft: #fdecea; --amber: #a15c07; --amber-soft: #fef4e2; --green: #157347; --green-soft: #e5f5ec;
  --blue: #1d4ed8; --blue-soft: #e8eefd; --violet: #6d28d9; --violet-soft: #f0e9fd; --gray-soft: #eef1f1;
  --radius: 12px; --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1514; --surface: #162120; --surface-2: #1b2827; --text: #e6efee; --muted: #9aaeac; --line: #2a3a38;
    --primary: #2bb3a6; --primary-ink: #04201d; --primary-soft: #133632;
    --red: #f4877c; --red-soft: #3a1b18; --amber: #f2b45a; --amber-soft: #3a2a12; --green: #6fd39b; --green-soft: #15301f;
    --blue: #8fb0ff; --blue-soft: #18233f; --violet: #c3a6ff; --violet-soft: #2a1d45; --gray-soft: #22302f;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 var(--font); -webkit-tap-highlight-color: transparent; }
a { color: var(--primary); text-decoration: none; }
h1 { font-size: 1.45rem; margin: 0; line-height: 1.2; }
h2 { font-size: 1.15rem; margin: 0; }
h3.section { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 10px; }
.card h3.section:first-child { margin-top: 0; }
p { margin: .4em 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.right { text-align: right; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.grow { flex: 1; min-width: 0; }
.boot { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- shell ---------- */
.shell { min-height: 100vh; }
.sidebar { display: none; }
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top)); background: var(--primary); color: var(--primary-ink); }
.topbar img { width: 28px; height: 28px; border-radius: 7px; background: #fff2; }
.content { padding: 16px 16px 96px; max-width: 1200px; margin: 0 auto; }
.bottombar { position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; display: flex; background: var(--surface);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.bottombar a, .bottombar button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px 9px;
  font: 600 .7rem var(--font); color: var(--muted); background: none; border: 0; cursor: pointer; }
.bottombar .ico { font-size: 1.25rem; line-height: 1.1; }
.bottombar .active { color: var(--primary); }
.more-sheet { position: fixed; inset: 0; z-index: 25; background: rgba(0,0,0,.3); }
.sheet { position: absolute; left: 0; right: 0; bottom: calc(58px + env(safe-area-inset-bottom)); background: var(--surface); border-radius: 16px 16px 0 0; padding: 8px; }
.sheet a { display: flex; gap: 12px; padding: 14px; color: var(--text); font-weight: 600; border-radius: 10px; }
.sheet a:active { background: var(--surface-2); }

@media (min-width: 900px) {
  .shell { display: grid; grid-template-columns: 240px 1fr; }
  .topbar, .bottombar, .more-sheet { display: none; }
  .sidebar { display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; background: var(--surface); border-right: 1px solid var(--line); padding: 16px 12px; }
  .brand { display: flex; gap: 10px; align-items: center; padding: 4px 8px 18px; }
  .brand img { width: 36px; height: 36px; border-radius: 9px; }
  .brand small, .sidebar-user small { display: block; color: var(--muted); font-size: .8rem; }
  .sidebar nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
  .sidebar nav a { display: flex; gap: 10px; align-items: center; padding: 9px 12px; border-radius: 9px; color: var(--text); font-weight: 550; }
  .sidebar nav a:hover { background: var(--surface-2); }
  .sidebar nav a.active { background: var(--primary-soft); color: var(--primary); }
  .sidebar .ico { width: 20px; text-align: center; }
  .sidebar-user { padding: 12px; border-top: 1px solid var(--line); }
  .content { padding: 24px 32px 40px; width: 100%; }
}

/* ---------- page bits ---------- */
.page-head { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.page-title { display: flex; gap: 8px; align-items: center; min-width: 0; }
.page-title p { margin: 2px 0 0; }
.back { font-size: 1.8rem; line-height: 1; padding: 0 8px 4px 0; color: var(--muted); }
.actions, .row-gap { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row-gap { margin-top: 6px; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin: 22px 0 10px; flex-wrap: wrap; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.toolbar .grow { flex: 1 1 220px; }
.date-nav { display: flex; gap: 4px; align-items: center; }
label.inline { display: flex; gap: 6px; align-items: center; font-size: .85rem; color: var(--muted); }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 12px; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.stack .card { margin: 0; }
.grid2, .grid3 { display: grid; gap: 0 14px; grid-template-columns: 1fr; }
.grid2.top { align-items: start; gap: 14px; }
@media (min-width: 640px) { .grid2 { grid-template-columns: 1fr 1fr; } .grid3 { grid-template-columns: repeat(3, 1fr); } }

.stats { display: grid; gap: 10px; grid-template-columns: repeat(2, 1fr); margin-bottom: 8px; }
@media (min-width: 640px) { .stats { grid-template-columns: repeat(3, 1fr); } }
@media (min-width: 1100px) { .stats { grid-template-columns: repeat(6, 1fr); } }
.stat { display: block; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; color: var(--text); box-shadow: var(--shadow); border-left: 4px solid var(--line); }
.stat-label { font-size: .78rem; color: var(--muted); }
.stat-value { font-size: 1.35rem; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat.green { border-left-color: var(--green); } .stat.amber { border-left-color: var(--amber); } .stat.red { border-left-color: var(--red); } .stat.violet { border-left-color: var(--violet); }

.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.list-row { display: flex; gap: 12px; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); color: var(--text); }
.list-row.wrap { flex-wrap: wrap; }
.list-row:last-child { border-bottom: 0; }
a.list-row:hover { background: var(--surface-2); }
.inactive { opacity: .55; }
.avatar { width: 38px; height: 38px; flex: none; border-radius: 50%; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); font-weight: 700; }
.avatar.lg { width: 48px; height: 48px; font-size: 1.2rem; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: .74rem; font-weight: 650; white-space: nowrap; }
.badge.green { background: var(--green-soft); color: var(--green); } .badge.red { background: var(--red-soft); color: var(--red); }
.badge.amber { background: var(--amber-soft); color: var(--amber); } .badge.blue { background: var(--blue-soft); color: var(--blue); }
.badge.violet { background: var(--violet-soft); color: var(--violet); } .badge.teal { background: var(--primary-soft); color: var(--primary); }
.badge.gray { background: var(--gray-soft); color: var(--muted); } .badge.outline { border: 1px solid var(--line); color: var(--muted); }

/* appointment card */
.appt { display: grid; grid-template-columns: 64px 1fr; gap: 4px 12px; }
.appt-time { display: flex; flex-direction: column; color: var(--muted); font-size: .8rem; }
.appt-time strong { color: var(--text); font-size: 1rem; }
.appt-name { font-weight: 650; font-size: 1.02rem; color: var(--text); }
.appt-actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; padding-top: 8px; border-top: 1px dashed var(--line); margin-top: 6px; }
.appt-actions:empty { display: none; }
@media (min-width: 900px) { .appt { grid-template-columns: 80px 1fr auto; } .appt-actions { grid-column: auto; border: 0; margin: 0; padding: 0; justify-content: flex-end; align-content: center; max-width: 380px; } }

/* ---------- forms ---------- */
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field label { font-size: .82rem; font-weight: 600; color: var(--muted); }
.req { color: var(--red); }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; min-height: 40px; width: 100%; }
.toolbar input, .toolbar select, .date-nav input, label.inline input { width: auto; }
.toolbar .grow { width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
input:disabled { opacity: .6; }
textarea { resize: vertical; }
.check { display: flex; gap: 8px; align-items: center; margin: 6px 0 12px; font-weight: 500; cursor: pointer; }
.check input { width: 18px; height: 18px; min-height: 0; accent-color: var(--primary); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: 600 .9rem var(--font); padding: 9px 16px; min-height: 40px;
  border-radius: 9px; border: 1px solid var(--line); background: var(--surface); color: var(--text); cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); }
.btn.sm { padding: 5px 11px; min-height: 32px; font-size: .82rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn { background: none; border: 1px solid transparent; color: var(--muted); font-size: 1.1rem; min-width: 36px; height: 36px; border-radius: 8px; cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); }
.link-btn { background: none; border: 0; color: var(--primary); font: inherit; cursor: pointer; padding: 6px 0; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: none; padding: 7px 12px; font: 600 .82rem var(--font); color: var(--muted); cursor: pointer; }
.seg button.on { background: var(--primary-soft); color: var(--primary); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 18px 0 12px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 10px 12px; font: 600 .9rem var(--font); color: var(--muted); border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap; }
.tabs button.on { color: var(--primary); border-bottom-color: var(--primary); }

.slots { display: flex; flex-wrap: wrap; gap: 6px; }
.slot { border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 8px; padding: 7px 10px; font: 600 .82rem var(--font); cursor: pointer; }
.slot.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.picked { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--primary); background: var(--primary-soft); border-radius: 10px; margin-bottom: 12px; }
.pick-list { display: flex; flex-direction: column; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-bottom: 10px; }
.pick-list button { display: flex; justify-content: space-between; gap: 10px; padding: 10px 12px; border: 0; border-bottom: 1px solid var(--line); background: var(--surface); color: var(--text); font: inherit; text-align: left; cursor: pointer; }
.pick-list button:hover { background: var(--surface-2); }

/* consultation */
.consult-grid { display: grid; gap: 16px; }
@media (min-width: 1000px) { .consult-grid { grid-template-columns: 1fr 340px; } }
.patient-banner { display: flex; gap: 12px; align-items: center; }
.vitals-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 10px; }
@media (min-width: 640px) { .vitals-grid { grid-template-columns: repeat(4, 1fr); } }
.rx-row { display: grid; gap: 6px; padding: 10px 0; border-bottom: 1px dashed var(--line);
  grid-template-columns: minmax(0,1fr) minmax(0,1fr) 36px;
  grid-template-areas: "med med del" "dose freq freq" "dur ins ins"; }
.rx-row > * { min-width: 0; }
.rx-row .rx-med { grid-area: med; } .rx-row .rx-dose { grid-area: dose; } .rx-row .rx-freq { grid-area: freq; }
.rx-row .rx-dur { grid-area: dur; } .rx-row .rx-ins { grid-area: ins; } .rx-row .icon-btn { grid-area: del; }
@media (min-width: 760px) { .rx-row { grid-template-columns: 2fr 1fr 1.3fr 1fr 1.5fr 36px; grid-template-areas: "med dose freq dur ins del"; } }
.rx-editor > .btn { margin-top: 10px; }
.bill-row { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
.bill-row .qty { width: 64px; } .bill-row .price, input.price { width: 110px; }
.total-line { display: flex; gap: 12px; align-items: center; justify-content: flex-end; font-size: 1.05rem; margin-top: 4px; }
.sticky-actions { position: sticky; bottom: calc(66px + env(safe-area-inset-bottom)); z-index: 5; padding: 8px 0; }
@media (min-width: 900px) { .sticky-actions { bottom: 0; } }
.visit { font-size: .9rem; }
.visit-head { display: flex; gap: 10px; align-items: center; margin-bottom: 6px; }
.visit-head .muted { flex: 1; }
.vitals-row { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: .85rem; margin: 6px 0; }
.vitals-row b { color: var(--muted); font-weight: 600; font-size: .75rem; }
ul.rx { margin: 6px 0; padding-left: 18px; }
.info-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.info-grid label { display: block; font-size: .75rem; color: var(--muted); font-weight: 600; }
.info-grid .span2 { grid-column: 1 / -1; }
@media (min-width: 760px) { .info-grid { grid-template-columns: repeat(4, 1fr); } }
.sched-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 8px; }
.sched-row select, .sched-row input { width: auto; }
.copy-box { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 8px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2); }
.copy-box code { flex: 1 1 200px; overflow-wrap: anywhere; font-size: .85rem; }
pre.code { background: var(--surface-2); border: 1px solid var(--line); border-radius: 9px; padding: 10px; font-size: .78rem; overflow-x: auto; }
.steps li { margin-bottom: 6px; }
.bars { display: flex; flex-direction: column; gap: 6px; }
.bar-row { display: grid; grid-template-columns: 92px 1fr 96px; gap: 8px; align-items: center; }
.bar { background: var(--surface-2); border-radius: 4px; height: 12px; overflow: hidden; }
.bar div { background: var(--primary); height: 100%; border-radius: 4px; }

/* tables / paper */
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.table th { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.table .num { text-align: right; }
.table tfoot td { border-bottom: 0; padding: 4px 6px; }
.table tr.strong td { font-weight: 700; }
.scroll-x { overflow-x: auto; }
.paper { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; max-width: 820px; overflow-x: auto; }
.paper-head { display: flex; justify-content: space-between; gap: 12px; border-bottom: 2px solid var(--primary); padding-bottom: 10px; margin-bottom: 12px; }
.paper-patient { display: flex; flex-wrap: wrap; gap: 4px 20px; padding: 8px 0 10px; border-bottom: 1px solid var(--line); margin-bottom: 10px; font-size: .9rem; }
.rx-symbol { font-size: 1.8rem; margin: 10px 0 4px; color: var(--primary); }
.paper-sign { margin: 48px 0 0 auto; width: 200px; border-top: 1px solid var(--text); text-align: center; padding-top: 4px; font-size: .85rem; }

/* feedback */
.alert { padding: 10px 12px; border-radius: 9px; margin-bottom: 12px; font-size: .9rem; }
.alert .btn { margin-left: 10px; }
.alert.error { background: var(--red-soft); color: var(--red); }
.empty { text-align: center; color: var(--muted); padding: 36px 16px; background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }
.spinner { width: 28px; height: 28px; margin: 40px auto; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--primary); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; z-index: 100; left: 50%; transform: translateX(-50%); bottom: calc(76px + env(safe-area-inset-bottom)); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-weight: 600; font-size: .9rem; box-shadow: 0 6px 20px rgba(0,0,0,.2); max-width: calc(100vw - 32px); }
.toast.error { background: var(--red); color: #fff; }
@media (min-width: 900px) { .toast { bottom: 24px; } }

/* modal */
.modal-bg { position: fixed; inset: 0; z-index: 50; background: rgba(10, 20, 20, .45); display: flex; align-items: flex-end; justify-content: center; }
.modal { background: var(--surface); width: 100%; max-height: 92vh; overflow-y: auto; border-radius: 16px 16px 0 0; padding-bottom: env(safe-area-inset-bottom); }
.modal-head { position: sticky; top: 0; z-index: 2; background: var(--surface); display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 16px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding-top: 12px; margin-top: 8px; border-top: 1px solid var(--line); }
@media (min-width: 700px) { .modal-bg { align-items: center; } .modal { max-width: 520px; border-radius: 16px; } .modal.wide { max-width: 760px; } }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, var(--primary-soft), var(--bg) 60%); }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 28px 22px; box-shadow: var(--shadow); }
.login-logo { width: 52px; height: 52px; border-radius: 13px; }
.login-card h1 { margin-top: 12px; }
.login-card p { margin-bottom: 18px; }

/* print: only the paper */
@media print {
  body { background: #fff; color: #000; }
  .sidebar, .topbar, .bottombar, .no-print, .toast { display: none !important; }
  .shell { display: block; } .content { padding: 0; }
  .paper { border: 0; padding: 0; max-width: none; }
}
