:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --soft: #e8eefb;
  --danger: #dc2626;
  --kcal: #2563eb;
  --p: #16a34a;
  --c: #d97706;
  --f: #9333ea;
  --track: #e2e8f0;
  --radius: 14px;
  --tabs-h: 64px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --card: #151e2f;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --line: #243045;
    --accent: #3b82f6;
    --soft: #1c2a44;
    --danger: #f87171;
    --kcal: #60a5fa;
    --p: #4ade80;
    --c: #fbbf24;
    --f: #c084fc;
    --track: #243045;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
  -webkit-tap-highlight-color: transparent;
}
body.locked { overflow: hidden; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
h1 { font-size: 1.5rem; margin: 8px 0 12px; }
h2 { font-size: 1.05rem; margin: 0; }
h3 { margin: 0; }
h3.sub { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 4px 8px; }
small { color: var(--muted); font-weight: 400; }
.muted { color: var(--muted); font-size: .85rem; }
.center { text-align: center; }
.empty { color: var(--muted); text-align: center; padding: 20px; margin: 0; }

#app { max-width: 640px; margin: 0 auto; }
.content { padding: 12px 12px 0; }
.card { background: var(--card); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; box-shadow: 0 1px 2px rgb(0 0 0 / .05); }

/* top summary */
.top {
  position: sticky; top: 0; z-index: 5;
  background: var(--card);
  padding: calc(env(safe-area-inset-top) + 8px) 12px 10px;
  border-bottom: 1px solid var(--line);
}
.datebar { display: flex; align-items: center; gap: 6px; }
.datepick { position: relative; flex: 1; text-align: center; font-weight: 600; min-height: 48px; display: flex; align-items: center; justify-content: center; border-radius: 10px; background: var(--soft); }
.datepick input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; border: 0; }
.seg { display: flex; background: var(--soft); border-radius: 12px; padding: 4px; margin: 8px 0; gap: 4px; }
.seg button { flex: 1; min-height: 44px; border: 0; background: transparent; border-radius: 9px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--accent); color: var(--accent-text); }

.bars3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 8px; }
.bar-top { display: flex; justify-content: space-between; align-items: baseline; font-size: .8rem; gap: 4px; }
.bar.big .bar-top { font-size: .95rem; }
.bar .bl { color: var(--muted); font-weight: 600; }
.bar .bv { white-space: nowrap; }
.bar.big .bv b { font-size: 1.3rem; }
.track { height: 8px; background: var(--track); border-radius: 99px; overflow: hidden; margin-top: 4px; }
.bar.big .track { height: 12px; }
.fill { height: 100%; border-radius: 99px; transition: width .25s; }
.k-kcal .fill { background: var(--kcal); }
.k-p .fill { background: var(--p); }
.k-c .fill { background: var(--c); }
.k-f .fill { background: var(--f); }
.bar.over .fill { background: var(--danger); }
.left { font-size: .75rem; color: var(--muted); margin-top: 2px; }
.bar.big .left { font-size: .85rem; }
.bar.over .left { color: var(--danger); font-weight: 600; }
@media (max-width: 360px) { .bars3 .bv { font-size: .72rem; } }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 48px; padding: 0 16px; border-radius: 12px;
  border: 0; background: var(--accent); color: var(--accent-text); font-weight: 600;
}
.btn.ghost { background: var(--soft); color: var(--text); }
.btn.danger { color: var(--danger); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.full { width: 100%; margin-top: 10px; }
.btn.big { min-height: 56px; font-size: 1.1rem; }
.btn:disabled { opacity: .4; }
.icon { min-width: 48px; min-height: 48px; border: 0; background: transparent; font-size: 1.5rem; border-radius: 12px; line-height: 1; color: var(--text); }
.icon:active, .btn:active, .entry:active, .food-row:active { opacity: .7; }
.row-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.row-actions .btn { flex: 1; }
.row-actions .search { flex: 1; margin: 0; }

/* meals */
.meal { padding: 10px 6px 12px 14px; }
.meal header { display: flex; align-items: flex-start; justify-content: space-between; }
.meal .mh { padding-top: 4px; min-width: 0; }
.meal small { font-size: .8rem; }
.entries { list-style: none; margin: 6px 0 0; padding: 0 8px 0 0; }
.entries li + li { border-top: 1px solid var(--line); }
.entry {
  width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 10px;
  min-height: 52px; padding: 6px 0; background: none; border: 0; text-align: left;
}
.entry .en { display: flex; flex-direction: column; min-width: 0; }
.entry .en small { font-size: .8rem; }
.entry .em { display: flex; flex-direction: column; align-items: flex-end; white-space: nowrap; }
.entry .em small { font-size: .75rem; }
.meal-actions { display: flex; gap: 8px; margin-top: 10px; padding-right: 8px; flex-wrap: wrap; }
.meal-actions .btn { flex: 1; }

/* lists */
.list { padding: 0; overflow: hidden; }
.food-row {
  width: 100%; display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  min-height: 56px; padding: 10px 14px; border: 0; background: none; text-align: left;
  border-bottom: 1px solid var(--line);
}
.food-row:last-child { border-bottom: 0; }
.food-row .fn { font-weight: 600; }
.search, .field input {
  width: 100%; min-height: 48px; padding: 10px 14px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--card);
  font-size: 16px; outline: none;
}
.search:focus, .field input:focus { border-color: var(--accent); }
.search { margin-bottom: 8px; }

.tpl h2 { margin-bottom: 2px; }
.tpl .items { font-size: .85rem; margin: 8px 0 0; }

/* forms */
.field { display: block; margin-bottom: 10px; }
.field span { display: block; font-size: .8rem; color: var(--muted); margin-bottom: 4px; }
.grid4 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

/* tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  flex: 1; height: var(--tabs-h); border: 0; background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: .75rem; color: var(--muted); font-weight: 600;
}
.tabs button span { font-size: 1.35rem; filter: grayscale(1); opacity: .7; }
.tabs button.active { color: var(--accent); }
.tabs button.active span { filter: none; opacity: 1; }

/* bottom sheet */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgb(0 0 0 / .45); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-width: 640px; margin: 0 auto;
  max-height: 88vh; max-height: 88dvh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--card); border-radius: 18px 18px 0 0;
  padding: 6px 14px calc(16px + env(safe-area-inset-bottom));
  animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: .6; } }
.sheet-head { display: flex; align-items: center; gap: 4px; position: sticky; top: -6px; background: var(--card); z-index: 1; padding: 4px 0; }
.sheet-head h3 { flex: 1; font-size: 1.1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet .list { border: 1px solid var(--line); border-radius: var(--radius); }
.stack { display: flex; flex-direction: column; }
.stack .btn { justify-content: flex-start; }

.qty-row { display: flex; align-items: center; gap: 8px; margin: 12px 0; }
.qty-row input {
  flex: 1; min-width: 0; min-height: 60px; text-align: center; font-size: 1.8rem; font-weight: 700;
  border: 1px solid var(--line); border-radius: 12px; background: var(--card); outline: none;
}
.qty-row input:focus { border-color: var(--accent); }
.qty-unit { color: var(--muted); font-weight: 600; max-width: 80px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon.big { background: var(--soft); min-width: 56px; min-height: 56px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button { flex: 1; min-height: 44px; min-width: 56px; border: 0; border-radius: 10px; background: var(--soft); font-weight: 600; }
.preview { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; margin: 14px 0 4px; }
.preview div { display: flex; flex-direction: column; }
.preview b { font-size: 1.25rem; }
.preview small { font-size: .75rem; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 14px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 99px;
  font-size: .9rem; z-index: 30; max-width: 90vw; text-align: center;
}
.toast[hidden] { display: none; }

/* workout plan */
.workout { text-align: center; font-size: .95rem; margin: -2px 0 8px; }
.sched-row { display: grid; grid-template-columns: 40px 1fr 1.3fr 1.1fr; gap: 6px; align-items: center; margin-bottom: 8px; }
.sched-row select, .sched-row input {
  width: 100%; min-width: 0; min-height: 48px; padding: 6px 8px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--card); font-size: 15px;
}
.sched-row input:disabled { opacity: .35; }

/* saved meals */
.tpl-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.tpl-kcal { white-space: nowrap; color: var(--muted); }
.tpl-kcal b { font-size: 1.3rem; color: var(--text); }
.tpl-macros { display: flex; gap: 14px; font-weight: 600; font-size: .9rem; margin-top: 2px; }
.tpl-macros .k-p { color: var(--p); }
.tpl-macros .k-c { color: var(--c); }
.tpl-macros .k-f { color: var(--f); }
.tpl-total { margin: 4px 0 12px; }
.tpl-item { display: flex; align-items: center; gap: 6px; padding: 6px 4px 6px 12px; border-bottom: 1px solid var(--line); }
.tpl-item:last-child { border-bottom: 0; }
.tpl-item .en { flex: 1; display: flex; flex-direction: column; min-width: 0; font-weight: 600; }
.tpl-item .en small { font-size: .75rem; }
.tpl-item input { width: 72px; min-height: 44px; text-align: center; border: 1px solid var(--line); border-radius: 10px; background: var(--card); font-size: 16px; }
.tpl-item .u { width: 44px; font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.select { width: 100%; min-height: 48px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); font-size: 16px; }

/* meal plan options on the log */
.plan-label { font-size: .8rem; color: var(--muted); font-weight: 600; margin: 8px 0 4px; }
.plan-opts { display: flex; flex-direction: column; gap: 6px; padding-right: 8px; }
.plan-opt {
  display: flex; justify-content: space-between; align-items: center; gap: 10px; text-align: left;
  min-height: 56px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--soft);
}
.plan-opt .en { display: flex; flex-direction: column; min-width: 0; font-weight: 600; }
.plan-opt .en small { font-size: .75rem; font-weight: 400; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.plan-opt .em { display: flex; flex-direction: column; align-items: flex-end; white-space: nowrap; }
.plan-opt .em small { font-size: .75rem; }
.plan-opt:active { opacity: .7; }
