:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efeee8;
  --line: #e2e0d8;
  --ink: #1d1d1b;
  --muted: #65645e;
  --accent: #2f7d5b;
  --accent-ink: #ffffff;
  --accent-soft: #dfeee6;
  --over: #b4541a;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141513;
    --surface: #1e201d;
    --surface-2: #2a2d29;
    --line: #333631;
    --ink: #ecece6;
    --muted: #a3a49c;
    --accent: #5fbf92;
    --accent-ink: #0f1a14;
    --accent-soft: #223a2e;
    --over: #e8935a;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
main {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 88px);
  display: grid;
  gap: 12px;
}
h1, h2, h3, p { margin: 0; }
h2 { font-size: 15px; font-weight: 600; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  display: grid;
  gap: 10px;
  min-width: 0;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.num { font-variant-numeric: tabular-nums; }

.btn {
  border: 1px solid var(--line);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 10px 14px;
  min-height: 44px;
  overflow-wrap: anywhere;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.block { width: 100%; }
.link { background: none; border: 0; padding: 4px; color: var(--muted); text-decoration: underline; font-size: 13px; }
.icon-btn { background: none; border: 0; width: 44px; height: 44px; font-size: 22px; color: var(--muted); flex: none; }

input, select, textarea {
  width: 100%;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
}
textarea { resize: vertical; min-height: 76px; }
:is(input, select, textarea, button):focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: grid; grid-template-columns: minmax(0, 1fr); gap: 4px; font-size: 13px; color: var(--muted); min-width: 0; }
label.field > * { color: var(--ink); font-size: 16px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
/* iOS WebKit can add the padding outside width: 100% for native date/time inputs. */
.temporal-field {
  display: block;
  min-width: 0;
  padding-inline: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}
.temporal-field input {
  height: 44px;
  margin: 0;
  padding: 0;
  border: 0;
  vertical-align: top;
  background: transparent;
  appearance: none;
}
.temporal-field input[type="time"] { text-align: center; }
.temporal-field input::-webkit-date-and-time-value {
  margin: 0;
  line-height: 44px;
  text-align: inherit;
}
@media (max-width: 420px) { .goal-fields { grid-template-columns: minmax(0, 1fr); } }

.date-nav { display: flex; align-items: center; justify-content: space-between; }
.date-nav h1 { font-size: 17px; font-weight: 600; text-align: center; }

.hero { font-size: 38px; font-weight: 700; line-height: 1.1; letter-spacing: -.02em; }

.bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 3px; background: var(--accent); }
.bar.over > i { background: var(--over); }
.metric { display: grid; gap: 4px; min-width: 0; }
.metric .row { font-size: 14px; }
.over-text { color: var(--over); }

.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 2px; gap: 2px; }
.seg button { flex: 1; min-width: 0; min-height: 40px; border: 0; background: none; border-radius: 8px; font-size: 14px; color: var(--muted); padding: 4px; }
.seg button.on { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgb(0 0 0 / .12); }

.draft-item { display: grid; grid-template-columns: minmax(0, 1fr) 84px 44px; gap: 8px; align-items: center; }
.btn.recording { border-color: var(--over); color: var(--over); display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--over); animation: rec-pulse 1.2s ease-in-out infinite; }
@keyframes rec-pulse { 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) { .rec-dot { animation: none; } }
.item-line { display: grid; gap: 2px; min-width: 0; }
.photo-preview { display: grid; grid-template-columns: 72px minmax(0, 1fr); gap: 10px; align-items: center; }
.photo-preview img, .photo-shot { object-fit: cover; border-radius: 10px; background: var(--surface-2); }
.photo-preview img { width: 72px; height: 72px; }
.photo-shot { width: 100%; max-height: 220px; }
label.btn { position: relative; display: flex; align-items: center; justify-content: center; }
.btn.is-disabled { opacity: .55; pointer-events: none; }
.drop-target { outline: 2px dashed var(--accent); outline-offset: -2px; }
.log-item { padding: 8px 0; border-top: 1px solid var(--line); overflow-wrap: anywhere; }
.log-extra { margin: 4px 0 0; white-space: pre-wrap; color: var(--muted); font: 12px/1.4 ui-monospace, monospace; }
.camera-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}
.draft-item input { text-align: right; padding: 8px; }
.badge { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--accent-soft); white-space: nowrap; }
.badge.estimate { background: var(--surface-2); color: var(--muted); }
.badge.custom { background: var(--accent); color: var(--accent-ink); }
.food-row { width: 100%; text-align: left; background: none; border: 0; padding: 0; display: grid; gap: 2px; }

details > summary { list-style: none; cursor: pointer; }
details > summary::-webkit-details-marker { display: none; }
.meal-items { display: grid; gap: 6px; padding-top: 8px; margin-top: 8px; border-top: 1px solid var(--line); }
.clamp { overflow-wrap: anywhere; }

.chart { position: relative; display: flex; align-items: flex-end; gap: 2px; height: 140px; }
.chart button { flex: 1; min-width: 0; height: 100%; padding: 0; border: 0; background: none; display: flex; align-items: flex-end; }
.chart button i { display: block; width: 100%; background: var(--accent); border-radius: 3px 3px 0 0; min-height: 2px; }
.chart button.empty i { background: var(--surface-2); }
.chart button.sel i { background: var(--ink); }
.chart .target { position: absolute; left: 0; right: 0; border-top: 1px dashed var(--muted); pointer-events: none; }

.days { display: grid; grid-template-columns: repeat(10, minmax(0, 1fr)); gap: 4px; }
.days span { aspect-ratio: 1; display: grid; place-items: center; border-radius: 6px; background: var(--surface-2); color: var(--muted); font-size: 12px; }
.days span.on { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.days span.today { outline: 2px solid var(--ink); outline-offset: -2px; }

nav#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);
}
nav#tabs[hidden] { display: none; }
nav#tabs button { flex: 1; max-width: 160px; min-width: 0; min-height: 52px; border: 0; background: none; font-size: clamp(10px, 3.1vw, 12px); color: var(--muted); padding: 4px 2px; overflow-wrap: anywhere; }
nav#tabs button.on { color: var(--accent); font-weight: 600; }

#toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(env(safe-area-inset-bottom) + 68px);
  max-width: 480px;
  margin: 0 auto;
  background: var(--ink);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 10;
}

.group-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; flex-wrap: wrap; padding: 6px 2px 0; }
.inline-select { width: auto; min-height: 32px; padding: 4px 8px; font-size: 14px; }
.wchart { width: 100%; height: auto; display: block; }
.wchart polyline { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.wchart .measured { stroke: var(--accent); }
.wchart .avg { stroke: var(--muted); stroke-dasharray: 4 4; }
.wchart .dot { fill: var(--accent); stroke: var(--surface); stroke-width: 1; }
.wchart .dot.sel { fill: var(--ink); }
.wchart .hit { fill: transparent; cursor: pointer; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.measured-key { width: 16px; height: 2px; border-radius: 1px; background: var(--accent); }
.average-key { width: 16px; border-top: 2px dashed var(--muted); }

#loader { position: fixed; top: 0; left: 0; right: 0; z-index: 20; pointer-events: none; padding-top: env(safe-area-inset-top); }
#loader[hidden] { display: none; }
.loader-bar { height: 3px; background: var(--accent-soft); overflow: hidden; position: relative; }
.loader-bar::after { content: ''; position: absolute; top: 0; bottom: 0; width: 35%; background: var(--accent); animation: loader-slide 1.1s ease-in-out infinite; }
@keyframes loader-slide { from { left: -35%; } to { left: 100%; } }
.loader-text { margin: 8px auto 0; width: max-content; max-width: calc(100% - 32px); background: var(--ink); color: var(--bg); font-size: 13px; padding: 6px 12px; border-radius: 999px; }
.btn.busy, .link.busy { opacity: .65; cursor: progress; }
.btn.primary.busy::after { content: ''; display: inline-block; width: 12px; height: 12px; margin-left: 8px; vertical-align: -1px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .loader-bar::after, .btn.primary.busy::after { animation-duration: 3s; } }
.refine { display: grid; gap: 8px; padding-top: 10px; border-top: 1px solid var(--line); }

.catalog { display: grid; gap: 5px; }
.catalog-list { max-height: min(40vh, 280px); overflow: auto; overscroll-behavior: contain; list-style: none; padding: 0; margin: 0; border: 1px solid var(--line); border-radius: 10px; }
.catalog-list li + li { border-top: 1px solid var(--line); }
.catalog-list button { display: grid; gap: 3px; text-align: left; width: 100%; min-height: 44px; padding: 8px 12px; border: 0; background: transparent; color: inherit; }
.catalog-list button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.catalog-disabled { pointer-events: none; opacity: .55; }
.catalog-source { color: var(--accent); font-size: .78rem; }
.catalog-portion { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 4px; }
.catalog-portion input { min-width: 0; }
.meal-options > select, .meal-tools > .btn { flex: 1; min-width: 0; }
.meal-options > select { font-size: 14px; }
