/* ===== Palette ===== */
.app-shell {
  color-scheme: light;
  --page-plane:     #f2f1ee;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --border:         rgba(11,11,11,0.10);
  --glass-bg:       rgba(255,255,255,0.55);
  --glass-border:   rgba(255,255,255,0.6);
  --track:          rgba(11,11,11,0.08);

  --series-1: #2a78d6; /* calories - blue */
  --series-2: #eb6834; /* carbs - orange */
  --series-3: #1baf7a; /* protein - aqua */
  --series-4: #eda100; /* fat - yellow */
  --status-critical: #d03b3b;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .app-shell {
    color-scheme: dark;
    --page-plane:     #0d0d10;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #8b8a95;
    --border:         rgba(255,255,255,0.12);
    --glass-bg:       rgba(28,28,34,0.55);
    --glass-border:   rgba(255,255,255,0.10);
    --track:          rgba(255,255,255,0.10);

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --status-critical: #e66767;
  }
}

/* ==== Theme packs — each is a complete, fixed vibe (not light/dark-aware) ==== */

/* --- Stark: JARVIS HUD — dark, blue/cyan arc-reactor glow, angular --- */
[data-vibe="stark"].app-shell {
  color-scheme: dark;
  --page-plane:     #04070a;
  --text-primary:   #eaf6ff;
  --text-secondary: #7fa8bf;
  --text-muted:     #4d6b7a;
  --border:         rgba(0,217,255,0.25);
  --glass-bg:       rgba(8,16,22,0.75);
  --glass-border:   rgba(0,217,255,0.3);
  --track:          rgba(0,217,255,0.10);

  --series-1: #29d1ff; /* calories - arc cyan (hero ring) */
  --series-2: #0074e8; /* carbs - deep blue */
  --series-3: #ffb800; /* protein - HUD amber accent */
  --series-4: #c7cdd6; /* fat - silver */
  --status-critical: #ff5a5a;
  font-family: "SF Mono", "Roboto Mono", Consolas, monospace;
}
[data-vibe="stark"] .hero-card, [data-vibe="stark"] .glass {
  border-radius: 10px;
  box-shadow: 0 0 28px rgba(0,217,255,0.15), inset 0 0 0 1px rgba(0,217,255,0.2);
}
[data-vibe="stark"] .ring-fill { filter: drop-shadow(0 0 8px rgba(41,209,255,0.85)); }
[data-vibe="stark"] .mring-fill { filter: drop-shadow(0 0 5px rgba(41,209,255,0.6)); }
[data-vibe="stark"] .fab { background: linear-gradient(145deg, #29d1ff, #0057a8); box-shadow: 0 0 32px rgba(41,209,255,0.65); }
[data-vibe="stark"] .primary-btn, [data-vibe="stark"] .tab-btn.active { background: #0088e0; }
[data-vibe="stark"] .bg-glow {
  background:
    radial-gradient(38vh 38vh at 15% 8%, rgba(41,209,255,0.28), transparent 70%),
    radial-gradient(34vh 34vh at 90% 18%, rgba(0,116,232,0.22), transparent 70%),
    radial-gradient(40vh 40vh at 20% 85%, rgba(255,184,0,0.12), transparent 70%),
    radial-gradient(36vh 36vh at 95% 90%, rgba(199,205,214,0.12), transparent 70%);
}
/* Faint circuit-board grid — the "techie stuff" behind everything */
[data-vibe="stark"] .bg-glow::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    repeating-linear-gradient(0deg, rgba(41,209,255,0.07) 0px, rgba(41,209,255,0.07) 1px, transparent 1px, transparent 42px),
    repeating-linear-gradient(90deg, rgba(41,209,255,0.07) 0px, rgba(41,209,255,0.07) 1px, transparent 1px, transparent 42px);
}

/* --- Mofusand: pancake-cat illustrations — warm cream, soft and round --- */
[data-vibe="mofusand"].app-shell {
  color-scheme: light;
  --page-plane:     #fffaf3;
  --text-primary:   #6b4a3a;
  --text-secondary: #a9846d;
  --text-muted:     #cbb09c;
  --border:         rgba(201,137,91,0.2);
  --glass-bg:       rgba(255,255,255,0.75);
  --glass-border:   rgba(255,255,255,0.9);
  --track:          rgba(201,137,91,0.12);

  --series-1: #ff9baa; /* calories - donut pink */
  --series-2: #f4c15c; /* carbs - butter gold */
  --series-3: #c98a5b; /* protein - pancake caramel */
  --series-4: #e8637a; /* fat - berry red */
  --status-critical: #e8637a;
}
[data-vibe="mofusand"] .hero-card, [data-vibe="mofusand"] .glass,
[data-vibe="mofusand"] .sheet, [data-vibe="mofusand"] .tab-switch { border-radius: 28px; }
[data-vibe="mofusand"] .fab { background: linear-gradient(145deg, #ff9baa, #e87a9a); box-shadow: 0 8px 24px rgba(255,155,170,0.5); }
[data-vibe="mofusand"] .primary-btn, [data-vibe="mofusand"] .tab-btn.active { background: #ff9baa; color: #6b4a3a; }
[data-vibe="mofusand"] .bg-glow {
  background:
    radial-gradient(38vh 38vh at 15% 8%, rgba(255,155,170,0.3), transparent 70%),
    radial-gradient(34vh 34vh at 90% 18%, rgba(244,193,92,0.3), transparent 70%),
    radial-gradient(40vh 40vh at 20% 85%, rgba(201,138,91,0.22), transparent 70%),
    radial-gradient(36vh 36vh at 95% 90%, rgba(232,99,122,0.2), transparent 70%);
}

/* --- Theme picker (Settings sheet) --- */
.theme-picker { display: flex; gap: 10px; margin: 4px 0 12px; }
.theme-swatch {
  flex: 1;
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 10px 6px;
  background: var(--page-plane);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.theme-swatch.active { border-color: var(--series-1); color: var(--text-primary); }

/* ---- Inline icon sprite usage (replaces emoji everywhere) ---- */
.inline-icon {
  width: 1.1em;
  height: 1.1em;
  display: inline-block;
  vertical-align: -0.2em;
  fill: currentColor;
  flex-shrink: 0;
}
.streak .inline-icon { color: var(--status-critical); width: 16px; height: 16px; margin-right: 2px; }
.fab-icon { width: 26px; height: 26px; color: #fff; }
.swatch-icon { width: 22px; height: 22px; margin-bottom: 2px; }
.photo-preview .inline-icon { width: 18px; height: 18px; margin-right: 4px; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--page-plane);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

body.app-shell {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
  padding-bottom: 110px; /* clears bottom nav */
}

h1, h2 { margin: 0; }
h2 { font-size: 14px; font-weight: 600; color: var(--text-secondary); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.04em; }

/* ---- Decorative liquid-glass background blobs ---- */
.bg-glow {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(38vh 38vh at 15% 8%, color-mix(in srgb, var(--series-1) 35%, transparent), transparent 70%),
    radial-gradient(34vh 34vh at 90% 18%, color-mix(in srgb, var(--series-4) 30%, transparent), transparent 70%),
    radial-gradient(40vh 40vh at 20% 85%, color-mix(in srgb, var(--series-3) 28%, transparent), transparent 70%),
    radial-gradient(36vh 36vh at 95% 90%, color-mix(in srgb, var(--series-2) 25%, transparent), transparent 70%);
  filter: blur(10px);
}

/* Theme decoration layer — both sets always in the DOM, CSS shows only the
   active theme's set (kept out of .bg-glow since that layer is blurred). */
.bg-decor {
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  pointer-events: none;
}
.deco { position: absolute; display: none; }
.deco svg { width: 100%; height: 100%; display: block; }
[data-vibe="mofusand"] .deco-mofusand { display: block; opacity: 0.28; color: #c9895b; }
[data-vibe="stark"] .deco-stark {
  display: block;
  opacity: 0.4;
  font-family: "SF Mono", "Roboto Mono", Consolas, monospace;
  font-size: 10px;
  letter-spacing: 1px;
  color: #29d1ff;
  text-shadow: 0 0 6px rgba(41,209,255,0.6);
}

/* ---- Glass surface ---- */
.glass {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(20px) saturate(160%);
  -webkit-backdrop-filter: blur(20px) saturate(160%);
}

/* ---- Top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 16px 16px 0;
}
.icon-btn {
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  font-size: 18px;
  width: 38px;
  height: 38px;
  color: var(--text-primary);
  cursor: pointer;
  flex-shrink: 0;
}
.date-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  justify-content: center;
}
.date-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  min-width: 84px;
  text-align: center;
}
.streak {
  border-radius: 20px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

/* ---- Tab switch ---- */
.tab-switch {
  margin: 16px 16px 0;
  border-radius: 16px;
  padding: 4px;
  display: flex;
  gap: 4px;
}
.tab-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 600;
  padding: 10px;
  border-radius: 12px;
  cursor: pointer;
}
.tab-btn.active {
  background: var(--series-1);
  color: #fff;
}
.hidden { display: none !important; }

/* ---- Dashboard / Trends ---- */
.period-switch {
  margin: 16px 16px 0;
  border-radius: 16px;
  padding: 4px;
  display: flex;
  gap: 4px;
}
.period-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
  padding: 8px;
  border-radius: 12px;
  cursor: pointer;
}
.period-btn.active { background: var(--series-1); color: #fff; }

.range-nav {
  margin: 10px 16px 0;
  border-radius: 16px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}
.range-nav .date-label { min-width: auto; font-size: 13px; }
.range-nav .icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  width: 30px;
  height: 30px;
  font-size: 15px;
}
.range-nav .icon-btn:disabled { opacity: 0.35; cursor: default; }

.bar-chart-bar { rx: 4; }
.bar-chart-label { fill: var(--text-muted); font-size: 6px; font-family: system-ui, sans-serif; }
.bar-chart-value { fill: var(--text-primary); font-size: 7px; font-weight: 600; font-family: system-ui, sans-serif; }
.bar-chart-baseline { stroke: var(--border); stroke-width: 1; }

/* ---- Hero card ---- */
main { padding: 16px; display: flex; flex-direction: column; gap: 20px; }

.hero-card {
  border-radius: 28px;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.ring-wrap { position: relative; width: 180px; height: 180px; }
.ring-svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-track, .mring-track {
  fill: none;
  stroke: var(--track);
}
.ring-track { stroke-width: 12; }
.mring-track { stroke-width: 6; }
.ring-fill, .mring-fill {
  fill: none;
  stroke-linecap: round;
  transition: stroke-dashoffset 0.4s ease, stroke 0.2s ease;
}
.ring-fill { stroke-width: 12; stroke: var(--series-1); }
.mring-fill { stroke-width: 6; }
.macro-ring[data-macro="carbs"] .mring-fill { stroke: var(--series-2); }
.macro-ring[data-macro="protein"] .mring-fill { stroke: var(--series-3); }
.macro-ring[data-macro="fat"] .mring-fill { stroke: var(--series-4); }
.ring-fill.over, .mring-fill.over { stroke: var(--status-critical) !important; }

.ring-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.ring-value {
  font-size: 40px;
  font-weight: 700;
  font-variant-numeric: proportional-nums;
  line-height: 1;
}
.ring-label { font-size: 12px; color: var(--text-secondary); margin-top: 6px; }

.macro-rings {
  display: flex;
  justify-content: space-between;
  width: 100%;
  gap: 8px;
}
.macro-ring {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  flex: 1;
}
.macro-ring svg { width: 56px; height: 56px; transform: rotate(-90deg); }
.macro-ring-label {
  font-size: 11px;
  color: var(--text-secondary);
  text-align: center;
  line-height: 1.4;
}
.macro-ring-label span { color: var(--text-primary); font-weight: 600; }

/* ---- Auth gate ---- */
.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-card {
  width: 100%;
  max-width: 340px;
  border-radius: 24px;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.auth-card h1 { font-size: 22px; }
.auth-sub { font-size: 13px; color: var(--text-secondary); margin: 0 0 8px; }
.auth-error {
  font-size: 13px;
  color: var(--status-critical);
  min-height: 18px;
  margin: 0;
}
.logged-in-as {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  margin: 12px 0 0;
}
.logged-in-as span { color: var(--text-secondary); font-weight: 600; }

/* ---- Weight tab ---- */
.weight-hero { text-align: center; }
.starting-weight-label { font-size: 12px; color: var(--text-muted); margin-top: 6px; }
.activity-field { width: 100%; margin-top: 4px; }
.suggestion-box {
  width: 100%;
  border-radius: 16px;
  padding: 14px;
  background: var(--track);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
}
.suggestion-label { font-size: 12px; color: var(--text-secondary); margin: 0; }
.suggestion-value { font-size: 22px; font-weight: 700; }
#applySuggestion, #applyAdaptive { width: auto; padding: 8px 16px; font-size: 13px; }
.adaptive-box { background: color-mix(in srgb, var(--series-1) 12%, var(--track)); border: 1px solid var(--series-1); }
.adaptive-reasoning { font-size: 12px; color: var(--text-secondary); margin: 0; line-height: 1.4; }

/* ---- Remaining-today summary (Nutrition hero) ---- */
.remaining-label { font-size: 13px; font-weight: 600; color: var(--text-secondary); text-align: center; margin: 0; }
.remaining-label.over-budget { color: var(--status-critical); }
.field-hint.error { color: var(--status-critical); font-weight: 600; }
.primary-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---- Progression card ---- */
.progression-card {
  border-radius: 24px;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--page-plane);
  color: var(--text-primary);
  font-size: 15px;
  appearance: none;
}
.progression-chart { min-height: 120px; }
.progression-chart svg { width: 100%; height: auto; display: block; }
.progression-line {
  fill: none;
  stroke: var(--series-1);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.progression-dot { fill: var(--series-1); }
.progression-axis-label { fill: var(--text-muted); font-size: 6px; font-family: system-ui, sans-serif; }
.progression-value-label { fill: var(--text-primary); font-size: 7px; font-weight: 600; font-family: system-ui, sans-serif; }
.pct-hint { font-size: 12px; color: var(--text-secondary); margin: -4px 0 0; min-height: 16px; }
.name-with-action { display: flex; gap: 8px; align-items: stretch; }
.name-with-action input { flex: 1; min-width: 0; }
.small-btn { flex: 0 0 auto; padding: 10px 12px; font-size: 13px; white-space: nowrap; }
.field-hint { font-size: 12px; color: var(--text-muted); margin: -2px 0 4px; }
.field-hint-inline { font-weight: 400; color: var(--text-muted); font-size: 11px; }
#mealCalories[readonly], #targetCarbsInput[readonly] { opacity: 0.75; cursor: default; }
#mealList .meal-item { cursor: pointer; }

/* ---- Meal list ---- */
.meal-items { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.meal-item {
  display: flex;
  align-items: center;
  gap: 12px;
  border-radius: 16px;
  padding: 10px 12px;
}
.meal-thumb {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--track);
}
.workout-thumb {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}
.meal-info { flex: 1; min-width: 0; }
.meal-name { font-size: 14px; font-weight: 600; }
.meal-macros { font-size: 12px; color: var(--text-secondary); }
.entry-notes { font-size: 12px; color: var(--text-muted); font-style: italic; margin-top: 2px; }
.delete-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
}
.empty-state {
  font-size: 13px;
  color: var(--text-muted);
  text-align: center;
  padding: 16px 0;
}

/* ---- Bottom nav + FAB ---- */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  height: 76px;
  border-radius: 24px 24px 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.nav-hint {
  font-size: 12px;
  color: var(--text-muted);
}
.fab {
  position: absolute;
  top: -28px;
  left: 50%;
  transform: translateX(-50%);
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  font-size: 26px;
  color: #fff;
  background: linear-gradient(145deg, var(--series-1), color-mix(in srgb, var(--series-1) 70%, black));
  box-shadow: 0 8px 24px color-mix(in srgb, var(--series-1) 50%, transparent);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---- Bottom sheets (add meal / settings) ---- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 20;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }
.sheet {
  width: 100%;
  max-width: 480px;
  border-radius: 28px 28px 0 0;
  padding: 12px 20px 28px;
  transform: translateY(100%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 88vh;
  overflow-y: auto;
}
.sheet-backdrop.open .sheet { transform: translateY(0); }
.sheet-handle {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 4px auto 4px;
}
.sheet h2 { text-transform: none; letter-spacing: 0; font-size: 17px; color: var(--text-primary); }

#mealForm { display: flex; flex-direction: column; gap: 12px; }
.photo-label { align-self: flex-start; width: 100%; }
.photo-preview {
  width: 100%;
  /* Fixed, not min — reserving the full height up front means adding a
     photo doesn't grow the box and reflow everything below it (macro
     fields, buttons) inside an already-expensive backdrop-filter sheet.
     That reflow-while-blurring was the real cause of the reported flicker,
     not image file size — the earlier compression fix was real (protects
     against the 1MB Firestore limit and slow decode) but didn't touch this. */
  height: 160px;
  border: 1px dashed var(--border);
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
  overflow: hidden;
  /* Isolate this box's repaint from the rest of the blurred sheet so
     swapping its contents doesn't force a wider backdrop-filter recompute. */
  contain: layout paint;
}
.photo-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 14px;
}
input[type="text"], input[type="number"] {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--page-plane);
  color: var(--text-primary);
  font-size: 15px;
}
.macro-inputs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.notes-input {
  width: 100%;
  min-height: 60px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--page-plane);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; color: var(--text-secondary); }

.sheet-actions { display: flex; gap: 10px; margin-top: 4px; }
.primary-btn, .ghost-btn { flex: 1; border-radius: 14px; padding: 14px; font-size: 15px; font-weight: 600; cursor: pointer; }
.primary-btn { background: var(--series-1); color: #fff; border: none; }
.ghost-btn { background: transparent; color: var(--text-secondary); border: 1px solid var(--border); }
