/* Nährwerk – mobile-first, iPhone Safari, Dark Mode über prefers-color-scheme */

:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #eef2f0;
  --text: #15201b;
  --muted: #66746d;
  --line: #dde4e0;
  --accent: #1fa874;
  --accent-strong: #148a5d;
  --accent-soft: #dcf5ea;
  --danger: #d6453d;
  --warn: #e39b16;
  --carbs: #f0a32a;
  --protein: #3d7be0;
  --fat: #d55d8f;
  --ring-track: #e3e9e6;
  --shadow: 0 1px 2px rgba(16, 32, 24, 0.06), 0 4px 16px rgba(16, 32, 24, 0.05);
  --radius: 16px;
  --tabbar-h: 58px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1412;
    --surface: #18201c;
    --surface-2: #212b26;
    --text: #e8efeb;
    --muted: #93a29a;
    --line: #2b3631;
    --accent: #35c48d;
    --accent-strong: #5ad6a5;
    --accent-soft: #173a2c;
    --danger: #f06a62;
    --ring-track: #26312c;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-strong); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.6rem; font-weight: 700; }
h2 { font-size: 1.1rem; font-weight: 650; }
h3 { font-size: 1rem; font-weight: 600; }
[hidden] { display: none !important; }

.app { min-height: 100vh; min-height: 100dvh; }
.view {
  padding: calc(var(--safe-top) + 12px) calc(var(--safe-right) + 16px) calc(var(--safe-bottom) + var(--tabbar-h) + 24px) calc(var(--safe-left) + 16px);
  max-width: 640px;
  margin: 0 auto;
}
.view.no-tabbar { padding-bottom: calc(var(--safe-bottom) + 24px); }
.splash { display: grid; place-items: center; min-height: 70vh; }
.splash img { border-radius: 18px; opacity: 0.85; }

/* --- Tabbar --- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  padding: 6px calc(var(--safe-right) + 8px) calc(var(--safe-bottom) + 4px) calc(var(--safe-left) + 8px);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
  backdrop-filter: saturate(180%) blur(18px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); font-size: 0.7rem; padding: 4px 0;
}
.tabbar a.active { color: var(--accent); }
.tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* --- Kopfzeilen --- */
.topbar { display: flex; align-items: center; gap: 8px; min-height: 44px; margin-bottom: 12px; }
.topbar h1 { flex: 1; font-size: 1.25rem; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .spacer { width: 44px; }
.datebar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.datebar .date-label { position: relative; flex: 1; text-align: center; font-weight: 650; font-size: 1.15rem; }
.datebar .date-label small { display: block; font-weight: 400; color: var(--muted); font-size: 0.8rem; }
.datebar input[type="date"] { position: absolute; inset: 0; opacity: 0; width: 100%; }

/* --- Buttons --- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 18px; border-radius: 12px; border: 0;
  background: var(--accent); color: #fff; font-weight: 600; cursor: pointer;
  touch-action: manipulation;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.ghost { background: transparent; color: var(--accent-strong); padding: 10px 8px; }
.btn.danger { background: transparent; color: var(--danger); }
.btn.block { width: 100%; }
.btn.small { min-height: 34px; padding: 6px 12px; font-size: 0.9rem; border-radius: 10px; }
.icon-btn {
  width: 44px; height: 44px; display: grid; place-items: center; border-radius: 50%;
  border: 0; background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.star.on svg { fill: #f5b301; stroke: #f5b301; }

/* --- Karten --- */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px; }
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 14px 16px 8px; }
.card-head .sum { color: var(--muted); font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }
.row { display: flex; gap: 8px; align-items: center; }
.row > * { flex: 1; }
.stack > * + * { margin-top: 12px; }
.num { font-variant-numeric: tabular-nums; }

/* --- Kalorien-Ring --- */
.summary { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; }
.summary .side { text-align: center; }
.summary .side b { display: block; font-size: 1.2rem; font-variant-numeric: tabular-nums; }
.summary .side span { color: var(--muted); font-size: 0.8rem; }
.ring { position: relative; width: 150px; height: 150px; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 12; stroke-linecap: round; }
.ring .track { stroke: var(--ring-track); }
.ring .value { stroke: var(--accent); transition: stroke-dashoffset 0.5s ease; }
.ring.over .value { stroke: var(--danger); }
.ring .label { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring .label b { font-size: 1.8rem; font-variant-numeric: tabular-nums; line-height: 1; }
.ring .label span { color: var(--muted); font-size: 0.8rem; margin-top: 4px; }
.ring.over .label b { color: var(--danger); }

.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 16px; }
.macro .name { font-size: 0.8rem; color: var(--muted); }
.macro .val { font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.bar { height: 6px; border-radius: 3px; background: var(--ring-track); overflow: hidden; margin: 4px 0; }
.bar i { display: block; height: 100%; border-radius: 3px; transition: width 0.5s ease; }
.bar.carbs i { background: var(--carbs); }
.bar.protein i { background: var(--protein); }
.bar.fat i { background: var(--fat); }
.bar.over i { background: var(--danger); }
.health-line { display: flex; justify-content: center; gap: 16px; margin-top: 12px; font-size: 0.85rem; color: var(--muted); flex-wrap: wrap; }

/* --- Listen --- */
.list { list-style: none; margin: 0; padding: 0; }
.list li { border-top: 1px solid var(--line); }
.list .item {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 12px 16px; background: none; border: 0; text-align: left; cursor: pointer; color: inherit;
}
.list .item:active { background: var(--surface-2); }
.list .item .main { flex: 1; min-width: 0; }
.list .item .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .item .sub { color: var(--muted); font-size: 0.82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .item .kcal { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 0.9rem; white-space: nowrap; }
.list .item img.thumb { width: 40px; height: 40px; object-fit: cover; border-radius: 8px; background: var(--surface-2); flex: none; }
.list .item .ph { width: 40px; height: 40px; border-radius: 8px; background: var(--surface-2); flex: none; display: grid; place-items: center; color: var(--muted); font-size: 1.1rem; }
.list .empty { padding: 12px 16px; color: var(--muted); font-size: 0.9rem; border-top: 1px solid var(--line); }
.meal-actions { display: flex; justify-content: space-between; border-top: 1px solid var(--line); padding: 4px 8px; }
.section-title { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 16px 4px 8px; }
.badge { display: inline-block; font-size: 0.7rem; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--muted); margin-left: 4px; vertical-align: middle; }
.badge.fav { background: #fff3c4; color: #8a6500; }
@media (prefers-color-scheme: dark) { .badge.fav { background: #3d3415; color: #f5cf5a; } }

/* --- Formulare --- */
label.field { display: block; }
label.field > span { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 4px; }
input[type="text"], input[type="password"], input[type="number"], input[type="search"], input[type="date"], select, textarea {
  width: 100%; min-height: 44px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
  font-size: 16px; /* verhindert Zoom in iOS Safari */
  -webkit-appearance: none; appearance: none;
}
select { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 32px; }
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; }
.switch input { width: 51px; height: 31px; -webkit-appearance: none; appearance: none; background: var(--ring-track); border-radius: 16px; position: relative; flex: none; transition: background 0.2s; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.3); transition: transform 0.2s; }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { transform: translateX(20px); }
.error-msg { color: var(--danger); font-size: 0.9rem; min-height: 1.2em; }

/* --- Segmente/Tabs --- */
.segmented { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; margin-bottom: 12px; }
.segmented button { flex: 1; min-height: 36px; border: 0; border-radius: 9px; background: transparent; font-weight: 550; font-size: 0.9rem; cursor: pointer; }
.segmented button.active { background: var(--surface); box-shadow: var(--shadow); color: var(--accent-strong); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: 0.85rem; cursor: pointer; }
.chip:active { background: var(--surface-2); }

/* --- Produktdetail --- */
.product-head { display: flex; gap: 12px; align-items: center; }
.product-head img { width: 72px; height: 72px; object-fit: contain; border-radius: 12px; background: #fff; }
.nutri-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.nutri-table td { padding: 6px 0; border-top: 1px solid var(--line); }
.nutri-table td:last-child { text-align: right; }
.preview { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; gap: 4px; margin-top: 12px; }
.preview b { display: block; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.preview span { font-size: 0.75rem; color: var(--muted); }
.amount-row { display: flex; gap: 8px; }
.amount-row input { flex: 1; font-size: 1.2rem; text-align: center; }
.amount-row select { width: auto; flex: none; min-width: 120px; }

/* --- Scanner --- */
.scanner { position: relative; background: #000; border-radius: var(--radius); overflow: hidden; aspect-ratio: 3 / 4; max-height: 60vh; width: 100%; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .frame { position: absolute; left: 10%; right: 10%; top: 35%; bottom: 35%; border: 3px solid rgba(255,255,255,0.85); border-radius: 14px; box-shadow: 0 0 0 9999px rgba(0,0,0,0.35); }
.scanner .status { position: absolute; left: 0; right: 0; bottom: 12px; text-align: center; color: #fff; font-size: 0.9rem; text-shadow: 0 1px 3px #000; }

/* --- Foto --- */
.photo-preview { width: 100%; max-height: 300px; object-fit: contain; border-radius: 12px; background: var(--surface-2); }
.ai-item { border-top: 1px solid var(--line); padding: 12px 0; }
.ai-item:first-child { border-top: 0; }
.ai-item .head { display: flex; align-items: center; gap: 8px; }
.ai-item .head input[type="text"] { flex: 1; }
.ai-item .grid-4 input { padding: 8px 6px; text-align: center; }
.ai-item .grid-4 label span { font-size: 0.72rem; text-align: center; }
.ai-item.excluded { opacity: 0.45; }
.conf { font-size: 0.72rem; padding: 2px 6px; border-radius: 6px; white-space: nowrap; }
.conf.low { background: #fde3e1; color: #a12a22; }
.conf.medium { background: #fff1d1; color: #8a5a00; }
.conf.high { background: var(--accent-soft); color: var(--accent-strong); }
@media (prefers-color-scheme: dark) { .conf.low { background: #4a1f1c; color: #ffb3ad; } .conf.medium { background: #3d3212; color: #f5cf5a; } }
.checkbox { display: inline-flex; align-items: center; gap: 6px; font-size: 0.85rem; color: var(--muted); }
.checkbox input { width: 20px; height: 20px; accent-color: var(--accent); }
.spinner { width: 28px; height: 28px; border: 3px solid var(--ring-track); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; margin: 16px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Statistik --- */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 12px; }
.tile { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px; }
.tile b { display: block; font-size: 1.3rem; font-variant-numeric: tabular-nums; }
.tile span { color: var(--muted); font-size: 0.8rem; }
.chart-box { position: relative; height: 220px; }
.chart-box.small { height: 180px; }

/* --- Login --- */
.login { min-height: 80vh; display: flex; flex-direction: column; justify-content: center; }
.login .logo { width: 84px; height: 84px; margin: 0 auto 16px; display: block; }
.login h1 { text-align: center; margin-bottom: 24px; }

/* --- Token --- */
.token-box { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.85rem; word-break: break-all; background: var(--surface-2); padding: 12px; border-radius: 10px; user-select: all; -webkit-user-select: all; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.85em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; word-break: break-all; }

/* --- Toast & Sheet --- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-bottom) + var(--tabbar-h) + 16px); z-index: 50;
  background: #1c2621; color: #fff; padding: 10px 16px; border-radius: 12px;
  font-size: 0.9rem; max-width: calc(100% - 32px); box-shadow: 0 8px 24px rgba(0,0,0,0.25);
}
.toast.error { background: var(--danger); }
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: 100%; max-width: 640px; background: var(--surface); border-radius: 18px 18px 0 0; padding: 8px 16px calc(var(--safe-bottom) + 16px); animation: up 0.2s ease; }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
.sheet .btn { margin-top: 8px; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }

.offline-banner { background: var(--warn); color: #000; text-align: center; font-size: 0.85rem; padding: 6px; border-radius: 10px; margin-bottom: 10px; }

@media (min-width: 560px) {
  .tiles { grid-template-columns: repeat(4, 1fr); }
  .ring { width: 170px; height: 170px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
