:root {
  --bg: #f5f6f4;
  --card: #ffffff;
  --text: #17201a;
  --muted: #6b766e;
  --line: #e3e7e3;
  --accent: #16a34a;
  --accent-soft: #dcfce7;
  --warn: #dc2626;
  --protein: #2563eb;
  --fat: #d97706;
  --carbs: #9333ea;
  --radius: 16px;
  --shadow: 0 1px 3px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1411;
    --card: #182019;
    --text: #e8eee9;
    --muted: #93a096;
    --line: #2a342c;
    --accent: #22c55e;
    --accent-soft: #14301f;
    --warn: #f87171;
    --protein: #60a5fa;
    --fat: #fbbf24;
    --carbs: #c084fc;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0 0 12px; line-height: 1.2; }
h2 { font-size: 22px; }
h3 { font-size: 17px; }
main { max-width: 560px; margin: 0 auto; padding: 16px 16px calc(90px + env(safe-area-inset-bottom)); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.error { color: var(--warn); min-height: 1.2em; margin: 8px 0 0; }
.ok { color: var(--accent); min-height: 1.2em; margin: 8px 0 0; }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }

input, select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; color: var(--text); background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; padding: 11px 14px; cursor: pointer; text-align: center;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--warn); }
.btn.big { width: 100%; padding: 16px; font-size: 17px; }
.btn:disabled { opacity: .6; cursor: default; }
.link { background: none; border: 0; color: var(--accent); font: inherit; cursor: pointer; padding: 8px; }
.icon { background: none; border: 0; font-size: 28px; color: var(--text); width: 44px; height: 44px; cursor: pointer; border-radius: 50%; }
.icon:disabled { opacity: .25; }

/* auth */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; text-align: center; }
.auth-card .logo { font-size: 56px; }
.auth-card form { display: grid; gap: 10px; margin-top: 20px; }

/* today */
.day-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.day-nav h2 { margin: 0; font-size: 19px; }
.summary { display: flex; gap: 16px; align-items: center; }
.ring-wrap { position: relative; width: 132px; height: 132px; flex: none; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 11; }
.ring-bg { stroke: var(--line); }
.ring-fg { stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset .5s, stroke .3s; }
.ring-fg.over { stroke: var(--warn); }
.ring-text { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ring-text b { font-size: 28px; }
.ring-text span { font-size: 12px; color: var(--muted); }
.macros { flex: 1; min-width: 0; display: grid; gap: 7px; }
.eaten { font-weight: 600; margin-bottom: 2px; }
.macro { display: grid; grid-template-columns: 70px 1fr auto; gap: 8px; align-items: center; font-size: 13px; }
.macro label { color: var(--muted); }
.macro span { font-variant-numeric: tabular-nums; white-space: nowrap; }
.bar { height: 7px; border-radius: 4px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; border-radius: 4px; transition: width .5s; }
.macro[data-k="protein"] i { background: var(--protein); }
.macro[data-k="fat"] i { background: var(--fat); }
.macro[data-k="carbs"] i { background: var(--carbs); }
.feedback { margin: 0 4px 14px; font-size: 15px; }
.actions { display: grid; gap: 10px; margin-bottom: 18px; }

.meals { display: grid; gap: 10px; }
.meal { display: flex; gap: 12px; align-items: center; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px; cursor: pointer; }
.meal img, .meal .ph { width: 64px; height: 64px; border-radius: 12px; object-fit: cover; flex: none; background: var(--accent-soft); display: flex; align-items: center; justify-content: center; font-size: 28px; }
.meal .info { flex: 1; min-width: 0; }
.meal .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meal .sub { color: var(--muted); font-size: 13px; }
.meal .kcal { font-weight: 700; white-space: nowrap; }

/* sheet */
.sheet { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet-body {
  background: var(--card); width: 100%; max-width: 560px; max-height: 92vh; overflow: auto;
  border-radius: 20px 20px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-body .preview { width: 100%; max-height: 300px; object-fit: cover; border-radius: 14px; margin-bottom: 12px; }
.sheet-body .grid { display: grid; gap: 10px; }
.items { display: grid; gap: 8px; margin: 10px 0; }
.item { border: 1px solid var(--line); border-radius: 12px; padding: 10px; display: grid; gap: 8px; }
.item .top { display: flex; gap: 8px; align-items: center; }
.item .top input { flex: 1; }
.item .nums { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.item .nums label { font-size: 11px; color: var(--muted); display: grid; gap: 2px; }
.item .nums input { padding: 8px 6px; font-size: 14px; }
.item .del { background: none; border: 0; color: var(--muted); font-size: 20px; cursor: pointer; padding: 4px 8px; }
.total-line { font-weight: 700; display: flex; justify-content: space-between; margin: 6px 0 12px; }
.note { background: var(--accent-soft); border-radius: 12px; padding: 10px 12px; font-size: 14px; margin-bottom: 10px; }
.spinner { width: 42px; height: 42px; border: 4px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; margin: 20px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* history */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; margin-bottom: 12px; }
.stats b { display: block; font-size: 22px; }
.stats span { font-size: 12px; color: var(--muted); }
.chart svg { width: 100%; height: auto; display: block; }
.chart .b { fill: var(--accent); cursor: pointer; }
.chart .b.over { fill: var(--warn); }
.chart .b.empty { fill: var(--line); }
.chart .goal { stroke: var(--muted); stroke-dasharray: 4 4; }
.chart .wl { fill: none; stroke: var(--accent); stroke-width: 2.5; }
.chart .wd { fill: var(--accent); }
.chart text { fill: var(--muted); font-size: 10px; }
#weight-form { margin-bottom: 12px; }
.day-list { display: grid; gap: 8px; }
.day-row { display: flex; justify-content: space-between; align-items: center; background: var(--card); border-radius: 12px; padding: 12px 14px; cursor: pointer; box-shadow: var(--shadow); }
.day-row .d { font-weight: 600; }
.day-row .v { font-variant-numeric: tabular-nums; }
.day-row .v.over { color: var(--warn); }
.day-row .v.ok { color: var(--accent); }

/* profile */
.form { display: grid; gap: 12px; }
.form label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.norm { background: var(--accent-soft); border-radius: 12px; padding: 12px; font-size: 14px; }
.norm b { font-size: 20px; }

/* tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; justify-content: center;
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabs button { flex: 1; max-width: 186px; background: none; border: 0; padding: 8px 0 10px; font-size: 22px; color: var(--muted); cursor: pointer; display: grid; justify-items: center; gap: 2px; filter: grayscale(1); opacity: .7; }
.tabs button span { font-size: 11px; font-weight: 600; }
.tabs button.active { color: var(--accent); filter: none; opacity: 1; }

.toast { position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; z-index: 30; max-width: calc(100% - 32px); font-size: 14px; }

@media (max-width: 400px) {
  .summary { flex-direction: column; }
  .macros { width: 100%; }
  .item .nums { gap: 4px; }
  .item .nums input { padding: 8px 4px; }
}
