:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #10b981;
  --accent-ink: #ffffff;
  --danger: #ef4444;
  --p: #3b82f6;  /* protein */
  --c: #f59e0b;  /* carbs */
  --f: #ec4899;  /* fat */
  --usda: #2563eb;
  --ciqual: #7c3aed;
  --off: #ea580c;
  --custom: #0d9488;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.05);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f14;
    --card: #151b23;
    --text: #e5e7eb;
    --muted: #8b95a3;
    --line: #253041;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); min-height: 1.2em; }
h1, h2, h3 { margin: 0; }
h2 { font-size: 20px; }
h3 { font-size: 15px; }

main { max-width: 640px; margin: 0 auto; padding: calc(12px + env(safe-area-inset-top)) 14px 20px; }
.view > * + * { margin-top: 12px; }

.card { background: var(--card); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); border: 1px solid var(--line); }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }

.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 10px 14px; border-radius: 12px; font: inherit; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 6px 10px; font-size: 13px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.icon-btn { appearance: none; border: 0; background: transparent; font-size: 20px; padding: 6px 10px; cursor: pointer; color: var(--text); }

input, select {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 12px; width: 100%; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label.field { display: block; }
label.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }

/* Lock */
.lock { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px; background: var(--bg); z-index: 50; }
.lock-card { width: 100%; max-width: 320px; text-align: center; display: grid; gap: 12px; }
.logo { font-size: 48px; }

/* Tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  appearance: none; border: 0; background: none; color: var(--muted); font: inherit; font-size: 11px;
  padding: 8px 0 10px; display: grid; gap: 2px; justify-items: center; cursor: pointer;
}
.tabs button span { font-size: 20px; filter: grayscale(1); opacity: .7; }
.tabs button.active { color: var(--accent); font-weight: 600; }
.tabs button.active span { filter: none; opacity: 1; }

/* Today summary */
.daynav { display: flex; align-items: center; justify-content: space-between; }
.daynav h2 { font-size: 18px; text-align: center; }
.summary { display: grid; grid-template-columns: 120px 1fr; gap: 16px; align-items: center; }
.ring { position: relative; width: 120px; height: 120px; }
.ring svg { transform: rotate(-90deg); }
.ring .center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring .big { font-size: 24px; font-weight: 800; line-height: 1; }
.macro { display: grid; gap: 4px; }
.macro + .macro { margin-top: 10px; }
.macro .top { display: flex; justify-content: space-between; font-size: 13px; }
.bar { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 99px; }
.over { color: var(--danger); }

/* Meals */
.meal-head { display: flex; justify-content: space-between; align-items: center; }
.meal-head .kcal { color: var(--muted); font-size: 13px; }
.entry { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; }
.entry:first-of-type { border-top: 0; }
.entry .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .meta { font-size: 12px; color: var(--muted); }
.entry .kc { font-weight: 700; white-space: nowrap; }
.add-line { margin-top: 8px; }

/* Search */
.searchbar { display: flex; gap: 8px; position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 4px 0 8px; }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--text); border-radius: 99px;
  padding: 6px 12px; font: inherit; font-size: 13px; white-space: nowrap; cursor: pointer;
}
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.result { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; }
.result:first-child { border-top: 0; }
.result img { width: 40px; height: 40px; object-fit: cover; border-radius: 8px; background: var(--line); flex: none; }
.result .name { font-weight: 600; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.result .meta { font-size: 12px; color: var(--muted); }
.result .kc { text-align: right; font-weight: 700; white-space: nowrap; }
.result .kc small { display: block; font-weight: 400; color: var(--muted); font-size: 11px; }
.badge { display: inline-block; font-size: 10px; font-weight: 700; letter-spacing: .03em; padding: 2px 6px; border-radius: 6px; color: #fff; vertical-align: middle; }
.badge.usda { background: var(--usda); }
.badge.ciqual { background: var(--ciqual); }
.badge.off { background: var(--off); }
.badge.custom { background: var(--custom); }
.group-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.pdots { font-size: 12px; color: var(--muted); }
.pdots b { font-weight: 600; }
.pdots .p { color: var(--p); } .pdots .c { color: var(--c); } .pdots .f { color: var(--f); }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.7s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Sheet */
.sheet-wrap { position: fixed; inset: 0; z-index: 40; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 92vh; overflow-y: auto;
  background: var(--card); border-radius: 20px 20px 0 0; padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  max-width: 640px; margin: 0 auto; animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: .5; } }
.sheet-handle { width: 40px; height: 5px; border-radius: 99px; background: var(--line); margin: 4px auto 12px; cursor: pointer; }
.sheet-content > * + * { margin-top: 12px; }
.food-title { font-size: 18px; font-weight: 700; }
.kcal-hero { display: flex; align-items: baseline; gap: 6px; }
.kcal-hero .n { font-size: 40px; font-weight: 800; line-height: 1; }
.macros3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.macros3 > div { border-radius: 12px; padding: 10px; background: var(--bg); text-align: center; }
.macros3 b { display: block; font-size: 18px; }
.macros3 .p b { color: var(--p); } .macros3 .c b { color: var(--c); } .macros3 .f b { color: var(--f); }
table.nut { width: 100%; border-collapse: collapse; font-size: 14px; }
table.nut th, table.nut td { padding: 7px 4px; border-top: 1px solid var(--line); text-align: right; }
table.nut th:first-child, table.nut td:first-child { text-align: left; }
table.nut thead th { border-top: 0; color: var(--muted); font-weight: 500; font-size: 12px; }
.qty { display: grid; grid-template-columns: 110px 1fr; gap: 8px; }
.presets { display: flex; gap: 6px; flex-wrap: wrap; }

/* History */
.hbars { display: flex; align-items: flex-end; gap: 4px; height: 140px; position: relative; }
.hbars .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; cursor: pointer; }
.hbars .col i { display: block; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; }
.hbars .col.over i { background: var(--danger); }
.hbars .target { position: absolute; left: 0; right: 0; border-top: 2px dashed var(--muted); opacity: .5; pointer-events: none; }
.hlabels { display: flex; gap: 4px; font-size: 10px; color: var(--muted); }
.hlabels span { flex: 1; text-align: center; }
.hrow { display: grid; grid-template-columns: 1fr auto auto auto auto; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 14px; cursor: pointer; }
.hrow:first-child { border-top: 0; }

/* Scanner */
.scanner video { width: 100%; border-radius: 12px; background: #000; aspect-ratio: 4/3; object-fit: cover; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(84px + env(safe-area-inset-bottom));
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; z-index: 60; font-weight: 600; font-size: 14px;
  max-width: 90vw;
}
.progress { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }
