:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #15171c;
  --muted: #646a76;
  --border: #dde0e6;
  --accent: #0f8a6c;
  --accent-text: #ffffff;
  --danger: #c4372f;
  --protein: #3b6fd8;
  --carbs: #d9901a;
  --fat: #b54aa6;
  --over: #c4372f;
  --on: #0f8a6c;
  --off: #d9901a;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%);
  --radius: 14px;
  --tabbar-h: 58px;
  --topbar-h: 48px;
  --fab-size: 60px;
  --fab-bg: #0f8a6c; /* accent; same in dark mode so the white icon keeps its contrast */
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --surface: #1b1e25;
    --surface-2: #252932;
    --text: #eceef2;
    --muted: #9aa1ad;
    --border: #2f343e;
    --accent: #2bb38f;
    --accent-text: #08130f;
    --danger: #ef6b62;
    --protein: #6d97f0;
    --carbs: #f0b04a;
    --fat: #d97ccc;
    --over: #ef6b62;
    --on: #2bb38f;
    --off: #f0b04a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: .95rem; color: var(--muted); font-weight: 600; }
p { margin: 0; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.pad { padding: 0 4px; }

/* layout */
.topbar {
  position: sticky; top: 0; z-index: 5;
  min-height: calc(var(--topbar-h) + env(safe-area-inset-top));
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 8px) calc(env(safe-area-inset-right) + 16px) 8px calc(env(safe-area-inset-left) + 16px);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
.brand { font-weight: 700; }

.view {
  display: flex; flex-direction: column; gap: 12px;
  max-width: 640px; margin: 0 auto;
  /* bottom: tab bar + home indicator (safe area) + room for the raised scan button */
  padding: 4px calc(env(safe-area-inset-right) + 12px) calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 32px) calc(env(safe-area-inset-left) + 12px);
}

/* Bottom bar: Today · Stats · (scan) · Goals · Settings. Its height grows by the home-indicator
   inset (iPhone home-screen app and Safari), so nothing sits under the indicator. */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: stretch;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 44px; min-width: 44px;
  color: var(--muted); text-decoration: none; font-size: .7rem;
}
.tabbar a.active { color: var(--accent); }
.nav-icon { font-size: 1.25rem; line-height: 1; }
/* Raised round scan button in the middle of the bar */
.tabbar a.scan-fab {
  justify-self: center; align-self: start;
  width: var(--fab-size); height: var(--fab-size); margin-top: calc(var(--fab-size) / -2.6);
  border-radius: 50%; background: var(--fab-bg); color: #fff;
  box-shadow: 0 4px 14px rgb(0 0 0 / 28%), 0 0 0 4px var(--surface);
}
.tabbar a.scan-fab.active { color: #fff; }
.tabbar a.scan-fab:active { transform: scale(.95); }
.tabbar a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 12px; }
.tabbar a.scan-fab:focus-visible { border-radius: 50%; }

.card {
  background: var(--surface); border-radius: var(--radius); padding: 14px;
  box-shadow: var(--shadow); border: 1px solid var(--border);
}
.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; align-items: center; }
.row.gap, .gap { gap: 8px; }
.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }
.align-end { align-items: flex-end; }
.row > .field { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* controls */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  font: inherit; font-weight: 600; padding: 11px 16px; border-radius: 12px; text-align: center; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; min-height: 44px; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.primary.danger, .sheet .btn.danger.block { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.small { padding: 6px 12px; min-height: 36px; font-size: .88rem; }
.btn.block { width: 100%; }
.btn.link { background: none; border: none; color: var(--accent); font-weight: 500; }
.btn:disabled { opacity: .5; }
.icon-btn {
  appearance: none; border: none; background: none; color: var(--text); font-size: 1.4rem;
  min-width: 44px; min-height: 44px; border-radius: 10px; cursor: pointer;
}
.icon-btn:disabled { opacity: .3; }

input, select {
  font: inherit; font-size: 16px; /* >= 16px prevents iOS zoom on focus */
  width: 100%; padding: 11px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
input[type="checkbox"] { width: 20px; height: 20px; padding: 0; accent-color: var(--accent); }
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.big-input { font-size: 1.4rem; font-weight: 700; }
.code-input { font-size: 1.6rem; letter-spacing: .3em; text-align: center; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field-label { font-size: .82rem; color: var(--muted); }
.hint { font-size: .78rem; color: var(--muted); }
.check { display: flex; align-items: center; gap: 8px; }

.segmented { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.segmented button {
  flex: 1; appearance: none; border: none; background: none; color: var(--muted); font: inherit; font-size: .88rem;
  font-weight: 600; padding: 8px 4px; border-radius: 8px; min-height: 36px; cursor: pointer;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  appearance: none; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  font: inherit; font-size: .85rem; padding: 6px 12px; border-radius: 999px; min-height: 34px; cursor: pointer;
}

.notice { background: color-mix(in srgb, var(--accent) 12%, var(--surface)); border-radius: 10px; padding: 10px 12px; }
a.link { color: var(--text); text-decoration: none; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: calc(env(safe-area-inset-top) + 16px) 16px calc(env(safe-area-inset-bottom) + 16px); }
.login-card { width: 100%; max-width: 380px; text-align: center; }
.logo { width: 64px; height: 64px; margin: 0 auto; border-radius: 16px; background: var(--accent); position: relative; }
.logo::after { content: ""; position: absolute; inset: 14px; border-radius: 50%; border: 6px solid var(--accent-text); border-right-color: transparent; }

/* today */
.date-nav { display: flex; align-items: center; justify-content: space-between; }
.date-label { appearance: none; border: none; background: none; color: var(--text); font: inherit; font-weight: 700; font-size: 1.1rem; padding: 8px; cursor: pointer; }
.summary { display: grid; grid-template-columns: 132px 1fr; gap: 14px; align-items: center; }
.ring-wrap { position: relative; width: 132px; height: 132px; }
.ring { width: 100%; height: 100%; }
.ring-bg { fill: none; stroke: var(--surface-2); stroke-width: 11; }
.ring-fg { fill: none; stroke: var(--accent); stroke-width: 11; stroke-linecap: round; transition: stroke-dasharray .3s; }
.ring-fg.over { stroke: var(--over); }
.ring-text { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; font-size: .75rem; color: var(--muted); }
.ring-text strong { font-size: 1.5rem; color: var(--text); }
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-label { display: flex; justify-content: space-between; font-size: .85rem; margin-bottom: 4px; }
.bar-track { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar-fill { height: 100%; border-radius: 4px; transition: width .3s; }
.bar-fill.protein { background: var(--protein); }
.bar-fill.carbs { background: var(--carbs); }
.bar-fill.fat { background: var(--fat); }
.bar-fill.over { background: var(--over); }

.meal header { margin-bottom: 4px; }
.meal.other { border-style: dashed; }
.meal.flash { animation: flash 1.6s ease-out; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: var(--shadow); } }
/* + on each meal block: round, 44px touch target */
.add-btn {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: 44px; height: 44px; border-radius: 50%;
  background: color-mix(in srgb, var(--accent) 14%, var(--surface)); color: var(--accent);
  font-size: 1.6rem; font-weight: 600; line-height: 1; text-decoration: none;
}
.add-btn:active { transform: scale(.94); }

/* Today header: date + small preset chip (quiet styling, doesn't compete with logging) */
.date-center { display: flex; flex-direction: column; align-items: center; min-width: 0; }
.preset-chip {
  appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  font: inherit; font-size: .8rem; border-radius: 999px; padding: 0 12px; min-height: 32px; max-width: 60vw;
  display: inline-flex; align-items: center; cursor: pointer; position: relative;
}
.preset-chip::before { content: ""; position: absolute; inset: -6px -2px; } /* 44px tap area */
.preset-chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Preset menu */
.preset-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.preset-row { border: 1px solid var(--border); border-radius: 12px; padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.preset-row.active { border-color: var(--accent); }
.preset-pick {
  appearance: none; border: none; background: none; color: inherit; font: inherit; text-align: left;
  display: flex; flex-direction: column; gap: 2px; padding: 4px; min-height: 44px; cursor: pointer;
}
.preset-title { display: flex; align-items: center; gap: 6px; }
.preset-title .check { color: var(--accent); width: 1em; font-weight: 700; }
.badge { font-size: .7rem; padding: 1px 6px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }

/* Meal chips on the portion sheet: large, one row, scrolls sideways */
.slot-chips {
  display: flex; gap: 8px; overflow-x: auto; scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch;
  padding: 2px 2px 6px; scrollbar-width: none;
}
.slot-chips::-webkit-scrollbar { display: none; }
.slot-chip {
  flex: none; scroll-snap-align: center; appearance: none; cursor: pointer;
  min-height: 48px; padding: 0 18px; border-radius: 14px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: inherit; font-weight: 600; white-space: nowrap;
}
.slot-chip.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.sheet-footer {
  display: flex; flex-direction: column; gap: 8px; padding: 10px 16px 12px;
  border-top: 1px solid var(--border); background: var(--surface);
}
.btn.grow { flex: 1; }

/* Preset editor */
.palette {
  position: sticky; top: calc(var(--topbar-h) + env(safe-area-inset-top)); z-index: 4;
  background: var(--bg); padding: 8px 0; display: flex; flex-direction: column; gap: 6px;
}
/* One compact row that scrolls sideways; a sideways swipe scrolls it (pan-x), dragging down picks a block up. */
.palette-chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.palette-chips::-webkit-scrollbar { display: none; }
.palette-chip {
  flex: none; appearance: none; cursor: grab; min-height: 44px; padding: 0 14px; border-radius: 12px; white-space: nowrap;
  border: 1px dashed var(--accent); background: var(--surface); color: var(--text); font: inherit; font-size: .9rem;
  touch-action: pan-x; user-select: none; -webkit-user-select: none;
}
.block-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.block-row { display: flex; gap: 6px; align-items: stretch; border: 1px solid var(--border); border-radius: 12px; padding: 6px; background: var(--surface); }
.block-row.dragging { opacity: .35; }
.block-fields { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.block-fields input[type="text"] { flex: 1; min-width: 0; }
.block-fields input[type="time"] { width: auto; flex: 1; min-width: 7.5em; }
.block-type { font-size: .78rem; color: var(--muted); min-width: 5.5em; }
.drag-handle {
  appearance: none; border: none; background: var(--surface-2); color: var(--muted); border-radius: 8px;
  width: 44px; min-height: 44px; font-size: 1.3rem; cursor: grab;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.drop-indicator { height: 4px; border-radius: 2px; background: var(--accent); list-style: none; }
.drag-ghost {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none;
  padding: 10px 14px; border-radius: 12px; background: var(--accent); color: var(--accent-text);
  font-weight: 600; box-shadow: 0 6px 20px rgb(0 0 0 / 30%);
}
body.is-dragging { user-select: none; -webkit-user-select: none; cursor: grabbing; }
.empty-hint { text-align: center; padding: 12px; }
.failed-list { margin: 0; padding-left: 18px; color: var(--danger); }
.notice.warn { background: color-mix(in srgb, var(--carbs) 18%, var(--surface)); }
.notice.error { background: color-mix(in srgb, var(--danger) 16%, var(--surface)); }
.entries { list-style: none; margin: 0; padding: 0; }
.entries li + li { border-top: 1px solid var(--border); }
.entry {
  appearance: none; border: none; background: none; color: inherit; font: inherit; text-align: left;
  width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 2px; cursor: pointer; min-height: 48px;
}
.entry-main { display: flex; flex-direction: column; min-width: 0; }
.entry-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry-kcal { font-weight: 700; white-space: nowrap; }
.entries li.row { gap: 8px; padding: 6px 0; }

/* sheets */
.overlay {
  position: fixed; inset: 0; z-index: 20; background: rgb(0 0 0 / 45%);
  display: flex; align-items: flex-end; justify-content: center; animation: fade .15s;
}
.sheet {
  width: 100%; max-width: 640px; max-height: 92vh; display: flex; flex-direction: column;
  background: var(--surface); border-radius: 18px 18px 0 0; animation: up .2s;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 0 16px; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 8px 16px 16px; display: flex; flex-direction: column; gap: 12px; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

.product-head { display: flex; gap: 12px; align-items: center; }
.thumb { width: 64px; height: 64px; border-radius: 10px; object-fit: contain; background: #fff; flex: none; }
.thumb.placeholder { display: grid; place-items: center; font-size: 1.8rem; background: var(--surface-2); }
.product-name { font-weight: 700; font-size: 1.05rem; }
.macro-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.macro-grid > div { background: var(--surface-2); border-radius: 10px; padding: 8px 2px; display: flex; flex-direction: column; }
.macro-grid strong { font-size: 1.15rem; }
.macro-grid span { font-size: .75rem; color: var(--muted); }
.macro-grid .minor strong { font-size: .95rem; }
.ingredients summary { cursor: pointer; color: var(--muted); }
.ingredients p { font-size: .85rem; margin-top: 6px; }

/* scanner */
.scanner { position: relative; border-radius: 12px; overflow: hidden; background: #000; aspect-ratio: 4 / 3; }
.scanner-video { width: 100%; height: 100%; object-fit: cover; display: block; }
/* Position comes from GUIDE in scanner.js (set inline via CSSOM); this inset is only a fallback. */
.scan-frame { position: absolute; inset: 30% 8%; border: 3px solid rgb(255 255 255 / 90%); border-radius: 10px; box-shadow: 0 0 0 999px rgb(0 0 0 / 35%); pointer-events: none; }
.scan-frame::after { content: ""; position: absolute; left: 6%; right: 6%; top: 50%; height: 2px; background: rgb(255 80 80 / 80%); }
.scan-hint { position: absolute; left: 0; right: 0; bottom: 8px; margin: 0; text-align: center; color: #fff; font-size: .82rem; text-shadow: 0 1px 2px #000; pointer-events: none; }
.scan-debug { margin: 0; padding: 8px 10px; border-radius: 8px; background: var(--surface-2); font: 12px/1.45 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; word-break: break-all; }

/* stats */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 2px; box-shadow: var(--shadow); }
.tile strong { font-size: 1.2rem; }
.chart { margin: 8px 0 0; }
.chart svg { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 9px; }
.chart .bar-mark.on { fill: var(--on); }
.chart .bar-mark.off { fill: var(--off); }
.chart .target-mark { stroke: var(--text); stroke-width: 2; }
.legend { font-size: .75rem; color: var(--muted); margin-top: 6px; }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }
.key.on { background: var(--on); }
.key.off { background: var(--off); }
.key.target { height: 2px; background: var(--text); vertical-align: 3px; }
.macro-table { width: 100%; border-collapse: collapse; margin-top: 6px; }
.macro-table th, .macro-table td { text-align: right; padding: 6px 4px; border-bottom: 1px solid var(--border); }
.macro-table th[scope="row"], .macro-table thead th:first-child { text-align: left; }

details.card > summary { font-weight: 700; cursor: pointer; }
details.card[open] > summary { margin-bottom: 10px; }
.privacy p { font-size: .9rem; }

/* sync + toast */
.sync-dot { width: 32px; height: 32px; border: none; border-radius: 50%; background: none; position: relative; cursor: pointer; }
.sync-dot::after { content: ""; position: absolute; inset: 10px; border-radius: 50%; background: var(--muted); }
.sync-dot.ok::after { background: var(--on); }
.sync-dot.syncing::after { background: var(--carbs); animation: pulse 1s infinite alternate; }
.sync-dot.offline::after { background: var(--muted); }
.sync-dot.error::after { background: var(--danger); }
@keyframes pulse { to { opacity: .3; } }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 34px); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: .2s;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; max-width: calc(100vw - 32px); font-size: .92rem;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
