/* Food Diary stylesheet. Mobile first, no framework. */
:root {
  --bg: #f6f5f0;
  --surface: #ffffff;
  --surface-2: #efede6;
  --text: #1f2421;
  --muted: #6b716d;
  --line: #dcd9cf;
  --accent: #2f6f4e;
  --accent-text: #ffffff;
  --accent-soft: #dcefe4;
  --danger: #b4463a;
  --danger-soft: #f6e0dc;
  --warn: #b8862b;
  --warn-soft: #f7ecd4;
  --ok: #2f6f4e;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
  --nav-h: 58px;
  --page-x: .45rem;
  --card-x: .7rem;
  --protein: #3b6fb6;
  --carbs: #c98a2b;
  --fat: #b4463a;
  --kcal: #2f6f4e;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15181a;
    --surface: #1f2427;
    --surface-2: #272d31;
    --text: #e7e9e4;
    --muted: #9aa39d;
    --line: #353c40;
    --accent: #5fb58a;
    --accent-text: #0f1a14;
    --accent-soft: #223a2e;
    --danger: #e07a6e;
    --danger-soft: #3a2420;
    --warn: #d9a64a;
    --warn-soft: #3a311d;
    --ok: #5fb58a;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
    --bg: #15181a;
    --surface: #1f2427;
    --surface-2: #272d31;
    --text: #e7e9e4;
    --muted: #9aa39d;
    --line: #353c40;
    --accent: #5fb58a;
    --accent-text: #0f1a14;
    --accent-soft: #223a2e;
    --danger: #e07a6e;
    --danger-soft: #3a2420;
    --warn: #d9a64a;
    --warn-soft: #3a311d;
    --ok: #5fb58a;
    --shadow: none;
  }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
small, .muted { color: var(--muted); }
hr { border: 0; border-top: 1px solid var(--line); margin: 1rem 0; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; background: var(--surface-2); padding: .1em .3em; border-radius: 4px; }
pre { background: var(--surface-2); padding: .75rem; border-radius: var(--radius); overflow: auto; }

.topbar {
  padding: calc(.6rem + env(safe-area-inset-top)) 0 .6rem;
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.topbar-inner { max-width: 720px; margin: 0 auto; display: flex; align-items: center; justify-content: space-between; padding: 0 calc(var(--page-x) + var(--card-x) + 1px); }
.brand { font-weight: 700; color: var(--text); letter-spacing: .01em; }
.user-menu { position: relative; }
.user-menu > summary { list-style: none; display: flex; align-items: center; gap: .5rem; cursor: pointer; color: var(--muted); font-size: .9rem; padding: .2rem .3rem; border-radius: 999px; }
.user-menu > summary::-webkit-details-marker { display: none; }
.user-menu > summary:hover { background: var(--surface-2); }
.user-menu[open] > summary { color: var(--text); }
.user-menu .menu {
  position: absolute; right: 0; top: calc(100% + .35rem); z-index: 30; min-width: 13rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,.12); padding: .3rem;
}
.user-menu .menu a, .user-menu .menu button { display: block; width: 100%; text-align: left; padding: .55rem .75rem; border: 0; background: none; color: var(--text); border-radius: 8px; font-weight: 500; min-height: 0; height: auto; }
.user-menu .menu a:hover, .user-menu .menu button:hover { background: var(--surface-2); text-decoration: none; }
.user-menu .menu hr { margin: .3rem 0; }
.user-menu .menu .menu-title { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: .3rem .75rem 0; }
.user-menu .menu form { margin: 0; }
.avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto; }
.avatar-initial { background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: .85rem; }
.avatar-lg { width: 72px; height: 72px; font-size: 1.8rem; }
.avatar-row { display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.avatar-form { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }
.avatar-form label.btn { cursor: pointer; }
@media (max-width: 400px) { .user-menu .user-name { max-width: 7rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } }

.container { max-width: 720px; margin: 0 auto; padding: .6rem var(--page-x); }
.container > h1, .container > p { padding-left: calc(var(--card-x) + 1px); padding-right: calc(var(--card-x) + 1px); }
@media (min-width: 760px) { :root { --page-x: 1.5rem; --card-x: 1rem; } .container { padding: 1.5rem var(--page-x); } }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: .8rem var(--card-x); margin-bottom: .7rem; box-shadow: var(--shadow);
}
@media (min-width: 760px) { .card { padding: 1rem var(--card-x); margin-bottom: 1rem; } }
.card > :last-child { margin-bottom: 0; }

.flash {
  background: var(--accent-soft); color: var(--text); border: 1px solid var(--accent);
  border-radius: var(--radius); padding: .6rem .9rem; margin-bottom: 1rem;
}
.flash.error, .error-box { background: var(--danger-soft); border-color: var(--danger); }
.error-text { color: var(--danger); }

/* Forms */
label { display: block; font-size: .9rem; color: var(--muted); margin-bottom: .25rem; }
input[type=text], input[type=password], input[type=email], input[type=number], input[type=date],
input[type=time], input[type=search], input[type=url], select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; padding: .5rem .75rem; min-height: 44px;
  box-sizing: border-box; -webkit-appearance: none; appearance: none;
}
input[type=text], input[type=password], input[type=email], input[type=number], input[type=date],
input[type=time], input[type=search], input[type=url], select { height: 44px; line-height: 1.3; }
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 1.05rem) 50%, calc(100% - .7rem) 50%; background-size: .35rem .35rem; background-repeat: no-repeat; padding-right: 1.8rem;
}
input[type=date]::-webkit-calendar-picker-indicator, input[type=time]::-webkit-calendar-picker-indicator { padding: 0; margin: 0; width: 1.1rem; height: 1.1rem; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
textarea { min-height: 5rem; resize: vertical; }
.field { margin-bottom: .9rem; }
.row { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end; }
.row > * { flex: 1 1 0; min-width: 0; }
.row > div { flex: 1 1 9rem; }
.row input[type=date] { min-width: 9.5rem; }
.row > button, .row > .btn, .row > form { flex: 0 0 auto; }
.row > button, .row > .btn { height: 44px; }
.row > button.sm { height: 34px; }
.row.tight { gap: .4rem; }
.inline { display: inline-flex; align-items: center; gap: .5rem; }
fieldset { border: 0; padding: 0; margin: 0 0 .9rem; }
.check { display: flex; align-items: center; gap: .5rem; color: var(--text); font-size: 1rem; }
.check input { width: 1.2rem; height: 1.2rem; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; min-height: 44px; padding: .55rem 1rem;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  color: var(--text); cursor: pointer; text-decoration: none; white-space: nowrap;
}
button:hover, .btn:hover { text-decoration: none; background: var(--surface-2); }
.btn-primary, button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover, button.primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger, button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover, button.danger:hover { background: var(--danger-soft); }
.btn-sm, button.sm { min-height: 34px; padding: .3rem .7rem; font-size: .9rem; font-weight: 500; }
.btn-link, button.link { background: none; border: 0; color: var(--accent); padding: .3rem .4rem; min-height: 34px; font-weight: 500; }
.btn-block { width: 100%; }
button[disabled], .btn[aria-disabled=true] { opacity: .5; cursor: default; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-top: .75rem; }
.actions.right { justify-content: flex-end; }
form.inline-form { display: inline; }

/* Lists & tables */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; justify-content: space-between; align-items: center; gap: .75rem; padding: .6rem 0; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list.compact li { padding: .35rem 0; font-size: .92rem; }
table { width: 100%; border-collapse: collapse; font-size: .95rem; }
th, td { text-align: left; padding: .5rem .4rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: .85rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

/* Chips / badges */
.chip {
  display: inline-flex; align-items: center; gap: .3rem; padding: .3rem .7rem; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface-2); font-size: .85rem; color: var(--text);
  min-height: 34px; cursor: pointer;
}
.chip[aria-pressed=true], .chip.on { background: var(--accent-soft); border-color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .75rem; background: var(--surface-2); color: var(--muted); }
.badge.admin { background: var(--accent-soft); color: var(--accent); }
.badge.off { background: var(--danger-soft); color: var(--danger); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }

/* Bottom navigation */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottom-nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; min-height: var(--nav-h); font-size: .72rem; color: var(--muted); text-decoration: none;
}
.bottom-nav a .ico { font-size: 1.15rem; line-height: 1; }
.bottom-nav a[aria-current=page] { color: var(--accent); font-weight: 600; }

/* Day page */
.day-nav { display: flex; align-items: center; justify-content: space-between; gap: .25rem; margin-bottom: .5rem; }
.day-nav h1 { font-size: 1.15rem; margin: 0; text-align: center; flex: 1; line-height: 1.3; }
.nav-arrow { flex: 0 0 40px; width: 40px; height: 40px; min-height: 0; padding: 0; border: 0; background: transparent; box-shadow: none; color: var(--accent); font-size: 1.7rem; line-height: 1; display: inline-flex; align-items: center; justify-content: center; }
.nav-arrow:hover { background: var(--surface-2); border-radius: 50%; }
.today-tag { display: inline-block; font-size: .72em; font-weight: 500; line-height: 1.5; padding: 0 .5em; border-radius: 999px; border: 1px solid transparent; vertical-align: middle; }
.today-link { color: var(--accent); border-color: var(--accent); text-decoration: none; }
.today-link:hover { background: var(--accent-soft); text-decoration: none; }
.meal { margin-bottom: 1rem; }
.meal-head { display: flex; justify-content: space-between; align-items: baseline; padding: 0 .25rem .35rem; }
.meal-head h2 { margin: 0; }
.meal-kcal { color: var(--muted); font-variant-numeric: tabular-nums; font-size: .9rem; }
.entry { display: grid; grid-template-columns: 1fr auto auto; gap: .25rem 0; align-items: start; padding: .4rem .25rem; }
.entry .order { display: flex; flex-direction: row; align-items: center; gap: 0; min-width: 0; max-width: 0; overflow: hidden; opacity: 0; pointer-events: none; transition: opacity .25s ease, max-width .25s ease, margin-left .25s ease; }
.entry .order form { display: flex; margin: 0; }
.meal:has(details.add-form[open]) .entry .order { opacity: 1; pointer-events: auto; max-width: 3rem; margin-left: .4rem; }
@supports not selector(:has(a)) { .entry .order { opacity: 1; pointer-events: auto; max-width: 3rem; margin-left: .4rem; } }
.entry .order button { min-height: 0; height: 20px; width: 18px; padding: 0; border: 0; background: none; color: var(--muted); font-size: .55rem; line-height: 1; }
.entry .order button:hover { color: var(--accent); background: none; }
.tgroup { padding: .35rem 0 .2rem; border-top: 1px solid var(--line); }
.tgroup:first-of-type { border-top: 0; }
.tgroup-head { display: flex; justify-content: space-between; align-items: baseline; padding: 0 .25rem; font-size: .8rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.tgroup-head a { color: var(--muted); text-decoration: none; border-bottom: 1px dotted var(--line); }
.tgroup-head a:hover { color: var(--accent); }
.weight-row { margin-top: 1rem; align-items: flex-end; }
.weight-row > div { flex: 1; }
.weight-row button { flex: 0 0 auto; }
.weight-row label a { font-weight: 400; }
.entry .time { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; min-width: 2.8rem; padding-top: .15rem; }
.entry .desc { min-width: 0; display: flex; flex-wrap: wrap; column-gap: .4rem; align-items: baseline; }
.entry .desc a { color: var(--text); }
.entry .desc .name { flex: 1 1 7rem; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .amount { color: var(--muted); font-size: .9rem; flex: 0 1 auto; max-width: 62%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .note { color: var(--muted); font-size: .85rem; margin-top: .1rem; flex-basis: 100%; }
.entry .kcal { font-variant-numeric: tabular-nums; color: var(--muted); font-size: .9rem; white-space: nowrap; padding-top: .15rem; margin-left: .6rem; min-width: 2.4rem; text-align: right; }
.entry .thumbs { grid-column: 1 / span 3; display: flex; gap: .4rem; flex-wrap: wrap; margin-top: .3rem; }
.entry .thumbs img { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); }
.entry.symptom { background: var(--warn-soft); border-radius: 8px; margin-top: .25rem; grid-template-columns: auto 1fr auto; }
.entry.symptom .desc { color: var(--text); }
.add-form { padding: .4rem .25rem 0; border-top: 1px dashed var(--line); margin-top: .25rem; }
.add-form summary { padding: .35rem 0; list-style: none; display: flex; align-items: center; gap: .5rem; }
.add-form summary::-webkit-details-marker { display: none; }
.add-form summary::before { content: ""; width: 0; height: 0; border-left: .5rem solid currentColor; border-top: .35rem solid transparent; border-bottom: .35rem solid transparent; transition: transform .2s ease; flex: 0 0 auto; }
.add-form[open] > summary::before { transform: rotate(90deg); }
.add-form .add-body { overflow: hidden; margin: 0 -4px -4px; padding: 0 4px 4px; }
.add-form[open] > .add-body { animation: slide-open .22s ease-out; }
@keyframes slide-open { from { max-height: 0; opacity: 0; } to { max-height: 60rem; opacity: 1; } }
.add-form .add-body.closing { animation: slide-close .18s ease-in forwards; }
@keyframes slide-close { from { max-height: 60rem; opacity: 1; } to { max-height: 0; opacity: 0; } }
.quick-add { display: flex; gap: .4rem; }
.quick-add input { flex: 1; }

/* Totals */
.totals { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .4rem; }
.total { background: var(--surface-2); border-radius: 10px; padding: .45rem .5rem; min-width: 0; }
.total .label { font-size: .72rem; line-height: 1.2; color: var(--muted); }
.total .value { font-weight: 700; font-variant-numeric: tabular-nums; font-size: 1rem; line-height: 1.3; white-space: nowrap; }
.total .value small { display: block; font-weight: 400; font-size: .72rem; color: var(--muted); white-space: normal; line-height: 1.25; }
.total > small { display: block; font-size: .72rem; }
.bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: .35rem; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 3px; }
.bar.over > span { background: var(--danger); }
.bar.none > span { background: var(--muted); }
.total.kcal .bar > span { background: var(--kcal); }
.total.protein .bar > span { background: var(--protein); }
.total.carbs .bar > span { background: var(--carbs); }
.total.fat .bar > span { background: var(--fat); }

/* Amount row: grams, portion, count, unlink */
[data-food-amount] [data-amount-g] { flex: 1 1 4.5rem; }
[data-food-amount] [data-portion] { flex: 2 1 7rem; }
[data-food-amount] [data-count] { flex: 0 0 3.6rem !important; }
[data-food-amount] [data-unlink] { flex: 0 0 2.2rem !important; width: 2.2rem; padding: 0; height: 44px; }

/* Food picker */
.picker { position: relative; }
.picker-results { list-style: none; margin: .3rem 0 0; padding: 0; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); max-height: 50vh; overflow: auto; }
.picker-results:empty { display: none; }
.picker-results li { border-top: 1px solid var(--line); }
.picker-results li:first-child { border-top: 0; }
.picker-results button { width: 100%; justify-content: space-between; border: 0; border-radius: 0; background: none; font-weight: 500; text-align: left; }
.picker-results .meta { color: var(--muted); font-size: .8rem; font-weight: 400; }
.picker-results .dish-row { display: flex; align-items: center; justify-content: space-between; gap: .4rem; padding: .35rem .6rem; }
.picker-results .dish-name { min-width: 0; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-results .dish-actions { display: flex; gap: .3rem; flex: 0 0 auto; align-items: center; }
.picker-results .dish-actions button, .picker-results .dish-actions a {
  width: auto; min-height: 32px; height: 32px; padding: 0 .6rem; border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface-2); font-size: .85rem; font-weight: 500; display: inline-flex; align-items: center; text-decoration: none; color: var(--text);
}
.preview { color: var(--muted); font-size: .9rem; min-height: 1.4rem; }
.preview:empty { display: none; }

/* Charts */
.chart { width: 100%; height: auto; display: block; }
.chart text { font-size: 11px; fill: var(--muted); }
@media (max-width: 560px) { .chart text { font-size: 19px; } }
@media (min-width: 561px) and (max-width: 900px) { .chart text { font-size: 14px; } }
.chart .axis { stroke: var(--line); }
.chart .target { stroke: var(--danger); stroke-dasharray: 4 3; }
.chart .bar-kcal { fill: var(--kcal); }
.chart .bar-protein { fill: var(--protein); }
.chart .bar-carbs { fill: var(--carbs); }
.chart .bar-fat { fill: var(--fat); }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; }
.chart .dot { fill: var(--accent); }
.chart .sev1 { fill: #9ccc9c; } .chart .sev2 { fill: #d4c56a; } .chart .sev3 { fill: #e0a040; } .chart .sev4 { fill: #d9663a; } .chart .sev5 { fill: #b02a2a; }
.legend { display: flex; gap: .75rem; flex-wrap: wrap; font-size: .8rem; color: var(--muted); }
.legend span::before { content: ""; display: inline-block; width: .7rem; height: .7rem; border-radius: 2px; margin-right: .3rem; vertical-align: middle; background: var(--sw); }

/* Photos */
.photo-grid { display: flex; gap: .5rem; flex-wrap: wrap; }
.photo-grid img { width: 96px; height: 96px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); }
.photo-full { max-width: 100%; height: auto; border-radius: var(--radius); }

/* Scanner */
.scanner { position: relative; background: #000; border-radius: var(--radius); overflow: hidden; aspect-ratio: 4 / 3; }
.scanner video { width: 100%; height: 100%; object-fit: cover; }
.scanner .frame { position: absolute; inset: 15% 10%; border: 2px solid rgba(255,255,255,.8); border-radius: 8px; }

/* Misc */
.stack > * + * { margin-top: .75rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.right { text-align: right; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }
.htmx-indicator { opacity: 0; transition: opacity .2s; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
details summary { cursor: pointer; color: var(--accent); font-weight: 500; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.auth-card { max-width: 420px; margin: 2rem auto; }
.secret { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; word-break: break-all; background: var(--surface-2); padding: .6rem; border-radius: 8px; user-select: all; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* Install hint */
.install-hint {
  display: flex; align-items: center; gap: .5rem; max-width: 720px; margin: .75rem auto 0; padding: .5rem .75rem;
  background: var(--accent-soft); border: 1px solid var(--accent); border-radius: var(--radius); font-size: .9rem;
}
.install-hint span { flex: 1; }
@media (min-width: 760px) { .install-hint { margin-left: 1.5rem; margin-right: 1.5rem; } }
