/* Tokens. Every colour and time in the app comes from here. */
:root {
  --bg: #f0f6f8; --surface: #fff; --raised: #f6fafb; --ink: #12343f; --muted: #4b636c; --line: #d3e2e7; --field: #78929b;
  --primary: #0e7490; --primary-strong: #155e75; --on-primary: #fff; --tint: #e0f2f7;
  --ok: #047857; --ok-bg: #dcf5ea; --warn: #8a4b04; --warn-bg: #fdf0d8; --bad: #b42318; --bad-bg: #fde8e6; --off: #4b5563; --off-bg: #e8ecef;
  --ring: #b45309; --shadow: 0 1px 2px rgb(8 50 63 / .06), 0 8px 24px rgb(8 50 63 / .06);
  --fast: 140ms; --base: 240ms; --slow: 700ms; --out: cubic-bezier(.2, .8, .2, 1); --spring: cubic-bezier(.34, 1.56, .64, 1);
  --side: 15rem; color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme=light]) {
    --bg: #0a181e; --surface: #11252d; --raised: #17303a; --ink: #e6f2f5; --muted: #a6bfc7; --line: #274552; --field: #6f8c97;
    --primary: #38bdd8; --primary-strong: #67d3e8; --on-primary: #04222b; --tint: #123744;
    --ok: #5fdcab; --ok-bg: #0f3a2d; --warn: #f7c66b; --warn-bg: #3d2c0c; --bad: #ff9d94; --bad-bg: #451c19; --off: #c0ccd1; --off-bg: #263840;
    --ring: #fbbf24; --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 8px 24px rgb(0 0 0 / .25); color-scheme: dark;
  }
}
:root[data-theme=dark] {
  --bg: #0a181e; --surface: #11252d; --raised: #17303a; --ink: #e6f2f5; --muted: #a6bfc7; --line: #274552; --field: #6f8c97;
  --primary: #38bdd8; --primary-strong: #67d3e8; --on-primary: #04222b; --tint: #123744;
  --ok: #5fdcab; --ok-bg: #0f3a2d; --warn: #f7c66b; --warn-bg: #3d2c0c; --bad: #ff9d94; --bad-bg: #451c19; --off: #c0ccd1; --off-bg: #263840;
  --ring: #fbbf24; --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 8px 24px rgb(0 0 0 / .25); color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-height: 100dvh; font: 16px/1.5 system-ui, 'Segoe UI', sans-serif; color: var(--ink); background: var(--bg); }
h1 { margin: 0; font-size: 1.6rem; line-height: 1.2; letter-spacing: -.01em; text-wrap: balance; }
h2 { margin: 0 0 .75rem; font-size: 1.1rem; }
h3 { margin: 0 0 .5rem; font-size: 1rem; }
p { margin: 0 0 .75rem; }
ul { list-style: none; margin: 0; padding: 0; }
svg { width: 1.25em; height: 1.25em; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.sprite { position: absolute; width: 0; height: 0; }
.muted, li small { color: var(--muted); }
.spacer { flex: 1; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; }

/* Shell: sidebar on wide screens, bottom bar on phones */
body:not(.signed-out) { display: grid; grid-template-columns: var(--side) minmax(0, 1fr); }
.signed-out #side, .signed-out #account, .signed-out #place { display: none; }
#side { position: sticky; top: 0; height: 100dvh; padding: 1.25rem .9rem; background: var(--surface); border-right: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: .6rem; padding: 0 .4rem 1.5rem; font-size: 1.1rem; }
.brand svg { width: 2rem; height: 2rem; padding: .35rem; border-radius: .6rem; background: var(--primary); color: var(--on-primary); stroke-width: 4; }
.brand.small { display: none; padding: 0; }
.signed-out .brand.small { display: flex; }
#nav { display: grid; gap: .25rem; }
#nav button { display: flex; align-items: center; justify-content: flex-start; gap: .75rem; min-height: 46px; padding: .6rem .8rem; background: none; color: var(--muted); border-radius: .7rem; box-shadow: none; }
#nav button:hover { background: var(--raised); color: var(--ink); }
#nav button[aria-current=page] { background: var(--tint); color: var(--primary-strong); }
#nav button[aria-current=page] svg { animation: pop var(--base) var(--spring); }
header { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: .75rem; min-height: 64px; padding: .5rem 1.5rem; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); }
header::after { content: ''; position: absolute; inset: auto 0 0; height: 2px; background: linear-gradient(90deg, transparent, var(--primary), transparent); background-size: 40% 100%; background-repeat: no-repeat; opacity: 0; }
body[data-busy] header::after { opacity: 1; animation: busy 1s linear infinite; }
#account { display: flex; align-items: center; gap: .5rem; min-width: 0; }
#who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
main { max-width: 68rem; margin: 0 auto; padding: 1rem 1.5rem 3rem; }
.view { animation: rise var(--base) var(--out); }
.title { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; }
.title p { margin: .25rem 0 0; }

/* Sign in */
#gate { display: grid; grid-template-columns: 1.1fr 1fr; align-items: center; gap: 2.5rem; min-height: calc(100dvh - 10rem); }
body:not(.signed-out) #gate { display: none; }
#gate form { width: 100%; max-width: 25rem; justify-self: center; padding: 2rem; animation: rise var(--base) var(--out); }
#gate h1 { margin-bottom: 1.25rem; }
#gate form button { width: 100%; }
.welcome { display: grid; align-content: end; min-height: 24rem; padding: 2rem; border-radius: 1.5rem; color: #fff; background: linear-gradient(150deg, #0e7490, #0b4f63 60%, #065f46); overflow: hidden; }
.welcome p { margin: 1rem 0 0; font-size: 2rem; font-weight: 700; line-height: 1.15; }
.pulse { width: 100%; height: 8rem; stroke: #a5f3fc; stroke-width: 3; }
.pulse path { vector-effect: non-scaling-stroke; stroke-dasharray: 100; animation: draw 2.6s var(--out) both; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 1rem; padding: 1.25rem; box-shadow: var(--shadow); }
.card + .card { margin-top: 1rem; }
.grid-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.grid-two { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(22rem, 100%), 1fr)); gap: 1rem; margin-bottom: 1rem; align-items: start; }
.grid-cards .card, .grid-two .card { margin: 0; }
.kpi { display: flex; align-items: center; gap: 1rem; animation: rise var(--base) var(--out) both; transition: transform var(--fast) var(--out); }
.kpi:nth-child(2) { animation-delay: 50ms; } .kpi:nth-child(3) { animation-delay: 100ms; } .kpi:nth-child(4) { animation-delay: 150ms; }
.kpi > svg { width: 2.75rem; height: 2.75rem; padding: .7rem; border-radius: .8rem; background: var(--tint); color: var(--primary-strong); }
.kpi strong { display: block; font-size: 1.9rem; line-height: 1.1; font-variant-numeric: tabular-nums; }
.kpi.ok > svg { background: var(--ok-bg); color: var(--ok); }
.kpi.warn > svg { background: var(--warn-bg); color: var(--warn); }
.bars { display: grid; grid-template-columns: repeat(7, 1fr); gap: .5rem; height: 11rem; }
.bars li { display: grid; grid-template-rows: auto 1fr auto; justify-items: center; gap: .3rem; font-size: .85rem; border: 0; }
.bars i { align-self: end; width: 100%; max-width: 2.4rem; height: calc(var(--v) * 100%); min-height: 3px; border-radius: .4rem .4rem 0 0; background: var(--primary); transform-origin: bottom; animation: grow var(--slow) var(--out) both; }
.bars li:first-child i { background: var(--ok); }

/* Forms and buttons */
label { display: block; margin-bottom: .3rem; font-weight: 600; }
input:not([type=checkbox]), select, textarea { width: 100%; min-height: 46px; padding: .6rem .8rem; font: inherit; color: inherit; background: var(--surface); border: 1px solid var(--field); border-radius: .7rem; transition: border-color var(--fast), box-shadow var(--fast); }
input:not([type=checkbox]):focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 4px var(--tint); }
#gate input, .card > input, #picker select { margin-bottom: 1rem; }
::placeholder { color: var(--muted); opacity: 1; }
textarea { min-height: 5rem; resize: vertical; }
button { display: inline-flex; align-items: center; justify-content: center; gap: .5rem; min-height: 46px; padding: .6rem 1.1rem; font: inherit; font-weight: 600; border: 0; border-radius: .7rem; background: var(--primary); color: var(--on-primary); cursor: pointer; transition: background var(--fast), color var(--fast), transform var(--fast) var(--out); touch-action: manipulation; }
button:hover { background: var(--primary-strong); }
button:active:not(:disabled) { transform: scale(.97); }
button:disabled { opacity: .55; cursor: default; }
button.secondary { background: var(--surface); color: var(--primary-strong); box-shadow: inset 0 0 0 1px var(--primary); }
button.secondary:hover { background: var(--tint); }
button.danger { background: var(--surface); color: var(--bad); box-shadow: inset 0 0 0 1px var(--bad); }
button.danger:hover, button.danger.armed { background: var(--bad-bg); }
button.link { padding: .4rem 0; background: none; color: var(--primary-strong); }
button.icon { width: 46px; padding: 0; background: none; color: var(--ink); }
button.icon:hover { background: var(--tint); }
.inline { display: flex; gap: .6rem; }
.inline input { flex: 1; min-width: 0; }
form.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr)); gap: 1rem; padding: .5rem 0 1rem; }
form.grid label { margin: 0; }
form.grid label > * { margin-top: .3rem; font-weight: 400; }
form.grid > :is(.wide, .allergy, button), form.grid label:has(textarea) { grid-column: 1 / -1; justify-self: stretch; }
form.grid > button { justify-self: start !important; }
.medicine { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: .8rem; margin: 0 0 .8rem; padding: 1rem; border: 1px solid var(--line); border-radius: .8rem; animation: rise var(--base) var(--out); }
.medicine > ul { grid-column: 1 / -1; }
.medicine > button { align-self: end; }

/* Switch */
input[type=checkbox].switch { appearance: none; position: relative; flex: none; width: 52px; height: 30px; margin: 0; border-radius: 15px; background: var(--field); cursor: pointer; transition: background var(--base); }
input[type=checkbox].switch::before { content: ''; position: absolute; top: 4px; left: 4px; width: 22px; height: 22px; border-radius: 11px; background: #fff; box-shadow: 0 1px 3px rgb(0 0 0 / .35); transition: transform var(--base) var(--spring); }
input[type=checkbox].switch:checked { background: var(--ok); }
:root[data-theme=dark] input[type=checkbox].switch:checked { background: #0f9f6e; }
input[type=checkbox].switch:checked::before { transform: translateX(22px); }
input[type=checkbox].switch:disabled { opacity: .5; cursor: default; }

/* Lists */
li { border-top: 1px solid var(--line); }
li:first-child { border-top: 0; }
li strong, li small { display: block; }
li label { display: flex; align-items: center; gap: 1rem; margin: 0; padding: .9rem 0; font-weight: 400; cursor: pointer; }
li form.grid label:not(.check) { display: block; padding: 0; font-weight: 600; }
li label > span { flex: 1; min-width: 0; }
button.row { display: flex; justify-content: flex-start; gap: .9rem; flex: 1; min-width: 0; width: 100%; padding: .75rem .6rem; text-align: left; background: none; color: inherit; font-weight: 400; }
button.row:hover { background: var(--raised); }
.row > span:last-child { min-width: 0; overflow-wrap: anywhere; }
.avatar { display: grid; place-items: center; flex: none; width: 2.6rem; height: 2.6rem; border-radius: 50%; background: var(--tint); color: var(--primary-strong); font-weight: 700; }
#results li, #slots li, #list li { animation: rise var(--base) var(--out) both; }
:is(#results, #slots, #list) li:nth-child(2) { animation-delay: 30ms; } :is(#results, #slots, #list) li:nth-child(3) { animation-delay: 60ms; }
:is(#results, #slots, #list) li:nth-child(4) { animation-delay: 90ms; } :is(#results, #slots, #list) li:nth-child(n+5) { animation-delay: 120ms; }
.slot, .item { display: flex; flex-wrap: wrap; align-items: center; gap: .8rem; padding: .6rem 0; }
.slot { flex-wrap: nowrap; }
.item > div { flex: 1; min-width: 10rem; overflow-wrap: anywhere; }
.slot select { width: auto; }
.chip { display: inline-flex; align-items: center; gap: .35rem; padding: .2rem .7rem; border-radius: 99px; font-size: .85rem; font-weight: 600; white-space: nowrap; background: var(--tint); color: var(--primary-strong); }
.chip.ok { background: var(--ok-bg); color: var(--ok); } .chip.warn { background: var(--warn-bg); color: var(--warn); }
.chip.bad { background: var(--bad-bg); color: var(--bad); } .chip.off { background: var(--off-bg); color: var(--off); }
.allergy { display: flex; align-items: center; gap: .6rem; padding: .7rem .9rem; margin: 0 0 .75rem; border-radius: .7rem; background: var(--bad-bg); color: var(--bad); font-weight: 700; }
details { border-top: 1px solid var(--line); }
summary { padding: .9rem 0; font-weight: 600; color: var(--primary-strong); cursor: pointer; }
details[open] > :not(summary) { animation: rise var(--base) var(--out); }
article { padding: 1rem 1.1rem; margin-bottom: .8rem; border: 1px solid var(--line); border-left: 4px solid var(--primary); border-radius: .8rem; background: var(--surface); overflow-wrap: anywhere; }
article p { margin: .25rem 0; white-space: pre-line; }
article ol { margin: .5rem 0; padding-left: 1.4rem; }
article li { border: 0; }
code.secret { display: block; padding: .8rem; margin: .5rem 0; border-radius: .6rem; background: var(--raised); border: 1px dashed var(--field); overflow-wrap: anywhere; user-select: all; }

#message { margin: 0; color: var(--bad); font-weight: 600; text-align: center; }
#message:not(:empty) { padding: .7rem 1rem; margin-bottom: 1rem; border-radius: .7rem; background: var(--bad-bg); animation: shake 300ms; }
#toast { position: fixed; z-index: 10; right: 1.5rem; bottom: 1.5rem; display: flex; align-items: center; gap: .6rem; padding: .8rem 1.1rem; border-radius: .8rem; background: var(--ink); color: var(--bg); font-weight: 600; box-shadow: var(--shadow); opacity: 0; transform: translateY(1rem); pointer-events: none; transition: opacity var(--base), transform var(--base) var(--out); }
#toast.on { opacity: 1; transform: none; }

@keyframes rise { from { opacity: 0; transform: translateY(10px); } }
@keyframes pop { from { transform: scale(.7); } }
@keyframes grow { from { transform: scaleY(0); } }
@keyframes draw { from { stroke-dashoffset: 100; } to { stroke-dashoffset: 0; } }
@keyframes busy { from { background-position: -60% 0; } to { background-position: 160% 0; } }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

@media (max-width: 820px) {
  body:not(.signed-out) { display: block; }
  #side { position: fixed; z-index: 6; top: auto; inset: auto 0 0; height: auto; padding: .3rem .4rem calc(.3rem + env(safe-area-inset-bottom)); border: 0; border-top: 1px solid var(--line); }
  #side .brand { display: none; }
  .brand.small { display: flex; }
  #nav { display: flex; justify-content: space-around; }
  #nav button { flex: 1; flex-direction: column; justify-content: center; gap: .15rem; min-height: 54px; padding: .3rem; font-size: .78rem; }
  header { padding: .5rem 1rem; }
  #place { display: none; }
  main { padding: .75rem 1rem 6.5rem; }
  #toast { left: 1rem; right: 1rem; bottom: calc(5.5rem + env(safe-area-inset-bottom)); }
  .grid-cards { grid-template-columns: 1fr 1fr; gap: .7rem; }
  .kpi { padding: .9rem; gap: .7rem; line-height: 1.25; }
  .kpi > svg { width: 2.2rem; height: 2.2rem; padding: .5rem; }
  .kpi strong { font-size: 1.5rem; }
  #gate { grid-template-columns: 1fr; gap: 1.25rem; min-height: 0; }
  .welcome { min-height: 0; padding: 1.25rem; }
  .welcome p { margin: 0; font-size: 1.3rem; } .pulse { height: 4rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-delay: 0ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; }
}
#rx { display: none; }
@media print {
  body { display: block !important; background: #fff; color: #000; }
  #side, .page, #toast { display: none !important; }
  #rx { display: block; }
  .sign { margin-top: 5rem; text-align: right; }
}

#register.card { margin-bottom: 1rem; }
.visits { margin-top: 1.5rem; }
.systems { margin-top: 1rem; }

.notice { padding: .7rem .9rem; border-radius: .7rem; background: var(--warn-bg); color: var(--warn); }
a { color: var(--primary-strong); }
#who { display: block; min-height: 46px; padding: .4rem; color: var(--ink); }

/* ABHA */
.abha > * + * { margin-top: .9rem; }
.abha h2 { margin-top: 1.4rem; }
.abha h1:focus { outline: none; }
.abha .tick { display: flex; gap: .8rem; align-items: flex-start; font-weight: 400; margin: .8rem 0 0; cursor: pointer; }
.abha .tick input { flex: none; width: 1.3rem; height: 1.3rem; margin-top: .15rem; }
.abha input[type=radio] { flex: none; width: 1.3rem; height: 1.3rem; min-height: 0; }
.abha .terms { max-height: 14rem; overflow: auto; padding: 1rem; border: 1px solid var(--line); border-radius: .7rem; background: var(--raised); }
.abha .choices { display: flex; flex-wrap: wrap; gap: .6rem; }
.abha .sent { font-weight: 600; }
.abha p.bad { color: var(--bad); font-weight: 600; }
.abha .rules .met { color: var(--ok); } .abha .rules .unmet { color: var(--bad); }
.abha .rules .met::before { content: '✓ '; } .abha .rules .unmet::before { content: '✗ '; }
.abha .who { display: flex; gap: 1rem; align-items: flex-start; overflow-wrap: anywhere; }
.abha .who p { margin: .2rem 0; }
.abha .photo { width: 5.5rem; border-radius: .7rem; }
.abha .abha-card { display: block; max-width: 100%; margin-bottom: .6rem; border: 1px solid var(--line); border-radius: .7rem; }
.abha .scroll { overflow-x: auto; }
.abha table { width: 100%; border-collapse: collapse; }
.abha th, .abha td { padding: .5rem .7rem; text-align: left; border-top: 1px solid var(--line); vertical-align: top; }
input[readonly] { background: var(--raised) !important; }

/* Care screens */
label.check { display: flex; align-items: center; gap: .6rem; font-weight: 400; min-height: 44px; margin: 0; }
label.check input { width: 22px; height: 22px; margin: 0 !important; accent-color: var(--primary); flex: none; }
.actions { display: flex; flex-wrap: wrap; gap: .6rem; margin: .5rem 0; }
.filebutton { display: inline-flex; align-items: center; min-height: 46px; padding: .6rem 1.1rem; margin: 0; border-radius: .7rem; color: var(--primary-strong); box-shadow: inset 0 0 0 1px var(--primary); cursor: pointer; position: relative; }
.filebutton:hover { background: var(--tint); }
.filebutton:focus-within { outline: 3px solid var(--ring); outline-offset: 2px; }
.pen { width: 100%; height: auto; border: 1px solid var(--field); border-radius: .7rem; touch-action: none; background: #fff; cursor: crosshair; }
.photo { max-width: 100%; border-radius: .7rem; border: 1px solid var(--line); margin-top: .6rem; }
.item > .wide { flex-basis: 100%; }
.item > .wide:empty { display: none; }
table { width: 100%; border-collapse: collapse; margin: .4rem 0; }
th, td { text-align: left; padding: .4rem .5rem; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
.big { font-size: 1.9rem; font-weight: 700; margin: 0; }
.beds { display: grid; grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr)); gap: .7rem; }
.bed { flex-direction: column; align-items: flex-start; gap: 0; min-height: 4.5rem; text-align: left; background: var(--ok-bg); color: var(--ok); }
.bed:disabled { opacity: 1; }
.bed.taken { background: var(--warn-bg); color: var(--warn); }
.bed.taken:hover { background: var(--tint); }
.badge { margin-left: auto; min-width: 1.5rem; padding: 0 .4rem; border-radius: 1rem; background: var(--bad); color: var(--surface); font-size: .8rem; text-align: center; }
#banner:not(:empty) { display: flex; flex-wrap: wrap; align-items: center; gap: .8rem; padding: .8rem 1rem; margin-bottom: 1rem; border-radius: .8rem; background: var(--bad-bg); color: var(--bad); animation: rise var(--base) var(--out); }
#nav { overflow-y: auto; max-height: calc(100dvh - 6rem); }
@media (max-width: 820px) {
  #nav { overflow-x: auto; overflow-y: hidden; max-height: none; justify-content: flex-start; }
  #nav button { flex: 1 0 4.8rem; }
  .badge { margin: 0; }
}

.qr { display: block; width: min(13rem, 100%); height: auto; margin: 0 auto .8rem; border-radius: .6rem; }
.scroll { overflow-x: auto; }
.taper { flex-wrap: wrap; align-items: end; }
.taper label { flex: 1 1 8rem; }

.plainpage { display: block !important; }
.narrow { max-width: 32rem; padding-top: 1.5rem; }
#book > button, #open > button { width: 100%; }
.narrow input:not([type=checkbox]), .narrow select { margin-bottom: 1rem; }
.growth { width: 100%; height: auto; stroke: none; overflow: visible; }
.growth text { font: 11px system-ui, sans-serif; fill: var(--muted); }
.growth .curve { fill: none; stroke: var(--field); stroke-width: 1.5; }
.growth .mid { stroke: var(--ok); }
.growth .edge { stroke: var(--bad); stroke-dasharray: 4 3; }
.growth .mine { fill: none; stroke: var(--primary); stroke-width: 2; }
.growth circle { fill: var(--primary); }
.meter { display: grid; grid-template-columns: minmax(6rem, 12rem) 1fr auto; gap: .6rem; align-items: center; padding: .35rem 0; overflow-wrap: anywhere; }
.meter i { height: .8rem; border-radius: .4rem; background: var(--primary); width: calc(var(--v) * 100%); min-width: 3px; transform-origin: left; animation: widen var(--slow) var(--out) both; }
@keyframes widen { from { transform: scaleX(0); } }
.legal { text-align: center; font-size: .9rem; margin-top: 1.5rem; }
body:not(.signed-out) .legal { display: none; }
