:root {
  --bg: #f4f1e8;
  --surface: #fffdf7;
  --surface-2: #ece8dc;
  --ink: #1c1f1a;
  --muted: #666b60;
  --line: #ddd8c9;
  --accent: #1d6b47;
  --accent-ink: #ffffff;
  --accent-soft: #dcebdf;
  --protein: #bf5138;
  --carbs: #b98516;
  --fat: #3f78ad;
  --danger: #b3382c;
  --shadow: 0 1px 2px rgba(40, 36, 20, .06), 0 6px 20px rgba(40, 36, 20, .06);
  --radius: 18px;
  --tabs-h: 62px;
  --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: #111411;
    --surface: #1a1e1a;
    --surface-2: #242924;
    --ink: #ecefe7;
    --muted: #9aa094;
    --line: #2d332d;
    --accent: #62c492;
    --accent-ink: #0d1a12;
    --accent-soft: #1f3327;
    --protein: #e58068;
    --carbs: #e0b04a;
    --fat: #74a9dc;
    --danger: #ee7a6c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    --seg-on: #3b433b;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}
h1, h2, h3, p { margin: 0; }
h1 { font-size: 28px; letter-spacing: -.02em; font-weight: 750; }
h2 { font-size: 18px; letter-spacing: -.01em; }
h3 { font-size: 13px; font-weight: 650; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.num, .tick { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; }
.between { justify-content: space-between; }
.gap { gap: 10px; }
.grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 12px; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
#view { max-width: 560px; margin: 0 auto; padding: calc(var(--safe-t) + 14px) 16px calc(var(--tabs-h) + var(--safe-b) + 28px); }
.page > * + * { margin-top: 14px; }
.card { background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card > * + * { margin-top: 12px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }

#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
  height: calc(var(--tabs-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
#tabs button { background: none; border: 0; height: var(--tabs-h); display: grid; place-items: center; gap: 0; color: var(--muted); font-size: 11px; font-weight: 600; padding: 6px 0 4px; }
#tabs button[aria-current="page"] { color: var(--accent); }
#tabs svg { width: 24px; height: 24px; stroke: currentColor; fill: none; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
#tabs .fab { width: 52px; height: 52px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); justify-self: center; box-shadow: 0 6px 16px color-mix(in srgb, var(--accent) 40%, transparent); padding: 0; }
#tabs .fab svg { width: 26px; height: 26px; stroke-width: 2.4; }

/* ---------- controls ---------- */
.btn { border: 0; border-radius: 14px; background: var(--accent); color: var(--accent-ink); font-weight: 650; padding: 13px 18px; min-height: 48px; }
.btn:disabled { opacity: .5; }
.btn.ghost { background: var(--surface-2); color: var(--ink); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.block { width: 100%; }
.btn.sm { min-height: 36px; padding: 7px 12px; border-radius: 10px; font-size: 14px; }
.link { background: none; border: 0; color: var(--accent); font-weight: 600; padding: 6px 0; }
.link.danger { color: var(--danger); }
.icon-btn { background: var(--surface-2); border: 0; width: 36px; height: 36px; border-radius: 50%; font-size: 15px; line-height: 1; flex: none; }

input, select, textarea { width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px; min-height: 46px; font-size: 16px; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.field { display: block; }
.field-label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.hint { display: block; font-size: 12.5px; color: var(--muted); margin-top: 5px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }

.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 9px; padding: 9px 6px; font-size: 14px; font-weight: 600; color: var(--muted); min-height: 38px; }
.seg button[aria-selected="true"] { background: var(--seg-on, var(--surface)); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

/* ---------- diary ---------- */
.datebar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.datebar h1 { flex: 1; }
.datebar input[type="date"] { position: absolute; opacity: 0; width: 36px; height: 36px; min-height: 0; padding: 0; }
.datepick { position: relative; width: 36px; height: 36px; }
.datepick span { position: absolute; inset: 0; display: grid; place-items: center; background: var(--surface-2); border-radius: 50%; pointer-events: none; font-size: 15px; }

.hero { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.hero .big { font-size: 44px; line-height: 1; font-weight: 780; letter-spacing: -.03em; }
.hero .big.over { color: var(--danger); }
.bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 99px; background: var(--accent); transition: width .3s ease; }
.bar.p i { background: var(--protein); }
.bar.c i { background: var(--carbs); }
.bar.f i { background: var(--fat); }
.bar.over i { background: var(--danger); }
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.macro b { display: block; font-size: 17px; }
.macro span { font-size: 12.5px; color: var(--muted); }
.macro .bar { margin-top: 6px; height: 6px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.dot.p { background: var(--protein); } .dot.c { background: var(--carbs); } .dot.f { background: var(--fat); }

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 8px; }
.list li + li { border-top: 1px solid var(--line); }
.item { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; width: 100%; background: none; border: 0; text-align: left; padding: 11px 0; min-height: 52px; }
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .kcal { font-weight: 650; white-space: nowrap; }
.item .time { width: 62px; flex: none; font-size: 12.5px; color: var(--muted); }
.banner { background: var(--accent-soft); border-radius: var(--radius); padding: 14px 16px; display: flex; align-items: center; gap: 12px; }
.empty { color: var(--muted); text-align: center; padding: 18px 8px; font-size: 15px; }
.nutri-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; font-size: 14px; }
.nutri-row .bar { grid-column: 1 / -1; height: 5px; }
details > summary { list-style: none; cursor: pointer; display: flex; justify-content: space-between; align-items: center; font-weight: 650; }
details > summary::-webkit-details-marker { display: none; }
details > summary::after { content: "⌄"; color: var(--muted); font-size: 18px; }
details[open] > summary::after { content: "⌃"; }
details > div { margin-top: 12px; }

/* ---------- charts ---------- */
.chart { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .hline { stroke: var(--muted); stroke-width: 1.2; stroke-dasharray: 4 4; }
.chart .tick { fill: var(--muted); font-size: 10px; }
.chart .trend { stroke: var(--accent); stroke-width: 2.4; stroke-linejoin: round; stroke-linecap: round; }
.chart .scale { fill: var(--muted); opacity: .55; }
.chart .intake { fill: var(--accent); opacity: .8; }
.stat { font-size: 30px; font-weight: 760; letter-spacing: -.02em; line-height: 1.1; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stats b { display: block; font-size: 18px; }
.stats span { font-size: 12.5px; color: var(--muted); }
.up { color: var(--carbs); } .down { color: var(--accent); }

/* ---------- habits ---------- */
.habit { display: flex; align-items: center; gap: 12px; padding: 12px 0; }
.check { width: 34px; height: 34px; border-radius: 50%; border: 2px solid var(--line); background: none; flex: none; font-size: 16px; color: transparent; }
.check.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button { width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--surface-2); font-size: 18px; }
.stepper input { width: 62px; text-align: center; min-height: 36px; padding: 6px; }
.week { display: flex; gap: 4px; margin-top: 4px; }
.week i { width: 8px; height: 8px; border-radius: 50%; background: var(--surface-2); }
.week i.on { background: var(--accent); }

/* ---------- photos ---------- */
.photos { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.photos button { border: 0; padding: 0; background: var(--surface-2); border-radius: 10px; overflow: hidden; aspect-ratio: 3 / 4; position: relative; }
.photos img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photos span { position: absolute; left: 6px; bottom: 5px; font-size: 11px; color: #fff; text-shadow: 0 1px 3px #000; }
.photo-full { width: 100%; border-radius: 12px; display: block; }

/* ---------- sheets ---------- */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, 0); transition: background .2s; display: flex; align-items: flex-end; justify-content: center; }
.sheet-wrap.open { background: rgba(0, 0, 0, .42); }
.sheet { width: 100%; max-width: 560px; max-height: calc(100dvh - var(--safe-t) - 24px); background: var(--bg); border-radius: 22px 22px 0 0; display: flex; flex-direction: column; transform: translateY(100%); transition: transform .22s cubic-bezier(.2, .8, .2, 1); }
.sheet-wrap.open .sheet { transform: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 16px 10px; }
.sheet-head h2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 16px calc(var(--safe-b) + 20px); }
.sheet-body > * + * { margin-top: 12px; }
#scan-region { border-radius: 14px; overflow: hidden; background: #000; min-height: 200px; }
#scan-region video { display: block; width: 100% !important; }

#toast { position: fixed; left: 50%; bottom: calc(var(--tabs-h) + var(--safe-b) + 18px); transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 99px; font-size: 14px; font-weight: 600; opacity: 0; pointer-events: none; transition: all .2s; z-index: 80; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

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

/* ---------- training and AI ---------- */
.tag { display: inline-block; margin-left: 8px; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-size: 11.5px; font-weight: 700; vertical-align: middle; }
.tips { margin: 0; padding-left: 18px; font-size: 14.5px; }
.tips li + li { margin-top: 8px; }
.name.wrap, .sub.wrap { white-space: normal; }
textarea { resize: vertical; min-height: 64px; font-family: inherit; }
.meal-preview { width: 100%; max-height: 240px; object-fit: cover; border-radius: 14px; display: block; }
.tick { width: 22px; height: 22px; min-height: 0; flex: none; accent-color: var(--accent); padding: 0; }
label.item { cursor: pointer; }
