:root {
  --bg: #f5f7f4;
  --surface: #ffffff;
  --surface-2: #eef1ec;
  --line: #dfe4dc;
  --text: #17201b;
  --muted: #66716a;
  --accent: #2f9e5b;
  --accent-ink: #ffffff;
  --accent-soft: #dcf1e3;
  --danger: #c8412f;
  --kcal: #2f9e5b;
  --protein: #d9703a;
  --carbs: #3a7fd9;
  --fat: #c9a227;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 4px 16px rgb(0 0 0 / .04);
  --tabs-h: 64px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1412;
    --surface: #18201c;
    --surface-2: #212b26;
    --line: #2b3630;
    --text: #e9efe9;
    --muted: #93a098;
    --accent: #4cc47c;
    --accent-ink: #08140d;
    --accent-soft: #1d3527;
    --danger: #ef6a57;
    --kcal: #4cc47c;
    --protein: #f08b52;
    --carbs: #5b9cf0;
    --fat: #e2bd45;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
main {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 16px) 16px calc(var(--tabs-h) + env(safe-area-inset-bottom) + 24px);
}
h1 { font-size: 1.6rem; margin: 4px 0 16px; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 24px 0 10px; }
h3 { font-size: .95rem; margin: 0 0 8px; }
p { margin: 0 0 10px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.boot { padding: 40vh 0; text-align: center; color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }

/* ---------- cards & layout ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.row { display: flex; gap: 10px; align-items: center; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }

/* ---------- forms ---------- */
label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 12px;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 84px; }
input[type="number"] { -moz-appearance: textfield; }
input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-weight: 600;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 12px;
  padding: 11px 16px;
  cursor: pointer;
  text-decoration: none;
  min-height: 44px;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .55; pointer-events: none; }
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  display: inline-grid; place-items: center; cursor: pointer; font-size: 1.2rem;
}
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button {
  flex: 1; font: inherit; font-size: .9rem; border: 0; background: transparent; color: var(--muted);
  padding: 8px 6px; border-radius: 9px; cursor: pointer;
}
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.err { color: var(--danger); font-size: .9rem; min-height: 1.2em; }

/* ---------- auth ---------- */
.auth { max-width: 380px; margin: 10vh auto 0; }
.logo { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.3rem; margin-bottom: 24px; }
.logo img { width: 44px; height: 44px; }

/* ---------- today ---------- */
.daynav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.daynav .title { text-align: center; }
.daynav .title strong { display: block; font-size: 1.15rem; }
.summary { display: grid; grid-template-columns: 150px 1fr; gap: 18px; align-items: center; }
.ring { position: relative; width: 150px; height: 150px; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring .track { stroke: var(--surface-2); }
.ring .bar { stroke: var(--kcal); transition: stroke-dashoffset .5s ease; }
.ring.over .bar { stroke: var(--danger); }
.ring .center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring .center strong { font-size: 1.7rem; line-height: 1.1; }
.ring .center span { font-size: .78rem; color: var(--muted); }
.macro + .macro { margin-top: 12px; }
.macro .row { font-size: .88rem; }
.macro .meter { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.macro .meter i { display: block; height: 100%; border-radius: 99px; width: 0; transition: width .5s ease; }
.m-protein i { background: var(--protein); }
.m-carbs i { background: var(--carbs); }
.m-fat i { background: var(--fat); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; }
.dot.protein { background: var(--protein); } .dot.carbs { background: var(--carbs); } .dot.fat { background: var(--fat); }

.meal-head { display: flex; justify-content: space-between; align-items: baseline; margin: 20px 4px 8px; }
.meal-head h2 { margin: 0; }
.entry {
  display: flex; align-items: center; gap: 12px; width: 100%;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 12px 14px; margin-bottom: 8px; text-align: left; font: inherit; color: inherit; cursor: pointer;
}
.entry .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .macros { font-size: .8rem; color: var(--muted); }
.entry .kcal { font-weight: 700; white-space: nowrap; }
.empty { color: var(--muted); font-size: .9rem; padding: 4px 4px 8px; }

/* ---------- add ---------- */
.capture { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.capture .btn { min-height: 92px; flex-direction: column; font-size: .95rem; }
.capture .btn svg { width: 28px; height: 28px; }
.preview { position: relative; border-radius: var(--radius); overflow: hidden; background: var(--surface-2); }
.preview img { display: block; width: 100%; max-height: 320px; object-fit: cover; }
.preview .icon-btn { position: absolute; top: 10px; right: 10px; background: rgb(0 0 0 / .55); color: #fff; border: 0; }
.textline { position: relative; }
.textline textarea { padding-right: 56px; }
.mic { position: absolute; right: 8px; top: 8px; }
.mic.on { background: var(--danger); color: #fff; border-color: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 8px rgb(200 65 47 / .15); } }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.chip {
  flex: none; font: inherit; font-size: .85rem; border: 1px solid var(--line); background: var(--surface);
  color: var(--text); border-radius: 99px; padding: 7px 12px; cursor: pointer; white-space: nowrap;
}
.spinner {
  width: 22px; height: 22px; border-radius: 50%;
  border: 3px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* editor */
.totals { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; }
.totals div { background: var(--surface-2); border-radius: 12px; padding: 8px 4px; }
.totals strong { display: block; font-size: 1.15rem; }
.totals span { font-size: .75rem; color: var(--muted); }
.item { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 12px; }
.item .grid4 input { padding: 9px 8px; text-align: center; }
.item .grid4 label { text-align: center; font-size: .75rem; }
.badge { display: inline-block; font-size: .75rem; padding: 3px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.badge.low { background: rgb(200 65 47 / .12); color: var(--danger); }

/* ---------- charts ---------- */
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .goal { stroke: var(--muted); stroke-dasharray: 4 4; stroke-width: 1; }
.chart .barv { fill: var(--kcal); }
.chart .barv.over { fill: var(--danger); }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .trend { fill: none; stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 3 3; }
.chart .pt { fill: var(--surface); stroke: var(--accent); stroke-width: 2; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.stats strong { display: block; font-size: 1.2rem; }
.stats span { font-size: .78rem; color: var(--muted); }
.list-row { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.list-row:first-child { border-top: 0; }

/* ---------- tabs ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around; align-items: center;
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabs a {
  flex: 1; max-width: 120px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .7rem;
}
.tabs a svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs a.on { color: var(--accent); }
.tabs .tab-add {
  flex: none; width: 54px; height: 54px; border-radius: 50%; background: var(--accent); color: var(--accent-ink);
  justify-content: center; margin-top: -18px; box-shadow: 0 6px 18px rgb(47 158 91 / .35);
}
.tabs .tab-add.on { color: var(--accent-ink); }
.tabs .tab-add svg { width: 28px; height: 28px; stroke-width: 2.4; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%); z-index: 20;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: .9rem;
  max-width: calc(100% - 32px); box-shadow: 0 6px 20px rgb(0 0 0 / .2);
}
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 380px) {
  .summary { grid-template-columns: 1fr; justify-items: center; }
  .summary .macros-col { width: 100%; }
}
.item:first-child { border-top: 0; padding-top: 0; margin-top: 0; }
.item .amount { width: 96px; flex: none; }
.daynav .title { flex-direction: column; gap: 0; font-weight: 400; }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn[disabled] { opacity: .35; }
