:root {
  --brand: #2a2680;
  --brand-2: #3a35a0;
  --accent: #e0262f;
  --bg: #f6f6fb;
  --surface: #ffffff;
  --text: #1b1a33;
  --muted: #5d5c78;
  --line: #e2e1ee;
  --field: #ffffff;
  --ok: #157a4a;   --ok-bg: #e6f5ed;
  --warn: #9a5b00; --warn-bg: #fff3dc;
  --bad: #b3141c;  --bad-bg: #fde8e9;
  --info: #2a2680; --info-bg: #ecebfa;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(27, 26, 51, .06), 0 4px 16px rgba(27, 26, 51, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --brand: #a9a5ff;
    --brand-2: #8c87f5;
    --accent: #ff5a62;
    --bg: #12121c;
    --surface: #1c1c2a;
    --text: #ececf5;
    --muted: #a3a2bb;
    --line: #2f2e44;
    --field: #15151f;
    --ok: #5bd69a;   --ok-bg: #143224;
    --warn: #f5b84a; --warn-bg: #352812;
    --bad: #ff7b81;  --bad-bg: #3a1619;
    --info: #b9b6ff; --info-bg: #23224a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
a { color: var(--brand); }
h1 { font-size: 1.6rem; line-height: 1.2; margin: .2rem 0 .3rem; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 1.4rem 0 .6rem; }
h3 { font-size: .95rem; margin: 1rem 0 .3rem; }
.sub { color: var(--muted); margin: 0 0 1rem; }
.fine { color: var(--muted); font-size: .8rem; display: block; margin-top: .4rem; }
.print-only { display: none; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .6rem max(16px, env(safe-area-inset-left));
  background: linear-gradient(135deg, #3a35a0, #1f1c5c);
  color: #fff;
}
.brand { display: flex; align-items: center; gap: .6rem; color: #fff; text-decoration: none; }
.brand img { border-radius: 9px; box-shadow: 0 0 0 1px rgba(255,255,255,.25); }
.brand span { display: flex; flex-direction: column; line-height: 1.1; }
.brand small { opacity: .8; font-size: .75rem; }
.nav { display: flex; gap: .2rem; }
.nav a { color: #fff; opacity: .8; text-decoration: none; padding: .4rem .7rem; border-radius: 999px; font-size: .95rem; }
.nav a.active, .nav a:hover { opacity: 1; background: rgba(255,255,255,.14); }
.tools { margin-left: auto; display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.unit-switch { display: flex; background: rgba(255,255,255,.14); border-radius: 999px; padding: 3px; }
.unit-switch button { border: 0; background: none; color: #fff; padding: .3rem .7rem; border-radius: 999px; font: inherit; font-size: .85rem; cursor: pointer; opacity: .8; }
.unit-switch button[aria-pressed="true"] { background: #fff; color: #2a2680; opacity: 1; font-weight: 600; }
.patient-chip { display: flex; align-items: center; gap: .3rem; background: var(--accent); border-radius: 999px; padding: .2rem .3rem .2rem .8rem; font-size: .85rem; }
.patient-chip a { color: #fff; text-decoration: none; font-weight: 600; }
.patient-chip button { border: 0; background: rgba(255,255,255,.25); color: #fff; width: 22px; height: 22px; border-radius: 50%; cursor: pointer; line-height: 1; }

main { max-width: 1100px; margin: 0 auto; padding: 1.2rem 16px 3rem; }
.footer { text-align: center; color: var(--muted); font-size: .8rem; padding: 1rem 16px 2rem; }

/* ---------- Home ---------- */
.intro p { color: var(--muted); margin: 0 0 1rem; max-width: 60ch; }
input[type="search"] {
  width: 100%; max-width: 480px; padding: .7rem 1rem; border-radius: 999px;
  border: 1px solid var(--line); background: var(--field); color: var(--text); font: inherit;
}
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: .8rem; }
.card {
  display: flex; flex-direction: column; gap: .2rem; padding: 1rem 1.1rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); color: var(--text); text-decoration: none;
  border-left: 4px solid var(--brand-2); transition: transform .12s ease;
}
.card:hover { transform: translateY(-2px); }
.card span { color: var(--muted); font-size: .85rem; }
[hidden] { display: none !important; }

/* ---------- Panels & forms ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); margin-bottom: 1rem; }
.panel.narrow { max-width: 520px; }
.back { display: inline-block; margin-bottom: .5rem; text-decoration: none; font-size: .9rem; }
.calc-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 1rem; align-items: start; }
.field { display: flex; flex-direction: column; gap: .3rem; margin: 0 0 .9rem; border: 0; padding: 0; }
.field > span, .field legend { font-weight: 600; font-size: .9rem; padding: 0; }
.opt { font-weight: 400; color: var(--muted); font-size: .78rem; }
.hint { color: var(--muted); font-size: .78rem; }
input:not([type="radio"]):not([type="checkbox"]):not([type="search"]), select, textarea {
  width: 100%; padding: .6rem .7rem; border: 1px solid var(--line); border-radius: 10px;
  background: var(--field); color: var(--text); font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-2); outline-offset: 1px; }
.input-unit { display: flex; align-items: stretch; }
.input-unit input { border-radius: 10px 0 0 10px !important; }
.input-unit em { font-style: normal; display: flex; align-items: center; padding: 0 .7rem; border: 1px solid var(--line); border-left: 0; border-radius: 0 10px 10px 0; color: var(--muted); font-size: .85rem; white-space: nowrap; background: var(--bg); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; width: fit-content; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: .5rem 1.1rem; }
.seg input:checked + span { background: var(--brand-2); color: #fff; }
.seg input:focus-visible + span { outline: 2px solid var(--brand-2); }
.check { display: flex; gap: .6rem; align-items: flex-start; padding: .5rem 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.check input { width: 18px; height: 18px; margin-top: .15rem; accent-color: var(--brand-2); flex: none; }
.check span { display: flex; flex-direction: column; }

button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: .3rem;
  background: var(--brand-2); color: #fff; border: 1px solid transparent; border-radius: 10px;
  padding: .6rem 1.1rem; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
button:disabled { opacity: .7; cursor: default; }
.ghost { background: transparent; color: var(--brand); border-color: var(--line); }
.small { padding: .3rem .6rem; font-size: .8rem; font-weight: 500; }
.danger { color: var(--bad); }
.prefill-note { background: var(--info-bg); color: var(--info); padding: .5rem .7rem; border-radius: 10px; font-size: .85rem; margin: 0 0 1rem; }

/* ---------- Results ---------- */
.results:empty { display: none; }
.result { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: .2rem 1rem; padding: .55rem 0; border-bottom: 1px solid var(--line); }
.result:last-of-type { border-bottom: 0; }
.result .label { color: var(--muted); font-size: .9rem; }
.result .value { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.result .value small { font-weight: 500; color: var(--muted); }
.result .note { flex-basis: 100%; color: var(--muted); font-size: .78rem; }
.result.big .value { font-size: 1.8rem; color: var(--brand); }
.result[class*="tone-"] .value { padding: .1rem .6rem; border-radius: 8px; font-size: .95rem; }
.tone-ok .value { background: var(--ok-bg); color: var(--ok) !important; }
.tone-warn .value { background: var(--warn-bg); color: var(--warn) !important; }
.tone-bad .value { background: var(--bad-bg); color: var(--bad) !important; }
.result.big[class*="tone-"] .value { font-size: 1.4rem; }
.notes { margin: .6rem 0 0; padding-left: 1.1rem; color: var(--muted); font-size: .85rem; }
.msg { color: var(--muted); margin: 0; }
.msg.bad { color: var(--bad); }
.actions { margin-top: 1rem; }
.form-heading { color: var(--brand); border-bottom: 1px solid var(--line); padding-bottom: .3rem; margin: 1.2rem 0 .7rem; }
.form-heading:first-child { margin-top: 0; }
.ext-link { font-size: .85rem; margin: .8rem 0 0; }
.evidence summary { cursor: pointer; font-weight: 600; color: var(--brand); }
.formula { margin-top: .8rem; font-size: .9rem; background: var(--bg); padding: .8rem; border-radius: 10px; overflow-x: auto; }
.refs, .evidence ul { padding-left: 1.2rem; font-size: .85rem; }
.refs li { margin-bottom: .4rem; }

/* ---------- Patients ---------- */
.row-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: .8rem; }
.btns { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.patient-list { list-style: none; padding: 0; margin: 1rem 0; display: grid; gap: .5rem; }
.patient-list a { display: flex; justify-content: space-between; flex-wrap: wrap; gap: .3rem 1rem; padding: .8rem 1rem; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; text-decoration: none; color: var(--text); }
.patient-list a span { color: var(--muted); font-size: .9rem; }
.empty { color: var(--muted); }
.followups { list-style: none; padding: 0; margin: 0; }
.followups li { display: flex; justify-content: space-between; padding: .4rem 0; border-bottom: 1px solid var(--line); }
.followups li:last-child { border-bottom: 0; }
.followups .overdue span { color: var(--bad); font-weight: 600; }
.pt-notes { white-space: pre-wrap; margin: 0; font-size: .9rem; }
.followup-set { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.followup-set label { display: flex; gap: .6rem; align-items: center; font-weight: 600; }
.followup-set input { width: auto !important; }
.insights { list-style: none; padding: 0; margin: 0; display: grid; gap: .5rem; }
.insights li { padding: .6rem .8rem; border-radius: 10px; border-left: 4px solid; font-size: .92rem; }
.insights cite { display: block; font-style: normal; font-size: .75rem; opacity: .8; margin-top: .2rem; }
.lvl-ok { background: var(--ok-bg); border-color: var(--ok); }
.lvl-warn { background: var(--warn-bg); border-color: var(--warn); }
.lvl-bad { background: var(--bad-bg); border-color: var(--bad); }
.lvl-info { background: var(--info-bg); border-color: var(--info); }
.charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 1rem; }
.chart-card { margin: 0; }
.chart-card figcaption { display: flex; justify-content: space-between; gap: .5rem; font-weight: 600; margin-bottom: .4rem; }
.chart-card figcaption small { color: var(--muted); font-weight: 400; }
.chart-card figcaption b { color: var(--brand); font-variant-numeric: tabular-nums; }
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .band { stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 5 5; opacity: .6; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .line { fill: none; stroke: var(--brand-2); stroke-width: 2.5; vector-effect: non-scaling-stroke; }
.chart .dot { fill: var(--surface); stroke: var(--brand-2); stroke-width: 2.5; vector-effect: non-scaling-stroke; }
.chart .s2.line, .chart .s2.dot { stroke: var(--accent); }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--brand-2); margin: 0 .3rem 0 .6rem; vertical-align: middle; }
.key.s2 { background: var(--accent); }
.history { list-style: none; padding: 0; margin: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.history li { display: flex; gap: 1rem; align-items: center; padding: .6rem 1rem; border-bottom: 1px solid var(--line); font-size: .9rem; }
.history li:last-child { border-bottom: 0; }
.history .date { color: var(--muted); font-variant-numeric: tabular-nums; flex: none; }
.history li > span:nth-child(2) { flex: 1; }
.danger-zone { margin-top: 2rem; }
.visit-group { border: 1px solid var(--line); }
.visit-group legend { font-weight: 700; color: var(--brand); padding: 0 .3rem; }
.visit-group .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 0 1rem; }
.narrow-field { max-width: 240px; margin: 0; }

@media (max-width: 760px) {
  .calc-layout { grid-template-columns: 1fr; }
  .tools { margin-left: 0; width: 100%; justify-content: space-between; }
  .nav { order: 3; width: 100%; }
  h1 { font-size: 1.35rem; }
  .charts { grid-template-columns: 1fr; }
}

/* ---------- Print ---------- */
@media print {
  :root { --bg: #fff; --surface: #fff; --text: #000; --muted: #444; --line: #ccc; --shadow: none; color-scheme: light; }
  .no-print, .topbar, .footer, button { display: none !important; }
  .print-only { display: block; }
  .letterhead { display: grid; grid-template-columns: auto 1fr; gap: .2rem 1rem; align-items: center; border-bottom: 3px solid #2a2680; padding: 0 0 .6rem; margin: 0 0 1rem; font-size: .85rem; }
  .letterhead strong { font-size: 1.2rem; color: #2a2680; }
  .letterhead ul { grid-column: 1 / -1; margin: .2rem 0 0; padding-left: 1rem; color: #444; }
  main { padding: 0; max-width: none; }
  .panel, .chart-card, .history { break-inside: avoid; box-shadow: none; }
  .charts { grid-template-columns: 1fr 1fr; }
}

/* ---------- Paste report ---------- */
#rp-text { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85rem; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 0 1rem; }
.found { width: 100%; border-collapse: collapse; font-size: .9rem; }
.found th { text-align: left; color: var(--muted); font-weight: 600; font-size: .8rem; padding: .3rem; border-bottom: 1px solid var(--line); }
.found td { padding: .4rem .3rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
.found td .field { margin: 0; min-width: 150px; }
.found tr.assumed td { background: var(--warn-bg); }
.found .src { color: var(--muted); font-family: ui-monospace, Menlo, monospace; font-size: .72rem; }
.found input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--brand-2); }
.mini-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1rem; }
.mini-calc { border: 1px solid var(--line); border-radius: 12px; padding: .6rem .9rem; }
.mini-calc h3 { margin: 0 0 .3rem; font-size: .95rem; }
.mini-calc h3 a { text-decoration: none; }
@media (max-width: 760px) {
  .found thead { display: none; }
  .found tr { display: grid; grid-template-columns: auto 1fr; gap: .2rem .6rem; padding: .5rem 0; border-bottom: 1px solid var(--line); }
  .found td { border: 0; padding: 0; }
  .found td:nth-child(3), .found td:nth-child(4) { grid-column: 1 / -1; }
}
.dropzone { display: flex; flex-direction: column; gap: .2rem; align-items: center; text-align: center; padding: 1.4rem 1rem; margin-bottom: .6rem; border: 2px dashed var(--line); border-radius: var(--radius); cursor: pointer; color: var(--muted); }
.dropzone strong { color: var(--brand); }
.dropzone span { font-size: .85rem; }
.dropzone:hover, .dropzone.over { border-color: var(--brand-2); background: var(--info-bg); }
#rp-text-wrap { margin: .4rem 0 1rem; }
#rp-text-wrap summary { cursor: pointer; color: var(--brand); font-size: .9rem; }
.prefill-note p { margin: 0 0 .5rem; }

/* ---------- Diet leaflet ---------- */
.leaflet h1 { color: var(--brand); }
.lf-target { background: var(--info-bg); padding: .6rem .8rem; border-radius: 10px; margin-bottom: .6rem; }
.lf-table td:last-child { white-space: nowrap; text-align: right; }
.lf-tips li { margin-bottom: .3rem; }
.lf-sign { margin-top: 1rem; font-weight: 600; }
@media print { .referral { break-before: page; } .leaflet { border: 0; } }
.btns select { width: auto; }

/* ---------- Sync ---------- */
.sync-badge { font-size: .78rem; text-decoration: none; color: #fff; padding: .2rem .6rem; border-radius: 999px; background: rgba(255,255,255,.14); white-space: nowrap; }
.sync-badge.err { background: var(--accent); }
.sync-badge.busy { opacity: .8; }
.sync-line.ok b { color: var(--ok); }
.sync-line.bad b { color: var(--bad); }
#sync-form { max-width: 520px; margin-top: .8rem; }
.code-box { font: 700 1.5rem/1.3 ui-monospace, Menlo, monospace; letter-spacing: .12em; text-align: center; padding: 1rem; margin: .8rem 0; border: 2px dashed var(--brand-2); border-radius: 12px; background: var(--info-bg); color: var(--info); word-break: break-all; }
.sync-more { margin: .8rem 0; }
.sync-more summary { cursor: pointer; color: var(--brand); font-weight: 600; }
.sync-more form { margin-top: .7rem; max-width: 520px; }
.role-pill { display: inline-block; padding: .1rem .6rem; border-radius: 999px; font-size: .8rem; font-weight: 600; background: var(--bg); border: 1px solid var(--line); margin-right: .4rem; }
.role-pill.primary { background: var(--brand-2); color: #fff; border-color: transparent; }
.carb-tag { font-size: .8rem; font-weight: 600; color: var(--info); background: var(--info-bg); padding: .05rem .5rem; border-radius: 999px; margin-left: .4rem; }
.lf-goal { font-weight: 600; color: var(--brand); margin: .3rem 0 .6rem; }
.lf-name { font: 800 1.6rem/1.2 Georgia, "Times New Roman", serif; color: var(--brand); margin: 0 0 .2rem; letter-spacing: .01em; }
.lf-name::before { content: "✦ "; color: var(--accent); }
@media print { .lf-name { color: #2a2680; } .lf-name::before { color: #e0262f; } }
