:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #161a20;
  --text-muted: #5c6470;
  --border: #e1e4e9;
  --grid: #d8dce2;
  --accent: #2f6fb0;
  --good: #1a8f5e;
  --good-fill: #1a8f5e33;
  --low: #c0392b;
  --low-fill: #c0392b26;
  --high: #b8791a;
  --high-fill: #b8791a26;
  --carb: #8a5cc7;
  --bolus: #2f6fb0;
  --smb: #9aa3b0;
  --basal: #3a8fb7;
  --basal-fill: #3a8fb733;
  --shadow: 0 1px 2px rgba(20,22,26,0.06), 0 1px 1px rgba(20,22,26,0.04);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c;
    --surface: #1c2027;
    --text: #eef0f3;
    --text-muted: #9aa3b0;
    --border: #2b3038;
    --grid: #333944;
    --accent: #7fb0e8;
    --good: #3ddc97;
    --good-fill: #3ddc9733;
    --low: #ff7a72;
    --low-fill: #ff7a7233;
    --high: #f4b942;
    --high-fill: #f4b94233;
    --carb: #c49fef;
    --bolus: #7fb0e8;
    --smb: #6b7280;
    --basal: #6fcbee;
    --basal-fill: #3a8fb733;
    --shadow: 0 1px 2px rgba(0,0,0,0.4), 0 1px 1px rgba(0,0,0,0.3);
  }
}
:root[data-theme="dark"] {
  --bg: #14171c;
  --surface: #1c2027;
  --text: #eef0f3;
  --text-muted: #9aa3b0;
  --border: #2b3038;
  --grid: #333944;
  --accent: #7fb0e8;
  --good: #3ddc97;
  --good-fill: #3ddc9733;
  --low: #ff7a72;
  --low-fill: #ff7a7233;
  --high: #f4b942;
  --high-fill: #f4b94233;
  --carb: #c49fef;
  --bolus: #7fb0e8;
  --smb: #6b7280;
  --basal: #6fcbee;
  --basal-fill: #3a8fb733;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  padding: 14px 14px 40px;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.wrap { max-width: 460px; margin: 0 auto; }

@media (min-width: 640px) {
  body { padding: 40px 20px 56px; }
  .wrap { max-width: 700px; }
  h1 { font-size: 1.3rem; }
  .date { font-size: 0.85rem; }
}

header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
h1 { font-size: 1.05rem; margin: 0; letter-spacing: -0.01em; }
.date { color: var(--text-muted); font-size: 0.72rem; margin-top: 1px; }
#themeToggle {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 50%;
  width: 30px;
  height: 30px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
#themeToggle svg { width: 16px; height: 16px; }
#themeToggle:hover { color: var(--text); border-color: var(--text-muted); }

.now-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
}
.bg-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  box-shadow: var(--shadow);
  min-width: 130px;
  flex: 0 0 auto;
}
.bg-value {
  font-size: 2.2rem;
  font-weight: 700;
  line-height: 1;
  display: flex;
  align-items: baseline;
  gap: 4px;
}
.bg-value.low { color: var(--low); }
.bg-value.high { color: var(--high); }
.bg-value.good { color: var(--good); }
.arrow { font-size: 1.4rem; }
.bg-sub { color: var(--text-muted); font-size: 0.72rem; margin-top: 4px; }

.tiles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  flex: 1 1 240px;
}
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 7px 6px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.tile .label { font-size: 0.52rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.02em; margin-bottom: 1px; }
.tile .value { font-size: clamp(0.95rem, 4.5vw, 1.25rem); font-weight: 600; line-height: 1.1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .sub { font-size: 0.6rem; color: var(--text-muted); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

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

svg { width: 100%; height: auto; display: block; overflow: visible; }
.grid-line { stroke: var(--grid); stroke-width: 1; }
.axis-label { fill: var(--text-muted); font-size: 10px; }
.band { fill: var(--good-fill); }
.glucose-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.dot-low { fill: var(--low); }
.dot-high { fill: var(--high); }
.crosshair { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; opacity: 0; }
.hover-dot { fill: var(--text); stroke: var(--surface); stroke-width: 1.5; opacity: 0; }

.carb-marker, .bolus-marker { cursor: pointer; }
.carb-marker circle { fill: var(--carb); stroke: var(--surface); stroke-width: 1; }
.carb-marker text { fill: var(--carb); font-size: 13px; font-weight: 700; }
.carb-marker.stacked text { font-size: 9px; }
.bolus-marker.manual path { fill: var(--bolus); stroke: var(--surface); stroke-width: 1; }
.bolus-marker.manual text { fill: var(--bolus); font-size: 13px; font-weight: 700; }
.bolus-marker.manual.stacked text { font-size: 9px; }
.bolus-marker.smb circle { fill: var(--smb); stroke: var(--surface); stroke-width: 0.75; }

.basal-area { fill: var(--basal-fill); }
.basal-line { fill: none; stroke: var(--basal); stroke-width: 1.5; }

#tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--text);
  color: var(--bg);
  font-size: 0.74rem;
  padding: 4px 8px;
  border-radius: 6px;
  transform: translate(-50%, -130%);
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.08s;
}
.chart-wrap { position: relative; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 0.72rem;
  color: var(--text-muted);
}
.legend .item { display: flex; align-items: center; gap: 5px; }
.legend .swatch { width: 9px; height: 9px; border-radius: 3px; display: inline-block; }
.legend .swatch.band { background: var(--good-fill); border: 1px solid var(--good); }
.legend .swatch.carb { background: var(--carb); border-radius: 50%; }
.legend .swatch.bolus { background: var(--bolus); }
.legend .swatch.smb { background: var(--smb); border-radius: 50%; }
.legend .swatch.basal { background: var(--basal); }

.section-title { font-size: 0.78rem; font-weight: 600; margin-bottom: 8px; }
.oura-sub { color: var(--text-muted); font-size: 0.68rem; margin-top: 8px; }

.card + .card { margin-top: 10px; }

footer { color: var(--text-muted); font-size: 0.68rem; text-align: center; margin-top: 12px; }
