:root {
  --bg: #f4f6f7;
  --surface: #ffffff;
  --ink: #142029;
  --muted: #5d6c78;
  --line: #e1e7eb;
  --line-strong: #c9d3da;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e5f3f1;
  --warn: #a15c07;
  --warn-soft: #fdf3e2;
  --bad: #b42318;
  --bad-soft: #fdecea;
  --good: #1f7a3a;
  --good-soft: #e7f5ec;
  --blue: #1d5fb8;
  --blue-soft: #e8f0fb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 32, 41, .06);
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 24px; margin: 0; letter-spacing: -.01em; }
h1 a { color: inherit; }
h2 { font-size: 16px; margin: 0 0 12px; }
code, kbd { font: 13px ui-monospace, Menlo, Consolas, monospace; background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.warn-text { color: var(--warn); }
.error-text { color: var(--bad); min-height: 1.2em; margin: 8px 0 0; }
.boot, .loading { padding: 40px; color: var(--muted); text-align: center; }

/* layout */
.app { display: flex; min-height: 100vh; }
.nav {
  width: 210px; flex: none; background: var(--surface); border-right: 1px solid var(--line);
  padding: 16px 10px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 2px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 650; }
.brand img { width: 30px; height: 30px; }
.nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 8px; color: var(--muted); font-weight: 500;
}
.nav a svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav a:hover { background: var(--bg); color: var(--ink); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
main { flex: 1; min-width: 0; padding: 24px 28px 110px; max-width: 1180px; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .sub { margin: 4px 0 0; color: var(--muted); }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.card.error { color: var(--bad); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.cols { display: grid; grid-template-columns: minmax(0, 1.7fr) minmax(280px, 1fr); gap: 16px; align-items: start; }
.cols > aside > .card:last-child { margin-bottom: 16px; }

.grid { display: grid; gap: 12px 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }
.span3 { grid-column: 1 / -1; }
.top-gap { margin-top: 16px; }

/* form controls */
.field { display: flex; flex-direction: column; gap: 5px; margin: 0; }
.field > span { font-size: 13px; color: var(--muted); font-weight: 500; display: flex; align-items: center; gap: 8px; }
.field.inline { flex-direction: row; align-items: center; gap: 10px; }
.input {
  width: 100%; font: inherit; color: inherit; background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: 8px; padding: 9px 11px; min-height: 40px;
}
textarea.input { resize: vertical; min-height: 0; }
.input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.input.sm { min-height: 32px; padding: 5px 8px; font-size: 13px; }
.input.month { width: auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 550;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  padding: 8px 14px; min-height: 40px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.btn.big { min-height: 52px; padding: 10px 28px; font-size: 17px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); cursor: pointer; text-decoration: underline; }
.icon-btn {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-size: 20px; line-height: 1;
  width: 34px; height: 34px; border-radius: 8px; cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--bad-soft); color: var(--bad); }
.icon-btn.danger { font-size: 13px; width: auto; padding: 0 12px; color: var(--bad); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }

.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 8px; overflow: hidden; flex-wrap: wrap; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 8px 12px; cursor: pointer; border-right: 1px solid var(--line); font-size: 14px; min-height: 38px; }
.seg label:last-child span { border-right: 0; }
.seg input:checked + span { background: var(--accent); color: var(--accent-ink); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-weight: 500; }
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }

.chips, .follow { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; align-items: center; }
.follow .input { width: auto; }
.chip-btn {
  font: inherit; font-size: 13px; border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  padding: 4px 10px; border-radius: 999px; cursor: pointer;
}
.chip-btn:hover { border-color: var(--accent); color: var(--accent); }
.nad {
  font: inherit; font-size: 11px; font-weight: 650; border: 1px solid var(--line-strong); background: var(--bg);
  color: var(--muted); border-radius: 4px; padding: 0 6px; cursor: pointer; line-height: 18px;
}
.nad:hover { color: var(--accent); border-color: var(--accent); }

/* lists */
.list { display: flex; flex-direction: column; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 11px 4px; border-top: 1px solid var(--line); color: inherit;
}
.list > .row:first-child { border-top: 0; }
.link-row:hover { background: var(--bg); }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; color: var(--ink); }
.row-sub { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; }
.row-end { text-align: right; font-size: 13px; display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.empty { color: var(--muted); padding: 14px 4px; }
.token {
  width: 38px; height: 38px; flex: none; border-radius: 50%; background: var(--accent-soft); color: var(--accent);
  display: grid; place-items: center; font-weight: 700;
}
.q-done, .q-cancelled { opacity: .62; }
.q-cancelled .row-title { text-decoration: line-through; }
.q-in { background: var(--blue-soft); border-radius: 8px; }
.avatar {
  width: 38px; height: 38px; flex: none; border-radius: 50%; background: var(--bg); color: var(--muted);
  display: grid; place-items: center; font-weight: 650;
}
.date-box {
  width: 48px; flex: none; text-align: center; border: 1px solid var(--line); border-radius: 8px; padding: 3px 0; line-height: 1.1;
}
.date-box b { display: block; font-size: 18px; }
.date-box span { font-size: 11px; color: var(--muted); }

.chip { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px; white-space: nowrap; background: var(--bg); color: var(--muted); }
.chip-waiting { background: var(--warn-soft); color: var(--warn); }
.chip-in { background: var(--blue-soft); color: var(--blue); }
.chip-done { background: var(--good-soft); color: var(--good); }
.chip-booked { background: var(--accent-soft); color: var(--accent); }
.chip-due { background: var(--bad-soft); color: var(--bad); }

.stats { display: flex; gap: 10px; flex-wrap: wrap; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 14px; min-width: 92px; }
.stat b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }
.stats.big { margin-bottom: 16px; }
.stats.big .stat { flex: 1; padding: 12px 16px; }
.stats.big .stat b { font-size: 22px; }
.stat.good b { color: var(--good); }
.stat.warn b { color: var(--warn); }

.kv { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-top: 1px solid var(--line); }
.kv:first-of-type { border-top: 0; }
.kv span { color: var(--muted); }
.kv b { font-weight: 550; text-align: right; }

.alert { background: var(--bad-soft); color: var(--bad); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px; }
.note { background: var(--warn-soft); color: #6b4106; border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px; font-size: 14px; }
.url { font: 600 16px ui-monospace, Menlo, Consolas, monospace; background: var(--accent-soft); color: var(--accent); padding: 10px 12px; border-radius: 8px; margin: 8px 0; word-break: break-all; }

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { text-align: right; padding: 8px 10px; border-top: 1px solid var(--line); white-space: nowrap; }
.table th:first-child, .table td:first-child { text-align: left; }
.table th { font-size: 12px; color: var(--muted); font-weight: 600; border-top: 0; }

/* picker */
.picker-bar { display: flex; gap: 8px; }
.picker-results:not(:empty) { margin-top: 6px; }
.picker-form { margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--line-strong); }
.day-bar { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.day-bar .input { width: auto; }
.book-opts { margin-bottom: 12px; }

/* visit */
.visit-head { align-items: center; }
.endo .endo-body { display: none; }
.endo.on .endo-body { display: block; }
.endo .endo-body > .field { margin-top: 12px; }
.photo-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 14px 0 10px; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.thumb { margin: 0; position: relative; background: var(--bg); border-radius: 8px; padding: 6px; }
.thumb img { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: 6px; display: block; background: #000; }
.thumb .input { margin-top: 6px; }
.thumb .icon-btn { position: absolute; top: 10px; right: 10px; background: rgba(255,255,255,.9); }
.thumb.drawing img { aspect-ratio: 1000/560; object-fit: contain; background: #fff; border: 1px solid var(--line); }
.sk-add { display: flex; gap: 6px; flex-wrap: wrap; }
.hand-rx { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; padding: 10px 12px; margin-bottom: 14px; background: var(--bg); border-radius: 10px; }
.hand-rx.has { align-items: flex-start; flex-wrap: nowrap; }
.hand-rx.has img { width: 130px; flex: none; background: #fff; border: 1px solid var(--line); border-radius: 6px; cursor: pointer; }
.hand-rx p { margin: 2px 0 8px; }
.input.ai, .med-row.ai .input { background: var(--warn-soft); border-color: var(--warn); }
.med-row.ai-check { outline: 2px solid var(--bad); outline-offset: 2px; border-radius: 6px; }
.ai-note { background: var(--warn-soft); border: 1px solid var(--warn); border-radius: 10px; padding: 10px 12px; margin-bottom: 14px; font-size: 14px; }
.ai-note ul { margin: 6px 0; padding-left: 20px; }

.med-head, .med-row { display: grid; grid-template-columns: 2.4fr .9fr 1.1fr 1fr 1.3fr 34px; gap: 6px; }
.med-head { font-size: 12px; color: var(--muted); font-weight: 600; padding: 0 2px 4px; }
.med-row { margin-bottom: 6px; align-items: center; }

.action-bar {
  position: sticky; bottom: 12px; z-index: 5; display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px;
  box-shadow: 0 6px 24px rgba(20, 32, 41, .12);
}
.save-state { flex: 1; color: var(--good); font-size: 13px; font-weight: 600; }
.save-state.busy { color: var(--muted); }
.save-state.bad { color: var(--bad); }

/* capture overlay */
.overlay { position: fixed; inset: 0; background: rgba(5, 12, 16, .92); z-index: 50; display: grid; place-items: center; padding: 16px; }
.overlay.flash { background: rgba(255, 255, 255, .5); }
.capture { width: min(1100px, 100%); display: flex; flex-direction: column; gap: 10px; color: #fff; }
.capture-head, .capture-foot { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.capture-head .input { width: auto; max-width: 320px; }
.capture-head .muted { color: #b9c5cc; flex: 1; }
.capture-head .btn.ghost { color: #fff; }
.capture-foot { justify-content: space-between; }
.capture-foot .muted { color: #b9c5cc; }
.upi-card { background: #fff; color: var(--ink); border-radius: 14px; padding: 20px; width: min(380px, 100%); text-align: center; display: flex; flex-direction: column; gap: 6px; }
.upi-amt { font-size: 26px; font-weight: 700; }
.upi-qr svg { width: 100%; max-width: 300px; height: auto; display: block; margin: 4px auto; }
.upi-card .actions { justify-content: center; }
.capture video { width: 100%; max-height: 72vh; background: #000; border-radius: 10px; }

/* sketch pad */
.sketch-ov { padding: 10px; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.sketch { display: flex; flex-direction: column; align-items: center; gap: 8px; width: 100%; height: 100%; }
.sketch-bar { display: flex; gap: 8px 14px; align-items: center; flex-wrap: wrap; width: 100%; color: #fff; }
.sk-title { font-size: 17px; }
.sk-cap { width: 200px; color: var(--ink); }
.sk-group { display: flex; gap: 6px; align-items: center; }
.sk-end { margin-left: auto; }
.sk-color { width: 34px; height: 34px; border-radius: 50%; background: var(--c); border: 3px solid #fff; cursor: pointer; padding: 0; }
.sk-color.on { box-shadow: 0 0 0 3px var(--accent), 0 0 0 5px #fff; }
.sk-tool.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.sk-light { color: #fff; }
.sketch-stage { position: relative; background: #fff; border-radius: 6px; box-shadow: 0 4px 30px rgba(0,0,0,.4); }
.sketch-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.sk-hint { margin: 0; color: #b9c5cc; font-size: 13px; text-align: center; }

/* auth */
body.auth { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.auth-card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px; width: min(360px, 100%); text-align: center; box-shadow: var(--shadow); }
.auth-card.wide-card { width: min(560px, 100%); text-align: left; }
.auth-card h1 { margin-bottom: 4px; }
.auth-card .grid { margin: 18px 0 8px; }
.auth-card .btn.wide { margin-top: 12px; }
.auth-logo { width: 52px; height: 52px; margin-bottom: 10px; }
.pin { text-align: center; font-size: 26px; letter-spacing: .4em; margin-top: 14px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; font-weight: 500; transition: .2s; z-index: 60;
  max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { background: var(--bad); }

/* print preview and paper */
body.print-view .nav { display: none; }
body.print-view main { max-width: none; padding: 16px; }
.print-bar { display: flex; gap: 8px; align-items: center; margin: 0 auto 16px; max-width: 210mm; flex-wrap: wrap; }
.sheet { background: #fff; color: #000; margin: 0 auto; padding: 12mm; box-shadow: 0 2px 16px rgba(0,0,0,.12); font-size: 12.5px; line-height: 1.45; }
.sheet.a5 { width: 148mm; min-height: 210mm; padding: 9mm; }
.sheet.a4 { width: 210mm; min-height: 297mm; font-size: 13.5px; }
.lh { display: flex; justify-content: space-between; gap: 16px; border-bottom: 2px solid #0f766e; padding-bottom: 8px; margin-bottom: 10px; }
.lh-right { text-align: right; }
.lh-doctor { font-size: 1.45em; font-weight: 700; color: #0f766e; }
.lh-clinic { font-size: 1.2em; font-weight: 700; }
.lh-qual { font-weight: 600; }
.lh-small { font-size: .88em; color: #333; }
.pt-line { display: grid; grid-template-columns: 2fr 1fr 1fr 1.3fr; gap: 8px; border-bottom: 1px solid #bbb; padding: 4px 0 8px; margin-bottom: 8px; }
.pt-line span { display: block; font-size: .78em; color: #555; text-transform: uppercase; letter-spacing: .04em; }
.pallergy { color: #b42318; font-weight: 700; margin-bottom: 6px; }
.pblock { margin: 6px 0; break-inside: avoid; }
.pblock h3 { font-size: .8em; text-transform: uppercase; letter-spacing: .05em; color: #555; margin: 0 0 2px; }
.rx-mark { font-size: 2em; font-weight: 700; font-family: Georgia, serif; margin: 10px 0 2px; }
.rx { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
.rx td { padding: 4px 0; vertical-align: top; border-bottom: 1px dotted #ccc; }
.rx td.n { width: 22px; color: #555; }
.rx-sub { color: #333; }
.pfollow { margin-top: 10px; padding: 6px 10px; border: 1px solid #0f766e; border-radius: 6px; display: inline-block; }
.sign { margin-top: 28px; text-align: right; }
.sign-line { display: inline-block; width: 45%; border-bottom: 1px solid #000; height: 28px; }
.pfoot { margin-top: 14px; border-top: 1px solid #ccc; padding-top: 4px; font-size: .82em; color: #555; text-align: center; }
.ptitle { text-align: center; font-size: 1.15em; margin: 4px 0 10px; text-transform: uppercase; letter-spacing: .04em; }
.pimgs { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; margin: 8px 0; }
.pimgs.n1 { grid-template-columns: 1fr; }
.pimgs.n1 img { max-height: 90mm; }
.pimgs figure { margin: 0; break-inside: avoid; text-align: center; }
.pimgs img { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: 4px; background: #000; }
.pimgs figcaption { font-size: .85em; color: #333; }
.pimgs.pdraw img { aspect-ratio: auto; object-fit: contain; background: #fff; }
.pimgs.pdraw.n1 { width: 75%; margin-inline: auto; }
.hand img { display: block; width: 100%; object-fit: contain; object-position: left top; }
.sheet.a5 .hand img { max-height: 128mm; }
.sheet.a4 .hand img { max-height: 200mm; }

@media print {
  body { background: #fff; }
  .nav, .print-bar, #toast { display: none !important; }
  main { padding: 0 !important; max-width: none; }
  .sheet { box-shadow: none; padding: 0; width: auto !important; min-height: 0 !important; }
}

/* phones and small tablets */
@media (max-width: 900px) {
  .cols { grid-template-columns: 1fr; }
  .g3, .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 760px) {
  .app { display: block; }
  .nav {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; height: auto; width: auto; z-index: 20;
    flex-direction: row; justify-content: space-around; padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--line);
  }
  .brand { display: none; }
  .nav a { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 4px; flex: 1; }
  main { padding: 16px 16px 150px; }
  h1 { font-size: 21px; }
  .card { padding: 14px; }
  .g2, .g3, .g4 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .row { flex-wrap: wrap; }
  .row-actions { width: 100%; justify-content: flex-start; padding-left: 50px; }
  .q-in { padding: 11px 8px; }
  .med-head { display: none; }
  .med-row { grid-template-columns: 1fr 1fr; padding: 10px; background: var(--bg); border-radius: 8px; }
  .med-row .m-name { grid-column: 1 / -1; }
  .med-row .m-notes { grid-column: 1 / 2; }
  .action-bar { bottom: calc(66px + env(safe-area-inset-bottom)); gap: 6px; padding: 8px; }
  .action-bar .btn { min-height: 34px; padding: 4px 10px; font-size: 13px; }
  .save-state { flex-basis: 100%; font-size: 12px; }
  .action-bar .icon-btn.danger { height: 34px; padding: 0 8px; }
  .stats .stat { flex: 1; min-width: 0; }
  .picker-bar { flex-direction: column; }
  .page-head { align-items: flex-start; }
  body.print-view .sheet { transform-origin: top left; }
}
