:root {
  --bg: #000;
  --card: #141416;
  --card-edge: #2a2a2e;
  --text: #eceff1;
  --muted: #9aa0a6;
  --green: #4caf7d;
  --protein: #ef8354;
  --fat: #d4a528;
  --carbs: #42a5f5;
  --fiber: #8bc34a;
  --water: #4fc3f7;
  --red: #ef5350;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body {
  padding: env(safe-area-inset-top) 16px calc(88px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 0;
}
.logo { width: 34px; height: 34px; border-radius: 9px; }
.title { flex: 1; }
h1 { font-size: 20px; margin: 0; line-height: 1.1; }
.icon-button { background: none; border: 0; color: var(--muted); font-size: 24px; padding: 6px; }
.icon-button.nav { font-size: 30px; line-height: 1; padding: 2px 8px 6px; }
.icon-button:disabled { opacity: 0.25; }
.title { min-width: 0; }
.link { background: none; border: 0; padding: 0 0 0 4px; color: var(--green); font-size: 13px; }

main { display: flex; flex-direction: column; gap: 12px; }

.card {
  background: linear-gradient(#1c1c20, var(--card));
  border: 1px solid var(--card-edge);
  border-radius: 18px;
  padding: 14px;
}

.row { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }

/* --- energy ring --- */
.energy { display: flex; align-items: center; gap: 18px; }
.ring { width: 120px; height: 120px; flex: none; transform: rotate(-90deg); }
.ring-track, .ring-fill { fill: none; stroke-width: 11; }
.ring-track { stroke: #26262b; }
.ring-fill { stroke: var(--green); stroke-linecap: round; transition: stroke-dasharray 0.4s; }
.energy.over .ring-fill { stroke: var(--red); }
.energy.over #kcal-left { color: var(--red); }
.big { font-size: 34px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }

/* --- bars --- */
.macros { display: flex; flex-direction: column; gap: 10px; }
.macro .row { margin-bottom: 4px; font-size: 14px; }
.bar { height: 8px; background: #26262b; border-radius: 4px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; border-radius: 4px; transition: width 0.4s; }
.bar-fill.protein { background: var(--protein); }
.bar-fill.fat { background: var(--fat); }
.bar-fill.carbs { background: var(--carbs); }
.bar-fill.fiber { background: var(--fiber); }
.bar-fill.water { background: var(--water); }
.bar-fill.over { background: var(--red); }
.extras { margin-top: 10px; }
.extras:empty { display: none; }

.water-buttons { display: flex; gap: 8px; margin-top: 10px; }
.pill {
  flex: 1;
  background: #1f2a30;
  border: 1px solid #2c3d46;
  border-radius: 12px;
  padding: 9px 0;
  font-size: 15px;
}
.pill:first-child { flex: 0.7; background: none; color: var(--muted); }
.pill:active { opacity: 0.6; }

.feed { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.feed li { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.feed img, .feed .no-thumb {
  width: 48px; height: 48px; flex: none; border-radius: 10px; object-fit: cover; background: #26262b;
  display: flex; align-items: center; justify-content: center; font-size: 20px;
}
.feed .what { flex: 1; min-width: 0; }
.feed .names { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.feed .kcal { font-variant-numeric: tabular-nums; }
.feed li.pending .names { color: var(--fat); }
.feed:empty { display: none; }
.feed:not(:empty) + #feed-empty { display: none; }

/* --- add bar --- */
.add-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  gap: 8px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--card-edge);
}
.add {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: var(--card);
  border: 1px solid var(--card-edge);
  border-radius: 14px;
  padding: 8px 0;
  font-size: 20px;
}
.add span { font-size: 12px; color: var(--muted); }
.add.primary { flex: 1.4; background: var(--green); border-color: var(--green); color: #04150c; }
.add.primary span { color: #04150c; font-weight: 600; }
.add:active { opacity: 0.7; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #2a2a2e;
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; }

/* --- dialogs --- */
dialog {
  width: min(100% - 24px, 480px);
  /* Clear of the iPhone status bar and home indicator: the app draws under them. */
  max-height: calc(100% - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  transform: translateY(calc((env(safe-area-inset-top) - env(safe-area-inset-bottom)) / 2));
  background: #141416;
  color: var(--text);
  border: 1px solid var(--card-edge);
  border-radius: 20px;
  padding: 18px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.7); }
dialog h2 { margin: 0 0 4px; font-size: 20px; }
form { display: flex; flex-direction: column; gap: 12px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
input, select, textarea {
  font: inherit;
  font-size: 16px; /* 16px stops iOS from zooming into the field */
  color: var(--text);
  background: #1f1f23;
  border: 1px solid var(--card-edge);
  border-radius: 10px;
  padding: 10px;
  width: 100%;
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.segmented { display: flex; background: #1f1f23; border-radius: 10px; padding: 3px; }
.segmented label { flex: 1; }
.segmented input { position: absolute; opacity: 0; width: 0; }
.segmented span { display: block; text-align: center; padding: 8px; border-radius: 8px; color: var(--text); }
.segmented input:checked + span { background: #3a3a40; }

.preview {
  background: #10261a;
  border: 1px solid #1f4a33;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 14px;
}
.preview b { font-size: 18px; }
.tip {
  background: #2b2210;
  border: 1px solid #5a4417;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 14px;
}
.tip:empty { display: none; }

.dialog-actions { display: flex; gap: 10px; margin-top: 4px; }
.dialog-actions button { flex: 1; }
.primary-button, .secondary {
  border-radius: 12px;
  padding: 11px;
  border: 1px solid var(--card-edge);
  background: #1f1f23;
}
.primary-button { background: var(--green); border-color: var(--green); color: #04150c; font-weight: 600; }
.wide { width: 100%; }
.scan {
  width: 100%; padding: 12px; font-size: 16px; font-weight: 600;
  color: var(--green); background: #10261a; border: 1px solid var(--green); border-radius: 12px;
}
#scanner-video { width: 100%; border-radius: 12px; background: #000; aspect-ratio: 1; object-fit: cover; }
code { font-size: 12px; color: var(--text); }
textarea { resize: none; }

/* --- entry (confirmation) --- */
.entry-head { display: flex; gap: 12px; align-items: center; }
#entry-thumb { width: 72px; height: 72px; border-radius: 12px; object-fit: cover; background: #26262b; flex: none; }
#entry-thumb[src=""], #entry-thumb:not([src]) { display: none; }
.entry-photos { display: flex; gap: 8px; align-items: center; }
.photo-list { display: flex; gap: 6px; flex: 1; min-width: 0; overflow-x: auto; }
.photo-list img { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; flex: none; }
.entry-photos button { flex: none; padding: 8px 12px; margin-left: auto; }
.entry-total { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.status-line { font-size: 14px; color: var(--muted); }
.status-line:empty { display: none; }
.status-line.busy::before {
  content: ""; display: inline-block; width: 12px; height: 12px; margin-right: 8px; vertical-align: -1px;
  border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.status-line.error { color: var(--red); }
@keyframes spin { to { transform: rotate(360deg); } }
.entry-items { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.entry-items li { background: #1a1a1e; border: 1px solid var(--card-edge); border-radius: 12px; padding: 8px; }
.item-top { display: flex; gap: 6px; align-items: center; }
.item-top .item-name { flex: 1; min-width: 0; padding: 7px 8px; }
.item-top .item-grams { width: 76px; flex: none; text-align: right; padding: 7px 8px; }
.item-top .unit { color: var(--muted); font-size: 14px; }
.item-remove { background: none; border: 0; color: var(--muted); font-size: 18px; padding: 4px 6px; }
.item-facts { margin-top: 5px; font-size: 13px; color: var(--muted); }
.item-facts .guess { color: var(--fat); }
.ask { display: flex; flex-direction: column; gap: 10px; }
.ask.has-question { background: #2b2210; border: 1px solid #5a4417; border-radius: 12px; padding: 12px; }
#entry-question:empty { display: none; }
.correct { display: flex; gap: 8px; }
.correct input { flex: 1; min-width: 0; }
.correct button { flex: none; padding: 0 12px; }
.danger { color: var(--red); }
.hidden { display: none !important; }

/* --- history --- */
#stats-dialog[open] { display: flex; flex-direction: column; gap: 12px; }
.stats-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat { background: #1a1a1e; border: 1px solid var(--card-edge); border-radius: 12px; padding: 8px 10px; }
.stat .label { font-size: 12px; color: var(--muted); }
.stat .value { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .sub { font-size: 12px; color: var(--muted); }
.stats-summary .note { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.stats-readout { min-height: 18px; color: var(--muted); font-variant-numeric: tabular-nums; }
.stats-chart { width: 100%; aspect-ratio: 340 / 150; height: auto; flex: none; display: block; touch-action: manipulation; }
.stats-chart .halo { stroke: #141416; stroke-width: 3px; paint-order: stroke; stroke-linejoin: round; }
.stats-chart .under { fill: var(--green); }
.stats-chart .over { fill: var(--red); }
.stats-chart .empty { fill: #3a3a40; }
.stats-chart .selected { stroke: var(--text); stroke-width: 1.5; }
.stats-chart .target { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.stats-chart text { fill: var(--muted); font-size: 10px; }
.stats-chart .hit { fill: transparent; cursor: pointer; }
.stats-days { list-style: none; margin: 0; padding: 0; }
.stats-days li { display: flex; gap: 10px; align-items: baseline; padding: 9px 2px; border-top: 1px solid var(--card-edge); cursor: pointer; font-variant-numeric: tabular-nums; }
.stats-days .when { flex: 1; }
.stats-days .p { color: var(--muted); font-size: 13px; width: 56px; text-align: right; }
.stats-days .k { width: 86px; text-align: right; }
.stats-days .flag { width: 40px; font-size: 12px; color: var(--red); }
.stats-days li.empty { color: var(--muted); }

/* --- repeat --- */
.repeat-list { max-height: 55vh; overflow-y: auto; margin: 4px 0 12px; }
#entry-again { margin-top: 2px; }
.day-field { flex-direction: row; align-items: center; gap: 10px; }
.day-field input { width: auto; flex: none; padding: 7px 10px; }

/* --- water: plain water + drinks logged as food --- */
.bar.split { display: flex; }
.bar.split .bar-fill { border-radius: 0; }
.bar.split .bar-fill:first-child { border-radius: 4px 0 0 4px; }
.bar-fill.drinks { background: #2a7fa8; }
.water-note { margin-top: 6px; }
.water-note:empty { display: none; }
.item-facts .drink { color: var(--water); }

/* --- weight --- */
.weight-card { cursor: pointer; }
.weight-card .row { margin-bottom: 2px; }
#weight-now { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.weight-card.due #weight-sub { color: var(--fat); }
#weight-dialog[open] { display: flex; flex-direction: column; gap: 12px; }
.weight-chart { aspect-ratio: 340 / 170; }
.weight-chart .grid { stroke: #2a2a2e; stroke-width: 1; }
.weight-chart .line { fill: none; stroke: var(--water); stroke-width: 2; stroke-linejoin: round; }
.weight-chart .dot { fill: var(--water); }
.weight-chart .dot.selected { fill: var(--text); }
.weight-chart .plan { stroke: var(--green); stroke-width: 1.5; stroke-dasharray: 4 4; }
.weight-list li { align-items: center; }
.weight-list .item-remove { padding: 0 4px; }

/* --- daily advice --- */
.advice .row { margin-bottom: 4px; }
.advice-headline { font-weight: 600; }
.advice-headline:empty { display: none; }
.advice-tips { margin: 6px 0 0; padding-left: 18px; font-size: 14px; display: flex; flex-direction: column; gap: 6px; }
.advice-tips:empty { display: none; }
.advice .status-line { margin-top: 4px; }
#advice-review { color: var(--green); font-size: 14px; }
.week-row { align-items: center !important; }
.week-row .when .small { margin-top: 2px; }
.weight-range { margin-top: -6px; }

/* advice card: collapsed shows the headline only */
.advice-toggle { display: flex; align-items: center; gap: 6px; background: none; border: 0; padding: 0; color: inherit; font: inherit; }
.advice-toggle .chev { color: var(--muted); font-size: 13px; transition: transform 0.2s; }
.advice:not(.collapsed) .advice-toggle .chev { transform: rotate(180deg); }
.advice-toggle.no-tips .chev { display: none; }
.advice-headline { cursor: pointer; }
.advice.collapsed .advice-tips { display: none; }
.advice.collapsed .advice-headline {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

/* weight form: weight and day side by side, Save below (an iOS date field won't shrink) */
.weight-form { display: grid; grid-template-columns: 1fr 1.6fr; gap: 8px; }
.weight-form label { min-width: 0; }
.weight-form input { min-width: 0; max-width: 100%; }
.weight-form button { grid-column: 1 / -1; }
.weight-range span { padding-inline: 4px; }
