:root {
  color-scheme: light;
  --surface-0: #f4f4f2;
  --surface-1: #fcfcfb;
  --border: #e2e1dc;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #77766f;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;      /* glucose line */
  --series-dose: #4a3aa7;   /* rapid dose markers (violet) */
  --series-basal: #1baf7a;  /* basal dose markers (aqua) */
  --series-meal: #eb6834;   /* meal markers (orange) */
  --range-fill: #e6f1e6;
  --grid: #ebeae6;
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --warn-bg: #fff4dc;
  --crit-bg: #fde8e8;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-0: #111110;
    --surface-1: #1a1a19;
    --border: #33332f;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #94938a;
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-dose: #9085e9;
    --series-basal: #199e70;
    --series-meal: #d95926;
    --range-fill: #1d2a1d;
    --grid: #2a2a27;
    --warn-bg: #3a2f12;
    --crit-bg: #3d1a1a;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #111110;
  --surface-1: #1a1a19;
  --border: #33332f;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #94938a;
  --accent: #3987e5;
  --series-1: #3987e5;
  --series-dose: #9085e9;
  --series-basal: #199e70;
  --series-meal: #d95926;
  --range-fill: #1d2a1d;
  --grid: #2a2a27;
  --warn-bg: #3a2f12;
  --crit-bg: #3d1a1a;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--surface-0);
  color: var(--text-primary);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
.hidden, [hidden] { display: none !important; }
.muted { color: var(--text-muted); font-size: .9em; }
.small-text { font-size: .85em; }
h1 { margin: 0 0 16px; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }
.tab > h2 { margin-top: 20px; }
h3 { font-size: .95rem; margin: 16px 0 8px; color: var(--text-secondary); }
code { font-size: .85em; word-break: break-all; }

#app { min-height: 100vh; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
main { max-width: 720px; margin: 0 auto; padding: 0 16px 16px; }

/* header */
#bg-header {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface-1); border-bottom: 1px solid var(--border);
}
.bg-main { font-size: 3rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1; }
#bg-arrow { font-size: 2rem; margin-left: 6px; color: var(--text-secondary); }
.bg-meta { text-align: right; color: var(--text-secondary); font-size: .9rem; }
.bg-low { color: var(--critical); }
.bg-high { color: var(--warning); }
.stale { opacity: .45; }

.banner { margin: 12px 16px 0; padding: 10px 12px; border-radius: 10px; background: var(--warn-bg); }
.banner button { margin-top: 6px; }

/* cards & forms */
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; padding: 16px; margin-top: 16px; }
.card.narrow { max-width: 360px; margin: 20vh auto 0; }
label { display: block; margin: 0 0 12px; color: var(--text-secondary); font-size: .9rem; }
label.check { display: flex; gap: 8px; align-items: center; color: var(--text-primary); }
input, select {
  display: block; width: 100%; margin-top: 4px; padding: 10px 12px; font: inherit; color: var(--text-primary);
  background: var(--surface-0); border: 1px solid var(--border); border-radius: 10px; min-height: 44px;
}
input[type=checkbox] { width: 22px; height: 22px; min-height: 0; margin: 0; }
.row { display: flex; gap: 12px; }
.row > label { flex: 1; min-width: 0; }
.row-inline { display: flex; gap: 8px; margin-top: 4px; }
.row-inline input { margin-top: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
button {
  font: inherit; padding: 10px 16px; min-height: 44px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-0); color: var(--text-primary);
}
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.small { min-height: 36px; padding: 6px 12px; font-size: .9rem; }
button:disabled { opacity: .5; }
.card > button { width: 100%; margin-top: 4px; }
.card > button + button { margin-top: 8px; }
.status { min-height: 1.2em; margin: 8px 0 0; color: var(--text-secondary); }
.error { color: var(--critical); }

.photo-btn {
  display: flex; align-items: center; justify-content: center; min-height: 64px; margin-bottom: 12px;
  border: 2px dashed var(--border); border-radius: 12px; color: var(--text-primary); font-size: 1rem; cursor: pointer;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.chips button { min-height: 36px; padding: 6px 12px; border-radius: 18px; font-size: .9rem; }
.chips button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.blocks .block { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.blocks input { margin: 0; }
.blocks .block button { min-height: 44px; padding: 0 12px; }

/* estimate */
.estimate-photo { width: 100%; max-height: 240px; object-fit: cover; border-radius: 10px; margin-bottom: 8px; }
.est-items { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: .9rem; }
.est-items td, .est-items th { padding: 6px 4px; border-bottom: 1px solid var(--border); text-align: left; }
.est-items td.num, .est-items th.num { text-align: right; font-variant-numeric: tabular-nums; }
.est-items input { width: 64px; padding: 6px; min-height: 36px; margin: 0; text-align: right; display: inline-block; }
.est-total { font-size: 1.1rem; margin: 4px 0; }

/* suggestion */
.suggest-units { font-size: 2.4rem; font-weight: 700; margin: 12px 0 4px; font-variant-numeric: tabular-nums; }
.steps { margin: 8px 0; padding-left: 18px; color: var(--text-secondary); font-size: .9rem; }
.warn { padding: 8px 10px; margin: 6px 0; border-radius: 8px; background: var(--warn-bg); font-size: .9rem; }
.warn::before { content: "⚠ "; }
.blocked { padding: 10px 12px; margin: 8px 0; border-radius: 8px; background: var(--crit-bg); }
.blocked::before { content: "⛔ "; }

/* chart */
.range-row { display: flex; gap: 6px; margin-top: 12px; }
.range-row button { flex: 1; min-height: 36px; padding: 4px; }
.range-row button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chart { position: relative; height: 260px; margin-top: 12px; background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: 100%; }
.chart .axis text { fill: var(--text-muted); font-size: 11px; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .range { fill: var(--range-fill); }
.chart .glucose { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .low-pt { fill: var(--critical); stroke: var(--surface-1); stroke-width: 2; }
.chart .dose { fill: var(--series-dose); stroke: var(--surface-1); stroke-width: 2; }
.chart .basal { fill: var(--series-basal); stroke: var(--surface-1); stroke-width: 2; }
.chart .meal { fill: var(--series-meal); stroke: var(--surface-1); stroke-width: 2; }
.chart .marker-label { fill: var(--text-secondary); font-size: 10px; text-anchor: middle; }
.chart .cross { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .cross-dot { fill: var(--series-1); stroke: var(--surface-1); stroke-width: 2; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 8px; font-size: .8rem; color: var(--text-secondary); }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
.sw-range { background: var(--range-fill); border: 1px solid var(--border); border-radius: 2px; }
.sw-dose { background: var(--series-dose); }
.sw-basal { background: var(--series-basal); }
.sw-meal { background: var(--series-meal); }
.tooltip {
  position: fixed; z-index: 20; pointer-events: none; padding: 6px 10px; border-radius: 8px; font-size: .85rem;
  background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border); box-shadow: 0 2px 8px rgba(0,0,0,.15);
}

/* lists */
.list { list-style: none; padding: 0; margin: 0; }
.list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.list li .what { flex: 1; min-width: 0; }
.list li .when { color: var(--text-muted); font-size: .85rem; white-space: nowrap; }
.list li button { min-height: 36px; padding: 4px 10px; font-size: .85rem; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }

/* report */
.report-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.report-table th, .report-table td { padding: 6px 4px; border-bottom: 1px solid var(--border); text-align: right; font-variant-numeric: tabular-nums; }
.report-table th:first-child, .report-table td:first-child { text-align: left; }
.report-table th { color: var(--text-secondary); font-weight: 600; }
.conf { font-size: .75rem; padding: 2px 6px; border-radius: 6px; border: 1px solid var(--border); color: var(--text-secondary); white-space: nowrap; }
.table-scroll { overflow-x: auto; }

/* nav */
#tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10; display: flex;
  background: var(--surface-1); border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom);
}
#tabs button { flex: 1; border: 0; border-radius: 0; background: none; min-height: 56px; font-size: .85rem; color: var(--text-muted); padding: 4px 2px; }
#tabs button.on { color: var(--accent); font-weight: 700; }

@media (max-width: 380px) {
  .grid2 { grid-template-columns: 1fr; }
  .bg-main { font-size: 2.4rem; }
}
