/* Nutrition Planner – iOS-inspiriertes Design */
:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --card: #ffffff;
  --card-raised: #ffffff;
  --label: #000000;
  --secondary: rgba(60, 60, 67, 0.6);
  --tertiary: rgba(60, 60, 67, 0.3);
  --separator: rgba(60, 60, 67, 0.18);
  --fill: rgba(118, 118, 128, 0.12);
  --fill-strong: rgba(118, 118, 128, 0.2);
  --tint: #007aff;
  --danger: #ff3b30;
  --ok: #248a3d;
  --c-kcal: #ff2d55;
  --c-protein: #34c759;
  --c-fat: #ff9500;
  --c-carbs: #32ade6;
  --radius: 14px;
  --shadow: 0 1px 0 rgba(0, 0, 0, 0.02);
  --bar-bg: rgba(249, 249, 249, 0.86);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --card-raised: #2c2c2e;
    --label: #ffffff;
    --secondary: rgba(235, 235, 245, 0.6);
    --tertiary: rgba(235, 235, 245, 0.3);
    --separator: rgba(84, 84, 88, 0.55);
    --fill: rgba(118, 118, 128, 0.24);
    --fill-strong: rgba(118, 118, 128, 0.36);
    --tint: #0a84ff;
    --danger: #ff453a;
    --ok: #30d158;
    --c-kcal: #ff375f;
    --c-protein: #30d158;
    --c-fat: #ff9f0a;
    --c-carbs: #64d2ff;
    --bar-bg: rgba(29, 29, 31, 0.86);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--label);
  font: 17px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  letter-spacing: -0.01em;
  min-height: 100vh;
  overscroll-behavior-y: none;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
input { border: 0; outline: none; background: none; min-width: 0; }
input::placeholder { color: var(--tertiary); }

.muted { color: var(--secondary); }
.error { color: var(--danger); font-size: 15px; margin: 10px 4px 0; }

.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: 1px; }
.c-kcal { background: var(--c-kcal); }
.c-protein { background: var(--c-protein); }
.c-fat { background: var(--c-fat); }
.c-carbs { background: var(--c-carbs); }

/* ---------------------------------------------------------------- Login */
.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
}
.login-card { width: 100%; max-width: 380px; text-align: center; }
.login-icon { border-radius: 20px; display: block; margin: 0 auto 14px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15); }
.login-card h1 { font-size: 30px; margin: 0; letter-spacing: -0.02em; }
.login-card .muted { margin: 4px 0 22px; }
.login-card .group { text-align: left; }

.btn-primary {
  width: 100%;
  margin-top: 18px;
  height: 50px;
  border-radius: 12px;
  background: var(--tint);
  color: #fff;
  font-weight: 600;
  transition: opacity 0.15s;
}
.btn-primary:disabled { opacity: 0.5; }
.btn-primary:active { opacity: 0.75; }

/* ---------------------------------------------- iOS-Gruppen (Formulare) */
.group {
  background: var(--card);
  border-radius: 12px;
  overflow: hidden;
}
.field {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 46px;
  padding: 0 16px;
  position: relative;
}
.field + .field::before {
  content: "";
  position: absolute;
  top: 0; left: 16px; right: 0;
  border-top: 0.5px solid var(--separator);
}
.field > span { flex: 0 0 auto; min-width: 104px; }
.field > input { flex: 1; text-align: right; font-size: 17px; padding: 12px 0; }
.field > em { font-style: normal; color: var(--secondary); min-width: 30px; }
.group-label {
  font-size: 13px;
  text-transform: uppercase;
  color: var(--secondary);
  margin: 22px 16px 7px;
  letter-spacing: 0;
}

/* ------------------------------------------------------------------ App */
#app-view {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(8px + var(--safe-top)) 16px calc(110px + var(--safe-bottom));
}

.topbar { display: flex; align-items: flex-end; justify-content: space-between; padding: 10px 4px 12px; }
.eyebrow { margin: 0; font-size: 13px; font-weight: 600; text-transform: uppercase; color: var(--secondary); letter-spacing: 0.02em; }
.large-title { margin: 0; font-size: 34px; line-height: 1.15; font-weight: 700; letter-spacing: -0.025em; }
.topbar-actions { display: flex; align-items: center; gap: 10px; padding-bottom: 4px; }
.save-state { font-size: 13px; color: var(--secondary); transition: opacity 0.3s; }
.save-state.err { color: var(--danger); }

.icon-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  display: grid; place-items: center;
  color: var(--tint);
  background: var(--fill);
}
.icon-btn:active { opacity: 0.6; }
.icon-btn.tinted { background: var(--tint); color: #fff; width: 30px; height: 30px; }
.text-btn { color: var(--tint); font-size: 17px; padding: 6px 2px; }
.text-btn.strong { font-weight: 600; }
.text-btn:active { opacity: 0.5; }
.text-btn:disabled { opacity: 0.35; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.section-title { font-size: 22px; font-weight: 700; margin: 26px 4px 10px; letter-spacing: -0.02em; }
.section-head { display: flex; align-items: flex-end; justify-content: space-between; }
.section-head .section-title { margin-bottom: 10px; }
.section-actions { display: flex; align-items: center; gap: 14px; margin-bottom: 10px; }

/* --------------------------------------------------------------- Ringe */
.rings-card {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 18px;
}
.rings { width: 132px; height: 132px; flex: 0 0 auto; overflow: visible; }
.rings circle { fill: none; stroke-linecap: round; transition: stroke-dashoffset 0.5s cubic-bezier(.2, .8, .2, 1), opacity 0.3s; }
.rings [data-key="kcal"] { stroke: var(--c-kcal); }
.rings [data-key="protein"] { stroke: var(--c-protein); }
.rings [data-key="fat"] { stroke: var(--c-fat); }
.rings [data-key="carbs"] { stroke: var(--c-carbs); }
.rings .track { opacity: 0.18; }
.rings .sel { opacity: 0.45; }

.legend { list-style: none; margin: 0; padding: 0; flex: 1; min-width: 0; display: grid; gap: 8px; }
.legend li { display: grid; grid-template-columns: auto 1fr; column-gap: 8px; align-items: baseline; }
.legend .lbl { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.02em; }
.legend .val { text-align: right; font-variant-numeric: tabular-nums; font-size: 15px; white-space: nowrap; }
.legend .val small { color: var(--secondary); font-size: 13px; }
.legend .sub { grid-column: 1 / -1; text-align: right; font-size: 12px; color: var(--secondary); font-variant-numeric: tabular-nums; min-height: 0; }
.legend .over { color: var(--danger); }

/* ----------------------------------------------------------- Übersicht */
.grid-card { padding: 12px 10px 10px; }
.grid {
  display: grid;
  grid-template-columns: minmax(62px, auto) repeat(4, minmax(0, 1fr));
  gap: 6px 6px;
  align-items: center;
}
.gh { font-size: 12px; font-weight: 600; color: var(--secondary); text-align: center; text-transform: uppercase; letter-spacing: 0.01em; white-space: nowrap; padding-bottom: 2px; }
.gh .dot { width: 6px; height: 6px; margin-right: 3px; }
.rl { font-size: 15px; font-weight: 600; padding-left: 4px; }

.cell-input, .cell-out {
  display: block;
  width: 100%;
  height: 38px;
  border-radius: 9px;
  text-align: center;
  font-size: 16px; /* >= 16px verhindert Auto-Zoom in iOS Safari */
  font-variant-numeric: tabular-nums;
  line-height: 38px;
}
.cell-input { background: var(--fill); transition: background 0.15s, box-shadow 0.15s; }
.cell-input:focus { background: var(--card); box-shadow: inset 0 0 0 2px var(--tint); }
.cell-input.invalid { box-shadow: inset 0 0 0 2px var(--danger); }
.cell-out { color: var(--label); font-weight: 600; }
.cell-out.neg { color: var(--danger); }
.cell-out.sel { color: var(--tint); }
.cell-out.zero { color: var(--tertiary); font-weight: 500; }
.hint { margin: 10px 4px 0; font-size: 12px; color: var(--secondary); }

/* --------------------------------------------------------------- Suche */
.search {
  display: flex; align-items: center; gap: 6px;
  background: var(--fill);
  border-radius: 10px;
  padding: 0 10px;
  height: 38px;
  color: var(--secondary);
  margin-bottom: 12px;
}
.search input { flex: 1; color: var(--label); font-size: 17px; height: 100%; }
.search input::-webkit-search-cancel-button { -webkit-appearance: none; }

/* ------------------------------------------------------- Lebensmittel */
.food-list { list-style: none; margin: 0; padding: 0; overflow: hidden; }
.food {
  position: relative;
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 12px;
  padding: 11px 14px 11px 14px;
  min-height: 60px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: background 0.15s;
}
.food:active { background: var(--fill); }
.food + .food::before {
  content: "";
  position: absolute;
  top: 0; left: 54px; right: 0;
  border-top: 0.5px solid var(--separator);
}
.check {
  width: 24px; height: 24px;
  border-radius: 50%;
  border: 1.6px solid var(--tertiary);
  display: grid; place-items: center;
  transition: background 0.15s, border-color 0.15s;
}
.check svg { opacity: 0; transform: scale(0.6); transition: opacity 0.15s, transform 0.15s; }
.food.selected .check { background: var(--tint); border-color: var(--tint); }
.food.selected .check svg { opacity: 1; transform: scale(1); }

.food-main { min-width: 0; }
.food-name { font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.food-meta { font-size: 13px; color: var(--secondary); margin-top: 1px; font-variant-numeric: tabular-nums; }
.food-calc { font-size: 13px; color: var(--tint); margin-top: 2px; font-weight: 500; font-variant-numeric: tabular-nums; }
.food:not(.selected) .food-calc { display: none; }

.amount {
  display: flex; align-items: center;
  background: var(--fill);
  border-radius: 9px;
  height: 36px;
  padding: 0 10px 0 4px;
  cursor: text;
}
.amount input {
  width: 58px;
  text-align: right;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
  height: 100%;
  padding-right: 4px;
}
.amount span { font-size: 15px; color: var(--secondary); max-width: 64px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.food.selected .amount { background: color-mix(in srgb, var(--tint) 14%, transparent); }
.amount:focus-within { box-shadow: inset 0 0 0 2px var(--tint); background: var(--card); }

.lead { display: grid; place-items: center; }
.chevron, .edit-ico { display: none; }
.chevron { color: var(--tertiary); }
.edit-ico { color: var(--tint); }
.editing .food .check, .editing .food .amount { display: none; }
.editing .food .edit-ico, .editing .food .chevron { display: block; }
.editing .food .food-calc { display: none; }

.empty { text-align: center; padding: 18px; }

/* ------------------------------------------------------ Auswahl-Leiste */
.selbar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(12px + var(--safe-bottom));
  width: calc(100% - 24px);
  max-width: 616px;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  border-radius: 18px;
  background: var(--bar-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18), 0 0 0 0.5px var(--separator);
  animation: rise 0.25s cubic-bezier(.2, .8, .2, 1);
  z-index: 10;
}
.selbar-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.selbar-text strong { font-size: 13px; color: var(--secondary); font-weight: 600; }
.selbar-text span { font-size: 15px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@keyframes rise { from { transform: translate(-50%, 30px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

/* --------------------------------------------------------------- Sheet */
.sheet-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 20;
  animation: fade 0.25s ease;
}
.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  margin: 0 auto;
  max-width: 640px;
  max-height: calc(100vh - 40px - var(--safe-top));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
  border-radius: 14px 14px 0 0;
  padding: 0 16px calc(24px + var(--safe-bottom));
  z-index: 21;
  animation: slide 0.32s cubic-bezier(.2, .8, .2, 1);
}
.sheet-grabber { width: 36px; height: 5px; border-radius: 3px; background: var(--tertiary); margin: 6px auto 0; }
.sheet-header {
  position: sticky; top: 0;
  display: grid; grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 8px 0 12px;
  background: var(--bg);
  z-index: 1;
}
.sheet-header h3 { margin: 0; font-size: 17px; font-weight: 600; }
.sheet-header .text-btn:first-child { justify-self: start; }
.sheet-header .text-btn:last-child { justify-self: end; }
.sheet-body .group { margin-top: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 2px 0; }
.chip {
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--fill);
  font-size: 14px;
  color: var(--label);
}
.chip:active { background: var(--fill-strong); }
.btn-destructive {
  width: 100%;
  margin-top: 26px;
  height: 48px;
  border-radius: 12px;
  background: var(--card);
  color: var(--danger);
  font-weight: 500;
}
.btn-destructive:active { opacity: 0.6; }
@keyframes slide { from { transform: translateY(100%); } to { transform: translateY(0); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* --------------------------------------------------------------- Toast */
.toast {
  position: fixed;
  top: calc(12px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 14px;
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
  font-size: 15px;
  z-index: 30;
  animation: fade 0.2s ease;
}
.toast.err { color: var(--danger); }

@media (max-width: 380px) {
  .rings { width: 112px; height: 112px; }
  .rings-card { gap: 14px; padding: 16px 14px; }
  .grid { grid-template-columns: 56px repeat(4, minmax(0, 1fr)); gap: 6px 4px; }
  .rl { font-size: 14px; padding-left: 2px; }
  .cell-input, .cell-out { font-size: 16px; }
  .amount input { width: 48px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
