/* Vitals. Dark, phone-first. Colours are named once here and used by role. */

:root {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #252523;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);

  /* series */
  --blue: #3987e5;
  --orange: #d95926;

  /* glucose ranges (status colours: always shown with a label) */
  --very-low: #d03b3b;
  --low: #ee8f8f;
  --in-range: #0ca30c;
  --high: #fab219;
  --very-high: #e2662c;

  --danger: #d03b3b;
  --radius: 14px;
  --tabbar-h: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: contain;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

h1, h2, h3, p { margin: 0; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 15px; font-weight: 600; color: var(--ink); }
a { color: var(--blue); }

.screen {
  max-width: 680px;
  margin: 0 auto;
  padding: calc(14px + var(--safe-t)) 16px calc(var(--tabbar-h) + var(--safe-b) + 28px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  outline: none;
}

.loading, .empty { color: var(--muted); padding: 28px 4px; text-align: center; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.subtle { color: var(--ink-2); }
.row { display: flex; align-items: center; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grow { flex: 1 1 auto; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* ---------- cards ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.card-sub { color: var(--muted); font-size: 13px; }
.stale { opacity: 0.45; transition: opacity 120ms; }

/* ---------- hero (current glucose) ---------- */

.hero { align-items: center; text-align: center; gap: 6px; padding: 18px 14px 16px; }
.hero-value { display: flex; align-items: center; justify-content: center; gap: 10px; }
.hero-number { font-size: 76px; line-height: 1; font-weight: 650; letter-spacing: -0.02em; }
.hero-arrow { font-size: 40px; line-height: 1; color: var(--ink-2); }
.hero-unit { color: var(--muted); font-size: 14px; }
.hero-asof { color: var(--ink-2); font-size: 15px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2);
  font-size: 13px; color: var(--ink-2); white-space: nowrap;
}
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.very-low { background: var(--very-low); }
.dot.low { background: var(--low); }
.dot.in-range { background: var(--in-range); }
.dot.high { background: var(--high); }
.dot.very-high { background: var(--very-high); }
.dot.blue { background: var(--blue); }
.dot.orange { background: var(--orange); }

/* ---------- controls ---------- */

.filters { display: flex; flex-direction: column; gap: 10px; }
.seg { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.seg::-webkit-scrollbar { display: none; }
.seg button {
  flex: 1 0 auto; min-height: 44px; min-width: 44px; padding: 0 10px;
  border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--ink-2); font: inherit; font-size: 14.5px; cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--ink); color: #0b0b0b; border-color: var(--ink); font-weight: 600; }

.btn {
  min-height: 48px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--ink);
  font: inherit; font-weight: 550; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  text-decoration: none;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-big { min-height: 68px; font-size: 20px; font-weight: 650; width: 100%; border-radius: 16px; }
.btn-small { min-height: 40px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn-ghost { background: transparent; }
.btn-row { display: flex; gap: 10px; }
.btn-row > * { flex: 1; }

.toggle { display: inline-flex; align-items: center; gap: 10px; min-height: 44px; cursor: pointer; color: var(--ink-2); font-size: 15px; }
.toggle input { appearance: none; -webkit-appearance: none; width: 46px; height: 28px; border-radius: 999px; background: var(--axis); position: relative; margin: 0; flex: none; cursor: pointer; transition: background 120ms; }
.toggle input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: var(--ink); transition: transform 120ms; }
.toggle input:checked { background: var(--blue); }
.toggle input:checked::after { transform: translateX(18px); }

label.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); min-width: 0; }
input[type="text"], input[type="number"], input[type="date"], select, textarea {
  width: 100%; min-height: 48px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--ink);
  font: inherit; font-size: 16px;
}
textarea { min-height: 110px; resize: vertical; line-height: 1.4; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--blue); outline-offset: 2px;
}
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; color: var(--ink-2); font-size: 15px; cursor: pointer; }
.check input { width: 22px; height: 22px; accent-color: var(--blue); flex: none; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-grid .wide { grid-column: 1 / -1; }
.form-error { color: var(--low); font-size: 14px; }

/* ---------- stat tiles ---------- */

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; min-width: 0; }
.tile-label { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-value { font-size: 24px; font-weight: 650; line-height: 1.2; }
.tile-value small { font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 3px; }
.tile-note { color: var(--muted); font-size: 12px; }
.tiles.two { grid-template-columns: 1fr 1fr; }
.tile.wide { grid-column: 1 / -1; }
.tile.wide .tile-label { white-space: normal; }
@media (max-width: 340px) { .tiles { grid-template-columns: repeat(2, 1fr); } }

/* ---------- time-in-range bar ---------- */

.tir { display: flex; gap: 2px; height: 28px; }
.tir-seg { border-radius: 4px; min-width: 3px; display: flex; align-items: center; padding-left: 6px; font-size: 12px; font-weight: 650; overflow: visible; white-space: nowrap; }
.tir-seg.very-low { background: var(--very-low); color: #fff; }
.tir-seg.low { background: var(--low); color: #0b0b0b; }
.tir-seg.in-range { background: var(--in-range); color: #fff; }
.tir-seg.high { background: var(--high); color: #0b0b0b; }
.tir-seg.very-high { background: var(--very-high); color: #fff; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 13px; color: var(--ink-2); }
.legend > span { display: inline-flex; align-items: center; gap: 6px; }
.legend b { color: var(--ink); font-weight: 600; }
.key-line { width: 16px; height: 0; border-top: 2px solid var(--muted); flex: none; }
.key-line.blue { border-color: var(--blue); }
.key-line.orange { border-color: var(--orange); }
.key-box { width: 12px; height: 12px; border-radius: 3px; flex: none; background: var(--axis); }
.key-box.blue { background: var(--blue); }
.key-box.orange { background: var(--orange); }
.key-box.band { background: rgba(12, 163, 12, 0.22); }
.key-box.note { background: rgba(255, 255, 255, 0.10); }
.key-box.flagged { background: rgba(250, 178, 25, 0.28); }
.key-tri { width: 0; height: 0; border-left: 5px solid transparent; border-right: 5px solid transparent; border-bottom: 9px solid var(--ink-2); flex: none; }

/* ---------- charts ---------- */

.chart { position: relative; width: 100%; height: 240px; }
.chart.tall { height: 280px; }
.chart.short { height: 150px; }
.chart.mini { height: 120px; }
.chart canvas { display: block; }
.chart-title { font-size: 13px; color: var(--ink-2); }

/* ---------- tables ---------- */

.table-wrap { overflow-x: auto; margin: 0 -14px; padding: 0 14px; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 8px 8px; text-align: right; border-bottom: 1px solid var(--grid); white-space: nowrap; font-variant-numeric: tabular-nums; }
th { color: var(--muted); font-weight: 500; font-size: 12px; }
th:first-child, td:first-child { text-align: left; padding-left: 0; }
td.text, th.text { text-align: left; white-space: normal; }
tr.flagged td { color: var(--muted); }
tr:last-child td { border-bottom: 0; }
.delta-up::before { content: "▲ "; font-size: 10px; }
.delta-down::before { content: "▼ "; font-size: 10px; }

/* ---------- lists ---------- */

.list { display: flex; flex-direction: column; }
.item { display: flex; align-items: flex-start; gap: 10px; padding: 12px 0; border-bottom: 1px solid var(--grid); }
.item:last-child { border-bottom: 0; padding-bottom: 0; }
.item:first-child { padding-top: 0; }
.item-main { flex: 1 1 auto; min-width: 0; }
.item-title { font-weight: 600; }
.item-sub { color: var(--muted); font-size: 13px; }
.item-text { color: var(--ink-2); font-size: 14px; white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 4px; }
.item-text.clamp { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.item-actions { display: flex; gap: 6px; flex: none; }
.icon-btn { width: 44px; height: 44px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); color: var(--ink-2); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; padding: 0; }
.icon-btn svg, .tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.link-btn { background: none; border: 0; color: var(--blue); font: inherit; font-size: 13px; padding: 6px 0; cursor: pointer; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); color: var(--ink-2); background: var(--surface-2); white-space: nowrap; }
.badge.flag { border-color: rgba(250, 178, 25, 0.5); color: var(--high); }
td .badge { margin-left: 6px; }

.big-number { font-size: 34px; font-weight: 650; line-height: 1.1; }

/* ---------- bottom tab bar ---------- */

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: rgba(20, 20, 19, 0.96); border-top: 1px solid var(--border);
  backdrop-filter: blur(10px);
}
.tabbar a {
  flex: 1 1 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); text-decoration: none; font-size: 11px; min-width: 0;
}
.tabbar a[aria-current="page"] { color: var(--ink); }
.tabbar a[aria-current="page"] svg { stroke: var(--blue); }

/* ---------- banner, toast, dialog ---------- */

.banner {
  position: sticky; top: 0; z-index: 30; padding: calc(10px + var(--safe-t)) 16px 10px;
  background: #3a2a08; color: #ffe2a3; font-size: 14px; border-bottom: 1px solid rgba(250, 178, 25, 0.4);
  display: flex; align-items: center; gap: 12px; justify-content: space-between;
}
.banner .btn { min-height: 40px; flex: none; }
.notice { border-color: rgba(250, 178, 25, 0.45); background: #2a2110; color: #ffe2a3; font-size: 14px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + var(--safe-b) + 14px); z-index: 40;
  background: var(--ink); color: #0b0b0b; padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 550;
  max-width: calc(100vw - 32px); box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
}
.toast.error { background: var(--danger); color: #fff; }

.dialog { border: 1px solid var(--border); border-radius: 16px; background: var(--surface); color: var(--ink); padding: 20px; width: min(92vw, 380px); }
.dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.dialog p { margin-bottom: 16px; }
.dialog-buttons { display: flex; gap: 10px; }
.dialog-buttons .btn { flex: 1; }

pre.sample { margin: 0; padding: 10px 12px; background: var(--surface-2); border-radius: 10px; font-size: 12px; overflow-x: auto; color: var(--ink-2); }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
