:root {
  color-scheme: light;
  --bg: #f7f7f5;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7973;
  --grid: #e8e7e2;
  --axis: #b9b8b1;
  --good: #0ca30c;
  --serious: #ec835a;
  --critical: #d03b3b;
  --warning: #fab219;
  --good-text: #006300;
  --critical-text: #b02a2a;
  --insulin: #2a78d6;
  --insulin-soft: #cde2fb;
  --carbs: #eda100;
  --basal: #86b6ef;
  --basal-line: #2a78d6;
  --range-band: rgba(12, 163, 12, 0.07);
  --pending: rgba(122, 121, 115, 0.12);
  --accent: #2a78d6;
  --accent-text: #ffffff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 2px 8px rgba(0, 0, 0, 0.05);
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #141413;
    --surface: #1a1a19;
    --surface-2: #262624;
    --border: #33332f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #8f8e86;
    --grid: #2b2b28;
    --axis: #55554f;
    --good-text: #4fd14f;
    --critical-text: #f08a8a;
    --insulin: #3987e5;
    --insulin-soft: #184f95;
    --carbs: #c98500;
    --basal: #1c5cab;
    --basal-line: #6da7ec;
    --range-band: rgba(12, 163, 12, 0.10);
    --pending: rgba(143, 142, 134, 0.14);
    --accent: #3987e5;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #141413;
  --surface: #1a1a19;
  --surface-2: #262624;
  --border: #33332f;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #8f8e86;
  --grid: #2b2b28;
  --axis: #55554f;
  --good-text: #4fd14f;
  --critical-text: #f08a8a;
  --insulin: #3987e5;
  --insulin-soft: #184f95;
  --carbs: #c98500;
  --basal: #1c5cab;
  --basal-line: #6da7ec;
  --range-band: rgba(12, 163, 12, 0.10);
  --pending: rgba(143, 142, 134, 0.14);
  --accent: #3987e5;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}
main {
  max-width: 760px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-t)) 16px calc(72px + var(--safe-b));
}
h1 { font-size: 22px; margin: 4px 0 12px; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin: 0 0 10px; color: var(--text-2); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
p { margin: 0 0 10px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.tabnum { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  margin-bottom: 12px;
}

/* ---------- Pulsanti e moduli ---------- */
button, .btn {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 14px;
  min-height: 44px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--critical-text); }
button.link { border: none; background: none; color: var(--accent); padding: 6px 0; min-height: 0; }
button:disabled { opacity: 0.5; cursor: default; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }
label { display: block; font-size: 14px; color: var(--text-2); margin-bottom: 4px; }
input, select {
  font: inherit; font-size: 16px; /* evita lo zoom automatico di iOS */
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  min-height: 44px;
}
.field { margin-bottom: 10px; }
.msg { padding: 10px 12px; border-radius: 10px; font-size: 14px; margin: 8px 0; background: var(--surface-2); }
.msg.err { color: var(--critical-text); }
.msg.ok { color: var(--good-text); }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; word-break: break-all; }

/* ---------- Intestazione glicemia ---------- */
.hero { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.hero-value { font-size: 56px; line-height: 1; font-weight: 700; letter-spacing: -0.03em; }
.hero-unit { font-size: 15px; color: var(--muted); margin-left: 4px; font-weight: 500; }
.hero-trend { font-size: 34px; margin-left: 6px; color: var(--text-2); display: inline-block; }
.hero-meta { text-align: right; font-size: 14px; color: var(--text-2); }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; font-weight: 600; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--text);
}
.chip .dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.stale { color: var(--critical-text); font-weight: 600; }

.sources { display: grid; gap: 4px; margin-top: 10px; font-size: 13px; color: var(--text-2); }
.sources b { color: var(--text); font-weight: 600; }

/* ---------- Controlli intervallo ---------- */
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { flex: 1; border: none; background: none; min-height: 34px; padding: 6px 8px; font-size: 14px; border-radius: 8px; color: var(--text-2); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.daynav { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.daynav button { min-width: 44px; }

/* ---------- Statistiche ---------- */
.stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-top: 12px; }
.stat { text-align: center; }
.stat .v { font-size: 18px; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
@media (max-width: 360px) { .stats { grid-template-columns: repeat(3, 1fr); } }

/* ---------- Grafico ---------- */
.chart-wrap { position: relative; margin: 6px -6px 0; touch-action: pan-y; user-select: none; -webkit-user-select: none; }
.chart-wrap svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart-wrap text { font-family: inherit; }
.tooltip {
  position: absolute; top: 0; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-radius: 10px; padding: 8px 10px; font-size: 13px; min-width: 130px; z-index: 2;
}
.tooltip .t { color: var(--muted); font-size: 12px; margin-bottom: 2px; }
.tooltip .line { display: flex; align-items: center; gap: 6px; }
.tooltip .sw { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--text-2); margin-top: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.legend i.sq { border-radius: 2px; }
.legend i.hatch { background: repeating-linear-gradient(135deg, var(--critical) 0 2px, transparent 2px 5px); border: 1px solid var(--critical); border-radius: 2px; }
.legend i.pend { background: repeating-linear-gradient(135deg, var(--muted) 0 1px, transparent 1px 5px); border-radius: 2px; }

/* ---------- Tabella eventi ---------- */
table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 7px 4px; border-bottom: 1px solid var(--grid); }
th { font-weight: 600; color: var(--text-2); font-size: 12px; text-transform: uppercase; }
td.num, th.num { text-align: right; }

.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.iconbtn { min-width: 44px; min-height: 44px; padding: 0; display: inline-flex; align-items: center; justify-content: center; border-radius: 12px; font-size: 20px; }
.disclaimer { font-size: 12px; color: var(--muted); margin-top: 14px; line-height: 1.45; }

.list-item { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--grid); }
.list-item:last-child { border-bottom: none; }

/* ---------- Etichetta di pagina ---------- */
.page-label {
  position: fixed; right: 8px; bottom: calc(8px + var(--safe-b));
  z-index: 50; min-height: 0; padding: 3px 8px; border-radius: 6px;
  font-size: 11px; color: var(--muted); background: var(--surface-2);
  border: 1px solid var(--border); opacity: 0.85;
}
.page-label.copied { color: var(--good-text); }

/* ---------- Impostazioni: sezioni e fonti dati ---------- */
:root { --dexcom: #1baf7a; --dexcom-soft: rgba(27, 175, 122, 0.12); --tandem: #2a78d6; --tandem-soft: rgba(42, 120, 214, 0.12); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --dexcom: #2cc48d; --dexcom-soft: rgba(44, 196, 141, 0.14); --tandem: #5598e7; --tandem-soft: rgba(85, 152, 231, 0.16); }
}
:root[data-theme="dark"] { --dexcom: #2cc48d; --dexcom-soft: rgba(44, 196, 141, 0.14); --tandem: #5598e7; --tandem-soft: rgba(85, 152, 231, 0.16); }

.section-title { margin: 22px 2px 8px; }
.source { border-left: 4px solid var(--src); }
.source-dexcom { --src: var(--dexcom); --src-soft: var(--dexcom-soft); }
.source-tandem { --src: var(--tandem); --src-soft: var(--tandem-soft); }
.source-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.source-icon {
  flex: none; width: 44px; height: 44px; border-radius: 12px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--src-soft); color: var(--src);
}
.source-icon svg { width: 26px; height: 26px; }
.source-title { display: flex; flex-direction: column; flex: 1; min-width: 0; line-height: 1.25; }
.source-title b { font-size: 18px; }
.chip.ok { color: var(--good-text); }
.chip.warn { color: var(--critical-text); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0 0 10px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
button.link.danger { color: var(--critical-text); }

details.fold { padding: 0; }
details.fold > summary {
  list-style: none; cursor: pointer; padding: 14px; font-weight: 600;
  display: flex; justify-content: space-between; align-items: center; min-height: 48px;
}
details.fold > summary::-webkit-details-marker { display: none; }
details.fold > summary::after { content: '›'; color: var(--muted); font-size: 20px; transition: transform .15s; }
details.fold[open] > summary::after { transform: rotate(90deg); }
.fold-body { padding: 0 14px 14px; }

/* ---------- Freschezza dati pompa ---------- */
.pump-fresh {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  margin-top: 14px; padding: 10px 12px; border-radius: 12px; background: var(--surface-2);
}
.pump-label { font-size: 13px; font-weight: 600; color: var(--text-2); }
.pump-age { font-size: 28px; font-weight: 700; letter-spacing: -0.01em; white-space: nowrap; line-height: 1.15; }
.pump-right { text-align: right; }
.pump-fresh.warn .pump-ago { color: var(--serious); }
.pump-fresh.crit .pump-ago { color: var(--critical-text); }
.pump-fresh.none .pump-age { color: var(--muted); }

/* ---------- Campo password con occhio ---------- */
.pw-wrap { position: relative; }
.pw-wrap input { padding-right: 52px; }
.pw-eye {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  min-height: 40px; min-width: 44px; padding: 0; border: none; background: none; font-size: 18px; line-height: 1;
}

/* ---------- App installata: fascia dietro la barra di stato di iOS ---------- */
body::before {
  content: ''; position: fixed; top: 0; left: 0; right: 0;
  height: env(safe-area-inset-top, 0px); background: #141413; z-index: 60; /* testo barra di stato bianco (black-translucent) */
}
.haptic { position: fixed; left: -9999px; top: 0; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* ---------- Ricarica forzata (tutte le schermate) ---------- */
.hard-reload {
  position: fixed; left: 8px; bottom: calc(8px + var(--safe-b));
  z-index: 50; min-height: 0; padding: 3px 8px; border-radius: 6px;
  font-size: 11px; color: var(--muted); background: var(--surface-2);
  border: 1px solid var(--border); opacity: 0.85;
}
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
