:root {
  --bg: #0d0f14;
  --surface: #161a22;
  --surface-2: #1e2430;
  --line: #262d3a;
  --text: #e8ecf3;
  --muted: #8b95a7;
  --accent: #34d399;
  --accent-dim: #10795a;
  --protein: #60a5fa;
  --carbs: #fbbf24;
  --fat: #f472b6;
  --danger: #f87171;
  --radius: 16px;
  --tab-h: 62px;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f7fa; --surface: #ffffff; --surface-2: #eef1f6; --line: #dde3ec;
    --text: #131720; --muted: #64708a; --accent: #0f9d6e; --accent-dim: #9decc9;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  padding-bottom: calc(var(--tab-h) + env(safe-area-inset-bottom));
  overscroll-behavior-y: contain;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
h1,h2,h3 { margin: 0; font-weight: 650; letter-spacing: -0.02em; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.screen { display: none; padding: 14px 14px 24px; max-width: 620px; margin: 0 auto; }
.screen.active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 8px 0 14px;
}
.topbar h1 { font-size: 19px; }
.topbar .sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.iconbtn {
  width: 38px; height: 38px; border-radius: 11px; background: var(--surface);
  display: grid; place-items: center; font-size: 17px; color: var(--muted);
  border: 1px solid var(--line);
}
.iconbtn:active { background: var(--surface-2); }

.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 15px; margin-bottom: 12px;
}
.card h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); margin-bottom: 11px; }

/* ---------- calorie ring ---------- */
.ring-wrap { display: flex; align-items: center; gap: 18px; }
.ring { position: relative; width: 132px; height: 132px; flex: none; }
.ring svg { transform: rotate(-90deg); display: block; }
.ring .val {
  position: absolute; inset: 0; display: grid; place-content: center; text-align: center;
}
.ring .num { font-size: 29px; font-weight: 700; letter-spacing: -0.03em; line-height: 1; }
.ring .lbl { font-size: 11px; color: var(--muted); margin-top: 3px; text-transform: uppercase; letter-spacing: .06em; }
.ring.over .num { color: var(--danger); }

.macros { flex: 1; display: grid; gap: 11px; min-width: 0; }
.macro-row { display: grid; grid-template-columns: 42px 1fr auto; gap: 9px; align-items: center; font-size: 12.5px; }
.macro-row .name { color: var(--muted); }
.macro-row .amt { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 11.5px; }
.bar { height: 7px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 99px; transition: width .35s ease; }

/* ---------- meals ---------- */
.meal { margin-bottom: 10px; }
.meal-head {
  display: flex; align-items: baseline; justify-content: space-between;
  padding: 4px 2px 7px; font-size: 13px;
}
.meal-head .t { font-weight: 650; text-transform: capitalize; }
.meal-head .k { color: var(--muted); font-variant-numeric: tabular-nums; }
.entry {
  display: flex; align-items: center; gap: 11px; padding: 11px 13px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 13px; margin-bottom: 7px;
}
.entry .body { flex: 1; min-width: 0; }
.entry .nm { font-size: 14.5px; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .meta { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.entry .kc { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 14.5px; }
.entry .src { font-size: 13px; opacity: .75; }
.entry.pressed { background: var(--surface-2); }
.empty { color: var(--muted); font-size: 13px; padding: 9px 2px; }

/* ---------- buttons ---------- */
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 13px 16px; border-radius: 13px; font-weight: 600; font-size: 15px;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
}
.btn:active { transform: scale(.985); }
.btn.primary { background: var(--accent); color: #04231a; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; }
.btn-row { display: flex; gap: 9px; }
.btn-row .btn { flex: 1; }

.add-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 11px; }
.add-tile {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 17px 14px; text-align: left; display: flex; flex-direction: column; gap: 6px;
}
.add-tile:active { background: var(--surface-2); }
.add-tile .ico { font-size: 24px; }
.add-tile .t { font-weight: 650; font-size: 14.5px; }
.add-tile .d { font-size: 11.5px; color: var(--muted); line-height: 1.35; }

/* ---------- forms ---------- */
label.field { display: block; margin-bottom: 13px; }
label.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
input[type=text], input[type=number], input[type=date], select, textarea {
  width: 100%; padding: 11px 13px; border-radius: 11px;
  background: var(--surface-2); border: 1px solid var(--line);
  -webkit-appearance: none; appearance: none;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-dim); outline-offset: -1px; }
textarea { min-height: 92px; resize: vertical; }
.seg { display: flex; gap: 6px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.seg button {
  flex: 1; padding: 9px 6px; border-radius: 9px; font-size: 13px; font-weight: 600; color: var(--muted);
}
.seg button.on { background: var(--accent); color: #04231a; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------- estimate review ---------- */
.est-item {
  display: grid; grid-template-columns: 1fr auto auto; gap: 9px; align-items: center;
  padding: 10px 12px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; margin-bottom: 7px;
}
.est-item input.kc { width: 78px; text-align: right; padding: 7px 9px; font-variant-numeric: tabular-nums; }
.est-item .nm { font-size: 14px; font-weight: 550; }
.est-item .po { font-size: 11.5px; color: var(--muted); }
.est-item .rm { color: var(--muted); font-size: 17px; padding: 0 3px; }
.conf { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; color: var(--muted); }
.dot { width: 7px; height: 7px; border-radius: 99px; background: var(--accent); }
.dot.medium { background: var(--carbs); } .dot.low { background: var(--danger); }
.thumb { width: 100%; max-height: 210px; object-fit: cover; border-radius: 13px; margin-bottom: 11px; }

/* ---------- charts ---------- */
.chart { width: 100%; height: 168px; display: block; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .trend { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.chart .pts { fill: var(--muted); }
.chart .goal { stroke: var(--accent); stroke-dasharray: 4 4; opacity: .55; }
.chart .bar-k { fill: var(--surface-2); }
.chart .bar-k.over { fill: var(--danger); opacity: .8; }
.chart .bar-k.under { fill: var(--accent); opacity: .75; }
.chart .tgt { stroke: var(--muted); stroke-dasharray: 3 3; }
.axis { font-size: 10px; fill: var(--muted); }

.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 13px; padding: 12px; }
.stat .v { font-size: 20px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat .l { font-size: 11px; color: var(--muted); margin-top: 3px; }
.stat .v.good { color: var(--accent); }
.stat .v.bad { color: var(--danger); }

/* ---------- tabbar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(4, 1fr);
}
.tabbar button {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; color: var(--muted); font-size: 10.5px; font-weight: 600;
}
.tabbar button .ico { font-size: 20px; line-height: 1; }
.tabbar button.on { color: var(--accent); }

/* ---------- sheet / toast ---------- */
.sheet-bg {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s ease;
}
.sheet {
  background: var(--bg); border-radius: 20px 20px 0 0; width: 100%; max-width: 620px;
  max-height: 88vh; overflow-y: auto; padding: 18px 15px calc(20px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); animation: up .22s cubic-bezier(.2,.8,.3,1);
}
@keyframes up { from { transform: translateY(100%); } to { transform: none; } }
.sheet h2 { font-size: 17px; margin-bottom: 14px; }
.grabber { width: 38px; height: 4px; border-radius: 99px; background: var(--line); margin: -6px auto 13px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tab-h) + 18px + env(safe-area-inset-bottom));
  background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
  padding: 11px 17px; border-radius: 12px; z-index: 90; font-size: 13.5px; font-weight: 550;
  max-width: 88vw; text-align: center; box-shadow: 0 8px 28px rgba(0,0,0,.35);
  animation: fade .16s ease;
}
.toast.err { border-color: var(--danger); color: var(--danger); }

.spinner {
  width: 17px; height: 17px; border: 2px solid var(--line);
  border-top-color: var(--accent); border-radius: 99px; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.thinking { display: flex; flex-direction: column; align-items: center; gap: 13px; padding: 34px 0; color: var(--muted); font-size: 13.5px; }
.thinking .spinner { width: 27px; height: 27px; border-width: 3px; }

/* ---------- login gate ---------- */
.gate {
  position: fixed; inset: 0; z-index: 100; background: var(--bg);
  display: flex; align-items: center; justify-content: center; padding: 22px;
}
.gate-box { width: 100%; max-width: 340px; text-align: center; }
.gate-mark {
  font-size: 30px; font-weight: 700; letter-spacing: -.04em;
  color: var(--accent); margin-bottom: 22px;
}
.gate h2 { font-size: 19px; margin-bottom: 6px; }
.gate p { color: var(--muted); font-size: 13.5px; margin: 0 0 20px; }
.gate form { display: grid; gap: 10px; }
.gate input { text-align: center; }
.gate-err { color: var(--danger); font-size: 13px; min-height: 19px; margin-top: 12px; }
