:root {
  --bg: #f4f5f0;
  --card: #ffffff;
  --card-2: #eceee6;
  --text: #1a1d18;
  --muted: #6b7166;
  --line: #dfe2d8;
  --accent: #2f7d4f;
  --accent-ink: #ffffff;
  --accent-soft: #dcefe2;
  --danger: #c0392b;
  --kcal: #2f7d4f;
  --prot: #3a6fd8;
  --carb: #d99a1e;
  --fat: #c9497a;
  --over: #d0573a;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  --radius: 16px;
  --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: #111311;
    --card: #1b1e1b;
    --card-2: #252925;
    --text: #eef1ea;
    --muted: #9aa294;
    --line: #2e332d;
    --accent: #5cc283;
    --accent-ink: #0d1a11;
    --accent-soft: #1f3a29;
    --danger: #ff6b5b;
    --kcal: #5cc283;
    --prot: #6f9cff;
    --carb: #f0b445;
    --fat: #ec6fa0;
    --over: #ff7f5c;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.num { font-variant-numeric: tabular-nums; }

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

/* ---------------------------------------------------------------- tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
  padding: 6px 8px calc(6px + var(--safe-b));
  background: color-mix(in srgb, var(--card) 88%, transparent);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 11px; padding: 4px 0;
}
.tabbar a.active { color: var(--accent); }
.tabbar svg, .icon svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.fab {
  justify-self: center; width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink); display: grid; place-items: center;
  box-shadow: 0 4px 14px color-mix(in srgb, var(--accent) 40%, transparent);
  margin-top: -18px;
}
.fab svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; }
body.composing .tabbar { display: none; }

/* ---------------------------------------------------------------- generic */
h1 { font-size: 26px; margin: 4px 0 12px; letter-spacing: -.02em; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 4px 8px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; }
.card + .card { margin-top: 10px; }
.stack > * + * { margin-top: 10px; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hidden { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 12px; padding: 12px 16px; font-weight: 600;
  background: var(--card-2); color: var(--text); text-decoration: none; cursor: pointer;
  min-height: 44px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { background: transparent; color: var(--danger); }
.btn.ghost { background: transparent; color: var(--accent); padding: 8px 10px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.btn.small { min-height: 34px; padding: 6px 12px; font-size: 14px; border-radius: 10px; }
.icon-btn {
  border: 0; background: var(--card-2); width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center; cursor: pointer; flex: none; color: var(--text);
}
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.plain { background: transparent; }

label.field { display: block; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin: 0 0 4px 2px; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--card); border-radius: 12px;
  padding: 11px 12px; font-size: 16px; outline: none; min-height: 44px;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
/* iOS Safari gives date inputs an intrinsic min width that overflows grids */
input[type=date] { -webkit-appearance: none; appearance: none; min-width: 0; max-width: 100%; display: block; }
input[type=date]::-webkit-date-and-time-value { text-align: left; }
.grid2 > *, .grid3 > * { min-width: 0; }
textarea { resize: vertical; min-height: 80px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.check { display: flex; gap: 10px; align-items: center; padding: 6px 2px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }

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

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 12px; font-size: 14px; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); }

.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; background: var(--card-2); color: var(--muted); vertical-align: middle; }
.badge.ai { background: color-mix(in srgb, var(--prot) 15%, transparent); color: var(--prot); }
.badge.off { background: color-mix(in srgb, var(--carb) 18%, transparent); color: color-mix(in srgb, var(--carb) 70%, var(--text)); }

.note { background: var(--card-2); border-radius: 12px; padding: 10px 12px; font-size: 14px; }
.note.warn { background: color-mix(in srgb, var(--carb) 16%, var(--card)); }
.note.info { background: color-mix(in srgb, var(--prot) 12%, var(--card)); }

/* ---------------------------------------------------------------- day header */
.datebar { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.datebar .title { flex: 1; text-align: center; }
.datebar .title b { display: block; font-size: 20px; letter-spacing: -.01em; }
.datebar .title small { color: var(--muted); font-size: 13px; }
.datebar input[type=date] { position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px; }

.summary { display: grid; grid-template-columns: 128px 1fr; gap: 16px; align-items: center; }
.ring { position: relative; width: 128px; height: 128px; }
.ring svg { width: 128px; height: 128px; transform: rotate(-90deg); }
.ring .center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring .center b { font-size: 26px; letter-spacing: -.02em; line-height: 1.05; }
.ring .center small { font-size: 12px; color: var(--muted); }
.macro + .macro { margin-top: 10px; }
.macro .lbl { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 4px; }
.macro .lbl b { font-weight: 600; }
.bar { height: 8px; border-radius: 99px; background: var(--card-2); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 99px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }

.mealtype { margin-top: 18px; }
.mealtype .head { display: flex; align-items: center; justify-content: space-between; margin: 0 4px 8px; }
.mealtype .head h3 { margin: 0; }
.meal { display: block; width: 100%; text-align: left; border: 0; cursor: pointer; }
.meal .items { margin-top: 6px; font-size: 14px; color: var(--muted); }
.meal .items div { display: flex; justify-content: space-between; gap: 8px; }
.meal .macros { font-size: 12px; color: var(--muted); margin-top: 6px; }
.empty-type { border: 1.5px dashed var(--line); border-radius: var(--radius); padding: 12px; color: var(--muted); text-align: center; font-size: 14px; width: 100%; background: transparent; }

/* ---------------------------------------------------------------- composer */
.topbar { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.topbar h1 { flex: 1; margin: 0; font-size: 20px; }
.addgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.addbtn {
  border: 0; background: var(--card); box-shadow: var(--shadow); border-radius: var(--radius);
  padding: 14px 12px; text-align: left; display: flex; flex-direction: column; gap: 4px; cursor: pointer; min-height: 84px;
}
.addbtn .e { font-size: 24px; line-height: 1; }
.addbtn b { font-size: 15px; }
.addbtn small { font-size: 12px; color: var(--muted); }
.addbtn.wide { grid-column: span 2; flex-direction: row; align-items: center; min-height: 0; gap: 12px; }
.item { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.item:last-child { border-bottom: 0; }
.item .info { flex: 1; min-width: 0; cursor: pointer; }
.item .info .n { font-weight: 500; }
.item .info .m { font-size: 12px; color: var(--muted); }
.item .g { width: 78px; flex: none; position: relative; }
.item .g input { padding: 8px 26px 8px 8px; min-height: 38px; text-align: right; }
.item .g::after { content: 'g'; position: absolute; right: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 14px; pointer-events: none; }
.totals-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
  padding: 10px 16px calc(10px + var(--safe-b));
}
.totals-bar .inner { max-width: 640px; margin: 0 auto; display: flex; gap: 12px; align-items: center; }
.totals-bar .t { flex: 1; font-size: 13px; color: var(--muted); }
.totals-bar .t b { font-size: 20px; color: var(--text); display: block; }

/* ---------------------------------------------------------------- lists */
.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list .li { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; width: 100%; background: transparent; border-left: 0; border-right: 0; border-top: 0; text-align: left; }
.list .li:last-child { border-bottom: 0; }
.list .li img { width: 40px; height: 40px; object-fit: cover; border-radius: 8px; background: var(--card-2); flex: none; }
.list .li .ph { width: 40px; height: 40px; border-radius: 8px; background: var(--card-2); display: grid; place-items: center; flex: none; font-size: 20px; }
.list .li .sub { font-size: 12px; color: var(--muted); }
.list .li .k { text-align: right; font-size: 13px; color: var(--muted); flex: none; }
.list .li .k b { color: var(--text); font-size: 15px; display: block; }
.list .empty { padding: 16px; text-align: center; color: var(--muted); font-size: 14px; }

/* ---------------------------------------------------------------- sheets */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.35); opacity: 0; transition: opacity .2s; }
.sheet {
  position: relative; width: 100%; max-width: 640px; max-height: calc(100dvh - var(--safe-t) - 20px);
  background: var(--bg); border-radius: 20px 20px 0 0; display: flex; flex-direction: column;
  transform: translateY(100%); transition: transform .25s cubic-bezier(.2,.8,.2,1);
}
.sheet-wrap.open .sheet { transform: none; }
.sheet-wrap.open .sheet-backdrop { opacity: 1; }
.sheet-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px 10px; }
.sheet-head h2 { flex: 1; }
.sheet-body { overflow-y: auto; padding: 4px 16px calc(20px + var(--safe-b)); -webkit-overflow-scrolling: touch; }
.sheet-foot { padding: 10px 16px calc(12px + var(--safe-b)); border-top: 1px solid var(--line); background: var(--bg); }
.sheet-foot + .sheet-body, .sheet-body:has(+ .sheet-foot) { padding-bottom: 12px; }

.bigg { display: flex; align-items: center; gap: 8px; }
.bigg input { font-size: 32px; font-weight: 700; text-align: center; min-height: 64px; letter-spacing: -.02em; }
.preview { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.preview div { background: var(--card); border-radius: 12px; padding: 8px 4px; }
.preview b { display: block; font-size: 18px; }
.preview small { font-size: 11px; color: var(--muted); }

.thumbs { display: flex; gap: 8px; flex-wrap: wrap; }
.thumbs .t { position: relative; width: 72px; height: 72px; border-radius: 12px; overflow: hidden; background: var(--card-2); }
.thumbs .t img { width: 100%; height: 100%; object-fit: cover; }
.thumbs .t button { position: absolute; top: 3px; right: 3px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 12px; line-height: 22px; padding: 0; }
.thumbs .add { border: 1.5px dashed var(--line); background: transparent; display: grid; place-items: center; font-size: 26px; color: var(--muted); }
.photo-preview { width: 100%; max-height: 220px; object-fit: contain; border-radius: 12px; background: var(--card-2); }

.est-row { background: var(--card); border-radius: 12px; padding: 10px; }
.est-row + .est-row { margin-top: 8px; }
.est-row .mini { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-top: 8px; }
.est-row .mini label span { font-size: 11px; color: var(--muted); display: block; text-align: center; }
.est-row .mini input { padding: 6px 4px; min-height: 36px; text-align: center; font-size: 15px; }

.spinner { width: 22px; height: 22px; border: 2.5px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { display: flex; align-items: center; gap: 12px; padding: 18px 4px; color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: calc(100px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: all .2s; z-index: 100; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.err { background: var(--danger); color: #fff; }

/* ---------------------------------------------------------------- charts */
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 10px; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); }
.stat small { color: var(--muted); font-size: 12px; }
.stat b { display: block; font-size: 22px; letter-spacing: -.02em; }
