@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1410;
    --bg-elev: #262019;
    --border: #3a2f24;
    --text: #f2ece3;
    --text-dim: #b7a99757;
    --text-muted: #a99a86;
    --accent: #e6602a;
    --accent-ink: #fff3ec;
    --good: #6ea36a;
    --warn: #d9a441;
    --bad: #c96257;
  }
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #faf6f0;
    --bg-elev: #ffffff;
    --border: #e6dccd;
    --text: #241c14;
    --text-dim: #6b5e4e;
    --text-muted: #83725d;
    --accent: #d1531f;
    --accent-ink: #ffffff;
    --good: #3f7a3c;
    --warn: #9c7317;
    --bad: #a5382c;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1410; --bg-elev: #262019; --border: #3a2f24; --text: #f2ece3;
  --text-dim: #cbbcaa; --text-muted: #a99a86; --accent: #e6602a; --accent-ink: #fff3ec;
  --good: #6ea36a; --warn: #d9a441; --bad: #c96257;
}
:root[data-theme="light"] {
  --bg: #faf6f0; --bg-elev: #ffffff; --border: #e6dccd; --text: #241c14;
  --text-dim: #6b5e4e; --text-muted: #83725d; --accent: #d1531f; --accent-ink: #ffffff;
  --good: #3f7a3c; --warn: #9c7317; --bad: #a5382c;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}

#app { max-width: 480px; margin: 0 auto; padding: 16px 16px 96px; display: flex; flex-direction: column; gap: 14px; }

h1, h2, h3 { text-wrap: balance; margin: 0; }
h1 { font-size: 1.3rem; font-weight: 700; }
h2 { font-size: 1rem; font-weight: 700; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; font-size: 0.78rem; }
p { margin: 0; }

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 4px 2px 0;
}
.topbar .level-badge {
  background: var(--accent); color: var(--accent-ink);
  border-radius: 999px; padding: 4px 12px; font-weight: 700; font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}
.streak { display: flex; align-items: center; gap: 4px; font-weight: 700; font-variant-numeric: tabular-nums; }

.kcal-hero { text-align: center; padding: 22px 16px; }
.kcal-hero .remaining { font-size: 2.6rem; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.kcal-hero .remaining.over { color: var(--bad); }
.kcal-hero .label { color: var(--text-muted); font-size: 0.85rem; }
.meter { height: 10px; border-radius: 999px; background: var(--border); overflow: hidden; margin-top: 10px; }
.meter > div { height: 100%; background: var(--accent); transition: width 0.3s ease; }
.meter > div.over { background: var(--bad); }
.kcal-breakdown { display: flex; justify-content: space-around; font-size: 0.78rem; color: var(--text-muted); font-variant-numeric: tabular-nums; }

.tier-row { display: flex; align-items: center; gap: 10px; }
.tier-ladder { display: flex; gap: 4px; flex: 1; }
.tier-ladder .seg { flex: 1; height: 8px; border-radius: 4px; background: var(--border); }
.tier-ladder .seg.done { background: var(--good); }
.tier-ladder .seg.next { background: var(--accent); }
.tier-labels { display: flex; justify-content: space-between; font-size: 0.72rem; color: var(--text-muted); }

/* Small, low-emphasis manual/ad-hoc entry points — the primary flow is
   notification-driven, this is just the fallback for logging off-schedule. */
.manual-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; padding: 4px 0 8px; }
.manual-actions button {
  background: transparent; border: 1px solid var(--border); border-radius: 999px;
  padding: 6px 12px; font-size: 0.75rem; color: var(--text-muted); cursor: pointer;
}
.manual-actions button:active { transform: scale(0.96); }

.today-plan-list { display: flex; flex-direction: column; gap: 6px; }
.plan-item {
  display: flex; align-items: center; gap: 8px; padding: 6px 0; font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
}
.plan-item:last-child { border-bottom: none; }
.plan-item .check { flex-shrink: 0; width: 20px; text-align: center; }
.plan-item.done { color: var(--text-muted); text-decoration: line-through; }
.plan-item.pending button {
  margin-left: auto; background: var(--accent); color: var(--accent-ink); border: none;
  border-radius: 999px; padding: 4px 12px; font-size: 0.78rem; cursor: pointer; text-decoration: none;
}

.menu-item-list { display: flex; flex-direction: column; gap: 4px; }
.menu-category-label { font-size: 0.72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; margin-top: 6px; }
.menu-item {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 12px; cursor: pointer; text-align: left; width: 100%; font-size: 0.88rem; color: var(--text);
}
.menu-item:active { transform: scale(0.98); }
.menu-item .price { color: var(--text-muted); font-size: 0.78rem; white-space: nowrap; }

.update-banner {
  position: fixed; bottom: 0; left: 0; right: 0; background: var(--accent); color: var(--accent-ink);
  padding: 12px 16px; display: flex; align-items: center; justify-content: center; gap: 12px;
  font-size: 0.88rem; font-weight: 600; z-index: 200;
}
.update-banner button {
  background: var(--accent-ink); color: var(--accent); border: none; border-radius: 999px;
  padding: 6px 14px; font-weight: 700; cursor: pointer; font-size: 0.85rem;
}

button.primary {
  background: var(--accent); color: var(--accent-ink); border: none; border-radius: 12px;
  padding: 12px 16px; font-weight: 700; font-size: 0.95rem; cursor: pointer;
}
button.secondary {
  background: transparent; color: var(--text); border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 14px; font-weight: 600; font-size: 0.88rem; cursor: pointer;
}
button:disabled { opacity: 0.5; cursor: default; }

input, textarea, select {
  background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; font-size: 0.95rem; width: 100%; font-family: inherit;
}
textarea { resize: vertical; min-height: 80px; }
label { font-size: 0.8rem; color: var(--text-muted); display: block; margin-bottom: 4px; }
.field { display: flex; flex-direction: column; gap: 2px; }
.field-row { display: flex; gap: 8px; }
.field-row .field { flex: 1; }

.food-log-item { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--border); }
.food-log-item:last-child { border-bottom: none; }
.food-log-item img { width: 48px; height: 48px; object-fit: cover; border-radius: 8px; flex-shrink: 0; }
.food-log-item .meta { flex: 1; min-width: 0; }
.food-log-item .meal-type { font-size: 0.7rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }
.food-log-item .kcal { font-weight: 700; font-variant-numeric: tabular-nums; }
.food-log-item .comment { font-size: 0.85rem; color: var(--text-dim); font-style: italic; }
.food-log-item .del { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 1rem; }

.badge-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 8px; }
.badge {
  border: 1px solid var(--border); border-radius: 12px; padding: 8px; text-align: center;
  font-size: 0.7rem; opacity: 0.35;
}
.badge.earned { opacity: 1; border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--bg-elev)); }
.badge .icon { font-size: 1.3rem; }

.challenge-banner { display: flex; align-items: center; gap: 10px; }
.challenge-banner .icon { font-size: 1.6rem; }

canvas#weightChart { width: 100%; height: 160px; }

.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: var(--accent-ink); padding: 12px 20px; border-radius: 999px;
  font-weight: 600; box-shadow: 0 6px 20px rgba(0,0,0,0.3); z-index: 100; max-width: 90vw; text-align: center;
}

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); display: flex; align-items: flex-end;
  justify-content: center; z-index: 50;
}
.modal {
  background: var(--bg-elev); border-radius: 20px 20px 0 0; padding: 20px; width: 100%; max-width: 480px;
  display: flex; flex-direction: column; gap: 12px; max-height: 88vh; overflow-y: auto;
}
.modal .close { align-self: flex-end; background: none; border: none; color: var(--text-muted); font-size: 1.2rem; cursor: pointer; }

.meal-type-picker { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.meal-type-picker button {
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 8px; font-size: 0.8rem;
  color: var(--text); cursor: pointer;
}
.meal-type-picker button.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 15%, var(--bg)); }

.center-screen { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-card { max-width: 360px; width: 100%; display: flex; flex-direction: column; gap: 12px; }

.setup-steps { margin: 0; padding-left: 20px; font-size: 0.85rem; color: var(--text-dim); display: flex; flex-direction: column; gap: 8px; }
.setup-steps ul { margin: 4px 0 0; padding-left: 18px; }
.setup-steps code {
  background: var(--bg); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px;
  font-size: 0.82em; word-break: break-all;
}

.error-text { color: var(--bad); font-size: 0.85rem; }
.hint { color: var(--text-muted); font-size: 0.78rem; }
.hidden { display: none !important; }
