:root {
  color-scheme: light;
  --page: #f6f6f3;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #74726c;
  --grid: #e7e6e0;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.09);
  --accent: #2a78d6;
  --series-1: #2a78d6;
  --series-1-soft: #86b6ef;
  --band: rgba(42, 120, 214, 0.12);
  --stage-deep: #184f95;
  --stage-light: #6da7ec;
  --stage-rem: #6250d6;
  --stage-awake: #e87ba4;
  --good: #006300;
  --good-bg: rgba(12, 163, 12, 0.12);
  --warn: #8a5a00;
  --warn-bg: rgba(250, 178, 25, 0.2);
  --bad: #b02a2a;
  --bad-bg: rgba(208, 59, 59, 0.12);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 2px 8px rgba(0, 0, 0, 0.03);
  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242422;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #98968f;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.08);
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-1-soft: #3471bd;
    --band: rgba(57, 135, 229, 0.16);
    --stage-deep: #256abf;
    --stage-light: #86b6ef;
    --stage-rem: #9085e9;
    --stage-awake: #d55181;
    --good: #2fc02f;
    --good-bg: rgba(12, 163, 12, 0.18);
    --warn: #fab219;
    --warn-bg: rgba(250, 178, 25, 0.15);
    --bad: #f07272;
    --bad-bg: rgba(208, 59, 59, 0.2);
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #242422;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #98968f;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.08);
  --accent: #3987e5;
  --series-1: #3987e5;
  --series-1-soft: #3471bd;
  --band: rgba(57, 135, 229, 0.16);
  --stage-deep: #256abf;
  --stage-light: #86b6ef;
  --stage-rem: #9085e9;
  --stage-awake: #d55181;
  --good: #2fc02f;
  --good-bg: rgba(12, 163, 12, 0.18);
  --warn: #fab219;
  --warn-bg: rgba(250, 178, 25, 0.15);
  --bad: #f07272;
  --bad-bg: rgba(208, 59, 59, 0.2);
  --shadow: none;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.4;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}

body.loading .view { opacity: 0.5; transition: opacity 0.2s; }

button { font: inherit; color: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand img { border-radius: 7px; }

.view {
  max-width: 980px;
  margin: 0 auto;
  padding: 4px 16px 24px;
}

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

.empty { text-align: center; color: var(--ink-2); padding: 32px 20px; margin-top: 12px; }
.empty strong { display: block; color: var(--ink); font-size: 17px; margin-bottom: 6px; }
.empty p { margin: 0 0 14px; }

.btn {
  border: 0;
  background: var(--accent);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-weight: 600;
  cursor: pointer;
}

.muted { color: var(--muted); font-weight: 400; }

/* ---------- chip e variazioni ---------- */

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--ink-2);
  font-size: 13px;
  white-space: nowrap;
}
.chip.good { background: var(--good-bg); color: var(--good); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }
.chip.bad { background: var(--bad-bg); color: var(--bad); }
.ico { font-size: 0.85em; line-height: 1; }

.delta {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.delta.good { color: var(--good); }
.delta.warn { color: var(--warn); }
.delta.bad { color: var(--bad); }
.delta.muted { color: var(--muted); }

/* ---------- vista Oggi ---------- */

.day-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 4px 0 12px;
}
.day-title { text-align: center; display: flex; flex-direction: column; }
.day-title strong { font-size: 19px; text-transform: capitalize; }
.day-title span { font-size: 13px; color: var(--muted); text-transform: capitalize; }

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:disabled { opacity: 0.3; cursor: default; }

.hero {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.hero-main { display: flex; flex-direction: column; }
.hero-label { font-size: 13px; color: var(--muted); }
.hero-value { font-size: 44px; font-weight: 700; line-height: 1.05; }
.hero-sub { font-size: 14px; color: var(--ink-2); }
.hero-chips { display: flex; flex-wrap: wrap; gap: 6px; }

.tiles {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
@media (min-width: 640px) { .tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (min-width: 900px) { .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); } }

.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.tile h3 { margin: 0; font-size: 13px; font-weight: 600; color: var(--muted); }
.tile-value { font-size: 26px; font-weight: 700; line-height: 1.15; }
.tile-value .unit { font-size: 13px; font-weight: 500; color: var(--muted); margin-left: 4px; }
.tile-sub { font-size: 12.5px; color: var(--ink-2); }
.spark { width: 100%; height: 28px; margin-top: auto; padding-top: 4px; }

.tiles .tile:first-child { grid-column: 1 / -1; }
@media (min-width: 640px) { .tiles .tile:first-child { grid-column: span 2; } }

.stages { margin: 6px 0 2px; }
.stages-bar {
  display: flex;
  gap: 2px;
  height: 10px;
  border-radius: 5px;
  overflow: hidden;
}
.stages-legend {
  list-style: none;
  padding: 0;
  margin: 8px 0 0;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.stages-legend .key {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 3px;
  margin-right: 5px;
  vertical-align: -1px;
}

.section-title { font-size: 15px; margin: 20px 2px 8px; }

/* ---------- liste ---------- */

.list { padding: 4px 0; }
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
}
.row + .row { border-top: 1px solid var(--border); }
.row-icon { font-size: 22px; width: 32px; text-align: center; }
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; font-size: 13.5px; }
.row-main strong { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-side { display: flex; flex-direction: column; align-items: flex-end; font-size: 12px; }
.row-side strong { font-size: 17px; }

/* ---------- vista Trend ---------- */

.filters {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 4px 0 12px;
  scrollbar-width: none;
}
.pill {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 14px;
  white-space: nowrap;
  cursor: pointer;
}
.pill.active { background: var(--ink); color: var(--page); border-color: var(--ink); font-weight: 600; }

.charts { display: grid; gap: 12px; }
@media (min-width: 900px) { .charts { grid-template-columns: 1fr 1fr; } }

.chart-card header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.chart-card h3 { margin: 0; font-size: 15px; }
.chart-summary { font-size: 13px; color: var(--ink-2); }

.chart { position: relative; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .band { fill: var(--band); }
.chart .hair { stroke: var(--axis); stroke-width: 1; }
.chart .hover-dot { stroke: var(--surface); stroke-width: 2; }
.chart .overlay { outline: none; cursor: crosshair; }
.chart-empty { color: var(--muted); font-size: 13px; text-align: center; margin: 40px 0; }

.tip {
  position: absolute;
  pointer-events: none;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12.5px;
  min-width: 120px;
  z-index: 2;
}
.tip-date { color: var(--muted); margin-bottom: 4px; text-transform: capitalize; }
.tip-row { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.tip-row strong { font-variant-numeric: tabular-nums; }
.tip-label { color: var(--ink-2); }
.tip-key { width: 12px; height: 3px; border-radius: 2px; flex: none; }
.tip-key.band-key { height: 8px; background: var(--band); }

.table-toggle { margin-top: 8px; font-size: 13px; }
.table-toggle summary { color: var(--muted); cursor: pointer; }
.table-wrap { max-height: 260px; overflow: auto; margin-top: 8px; }
.table-wrap table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table-wrap th, .table-wrap td { text-align: right; padding: 4px 6px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table-wrap th:first-child, .table-wrap td:first-child { text-align: left; }
.table-wrap th { position: sticky; top: 0; background: var(--surface); color: var(--muted); font-weight: 600; }

/* ---------- barra di navigazione ---------- */

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  display: flex;
  justify-content: center;
  gap: 4px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
}
.tabbar button {
  flex: 1;
  max-width: 160px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 4px;
  border: 0;
  border-radius: 12px;
  background: none;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
}
.tabbar button span { font-size: 18px; line-height: 1.1; }
.tabbar button.active { color: var(--accent); background: var(--surface-2); font-weight: 600; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
