:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #15191e;
  --muted: #6b7380;
  --line: #e3e6ea;
  --accent: #1f8a5b;
  --accent-text: #ffffff;
  --prot: #3b6fd8;
  --over: #d2462f;
  --chip: #e9ecef;
  --shadow: 0 6px 24px rgba(0, 0, 0, .12);
  --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: #101418;
    --card: #1a2027;
    --text: #eef1f4;
    --muted: #8d97a3;
    --line: #2a323b;
    --accent: #2fb67a;
    --accent-text: #06140d;
    --prot: #5b8df0;
    --over: #f06a52;
    --chip: #242c35;
    --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  padding-bottom: calc(96px + var(--safe-b));
  overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; color: inherit; }

/* header */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 4px;
  padding: calc(8px + var(--safe-t)) 8px 8px;
  background: var(--bg);
  max-width: 560px; margin: 0 auto;
}
.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 12px;
  background: transparent; font-size: 28px; line-height: 1;
  display: grid; place-items: center; flex: none;
}
.icon-btn:active { background: var(--chip); }
.icon-btn:disabled { opacity: .25; }
.day-title {
  flex: 1; border: 0; background: transparent; font-weight: 650; font-size: 18px;
  height: 44px; border-radius: 12px;
}
.gear { margin-left: 4px; color: var(--muted); }

main { max-width: 560px; margin: 0 auto; padding: 0 16px; }

/* summary */
.summary {
  background: var(--card); border-radius: 18px; padding: 16px 16px 12px;
  display: grid; gap: 14px;
}
.metric-row { display: flex; align-items: baseline; gap: 6px; }
.big { font-size: 34px; font-weight: 750; letter-spacing: -.5px; font-variant-numeric: tabular-nums; }
.metric:nth-child(2) .big { font-size: 26px; }
.unit { color: var(--muted); font-weight: 550; }
.left { margin-left: auto; font-weight: 650; font-variant-numeric: tabular-nums; }
.left.over { color: var(--over); }
.left.done { color: var(--prot); }
.bar { height: 10px; background: var(--chip); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.fill { height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .35s ease; }
.bar.prot .fill { background: var(--prot); }
.bar.over .fill { background: var(--over); }
.goal { color: var(--muted); font-size: 13px; margin-top: 4px; font-variant-numeric: tabular-nums; }

.past-banner {
  margin-top: 10px; padding: 8px 12px; border-radius: 12px;
  background: color-mix(in srgb, var(--prot) 16%, transparent); font-size: 14px;
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
}
.past-banner button { margin-left: auto; border: 0; background: none; color: var(--prot); font-weight: 650; padding: 4px; }

/* recent chips */
.recent { margin: 14px -16px 0; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 0 16px 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 0; background: var(--chip); border-radius: 99px;
  padding: 9px 14px; font-size: 14px; max-width: 230px;
  display: flex; gap: 6px; white-space: nowrap;
}
.chip span { overflow: hidden; text-overflow: ellipsis; }
.chip small { color: var(--muted); flex: none; font-size: 13px; line-height: 1.5; }
.chip:active { filter: brightness(.9); }

/* entries */
.entries { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 8px; }
.entry-wrap { position: relative; border-radius: 14px; overflow: hidden; background: var(--over); }
.entry-wrap::after {
  content: "Delete"; position: absolute; right: 16px; top: 50%; transform: translateY(-50%);
  color: #fff; font-weight: 650; font-size: 14px;
}
.entry {
  position: relative; z-index: 1; background: var(--card); padding: 12px 14px;
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; touch-action: pan-y;
  user-select: none; -webkit-user-select: none;
}
.entry.anim { transition: transform .2s ease; }
.entry .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .meta { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.entry .kcal { text-align: right; font-weight: 650; font-variant-numeric: tabular-nums; }
.entry .prot { text-align: right; color: var(--prot); font-size: 13px; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); text-align: center; margin-top: 32px; }

/* bottom bar */
.bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex; gap: 10px; padding: 10px 16px calc(10px + var(--safe-b));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
  max-width: 560px; margin: 0 auto;
}
.btn-scan {
  flex: 1.7; height: 64px; border: 0; border-radius: 18px;
  background: var(--accent); color: var(--accent-text);
  font-size: 21px; font-weight: 750;
  display: flex; align-items: center; justify-content: center; gap: 10px;
  box-shadow: var(--shadow);
}
.btn-scan:active { transform: scale(.98); }
.btn-secondary {
  flex: 1; height: 64px; border: 0; border-radius: 18px; min-width: 0;
  background: var(--card); font-weight: 650; font-size: 13px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
.btn-secondary .ico { font-size: 24px; line-height: 22px; font-weight: 500; }
.btn-secondary:active { transform: scale(.98); }

/* AI photo */
.thumbs { display: flex; gap: 8px; margin: 0 0 14px; overflow-x: auto; }
.thumbs img { height: 72px; border-radius: 10px; flex: none; }
.ai-wait { display: flex; align-items: center; gap: 12px; margin: 6px 0 18px; font-weight: 600; }
.spinner {
  width: 26px; height: 26px; border-radius: 50%; flex: none;
  border: 3px solid var(--chip); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.ai-meta { color: var(--muted); font-size: 13px; margin: -6px 0 14px; }
.ai-meta .conf-low { color: var(--over); font-weight: 650; }
.tag {
  display: inline-block; font-size: 10px; font-weight: 750; letter-spacing: .4px;
  padding: 1px 5px; border-radius: 5px; margin-left: 6px; vertical-align: 1px;
  background: color-mix(in srgb, var(--prot) 20%, transparent); color: var(--prot);
}
.btn.ai { background: color-mix(in srgb, var(--prot) 18%, transparent); color: var(--prot); width: 100%; margin-bottom: 12px; flex: none; }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: 14px; margin: 0 0 14px; }
.check input { width: 20px; height: 20px; margin: 0; flex: none; accent-color: var(--accent); }
.ai-meta a { color: var(--prot); }
select.field-select {
  width: 100%; height: 48px; padding: 0 10px; font: inherit; font-size: 16px; color: inherit;
  border: 1px solid var(--line); border-radius: 12px; background: var(--bg);
}

/* toast */
.toast {
  position: fixed; left: 12px; right: 12px; z-index: 30;
  bottom: calc(86px + var(--safe-b));
  max-width: 536px; margin: 0 auto;
  background: #20262d; color: #f1f3f5; border-radius: 16px; padding: 12px 14px;
  box-shadow: var(--shadow);
  animation: toast-in .18s ease;
}
@keyframes toast-in { from { transform: translateY(12px); opacity: 0; } }
.toast-msg { font-size: 15px; }
.toast-msg b { font-weight: 650; }
.toast-actions { display: flex; gap: 8px; margin-top: 10px; }
.toast-actions:empty { display: none; }
.toast-actions button {
  flex: 1; height: 42px; border: 0; border-radius: 11px;
  background: #343c46; color: #fff; font-weight: 650;
}
.toast-actions button.primary { background: #2fb67a; color: #06140d; }

/* scanner */
.scanner { position: fixed; inset: 0; z-index: 40; background: #000; }
.scanner video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scan-frame {
  position: absolute; left: 8%; right: 8%; top: 38%; height: 22%;
  border: 3px solid rgba(255, 255, 255, .9); border-radius: 16px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .45);
}
.scan-line {
  position: absolute; left: 6%; right: 6%; top: 50%; height: 2px;
  background: #ff4d3a; box-shadow: 0 0 8px #ff4d3a;
}
.scanner.hit .scan-frame { border-color: #2fb67a; }
.scan-top {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + var(--safe-t)) 10px 10px;
}
.icon-btn.light { color: #fff; background: rgba(0, 0, 0, .4); font-size: 22px; }
.icon-btn.light.on { background: #ffd23f; color: #000; }
.scan-status { flex: 1; text-align: center; color: #fff; font-size: 15px; text-shadow: 0 1px 3px #000; }
.scan-bottom {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; justify-content: center;
  padding: 16px 16px calc(24px + var(--safe-b));
}
.btn-ghost {
  border: 1px solid rgba(255, 255, 255, .5); background: rgba(0, 0, 0, .4); color: #fff;
  border-radius: 99px; padding: 12px 22px; font-weight: 600;
}

/* sheet */
.sheet-backdrop { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .45); }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51;
  max-width: 560px; margin: 0 auto; max-height: 92dvh; overflow-y: auto;
  background: var(--card); border-radius: 22px 22px 0 0;
  padding: 18px 16px calc(18px + var(--safe-b));
  animation: sheet-in .2s ease;
}
@keyframes sheet-in { from { transform: translateY(40px); opacity: 0; } }
.sheet h2 { margin: 0 0 4px; font-size: 20px; }
.sheet .sub { color: var(--muted); font-size: 14px; margin: 0 0 14px; }
.sheet h3 { margin: 22px 0 8px; font-size: 15px; color: var(--muted); font-weight: 650; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; font-weight: 550; }
.field input {
  width: 100%; height: 48px; padding: 0 12px; font-size: 17px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--bg);
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 3px; height: 48px; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 9px; font-weight: 650; }
.seg button.on { background: var(--accent); color: var(--accent-text); }
.portions { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 12px; }
.portions button { border: 0; background: var(--chip); border-radius: 99px; padding: 9px 14px; font-size: 14px; font-weight: 600; }
.portions button.on { background: var(--accent); color: var(--accent-text); }
.preview { font-size: 17px; margin: 4px 0 16px; font-variant-numeric: tabular-nums; }
.preview b { font-weight: 700; }
.preview .p { color: var(--prot); }
.actions { display: flex; gap: 10px; margin-top: 6px; }
.btn {
  flex: 1; height: 52px; border: 0; border-radius: 14px; font-weight: 700; font-size: 16px;
  background: var(--chip);
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { background: transparent; color: var(--over); flex: 0 0 auto; padding: 0 14px; }
.list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.list li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list .li-main { flex: 1; min-width: 0; }
.list .li-main div:first-child { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .li-main div:last-child { color: var(--muted); font-size: 13px; }
.list button { border: 0; background: var(--chip); border-radius: 10px; padding: 8px 12px; font-size: 14px; font-weight: 600; }
.link-row { display: flex; gap: 10px; flex-wrap: wrap; }
.link-row .btn { flex: 1 1 140px; }
.note { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
