:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #1d2521;
  --muted: #66716b;
  --line: #e1e6e3;
  --accent: #2f7d5b;
  --accent-soft: #e3f1ea;
  --warn: #c0612b;
  --protein: #3d6fd1;
  --carbs: #d1a23d;
  --fat: #b3527e;
  --fiber: #5c9a3a;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614;
    --card: #1c221f;
    --text: #e6ece9;
    --muted: #96a29b;
    --line: #2c3530;
    --accent: #5fbf92;
    --accent-soft: #20332a;
    --warn: #e58a55;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
main { max-width: 640px; margin: 0 auto; padding: 12px 16px; }
h2 { font-size: 1.1rem; margin: 0 0 10px; }
h3 { margin: 0 0 4px; font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; gap: 8px; justify-content: center; align-items: center;
  padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--line);
}
.topbar input { width: auto; }

.tab { display: none; }
.tab.active { display: block; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px; margin-bottom: 12px;
}

label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 10px; flex: 1; }
input, select {
  display: block; width: 100%; margin-top: 4px; padding: 10px;
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px;
}
.row { display: flex; gap: 8px; align-items: flex-end; }
.row > * { flex: 1; min-width: 0; }
.row > button { flex: 0 0 auto; }

button {
  font: inherit; padding: 10px 14px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
}
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.wide { width: 100%; margin-top: 4px; }
button.link { background: none; border: none; color: var(--accent); padding: 10px 0; text-decoration: underline; }
.icon-btn { padding: 6px 12px; font-size: 1.2rem; line-height: 1; }

/* Summary */
.kcal-big { display: flex; justify-content: space-between; align-items: baseline; }
.kcal-big strong { font-size: 1.8rem; }
.bar { height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; margin: 4px 0 10px; }
.bar > div { height: 100%; background: var(--accent); border-radius: 4px; }
.bar.over > div { background: var(--warn); }
.macros { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px 14px; }
.macro-label { display: flex; justify-content: space-between; font-size: .85rem; }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list li {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.list li:last-child { border-bottom: none; }
.list .grow { flex: 1; min-width: 0; }
.list .name { overflow: hidden; text-overflow: ellipsis; }
.del { border: none; background: none; color: var(--muted); padding: 4px 8px; }
.meal-head { display: flex; justify-content: space-between; }

.result { cursor: pointer; }
.result:hover { background: var(--accent-soft); }
.section-label { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 14px 0 2px; }
.badge { font-size: .7rem; padding: 1px 6px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chips button { padding: 6px 10px; font-size: .85rem; }
.chips button.active { background: var(--accent-soft); border-color: var(--accent); }

details summary { cursor: pointer; font-weight: 600; }
details pre { white-space: pre-wrap; font: inherit; margin: 6px 0; }
.reason { font-size: .85rem; color: var(--accent); }
.tip { padding-left: 12px; border-left: 3px solid var(--accent); margin: 8px 0; }

/* Tab bar */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  flex: 1; border: none; border-radius: 0; background: none; padding: 8px 0 10px;
  font-size: .72rem; color: var(--muted); display: flex; flex-direction: column; align-items: center;
}
.tabbar button span { font-size: 1.3rem; }
.tabbar button.active { color: var(--accent); font-weight: 600; }

/* Dialog */
dialog {
  width: min(600px, 100vw); max-height: 92vh; margin: auto auto 0; padding: 16px;
  border: none; border-radius: 16px 16px 0 0; background: var(--card); color: var(--text);
}
@media (min-width: 640px) { dialog { margin: auto; border-radius: 16px; } }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
#food-query { margin-bottom: 8px; }
#scan-video { width: 100%; border-radius: 10px; margin-top: 8px; }

#toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px;
  opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 20;
}
#toast.show { opacity: 1; }

/* auth / signed-out state */
body.signed-out main, body.signed-out .tabbar, body.signed-out .topbar { display: none; }
#auth-view { max-width: 420px; margin: 8vh auto 0; padding: 0 16px; }
#auth-view .card { margin-bottom: 12px; }
.topbar .link { margin-left: auto; }
#update-banner {
  position: fixed; left: 12px; right: 12px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 20;
  display: flex; gap: 12px; align-items: center; justify-content: space-between;
  border-color: var(--accent); background: var(--accent-soft); margin: 0;
}
#update-banner[hidden] { display: none; }
#invite-list li { flex-wrap: wrap; }
