:root {
  --bg: #f6f5f1;
  --card: #ffffff;
  --ink: #1c1d21;
  --muted: #6b6e78;
  --line: #e4e2dc;
  --accent: #3b6fd8;
  --accent-ink: #ffffff;
  --accent-soft: #e3ebfb;
  --good: #2f8a57;
  --bad: #c2483d;
  --warn: #b7791f;
  --grid: #ecebe6;
  --h0: #ebeae4; --h1: #c9d8f5; --h2: #92b2ee; --h3: #5b8ae2; --h4: #2f62c9;
  --radius: 14px;
  --nav-h: 62px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161c; --card: #1d2028; --ink: #e8e9ee; --muted: #9195a3; --line: #2c303b;
    --accent: #7aa2f7; --accent-ink: #10131a; --accent-soft: #263453;
    --good: #5fc48a; --bad: #f07b6f; --warn: #e0b050; --grid: #262a33;
    --h0: #252932; --h1: #2b3e66; --h2: #3c5c9e; --h3: #5a84d6; --h4: #8fb1f7;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14161c; --card: #1d2028; --ink: #e8e9ee; --muted: #9195a3; --line: #2c303b;
  --accent: #7aa2f7; --accent-ink: #10131a; --accent-soft: #263453;
  --good: #5fc48a; --bad: #f07b6f; --warn: #e0b050; --grid: #262a33;
  --h0: #252932; --h1: #2b3e66; --h2: #3c5c9e; --h3: #5a84d6; --h4: #8fb1f7;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
html, body { overflow-x: hidden; }
main { max-width: 640px; margin: 0 auto; padding: 12px 16px calc(var(--nav-h) + 24px + env(safe-area-inset-bottom)); }
h2, h3, h4 { margin: 0; }
h3 { font-size: 15px; font-weight: 650; }
h4 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 14px 0 4px; }
a { color: var(--accent); text-decoration: none; }
small, .small { font-size: 12.5px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.hidden { display: none !important; }
.grow { flex: 1; min-width: 0; }
.good { color: var(--good); } .bad { color: var(--bad); }
.page-title { font-size: 22px; margin: 6px 0 12px; }
.empty { color: var(--muted); text-align: center; padding: 18px 0; }

/* nav */
nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
nav a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--muted); font-size: 11.5px; }
nav a .nav-i { font-size: 19px; line-height: 1; filter: grayscale(1); opacity: .75; }
nav a.on { color: var(--accent); font-weight: 600; }
nav a.on .nav-i { filter: none; opacity: 1; }

/* cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 10px 0; }
details.card > summary { list-style: none; display: flex; justify-content: space-between; align-items: center; cursor: pointer; font-weight: 650; padding: 2px 0; }
details.card > summary::-webkit-details-marker { display: none; }
details.card > summary::after { content: '▾'; color: var(--muted); margin-left: 8px; transition: transform .15s; }
details.card:not([open]) > summary::after { transform: rotate(-90deg); }
details.card > summary span { flex: 1; }
.group-count { color: var(--muted); font-weight: 400; }

/* day header */
.day-head { display: flex; align-items: center; gap: 8px; margin: 4px 0 6px; }
.day-title { flex: 1; text-align: center; }
.day-title h2 { font-size: 20px; }
.day-title small { color: var(--muted); }
.icon-btn {
  min-width: 40px; height: 40px; border: 0; background: transparent; color: var(--ink);
  font-size: 22px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}
.icon-btn:disabled { opacity: .25; }
.icon-btn:active { background: var(--accent-soft); }
.progress { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.progress .bar { flex: 1; height: 5px; border-radius: 3px; background: var(--grid); overflow: hidden; }
.progress .bar span { display: block; height: 100%; background: var(--accent); transition: width .2s; }

/* metric rows */
.mrow { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 0; border-top: 1px solid var(--line); }
details .mrow:first-of-type { margin-top: 8px; }
.mrow.scale-row { flex-direction: column; align-items: stretch; gap: 6px; }
.mlabel { font-size: 14.5px; }
.mlabel small { color: var(--muted); }
.badge { display: inline-block; font-size: 10.5px; padding: 1px 6px; margin-left: 6px; border-radius: 8px; background: var(--accent-soft); color: var(--accent); vertical-align: 1px; }
.derived-val { color: var(--muted); }
.derived-val small { font-size: 11px; }

.scale { display: grid; gap: 4px; grid-template-columns: repeat(10, 1fr); }
.scale.n5 { grid-template-columns: repeat(5, 1fr); }
.scale.n7 { grid-template-columns: repeat(7, 1fr); }
.scale.n3 { grid-template-columns: repeat(3, 1fr); }
.scale-btn {
  height: 38px; border-radius: 9px; border: 1px solid var(--line); background: var(--bg); color: var(--ink);
  font-size: 15px; font-weight: 600; cursor: pointer; padding: 0;
}
.scale-btn.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.stepper { display: flex; align-items: center; gap: 4px; }
.step-btn { width: 40px; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); font-size: 20px; cursor: pointer; }
.step-btn:active { background: var(--accent-soft); }
input.num { width: 64px; text-align: center; }

.chip {
  min-width: 64px; height: 36px; padding: 0 14px; border-radius: 18px; border: 1px solid var(--line);
  background: var(--bg); color: var(--muted); font-weight: 600; cursor: pointer;
}
.chip.on { background: var(--good); border-color: var(--good); color: #fff; }
.chip.off { color: var(--ink); }
.chip.small { min-width: 44px; height: 28px; padding: 0 10px; font-size: 12px; }
.chip.small.on { background: var(--accent); border-color: var(--accent); }

/* forms */
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 10px; min-height: 38px; max-width: 100%;
}
textarea { width: 100%; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: flex; flex-direction: column; gap: 4px; margin: 8px 0; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { color: var(--ink); }
.notes label input { width: 100%; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px; border-radius: 11px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn.wide { width: 100%; margin: 6px 0; }
.btn.big { min-height: 48px; font-size: 16px; }
.btn:disabled { opacity: .5; }
.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.row.end { justify-content: flex-end; }
.row > input[type=text] { flex: 1; min-width: 0; }
.seg { display: flex; background: var(--card); border: 1px solid var(--line); border-radius: 11px; padding: 3px; margin: 8px 0; }
.seg button { flex: 1; border: 0; background: transparent; color: var(--muted); font: inherit; font-size: 13px; padding: 7px 4px; border-radius: 8px; cursor: pointer; }
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
code.copy { display: block; font-size: 12px; padding: 8px; background: var(--bg); border-radius: 8px; word-break: break-all; user-select: all; }

/* food */
.food-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.food-totals b { font-size: 17px; }
.food-totals small { color: var(--muted); }
.food-actions { display: flex; gap: 8px; margin: 10px 0 4px; }
.food-actions .btn { flex: 1; }
.totals { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; }
.total b { display: block; font-size: 20px; }
.total small { display: block; color: var(--muted); }
.total .goal { font-size: 11px; }
.food-item { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); color: var(--ink); }
.food-item:first-child { border-top: 0; }
.food-item img, .thumb-ph { width: 46px; height: 46px; border-radius: 10px; object-fit: cover; flex: none; }
.thumb-ph { display: flex; align-items: center; justify-content: center; background: var(--bg); color: var(--muted); }
.fi-main { flex: 1; min-width: 0; }
.fi-main div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fi-main small, .fi-num small { color: var(--muted); }
.fi-num { text-align: right; white-space: nowrap; }
.photo-row { display: flex; gap: 8px; margin-bottom: 8px; }
.photo-row .big { flex: 1; }
.photo-preview img { width: 100%; max-height: 260px; object-fit: cover; border-radius: 12px; margin-bottom: 8px; }
.ai-status { font-size: 13px; color: var(--muted); min-height: 1.2em; }
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }
.items { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.item { border: 1px solid var(--line); border-radius: 12px; padding: 8px; }
.item-top { display: flex; gap: 6px; }
.item-top input { flex: 1; min-width: 0; font-weight: 600; }
.item-nums { display: grid; grid-template-columns: 1.6fr 1fr 1fr 1fr 1fr; gap: 4px; }
.item-nums label { margin: 4px 0 0; font-size: 11px; gap: 2px; }
.item-nums input { width: 100%; padding: 6px 4px; min-height: 34px; text-align: center; }
.item-nums label:first-child input { text-align: left; }
.items-total { margin: 8px 0; }
.lib-results { display: flex; flex-direction: column; margin-top: 6px; }
.lib-hit { display: flex; justify-content: space-between; gap: 8px; text-align: left; border: 0; border-top: 1px solid var(--line); background: transparent; color: var(--ink); font: inherit; padding: 9px 2px; cursor: pointer; }
.lib-hit small { color: var(--muted); white-space: nowrap; }
input[type=search] { width: 100%; }

/* trends */
.group-title { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 12px 0 2px; }
.group-title:first-child { margin-top: 0; }
.trend-row { display: grid; grid-template-columns: minmax(0, 1fr) 80px 112px; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); color: var(--ink); }
.tr-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tr-val { text-align: right; white-space: nowrap; }
.spark { width: 80px; height: 24px; }
.spark-line { fill: none; stroke: var(--accent); stroke-width: 1.6; }
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 8px; }
.stat-grid div { background: var(--bg); border-radius: 10px; padding: 8px 10px; }
.stat-grid small { display: block; color: var(--muted); }
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 9.5px; }
.chart .axis-label { fill: var(--muted); font-size: 10px; }
.chart .dots circle { fill: var(--accent); opacity: .35; }
.chart .trend { fill: none; stroke: var(--accent); stroke-width: 2.2; stroke-linejoin: round; }
.chart .goal-line { stroke: var(--good); stroke-dasharray: 4 3; }
.chart .bar { fill: var(--accent); opacity: .8; }
.chart .bar.hi { fill: var(--good); }
.chart .sdot { fill: var(--accent); opacity: .4; }
.chart .fit { stroke: var(--bad); stroke-width: 2; }
.chart-empty { fill: var(--muted); font-size: 12px; }
.heat .hc.none { fill: transparent; stroke: var(--grid); }
.heat .l0 { fill: var(--h0); } .heat .l1 { fill: var(--h1); } .heat .l2 { fill: var(--h2); } .heat .l3 { fill: var(--h3); } .heat .l4 { fill: var(--h4); }

/* insights */
.ins-row { display: grid; grid-template-columns: minmax(0, 1fr) 90px 46px; align-items: center; gap: 8px; width: 100%; border: 0; border-top: 1px solid var(--line); background: transparent; color: var(--ink); font: inherit; text-align: left; padding: 9px 0; cursor: pointer; }
.ins-row:not(.sig) .ins-name { color: var(--muted); }
.ins-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ins-bar { position: relative; height: 8px; background: var(--grid); border-radius: 4px; }
.ins-bar::after { content: ''; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--muted); }
.ins-bar span { position: absolute; top: 0; bottom: 0; border-radius: 4px; }
.ins-bar .pos { background: var(--accent); }
.ins-bar .neg { background: var(--warn); }
.ins-r { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.ins-row:not(.sig) .ins-r { color: var(--muted); }

/* settings */
.mitem { display: flex; align-items: center; gap: 4px; padding: 4px 0; border-top: 1px solid var(--line); }
.mitem small { color: var(--muted); }
.mitem.archived .grow { color: var(--muted); text-decoration: line-through; }
.form label { margin: 6px 0; }

/* modal + toast */
.modal-wrap { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.modal { background: var(--card); width: 100%; max-width: 560px; max-height: 88vh; overflow: auto; border-radius: 18px 18px 0 0; padding: 14px 16px calc(16px + env(safe-area-inset-bottom)); }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
#toast { position: fixed; left: 50%; bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); opacity: 0; z-index: 60;
  background: var(--ink); color: var(--bg); padding: 9px 16px; border-radius: 20px; font-size: 14px; transition: all .2s; pointer-events: none; max-width: 90vw; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
pre { white-space: pre-wrap; word-break: break-word; }
.scale.n11 { grid-template-columns: repeat(11, 1fr); }
.scale.n11 .scale-btn { font-size: 13.5px; }
