:root {
  --bg: #f6f6f3;
  --card: #ffffff;
  --text: #141414;
  --muted: #8a8a85;
  --line: #e7e7e2;
  --accent: #141414;
  --accent-text: #ffffff;
  --cal: #ff7a45;
  --protein: #e5484d;
  --carbs: #e9a23b;
  --fat: #3e8ed0;
  --good: #2f9e62;
  --danger: #d93f3f;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111111;
    --card: #1c1c1e;
    --text: #f2f2f2;
    --muted: #8e8e93;
    --line: #2c2c2e;
    --accent: #f2f2f2;
    --accent-text: #111111;
  }
}

* { 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", system-ui, sans-serif;
  padding: env(safe-area-inset-top) 16px calc(96px + env(safe-area-inset-bottom));
  min-height: 100vh;
}
h1, h2, h3 { margin: 16px 0 10px; }
h2 { font-size: 22px; }
h3 { font-size: 17px; }
small { display: block; color: var(--muted); font-size: 12px; }
.muted { color: var(--muted); }
.center { text-align: center; }
[hidden] { display: none !important; }

.card { background: var(--card); border-radius: 18px; padding: 16px; margin-bottom: 12px; }
.hint { color: var(--text); border: 1px dashed var(--muted); }

.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--text);
  font: inherit; font-weight: 600; padding: 12px 16px; border-radius: 14px; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--danger); width: 100%; margin-top: 8px; }
.icon-btn { background: none; border: 0; color: var(--text); font-size: 30px; padding: 0 12px; cursor: pointer; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 12px; width: 100%; min-width: 0;
}
.row { display: flex; gap: 8px; align-items: center; }
.row > .btn { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form label, #step-result label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.form label input, .form label select, #step-result label input { margin-top: 4px; }
.form .btn { width: 100%; margin-top: 6px; }
label.check { display: flex !important; gap: 10px; align-items: center; color: var(--text) !important; }
label.check input { width: auto; margin: 0; }

/* Today */
.datebar { display: flex; align-items: center; justify-content: space-between; }
.datebar h2 { margin: 12px 0; }
.hero { position: relative; display: flex; flex-direction: column; align-items: center; padding-top: 20px; }
.ring { width: 190px; height: 190px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 10; stroke-linecap: round; }
.ring-bg { stroke: var(--line); }
.ring-fg { stroke: var(--cal); transition: stroke-dashoffset .5s ease; }
.ring.over .ring-fg { stroke: var(--danger); }
.ring-text { position: absolute; top: 20px; height: 190px; display: flex; flex-direction: column; justify-content: center; align-items: center; }
.big { font-size: 40px; font-weight: 700; font-variant-numeric: tabular-nums; }
.hero-stats, .activity, .stats3 { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; width: 100%; margin-top: 12px; }
.activity { grid-template-columns: 1fr 1fr; margin-top: 0; }
.hero-stats span, .activity span, .stats3 span { font-weight: 700; font-size: 18px; font-variant-numeric: tabular-nums; }

.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.macro { padding: 12px; margin-bottom: 12px; }
.macro b { font-size: 18px; display: block; margin: 2px 0 6px; }
.macro span { font-size: 12px; }
.bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-bottom: 6px; }
.bar i { display: block; height: 100%; width: 0; border-radius: 3px; transition: width .5s ease; }
[data-macro=protein] .bar i { background: var(--protein); }
[data-macro=carbs] .bar i { background: var(--carbs); }
[data-macro=fat] .bar i { background: var(--fat); }

.meals, .list, .items { list-style: none; margin: 0; padding: 0; }
.meals li { display: flex; gap: 12px; align-items: center; background: var(--card); border-radius: 16px; padding: 10px; margin-bottom: 10px; }
.meals img, .meals .ph { width: 56px; height: 56px; border-radius: 12px; object-fit: cover; flex: none; background: var(--line); display: grid; place-items: center; font-size: 24px; }
.meals .info { flex: 1; min-width: 0; }
.meals .info b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meals .del { background: none; border: 0; color: var(--muted); font-size: 22px; padding: 8px; cursor: pointer; }
.p { color: var(--protein); } .c { color: var(--carbs); } .f { color: var(--fat); }

/* Progress */
#weight-unit { flex: none; }
#weight-form .btn { flex: none; }
#weight-chart { width: 100%; height: auto; margin-top: 12px; overflow: visible; }
#weight-chart polyline { fill: none; stroke: var(--text); stroke-width: 2; vector-effect: non-scaling-stroke; }
#weight-chart circle { fill: var(--text); }
#weight-chart text { fill: var(--muted); font-size: 10px; }
.list li { display: flex; justify-content: space-between; padding: 10px 4px; border-bottom: 1px solid var(--line); }

/* Nav */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabs button { background: none; border: 0; color: var(--muted); font: inherit; font-weight: 600; padding: 14px 0; cursor: pointer; }
.tabs button.active { color: var(--text); }
.fab {
  position: fixed; right: 20px; bottom: calc(72px + env(safe-area-inset-bottom)); width: 60px; height: 60px;
  border-radius: 30px; border: 0; background: var(--accent); color: var(--accent-text); font-size: 34px;
  line-height: 1; box-shadow: 0 6px 20px rgba(0,0,0,.25); cursor: pointer;
}

/* Sheet */
dialog {
  border: 0; border-radius: 22px; padding: 18px; width: min(440px, calc(100vw - 24px));
  max-height: calc(100vh - 40px); background: var(--card); color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { margin-top: 0; }
.photo-btn { display: block; text-align: center; margin-bottom: 10px; }
#note-input { margin-bottom: 10px; resize: none; }
#preview, #result-photo { width: 100%; max-height: 220px; object-fit: cover; border-radius: 14px; margin-bottom: 10px; }
.title-input { font-size: 20px; font-weight: 700; margin-bottom: 12px; }
.items li { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.items li span:last-child { color: var(--muted); white-space: nowrap; }
#step-result .row { margin-top: 12px; }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--text); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }

.login { margin-top: 30vh; }
.login form { display: grid; gap: 10px; }

#toast {
  position: fixed; left: 50%; bottom: calc(150px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  max-width: calc(100vw - 32px); z-index: 10;
}
code { word-break: break-all; }
.error { color: var(--danger); font-size: 14px; margin: 0 0 10px; }
