/* Sage greens on warm paper; tomato accent for "over target". */
:root {
  --bg: #eef3ea;
  --card: #fdfefb;
  --bar: #e3ebdd;
  --bar-edge: #cbd8c2;
  --text: #17210f;
  --muted: #5d6b55;
  --line: #d6e0cf;
  --accent: #3f7d2c;
  --accent-ink: #f7fbf3;
  --accent-soft: #dcebd2;
  --over: #c4472b;
  --p: #3b6fb8;
  --c: #c08a12;
  --f: #a3489a;
  --scrim: rgba(14, 24, 10, .55);
  --shadow: 0 1px 2px rgba(20, 40, 10, .06), 0 8px 22px rgba(20, 40, 10, .08);
  --radius: 18px;
  --nav-h: 64px;
  --top-h: 56px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111611;
    --card: #1b221a;
    --bar: #192018;
    --bar-edge: #2a3527;
    --text: #e8f0e2;
    --muted: #9aab90;
    --line: #2c3829;
    --accent: #8fcf6f;
    --accent-ink: #12200b;
    --accent-soft: #263a1d;
    --over: #f0876c;
    --p: #84aef0;
    --c: #e3b84d;
    --f: #dc8ad2;
    --scrim: rgba(0, 0, 0, .65);
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 22px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.pad { padding: 24px; }
.num { font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- chrome */
.top {
  position: sticky; top: 0; z-index: 5;
  height: calc(var(--top-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 16px 0;
  display: flex; align-items: center; justify-content: space-between;
  background: var(--bar); border-bottom: 1px solid var(--bar-edge);
}
.top h1 { font-size: 1.25rem; font-weight: 800; }
.top-right { display: flex; align-items: center; gap: 10px; }
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.sync-dot.pending { background: var(--c); }
.sync-dot.error { background: var(--over); }
.avatar-btn { width: 32px; height: 32px; border-radius: 50%; overflow: hidden; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); font-weight: 700; text-decoration: none; }
.avatar-btn img { width: 100%; height: 100%; object-fit: cover; }

.page {
  max-width: 560px; margin: 0 auto;
  padding: 16px 16px calc(var(--nav-h) + 24px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; background: var(--bar); border-top: 1px solid var(--bar-edge);
}
.nav a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--muted); text-decoration: none; font-size: .75rem; font-weight: 600; }
.nav a.on { color: var(--accent); }
.nav svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------------------------------------------------------------- cards */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.card-head h2 { font-size: 1rem; font-weight: 750; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }

.big { font-size: 2.4rem; font-weight: 800; letter-spacing: -.02em; line-height: 1; }
.big small { font-size: 1rem; font-weight: 600; color: var(--muted); letter-spacing: 0; }
.over { color: var(--over); }

.btn {
  border: 0; border-radius: 12px; padding: 10px 14px; min-height: 44px;
  background: var(--accent-soft); color: var(--accent); font-weight: 700; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; }
.btn.danger { background: transparent; color: var(--over); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

input[type=number], input[type=text], input[type=search], select {
  width: 100%; min-height: 44px; padding: 8px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); font-size: 16px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); font-weight: 600; }
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }
.unit-input { position: relative; }
.unit-input span { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; font-size: .9rem; }
.unit-input input { padding-right: 44px; }

.seg { display: flex; background: var(--bg); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 9px; padding: 8px 4px; font-weight: 700; color: var(--muted); cursor: pointer; font-size: .9rem; }
.seg button.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* ---------------------------------------------------------------- macros */
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.macro { display: flex; flex-direction: column; gap: 4px; }
.macro b { font-size: 1.1rem; }
.macro .lbl { font-size: .75rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 3px; background: var(--accent); }
.macro.p .bar i { background: var(--p); }
.macro.c .bar i { background: var(--c); }
.macro.f .bar i { background: var(--f); }
.bar i.over { background: var(--over) !important; }

/* ---------------------------------------------------------------- lists */
.list { display: flex; flex-direction: column; }
.item { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); background: none; border-left: 0; border-right: 0; border-bottom: 0; text-align: left; width: 100%; cursor: pointer; }
.item:first-child { border-top: 0; }
.item .name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: .8rem; color: var(--muted); }
.item .kc { font-weight: 750; white-space: nowrap; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 6px 12px; font-size: .85rem; font-weight: 600; cursor: pointer; max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------------------------------------------------------------- stats */
table.stats { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.stats th, table.stats td { padding: 8px 4px; text-align: right; border-top: 1px solid var(--line); }
table.stats th { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); border-top: 0; }
table.stats th:first-child, table.stats td:first-child { text-align: left; font-weight: 650; }
table.stats td.n { color: var(--muted); font-size: .8rem; }
.approx { color: var(--muted); }
.spark { width: 100%; height: 110px; display: block; }
.spark path.line { fill: none; stroke: var(--accent); stroke-width: 2; vector-effect: non-scaling-stroke; }
.spark circle { fill: var(--accent); }
.spark text { fill: var(--muted); font-size: 10px; }

.day { display: grid; grid-template-columns: 1fr auto auto; gap: 12px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; background: none; border-left: 0; border-right: 0; border-bottom: 0; width: 100%; text-align: left; }
.day:first-child { border-top: 0; }
.day .v { min-width: 72px; text-align: right; font-variant-numeric: tabular-nums; font-weight: 650; }
.day .v.blank { color: var(--muted); font-weight: 400; }

/* ---------------------------------------------------------------- sheet + toast */
.sheet { position: fixed; inset: 0; z-index: 20; background: var(--scrim); display: flex; align-items: flex-end; justify-content: center; }
.sheet-panel {
  position: relative; width: 100%; max-width: 560px; max-height: 92dvh; overflow-y: auto;
  background: var(--card); border-radius: 22px 22px 0 0;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}
.sheet-panel h2 { font-size: 1.15rem; padding-right: 40px; }
.sheet-x { position: absolute; top: 10px; right: 10px; width: 40px; height: 40px; border: 0; border-radius: 50%; background: var(--bg); font-size: 1.4rem; cursor: pointer; }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); z-index: 30; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; max-width: calc(100% - 32px); }

.scanner { position: relative; border-radius: 16px; overflow: hidden; background: #000; aspect-ratio: 4 / 3; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .aim { position: absolute; left: 12%; right: 12%; top: 35%; bottom: 35%; border: 3px solid rgba(255,255,255,.85); border-radius: 12px; box-shadow: 0 0 0 999px rgba(0,0,0,.25); }
.note { background: var(--accent-soft); border-radius: 12px; padding: 10px 12px; font-size: .9rem; }
.note.warn { background: color-mix(in srgb, var(--over) 15%, transparent); }
