:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #f0efec;
  --border: #e4e2dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #75746f;
  --accent: #6250d6;
  --accent-soft: #ecebfb;
  --on-accent: #ffffff;
  --kcal: #6250d6;
  --protein: #2a78d6;
  --carbs: #eb6834;
  --fat: #1baf7a;
  --weight: #008300;
  --steps: #2a78d6;
  --critical: #d03b3b;
  --good: #0ca30c;
  --grid: #e9e7e2;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 2px 8px rgba(0, 0, 0, .04);
  --radius: 16px;
  --tabbar-h: 58px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #262624;
    --border: #2f2f2c;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #9a9991;
    --accent: #9085e9;
    --accent-soft: #2a2747;
    --on-accent: #111111;
    --kcal: #9085e9;
    --protein: #3987e5;
    --carbs: #d95926;
    --fat: #199e70;
    --weight: #008300;
    --steps: #3987e5;
    --grid: #2b2b29;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --surface-2: #262624;
  --border: #2f2f2c;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --text-3: #9a9991;
  --accent: #9085e9;
  --accent-soft: #2a2747;
  --on-accent: #111111;
  --kcal: #9085e9;
  --protein: #3987e5;
  --carbs: #d95926;
  --fat: #199e70;
  --weight: #008300;
  --steps: #3987e5;
  --grid: #2b2b29;
  --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, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior-y: none;
}
body.sheet-open { overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
h1, h2, h3, p { margin: 0; }
.muted { color: var(--text-3); }
.num { font-variant-numeric: tabular-nums; }

/* ---------- layout ---------- */
.view {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 8px) 16px calc(var(--tabbar-h) + var(--safe-bottom) + 24px);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card-title { font-size: 17px; font-weight: 650; }
.card-title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.card-sub { color: var(--text-2); font-size: 14px; margin-top: 4px; }
.page-head { padding: 8px 4px 4px; }
.page-head h1 { font-size: 30px; font-weight: 750; letter-spacing: -.02em; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: center;
  padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  backdrop-filter: saturate(180%) blur(16px);
  border-top: 1px solid var(--border);
}
.tabbar button {
  flex: 1; max-width: 180px; height: var(--tabbar-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--text-3); font-size: 11px; font-weight: 600;
}
.tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button.active { color: var(--accent); }

/* ---------- buttons ---------- */
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--text-2); flex: none;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn.filled { background: var(--accent); color: var(--on-accent); }
.icon-btn svg, .btn svg, .chip svg, .add-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 48px; padding: 0 18px; border-radius: 14px;
  background: var(--surface-2); color: var(--text); font-weight: 600;
}
.btn:active { opacity: .8; }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.danger { color: var(--critical); }
.btn.small { height: 36px; padding: 0 14px; border-radius: 11px; font-size: 15px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.link-btn { color: var(--accent); font-weight: 600; padding: 10px 0; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border);
  font-size: 14px; font-weight: 600; color: var(--text-2); white-space: nowrap;
}
.chip svg { width: 16px; height: 16px; }
.chip-row { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; }

/* segmented control */
.seg {
  display: flex; gap: 2px; padding: 3px;
  background: var(--surface-2); border-radius: 12px;
}
.seg button {
  flex: 1; height: 34px; border-radius: 9px;
  font-size: 14px; font-weight: 600; color: var(--text-2); white-space: nowrap;
}
.seg button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

/* ---------- today ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 2px 0; }
.date-btn { display: flex; flex-direction: column; align-items: center; min-width: 0; }
.date-btn span { font-size: 20px; font-weight: 700; text-transform: capitalize; }
.date-btn small { font-size: 13px; color: var(--text-3); }
.date-btn small.back { color: var(--accent); font-weight: 600; }

.summary-top { display: flex; align-items: center; gap: 20px; }
.ring-wrap { position: relative; width: 132px; height: 132px; flex: none; }
.ring { width: 100%; height: 100%; display: block; }
.ring-track { fill: none; stroke: var(--surface-2); stroke-width: 10; }
.ring-fill { fill: none; stroke: var(--kcal); stroke-width: 10; stroke-linecap: round; transition: stroke-dasharray .4s ease; }
.ring-fill.over { stroke: var(--critical); }
.ring-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.ring-center strong { font-size: 30px; font-weight: 750; letter-spacing: -.02em; line-height: 1.05; font-variant-numeric: tabular-nums; }
.ring-center span { font-size: 12px; color: var(--text-2); display: inline-flex; align-items: center; gap: 3px; }
.ring-center span.over { color: var(--critical); font-weight: 600; }
.ring-center svg { width: 13px; height: 13px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }
.summary-stats { display: grid; gap: 10px; margin: 0; flex: 1; }
.summary-stats div { display: flex; flex-direction: column; }
.summary-stats dt { font-size: 13px; color: var(--text-3); }
.summary-stats dd { margin: 0; font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.summary-stats dd small { font-size: 13px; font-weight: 500; color: var(--text-3); }

.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-top: 18px; }
.macro-head { font-size: 13px; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.macro-val { font-size: 15px; font-weight: 650; margin: 2px 0 6px; font-variant-numeric: tabular-nums; }
.macro-val small { font-size: 12px; color: var(--text-3); font-weight: 500; }
.swatch { width: 9px; height: 9px; border-radius: 3px; display: inline-block; flex: none; }
.bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.bar span { display: block; height: 100%; border-radius: 3px; }

.meal { padding: 14px 16px 8px; }
.meal-head { display: flex; align-items: center; gap: 8px; }
.meal-head .titles { flex: 1; min-width: 0; }
.meal-head h3 { font-size: 17px; font-weight: 650; }
.meal-head p { font-size: 13px; color: var(--text-3); font-variant-numeric: tabular-nums; }
.meal-head .kcal { font-weight: 650; font-variant-numeric: tabular-nums; }
.meal-head .icon-btn { margin-right: -10px; }
.add-btn {
  width: 100%; height: 44px; margin-top: 4px;
  display: flex; align-items: center; gap: 8px;
  color: var(--accent); font-weight: 600; font-size: 15px;
}
.add-btn svg { width: 20px; height: 20px; }

.list { list-style: none; margin: 6px 0 0; padding: 0; }
.list li { border-top: 1px solid var(--border); display: flex; align-items: center; }
.list li:first-child { border-top: 0; }
.row { flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px; padding: 10px 0; text-align: left; }
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.row-title { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 13px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-kcal { font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; display: flex; flex-direction: column; align-items: flex-end; line-height: 1.15; }
.row-kcal small { font-size: 11px; color: var(--text-3); font-weight: 500; }
.list .add-quick { margin-left: 4px; margin-right: -8px; color: var(--accent); }
.list .add-quick.done { color: var(--weight); }

.weight-card { display: flex; align-items: center; justify-content: space-between; }
.weight-card .label { font-size: 13px; color: var(--text-3); }
.weight-card .value { font-size: 18px; font-weight: 650; }

/* ---------- sheets ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 50;
  background: var(--bg);
  display: flex; flex-direction: column;
  transform: translateY(100%);
  transition: transform .26s cubic-bezier(.2, .8, .2, 1);
}
.sheet.open { transform: none; }
.sheet-head {
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 6px) 8px 6px;
  background: var(--bg);
}
.sheet-head h2 { flex: 1; text-align: center; font-size: 17px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-head-spacer { width: 40px; flex: none; }
.sheet-body {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 4px 16px calc(var(--safe-bottom) + 28px);
  display: flex; flex-direction: column; gap: 14px;
  max-width: 560px; width: 100%; margin: 0 auto;
}
.search-row { display: flex; gap: 8px; }
.search-box {
  flex: 1; display: flex; align-items: center; gap: 8px;
  height: 44px; padding: 0 12px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--border);
}
.search-box svg { width: 18px; height: 18px; fill: none; stroke: var(--text-3); stroke-width: 2; stroke-linecap: round; flex: none; }
.search-box input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; font-size: 16px; height: 100%; }
.search-box input::-webkit-search-cancel-button { -webkit-appearance: none; }
.icon-btn.filled { width: 44px; height: 44px; }
.section-label { font-size: 13px; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: .04em; margin: 6px 4px -6px; }
.list-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 0 16px; }
.empty { color: var(--text-3); text-align: center; padding: 22px 12px; font-size: 15px; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 2.5px solid var(--surface-2); border-top-color: var(--accent); animation: spin .8s linear infinite; margin: 18px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* food detail */
.food-head h3 { font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.food-head p { font-size: 15px; }
.amount-row { display: flex; gap: 8px; }
.amount-row input, .amount-row select, .field input, .field select {
  height: 48px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); padding: 0 12px; font-size: 17px; width: 100%; min-width: 0;
  -webkit-appearance: none; appearance: none;
}
.amount-row input { flex: 1; font-weight: 650; font-variant-numeric: tabular-nums; }
.amount-row select { flex: 1.3; }
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2375746f' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M7 10l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; background-size: 18px;
  padding-right: 34px !important;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.nutri { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.nutri-kcal { display: flex; align-items: baseline; gap: 6px; }
.nutri-kcal strong { font-size: 34px; font-weight: 750; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.nutri-kcal span { color: var(--text-2); }
.nutri-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 12px; }
.nutri-grid div { display: flex; flex-direction: column; }
.nutri-grid .lbl { font-size: 13px; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.nutri-grid .val { font-size: 18px; font-weight: 650; font-variant-numeric: tabular-nums; }
.nutri-grid .val small { font-size: 13px; color: var(--text-3); font-weight: 500; }
.split { display: flex; gap: 2px; height: 8px; border-radius: 4px; overflow: hidden; margin-top: 14px; background: var(--surface); }
.split span { display: block; height: 100%; }
.split span:first-child { border-radius: 4px 0 0 4px; }
.split span:last-child { border-radius: 0 4px 4px 0; }
.split.empty-split { background: var(--surface-2); }
.per100 { font-size: 14px; }
.actions { display: flex; gap: 10px; }
.actions .btn { flex: 1; }

/* forms */
.form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 14px; color: var(--text-2); font-weight: 550; display: flex; align-items: center; gap: 6px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.hint { font-size: 13px; color: var(--text-3); }
.hint.warn { color: var(--critical); }
.result-box { background: var(--accent-soft); border-radius: 12px; padding: 14px; display: flex; flex-direction: column; gap: 8px; }
.result-box .big { font-size: 26px; font-weight: 750; font-variant-numeric: tabular-nums; }
.kv { display: flex; justify-content: space-between; font-size: 14px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.kv b { color: var(--text); font-weight: 600; }

/* scanner */
.reader { width: 100%; border-radius: var(--radius); overflow: hidden; background: #000; min-height: 220px; }
.reader video { width: 100% !important; display: block; object-fit: cover; }
.scan-status { text-align: center; font-size: 15px; }
.manual-code { display: flex; gap: 8px; }
.manual-code input { flex: 1; min-width: 0; height: 48px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); padding: 0 12px; font-size: 17px; }

/* action sheet */
.action-overlay {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(0, 0, 0, 0); transition: background .2s;
  display: flex; align-items: flex-end; justify-content: center;
}
.action-overlay.open { background: rgba(0, 0, 0, .35); }
.action-sheet {
  width: 100%; max-width: 520px;
  margin: 0 8px calc(var(--safe-bottom) + 8px);
  display: flex; flex-direction: column; gap: 2px;
  transform: translateY(110%); transition: transform .22s cubic-bezier(.2, .8, .2, 1);
}
.action-overlay.open .action-sheet { transform: none; }
.action-title { background: var(--surface); text-align: center; font-size: 13px; color: var(--text-3); padding: 12px; border-radius: 14px 14px 0 0; }
.action { background: var(--surface); height: 56px; font-size: 18px; color: var(--accent); }
.action:last-of-type:not(.cancel) { border-radius: 0 0 14px 14px; }
.action.danger { color: var(--critical); }
.action.cancel { margin-top: 8px; border-radius: 14px; font-weight: 650; }

/* toast */
#toast {
  position: fixed; left: 50%; z-index: 100;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; font-size: 15px; font-weight: 550;
  max-width: calc(100vw - 32px); transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- charts ---------- */
.chart { position: relative; margin-top: 12px; }
.chart svg { display: block; width: 100%; touch-action: pan-y; }
.chart .axis-label { font-size: 11px; fill: var(--text-3); font-variant-numeric: tabular-nums; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .goal-line { stroke: var(--text-2); stroke-width: 1.5; stroke-dasharray: 4 4; }
.goal-key { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); margin-top: 2px; }
.goal-key line { stroke: var(--text-2); stroke-width: 1.5; stroke-dasharray: 4 3; }
.chart .bar-mark { fill: var(--kcal); }
.chart .bar-mark.dim { opacity: .35; }
.chart .line-mark { fill: none; stroke: var(--weight); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot-mark { fill: var(--weight); stroke: var(--surface); stroke-width: 2; }
.chart .crosshair { stroke: var(--text-3); stroke-width: 1; }
.tip {
  position: absolute; top: -6px; transform: translate(-50%, -100%);
  background: var(--text); color: var(--bg);
  border-radius: 8px; padding: 5px 9px; font-size: 13px; line-height: 1.3;
  white-space: nowrap; pointer-events: none; opacity: 0; transition: opacity .12s;
}
.tip.show { opacity: 1; }
.tip b { font-variant-numeric: tabular-nums; }
.chart-empty { color: var(--text-3); font-size: 14px; padding: 28px 0; text-align: center; }
.legend { display: grid; gap: 10px; margin-top: 14px; }
.legend-row { display: grid; grid-template-columns: auto 1fr auto auto; align-items: center; gap: 8px; font-size: 15px; font-variant-numeric: tabular-nums; }
.legend-row .pct { color: var(--text-3); font-size: 13px; min-width: 40px; text-align: right; }
.legend-row .goal { color: var(--text-3); font-size: 13px; min-width: 72px; text-align: right; }
details.table-view { margin-top: 10px; font-size: 14px; }
details.table-view summary { color: var(--accent); font-weight: 600; cursor: pointer; padding: 6px 0; }
details.table-view table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
details.table-view td, details.table-view th { padding: 7px 0; border-top: 1px solid var(--border); text-align: left; }
details.table-view th { color: var(--text-3); font-weight: 600; font-size: 13px; }
details.table-view td:last-child, details.table-view th:last-child { text-align: right; }
.del-btn { color: var(--critical); font-size: 14px; font-weight: 600; padding: 4px 0 4px 12px; }

.about { font-size: 13px; color: var(--text-3); text-align: center; padding: 4px 12px; }
.about a { color: var(--text-2); }

@media (prefers-reduced-motion: reduce) {
  .sheet, .action-sheet, .action-overlay, #toast, .ring-fill { transition: none; }
}

/* online sync */
.key-row { display: flex; gap: 8px; align-items: center; }
.key-row input {
  flex: 1; min-width: 0; height: 44px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); padding: 0 12px;
  font: 15px ui-monospace, "SF Mono", Menlo, monospace;
}
.key-details summary { color: var(--accent); font-weight: 600; font-size: 15px; cursor: pointer; padding: 4px 0 10px; }
.sync-state { display: flex; align-items: flex-start; gap: 8px; font-size: 15px; color: var(--text-2); }
.sync-state svg { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.sync-state.ok svg { stroke: var(--good); }
.sync-state.bad { color: var(--critical); }
.link-btn.danger { color: var(--critical); }

/* weight trend, activity */
.chart .dot-raw { fill: var(--text-3); stroke: var(--surface); stroke-width: 2; }
.chart .bar-mark.steps { fill: var(--steps); }
.key-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); display: inline-block; }
.goal-key .key-trend { margin-left: 10px; }
.goal-key .key-trend line { stroke: var(--weight); stroke-width: 2; stroke-dasharray: none; }
.act-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 10px 0 8px; }
.act-grid div { display: flex; flex-direction: column; }
.act-grid b { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.act-grid span { font-size: 13px; color: var(--text-3); }
