:root {
  color-scheme: light;
  --page: #faf6f5;
  --surface: #fffcfb;
  --band: #f6e9e8;
  --ink: #1a1112;
  --ink-2: #5a4e4f;
  --muted: #75696a;
  --grid: #ece2e0;
  --axis: #d4c6c4;
  --border: rgba(60, 20, 25, 0.13);
  /* A deep wine red: blood, not alarm. */
  --accent: #9e2b3a;
  --accent-ink: #ffffff;
  --sys: #2a78d6;
  --dia: #eb6834;
  --pulse: #1baf7a;
  --good-bg: #e3f3e3;
  --good-ink: #006300;
  --critical: #d03b3b;
  --critical-bg: #fbe7e7;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #120e0f;
    --surface: #1d1819;
    --band: #2a1c1e;
    --ink: #f3eded;
    --ink-2: #c9bdbe;
    --muted: #938789;
    --grid: #2f2728;
    --axis: #3d3335;
    --border: rgba(255, 230, 232, 0.12);
    --accent: #e38a93;
    --accent-ink: #1d1819;
    --sys: #3987e5;
    --dia: #d95926;
    --pulse: #199e70;
    --good-bg: #13301a;
    --good-ink: #0ca30c;
    --critical: #e66767;
    --critical-bg: #3a1a1a;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  line-height: 1.45;
}
a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0.5rem 0 1rem; }
h2 { font-size: 1.15rem; margin: 1.75rem 0 0.5rem; }
h3 { font-size: 1rem; margin: 1.25rem 0 0.25rem; color: var(--ink-2); }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

/* Header and footer */
header.top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.25rem 1rem;
  padding: 0.5rem 16px;
  background: var(--band);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 0.4rem; font-weight: 600; color: var(--accent); }
header nav { display: flex; gap: 0.25rem; flex: 1; }
header nav a {
  padding: 0.4rem 0.7rem; border-radius: 6px;
  color: var(--ink-2); text-decoration: none;
}
header nav a[aria-current] { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: inset 0 -2px var(--accent); }
.logout { margin: 0 0 0 auto; display: flex; align-items: baseline; gap: 0.5rem; color: var(--muted); font-size: 0.9rem; }
button.link {
  background: none; border: none; padding: 0.4rem 0; font: inherit;
  color: var(--muted); cursor: pointer; text-decoration: underline;
}
main { max-width: 960px; margin: 0 auto; padding: 1rem 16px 2rem; }
footer { text-align: center; color: var(--muted); font-size: 0.8rem; padding: 1rem 16px 2rem; }

/* Messages */
.flash, .error { padding: 0.6rem 0.9rem; border-radius: 8px; margin: 0 0 1rem; }
.flash { background: var(--good-bg); color: var(--good-ink); }
.flash::before { content: "✓ "; }
.error { background: var(--critical-bg); color: var(--ink); border-left: 4px solid var(--critical); }
.error::before { content: "⚠ "; color: var(--critical); }

/* Forms */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
}
.stack { display: flex; flex-direction: column; gap: 1rem; }
.stack > p { margin: 0; }
.narrow { max-width: 28rem; }
.add { max-width: 28rem; }
label { display: flex; flex-direction: column; gap: 0.3rem; font-weight: 600; }
.unit { font-weight: normal; color: var(--muted); font-size: 0.9em; }
input, textarea, button {
  font: inherit; color: inherit;
}
input, textarea {
  background: var(--page);
  border: 1px solid var(--axis);
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
  width: 100%;
  font-weight: normal;
}
input:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
fieldset { border: none; margin: 0; padding: 0; }
legend { font-weight: 600; padding: 0; margin-bottom: 0.3rem; }
.pair { display: flex; align-items: center; gap: 0.5rem; }
.pair input, input.short { max-width: 6.5rem; font-size: 1.4rem; text-align: center; }
.pair span { font-size: 1.6rem; color: var(--muted); }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0.3rem 0 0; }
button {
  border: 1px solid var(--axis);
  background: var(--page);
  border-radius: 8px;
  padding: 0.6rem 1rem;
  cursor: pointer;
  align-self: flex-start;
}
button.primary {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
  font-weight: 600; font-size: 1.1rem; padding: 0.7rem 2rem;
}
.recovery .code {
  font-family: ui-monospace, monospace; font-size: 1.3rem; letter-spacing: 0.05em;
  padding: 0.5rem 0.75rem; background: var(--page); border-radius: 8px;
  overflow-wrap: anywhere;
}

/* Tables */
.choices { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.25rem 0.5rem; margin: 0.25rem 0 0.75rem; }
.choices a { padding: 0.15rem 0.55rem; border-radius: 999px; text-decoration: none; border: 1px solid var(--border); }
.choices a[aria-current] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.muted { color: var(--muted); }
/* column-reverse makes the box start scrolled to the bottom: the newest reading. */
.table-scroll {
  display: flex; flex-direction: column-reverse;
  max-height: min(60vh, 32rem); overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
table.readings { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.readings th, .readings td { padding: 0.4rem 0.6rem; text-align: left; border-bottom: 1px solid var(--grid); vertical-align: top; }
.readings th { position: sticky; top: 0; background: var(--surface); color: var(--ink-2); font-weight: 600; font-size: 0.85rem; }
.readings .num { text-align: right; white-space: nowrap; }
.readings .date { white-space: nowrap; }
.readings .bp { font-weight: 600; }
.readings .note { color: var(--ink-2); min-width: 8rem; }
.readings .id { color: var(--muted); font-size: 0.8rem; }
.readings tbody tr:last-child td { border-bottom: none; }
.high { color: var(--critical); font-size: 0.75em; }
section > table.readings { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }

/* Charts (drawn by package chart) */
.chart-box { position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 4px 0; }
svg.chart { display: block; width: 100%; height: auto; overflow: visible; touch-action: pan-y; }
svg.chart text { font-size: 12px; fill: var(--ink-2); }
svg.chart .title { font-weight: 600; fill: var(--ink); }
svg.chart .tick, svg.chart .muted { fill: var(--muted); }
svg.chart .tick { font-variant-numeric: tabular-nums; }
svg.chart .grid { stroke: var(--grid); stroke-width: 1; }
svg.chart .axis { stroke: var(--axis); stroke-width: 1; }
svg.chart .guide { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 4 4; }
svg.chart .key { stroke-width: 3; stroke-linecap: round; }
svg.chart .sys { stroke: var(--sys); fill: var(--sys); }
svg.chart .dia { stroke: var(--dia); fill: var(--dia); }
svg.chart .pulse { stroke: var(--pulse); fill: var(--pulse); }
svg.chart polyline { fill: none; stroke-linejoin: round; stroke-linecap: round; }
svg.chart polyline.thin { stroke-width: 1.5; opacity: 0.6; }
svg.chart polyline.avg { stroke-width: 2.5; }
svg.chart .dots circle { stroke: var(--surface); stroke-width: 1; }
svg.chart .dots.faint { opacity: 0.45; }
svg.chart circle.sys, svg.chart circle.dia, svg.chart circle.pulse { stroke: var(--surface); stroke-width: 1; }
svg.chart circle.newest { stroke: var(--ink); stroke-width: 2; }
svg.chart .join { stroke: var(--axis); stroke-width: 1; }
svg.chart .cross { stroke: var(--ink-2); stroke-width: 1; pointer-events: none; }
svg.chart .mark { fill: none; stroke: var(--ink); stroke-width: 2; pointer-events: none; }
.tip {
  position: absolute; top: 28px; z-index: 1; pointer-events: none;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  padding: 0.35rem 0.6rem; font-size: 0.85rem; white-space: nowrap;
}
.tip strong { display: block; font-size: 1rem; font-variant-numeric: tabular-nums; }
.tip span { color: var(--ink-2); }

@media print {
  header.top nav, .logout, .choices, form { display: none; }
  .table-scroll { max-height: none; overflow: visible; }
  body { background: #fff; }
}
