:root {
  --bg: #f4f5f7; --surface: #fff; --surface-2: #eef0f3; --border: #e3e6ea;
  --text: #1a1d24; --muted: #5d6470; --accent: #0f766e; --accent-soft: #e2f3f0; --on-accent: #fff;
  --warn: #8a5300; --warn-soft: #fcf0dc; --danger: #b42318; --danger-soft: #fdeceb;
  --ok: #1b7339; --ok-soft: #e5f4e9; --info: #2f55c4; --info-soft: #e8edfc;
  --violet: #6d3fc0; --violet-soft: #f0eafb; --band: rgba(27, 115, 57, .10); --line: #0f766e;
  --rose: #b4235f; --rose-soft: #fce8f0;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318; --surface: #1b1e25; --surface-2: #252932; --border: #30353f;
    --text: #e9ebef; --muted: #a3aab6; --accent: #5eead4; --accent-soft: #143430; --on-accent: #0b1f1c;
    --warn: #f5c26b; --warn-soft: #372a14; --danger: #ff8f85; --danger-soft: #3a1d1b;
    --ok: #7ddc9a; --ok-soft: #163021; --info: #9db5ff; --info-soft: #1d2643;
    --violet: #c5a8ff; --violet-soft: #2a2140; --band: rgba(125, 220, 154, .12); --line: #5eead4;
    --rose: #ff9cc4; --rose-soft: #3a1b29;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px; line-height: 1.4; color: var(--text); background: var(--bg);
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }
a { color: var(--accent); }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* Sign-in gate: the only thing visible until the session check passes. */
.gate { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 24px; overflow: auto; }
.gate-box { max-width: 360px; width: 100%; display: flex; flex-direction: column; gap: 14px; text-align: center; align-items: stretch; }
.gate-box h1 { font-size: 22px; }
.gate-box p { color: var(--muted); }
.gate-icon { width: 56px; height: 56px; margin: 0 auto 4px; }
.spinner { width: 28px; height: 28px; margin: 0 auto; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.btn { display: block; text-align: center; text-decoration: none; font-weight: 600; padding: 13px 16px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.hint { font-size: 13px; color: var(--muted); }

.app { position: fixed; inset: 0; display: flex; flex-direction: column; }
.top { flex: none; background: var(--surface); border-bottom: 1px solid var(--border); padding: calc(8px + env(safe-area-inset-top)) 12px 8px; display: flex; align-items: center; gap: 10px; }
.who { display: flex; align-items: center; gap: 10px; background: none; border: 0; padding: 4px 6px 4px 2px; cursor: pointer; min-height: 44px; min-width: 0; text-align: left; border-radius: 10px; }
.who:active { background: var(--surface-2); }
.av { flex: none; width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; font-weight: 600; font-size: 14px; background: var(--accent-soft); color: var(--accent); }
.who-txt { display: flex; flex-direction: column; min-width: 0; }
.who-name { font-weight: 650; font-size: 17px; display: flex; align-items: center; gap: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.live { margin-left: auto; display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.dot.stale { background: var(--warn); }

.main { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding: 14px 14px 24px; display: flex; flex-direction: column; gap: 12px; outline: none; }
.main > * { width: 100%; max-width: 640px; margin-left: auto; margin-right: auto; }
.tabs { flex: none; display: grid; grid-template-columns: repeat(5, 1fr); background: var(--surface); border-top: 1px solid var(--border); padding: 4px 4px calc(6px + env(safe-area-inset-bottom)); }
.tab { background: none; border: 0; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 0; min-height: 52px; font-size: 11px; color: var(--muted); cursor: pointer; }
.tab svg { width: 24px; height: 24px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab[aria-current="page"] { color: var(--accent); font-weight: 600; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px; display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow); }
.card-h { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card-h h2 { font-size: 13px; font-weight: 600; color: var(--muted); }
.link { background: none; border: 0; color: var(--accent); font-size: 13px; font-weight: 600; padding: 6px 0 6px 8px; cursor: pointer; text-decoration: none; }
.rows { display: flex; flex-direction: column; }
.row { display: flex; gap: 12px; align-items: flex-start; padding: 10px 0; border-top: 1px solid var(--border); width: 100%; text-align: left; background: none; border-left: 0; border-right: 0; border-bottom: 0; }
.rows > .row:first-child { border-top: 0; padding-top: 0; }
.rows > .row:last-child { padding-bottom: 0; }
button.row { cursor: pointer; }
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; overflow-wrap: anywhere; }
.row-t { font-weight: 600; }
.sub { font-size: 13px; color: var(--muted); }
.end { text-align: right; flex: none; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-variant-numeric: tabular-nums; max-width: 45%; }
.date { flex: none; width: 44px; text-align: center; border-radius: 10px; background: var(--surface-2); padding: 4px 0; line-height: 1.15; }
.date b { display: block; font-size: 17px; }
.date span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .02em; }
.date.future { background: var(--accent-soft); }
.date.future span, .date.future b { color: var(--accent); }
.notes { white-space: pre-wrap; font-size: 13px; color: var(--text); background: var(--surface-2); border-radius: 10px; padding: 10px 12px; margin-top: 6px; overflow-wrap: anywhere; }
.values { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; }
.value { display: flex; justify-content: space-between; gap: 10px; font-size: 13px; }
.value b { font-variant-numeric: tabular-nums; }

.b { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.b.ok { background: var(--ok-soft); color: var(--ok); }
.b.warn { background: var(--warn-soft); color: var(--warn); }
.b.danger { background: var(--danger-soft); color: var(--danger); }
.b.info, .b.doctor_visit { background: var(--info-soft); color: var(--info); }
.b.lab_test, .b.lab_result { background: var(--accent-soft); color: var(--accent); }
.b.procedure { background: var(--violet-soft); color: var(--violet); }

.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); gap: 8px; }
.fact { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 8px 10px; display: flex; flex-direction: column; }
.fact span { font-size: 11px; color: var(--muted); }
.fact b { font-size: 15px; font-variant-numeric: tabular-nums; }
.fact small { font-size: 11px; color: var(--muted); }

.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; margin-left: -14px !important; margin-right: -14px !important; padding: 0 14px; flex: none; width: auto !important; max-width: none !important; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 8px 14px; min-height: 38px; cursor: pointer; font-size: 14px; }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 10px; padding: 3px; flex: none; }
.seg button { background: none; border: 0; border-radius: 8px; padding: 8px; min-height: 38px; cursor: pointer; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0, 0, 0, .12); }
.month { font-size: 13px; font-weight: 600; color: var(--muted); margin-top: 6px; margin-bottom: -4px; padding: 0 2px; }

.readout { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.readout b { font-size: 28px; font-weight: 650; font-variant-numeric: tabular-nums; }
.chart svg { display: block; width: 100%; height: 190px; touch-action: pan-y; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .band { fill: var(--band); }
.chart .ln { fill: none; stroke: var(--line); stroke-width: 2.5; stroke-linejoin: round; }
.chart .pt { fill: var(--surface); stroke: var(--line); stroke-width: 2.5; }
.chart .pt.mild { stroke: var(--warn); }
.chart .pt.severe { stroke: var(--danger); }
.chart .pt.sel { fill: var(--line); }
.chart .pt.sel.mild { fill: var(--warn); }
.chart .pt.sel.severe { fill: var(--danger); }
.chart .hit { fill: transparent; cursor: pointer; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; }
.stat b { font-size: 22px; font-variant-numeric: tabular-nums; }
.stat.danger b { color: var(--danger); }
.menu .row { align-items: center; min-height: 52px; }
.chev { color: var(--muted); flex: none; align-self: center; }
.danger-text { color: var(--danger); }
.back { display: flex; align-items: center; gap: 4px; background: none; border: 0; color: var(--accent); font-weight: 600; padding: 6px 0; min-height: 40px; cursor: pointer; align-self: flex-start; width: auto !important; margin-left: max(0px, calc((100% - 640px) / 2)) !important; }
.empty { color: var(--muted); font-size: 14px; }
.addr { background: none; border: 0; padding: 0; color: var(--accent); text-align: left; cursor: pointer; font-size: 13px; text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--accent) 40%, transparent); text-underline-offset: 2px; }
a.tel { color: var(--accent); font-weight: 600; text-decoration: none; white-space: nowrap; }
.mask { font-variant-numeric: tabular-nums; letter-spacing: .02em; }
.skeleton { height: 76px; border-radius: 14px; background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: shimmer 1.2s ease-in-out infinite; border: 1px solid var(--border); }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }
.error-card { border-color: var(--warn); }

.sheet-bg { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); display: flex; align-items: flex-end; justify-content: center; z-index: 30; }
.sheet { background: var(--surface); width: 100%; max-width: 640px; max-height: 80vh; overflow: auto; border-radius: 18px 18px 0 0; padding: 8px 16px calc(16px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 4px; }
.grab { width: 36px; height: 5px; border-radius: 3px; background: var(--border); align-self: center; margin-bottom: 8px; flex: none; }
.sheet h2 { font-size: 17px; margin-bottom: 6px; }
.pick { display: flex; align-items: center; gap: 12px; background: none; border: 0; border-top: 1px solid var(--border); padding: 10px 0; min-height: 56px; text-align: left; cursor: pointer; width: 100%; text-decoration: none; color: var(--text); }
#sheet-body > .pick:first-child { border-top: 0; }
.pick .row-main { flex: 1; }
.check { color: var(--accent); font-weight: 700; }
.sheet.tall { height: 88vh; max-height: 88vh; }
.sheet-note { margin-top: 10px; }

.icon-btn { flex: none; width: 40px; height: 40px; display: grid; place-items: center; background: none; border: 0; border-radius: 10px; color: var(--muted); cursor: pointer; }
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.live { margin-right: -4px; }

.note { background: var(--info-soft); color: var(--info); border-radius: 12px; padding: 10px 12px; font-size: 13px; }
.orig, .orig-name { display: block; font-size: 12px; color: var(--muted); font-weight: 400; }
.orig { font-style: italic; margin-top: 2px; }
.orig-notes { color: var(--muted); font-style: italic; }
.count-line { margin-bottom: 4px; }

/* Collapsible overview sections */
.fold { display: block; padding: 0; }
.fold > summary { list-style: none; cursor: pointer; padding: 14px; min-height: 48px; user-select: none; -webkit-user-select: none; }
.fold > summary::-webkit-details-marker { display: none; }
.fold > summary h2 { display: flex; align-items: center; gap: 6px; }
.fold-end { display: flex; align-items: center; gap: 2px; margin: -6px -4px -6px 0; }
.fold-end .link { padding: 6px 8px; }
.caret-d { color: var(--muted); transition: transform .15s ease; }
.fold:not([open]) .caret-d { transform: rotate(-90deg); }
.fold-body { padding: 0 14px 14px; }
.count { font-size: 11px; font-weight: 600; min-width: 20px; padding: 1px 6px; border-radius: 999px; background: var(--surface-2); color: var(--muted); text-align: center; }

/* Allergy carousel */
.allergies { display: flex; gap: 6px; overflow-x: auto; scroll-snap-type: x proximity; scrollbar-width: none; margin: 0 -14px; padding: 5px 14px 3px; }
.allergies::-webkit-scrollbar { display: none; }
.allergy { flex: none; width: 76px; display: flex; flex-direction: column; align-items: center; gap: 6px; background: none; border: 0; padding: 2px 0; cursor: pointer; scroll-snap-align: start; }
.a-icon { position: relative; width: 58px; height: 58px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; box-shadow: inset 0 0 0 1px var(--border); }
.a-icon svg { width: 40px; height: 40px; }
.allergy.severe .a-icon, .a-icon.severe { background: var(--danger-soft); box-shadow: inset 0 0 0 2px var(--danger); }
.a-flag { position: absolute; top: -2px; right: -2px; width: 20px; height: 20px; border-radius: 50%; background: var(--danger); color: #fff; font-size: 13px; font-weight: 800; display: grid; place-items: center; box-shadow: 0 0 0 2px var(--surface); }
.a-name { font-size: 12px; font-weight: 600; line-height: 1.25; text-align: center; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.allergy[aria-expanded="true"] .a-icon { outline: 2.5px solid var(--accent); outline-offset: 3px; }
.allergy.severe[aria-expanded="true"] .a-icon { outline-color: var(--danger); }
.allergy[aria-expanded="true"] .a-name { color: var(--accent); }
.allergy.severe[aria-expanded="true"] .a-name { color: var(--danger); }
.allergy-more { display: grid; grid-template-rows: 0fr; transition: grid-template-rows .22s ease; }
.allergy-more[data-open] { grid-template-rows: 1fr; }
.allergy-more-in { overflow: hidden; min-height: 0; }
.allergy-note { margin-top: 10px; padding: 10px 12px; border-radius: 12px; background: var(--surface-2); display: grid; gap: 3px; }
.allergy-note.severe { background: var(--danger-soft); }
.allergy-note p { margin: 0; }
.an-head { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; }
.an-head .orig-name { flex-basis: 100%; }

/* Care team roles */
.roles { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-bottom: 3px; }
.role { display: inline-block; font-size: 12px; font-weight: 700; letter-spacing: .01em; padding: 2px 9px; border-radius: 6px; background: var(--surface-2); color: var(--text); }
.role.primary { background: var(--accent); color: var(--on-accent); }
.role.info { background: var(--info-soft); color: var(--info); }
.role.violet { background: var(--violet-soft); color: var(--violet); }
.role.warn { background: var(--warn-soft); color: var(--warn); }
.role.rose { background: var(--rose-soft); color: var(--rose); }
.role.pharmacy { background: var(--ok-soft); color: var(--ok); margin-bottom: 3px; align-self: flex-start; }
.role-sub { font-size: 12px; font-weight: 600; color: var(--muted); }

/* Lab attention icons */
.si { flex: none; width: 22px; height: 22px; }
.si > circle { fill: var(--surface-2); }
.si g { stroke: var(--muted); }
.si.ok > circle { fill: var(--ok-soft); }
.si.ok g { stroke: var(--ok); }
.si.mild > circle { fill: var(--warn-soft); }
.si.mild g { stroke: var(--warn); }
.si.severe > circle { fill: var(--danger); }
.si.severe g { stroke: #fff; }

/* Lab test picker */
.picker { display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px 14px; cursor: pointer; box-shadow: var(--shadow); }
.picker .si { width: 28px; height: 28px; }
.picker-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.picker-label { font-size: 11px; color: var(--muted); font-weight: 600; }
.picker-name { font-size: 17px; font-weight: 650; overflow-wrap: anywhere; }
.picker-end { display: flex; align-items: center; gap: 4px; color: var(--muted); flex: none; }
.attention { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; background: var(--warn-soft); color: var(--warn); border: 0; border-radius: 12px; padding: 10px 12px; font-weight: 600; cursor: pointer; }
.attention .chev { margin-left: auto; color: inherit; }
.lab-list { display: flex; flex-direction: column; padding-bottom: 12px; }
.group-h { font-size: 12px; font-weight: 700; color: var(--muted); padding: 14px 0 4px; }
.lab-list .group-h + .pick { border-top: 0; }
.lab-pick { min-height: 58px; }
.lab-pick[aria-current="true"] { background: var(--accent-soft); margin: 0 -16px; padding-left: 16px; padding-right: 16px; width: calc(100% + 32px); }
.lab-pick .end { font-size: 13px; }
.v-name { display: flex; align-items: flex-start; gap: 8px; min-width: 0; }
.v-name .si { width: 18px; height: 18px; margin-top: 1px; }
.v-num { white-space: nowrap; }

/* Search */
.search { display: flex; align-items: center; gap: 8px; background: var(--surface-2); border-radius: 10px; padding: 0 12px; color: var(--muted); }
.search input { flex: 1; min-width: 0; border: 0; background: none; font: inherit; font-size: 16px; color: var(--text); padding: 10px 0; outline: none; }
.search:focus-within { box-shadow: 0 0 0 2px var(--accent); }
.sheet .search { position: sticky; top: -8px; z-index: 1; box-shadow: 0 0 0 8px var(--surface); }
.sheet .search:focus-within { box-shadow: 0 0 0 2px var(--accent), 0 0 0 8px var(--surface); }
.main > .search { flex: none; background: var(--surface); border: 1px solid var(--border); }
.card .search { margin-bottom: 2px; }
.uses { color: var(--text); }

/* Language sheet switch */
.switch { flex: none; width: 46px; height: 28px; border-radius: 999px; background: var(--border); position: relative; transition: background .15s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .25); transition: transform .15s; }
[aria-checked="true"] > .switch { background: var(--accent); }
[aria-checked="true"] > .switch::after { transform: translateX(18px); }

@media (prefers-reduced-motion: reduce) { .spinner, .skeleton { animation: none; } .caret-d, .switch, .switch::after, .allergy-more { transition: none; } }
