:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1d1f1c;
  --muted: #6b6f68;
  --border: #e3e1da;
  --accent: #2f6f4f;
  --accent-contrast: #ffffff;
  --danger: #b3261e;
  --warn-bg: #fff4d6;
  --warn-text: #7a5a00;
  --ok-bg: #e3f1e8;
  --ok-text: #245c3f;
  --row-bad: #fdecea;
  --radius: 10px;
  color-scheme: light;
}

[hidden] { display: none !important; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #151715;
    --surface: #1e211e;
    --text: #e8e9e4;
    --muted: #9ca198;
    --border: #323631;
    --accent: #6fbf8f;
    --accent-contrast: #0f1a13;
    --danger: #f2b8b5;
    --warn-bg: #3a3218;
    --warn-text: #f3d98a;
    --ok-bg: #1f3a2a;
    --ok-text: #a8dcbc;
    --row-bad: #3a2120;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0 0 .5rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }

.topbar {
  display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
  padding: .75rem 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; color: var(--text); text-decoration: none; }
.topbar nav { display: flex; gap: 1rem; flex-wrap: wrap; flex: 1; }
.topbar nav a { text-decoration: none; color: var(--muted); }
.topbar nav a:hover { color: var(--text); }

.container { max-width: 1000px; margin: 1.5rem auto; padding: 0 16px; }
.container.narrow { max-width: 420px; margin-top: 10vh; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.25rem; margin-bottom: 1.25rem;
}
.card.flush { padding: 0; overflow: hidden; }
.card .pad { padding: 1.25rem 1.25rem 0; }

.heading-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.heading-row > div:first-child { min-width: 0; }
.actions { display: flex; gap: .5rem; }

label { display: block; font-weight: 500; margin-bottom: .75rem; }
input, textarea, select {
  width: 100%; margin-top: .25rem; padding: .55rem .7rem; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
}
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.row { display: flex; gap: .75rem; align-items: flex-end; flex-wrap: wrap; }
.row > input { flex: 1; min-width: 200px; margin: 0; }
.row .grow { flex: 1; min-width: 200px; }
.row .small-field { width: 120px; }

button, .button {
  display: inline-block; padding: .55rem 1rem; font: inherit; font-weight: 500; cursor: pointer;
  border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  text-decoration: none; white-space: nowrap;
}
button.primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
button.danger { color: var(--danger); }
button.link { border: 0; background: none; color: var(--muted); padding: 0; }
button:disabled { opacity: .6; cursor: progress; }

.muted { color: var(--muted); }
.small { font-size: .85em; }
.strong { font-weight: 600; }
.nowrap { white-space: nowrap; }
.ellipsis { display: block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.error { color: var(--danger); }
.warning { color: var(--muted); margin: .5rem 0 0; }
ul.plain { list-style: none; padding: 0; margin: 0; }
ul.plain li { padding: .3rem 0; }

.tag {
  display: inline-block; font-size: .72rem; padding: .05rem .45rem; border-radius: 99px;
  background: var(--bg); border: 1px solid var(--border); color: var(--muted); vertical-align: middle;
}
.tag.warn { background: var(--warn-bg); color: var(--warn-text); border-color: transparent; }
.tag.ok { background: var(--ok-bg); color: var(--ok-text); border-color: transparent; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .55rem .75rem; border-bottom: 1px solid var(--border); vertical-align: top; }
thead th { font-size: .8rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
tbody tr:last-child td, tbody tr:last-child th { border-bottom: 0; }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.nutrition tbody th { font-weight: 500; white-space: nowrap; }

.summary-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.summary-head label { display: flex; align-items: center; gap: .5rem; margin: 0; font-weight: 400; }
.summary-head input { width: 80px; margin: 0; }

table.ingredients tr.unmatched td { background: var(--row-bad); }
table.ingredients tr.ignored td { color: var(--muted); }
input.grams { width: 80px; text-align: right; margin: 0; padding: .3rem .45rem; }
.food-cell { min-width: 260px; }
.food-name { margin-right: .25rem; }
details.swap { margin-top: .3rem; }
details.swap summary { cursor: pointer; color: var(--accent); font-size: .85em; }
details.swap input { margin-top: .4rem; }
.food-options { list-style: none; margin: .4rem 0 0; padding: 0; max-height: 260px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; }
.food-options li + li { border-top: 1px solid var(--border); }
button.option {
  width: 100%; display: flex; justify-content: space-between; gap: .75rem; text-align: left;
  border: 0; border-radius: 0; white-space: normal; padding: .45rem .6rem;
}
button.option:hover { background: var(--bg); }

.login h1 { margin-bottom: 0; }
.login form { margin-top: 1rem; }
.login button { width: 100%; }

pre.json {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: .75rem;
  overflow-x: auto; font-size: 12.5px; max-height: 480px;
}
.key-reveal { font-family: ui-monospace, Menlo, monospace; padding: .75rem; background: var(--warn-bg); color: var(--warn-text); border-radius: 8px; word-break: break-all; }
.status-ok { color: var(--ok-text); }
.status-error { color: var(--danger); }

@media (max-width: 640px) {
  .topbar { gap: .75rem; }
  .actions { width: 100%; }
}

/* admin */
.subnav, .filters { display: flex; gap: .25rem; flex-wrap: wrap; margin-bottom: 1rem; }
.subnav a, .filters a { padding: .3rem .7rem; border-radius: 99px; text-decoration: none; color: var(--muted); }
.subnav a.active, .filters a.active { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.filters { margin: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.stats .card { margin: 0; }
.stat .value { font-size: 1.8rem; font-weight: 600; font-variant-numeric: tabular-nums; }
dl.meta { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0; }
dl.meta dt { color: var(--muted); }
dl.meta dd { margin: 0; min-width: 0; }
.url-cell { max-width: 420px; }
.pager { display: flex; gap: 1rem; }
details.card summary { cursor: pointer; font-weight: 600; }
h3 { font-size: 1rem; margin: 1.25rem 0 .5rem; }
code { font-family: ui-monospace, Menlo, monospace; font-size: .9em; }

/* progress + save feedback */
.progress { display: flex; align-items: center; gap: .9rem; margin-top: 1rem; padding: .9rem 1rem;
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.progress[hidden] { display: none; }
.progress-step { font-weight: 500; }
.spinner { width: 22px; height: 22px; flex: none; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 10;
  max-width: calc(100% - 32px); padding: .6rem 1rem; border-radius: 8px; font-weight: 500;
  background: var(--text); color: var(--bg); box-shadow: 0 4px 16px rgba(0,0,0,.18); }
.toast[hidden] { display: none; }
.toast.ok { background: var(--ok-bg); color: var(--ok-text); border: 1px solid var(--ok-text); }
.toast.error { background: var(--row-bad); color: var(--danger); border: 1px solid var(--danger); }

.hint { margin: .25rem 0 1rem; max-width: 70ch; }
.card .pad h2 { margin-bottom: .25rem; }
tr.flash td { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: var(--ok-bg); } to { background: transparent; } }
form.htmx-request input.grams, button.option.htmx-request { opacity: .5; cursor: progress; }

.recovery-key { display: block; margin: .75rem 0; padding: .75rem; font: 1.15rem/1.6 ui-monospace, monospace;
  letter-spacing: .05em; background: var(--ok-bg); border-radius: 6px; user-select: all; word-break: break-all; }
label.check { display: flex; gap: .5rem; align-items: center; }
p.ok { color: var(--ok-text); }
button.danger { color: var(--danger); }

/* catalog refs list */
ul.plain { list-style: none; margin: 0; padding: 0; }
ul.plain li { display: flex; justify-content: space-between; gap: 1rem; align-items: baseline; padding: .35rem 0; border-top: 1px solid var(--border); }
ul.plain li:first-child { border-top: 0; }

/* food log */
.log-nav { align-items: center; }
.log-date { margin: -.5rem 0 1rem; text-transform: capitalize; }
.meal { margin-bottom: 1rem; }
.meal-head { display: flex; align-items: center; gap: .75rem; padding: .6rem 1rem; }
.meal-head h2 { margin: 0; font-size: 1.05rem; flex: 1; }
ul.entries li { padding: .5rem 1rem; align-items: center; flex-wrap: wrap; gap: .25rem .75rem; }
ul.entries li.selected { background: var(--ok-bg); }
.entry-check { margin: 0; width: auto; flex: none; }
button.entry-toggle { flex: 1; min-width: 0; text-align: left; color: var(--text); white-space: normal; font-weight: 400; }
.entry-details { flex-basis: 100%; margin: 0; padding-left: 1.75rem; }
dl.entry-details dt, dl.entry-details dd { font-size: .85rem; font-weight: 400; }
.select-bar { position: sticky; bottom: 1rem; z-index: 4; display: flex; flex-wrap: wrap; align-items: center; gap: .5rem 1rem;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .15); }
.select-bar [data-role=select-count] { flex: 1; }
ul.collection-items li { border: 0; padding: .1rem 0; }
ul.collection-edit li { align-items: center; gap: .5rem; }
ul.collection-edit .item-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
ul.collection-edit label { display: flex; align-items: center; gap: .35rem; margin: 0; font-weight: 400; }
fieldset.add-item { border: 1px solid var(--border); border-radius: 8px; padding: .5rem .75rem .75rem; margin: 0; }
fieldset.add-item .combo { margin: 0; }
fieldset.add-item .row { align-items: center; }
fieldset.add-item .row label { display: flex; align-items: center; gap: .35rem; margin: 0; }
.entry-name { min-width: 0; overflow-wrap: anywhere; }
dl.totals-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr)); gap: .5rem 1rem; }
.totals-grid dt { grid-row: 1; }
.totals-grid dd { grid-row: 2; font-size: 1.1rem; font-weight: 600; }
.footer-note { margin-top: 1.5rem; }
/* .card's margin-bottom would override the UA's margin:auto and push modals to the bottom of the screen. */
dialog.card { max-width: 34rem; width: calc(100% - 2rem); max-height: calc(100dvh - 2rem); margin: auto; box-sizing: border-box;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); }
/* In a modal (which scrolls) an absolute list is clipped: keep it in the flow so the dialog grows instead. */
dialog ul.suggestions { position: static; margin-top: .25rem; }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.close-row { display: flex; justify-content: space-between; align-items: center; }
.close-row h2 { margin: 0; }
.tabs { display: flex; gap: .25rem; margin: .75rem 0; flex-wrap: wrap; }
.tabs button[aria-selected=true] { background: var(--accent); color: var(--accent-contrast); }
.tabs button[hidden] { display: none; }
.combo { position: relative; margin: .75rem 0; }
.combo input { width: 100%; box-sizing: border-box; }
ul.suggestions { position: absolute; left: 0; right: 0; z-index: 5; margin: 0; padding: 0; list-style: none;
  max-height: 16rem; overflow-y: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
ul.suggestions[hidden] { display: none; }
ul.suggestions li { padding: .45rem .75rem; cursor: pointer; }
ul.suggestions li[aria-selected=true], ul.suggestions li:hover { background: var(--ok-bg); }
.amount { margin-top: .75rem; }
.stack { display: flex; flex-direction: column; gap: .75rem; }

/* statistics */
.stats-controls { display: flex; flex-wrap: wrap; gap: .5rem 1rem; align-items: center; }
.stats-controls .tabs { margin: 0; }
.tabs button[aria-pressed=true] { background: var(--accent); color: var(--accent-contrast); }
.chart-card { margin-top: 1rem; }
.chart { width: 100%; min-height: 240px; }
.chart .u-legend { color: var(--text); }
.chart-card table { font-size: .9rem; }
.chart-card td, .chart-card th { text-align: right; padding: .2rem .5rem; }
.chart-card td:first-child, .chart-card th:first-child { text-align: left; }
.scan-video { width: 100%; max-width: 24rem; border-radius: .5rem; background: #000; }
.scan-video[hidden] { display: none; }
