:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --text: #1c1f1d;
  --muted: #6b726d;
  --border: #e2e5e1;
  --accent: #2f9e5b;
  --accent-text: #ffffff;
  --danger: #c0392b;
  --danger-bg: #fbeae8;
  --warn-bg: #fff6dd;
  --track: #e9ece8;
  --overlay: rgba(0, 0, 0, .35);
  --radius: 14px;
  --tabbar-h: 58px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111312;
    --surface: #1c1f1d;
    --text: #eef1ee;
    --muted: #9aa29c;
    --border: #2c302d;
    --accent: #3fbf72;
    --accent-text: #0b1a10;
    --danger: #ff6b5c;
    --danger-bg: #3a1d1a;
    --warn-bg: #3a3218;
    --track: #2c302d;
    --overlay: rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 0; font-size: 1.35rem; }

main { padding: 16px; }
.view[hidden] { display: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 8px; font-size: 1.1rem; }
.placeholder p { margin: 0 0 6px; }
.muted { color: var(--muted); font-size: .9rem; }
.error { color: var(--danger); }

.row { display: flex; gap: 8px; margin-bottom: 12px; }
.row > input { flex: 1; min-width: 0; }

.btn {
  appearance: none;
  border: 0;
  border-radius: 10px;
  padding: 11px 16px;
  font: inherit;
  font-weight: 600;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn-small { padding: 6px 12px; font-size: .9rem; }
.btn[hidden] { display: none; }

input[type="text"],
input[type="search"],
select {
  font: inherit;
  font-size: 16px; /* verhindert Auto-Zoom in iOS Safari */
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  min-height: 44px;
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
}
.row > input { width: auto; }
input.invalid { border-color: var(--danger); }
input[readonly] { color: var(--muted); }
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;
}

.icon-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}

.date-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.date-label {
  appearance: none;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-align: center;
  min-height: 44px;
  padding: 0 8px;
  cursor: pointer;
}

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 12px 16px 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
.banner[hidden] { display: none; }
.banner .btn { background: var(--accent-text); color: var(--accent); }

/* Scanner */
.scanner {
  position: relative;
  aspect-ratio: 4 / 3;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 12px;
}
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-frame {
  position: absolute;
  inset: 30% 12%;
  border: 3px solid rgba(255, 255, 255, .85);
  border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .25);
  pointer-events: none;
}
.scanner.found .scan-frame { border-color: var(--accent); }
.scan-status {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  padding: 8px 12px;
  color: #fff;
  background: linear-gradient(transparent, rgba(0, 0, 0, .6));
  font-size: .9rem;
}

.product { display: flex; gap: 12px; align-items: flex-start; }
.product img { width: 72px; height: 72px; object-fit: contain; border-radius: 8px; background: #fff; flex-shrink: 0; }
.product h3 { margin: 0 0 2px; font-size: 1.05rem; }
.nutri { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 12px; text-align: center; }
.nutri div { background: var(--bg); border-radius: 8px; padding: 6px 2px; }
.nutri b { display: block; font-size: 1.05rem; }
.nutri span { font-size: .75rem; color: var(--muted); }

.info { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0; }
.info dt { color: var(--muted); }
.info dd { margin: 0; text-align: right; }

/* Tab-Leiste */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  max-width: 560px;
  margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tab {
  flex: 1;
  height: var(--tabbar-h);
  appearance: none;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: .75rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
}
.tab-icon { font-size: 1.3rem; filter: grayscale(1); opacity: .7; }
.tab.active { color: var(--accent); font-weight: 600; }
.tab.active .tab-icon { filter: none; opacity: 1; }

/* ---------- Phase 2 ---------- */

.btn { min-height: 44px; }
.btn-block { flex: 1; width: 100%; }
.row > .btn-block { width: auto; }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn:disabled { opacity: .45; cursor: default; }
.btn-small { min-height: 36px; }
.icon-btn.small { width: 44px; height: 44px; font-size: 1.3rem; }
.link {
  appearance: none; border: 0; background: none; padding: 10px 0 0; min-height: 44px;
  font: inherit; color: var(--accent); font-weight: 600; cursor: pointer;
}
.warn { background: var(--warn-bg); font-size: .9rem; }
.empty { text-align: center; padding: 12px 0; margin: 0; }
.img-failed { display: none !important; }

/* Formulare */
.field { display: block; margin-bottom: 12px; }
.field > .label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
.input-unit { display: flex; align-items: center; position: relative; }
.input-unit input { padding-right: 52px; }
.input-unit > span {
  position: absolute; right: 12px; color: var(--muted); font-size: .9rem; pointer-events: none;
}
.input-unit.small { width: 104px; flex-shrink: 0; }
.input-unit.small input { padding-right: 28px; text-align: right; }
.input-unit.small > span { right: 10px; }
.grid2, .grid3 { display: grid; gap: 0 8px; }
.grid2 { grid-template-columns: 1fr 1fr; }
.grid3 { grid-template-columns: repeat(3, 1fr); }
.grid3 .input-unit input { padding-right: 30px; }
.grid3 .input-unit > span { right: 8px; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; margin-bottom: 8px; cursor: pointer; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }

/* Segment-Schalter */
.seg {
  display: flex; background: var(--track); border-radius: 10px; padding: 3px; gap: 2px; margin-bottom: 12px;
}
.seg button {
  flex: 1; min-width: 0; min-height: 40px; appearance: none; border: 0; border-radius: 8px; background: none;
  color: var(--text); font: inherit; font-size: .9rem; cursor: pointer; padding: 0 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.seg button.active { background: var(--surface); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

.chips { display: flex; gap: 8px; margin-bottom: 12px; }
.chip {
  flex: 1; appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 10px; min-height: 48px; font: inherit; font-size: .9rem; font-weight: 600; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 4px;
}
.chip small { font-weight: 400; color: var(--muted); font-size: .75rem; }

.search { margin-bottom: 12px; }

/* Listen */
.list { list-style: none; margin: 0 0 12px; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.list > li + li { border-top: 1px solid var(--border); }
.list-head { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 16px 4px 6px; }
.list-item {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 56px; padding: 10px 14px;
  appearance: none; border: 0; background: none; color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.list-item.static { cursor: default; }
.li-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.li-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li-sub { font-size: .82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li-side { text-align: right; font-weight: 600; white-space: nowrap; display: flex; flex-direction: column; }
.li-side small { font-weight: 400; font-size: .72rem; color: var(--muted); }

/* Tagesübersicht */
.sum-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.sum-kcal { font-size: 1.6rem; }
.bar { height: 8px; border-radius: 4px; background: var(--track); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 4px; transition: width .25s; }
.bar.over > span { background: var(--danger); }
.over-text { color: var(--danger); font-weight: 600; font-size: .9rem; }
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 14px; }
.macro-label { font-size: .8rem; color: var(--muted); }
.macro-val { font-size: .9rem; margin-bottom: 4px; white-space: nowrap; }
.macro-rest { font-size: .78rem; margin-top: 3px; }
.macro-rest .muted, .macro-rest .over-text { font-size: inherit; }

/* Mahlzeiten */
.meal { padding: 6px 6px 6px 16px; }
.meal-head { display: flex; align-items: center; gap: 8px; }
.meal-head h2 { flex: 1; margin: 0; font-size: 1.05rem; }
.meal-kcal { color: var(--muted); font-size: .9rem; white-space: nowrap; }
.meal-head .icon-btn { border-color: transparent; color: var(--accent); font-weight: 600; }
/* Rechter Abstand am Eintrag statt an der Liste – sonst schaut die rote
   Wisch-Fläche (.swipe-row::after) im Abstand der Liste hervor. */
.entries { list-style: none; margin: 0; padding: 0 0 4px; overflow: hidden; }
.entry { border-top: 1px solid var(--border); background: var(--surface); padding-right: 10px; }
.entry-row { display: flex; align-items: center; }
.entry-main {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px; min-height: 52px; padding: 6px 0;
  appearance: none; border: 0; background: none; color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.entry-kcal { white-space: nowrap; font-weight: 600; }
.expand {
  width: 44px; height: 44px; flex-shrink: 0; appearance: none; border: 0; background: none; color: var(--muted);
  font-size: 1.1rem; cursor: pointer; transition: transform .2s; margin-right: -10px;
}
.expand.open { transform: rotate(180deg); }
.entry-items { list-style: none; margin: 0 0 8px; padding: 6px 10px; background: var(--bg); border-radius: 10px; font-size: .88rem; }
.entry-items li { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; }

.fab {
  position: fixed; z-index: 20;
  right: max(16px, calc((100vw - 560px) / 2 + 16px));
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text); font-size: 2rem; line-height: 1; cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .25);
}
#view-diary { padding-bottom: 72px; }

/* Zutaten-Zeilen */
.ing-list { list-style: none; margin: 0 0 12px; padding: 0; overflow: hidden; }
.ing {
  display: flex; align-items: center; gap: 8px; padding: 6px 0;
  border-bottom: 1px solid var(--border); background: var(--bg);
}

/* Wischen zum Löschen (ui.js swipeToDelete). Die Liste braucht overflow: hidden. */
.swipe-row { position: relative; touch-action: pan-y; }
.swipe-row:not(.swiping) { transition: transform .2s ease-out; }
/* Rote Fläche, die beim Wischen nach links von rechts hereinkommt */
.swipe-row::after {
  content: "Löschen"; position: absolute; top: 0; bottom: 0; left: 100%; width: 100vw;
  display: flex; align-items: center; padding-left: 16px;
  background: var(--danger-bg); color: var(--danger); font-weight: 600;
}
.swipe-row.swipe-armed::after { background: var(--danger); color: #fff; }
.ing-name { flex: 1; min-width: 0; overflow-wrap: anywhere; display: flex; flex-direction: column; }
.ing-kcal { color: var(--muted); font-size: .8rem; }
.sheet-h { margin: 4px 0 8px; font-size: 1.1rem; }
.preview { margin-bottom: 16px; }
.preview .nutri { margin-top: 4px; }

/* Nährwert-Tabelle */
.ntable { width: 100%; border-collapse: collapse; margin: 16px 0; font-size: .92rem; }
.ntable th, .ntable td { padding: 7px 4px; border-bottom: 1px solid var(--border); text-align: right; }
.ntable th:first-child { text-align: left; font-weight: 400; color: var(--muted); }
.ntable thead th { font-size: .8rem; color: var(--muted); font-weight: 600; }

/* Rechner */
.calc summary { list-style: none; cursor: pointer; display: flex; align-items: center; justify-content: space-between; min-height: 32px; }
.calc summary::-webkit-details-marker { display: none; }
.calc summary::after { content: "▾"; color: var(--muted); transition: transform .2s; }
.calc[open] summary::after { transform: rotate(180deg); }
.calc summary h2 { margin: 0; }
.calc[open] summary { margin-bottom: 12px; }
.suggestion { background: var(--bg); border-radius: 10px; padding: 10px; margin-bottom: 12px; }
.sug-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 8px; text-align: center; }
.sug-grid b { display: block; }
.sug-grid span { font-size: .75rem; color: var(--muted); }

/* Sheets (Vollbild-Dialoge) */
body.sheet-open { overflow: hidden; }
.sheet-overlay {
  position: fixed; inset: 0; z-index: 50; background: var(--overlay);
  animation: fade-in .15s ease-out;
}
.sheet {
  position: absolute; left: 0; right: 0; margin: 0 auto; max-width: 560px;
  /* Sichtbarer Bereich (ohne Tastatur), gesetzt in ui.js */
  top: var(--vv-top, 0px); height: var(--vv-h, 100%);
  display: flex; flex-direction: column; background: var(--bg);
  animation: slide-up .22s ease-out;
}
.sheet-head {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.sheet-title { margin: 0; font-size: 1.05rem; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 60vw; }
.sheet-close {
  justify-self: start; appearance: none; border: 0; background: none; color: var(--accent);
  font: inherit; font-weight: 600; min-height: 44px; padding: 0 8px; cursor: pointer;
}
.sheet-body {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 16px 16px 24px;
}
.sheet-foot {
  display: flex; gap: 8px; padding: 10px 16px calc(env(safe-area-inset-bottom) + 10px);
  border-top: 1px solid var(--border); background: var(--surface);
}
/* Tastatur offen: hinter der Tastatur nur neutraler Hintergrund, und unten
   kein Abstand für den Home-Balken (der liegt dann unter der Tastatur). */
body.kb-open .sheet-overlay { background: var(--bg); }
body.kb-open .sheet-foot { padding-bottom: 10px; }
@keyframes slide-up { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.toast {
  position: fixed; z-index: 100; left: 50%; top: calc(env(safe-area-inset-top) + 12px);
  transform: translate(-50%, -20px); opacity: 0; pointer-events: none;
  max-width: min(520px, calc(100vw - 32px)); padding: 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-weight: 600; font-size: .92rem; text-align: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .2); transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.show.has-action { pointer-events: auto; }
.toast-action {
  appearance: none; border: 0; background: none; color: inherit; font: inherit;
  text-decoration: underline; margin-left: 10px; padding: 6px 2px; cursor: pointer;
}

@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet-overlay { animation: none; }
}
