:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --ink: #17201b;
  --muted: #66736b;
  --line: #e2e8e4;
  --brand: #16a34a;
  --brand-ink: #ffffff;
  --brand-soft: #dcfce7;
  --danger: #dc2626;
  --warn: #ea580c;
  --protein: #2563eb;
  --carbs: #d97706;
  --fat: #db2777;
  --water: #0ea5e9;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1411;
    --card: #182019;
    --ink: #e8efe9;
    --muted: #93a198;
    --line: #26322a;
    --brand: #22c55e;
    --brand-ink: #062a12;
    --brand-soft: #143520;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-top: calc(56px + var(--safe-t));
  padding-bottom: calc(72px + var(--safe-b));
}
body.auth { padding: 0; min-height: 100vh; display: grid; place-items: center; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.sr-only { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* Auth */
.auth-card { width: min(400px, calc(100% - 32px)); background: var(--card); border-radius: var(--radius); padding: 28px 22px; box-shadow: var(--shadow); }
.auth-card h1 { font-size: 1.4rem; text-align: center; margin-bottom: 8px; }
.auth-card .muted { text-align: center; }
.auth-logo { display: flex; justify-content: center; margin-bottom: 10px; }

/* Forms */
.stack { display: grid; gap: 14px; }
label { display: grid; gap: 6px; font-size: .9rem; color: var(--muted); }
input, select, textarea {
  width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--bg); color: var(--ink); font-size: 16px; min-height: 46px;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: transparent; }
.row { display: flex; gap: 10px; align-items: center; }
.row > * { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.check { display: flex; align-items: center; gap: 10px; color: var(--ink); font-size: 1rem; }
.check input { width: 22px; height: 22px; min-height: 0; }
.alert { background: #fee2e2; color: #991b1b; padding: 10px 12px; border-radius: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 10px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--brand); padding: 6px 8px; min-height: 36px; }
.btn.block { width: 100%; }
.btn.sm { min-height: 36px; padding: 6px 12px; font-size: .9rem; }
.btn:disabled { opacity: .5; }

/* Top bar */
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 10;
  height: calc(56px + var(--safe-t)); padding: var(--safe-t) 8px 0;
  display: flex; align-items: center; justify-content: space-between;
  background: var(--card); border-bottom: 1px solid var(--line);
}
.topbar.hide-date .icon-btn { visibility: hidden; }
.topbar.hide-date .date-btn { cursor: default; }
.icon-btn { width: 44px; height: 44px; border: 0; background: none; font-size: 1.6rem; border-radius: 50%; cursor: pointer; color: var(--ink); }
.date-btn { border: 0; background: none; font-weight: 700; font-size: 1.05rem; padding: 8px 16px; border-radius: 999px; cursor: pointer; }
.date-btn:active, .icon-btn:active { background: var(--bg); }

/* Tab bar */
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding-bottom: var(--safe-b); background: var(--card); border-top: 1px solid var(--line);
}
.tabbar button {
  border: 0; background: none; padding: 8px 0 10px; font-size: .72rem; color: var(--muted);
  display: grid; justify-items: center; gap: 2px; cursor: pointer;
}
.tabbar button span { font-size: 1.35rem; filter: grayscale(1); opacity: .7; }
.tabbar button.active { color: var(--brand); font-weight: 700; }
.tabbar button.active span { filter: none; opacity: 1; }

.view { max-width: 640px; margin: 0 auto; padding: 14px 16px 24px; display: grid; gap: 14px; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; gap: 8px; }
.card-head h3 { font-size: 1.05rem; }

/* Summary ring */
.summary { display: grid; grid-template-columns: 132px 1fr; gap: 16px; align-items: center; }
.ring { position: relative; width: 132px; height: 132px; }
.ring svg { transform: rotate(-90deg); }
.ring .center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring .big { font-size: 1.6rem; font-weight: 800; line-height: 1; }
.ring.over .big { color: var(--danger); }
.kv { display: grid; gap: 4px; font-size: .92rem; }
.kv div { display: flex; justify-content: space-between; }
.kv b { font-variant-numeric: tabular-nums; }

.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 14px; }
.macro { font-size: .8rem; }
.macro .bar { height: 7px; border-radius: 99px; background: var(--line); overflow: hidden; margin: 4px 0 2px; }
.macro .bar i { display: block; height: 100%; border-radius: 99px; }
.macro b { font-size: .95rem; font-variant-numeric: tabular-nums; }

/* Water */
.water-row { display: flex; align-items: center; gap: 10px; }
.glasses { display: flex; flex-wrap: wrap; gap: 4px; flex: 1; }
.glass { width: 22px; height: 28px; border: 2px solid var(--water); border-top-width: 0; border-radius: 0 0 6px 6px; position: relative; overflow: hidden; }
.glass.full::after { content: ""; position: absolute; inset: 30% 0 0 0; background: var(--water); opacity: .75; }
.round { width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--bg); font-size: 1.3rem; font-weight: 700; cursor: pointer; flex: 0 0 auto; }
.round.primary { background: var(--water); color: #fff; border-color: var(--water); }

/* Meals */
.meal-total { font-weight: 700; font-variant-numeric: tabular-nums; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; }
.list li:first-child { border-top: 0; }
.list .grow { flex: 1; min-width: 0; }
.list .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .sub { font-size: .8rem; color: var(--muted); }
.list .kcal { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.list .star { border: 0; background: none; font-size: 1.2rem; cursor: pointer; padding: 4px; }
.empty { color: var(--muted); font-size: .9rem; padding: 6px 0; }
.meal-actions { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }

.fab {
  position: fixed; right: 18px; bottom: calc(86px + var(--safe-b)); z-index: 9;
  width: 60px; height: 60px; border-radius: 50%; border: 0; background: var(--brand); color: var(--brand-ink);
  font-size: 2rem; box-shadow: 0 6px 20px rgba(22,163,74,.4); cursor: pointer;
}

/* Sheet */
.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: calc(100% - 24px - var(--safe-t));
  display: flex; flex-direction: column;
  background: var(--card); border-radius: 20px 20px 0 0; animation: up .2s ease-out;
  max-width: 640px; margin: 0 auto;
}
@keyframes up { from { transform: translateY(40px); opacity: .5; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 8px 8px 18px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { font-size: 1.1rem; }
.sheet-body { overflow-y: auto; padding: 14px 16px calc(20px + var(--safe-b)); display: grid; gap: 12px; -webkit-overflow-scrolling: touch; }

.seg { display: flex; background: var(--bg); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px 4px; border-radius: 9px; font-size: .85rem; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--ink); font-weight: 700; box-shadow: var(--shadow); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 6px 12px; font-size: .85rem; cursor: pointer; }
.chip.on { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); font-weight: 700; }

.preview { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; background: var(--bg); border-radius: 12px; padding: 10px 4px; }
.preview b { display: block; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.preview span { font-size: .72rem; color: var(--muted); }
.big-input { font-size: 1.6rem; text-align: center; font-weight: 700; }

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

.toast {
  position: fixed; left: 50%; bottom: calc(92px + var(--safe-b)); transform: translateX(-50%); z-index: 60;
  background: #111; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: .9rem; max-width: calc(100% - 32px);
}
.toast.err { background: var(--danger); }

/* Charts */
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 10px; }
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.stat-row b { display: block; font-size: 1.2rem; font-variant-numeric: tabular-nums; }
.stat-row span { font-size: .75rem; color: var(--muted); }
.good { color: var(--brand); }
.bad { color: var(--danger); }
.spinner { width: 24px; height: 24px; border: 3px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; margin: 10px auto; }
@keyframes spin { to { transform: rotate(360deg); } }
.hint { background: var(--brand-soft); border-radius: 12px; padding: 12px 14px; font-size: .9rem; }

/* Target regulator */
.summary { cursor: pointer; }
.kcal-reg { display: flex; align-items: center; gap: 6px; }
.kcal-reg .round { width: 48px; font-size: .8rem; }
.kcal-val { flex: 1; min-width: 0; display: grid; justify-items: center; gap: 2px; }
.kcal-val input { font-size: 1.9rem; padding: 6px; min-height: 52px; }
input[type="range"] { padding: 0; min-height: 32px; border: 0; background: none; accent-color: var(--brand); }
.split-bar { display: flex; height: 12px; border-radius: 99px; overflow: hidden; background: var(--line); }
.split-bar i { display: block; height: 100%; transition: width .15s; }
.macro-row-head { display: flex; justify-content: space-between; align-items: center; font-size: .92rem; }
.macro-row-head b { font-variant-numeric: tabular-nums; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; }
.stepper input { text-align: center; font-size: 1.2rem; }
.stepper .round { flex: 0 0 auto; width: 52px; font-size: .9rem; }
.locked { opacity: .55; }
