:root {
  --bg: #F3F5F1;
  --surface: #FFFFFF;
  --surface-2: #E8ECE5;
  --surface-3: #DCE2D8;
  --ink: #1C2420;
  --ink-2: #56625B;
  --ink-3: #85918A;
  --line: #D5DCD2;
  --primary: #2D6A4F;
  --on-primary: #FFFFFF;
  --primary-soft: #D3EADC;
  --on-primary-soft: #133A29;
  --tomato: #C8432C;
  --tomato-soft: #F7DCD4;
  --protein: #3B6DB3;
  --carbs: #CF9220;
  --fat: #BF4D6B;
  --shadow: 0 1px 2px rgba(28, 36, 32, .06), 0 6px 20px rgba(28, 36, 32, .07);
  --font: "Figtree", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111613;
    --surface: #1A201C;
    --surface-2: #232B26;
    --surface-3: #2E3832;
    --ink: #E3E9E4;
    --ink-2: #A1ADA5;
    --ink-3: #74807A;
    --line: #2E3832;
    --primary: #84CDA7;
    --on-primary: #0B2A1B;
    --primary-soft: #1F4433;
    --on-primary-soft: #CFEBDB;
    --tomato: #F08A72;
    --tomato-soft: #4A231B;
    --protein: #86A9E0;
    --carbs: #E8B65A;
    --fat: #E58AA2;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font: 400 16px/1.45 var(--font);
  font-feature-settings: "tnum" 1;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--primary); }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
[hidden] { display: none !important; }

#view {
  max-width: 520px;
  margin: 0 auto;
  padding: calc(var(--safe-t) + 12px) 16px calc(var(--safe-b) + 108px);
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 8px;
  min-height: 48px; margin-bottom: 4px;
}
.topbar h1 { font-size: 22px; font-weight: 700; margin: 0; flex: 1; letter-spacing: -.01em; }
.daynav { display: flex; align-items: center; justify-content: space-between; flex: 1; }
.daynav h1 { text-align: center; }
.iconbtn {
  width: 44px; height: 44px; border-radius: 50%;
  border: 0; background: transparent; color: var(--ink-2);
  display: grid; place-items: center; flex: none;
}
.iconbtn:active { background: var(--surface-2); }
.iconbtn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.iconbtn:disabled { opacity: .3; }

/* ---------- the plate ---------- */
.plate-wrap { display: grid; place-items: center; padding: 8px 0 4px; }
.plate { width: min(72vw, 260px); aspect-ratio: 1; position: relative; }
.plate svg { width: 100%; height: 100%; display: block; }
.plate .rim { fill: var(--surface); }
.plate .well { fill: var(--surface-2); }
.plate .track { fill: none; stroke: var(--surface-3); stroke-width: 10; }
.plate .arc { fill: none; stroke: var(--primary); stroke-width: 10; stroke-linecap: round; transition: stroke-dashoffset .6s cubic-bezier(.2, .8, .2, 1); }
.plate.over .arc { stroke: var(--tomato); }
.plate-center {
  position: absolute; inset: 0; display: grid; place-content: center; text-align: center;
}
.plate-num { font-size: clamp(40px, 13vw, 54px); font-weight: 800; line-height: 1; letter-spacing: -.03em; }
.plate.over .plate-num { color: var(--tomato); }
.plate-label { color: var(--ink-2); font-size: 15px; margin-top: 6px; }
.plate-sub {
  display: flex; justify-content: center; gap: 28px;
  margin: 10px 0 18px; color: var(--ink-2); font-size: 14px; text-align: center;
}
.plate-sub b { display: block; color: var(--ink); font-size: 18px; font-weight: 700; }

/* ---------- macros ---------- */
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 22px; }
.macro { background: var(--surface); border-radius: 16px; padding: 12px 12px 14px; box-shadow: var(--shadow); }
.macro-name { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.macro-val { font-size: 17px; font-weight: 700; margin: 2px 0 8px; white-space: nowrap; }
.macro-val small { color: var(--ink-3); font-weight: 500; font-size: 13px; }
.bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 3px; background: currentColor; transition: width .6s cubic-bezier(.2, .8, .2, 1); }
.m-protein { color: var(--protein); }
.m-carbs { color: var(--carbs); }
.m-fat { color: var(--fat); }

/* ---------- activity ---------- */
.act { padding: 14px 16px; }
.act-row { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; }
.act-row b { display: block; font-size: 20px; font-weight: 700; }
.act-row span { font-size: 13px; color: var(--ink-2); }
.act-credit { color: var(--primary); }
.act .act-item { padding: 10px 2px; margin-top: 8px; }

/* ---------- meals ---------- */
.meal { background: var(--surface); border-radius: 20px; margin-bottom: 12px; box-shadow: var(--shadow); overflow: hidden; }
.meal-head { display: flex; align-items: center; padding: 6px 6px 6px 18px; min-height: 56px; }
.meal-head h2 { font-size: 17px; font-weight: 700; margin: 0; flex: 1; }
.meal-head .kcal { color: var(--ink-2); font-size: 15px; margin-right: 2px; }
.addbtn {
  width: 40px; height: 40px; border-radius: 12px; border: 0;
  background: var(--primary-soft); color: var(--on-primary-soft);
  display: grid; place-items: center; margin-left: 8px;
}
.addbtn svg { width: 22px; height: 22px; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; fill: none; }
.item {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 11px 18px; border: 0; border-top: 1px solid var(--line);
  background: transparent; text-align: left;
}
.item:active { background: var(--surface-2); }
.item-main { flex: 1; min-width: 0; }
.item-name, .item-meta { display: block; }
.item-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-meta { font-size: 13px; color: var(--ink-3); }
.item-kcal { font-weight: 600; white-space: nowrap; }
.ai-dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--protein); margin-left: 6px; vertical-align: middle; }

/* ---------- cards & forms ---------- */
.card { background: var(--surface); border-radius: 20px; padding: 18px; margin-bottom: 14px; box-shadow: var(--shadow); }
.card h2 { font-size: 17px; margin: 0 0 4px; }
.card p { margin: 0 0 12px; color: var(--ink-2); font-size: 15px; }
.callout { background: var(--primary-soft); color: var(--on-primary-soft); box-shadow: none; }
.callout p { color: inherit; opacity: .85; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); margin: 0 0 6px 2px; }
.input, select.input {
  width: 100%; height: 50px; padding: 0 14px;
  border-radius: 14px; border: 1.5px solid var(--line); background: var(--surface);
  font-size: 17px; appearance: none;
}
.input:focus { border-color: var(--primary); outline: none; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.with-unit { position: relative; }
.with-unit .input { padding-right: 46px; }
.with-unit em { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); font-style: normal; color: var(--ink-3); pointer-events: none; }

.btn {
  height: 52px; padding: 0 22px; border-radius: 26px; border: 0;
  background: var(--primary); color: var(--on-primary);
  font-weight: 700; font-size: 16px; width: 100%;
}
.btn:disabled { opacity: .5; }
.btn.tonal { background: var(--primary-soft); color: var(--on-primary-soft); }
.btn.text { background: transparent; color: var(--primary); }
.btn.danger { background: transparent; color: var(--tomato); }
.btn + .btn { margin-top: 8px; }

.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding: 2px 0 4px; margin-bottom: 12px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; height: 36px; padding: 0 14px; border-radius: 18px;
  border: 1.5px solid var(--line); background: transparent; color: var(--ink-2);
  font-size: 15px; font-weight: 600;
}
.chip[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }

.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  background: var(--surface-2); border-radius: 14px; padding: 4px; margin-bottom: 14px;
}
.seg button { height: 40px; border: 0; border-radius: 10px; background: transparent; font-weight: 600; color: var(--ink-2); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

.muted { color: var(--ink-2); }
.small { font-size: 13px; }
.center { text-align: center; }
.empty { text-align: center; color: var(--ink-2); padding: 28px 12px; }
.error { color: var(--tomato); font-size: 14px; margin: -4px 0 12px 2px; }

/* ---------- lists (search results) ---------- */
.list { background: var(--surface); border-radius: 20px; overflow: hidden; box-shadow: var(--shadow); }
.list .item:first-child { border-top: 0; }
.list-title { font-size: 14px; font-weight: 600; color: var(--ink-2); margin: 16px 4px 8px; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 2.5px solid var(--surface-3); border-top-color: var(--primary); animation: spin .8s linear infinite; margin: 14px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- scanner ---------- */
.scanner {
  position: relative; border-radius: 24px; overflow: hidden; background: #000;
  aspect-ratio: 3 / 4; max-height: 56vh; width: 100%; margin-bottom: 14px;
}
#reader { width: 100%; height: 100%; }
#reader video { width: 100% !important; height: 100% !important; object-fit: cover; }
#reader img, #qr-shaded-region { display: none !important; }
.scan-frame {
  position: absolute; left: 10%; right: 10%; top: 36%; height: 26%;
  border: 3px solid rgba(255, 255, 255, .9); border-radius: 18px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35); pointer-events: none;
}
.scan-hint { position: absolute; left: 0; right: 0; bottom: 18px; text-align: center; color: #fff; font-weight: 600; text-shadow: 0 1px 4px rgba(0, 0, 0, .5); }
.scan-msg { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; color: #fff; padding: 24px; }

/* ---------- weight ---------- */
.big { font-size: 48px; font-weight: 800; letter-spacing: -.03em; line-height: 1; }
.big small { font-size: 20px; font-weight: 600; color: var(--ink-2); letter-spacing: 0; }
.chart { width: 100%; height: 170px; display: block; margin-top: 8px; }
.chart .ln { fill: none; stroke: var(--primary); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .ar { fill: var(--primary); opacity: .1; }
.chart .gl { stroke: var(--ink-3); stroke-dasharray: 4 5; stroke-width: 1.5; }
.chart .pt { fill: var(--primary); }
.chart text { fill: var(--ink-3); font-size: 11px; font-family: var(--font); }
.progress { height: 10px; border-radius: 5px; background: var(--surface-2); overflow: hidden; margin: 12px 0 6px; }
.progress i { display: block; height: 100%; background: var(--primary); border-radius: 5px; }

.kv { display: grid; grid-template-columns: 1fr auto; row-gap: 8px; font-size: 15px; }
.kv dt { color: var(--ink-2); }
.kv dd { margin: 0; font-weight: 600; text-align: right; }
.token { font-family: ui-monospace, Menlo, monospace; font-size: 13px; word-break: break-all; background: var(--surface-2); padding: 12px; border-radius: 12px; margin-bottom: 10px; user-select: all; }

/* ---------- tab bar & fab ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: center; gap: 4px;
  padding: 8px 12px calc(var(--safe-b) + 8px);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; max-width: 120px; display: grid; justify-items: center; gap: 2px;
  text-decoration: none; color: var(--ink-3); font-size: 12px; font-weight: 600; padding: 4px 0;
}
.tabbar svg { width: 26px; height: 26px; padding: 2px 14px; box-sizing: content-box; border-radius: 15px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a[aria-current="page"] { color: var(--ink); }
.tabbar a[aria-current="page"] svg { background: var(--primary-soft); color: var(--on-primary-soft); }
.fab {
  position: fixed; right: max(16px, calc(50vw - 244px)); bottom: calc(var(--safe-b) + 84px); z-index: 21;
  width: 60px; height: 60px; border-radius: 20px; border: 0;
  background: var(--primary); color: var(--on-primary);
  display: grid; place-items: center; box-shadow: 0 6px 20px rgba(45, 106, 79, .35);
}
.fab svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.fab:active { transform: scale(.96); }

/* ---------- bottom sheet ---------- */
.scrim { position: fixed; inset: 0; background: rgba(10, 16, 12, .4); z-index: 40; animation: fade .2s ease; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-width: 560px; margin: 0 auto; max-height: 92dvh; overflow-y: auto;
  background: var(--surface); border-radius: 28px 28px 0 0;
  padding: 10px 20px calc(var(--safe-b) + 20px);
  animation: rise .28s cubic-bezier(.2, .8, .2, 1);
}
.sheet::before { content: ""; display: block; width: 36px; height: 4px; border-radius: 2px; background: var(--surface-3); margin: 0 auto 14px; }
.sheet h2 { font-size: 21px; margin: 0; line-height: 1.25; }
.sheet .brand { color: var(--ink-2); margin: 2px 0 14px; }
.sheet .field .input { background: var(--surface-2); border-color: transparent; }
.sheet .field .input:focus { border-color: var(--primary); }
.nutri { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; background: var(--surface-2); border-radius: 16px; padding: 12px 6px; margin: 4px 0 16px; }
.nutri b { display: block; font-size: 19px; }
.nutri span { font-size: 12px; color: var(--ink-2); font-weight: 600; }
.qty { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.qty .with-unit { flex: 1; }
.sheet .qty .input { background: var(--surface-2); border-color: transparent; text-align: center; font-weight: 700; font-size: 20px; padding: 0 40px; }
.sheet .qty .input:focus { border-color: var(--primary); }
.qty .iconbtn { background: var(--surface-2); color: var(--ink); width: 50px; height: 50px; border-radius: 14px; }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 96px); transform: translateX(-50%);
  z-index: 60; background: var(--ink); color: var(--bg);
  padding: 12px 20px; border-radius: 14px; font-weight: 600; font-size: 15px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2); animation: rise .25s ease; max-width: calc(100vw - 32px);
}

/* ---------- login ---------- */
.login { min-height: calc(100dvh - 140px); display: grid; align-content: center; }
.login-mark { width: 84px; height: 84px; margin: 0 auto 16px; }
.login h1 { text-align: center; font-size: 30px; margin: 0 0 4px; letter-spacing: -.02em; }
.login p.lede { text-align: center; color: var(--ink-2); margin: 0 0 28px; }

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