:root {
  --bg: #f8fafc; --card: #fff; --text: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --accent: #0f766e; --accent-ink: #fff; --danger: #b91c1c;
  /* Nutrient colors: validated categorical order (calories, protein, carbs, fat, fiber, calcium, vitamin K). */
  --k: #257de0; --p: #f47230; --c: #00b894; --f: #f2a600; --fi: #ee7fac; --ca: #008a17; --vk: #6a4ce2;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0b1220; --card: #111a2b; --text: #e2e8f0; --muted: #94a3b8; --line: #1e293b;
          --accent: #14b8a6; --accent-ink: #042f2e; 
          --k: #338ced; --p: #e76624; --c: #00a888; --f: #d28e05; --fi: #e24d8b; --ca: #0aac23; --vk: #9887ef; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); padding-bottom: env(safe-area-inset-bottom); }
main { max-width: 560px; margin: 0 auto; padding: 0 12px 40px; }
.top { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 4px; max-width: 560px; margin: 0 auto;
       padding: calc(8px + env(safe-area-inset-top)) 12px 8px; background: var(--bg); }
.top .day { flex: 1; font-size: 1.1rem; font-weight: 600; background: none; border: 0; color: inherit; padding: 8px; }
.icon { width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: inherit; font-size: 1.3rem; }
.gear { font-size: 1.1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px; margin: 10px 0; }
button { font: inherit; cursor: pointer; }
button.primary { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 10px; padding: 12px 16px; font-weight: 600; }
button.small { padding: 8px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--card); color: inherit; }
button.primary.small { border: 0; background: var(--accent); color: var(--accent-ink); }
button.danger { color: var(--danger); }
button:disabled { opacity: .6; cursor: progress; }
input, textarea { font: inherit; width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: inherit; }
.muted { color: var(--muted); }
.status { margin: 8px 0 0; min-height: 1.2em; font-size: .9rem; color: var(--muted); }
.status.error { color: var(--danger); }
.row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; }

/* Summary */
.summary .kcal { display: flex; align-items: baseline; gap: 6px; }
.summary .kcal b { font-size: 2rem; }
/* One grid for all rows so the tracks line up; each .bar contributes its three cells. */
.bars { display: grid; grid-template-columns: 64px minmax(60px, 1fr) auto; align-items: center; gap: 8px; margin-top: 10px; font-size: .9rem; }
.bar { display: contents; }
.track { position: relative; height: 10px; border-radius: 6px; background: var(--line); overflow: hidden; }
/* Goal range: shaded behind the fill, with tick marks at the low and high ends drawn over it. */
.track .band { position: absolute; top: 0; bottom: 0; background: color-mix(in srgb, var(--text) 16%, transparent); }
.track .tick { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: var(--text); opacity: .55; z-index: 1; }
.track .fill { position: relative; }
.fill { height: 100%; border-radius: 6px; }
.fill.k { background: var(--k); } .fill.p { background: var(--p); } .fill.c { background: var(--c); } .fill.f { background: var(--f); }
.fill.fi { background: var(--fi); } .fill.ca { background: var(--ca); } .fill.vk { background: var(--vk); }
.fill.over { opacity: .65; }

/* Log form */
.log form { display: grid; gap: 10px; }
.photo-pick { display: grid; place-items: center; min-height: 120px; border: 2px dashed var(--line); border-radius: 12px; cursor: pointer; overflow: hidden; color: var(--muted); }
.photo-pick img { width: 100%; max-height: 260px; object-fit: cover; }
.when summary { color: var(--muted); font-size: .9rem; cursor: pointer; }
.when input { margin-top: 6px; }

/* Meals */
.meal { padding: 0; overflow: hidden; }
.meal-head { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px; background: none; border: 0; color: inherit; text-align: left; }
.bar span:last-child { text-align: right; white-space: nowrap; }
.meal-main { flex: 1; min-width: 0; }
.meal-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meal-sub { font-size: .85rem; color: var(--muted); }
.meal-kcal { font-weight: 700; white-space: nowrap; }
.meal-body { padding: 0 12px 12px; }
.badge { font-size: .75rem; padding: 1px 6px; border-radius: 6px; border: 1px solid var(--line); margin-left: 4px; }
.items { width: 100%; border-collapse: collapse; font-size: .85rem; margin: 6px 0 10px; }
.items th, .items td { padding: 4px; border-bottom: 1px solid var(--line); text-align: right; }
.items th:first-child, .items td:first-child { text-align: left; }
.items td small { color: var(--muted); display: block; }
.revise { display: flex; gap: 8px; }
.manual { margin-top: 10px; }
.manual summary { color: var(--muted); font-size: .9rem; cursor: pointer; }
.edit { display: grid; gap: 8px; margin-top: 8px; }
.edit .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 8px; }
.edit label, dialog label { font-size: .85rem; color: var(--muted); display: grid; gap: 4px; }
.empty-day { text-align: center; color: var(--muted); padding: 20px; }

dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--card); color: var(--text); width: min(94vw, 440px); }
dialog form { display: grid; gap: 10px; }
dialog h2 { margin: 0 0 4px; font-size: 1.1rem; }
dialog button { padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: inherit; }

/* View switcher (second header row) */
.top { flex-wrap: wrap; }
.views { display: flex; gap: 6px; width: 100%; }
.views button { flex: 1; }
.views button[aria-pressed="true"], .ranges button[aria-pressed="true"], .seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* Trends */
.ranges { display: flex; gap: 8px; margin-top: 4px; }
.chart-card h3 { margin: 0; font-size: 1rem; }
.chart-card .stat { font-size: .85rem; color: var(--muted); margin: 2px 0 8px; }
.chart-box { position: relative; height: 180px; }
.table-view summary { cursor: pointer; color: var(--muted); }
.table-wrap { overflow-x: auto; }

/* Settings */
.profile { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin: 0; }
.profile legend { font-size: .85rem; color: var(--muted); padding: 0 4px; }
select { font: inherit; width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: inherit; }
.goal-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.goal-table th { text-align: left; font-weight: 500; color: var(--muted); font-size: .8rem; padding: 2px 4px; }
.goal-table td { padding: 3px 4px; vertical-align: top; }
.goal-table td:first-child { width: 42%; }
.goal-table input { padding: 6px 8px; }
.goal-table .basis { display: block; font-size: .75rem; color: var(--muted); }

/* Foods */
.food-controls { display: grid; gap: 10px; }
.food-controls label { font-size: .85rem; color: var(--muted); display: grid; gap: 4px; }
.seg { display: flex; gap: 6px; }
.seg button { flex: 1; }
.food-title { margin: 0 0 8px; font-size: 1rem; }
.food-match b { font-size: 1.4rem; }
/* Same three-column grid as the summary bars: label, track, value. */
.food-rows { display: grid; grid-template-columns: minmax(80px, 34%) minmax(50px, 1fr) auto; align-items: center; gap: 8px; font-size: .9rem; }
.food-rows .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.food-rows .name small { display: block; color: var(--muted); font-size: .75rem; }
.food-rows .val { text-align: right; white-space: nowrap; }
.food-rows .val small { color: var(--muted); }
.food-note { font-size: .85rem; margin: 10px 0 0; }
.food-rows .name small { overflow: hidden; text-overflow: ellipsis; }
.food-controls select { color: var(--text); }
