:root {
  --bg: #0d1412;
  --bg-2: #141d1a;
  --card: #17221f;
  --line: #24322e;
  --fg: #eef4f1;
  --fg-2: #94a8a1;
  --fg-3: #64786f;
  --accent: #21c08f;
  --accent-dim: #0f5e4a;
  --warn: #e0a03c;
  --danger: #e2685c;
  --tab-h: 58px;
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f7f5;
    --bg-2: #ffffff;
    --card: #ffffff;
    --line: #e2e9e5;
    --fg: #10201b;
    --fg-2: #5e716a;
    --fg-3: #8b9c95;
    --accent: #0f8f6b;
    --accent-dim: #0f5e4a;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

#app { height: 100%; display: flex; flex-direction: column; }

/* ── screens ─────────────────────────────────────────────── */
.screen {
  position: absolute;
  inset: 0 0 calc(var(--tab-h) + var(--safe-bot)) 0;
  display: flex;
  flex-direction: column;
}
.screen[hidden] { display: none; }

.hd {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: calc(var(--safe-top) + 14px) 20px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.hd > div:first-child { flex: 1; min-width: 0; }
.hd h1 { margin: 0; font-size: 28px; letter-spacing: -0.5px; font-weight: 700; }
.hd-sub { margin: 2px 0 0; color: var(--fg-2); font-size: 13px; }
.hd-nav {
  width: 36px; height: 36px; flex: none;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--fg);
  font-size: 20px; line-height: 1; cursor: pointer;
}
.hd-nav:disabled { opacity: .3; }

.scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 20px 0;
}
.spacer { height: 96px; }

.sec {
  margin: 22px 0 8px;
  font-size: 12px; font-weight: 700;
  letter-spacing: .08em; text-transform: uppercase;
  color: var(--fg-3);
}

/* ── protein ring ────────────────────────────────────────── */
.ring-card {
  position: relative;
  display: grid; place-items: center;
  padding: 8px 0 4px;
}
.ring { width: 220px; height: 220px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 11; stroke-linecap: round; }
.ring-bg { stroke: var(--line); }
.ring-fg {
  stroke: var(--accent);
  stroke-dasharray: 326.7;
  stroke-dashoffset: 326.7;
  transition: stroke-dashoffset .5s cubic-bezier(.4,0,.2,1), stroke .3s;
}
.ring-fg.over { stroke: var(--warn); }
.ring-label {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  pointer-events: none;
}
.ring-value { font-size: 52px; font-weight: 700; letter-spacing: -2px; line-height: 1; }
.ring-unit { display: none; }
.ring-caption { margin-top: 6px; font-size: 13px; color: var(--fg-2); }
.ring-remain { margin-top: 2px; font-size: 12px; color: var(--fg-3); }

/* ── stat tiles ──────────────────────────────────────────── */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 14px; }
.tiles-2 { grid-template-columns: repeat(2, 1fr); }
.tile {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.tile-v { font-size: 22px; font-weight: 650; letter-spacing: -.5px; }
.tile-k { font-size: 11px; color: var(--fg-3); text-align: center; }
.tile-sub { font-size: 10px; color: var(--fg-3); }

/* ── cards / forms ───────────────────────────────────────── */
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 16px; padding: 14px; margin-top: 12px;
}
.lbl { display: block; font-size: 13px; color: var(--fg-2); margin: 12px 0 5px; font-weight: 500; }
.card > .lbl:first-child { margin-top: 0; }
.opt { color: var(--fg-3); font-weight: 400; }

.in {
  width: 100%; padding: 11px 12px;
  background: var(--bg-2); color: var(--fg);
  border: 1px solid var(--line); border-radius: 11px;
  font: inherit; font-size: 16px; /* 16px stops iOS zoom-on-focus */
}
.in:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea.in { resize: vertical; min-height: 60px; }
select.in { appearance: none; }

.row { display: flex; gap: 8px; align-items: center; }
.row .in { flex: 1; }
.row-wrap { flex-wrap: wrap; margin-top: 10px; }

.btn {
  padding: 11px 16px; border-radius: 11px; border: 1px solid var(--line);
  background: var(--accent); color: #04120d;
  font: inherit; font-weight: 650; cursor: pointer; flex: none;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .45; }
.btn-primary { width: 100%; margin-top: 14px; padding: 13px; font-size: 16px; }
.btn-ghost { background: var(--bg-2); color: var(--fg); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); width: 100%; margin-top: 12px; }

.hint { margin: 8px 0 0; font-size: 12.5px; color: var(--fg-3); line-height: 1.4; }
.hint code { font-size: 11.5px; background: var(--bg-2); padding: 1px 4px; border-radius: 4px; }
.hint.err { color: var(--danger); }
.hint.ok { color: var(--accent); }

.link {
  background: none; border: 0; color: var(--accent);
  font: inherit; padding: 6px 2px; cursor: pointer;
}
.link:disabled { opacity: .4; }
.link-strong { font-weight: 650; }
.link-quiet { display: block; width: 100%; text-align: center; color: var(--fg-3); margin-top: 14px; font-size: 14px; }

/* ── meal list ───────────────────────────────────────────── */
.meal-list { display: flex; flex-direction: column; gap: 8px; }
.meal {
  display: flex; gap: 12px; align-items: center;
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 10px; cursor: pointer;
}
.meal img, .meal .noimg {
  width: 52px; height: 52px; flex: none;
  border-radius: 10px; object-fit: cover;
  background: var(--bg-2);
  display: grid; place-items: center;
  color: var(--fg-3); font-size: 19px;
}
.meal-mid { flex: 1; min-width: 0; }
.meal-name { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meal-meta { font-size: 12px; color: var(--fg-3); margin-top: 2px; }
.meal-pro { text-align: right; flex: none; }
.meal-pro b { display: block; font-size: 17px; color: var(--accent); }
.meal-pro span { font-size: 10px; color: var(--fg-3); }

.empty { text-align: center; color: var(--fg-3); font-size: 14px; padding: 26px 10px; }

/* ── history ─────────────────────────────────────────────── */
.day {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 14px; margin-top: 8px;
  display: flex; align-items: center; gap: 12px;
}
.day-d { flex: 1; }
.day-d b { font-size: 15px; }
.day-d span { display: block; font-size: 12px; color: var(--fg-3); margin-top: 2px; }
.day-p { font-size: 17px; font-weight: 650; }
.day-bar { height: 4px; border-radius: 2px; background: var(--line); margin-top: 6px; overflow: hidden; }
.day-bar i { display: block; height: 100%; background: var(--accent); }

/* ── weight ──────────────────────────────────────────────── */
.pace { margin: 0; font-size: 14px; line-height: 1.5; }
.pace b { color: var(--accent); }
.pace.hot b { color: var(--warn); }
.chart-card { padding: 12px 10px 8px; }
.chart { width: 100%; height: 170px; display: block; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .target { stroke: var(--fg-3); stroke-width: 1.5; stroke-dasharray: 4 4; fill: none; }
.chart .actual { stroke: var(--accent); stroke-width: 2.5; fill: none; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--accent); }
.chart .ax { fill: var(--fg-3); font-size: 9px; }
.legend { display: flex; gap: 16px; justify-content: center; margin-top: 6px; font-size: 11px; color: var(--fg-3); }
.legend span { display: flex; align-items: center; gap: 5px; }
.sw { width: 14px; height: 2px; border-radius: 1px; }
.sw-actual { background: var(--accent); }
.sw-target { background: var(--fg-3); }

.w-list { margin-top: 10px; }
.w-row {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 14px; background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; margin-top: 6px;
}
.w-row b { flex: 1; font-weight: 500; font-size: 14px; }
.w-row .v { font-weight: 650; }
.w-row .d { font-size: 12px; color: var(--fg-3); width: 52px; text-align: right; }

/* ── FAB ─────────────────────────────────────────────────── */
.fab {
  position: absolute;
  left: 20px; right: 20px;
  bottom: calc(14px + var(--safe-bot));
  padding: 15px; border: 0; border-radius: 15px;
  background: var(--accent); color: #04120d;
  font: inherit; font-size: 16px; font-weight: 700;
  box-shadow: 0 8px 24px rgba(0,0,0,.35);
  cursor: pointer;
}
.fab:active { transform: scale(.985); }

/* ── tab bar ─────────────────────────────────────────────── */
.tabs {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: calc(var(--tab-h) + var(--safe-bot));
  padding-bottom: var(--safe-bot);
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(18px);
  border-top: 1px solid var(--line);
}
.tab {
  background: none; border: 0; cursor: pointer;
  color: var(--fg-3); font: inherit; font-size: 10.5px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
.tab .ti { font-size: 19px; line-height: 1; }
.tab.is-on { color: var(--accent); }

/* ── sheet ───────────────────────────────────────────────── */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; }
.sheet-wrap[hidden] { display: none; }
.sheet-scrim { position: absolute; inset: 0; background: rgba(0,0,0,.5); }
.sheet {
  position: relative; width: 100%; max-height: 94%;
  background: var(--bg); border-radius: 20px 20px 0 0;
  display: flex; flex-direction: column;
  animation: rise .28s cubic-bezier(.32,.72,0,1);
}
@keyframes rise { from { transform: translateY(100%); } }

.sheet-hd {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--line);
}
.sheet-hd b { font-size: 16px; }
.sheet-body {
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(28px + var(--safe-bot));
}

.shots { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.shot {
  position: relative; aspect-ratio: 1;
  border: 1.5px dashed var(--line); border-radius: 14px;
  display: grid; place-items: center; overflow: hidden;
  background: var(--card); cursor: pointer;
}
.shot img { width: 100%; height: 100%; object-fit: cover; }
.shot-ph { display: flex; flex-direction: column; align-items: center; gap: 6px; color: var(--fg-3); font-size: 12.5px; }
.shot-ph b { font-size: 26px; font-weight: 400; line-height: 1; }
.shot-x {
  position: absolute; top: 6px; right: 6px;
  width: 26px; height: 26px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.6); color: #fff;
  font-size: 16px; line-height: 1; cursor: pointer;
}

.segs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 14px; }
.segs button {
  padding: 9px 4px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card);
  color: var(--fg-2); font: inherit; font-size: 13px; cursor: pointer;
}
.segs button.is-on { background: var(--accent-dim); border-color: var(--accent); color: var(--fg); }

.est-hd { display: flex; align-items: center; gap: 8px; margin-top: 20px; }
.est-hd b { font-size: 15px; }
.pill {
  font-size: 11px; padding: 3px 9px; border-radius: 20px;
  background: var(--bg-2); border: 1px solid var(--line); color: var(--fg-2);
}
.pill.low { color: var(--warn); border-color: var(--warn); }

.items { margin: 8px 0 0; padding-left: 18px; font-size: 13px; color: var(--fg-2); }
.items li { margin: 2px 0; }

.macros { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.macros label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--fg-2); }

.link-danger { color: var(--danger); }

.d-photos { display: flex; gap: 10px; margin-bottom: 4px; }
.d-photos img {
  flex: 1; min-width: 0; max-height: 300px;
  object-fit: cover; border-radius: 14px;
  background: var(--card);
}
.d-photos:empty { display: none; }
#d-tiles { margin-top: 12px; }
.sheet-hd b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 10px; }

/* ── toast ───────────────────────────────────────────────── */
.toast {
  position: fixed; z-index: 90;
  left: 50%; bottom: calc(var(--tab-h) + var(--safe-bot) + 22px);
  transform: translateX(-50%);
  background: var(--fg); color: var(--bg);
  padding: 11px 18px; border-radius: 24px;
  font-size: 14px; font-weight: 550;
  max-width: 88vw; text-align: center;
  box-shadow: 0 6px 22px rgba(0,0,0,.3);
  animation: pop .2s;
}
.toast[hidden] { display: none; }
@keyframes pop { from { opacity: 0; transform: translate(-50%, 8px); } }

.spin {
  display: inline-block; width: 13px; height: 13px;
  border: 2px solid var(--fg-3); border-top-color: transparent;
  border-radius: 50%; animation: sp .7s linear infinite;
  vertical-align: -2px; margin-right: 6px;
}
@keyframes sp { to { transform: rotate(360deg); } }
