@font-face {
  font-family: "Inter";
  src: url("fonts/inter-latin-wght.woff2") format("woff2");
  font-weight: 100 900;
  font-display: swap;
}

:root {
  --bg: #F5F3EE;
  --card: #FFFFFF;
  --text: #1A1A18;
  --muted: #5C5A55;
  --line: #F0EDE6;
  --border: #E6E2D9;
  --chip: #D8D3C8;
  --dash: #C9C4B8;
  --track: #ECE8E0;
  --green: #1F5A42;
  --green-dark: #143D2C;
  --green-track: #E3EEE8;
  --green-light: #8FD3AE;
  --carbs: #B5651D;
  --carbs-track: #F6E8D8;
  --sugar: #6E3A0E;
  --fat: #3B5998;
  --fat-track: #E4E8F2;
  --danger: #A3341F;
  --radius: 16px;
  --tab-h: 74px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.35;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--green); }
a:hover { color: var(--green-dark); }
.num { font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.strong { font-weight: 600; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Layout ---------- */
#app { min-height: 100vh; min-height: 100dvh; }
.view {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 20px) 20px calc(var(--tab-h) + env(safe-area-inset-bottom) + 24px);
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.view.no-tabs { padding-bottom: calc(env(safe-area-inset-bottom) + 24px); }
.view.wide { max-width: 1080px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around; align-items: center;
  padding: 10px 12px calc(env(safe-area-inset-bottom) + 12px);
  background: var(--card); border-top: 1px solid var(--border);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 64px; min-height: 44px;
  justify-content: center; text-decoration: none; color: var(--muted); font-size: 11px;
}
.tabbar a.active { color: var(--text); font-weight: 600; }
.tabbar a.fab {
  width: 60px; height: 60px; min-width: 60px; border-radius: 30px; background: var(--green); color: #fff;
  margin-top: -26px; box-shadow: 0 4px 14px rgba(31, 90, 66, 0.3);
}
.tabbar a.fab:hover { background: var(--green-dark); }
body.sub .tabbar { display: none; }

@media (min-width: 900px) {
  .tabbar {
    top: 0; right: auto; width: 96px; flex-direction: column; justify-content: flex-start; gap: 14px;
    padding: 28px 8px; border-top: none; border-right: 1px solid var(--border);
  }
  .tabbar a.fab { margin-top: 0; order: -1; margin-bottom: 10px; }
  body.sub .tabbar { display: flex; }
  #app { padding-left: 96px; }
  .view { padding-bottom: 40px; }
}

/* ---------- Headers ---------- */
.topbar { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.topbar .title { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.topbar .title > span:first-child { overflow: hidden; text-overflow: ellipsis; }
.h1 { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
.h2 { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.h3 { font-size: 17px; font-weight: 600; }
.section-label { font-size: 12px; font-weight: 600; color: var(--muted); letter-spacing: 0.6px; text-transform: uppercase; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: 44px; height: 44px; border: none; background: transparent; border-radius: 22px; cursor: pointer;
  color: var(--text); text-decoration: none;
}
.icon-btn:hover { background: rgba(0, 0, 0, 0.05); color: var(--text); }
.avatar { background: var(--border); font-weight: 600; font-size: 15px; }

/* ---------- Cards & lists ---------- */
.card { background: var(--card); border-radius: 20px; padding: 20px; }
.list { background: var(--card); border-radius: var(--radius); display: flex; flex-direction: column; overflow: hidden; }
.row {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 12px 16px; border: none; border-bottom: 1px solid var(--line);
  background: transparent; text-align: left; width: 100%; text-decoration: none; color: var(--text); cursor: pointer;
  min-height: 56px;
}
.row:last-child { border-bottom: none; }
.row:hover { background: #FAF9F6; color: var(--text); }
.row .main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.row .main > span:first-child { overflow: hidden; text-overflow: ellipsis; }
.row .val { font-size: 18px; font-weight: 600; white-space: nowrap; }
.row .val small { font-size: 12px; font-weight: 500; color: var(--muted); }
.empty { padding: 16px; color: var(--muted); font-size: 14px; }

.add-dashed {
  display: flex; align-items: center; justify-content: center; height: 52px; border-radius: var(--radius);
  border: 1.5px dashed var(--dash); text-decoration: none; font-size: 15px; color: var(--muted); background: transparent;
}
.add-dashed:hover { border-color: var(--muted); color: var(--text); }

/* ---------- Today ---------- */
.big { font-size: 56px; font-weight: 700; line-height: 1; }
.big .unit { font-size: 22px; font-weight: 500; }
.bar { height: 10px; border-radius: 5px; background: var(--track); overflow: hidden; }
.bar > div { height: 100%; border-radius: 5px; background: var(--text); transition: width .3s; }
.bar.thin { height: 6px; border-radius: 3px; }
.bar.green { background: var(--green-track); } .bar.green > div { background: var(--green); }
.bar.carbs { background: var(--carbs-track); } .bar.carbs > div { background: var(--carbs); }
.bar.fat { background: var(--fat-track); } .bar.fat > div { background: var(--fat); }
.bar.over > div { background: var(--danger); }
/* Zucker als Anteil im Kohlenhydrat-Balken */
.bar > div > .sugar { height: 100%; background: var(--sugar); border-radius: 3px 0 0 3px; }
.sugar-label { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); white-space: nowrap; }
.sugar-dot { width: 8px; height: 8px; border-radius: 2px; background: var(--sugar); flex: none; }
.macros { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.macro { display: flex; flex-direction: column; gap: 6px; }
.macro .v { font-size: 22px; font-weight: 600; }
.macro .v small { font-size: 14px; font-weight: 400; color: var(--muted); }
.meal-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.meal-head .add-mini { min-height: 36px; padding: 0 10px; display: inline-flex; align-items: center; border-radius: 18px; text-decoration: none; font-size: 13px; font-weight: 600; }
.day-nav { display: flex; gap: 2px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 56px; padding: 0 20px; border-radius: var(--radius); border: none; cursor: pointer;
  background: var(--green); color: #fff; font-weight: 600; font-size: 16px; text-decoration: none; width: 100%;
}
.btn:hover { background: var(--green-dark); color: #fff; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.secondary { background: var(--card); color: var(--text); border: 1.5px solid var(--chip); }
.btn.secondary:hover { border-color: var(--text); }
.btn.small { height: 44px; font-size: 14px; width: auto; border-radius: 14px; }
.btn.danger { background: transparent; color: var(--danger); height: 48px; }
.btn.danger:hover { background: rgba(163, 52, 31, 0.08); }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  flex: 1 1 0; min-width: 0; height: 44px; padding: 0 12px; border-radius: 22px; border: 1.5px solid var(--chip);
  background: transparent; font-size: 13px; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.chip.on { border-color: var(--text); background: var(--text); color: #fff; }
.chips.fit .chip { flex: 0 0 auto; }
/* Reiter (Segmented Control) */
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: 14px; background: var(--border); }
.tab { height: 40px; border: none; border-radius: 11px; background: transparent; font-size: 14px; font-weight: 500; color: var(--muted); cursor: pointer; }
.tab.on { background: var(--card); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08); }

/* ---------- Eintragen ---------- */
.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.tile {
  display: flex; flex-direction: column; gap: 10px; padding: 16px; border-radius: 18px; border: none; text-align: left;
  background: var(--card); color: var(--text); cursor: pointer; text-decoration: none; position: relative;
}
.tile.primary { background: var(--green); color: #fff; }
.tile.primary:hover { background: var(--green-dark); color: #fff; }
.tile:hover { color: inherit; }
.tile .t { font-weight: 600; font-size: 15px; }
.tile.wide { grid-column: 1 / -1; flex-direction: row; align-items: center; gap: 14px; border: 1.5px solid var(--green); color: var(--green-dark); }
.tile.wide:hover { background: var(--green-track); color: var(--green-dark); }
.dish-banner { display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-radius: var(--radius); background: var(--carbs-track); color: var(--text); text-decoration: none; min-height: 56px; }
.dish-banner:hover { color: var(--text); filter: brightness(0.98); }
.tile .s { font-size: 12px; opacity: 0.85; }
.tile input[type=file] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label, .field .lbl { font-size: 13px; color: var(--muted); }
.input {
  height: 52px; border-radius: 14px; border: 1.5px solid var(--chip); padding: 0 16px; font-size: 17px;
  background: var(--card); width: 100%; min-width: 0;
}
.input:focus { outline: none; border-color: var(--text); }
textarea.input { height: auto; min-height: 90px; padding: 12px 16px; }
.input.num { text-align: right; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.nut-form .nrow { display: grid; grid-template-columns: 1fr 120px 36px; align-items: center; gap: 10px; padding: 6px 16px; border-bottom: 1px solid var(--line); }
.nut-form .nrow:last-child { border-bottom: none; }
.nut-form .nrow.sub label { padding-left: 14px; color: var(--muted); }
.nut-form .nrow .input { height: 44px; font-size: 16px; }
.notice { padding: 12px 16px; border-radius: 14px; background: var(--carbs-track); color: #5E3410; font-size: 14px; }
.notice.info { background: var(--green-track); color: var(--green-dark); }

/* ---------- Menge ---------- */
.amount-card { padding: 16px 20px; display: flex; flex-direction: column; gap: 12px; }
.amount-num { display: flex; justify-content: center; align-items: baseline; gap: 6px; }
.amount-num .n { font-size: 64px; font-weight: 700; line-height: 1; }
.amount-num .u { font-size: 26px; font-weight: 500; color: var(--muted); }
.vals4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; text-align: center; }
.vals4 > div { display: flex; flex-direction: column; }
.vals4 .v { font-size: 22px; font-weight: 600; }
.vals4 .l { font-size: 11px; color: var(--muted); }
.unit-form { display: flex; flex-direction: column; gap: 10px; padding: 14px; border-radius: 14px; background: var(--bg); }
.unit-form .input { height: 48px; font-size: 16px; }
.keypad { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.keypad button {
  height: 58px; border-radius: 14px; border: none; background: var(--card); font-size: 26px; font-weight: 600;
  font-variant-numeric: tabular-nums; cursor: pointer; touch-action: manipulation;
}
.keypad button:active { background: var(--border); }

/* ---------- Rezept ---------- */
.dark-card { background: var(--text); color: #fff; border-radius: 20px; padding: 18px 20px; display: flex; flex-direction: column; gap: 12px; }
.dark-card .l { font-size: 11px; color: #CFCBC2; }
.dark-card .basis { font-size: 13px; color: #CFCBC2; }
.dark-card .v { font-size: clamp(20px, 6.4vw, 30px); font-weight: 700; }
.dark-card .v.p { color: var(--green-light); }
.ing-row { display: grid; grid-template-columns: minmax(0, 1fr) auto 44px; gap: 8px; align-items: center; padding: 8px 8px 8px 16px; border-bottom: 1px solid var(--line); }
.ing-row:last-child { border-bottom: none; }
.ing-row .input { height: 44px; font-size: 16px; padding: 0 10px; }
.ing-row .qty { display: flex; align-items: center; gap: 6px; }
.ing-row .qty input.input { width: 76px; }
.ing-row .qty select.input { width: auto; min-width: 62px; max-width: 120px; padding: 0 8px; text-align: left; text-overflow: ellipsis; }
.ing-row .qty .u { width: 62px; padding-left: 4px; font-size: 15px; }

/* ---------- Overlay / Sheet ---------- */
.overlay { position: fixed; inset: 0; z-index: 50; background: var(--bg); overflow-y: auto; }
.overlay .view { padding-bottom: calc(env(safe-area-inset-bottom) + 24px); }
.scanner { position: fixed; inset: 0; z-index: 60; background: #000; display: flex; flex-direction: column; }
.scanner video { flex: 1; width: 100%; min-height: 0; object-fit: cover; }
.scanner .frame {
  position: absolute; left: 50%; top: 42%; width: min(78vw, 360px); height: 150px; transform: translate(-50%, -50%);
  border: 3px solid rgba(255, 255, 255, 0.9); border-radius: 18px; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.35); pointer-events: none;
}
.scanner .bar-top { position: absolute; top: 0; left: 0; right: 0; padding: calc(env(safe-area-inset-top) + 12px) 12px 12px; display: flex; justify-content: space-between; align-items: center; color: #fff; }
.scanner .bar-top .icon-btn { color: #fff; }
.scanner .bottom { background: var(--bg); padding: 16px 20px calc(env(safe-area-inset-bottom) + 16px); display: flex; flex-direction: column; gap: 10px; }
.scanner .status { color: #fff; text-align: center; position: absolute; left: 0; right: 0; top: calc(42% + 100px); font-size: 14px; padding: 0 20px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 18px); transform: translateX(-50%);
  z-index: 100; background: var(--text); color: #fff; padding: 12px 18px; border-radius: 14px; font-size: 14px;
  max-width: calc(100vw - 32px); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}
.spinner { width: 22px; height: 22px; border: 3px solid var(--border); border-top-color: var(--green); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Verlauf ---------- */
.stats-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 4px; }
.stat .v { font-size: 26px; font-weight: 700; }
.stat .v small { font-size: 13px; font-weight: 500; color: var(--muted); }
.stack { display: flex; gap: 2px; border-radius: 6px; overflow: hidden; background: var(--track); }
.stack > div { min-width: 2px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend .sw { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .tip {
  position: absolute; pointer-events: none; background: var(--text); color: #fff; font-size: 12px; padding: 6px 10px;
  border-radius: 8px; white-space: nowrap; transform: translate(-50%, -100%); margin-top: -8px;
}
.micro { display: grid; grid-template-columns: minmax(110px, 1.2fr) 2fr 70px; gap: 10px; align-items: center; padding: 9px 16px; border-bottom: 1px solid var(--line); }
.micro:last-child { border-bottom: none; }
.micro .pct { text-align: right; font-weight: 600; }
.table-wrap { overflow-x: auto; background: var(--card); border-radius: var(--radius); }
table.data { border-collapse: collapse; width: 100%; font-size: 14px; }
table.data th, table.data td { padding: 9px 12px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { font-size: 12px; color: var(--muted); font-weight: 600; }
details summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; font-weight: 600; }

@media (min-width: 900px) {
  .history-cols { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 20px; align-items: start; }
  .stats-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

/* ---------- Login ---------- */
.auth { max-width: 400px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; justify-content: center; gap: 16px; padding: 24px 20px; }
.logo { width: 72px; height: 72px; display: block; }
.error { color: var(--danger); font-size: 14px; }
