/* Telegram theme first (--tg-theme-* are set by telegram-web-app.js), validated fallbacks outside Telegram.
   Macro colors are categorical slots 1–3 of the validated palette (light/dark steps, CVD-checked);
   critical red is the reserved status color and always comes with an icon and a label. */
:root {
  color-scheme: light;
  --fb-bg: #f2f2f0;
  --fb-card: #fcfcfb;
  --fb-ink: #0b0b0b;
  --fb-ink-2: #52514e;
  --fb-muted: #898781;
  --series-protein: #2a78d6;
  --series-fat: #eb6834;
  --series-carbs: #1baf7a;
  --series-insulin: #6250d6;
  --critical: #d03b3b;

  --bg: var(--tg-theme-secondary-bg-color, var(--fb-bg));
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--fb-card)));
  --ink: var(--tg-theme-text-color, var(--fb-ink));
  --ink-2: var(--tg-theme-subtitle-text-color, var(--tg-theme-hint-color, var(--fb-ink-2)));
  --muted: var(--tg-theme-hint-color, var(--fb-muted));
  --accent: var(--tg-theme-button-color, #2a78d6);
  --accent-ink: var(--tg-theme-button-text-color, #ffffff);
  --hairline: color-mix(in srgb, var(--ink) 10%, transparent);
  --series-kcal: var(--ink);
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --fb-bg: #0d0d0d;
  --fb-card: #1a1a19;
  --fb-ink: #f0efec;
  --fb-ink-2: #c3c2b7;
  --fb-muted: #898781;
  --series-protein: #3987e5;
  --series-fat: #d95926;
  --series-carbs: #199e70;
  --series-insulin: #9085e9;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(8px + var(--tg-content-safe-area-inset-top, 0px)) 16px calc(24px + var(--tg-safe-area-inset-bottom, 0px));
}

/* ---------- day switcher ---------- */
.daybar { display: flex; align-items: center; gap: 8px; }
.nav {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 12px;
  background: var(--card); font-size: 26px; line-height: 1; cursor: pointer;
}
.nav:disabled { opacity: 0.35; cursor: default; }
.day {
  position: relative; flex: 1; min-width: 0; height: 52px; border-radius: 12px; background: var(--card);
  display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: pointer;
}
.day__title { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; padding: 0 8px; }
.day__sub { font-size: 12px; color: var(--muted); }
/* The native date picker sits invisibly over the title: a tap opens the system calendar. */
.day__input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; border: 0; cursor: pointer; }
.today-row { display: flex; justify-content: center; min-height: 8px; }
.chip {
  margin-top: 8px; border: 0; border-radius: 999px; padding: 6px 14px;
  background: var(--accent); color: var(--accent-ink); font-size: 13px; font-weight: 600; cursor: pointer;
}

.content { margin-top: 8px; transition: opacity 0.15s; }
.content.loading { opacity: 0.55; }

.card { background: var(--card); border-radius: 16px; padding: 16px; margin-top: 12px; }
.card__title { margin: 0 0 12px; font-size: 13px; font-weight: 600; letter-spacing: 0.02em; text-transform: uppercase; color: var(--muted); }

/* ---------- hero: calories ---------- */
.hero__value { font-size: 52px; font-weight: 700; line-height: 1.05; letter-spacing: -0.01em; }
.hero__unit { font-size: 18px; font-weight: 600; color: var(--ink-2); margin-left: 4px; }
.hero__sub { margin: 4px 0 14px; color: var(--ink-2); }

/* ---------- meters ---------- */
.meter + .meter { margin-top: 16px; }
.meter__head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.meter__label { font-weight: 600; }
.meter__swatch { width: 10px; height: 10px; border-radius: 3px; background: var(--series); align-self: center; flex: none; }
.meter__value { margin-left: auto; color: var(--ink-2); }
.meter__value b { color: var(--ink); font-weight: 600; }
.meter__pct { min-width: 44px; text-align: right; font-weight: 600; }
.meter__track {
  position: relative; height: 12px; border-radius: 6px; overflow: hidden;
  background: color-mix(in srgb, var(--series) 18%, var(--card));
}
.meter__fill, .meter__over { position: absolute; top: 0; bottom: 0; border-radius: 0 4px 4px 0; }
.meter__fill { left: 0; background: var(--series); }
.meter__over { background: var(--critical); border-radius: 4px; }
.meter__tick { position: absolute; top: -2px; bottom: -2px; width: 2px; margin-left: -1px; background: var(--ink); opacity: 0.6; }
.meter__note { margin-top: 6px; font-size: 13px; color: var(--ink-2); }
.meter__note.over { color: var(--critical); font-weight: 600; }
.meter--kcal { --series: var(--series-kcal); }
.meter--protein { --series: var(--series-protein); }
.meter--fat { --series: var(--series-fat); }
.meter--carbs { --series: var(--series-carbs); }

/* ---------- calorie share: one stacked bar with a 2px surface gap between segments ---------- */
.share { display: flex; gap: 2px; height: 14px; }
.share__seg { min-width: 4px; background: var(--series); }
.share__seg:first-child { border-radius: 4px 0 0 4px; }
.share__seg:last-child { border-radius: 0 4px 4px 0; }
.share__seg:only-child { border-radius: 4px; }
.legend { list-style: none; margin: 12px 0 0; padding: 0; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.legend li { display: flex; flex-direction: column; gap: 2px; font-size: 13px; color: var(--ink-2); }
.legend .name { display: flex; align-items: center; gap: 6px; color: var(--ink); font-weight: 600; }
.legend .name::before { content: ""; width: 10px; height: 10px; border-radius: 3px; background: var(--series); }
.share__seg.protein, .legend .protein { --series: var(--series-protein); }
.share__seg.fat, .legend .fat { --series: var(--series-fat); }
.share__seg.carbs, .legend .carbs { --series: var(--series-carbs); }

/* ---------- insulin curve: one series (validated slot 7), recessive grid, threshold and "now" as hairlines ---------- */
.chart { position: relative; margin: 0 -4px; touch-action: pan-y; }
.chart svg { display: block; overflow: visible; }
.chart .grid { stroke: var(--hairline); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .area { fill: color-mix(in srgb, var(--series-insulin) 20%, transparent); }
.chart .line { fill: none; stroke: var(--series-insulin); stroke-width: 2; stroke-linejoin: round; }
.chart .threshold { stroke: var(--muted); stroke-width: 1; opacity: 0.7; }
.chart .now { stroke: var(--ink); stroke-width: 1; opacity: 0.35; }
.chart .meal-dot { fill: var(--series-insulin); stroke: var(--card); stroke-width: 2; }
.chart .cross line { stroke: var(--ink); stroke-width: 1; opacity: 0.4; }
.chart .cross circle { fill: var(--series-insulin); stroke: var(--card); stroke-width: 2; }
.tip {
  position: absolute; top: 0; padding: 6px 10px; border-radius: 10px; pointer-events: none; white-space: nowrap;
  background: var(--ink); color: var(--card); font-size: 12px; line-height: 1.35;
}
.tip span { opacity: 0.8; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.stat__label { font-size: 12px; color: var(--muted); }
.stat__value { font-size: 18px; font-weight: 600; display: flex; flex-direction: column; }
.stat__sub { font-size: 12px; font-weight: 400; color: var(--ink-2); }
.note { margin: 12px 0 0; font-size: 12px; color: var(--muted); }

/* ---------- meals ---------- */
.meals { list-style: none; margin: 0; padding: 0; }
.meal + .meal { border-top: 1px solid var(--hairline); }
.meal details > summary { list-style: none; display: grid; grid-template-columns: auto 1fr auto; gap: 2px 12px; padding: 10px 0; cursor: pointer; }
.meal summary::-webkit-details-marker { display: none; }
.meal__time { color: var(--muted); font-size: 13px; grid-row: span 2; padding-top: 2px; font-variant-numeric: tabular-nums; }
.meal__title { font-weight: 600; overflow-wrap: anywhere; }
.meal__kcal { font-weight: 600; white-space: nowrap; text-align: right; }
.meal__type { color: var(--ink-2); font-size: 13px; }
.meal__macros { color: var(--ink-2); font-size: 13px; white-space: nowrap; text-align: right; }
.items { list-style: none; margin: 0 0 10px; padding: 0 0 0 52px; font-size: 13px; color: var(--ink-2); }
.items li { display: flex; gap: 8px; padding: 3px 0; }
.items .n { flex: 1; color: var(--ink); overflow-wrap: anywhere; }
.items .v { white-space: nowrap; font-variant-numeric: tabular-nums; }

.empty, .hint { color: var(--ink-2); margin: 0; }
.hint { font-size: 14px; }
.error { text-align: center; padding: 32px 16px; color: var(--ink-2); }
.error button { margin-top: 12px; }
.footer { margin-top: 20px; text-align: center; font-size: 12px; color: var(--muted); }
