:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --fg: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --on-color: #1b1b1b;
  --focus: #2563eb;
  --band-fill: rgba(34, 197, 94, 0.13);
  --meal: #93c5fd;
  --novorapid: #f5b700;
  --tresiba: #a3e635;
  --very-low: #7f1d1d;
  --low: #ef4444;
  --in-range: #22c55e;
  --high: #fbbf24;
  --very-high: #c2410c;
  --warn: #b45309;
  --danger: #b91c1c;
  --radius: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #131c2e;
    --surface-2: #1c2740;
    --fg: #e5e7eb;
    --muted: #94a3b8;
    --line: #263247;
    --focus: #60a5fa;
    --band-fill: rgba(34, 197, 94, 0.14);
    --warn: #fbbf24;
    --danger: #f87171;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#view {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(8px + env(safe-area-inset-top)) 16px calc(96px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

h1 { font-size: 1.6rem; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 0.95rem; color: var(--muted); font-weight: 600; margin: 8px 0 0; }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }

.topline { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; padding-top: 8px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }

button, .button {
  font: inherit;
  min-height: 44px;
  border: 0;
  border-radius: 12px;
  padding: 0 14px;
  cursor: pointer;
  color: var(--fg);
  background: var(--surface-2);
}
button:disabled { opacity: 0.45; cursor: not-allowed; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
button.primary { background: var(--fg); color: var(--bg); font-weight: 700; }

input, select {
  font: inherit;
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--fg);
}
label { display: block; margin: 12px 0 6px; color: var(--muted); font-size: 0.9rem; }

/* glucose card */
.gcard {
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 8px solid var(--muted);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.gcard.band-very_low { border-left-color: var(--very-low); }
.gcard.band-low { border-left-color: var(--low); }
.gcard.band-in_range { border-left-color: var(--in-range); }
.gcard.band-high { border-left-color: var(--high); }
.gcard.band-very_high { border-left-color: var(--very-high); }
.gvalue { display: flex; align-items: baseline; gap: 8px; }
.gvalue .num { font-size: 3.4rem; font-weight: 750; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.gvalue .arrow { font-size: 2.1rem; line-height: 1; }
.gvalue .unit { color: var(--muted); }
.gmeta { display: flex; align-items: center; gap: 6px; color: var(--muted); margin-top: 6px; font-size: 0.9rem; }
.gdelta { color: var(--muted); font-size: 0.9rem; }
.gcard.stale .gvalue { opacity: 0.55; }
.gcard.stale .gmeta { color: var(--warn); font-weight: 600; }

/* segmented control */
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { min-height: 36px; border-radius: 999px; padding: 0 14px; background: transparent; border: 1px solid var(--line); font-size: 0.9rem; }
.seg button.on { background: var(--fg); color: var(--bg); border-color: transparent; }

.chart { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; padding: 6px 4px 0; }

/* big actions */
.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.action {
  min-height: 84px;
  border-radius: 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--on-color);
}
.action:active { transform: scale(0.97); }
.kind-meal { background: var(--meal); color: var(--on-color); }
.kind-novorapid { background: var(--novorapid); color: var(--on-color); }
.kind-tresiba { background: var(--tresiba); color: var(--on-color); }

.daysum { margin: 0; color: var(--muted); font-size: 0.9rem; }

/* entry list */
.entries { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.entries .empty { color: var(--muted); padding: 8px 2px; }
.entry {
  width: 100%;
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: 12px;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
  min-height: 56px;
}
.badge { width: 34px; height: 34px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; }
.entry-main { display: flex; flex-direction: column; min-width: 0; }
.entry-title { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.entry-note { color: var(--muted); font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry-amount { font-variant-numeric: tabular-nums; }
.entry-time { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.linked { color: var(--muted); display: inline-flex; }

/* tab bar */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  z-index: 10;
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-height: 48px;
  justify-content: center;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.72rem;
  border-radius: 12px;
}
.tab[aria-current="page"] { color: var(--fg); font-weight: 700; }

/* setup */
.setup { max-width: 420px; margin: 0 auto; padding: calc(32px + env(safe-area-inset-top)) 16px 32px; display: flex; flex-direction: column; gap: 16px; }
.setup .primary { margin-top: 12px; width: 100%; }
.disclaimer { color: var(--muted); font-size: 0.85rem; }

.uplot { font-family: inherit; }

/* logging sheet */
.sheet {
  margin: auto auto 0;
  width: 100%;
  max-width: 560px;
  max-height: 92vh;
  border: 0;
  border-radius: 22px 22px 0 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
}
.sheet::backdrop { background: rgba(2, 6, 23, 0.55); }
.sheet-body { display: flex; flex-direction: column; gap: 10px; padding: 12px 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet-head { display: grid; grid-template-columns: 80px 1fr 80px; align-items: center; }
.sheet-head h2 { margin: 0; color: var(--fg); font-size: 1.05rem; display: flex; align-items: center; justify-content: center; gap: 8px; }
button.ghost { background: transparent; color: var(--muted); padding: 0 4px; justify-self: start; }
.stepper { display: grid; grid-template-columns: 64px 1fr 64px; align-items: center; gap: 8px; margin-top: 4px; }
.round { width: 64px; height: 64px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; background: var(--surface); border: 1px solid var(--line); }
.amount { display: flex; align-items: baseline; justify-content: center; gap: 6px; }
.amount-input {
  width: 4.2ch;
  text-align: right;
  font-size: 3rem;
  font-weight: 750;
  border: 0;
  background: transparent;
  padding: 0;
  min-height: 0;
  font-variant-numeric: tabular-nums;
}
.amount .unit { font-size: 1.4rem; color: var(--muted); }
.hint { margin: 0; text-align: center; color: var(--muted); font-size: 0.85rem; }
.label { margin: 4px 0 6px; color: var(--muted); font-size: 0.85rem; }
.field-error { margin: 0; color: var(--danger); font-size: 0.85rem; text-align: center; min-height: 0; }
.field-error:empty { display: none; }
.linkbox:empty { display: none; }
.linkbox { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; }
.linkrow { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.linklabel { margin: 0; color: var(--fg); display: inline-flex; align-items: center; gap: 8px; font-size: 0.95rem; }
.switch { appearance: none; width: 52px; height: 32px; min-height: 0; border-radius: 999px; background: var(--line); position: relative; cursor: pointer; padding: 0; border: 0; flex: none; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: left 0.15s; }
.switch:checked { background: var(--in-range); }
.switch:checked::after { left: 23px; }
.switch:disabled { opacity: 0.4; }
.summary { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px; font-size: 0.9rem; }
.summary dt { color: var(--muted); }
.summary dd { margin: 0; font-weight: 600; text-align: right; }
.save { min-height: 54px; font-weight: 750; font-size: 1.05rem; border-radius: 16px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
button.danger { background: transparent; color: var(--danger); display: inline-flex; align-items: center; justify-content: center; gap: 6px; }

/* journal, stats, settings */
.daynav { display: grid; grid-template-columns: 52px 1fr 52px; gap: 8px; align-items: center; }
.small-round { width: 48px; height: 48px; }
.chips { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; }
.stat-chip { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; display: flex; flex-direction: column; }
.notice { display: flex; align-items: center; gap: 8px; margin: 0; padding: 10px 12px; border-radius: 12px; background: color-mix(in srgb, var(--warn) 14%, transparent); color: var(--fg); font-size: 0.9rem; }
.coverage { margin: 0; }
.bands { display: flex; flex-direction: column; gap: 10px; }
.bandbar { display: flex; height: 18px; border-radius: 6px; overflow: hidden; background: var(--surface-2); }
.bandbar span { height: 100%; }
.bandlist, .kv { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; margin: 0; }
.bandlist dt, .kv dt { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.bandlist dd, .kv dd { margin: 0; text-align: right; font-weight: 650; font-variant-numeric: tabular-nums; }
.bandlist dd.tir { color: var(--in-range); }
.bandlist .total { border-top: 1px solid var(--line); padding-top: 6px; }
.dot { width: 12px; height: 12px; border-radius: 3px; display: inline-block; flex: none; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; }
.tile b { font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.card h2 { margin: 0 0 10px; }
details.card summary { cursor: pointer; font-weight: 600; min-height: 32px; }
details.card .kv { margin-top: 10px; }
.reportline { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.reportline .small { word-break: break-all; }
.field { margin-bottom: 12px; }
.field label { margin-top: 0; }
.field label input, .field label select { margin-top: 6px; }
.field-status { display: inline-flex; align-items: center; gap: 4px; font-size: 0.85rem; color: var(--muted); min-height: 1.2em; }
.field-status.error { color: var(--danger); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.steps { margin: 0 0 10px; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }
code { background: var(--surface-2); padding: 1px 6px; border-radius: 6px; word-break: break-all; }

/* sliders */
.slider-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 8px; color: var(--muted); font-size: 0.9rem; }
.slider-value { color: var(--fg); font-weight: 750; font-size: 1.15rem; font-variant-numeric: tabular-nums; background: var(--surface-2); padding: 2px 10px; border-radius: 999px; }
.slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 32px;
  min-height: 0;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  --pct: 50%;
}
.slider::-webkit-slider-runnable-track {
  height: 8px;
  border-radius: 999px;
  background: linear-gradient(to right, var(--fg) var(--pct), var(--line) var(--pct));
}
.slider::-moz-range-track { height: 8px; border-radius: 999px; background: var(--line); }
.slider::-moz-range-progress { height: 8px; border-radius: 999px; background: var(--fg); }
.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 28px;
  height: 28px;
  margin-top: -10px;
  border-radius: 50%;
  background: var(--surface);
  border: 3px solid var(--fg);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
}
.slider::-moz-range-thumb { width: 24px; height: 24px; border-radius: 50%; background: var(--surface); border: 3px solid var(--fg); }
.slider:focus-visible { outline: none; }
.slider:focus-visible::-webkit-slider-thumb { outline: 3px solid var(--focus); outline-offset: 2px; }
.slider-scale { display: flex; justify-content: space-between; margin-top: 2px; }
