:root {
  color-scheme: light;
  --bg: #f4f5f2;
  --card: #ffffff;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink2: #52514e;
  --muted: #6f6d68;
  --line: #e1e0d9;
  --axis: #c3c2b7;
  --accent: #0c7a5e;
  --accent-soft: #e2f2ec;
  --on-accent: #ffffff;
  --critical: #d03b3b;
  --good: #006300;
  /* Makros = Kategorie-Slots 1–3 der validierten Palette (alle Paare geprüft) */
  --carbs: #2a78d6;
  --protein: #eb6834;
  --fat: #1baf7a;
  --series: #2a78d6;
  --shadow: 0 1px 2px rgba(11,11,11,.06), 0 4px 16px rgba(11,11,11,.05);
  --ring: rgba(11,11,11,.10);
  --radius: 18px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0d0d0d;
    --card: #1a1a19;
    --surface: #1a1a19;
    --ink: #f0efec;
    --ink2: #c3c2b7;
    --muted: #9a988f;
    --line: #2c2c2a;
    --axis: #383835;
    --accent: #1fae86;
    --accent-soft: #15332a;
    --on-accent: #06140f;
    --critical: #e66767;
    --good: #0ca30c;
    --carbs: #3987e5;
    --protein: #d95926;
    --fat: #199e70;
    --series: #3987e5;
    --shadow: none;
    --ring: rgba(255,255,255,.10);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font); font-size: 16px; line-height: 1.4; }
body { min-height: 100dvh; overscroll-behavior-y: none; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.1rem; } h3 { font-size: 1rem; }
.muted { color: var(--muted); } .small { font-size: .85rem; } .ink2 { color: var(--ink2); }
.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* Layout */
.page { max-width: 640px; margin: 0 auto; padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 96px); }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 4px 0 14px; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--ring); padding: 16px; margin-bottom: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }

/* Buttons */
.btn { border: 0; border-radius: 12px; padding: 12px 16px; background: var(--accent); color: var(--on-accent); font-weight: 600; min-height: 44px; }
.btn.secondary { background: var(--accent-soft); color: var(--accent); }
.btn.ghost { background: transparent; color: var(--accent); padding: 8px 10px; }
.btn.danger { background: transparent; color: var(--critical); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.icon-btn { border: 0; background: transparent; width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; color: var(--ink2); }
.icon-btn:active, .btn:active { transform: scale(.98); }
.icon-btn svg, .btn svg { width: 22px; height: 22px; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 12px; font-size: .9rem; color: var(--ink2); min-height: 36px; }
.chip[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

/* Formulare */
label.field { display: block; }
label.field > span { display: block; font-size: .85rem; color: var(--ink2); margin-bottom: 4px; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=search], select {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); min-height: 44px; font-size: 16px;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.seg { display: flex; background: var(--bg); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 8px 6px; border-radius: 10px; color: var(--ink2); font-size: .9rem; min-height: 38px; }
.seg button[aria-pressed="true"] { background: var(--card); color: var(--ink); font-weight: 600; box-shadow: var(--shadow); }
.error { color: var(--critical); font-size: .9rem; min-height: 1.2em; }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login .card { width: 100%; max-width: 380px; padding: 24px; }
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.brand img { width: 40px; height: 40px; border-radius: 10px; }

/* Datum */
.datenav { display: flex; align-items: center; gap: 2px; }
.datenav .label { font-weight: 700; font-size: 1.15rem; padding: 6px 8px; border: 0; background: none; }

/* Zusammenfassung */
.summary { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; text-align: center; }
.summary .side b { display: block; font-size: 1.15rem; }
.ring { position: relative; width: 150px; height: 150px; }
.ring svg { width: 150px; height: 150px; transform: rotate(-90deg); }
.ring .center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring .center b { font-size: 1.9rem; line-height: 1; }
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 16px; }
.macro .bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin: 6px 0 4px; }
.macro .bar i { display: block; height: 100%; border-radius: 3px; }
.macro .name { font-size: .85rem; color: var(--ink2); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.over { color: var(--critical); font-weight: 600; }

/* Mahlzeiten */
.meal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.meal-head .kcal { color: var(--ink2); font-size: .95rem; }
.entry { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); width: 100%; background: none; border-left: 0; border-right: 0; border-bottom: 0; text-align: left; }
.entry:first-of-type { margin-top: 8px; }
.entry .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .kcal { font-variant-numeric: tabular-nums; color: var(--ink2); white-space: nowrap; }
.add-line { display: flex; gap: 8px; margin-top: 10px; }

/* Wasser */
.glasses { display: flex; gap: 6px; flex-wrap: wrap; margin: 10px 0 4px; }
.glass { width: 28px; height: 36px; border-radius: 4px 4px 8px 8px; border: 2px solid var(--axis); background: transparent; padding: 0; position: relative; overflow: hidden; }
.glass.full { border-color: var(--accent); }
.glass.full::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 75%; background: var(--accent); opacity: .85; }

/* Bottom-Navigation */
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; background: color-mix(in srgb, var(--card) 92%, transparent); backdrop-filter: blur(12px); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); z-index: 20; }
.tabbar nav { max-width: 640px; margin: 0 auto; display: grid; grid-template-columns: repeat(3, 1fr); }
.tabbar button { border: 0; background: none; padding: 8px 4px 10px; display: grid; justify-items: center; gap: 2px; color: var(--muted); font-size: .75rem; min-height: 56px; }
.tabbar button svg { width: 24px; height: 24px; }
.tabbar button[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* Sheets */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.sheet { background: var(--card); width: 100%; max-width: 640px; max-height: 92dvh; border-radius: 20px 20px 0 0; display: flex; flex-direction: column; animation: up .2s ease-out; padding-bottom: env(safe-area-inset-bottom); }
.sheet.full { height: 92dvh; }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 12px 12px 8px 16px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { flex: 1; }
.sheet-body { padding: 14px 16px 18px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
@keyframes up { from { transform: translateY(40px); opacity: .5 } }
@keyframes fade { from { opacity: 0 } }

.list { margin: 0; padding: 0; list-style: none; }
.food { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); width: 100%; background: none; border-top: 0; border-left: 0; border-right: 0; text-align: left; }
.food img, .food .ph { width: 40px; height: 40px; border-radius: 8px; object-fit: cover; background: var(--bg); flex: none; }
.food .ph { display: grid; place-items: center; color: var(--muted); font-size: .7rem; }
.food .t { overflow: hidden; }
.food .t b { display: block; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.food .t > span { font-size: .85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.food .k { margin-left: auto; text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; color: var(--ink2); font-size: .9rem; }
.section-title { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 16px 0 4px; }
.badge { font-size: .7rem; padding: 2px 6px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); margin-left: 6px; vertical-align: middle; }

.nutri { width: 100%; border-collapse: collapse; font-size: .92rem; }
.nutri td { padding: 6px 0; border-bottom: 1px solid var(--line); }
.nutri td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.big-kcal { font-size: 2.2rem; font-weight: 700; line-height: 1; }

.scanner { position: relative; background: #000; border-radius: 14px; overflow: hidden; aspect-ratio: 3/4; }
.scanner video { width: 100%; height: 100%; object-fit: cover; }
.scanner .frame { position: absolute; left: 12%; right: 12%; top: 38%; height: 24%; border: 3px solid #fff; border-radius: 12px; box-shadow: 0 0 0 999px rgba(0,0,0,.35); }

/* Diagramme */
.chart { position: relative; margin-top: 10px; touch-action: pan-y; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .grid line { stroke: var(--line); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 11px; font-family: var(--font); font-variant-numeric: tabular-nums; }
.chart .goal { stroke: var(--ink2); stroke-width: 1.5; stroke-dasharray: 4 4; }
.chart .goal-label { fill: var(--ink2); font-weight: 600; }
.chart .line { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .pt { fill: var(--series); stroke: var(--surface); stroke-width: 2; }
.chart .bar { fill: var(--series); }
.chart .cross { stroke: var(--ink2); stroke-width: 1; }
.tip { position: absolute; pointer-events: none; background: var(--ink); color: var(--card); padding: 6px 9px; border-radius: 8px; font-size: .8rem; white-space: nowrap; transform: translate(-50%, -100%); z-index: 2; }
.tip b { display: block; }
.stats3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; margin-top: 6px; }
.stats3 b { display: block; font-size: 1.2rem; }
details.table summary { cursor: pointer; color: var(--accent); font-size: .9rem; margin-top: 10px; min-height: 32px; }
details.table table { width: 100%; border-collapse: collapse; font-size: .9rem; margin-top: 6px; }
details.table th, details.table td { text-align: right; padding: 4px 0; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
details.table th:first-child, details.table td:first-child { text-align: left; }

#toast { position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 84px); transform: translateX(-50%); background: var(--ink); color: var(--card); padding: 10px 16px; border-radius: 12px; font-size: .9rem; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 60; max-width: 90vw; }
#toast.show { opacity: 1; }
.spinner { width: 20px; height: 20px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg) } }
.empty { text-align: center; color: var(--muted); padding: 20px 8px; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* v2: Schnell-Hinzufügen, Vorlagen, Legende, Kacheln */
.tabbar nav { grid-template-columns: repeat(4, 1fr); }
.food-wrap { display: flex; align-items: center; border-bottom: 1px solid var(--line); }
.food-wrap .food { border-bottom: 0; flex: 1; min-width: 0; }
.quick-add { width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--accent-soft); color: var(--accent); flex: none; margin-left: 8px; display: grid; place-items: center; }
.quick-add svg { width: 20px; height: 20px; }
.quick-add.done { background: var(--accent); color: var(--on-accent); }
.quick-add.del { background: transparent; color: var(--muted); }
.legend { display: flex; gap: 16px; margin-top: 10px; color: var(--ink2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.lg-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.lg-line { width: 16px; height: 2px; border-radius: 1px; background: var(--series); display: inline-block; }
.chart .pt.raw { fill: var(--muted); stroke: var(--surface); stroke-width: 1.5; }
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 12px; }
.tile { background: var(--bg); border-radius: 14px; padding: 10px 12px; display: grid; gap: 1px; }
.tile b { font-size: 1.25rem; }

/* v3: Foto-Erkennung */
.ph-item { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.ph-g { width: 72px !important; text-align: right; padding: 8px 10px !important; min-height: 40px !important; flex: none; }
