:root {
  --accent: #16a34a;
  --accent-dark: #15803d;
  --accent-soft: #dcfce7;
  --carb: #38bdf8;
  --protein: #a855f7;
  --fat: #f59e0b;

  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e4e8ee;
  --text: #10161f;
  --muted: #6b7684;
  --danger: #dc2626;

  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(16, 22, 31, .06), 0 6px 20px rgba(16, 22, 31, .06);
  --tabbar-h: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --surface: #161b22;
    --surface-2: #1c232c;
    --border: #262d38;
    --text: #e8edf3;
    --muted: #939eab;
    --accent-soft: #16351f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior-y: none;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ------------------------------------------------------------------ layout */
#app { display: flex; flex-direction: column; min-height: 100%; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(var(--safe-t) + 10px) 14px 10px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: -.02em; }
.topbar-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

.view {
  flex: 1;
  padding: 12px 12px calc(var(--tabbar-h) + var(--safe-b) + 24px);
  max-width: 780px; width: 100%; margin: 0 auto;
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
}
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); font-size: 10.5px; font-weight: 600; letter-spacing: .01em;
}
.tab.active { color: var(--accent); }
.tab svg { width: 21px; height: 21px; }
.tab-scan { position: relative; }
.scan-fab {
  position: absolute; top: -20px;
  display: grid; place-items: center;
  width: 54px; height: 54px; border-radius: 50%;
  background: linear-gradient(160deg, var(--accent), var(--accent-dark));
  color: #fff; box-shadow: 0 6px 18px rgba(22, 163, 74, .42);
}
.scan-fab svg { width: 25px; height: 25px; stroke-width: 2; }

/* ------------------------------------------------------------------- cards */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  margin-bottom: 12px; overflow: hidden;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
}
.card-head h2 { margin: 0; font-size: 14px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase; color: var(--muted); }
.card-pad { padding: 14px; }

/* --------------------------------------------------------- calorie summary */
.summary { padding: 16px 14px 6px; }
.summary-math {
  display: grid; grid-template-columns: 1fr auto 1fr auto 1fr;
  align-items: center; gap: 6px; text-align: center;
}
.summary-math .op { color: var(--muted); font-size: 16px; padding-bottom: 14px; }
.summary-math b { display: block; font-size: 19px; font-weight: 700; letter-spacing: -.02em; }
.summary-math span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.summary-math .remaining b { color: var(--accent); }
.summary-math .over b { color: var(--danger); }

.kcal-bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin: 14px 0 4px; border: 1px solid var(--border); }
.kcal-bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), #22c55e); transition: width .35s ease; }
.kcal-bar.over i { background: linear-gradient(90deg, #f59e0b, var(--danger)); }

.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; padding: 10px 14px 16px; }
.macro { text-align: center; }
.macro .track { height: 5px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-bottom: 6px; border: 1px solid var(--border); }
.macro .track i { display: block; height: 100%; transition: width .35s ease; }
.macro.carbs .track i { background: var(--carb); }
.macro.protein .track i { background: var(--protein); }
.macro.fat .track i { background: var(--fat); }
.macro .val { font-size: 13px; font-weight: 700; }
.macro .lbl { font-size: 11px; color: var(--muted); }

/* -------------------------------------------------------------- list items */
.row {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 14px; width: 100%; text-align: left;
  border-bottom: 1px solid var(--border); background: none;
}
.row:last-child { border-bottom: none; }
.row:active { background: var(--surface-2); }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.row-kcal { font-weight: 700; font-size: 14.5px; white-space: nowrap; }
.row-kcal small { display: block; font-weight: 500; font-size: 10.5px; color: var(--muted); text-align: right; }
.thumb { width: 40px; height: 40px; border-radius: 9px; object-fit: cover; background: var(--surface-2); border: 1px solid var(--border); flex: none; }
.thumb-ph { display: grid; place-items: center; font-size: 15px; font-weight: 700; color: var(--muted); }

.add-row { display: flex; align-items: center; gap: 8px; padding: 11px 14px; color: var(--accent); font-weight: 650; font-size: 14px; width: 100%; }
.add-row svg { width: 18px; height: 18px; stroke-width: 2.2; }

.meal-total { font-size: 12px; color: var(--muted); font-weight: 600; }

.empty { padding: 28px 18px; text-align: center; color: var(--muted); }
.empty p { margin: 6px 0 0; font-size: 13.5px; }
.empty strong { display: block; font-size: 15px; color: var(--text); }

/* ----------------------------------------------------------------- buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 13px 18px; border-radius: 12px; font-weight: 650; font-size: 15px;
  background: var(--accent); color: #fff; width: 100%;
}
.btn:active { background: var(--accent-dark); }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--border); }
.btn:disabled { opacity: .5; }
.btn-row { display: flex; gap: 10px; margin-top: 14px; }

.icon-btn {
  display: grid; place-items: center; width: 38px; height: 38px;
  border-radius: 50%; background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  font-size: 20px; line-height: 1; flex: none;
}
.ghost-btn { background: rgba(255, 255, 255, .14); color: #fff; padding: 10px 16px; border-radius: 99px; font-size: 14px; font-weight: 600; }
.link-btn { color: var(--accent); font-weight: 650; font-size: 14px; padding: 4px; }

.chip-row { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: none; }
.chip-row::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 7px 14px; border-radius: 99px; font-size: 13px; font-weight: 600;
  background: var(--surface); border: 1px solid var(--border); color: var(--muted);
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ------------------------------------------------------------------- forms */
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; font-weight: 650; color: var(--muted); margin-bottom: 5px; text-transform: uppercase; letter-spacing: .03em; }
.input, select.input, textarea.input {
  width: 100%; padding: 12px 13px; border-radius: 11px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  appearance: none; outline: none;
}
.input:focus { border-color: var(--accent); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.hint { font-size: 12px; color: var(--muted); margin: -4px 0 12px; }

.search-bar { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.search-bar .input { flex: 1; }

/* ------------------------------------------------------------------ sheets */
.sheet-root { position: fixed; inset: 0; z-index: 60; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(3, 7, 12, .5); animation: fade .18s ease; }
.sheet {
  position: relative; width: 100%; max-width: 780px;
  max-height: 92vh; display: flex; flex-direction: column;
  background: var(--bg); border-radius: 22px 22px 0 0;
  box-shadow: 0 -8px 40px rgba(0, 0, 0, .3);
  animation: slide-up .24s cubic-bezier(.32, .72, 0, 1);
  padding-bottom: var(--safe-b);
}
.sheet-grabber { display: block; width: 100%; padding: 12px 0 6px; }
.sheet-grabber::before { content: ""; display: block; width: 38px; height: 4px; border-radius: 99px; background: var(--border); margin: 0 auto; }
.sheet-body { overflow-y: auto; padding: 4px 14px 18px; -webkit-overflow-scrolling: touch; }
.sheet-title { margin: 2px 0 14px; font-size: 19px; font-weight: 700; letter-spacing: -.02em; }
.sheet-title small { display: block; font-size: 13px; font-weight: 500; color: var(--muted); margin-top: 2px; }

@keyframes slide-up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

/* --------------------------------------------------------- nutrition facts */
.facts { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); overflow: hidden; }
.facts-row { display: flex; justify-content: space-between; padding: 9px 14px; border-bottom: 1px solid var(--border); font-size: 14px; }
.facts-row:last-child { border-bottom: none; }
.facts-row.major { font-weight: 700; }
.facts-row.sub { padding-left: 30px; color: var(--muted); font-size: 13px; }
.facts-row .pct { color: var(--muted); font-size: 12.5px; }

/* ----------------------------------------------------------------- scanner */
.scanner { position: fixed; inset: 0; z-index: 80; background: #000; }
.scanner video { width: 100%; height: 100%; object-fit: cover; }
.scanner-ui { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; }
.scanner-top { width: 100%; display: flex; justify-content: space-between; padding: calc(var(--safe-t) + 12px) 14px 0; }
.scanner-top .icon-btn { background: rgba(255, 255, 255, .18); border-color: transparent; color: #fff; }
.reticle {
  margin: auto; width: min(78vw, 330px); height: 190px; position: relative;
  border-radius: 18px; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .5);
  outline: 2px solid rgba(255, 255, 255, .85); outline-offset: -2px;
}
.reticle-line { position: absolute; left: 8%; right: 8%; height: 2px; background: #22c55e; box-shadow: 0 0 12px #22c55e; animation: sweep 2s ease-in-out infinite; }
@keyframes sweep { 0%, 100% { top: 12%; } 50% { top: 88%; } }
.scanner-status { color: #fff; font-size: 14.5px; font-weight: 600; text-align: center; padding: 0 24px; margin: 0 0 16px; text-shadow: 0 1px 6px rgba(0, 0, 0, .8); }
.scanner-ui .ghost-btn { margin-bottom: calc(var(--safe-b) + 26px); }

/* ------------------------------------------------------------------- toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); z-index: 90;
  background: #10161f; color: #fff; padding: 11px 18px; border-radius: 99px;
  font-size: 14px; font-weight: 600; box-shadow: 0 8px 26px rgba(0, 0, 0, .3);
  animation: fade .2s ease; max-width: 88vw; text-align: center;
}
.toast.error { background: var(--danger); }

/* ------------------------------------------------------------------ trends */
.bars { display: flex; align-items: flex-end; gap: 6px; height: 130px; padding: 10px 14px 0; }
.bars .bar { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; }
.bars .bar i { display: block; background: var(--accent); border-radius: 5px 5px 0 0; min-height: 2px; transition: height .3s ease; }
.bars .bar.over i { background: var(--fat); }
.bars .bar.empty i { background: var(--border); }
.bar-labels { display: flex; gap: 6px; padding: 6px 14px 14px; }
.bar-labels span { flex: 1; text-align: center; font-size: 10.5px; color: var(--muted); }

.spinner { width: 22px; height: 22px; border: 2.5px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; margin: 28px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

.badge { font-size: 10.5px; font-weight: 700; padding: 2px 7px; border-radius: 99px; background: var(--accent-soft); color: var(--accent-dark); text-transform: uppercase; letter-spacing: .03em; }
@media (prefers-color-scheme: dark) { .badge { color: #4ade80; } }
