/* Blood Pressure Tracker — web.
   Layout C: left rail, dense table, report within reach.
   One responsive layout: below 600px it is the phone app. */

:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --raised: #f1f3f6;
  --line: #dfe3e9;
  --fg: #16181d;
  --muted: #6b7280;
  --faint: #9aa1ab;
  --accent: #4A9BE0;
  --on-accent: #ffffff;
  --danger: #b91c1c;
  --radius: 8px;
  --rail: 180px;
  --max-width: 1280px;
}

:root[data-theme="dark"] {
  --bg: #14161a;
  --surface: #1c1f24;
  --raised: #22262c;
  --line: #2c3037;
  --fg: #e8eaed;
  --muted: #9aa1ab;
  --faint: #6b7280;
  --danger: #f87171;
}

* { box-sizing: border-box; }

/* Author display rules override the UA's [hidden] rule, so any element given
   a display value would ignore the attribute. Enforce it once, globally. */
[hidden] { display: none !important; }

html, body { height: 100%; }
body { overflow-x: hidden; }

/* The reading table has more columns than a phone has room for. It scrolls
   inside its own box rather than widening the page. */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ── shell ─────────────────────────────────────────────────────────────── */

.app {
  display: grid;
  grid-template-columns: var(--rail) minmax(0, 1fr);
  min-height: 100vh;
  /* A reading table stretched across an ultrawide monitor is unreadable: the
     eye loses the row between the date and the category. */
  max-width: var(--max-width);
  margin-inline: auto;
}

.rail {
  border-right: 1px solid var(--line);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--surface);
}

.rail__brand { font-size: 13px; font-weight: 600; color: var(--muted); padding: 0 8px 12px; }

.nav {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: var(--radius);
  color: var(--muted); font-size: 14px; font-weight: 500;
  background: none; border: 0; width: 100%; text-align: left; cursor: pointer;
  font-family: inherit;
}
.nav:hover { background: var(--raised); color: var(--fg); }
.nav[aria-current="page"] { color: var(--accent); background: var(--raised); }
.nav svg { width: 18px; height: 18px; flex: none; }

.rail__foot { margin-top: auto; padding-top: 12px; }

.status { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--faint); flex: none; }
.status[data-state="SYNCED"]::before { background: #4A9489; }
.status[data-state="SAVING"]::before { background: var(--accent); }
.status[data-state="OFFLINE"]::before { background: #DEAE6A; }
.status[data-state="ERROR"]::before,
.status[data-state="AUTH_REQUIRED"]::before { background: #CB6860; }

main { padding: 20px 24px 48px; min-width: 0; max-width: 100%; overflow-x: clip; }

/* ── controls ──────────────────────────────────────────────────────────── */

button, select, input, textarea { font: inherit; color: inherit; }

.btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--fg);
  border-radius: var(--radius); padding: 7px 13px; font-size: 14px; font-weight: 500;
  cursor: pointer;
}
.btn:hover { border-color: var(--muted); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn--primary:hover { filter: brightness(1.06); }
.btn--danger { color: var(--danger); border-color: var(--danger); background: none; }
.btn:disabled { opacity: .45; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn--tiny { padding: 4px 9px; font-size: 12px; }

.notice {
  background: var(--raised); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 12px; font-size: 13px; color: var(--muted); margin: 0 0 14px;
}

.field { display: flex; flex-direction: column; gap: 5px; }
.field > label { font-size: 13px; color: var(--muted); }
.input, select.input {
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 10px; font-size: 15px; width: 100%;
}
.input:focus { border-color: var(--accent); }
.input--num { font-size: 22px; text-align: center; padding: 10px 8px; font-variant-numeric: tabular-nums; }
.input[aria-invalid="true"] { border-color: var(--danger); }

.segmented { display: flex; gap: 6px; flex-wrap: wrap; }
.segmented button {
  border: 1px solid var(--line); background: none; color: var(--muted);
  border-radius: var(--radius); padding: 5px 11px; font-size: 13px; cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

/* ── chips ─────────────────────────────────────────────────────────────── */
/* WHO 1999 table fills with white text, matching the Android app exactly. */

.chip {
  display: inline-block; color: #fff; font-size: 12px; font-weight: 500;
  padding: 2px 8px; border-radius: 4px; white-space: nowrap;
}

/* ── history ───────────────────────────────────────────────────────────── */

.toolbar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 18px; }
.toolbar__spacer { flex: 1; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin-bottom: 18px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 11px 13px; }
.tile__label { font-size: 12px; color: var(--muted); }
.tile__value { font-size: 20px; font-weight: 500; font-variant-numeric: tabular-nums; margin-top: 2px; }

table.readings { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.readings th {
  text-align: left; font-size: 12px; font-weight: 500; color: var(--muted);
  padding: 0 10px 7px 0; border-bottom: 1px solid var(--line);
}
table.readings td { padding: 9px 10px 9px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
table.readings tbody tr:hover { background: var(--surface); }
.readings .num { font-weight: 500; }
.readings .dim { color: var(--muted); font-size: 13px; }
.readings .row-actions { text-align: right; white-space: nowrap; }
.icon-btn {
  background: none; border: 0; color: var(--muted); cursor: pointer;
  padding: 4px 6px; border-radius: 6px; font-size: 13px;
}
.icon-btn:hover { background: var(--raised); color: var(--fg); }
.icon-btn--danger:hover { color: var(--danger); }

.print-head { display: none; }

/* ── boot ──────────────────────────────────────────────────────────────── */

#boot { padding: 3rem 1.5rem; max-width: 26rem; margin: 0 auto; text-align: center; }
.boot__icon { border-radius: 20px; display: block; margin: 0 auto 18px; }
#boot h1 { font-size: 19px; font-weight: 500; margin: 0 0 6px; }
#boot-msg { font-size: 14px; color: var(--muted); margin: 0 0 20px; }
.boot__fine { font-size: 12px; color: var(--faint); margin-top: 28px; line-height: 1.6; }

/* ── latest reading, as the WHO-style filled banner the app uses ────────── */

.latest {
  border-radius: 14px; padding: 18px; margin-bottom: 12px; color: #fff;
}
.latest__top {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  font-size: 15px; font-weight: 500;
}
.latest__when { font-size: 13px; font-weight: 400; color: rgba(255,255,255,.7); }
.latest__row { display: flex; align-items: flex-end; margin-top: 10px; }
.latest__bp { font-size: 38px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.latest__unit { font-size: 15px; color: rgba(255,255,255,.7); margin: 0 0 3px 4px; }
.latest__pulse { margin-left: auto; display: flex; align-items: flex-end; }
.latest__pulse b { font-size: 26px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.latest__pulse span { font-size: 13px; color: rgba(255,255,255,.7); margin: 0 0 2px 3px; }

.crisis {
  background: color-mix(in srgb, #CB6860 14%, transparent);
  color: var(--danger); border-radius: 10px; padding: 12px; font-size: 13px;
  margin-bottom: 12px;
}

.tiles--three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.tile--period { text-align: center; }
.tile--period .tile__value {
  display: flex; align-items: center; justify-content: center; gap: 6px; font-size: 17px; font-weight: 700;
}
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }

.section-head { font-size: 15px; font-weight: 600; margin: 18px 0 10px; }
.section-head .dim { font-weight: 400; font-size: 13px; }

/* ── statistics, after the chart ────────────────────────────────────────── */

.stats { background: var(--surface); border: 1px solid var(--line);
         border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.stats h2 { font-size: 15px; font-weight: 600; margin: 0 0 12px; }
.stats__avg { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.stats__avg .label { font-size: 13px; color: var(--muted); }
.stats__avg .value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stats__avg .unit { font-size: 13px; color: var(--muted); font-weight: 400; }
.stats__grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
  gap: 12px; border-top: 1px solid var(--line); margin-top: 14px; padding-top: 14px;
  text-align: center;
}
.stats__grid .label { font-size: 12px; color: var(--muted); }
.stats__grid .value { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }

.legend { display: flex; justify-content: center; gap: 16px; margin-top: 10px; font-size: 13px; color: var(--muted); }
.legend span { display: flex; align-items: center; gap: 6px; }
.pulse-toggle { display: flex; justify-content: flex-end; margin-top: 8px; }

.chart-card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 16px 16px; margin-bottom: 14px;
}
.chart-card h2 { font-size: 13px; font-weight: 500; color: var(--muted); margin: 0 0 12px; }
.chart-box { position: relative; height: 320px; }

/* Statistics and distribution pair up once there is room for both; below
   that they stack, as they do on the phone. */
.chart-pair { display: grid; gap: 14px; align-items: start; }
@media (min-width: 900px) {
  .chart-pair { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  /* Matching heights so the pair reads as one row rather than two cards that
     happen to be adjacent. */
  .chart-pair > * { height: 100%; margin-bottom: 0; }
  .chart-pair .stats, .chart-pair .chart-card { display: flex; flex-direction: column; }
  .chart-pair .chart-box--short { flex: 1; height: auto; min-height: 200px; }
}
.chart-box--short { height: 220px; }
@media (max-width: 599px) { .chart-box { height: 240px; } }

.topbar {
  display: flex; align-items: center; gap: 12px;
  margin: -4px 0 16px; min-height: 34px;
}
.topbar__profile { flex: none; max-width: 15rem; }
.topbar__right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.topbar__view { font-size: 14px; color: var(--muted); }

/* Slimmer than a form field: this is a header control, not an input. */
/* No fixed height: a select sizes itself from font and padding, and forcing
   30px clipped the descenders in "My profile". */
.input--slim { padding: 5px 8px; font-size: 14px; line-height: 1.35; }

/* ── floating add button ────────────────────────────────────────────────── */

.fab {
  position: fixed; right: 24px; bottom: 24px; z-index: 4;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: var(--on-accent); border: 0; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 10px rgba(0,0,0,.28);
}
.fab svg { width: 26px; height: 26px; }
.fab:hover { filter: brightness(1.07); }
.fab:active { transform: scale(.96); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.empty { text-align: center; padding: 56px 20px; color: var(--muted); }
.empty h2 { font-size: 17px; font-weight: 500; color: var(--fg); margin: 0 0 6px; }

/* ── settings ──────────────────────────────────────────────────────────── */

.section-label {
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .07em;
  color: var(--muted); margin: 22px 0 10px;
}
.section-label:first-child { margin-top: 0; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 14px; max-width: 620px; }
.panel h2 { font-size: 15px; font-weight: 500; margin: 0 0 4px; }
.panel p { font-size: 13px; color: var(--muted); margin: 0 0 14px; }
.panel__row {
  display: flex; justify-content: space-between; align-items: center;
  gap: 16px; padding: 11px 0; border-top: 1px solid var(--line); flex-wrap: wrap;
}
.panel__row > span:first-child { flex: 1 1 16rem; min-width: 0; }
.panel__row label { display: block; }
.panel__row .dim { display: block; font-size: 12px; color: var(--muted); line-height: 1.5; margin-top: 2px; }
.panel__row select.input { flex: 0 0 auto; width: auto; min-width: 11rem; max-width: 100%; }
.panel__row:first-of-type { border-top: 0; }

.reference {
  border: 1px solid var(--line); border-radius: 12px; padding: 14px;
  margin: 4px 0 6px; display: flex; flex-direction: column; gap: 8px;
}
.reference__title { font-size: 13px; font-weight: 500; color: var(--muted); margin: 0; }
.reference__row { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.reference__range { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

ul.plain { list-style: none; margin: 0 0 4px; padding: 0; }
ul.plain li { position: relative; padding-left: 14px; margin-bottom: 4px; font-size: 13px; line-height: 1.5; }
ul.plain li::before { content: "•"; position: absolute; left: 2px; }

.preview {
  list-style: none; margin: 4px 0 10px; padding: 10px 12px;
  background: var(--raised); border-radius: var(--radius); font-size: 13px;
}
.preview li { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; }
.preview .num { font-variant-numeric: tabular-nums; font-weight: 500; }
.preview--skipped { color: var(--danger); }
.preview--skipped li { display: block; }

.format { margin-top: 6px; border-top: 1px solid var(--line); padding-top: 10px; }
.format summary { font-size: 13px; color: var(--muted); cursor: pointer; }
.format summary:hover { color: var(--fg); }
.format > p { font-size: 13px; color: var(--muted); margin: 10px 0; line-height: 1.55; }
.format__cols {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px; background: var(--raised); border-radius: var(--radius);
  padding: 9px 11px; overflow-x: auto; color: var(--fg) !important;
}

.disclaimer { font-size: 12px; color: var(--faint); max-width: 620px; margin-top: 20px; line-height: 1.6; }

/* ── modal ─────────────────────────────────────────────────────────────── */

.backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 10;
}
.modal {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  width: 100%; max-width: 400px; max-height: calc(100vh - 40px); overflow-y: auto;
  padding: 18px 20px 20px;
}
.modal__head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.modal__head h2 { font-size: 16px; font-weight: 500; margin: 0; }
.modal__nums { display: flex; gap: 10px; margin-bottom: 12px; }
.modal__nums .field { flex: 1; }
.modal__preview { min-height: 26px; margin-bottom: 14px; }
.modal__grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 14px; }
.modal__foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }
.errors { color: var(--danger); font-size: 13px; margin: 0 0 12px; }
.errors:empty { display: none; }

.check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }

/* ── responsive ────────────────────────────────────────────────────────── */

@media (max-width: 1023px) {
  :root { --rail: 60px; }
  .rail__brand, .nav span { display: none; }
  .nav { justify-content: center; padding: 11px 0; }
  .col-optional { display: none; }
  .status { justify-content: flex-end; }
}

@media (max-width: 599px) {
  /* Below this the layout is the phone app: bottom tabs, no rail. */
  :root { --rail: 0px; --max-width: none; }
  .app { grid-template-columns: minmax(0, 1fr); }
  .rail {
    position: fixed; bottom: 0; left: 0; right: 0; width: 100%; z-index: 20;
    flex-direction: row; justify-content: space-around;
    border-right: 0; border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
    background: var(--surface);
  }
  .rail__foot { margin: 0; padding: 0; display: flex; align-items: center; gap: 6px; }
  .nav span { display: inline; font-size: 11px; }
  .nav { flex-direction: column; gap: 3px; padding: 5px 0; }
  main { padding: 14px 14px 90px; }
  .fab { right: 16px; bottom: calc(74px + env(safe-area-inset-bottom, 0px));
         width: 52px; height: 52px; z-index: 19; }
  .topbar { margin-bottom: 12px; }
  .modal { max-width: none; height: 100%; max-height: none; border-radius: 0; border: 0; }
  .backdrop { padding: 0; }
  .modal__grid { grid-template-columns: 1fr; }
  .col-secondary { display: none; }
  .row-actions { display: none; }
  table.readings tbody tr { cursor: pointer; }
  .panel__row select.input { min-width: 0; width: 100%; }
  .panel__row > span:first-child { flex: 1 1 100%; }
}

/* ── print ─────────────────────────────────────────────────────────────── */
/* Chips are light fills with white text — fine on screen, invisible on a
   greyscale or low-ink printer. In print the label goes dark over an
   outlined swatch instead. */

@media print {
  :root { --bg: #fff; --surface: #fff; --raised: #fff; --line: #999; --fg: #000; --muted: #333; }
  .rail, .toolbar, .row-actions, .backdrop, .no-print, .fab { display: none !important; }

  /* Printing is triggered from Settings, so force the report — and only the
     report — regardless of which view happens to be on screen. */
  .printing #view-history[hidden] { display: block !important; }
  .printing #view-charts, .printing #view-settings { display: none !important; }

  /* The latest banner is a solid category fill with white text: fine on a
     screen, an empty box on a greyscale or low-ink printer. Same treatment as
     the chips — dark text in an outlined box. */
  .latest {
    background: none !important; color: #000 !important;
    border: 1px solid #666; border-radius: 8px;
  }
  .latest__when, .latest__unit, .latest__pulse span { color: #333 !important; }
  .crisis { border: 1px solid #666; background: none !important; color: #000 !important; }
  .print-head { display: block; margin-bottom: 14px; }
  .print-head h1 { font-size: 15pt; font-weight: 600; margin: 0 0 2px; }
  .print-head p { font-size: 10pt; color: #333; margin: 0; }
  .app { display: block; }
  main { padding: 0; }
  .tile { border: 1px solid #999; }
  .chip {
    color: #000 !important; background: none !important;
    border: 1px solid #666; padding: 1px 6px;
  }
  table.readings { font-size: 11pt; }
  table.readings td, table.readings th { border-bottom: 1px solid #ccc; }
  tr { page-break-inside: avoid; }
}
