:root {
  color-scheme: light dark;
  --surface: #fcfcfb;
  --card: #ffffff;
  --line: #e4e3df;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #6f6e69;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #c23534;
  --grid: #e9e8e4;
  --tabbar: 56px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --surface: #1a1a19;
    --card: #232322;
    --line: #383836;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #9d9c92;
    --accent: #3987e5;
    --danger: #e66767;
    --grid: #30302e;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--surface);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
main {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + var(--tabbar) + 24px);
}
h1 { font-size: 22px; margin: 4px 0 16px; }
h2 { font-size: 15px; font-weight: 600; margin: 0; }
p { margin: 0 0 8px; }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.error { color: var(--danger); font-size: 14px; margin: 8px 0; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.stack > * + * { margin-top: 12px; }

button, input, select, textarea { font: inherit; color: inherit; }
button {
  background: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 14px;
  min-height: 40px;
  cursor: pointer;
}
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.link { border: 0; padding: 6px 2px; color: var(--accent); min-height: 0; }
button.danger { color: var(--danger); }
button:disabled { opacity: .5; }
button.wide { width: 100%; }
input, select, textarea {
  width: 100%;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
  min-height: 42px;
  font-size: 16px; /* below 16px iOS zooms the page on focus */
}
label { display: block; font-size: 13px; color: var(--text-2); }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--text); }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.fields .full { grid-column: 1 / -1; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px; margin-bottom: 12px; }

#tabs {
  position: fixed; inset: auto 0 0 0;
  display: flex; justify-content: center;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
#tabs[hidden] { display: none; }
#tabs button { flex: 1; max-width: 186px; height: var(--tabbar); border: 0; border-radius: 0; color: var(--text-2); font-size: 14px; }
#tabs button.active { color: var(--text); font-weight: 600; box-shadow: inset 0 2px 0 var(--accent); }

.daynav { margin-bottom: 12px; }
.daynav strong { font-size: 17px; }
.daynav button { min-width: 44px; padding: 6px; }
.hero { font-size: 40px; font-weight: 650; line-height: 1.1; letter-spacing: -.01em; font-variant-numeric: tabular-nums; }
.meter { height: 8px; background: var(--grid); border-radius: 4px; overflow: hidden; margin: 12px 0 10px; }
.meter > div { height: 100%; background: var(--accent); border-radius: 4px; }
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; font-size: 13px; color: var(--text-2); }
.macros b { display: block; color: var(--text); font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }

.meal { margin: 18px 0 6px; }
.entry {
  display: flex; justify-content: space-between; gap: 12px; align-items: baseline;
  width: 100%; text-align: left;
  padding: 10px 0; border: 0; border-bottom: 1px solid var(--line); border-radius: 0;
}
.entry .name { overflow-wrap: anywhere; }
.entry .kcal { font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty { color: var(--text-3); padding: 8px 0; font-size: 14px; }
.addbar {
  position: fixed; left: 0; right: 0; z-index: 1;
  bottom: calc(env(safe-area-inset-bottom) + var(--tabbar) + 10px);
  display: flex; justify-content: center; padding: 0 16px; pointer-events: none;
}
.addbar button { pointer-events: auto; width: 100%; max-width: 528px; min-height: 48px; }
.today-space { height: 60px; }

dialog {
  width: 100%; max-width: 560px; height: 92dvh; max-height: 92dvh;
  margin: auto auto 0; padding: 0;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 14px 14px 0 0;
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.sheet { display: flex; flex-direction: column; height: 100%; }
.sheet-head { padding: 12px 16px 8px; }
.sheet-body { padding: 8px 16px calc(env(safe-area-inset-bottom) + 20px); overflow-y: auto; flex: 1; }
.segments { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; margin-top: 8px; }
.segments button { padding: 8px 2px; font-size: 14px; min-height: 38px; color: var(--text-2); }
.segments button.active { border-color: var(--accent); color: var(--text); font-weight: 600; }
.result {
  display: block; width: 100%; text-align: left; padding: 10px 0;
  border: 0; border-bottom: 1px solid var(--line); border-radius: 0;
}
.scanner { position: relative; background: #000; border-radius: 10px; overflow: hidden; aspect-ratio: 4 / 3; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner::after {
  content: ""; position: absolute; left: 10%; right: 10%; top: 50%; height: 2px; background: rgba(255, 255, 255, .8);
}
.thumb { display: block; max-width: 100%; max-height: 220px; border-radius: 8px; margin-top: 8px; }
.estimate-item { padding: 10px 0; border-bottom: 1px solid var(--line); }
.estimate-item .pick { display: flex; gap: 10px; align-items: center; color: var(--text); font-size: 15px; }
.estimate-item .pick input { width: 20px; min-height: 20px; flex: none; margin: 0; }
.estimate-item .fields { margin-top: 8px; grid-template-columns: repeat(2, 1fr); }

.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 12px; }
.tile .label { font-size: 13px; color: var(--text-2); }
.tile .value { font-size: 24px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile .note { font-size: 12px; color: var(--text-3); }
.ranges { display: flex; gap: 6px; margin-bottom: 12px; }
.ranges button { min-height: 34px; padding: 4px 12px; font-size: 14px; color: var(--text-2); }
.ranges button.active { border-color: var(--accent); color: var(--text); font-weight: 600; }

.chart { margin-bottom: 12px; }
.chart svg { display: block; width: 100%; touch-action: pan-y; }
.chart text { font-size: 11px; fill: var(--text-3); font-variant-numeric: tabular-nums; }
.chart text.endlabel { fill: var(--text); font-size: 12px; font-weight: 600; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .series { stroke: var(--accent); fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--accent); stroke: var(--card); stroke-width: 2; opacity: .55; }
.chart .bar { fill: var(--accent); }
.chart .target { stroke: var(--text); stroke-width: 1.5; }
.chart .cross { stroke: var(--text-3); stroke-width: 1; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-2); margin: 6px 0 8px; }
.legend i { display: inline-block; vertical-align: middle; margin-right: 5px; background: var(--accent); }
.legend i.key-dot { width: 8px; height: 8px; border-radius: 50%; opacity: .55; }
.legend i.key-line { width: 14px; height: 2px; }
.legend i.key-bar { width: 8px; height: 10px; border-radius: 2px 2px 0 0; }
.legend i.key-target { width: 14px; height: 2px; background: var(--text); }
.readout { min-height: 20px; font-size: 13px; color: var(--text); font-variant-numeric: tabular-nums; }
details summary { color: var(--accent); font-size: 14px; padding: 6px 0; cursor: pointer; }
table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
th, td { text-align: right; padding: 5px 4px; border-bottom: 1px solid var(--line); }
th:first-child, td:first-child { text-align: left; }
th { color: var(--text-2); font-weight: 500; }
.scrolly { max-height: 260px; overflow-y: auto; }
code { font: 13px ui-monospace, Menlo, monospace; overflow-wrap: anywhere; }
ol { padding-left: 20px; margin: 8px 0; font-size: 14px; }
ol li { margin-bottom: 6px; }
