:root {
  --bg: #f6f7f2;
  --card: #ffffff;
  --text: #1d2a1f;
  --muted: #6b766c;
  --line: #e3e7de;
  --accent: #2f8f5b;
  --accent-soft: #e2f2e8;
  --warn: #c2571a;
  --warn-soft: #fbe9dd;
  --protein: #3b7dd8;
  --carbs: #e0a326;
  --fat: #c4568a;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 2px 8px rgba(0, 0, 0, .04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121612;
    --card: #1b211c;
    --text: #e6ebe4;
    --muted: #96a197;
    --line: #2c352d;
    --accent: #4cbb7f;
    --accent-soft: #1f3a2a;
    --warn: #f08a4b;
    --warn-soft: #3b2619;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h2 { font-size: 1.35rem; margin: 0 0 4px; }
h3 { font-size: 1.05rem; margin: 0 0 10px; }
p { margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 24px;
  padding: 0 16px;
}
.brand { font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.tabs button {
  border: 0; background: none; color: var(--muted);
  font: inherit; font-weight: 600;
  padding: 16px 12px; cursor: pointer; white-space: nowrap;
  border-bottom: 3px solid transparent;
}
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }
@media (max-width: 760px) {
  .topbar { flex-direction: column; align-items: stretch; gap: 0; padding-top: 10px; }
  .tabs button { padding: 10px 10px 12px; }
}

main { max-width: 960px; margin: 0 auto; padding: 20px 16px 60px; }
.view { display: none; }
.view.active { display: block; }

/* Layout */
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.section-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.stack > * + * { margin-top: 16px; }

/* Controls */
.btn {
  border: 1px solid var(--accent); background: var(--accent); color: #fff;
  font: inherit; font-weight: 600; border-radius: 10px;
  padding: 8px 14px; cursor: pointer;
}
.btn:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.small { padding: 4px 10px; font-size: .85rem; }
.btn.danger { background: transparent; color: var(--warn); border-color: var(--warn); }
.icon-btn {
  border: 0; background: none; color: var(--muted); cursor: pointer;
  font-size: 1rem; padding: 4px 6px; border-radius: 6px;
}
.icon-btn:hover { background: var(--line); color: var(--text); }
input, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
  min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); font-weight: 600; }
label.field input, label.field select { color: var(--text); font-weight: 400; font-size: 15px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.check { display: inline-flex; gap: 6px; align-items: center; cursor: pointer; }

/* Today */
.day-nav { display: flex; align-items: center; gap: 4px; }
.day-nav strong { min-width: 150px; text-align: center; }
.summary { display: flex; gap: 24px; align-items: center; flex-wrap: wrap; }
.ring { position: relative; width: 150px; height: 150px; flex: none; }
.ring svg { transform: rotate(-90deg); }
.ring-label { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.ring-label b { font-size: 1.8rem; line-height: 1.1; }
.macros { flex: 1; min-width: 220px; }
.macro + .macro { margin-top: 10px; }
.macro-top { display: flex; justify-content: space-between; font-size: .9rem; }
.bar { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; margin-top: 4px; }
.bar > span { display: block; height: 100%; border-radius: 99px; }

.meal-card h3 { display: flex; justify-content: space-between; align-items: center; margin: 0; }
.meal-card h3 small { color: var(--muted); font-weight: 500; font-size: .85rem; }
.items { list-style: none; padding: 0; margin: 10px 0; }
.items li { display: flex; align-items: center; gap: 8px; padding: 7px 0; border-top: 1px solid var(--line); }
.items li:first-child { border-top: 0; }
.items .name { flex: 1; min-width: 0; }
.items .name span { display: block; color: var(--muted); font-size: .82rem; }
.items .kcal { font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }
.empty { color: var(--muted); font-size: .9rem; padding: 6px 0; }

.water { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.glass {
  width: 34px; height: 42px; border: 2px solid var(--protein); border-top-width: 0;
  border-radius: 0 0 8px 8px; background: transparent; cursor: pointer; padding: 0;
  position: relative; overflow: hidden;
}
.glass.full::after { content: ""; position: absolute; inset: 30% 0 0 0; background: var(--protein); opacity: .75; }
.tip { background: var(--accent-soft); border-color: transparent; }
.tip b { color: var(--accent); }
.streak { font-size: 1.6rem; font-weight: 700; }

/* Plan */
.plan-totals { display: flex; gap: 20px; flex-wrap: wrap; align-items: baseline; }
.plan-totals b { font-size: 1.4rem; }
.tag { display: inline-block; font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 1px 7px; border-radius: 99px; background: var(--line); color: var(--muted); }
.tag.like { background: var(--accent-soft); color: var(--accent); }

/* Foods */
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 99px; padding: 5px 12px; font: inherit; font-size: .88rem; cursor: pointer;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.food-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
@media (max-width: 400px) { .food-grid { gap: 8px; } .food .info { padding: 8px 9px 2px; } }
.food { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.food.like { border-color: var(--accent); background: var(--accent-soft); box-shadow: 0 0 0 1px var(--accent); }
.food.dislike { opacity: .5; }
.food-photo { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: var(--line); }
.food-photo.ph { display: flex; align-items: center; justify-content: center; font-size: 2.6rem; }
.food .info { flex: 1; padding: 10px 12px 4px; }
.food .info b { display: block; line-height: 1.3; margin-bottom: 2px; }
.food .info span { display: block; font-size: .8rem; color: var(--muted); }
.vote { display: flex; gap: 2px; padding: 0 8px 8px; }
.vote .on-plain { filter: none; opacity: .6; margin-left: auto; }
.vote button { border: 1px solid transparent; background: none; font-size: 1.15rem; padding: 4px 6px; border-radius: 8px; cursor: pointer; filter: grayscale(1); opacity: .6; }
.vote button.on { filter: none; opacity: 1; background: var(--card); border-color: var(--line); }

/* Progress */
.stat b { display: block; font-size: 1.5rem; line-height: 1.2; }
.stat span { font-size: .82rem; color: var(--muted); }
.good { color: var(--accent); }
.chart { width: 100%; height: auto; display: block; }
.chart .grid-line { stroke: var(--line); }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .goal { stroke: var(--accent); stroke-dasharray: 6 5; stroke-width: 1.5; }
.chart .avg { fill: none; stroke: var(--text); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--protein); }
.chart .kbar { fill: var(--accent); }
.chart .kbar.over { fill: var(--warn); }
.chart .target { stroke: var(--text); stroke-dasharray: 4 4; opacity: .5; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: .82rem; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 12px; height: 3px; vertical-align: middle; margin-right: 6px; border-radius: 2px; }
.weights-list { max-height: 260px; overflow-y: auto; }

.notice { border-radius: 10px; padding: 10px 12px; background: var(--warn-soft); color: var(--warn); font-size: .9rem; }
.notice.info { background: var(--accent-soft); color: var(--accent); }

/* Dialog */
dialog {
  border: 0; border-radius: var(--radius); padding: 0;
  width: min(560px, calc(100vw - 32px)); max-height: 85vh;
  background: var(--card); color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.dialog-head h3 { margin: 0; }
.dialog-body { padding: 14px 16px 16px; }
.dialog-body #addSearch { flex: 1; }
.servings { display: flex; gap: 6px; align-items: center; font-size: .85rem; color: var(--muted); }
.servings input { width: 76px; }
.add-results { margin: 12px 0; max-height: 46vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; }
.add-results button {
  display: flex; width: 100%; gap: 8px; justify-content: space-between; align-items: center;
  border: 0; border-top: 1px solid var(--line); background: none; color: var(--text);
  font: inherit; text-align: left; padding: 9px 12px; cursor: pointer;
}
.add-results button:first-child { border-top: 0; }
.add-results button:hover { background: var(--accent-soft); }
.add-results small { color: var(--muted); }
.quick-add summary { cursor: pointer; color: var(--muted); font-size: .9rem; margin-bottom: 8px; }
.quick-add input[name=name] { flex: 1; }
.quick-add input[name=kcal] { width: 90px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-weight: 600; opacity: 0; pointer-events: none; transition: .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Accounts */
body.loading .topbar, body.loading main > * { visibility: hidden; }
body.logged-out .tabs, body.logged-out .user-box { display: none; }
.user-box { margin-left: auto; display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.sync { font-size: .8rem; color: var(--muted); }
.sync.saved { color: var(--accent); }
.sync.offline { color: var(--warn); }
@media (max-width: 760px) {
  .user-box { position: absolute; top: 8px; right: 16px; }
  .user-box #userName { display: none; }
}
.auth-card { max-width: 400px; margin: 40px auto; text-align: center; padding: 28px; }
.auth-card input { width: 100%; }
.link { border: 0; background: none; color: var(--accent); font: inherit; font-weight: 600; padding: 0; cursor: pointer; text-decoration: underline; }

/* Photos */
.thumb { width: 40px; height: 40px; border-radius: 9px; object-fit: cover; flex: none; background: var(--line); }
.thumb.ph { display: inline-flex; align-items: center; justify-content: center; font-size: 1.25rem; }
.add-results .grow { flex: 1; min-width: 0; }
.add-results button { justify-content: flex-start; }
.photo-pick { display: flex; align-items: center; gap: 14px; }
.photo-drop {
  width: 96px; height: 96px; border: 2px dashed var(--line); border-radius: 12px; flex: none;
  display: flex; align-items: center; justify-content: center; text-align: center;
  cursor: pointer; overflow: hidden; color: var(--muted); font-size: 1.6rem; line-height: 1.1;
}
.photo-drop small { font-size: .75rem; }
.photo-drop img { width: 100%; height: 100%; object-fit: cover; }
.credits { padding-left: 18px; columns: 2 260px; }
.credits a { color: inherit; }
.field.wide { grid-column: span 2; }
@media (max-width: 480px) { .field.wide { grid-column: auto; } }

/* Planned meal hint on Today */
.planned-hint {
  display: flex; align-items: center; gap: 10px; justify-content: space-between;
  background: var(--accent-soft); border-radius: 10px; padding: 8px 10px; margin-bottom: 10px; font-size: .88rem;
}
.planned-hint .btn { white-space: nowrap; }

/* Calendar */
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.cal-head { text-align: center; font-size: .78rem; font-weight: 700; color: var(--muted); padding-bottom: 2px; }
.cal-cell {
  position: relative; aspect-ratio: 1 / 1; min-height: 52px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text);
  font: inherit; padding: 6px; cursor: pointer; text-align: left;
  display: flex; flex-direction: column; justify-content: space-between;
}
.cal-cell.blank { border: 0; background: none; cursor: default; }
.cal-cell:hover { border-color: var(--accent); }
.cal-num { font-weight: 700; font-size: .9rem; }
.cal-kcal { font-size: .72rem; color: var(--muted); }
.cal-icons { position: absolute; top: 4px; right: 5px; font-size: .7rem; }
.cal-cell.good { background: var(--accent-soft); border-color: transparent; }
.cal-cell.good .cal-kcal { color: var(--accent); font-weight: 600; }
.cal-cell.over { background: var(--warn-soft); border-color: transparent; }
.cal-cell.over .cal-kcal { color: var(--warn); font-weight: 600; }
.cal-cell.partial { background: repeating-linear-gradient(135deg, var(--accent-soft) 0 6px, transparent 6px 12px); }
.cal-cell.planned { border-style: dashed; border-color: var(--accent); }
.cal-cell.future { opacity: .75; }
.cal-cell.today { box-shadow: inset 0 0 0 2px var(--text); }
.cal-cell.selected { outline: 3px solid var(--accent); outline-offset: 1px; }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; }
.sw.good { background: var(--accent-soft); border: 1px solid var(--accent); }
.sw.over { background: var(--warn-soft); border: 1px solid var(--warn); }
.sw.partial { background: repeating-linear-gradient(135deg, var(--accent-soft) 0 3px, transparent 3px 6px); border: 1px solid var(--line); }
.sw.planned { border: 1px dashed var(--accent); }
@media (max-width: 520px) {
  .cal-grid { gap: 3px; }
  .cal-cell { padding: 3px 4px; min-height: 44px; border-radius: 7px; }
  .cal-kcal { font-size: .62rem; }
  .cal-icons { font-size: .6rem; top: 2px; right: 2px; }
}

/* Daily guide, check-in, checklist, weigh-in */
.guide { border-color: var(--accent); }
.guide ol { margin: 0 0 10px; padding-left: 20px; }
.guide li + li { margin-top: 6px; }
.checkin { border: 2px solid var(--accent); }
.checklist { display: flex; flex-wrap: wrap; gap: 8px; }
.check-item {
  display: inline-flex; align-items: center; gap: 8px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
  border-radius: 99px; padding: 7px 14px 7px 8px; font: inherit; font-size: .9rem; cursor: pointer;
}
.check-item .tick {
  width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--line);
  display: inline-flex; align-items: center; justify-content: center; font-size: .75rem; font-weight: 800;
}
.check-item.done { background: var(--accent-soft); border-color: transparent; color: var(--muted); }
.check-item.done .tick { background: var(--accent); border-color: var(--accent); color: #fff; }
.weigh { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.weigh .grow { flex: 1; min-width: 160px; display: flex; flex-direction: column; }
.weigh input { width: 110px; font-size: 1.1rem; font-weight: 600; }
.weigh-icon { font-size: 1.8rem; }
.weigh.done { background: var(--accent-soft); border-color: transparent; }
.save-meal { margin-bottom: 10px; }
.save-meal input { flex: 1; }

/* Add dialog extras */
.add-head { padding: 8px 12px 4px; font-size: .78rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; background: var(--bg); }
.online { margin: -4px 0 12px; }
.add-results.off:empty { display: none; }
.add-results.off { margin-top: 10px; }
.scan { margin-top: 10px; }
.scan video { width: 100%; max-height: 240px; object-fit: cover; border-radius: 10px; background: #000; }
.scan form input { flex: 1; }

/* Calendar weight */
.cal-kg { font-size: .72rem; font-weight: 700; color: var(--protein); }
.cal-kg-legend { color: var(--protein); font-size: .78rem; }
.day-weight input { width: 100px; }

/* Week planner */
.week-strip { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.week-day {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text);
  font: inherit; padding: 6px 2px; cursor: pointer; min-width: 0;
}
.week-day span { font-size: .72rem; color: var(--muted); font-weight: 600; }
.week-day b { font-size: 1.05rem; }
.week-day small { font-size: .68rem; color: var(--muted); min-height: 1em; }
.week-day.has { background: var(--accent-soft); border-color: transparent; }
.week-day.has small { color: var(--accent); font-weight: 600; }
.week-day.past { opacity: .55; }
.week-day.today { box-shadow: inset 0 0 0 2px var(--text); }
.week-day.selected { outline: 3px solid var(--accent); outline-offset: 1px; }

/* Journey */
.hero { background: var(--accent-soft); border-color: transparent; }
.hero-big { font-size: 2.4rem; font-weight: 800; color: var(--accent); line-height: 1.1; }
.hero p { margin: 4px 0 0; }
.milestones { list-style: none; margin: 0; padding: 0; }
.milestones li { display: flex; gap: 12px; align-items: center; padding: 8px 0; position: relative; }
.milestones li + li::before { content: ""; position: absolute; left: 11px; top: -12px; height: 20px; border-left: 2px solid var(--line); }
.milestones .name span { display: block; font-size: .82rem; color: var(--muted); }
.ms-dot {
  width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--line); flex: none;
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: .8rem; background: var(--card);
}
.milestones li.reached .ms-dot { background: var(--accent); border-color: var(--accent); color: #fff; }
.milestones li.reached b { color: var(--accent); }
.mtable { width: 100%; border-collapse: collapse; font-size: .9rem; }
.mtable th { text-align: left; color: var(--muted); font-size: .78rem; font-weight: 700; padding: 4px 6px; }
.mtable td { border-top: 1px solid var(--line); padding: 6px; }
.mtable tr.change td { font-weight: 700; color: var(--accent); }
.weights-list small { font-size: .78rem; color: var(--muted); margin-left: 4px; }
.weights-list small.good { color: var(--accent); }
