:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --accent-ink: #ffffff;

  /* data series (categorical slots 1-3) */
  --s-protein: #2a78d6;
  --s-carbs: #eb6834;
  --s-fat: #1baf7a;
  --s-weight: #2a78d6;
  --s-trend: #eb6834;

  /* status */
  --good: #006300;
  --good-mark: #0ca30c;
  --warn: #fab219;
  --critical: #d03b3b;
  --warn-bg: #fff6e0;
  --crit-bg: #fdeceb;

  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --s-protein: #3987e5;
    --s-carbs: #d95926;
    --s-fat: #199e70;
    --s-weight: #3987e5;
    --s-trend: #d95926;
    --good: #0ca30c;
    --warn-bg: #3a2f12;
    --crit-bg: #3a1a1a;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #262624;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --accent: #3987e5;
  --s-protein: #3987e5;
  --s-carbs: #d95926;
  --s-fat: #199e70;
  --s-weight: #3987e5;
  --s-trend: #d95926;
  --good: #0ca30c;
  --warn-bg: #3a2f12;
  --crit-bg: #3a1a1a;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.05rem; margin-bottom: 12px; }
h3 { font-size: 0.9rem; margin: 18px 0 8px; color: var(--ink-2); }

.muted { color: var(--ink-2); }
.small { font-size: 0.82rem; }

/* ---------- header & tabs ---------- */
.app-header {
  display: flex; flex-wrap: wrap; gap: 12px;
  align-items: center; justify-content: space-between;
  padding: 14px 16px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.date-nav { display: flex; gap: 6px; align-items: center; }

.tabs {
  display: flex; gap: 4px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.tab {
  background: none; border: 0; border-bottom: 2px solid transparent;
  padding: 10px 14px; font: inherit; color: var(--ink-2); cursor: pointer;
}
.tab.active { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }

main { max-width: 1100px; margin: 0 auto; padding: 16px; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 16px;
}
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-2 > .card { margin-bottom: 16px; }
@media (max-width: 800px) { .grid-2 { grid-template-columns: 1fr; gap: 0; } }

.notice {
  background: var(--warn-bg); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 16px; margin-bottom: 16px;
}

/* ---------- forms & buttons ---------- */
input, select, button { font: inherit; color: inherit; }
input, select {
  width: 100%; padding: 8px 10px; border: 1px solid var(--axis);
  border-radius: 8px; background: var(--surface);
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; min-width: 0; }
.field > span:first-child { font-size: 0.8rem; color: var(--ink-2); }
.field-row { display: flex; gap: 10px; flex-wrap: wrap; }
.field-row > .field { flex: 1 1 120px; }
.field-row > .field.grow { flex: 3 1 180px; }
.field-row.align-end { align-items: flex-end; }
.field-row.align-end > button { margin-bottom: 10px; }
.inline-inputs { display: flex; gap: 6px; align-items: center; }

.choice-group { border: 0; padding: 0; margin: 0 0 10px; min-width: 0; flex-direction: row; flex-wrap: wrap; gap: 8px; }
.choice-group legend { font-size: 0.8rem; color: var(--ink-2); padding: 0; margin-bottom: 4px; }
.choice {
  flex: 1 1 140px; display: flex; align-items: center; gap: 8px; cursor: pointer;
  padding: 8px 10px; border: 1px solid var(--axis); border-radius: 8px;
}
.choice input { width: auto; margin: 0; accent-color: var(--accent); }
.choice:has(input:checked) { border-color: var(--accent); background: var(--surface-2); font-weight: 600; }
.inline-inputs input { width: 5rem; }

button { cursor: pointer; }
.primary-btn, .ghost-btn, .danger-btn {
  border-radius: 8px; padding: 8px 14px; border: 1px solid transparent; white-space: nowrap;
}
.primary-btn { background: var(--accent); color: var(--accent-ink); }
.ghost-btn { background: transparent; border-color: var(--axis); }
.danger-btn { background: transparent; border-color: var(--critical); color: var(--critical); }
.icon-btn {
  width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--axis);
  background: transparent; font-size: 1.2rem; line-height: 1;
}
.link-btn { background: none; border: 0; color: var(--accent); padding: 0; font-weight: 600; }
.del-btn {
  background: none; border: 0; color: var(--muted); font-size: 1.1rem;
  padding: 2px 8px; border-radius: 6px;
}
.del-btn:hover { color: var(--critical); background: var(--crit-bg); }
.btn-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.file-btn { display: inline-block; cursor: pointer; }
#dateInput { width: auto; }

/* ---------- stat tiles ---------- */
.tiles { display: flex; flex-wrap: wrap; gap: 8px; align-items: stretch; margin-bottom: 8px; }
.tile {
  flex: 1 1 90px; background: var(--surface-2); border-radius: 8px;
  padding: 10px 12px; display: flex; flex-direction: column;
}
.tile.op {
  flex: 0 0 auto; background: none; padding: 0; justify-content: center;
  color: var(--muted); font-size: 1.2rem;
}
.tile-label { font-size: 0.78rem; color: var(--ink-2); }
.tile-value { font-size: 1.4rem; font-weight: 600; }
.tile-sub { font-size: 0.78rem; color: var(--ink-2); }
.tile-hero .tile-value { font-size: 1.6rem; }
.tile-value.over { color: var(--critical); }
.tiles-4 .tile { flex-basis: 140px; }
@media (max-width: 560px) { .tile.op { display: none; } .tile { flex-basis: 40%; } }

/* ---------- progress bars ---------- */
.bar-row { margin-top: 10px; }
.bar-label { display: flex; justify-content: space-between; font-size: 0.82rem; margin-bottom: 4px; }
.bar-label span:last-child { color: var(--ink-2); font-variant-numeric: tabular-nums; }
.bar { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; position: relative; }
.bar-fill { height: 100%; border-radius: 4px; transition: width 0.3s; }
.bar-fill.kcal { background: var(--accent); }
.bar-fill.over { background: var(--critical); }
.bar-fill.protein { background: var(--s-protein); }
.bar-fill.carbs { background: var(--s-carbs); }
.bar-fill.fat { background: var(--s-fat); }
.macro-bars { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
@media (max-width: 560px) { .macro-bars { grid-template-columns: 1fr; gap: 0; } }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* ---------- food search ---------- */
.results { list-style: none; margin: 0 0 8px; padding: 0; max-height: 300px; overflow-y: auto; }
.results li {
  display: flex; justify-content: space-between; gap: 10px; padding: 8px 10px;
  border-radius: 8px; cursor: pointer;
}
.results li:hover, .results li.active { background: var(--surface-2); }
.results .r-name { font-weight: 500; }
.results .r-meta { font-size: 0.78rem; color: var(--ink-2); white-space: nowrap; text-align: right; }
.results .r-cat { font-size: 0.75rem; color: var(--muted); }

.portion { border: 1px solid var(--accent); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; }
.portion-head { display: flex; flex-direction: column; margin-bottom: 8px; }
.portion-preview { font-variant-numeric: tabular-nums; margin: 0 0 10px; }

.custom-food summary { cursor: pointer; color: var(--accent); font-size: 0.9rem; margin-top: 6px; }
.custom-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; margin-top: 10px; }
.span-2 { grid-column: span 2; }

/* ---------- entry lists ---------- */
.entry-list { list-style: none; margin: 0; padding: 0; }
.entry-list li, .meal-row {
  display: flex; align-items: center; gap: 10px; padding: 8px 0;
  border-bottom: 1px solid var(--grid);
}
.entry-list li:last-child { border-bottom: 0; }
.entry-main { flex: 1; min-width: 0; }
.entry-main .sub { font-size: 0.78rem; color: var(--ink-2); }
.entry-kcal { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.empty { color: var(--muted); font-size: 0.88rem; padding: 6px 0; }

.meal-group { margin-bottom: 14px; }
.meal-group-head {
  display: flex; justify-content: space-between; font-weight: 600;
  padding-bottom: 4px; border-bottom: 1px solid var(--axis);
}
.meal-group-head span:last-child { font-variant-numeric: tabular-nums; color: var(--ink-2); font-weight: 400; font-size: 0.85rem; }

/* ---------- tables ---------- */
.table-scroll { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.data-table th, .data-table td { padding: 7px 8px; text-align: right; border-bottom: 1px solid var(--grid); white-space: nowrap; }
.data-table th:first-child, .data-table td:first-child { text-align: left; }
.data-table th { color: var(--ink-2); font-weight: 500; font-size: 0.8rem; }
.data-table td { font-variant-numeric: tabular-nums; }
.miss { color: var(--critical); font-weight: 600; }
.hit { color: var(--good); }

/* ---------- weekly ---------- */
.flag {
  display: flex; gap: 12px; align-items: flex-start; border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 12px; border: 1px solid var(--border);
}
.flag.warn { background: var(--warn-bg); }
.flag.ok { background: var(--surface-2); }
.flag-icon { font-size: 1.3rem; line-height: 1; }
.macro-week { display: grid; grid-template-columns: 90px 1fr auto; gap: 6px 12px; align-items: center; margin-bottom: 14px; }
.macro-week .bar { height: 10px; }
.macro-week .target-tick { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--ink); }
.macro-week .bar { overflow: visible; }
.dots { display: flex; gap: 4px; grid-column: 2 / 4; }
.dot {
  width: 22px; height: 22px; border-radius: 50%; font-size: 0.65rem;
  display: flex; align-items: center; justify-content: center; border: 1px solid var(--border);
}
.dot.m { background: var(--crit-bg); color: var(--critical); border-color: var(--critical); }
.dot.h { background: var(--surface-2); color: var(--good); }
.dot.n { color: var(--muted); border-style: dashed; }

/* ---------- chart ---------- */
.chart-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; margin: 10px 0 6px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 0.82rem; color: var(--ink-2); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-line { width: 16px; height: 0; border-top: 2px solid; }
.legend-line.dashed { border-top-style: dashed; }
.seg { display: inline-flex; border: 1px solid var(--axis); border-radius: 8px; overflow: hidden; }
.seg button { background: none; border: 0; padding: 5px 10px; font-size: 0.82rem; color: var(--ink-2); }
.seg button.on { background: var(--surface-2); color: var(--ink); font-weight: 600; }
.chart-wrap { position: relative; width: 100%; }
.chart-wrap svg { display: block; width: 100%; height: auto; }
.chart-empty { padding: 40px 0; text-align: center; color: var(--muted); }

.tooltip {
  position: fixed; pointer-events: none; z-index: 10;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.15); border-radius: 8px;
  padding: 8px 10px; font-size: 0.82rem; font-variant-numeric: tabular-nums;
}
.tooltip .tt-title { font-weight: 600; margin-bottom: 2px; }

/* ---------- profile ---------- */
.profile-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.macro-split { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; border: 1px solid var(--grid); border-radius: 8px; padding: 8px 12px 0; margin: 0 0 12px; }
.macro-split .field { flex: 1 1 80px; }
.macro-split legend { font-size: 0.8rem; color: var(--ink-2); padding: 0 4px; }
.macro-split > span { margin-bottom: 18px; }
@media (max-width: 560px) {
  .profile-grid, .custom-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: span 1; }
}
