:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --line: #deddd8;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7973;
  --accent: #2a78d6;
  --good: #0a7d32;   --good-bg: #e3f4e7;
  --bad: #b42318;    --bad-bg: #fde8e6;
  --warn: #8a5a00;   --warn-bg: #fdf1d6;
  --info-bg: #ebeae6;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --band: rgba(235, 104, 52, .14);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --line: #34342f;
    --text: #f4f4f1; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #5598e7;
    --good: #5fd08a; --good-bg: #173322; --bad: #ff8a80; --bad-bg: #3a1c1a;
    --warn: #f2c25b; --warn-bg: #3a2e12; --info-bg: #2a2a27;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --band: rgba(217, 89, 38, .22);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --line: #34342f;
  --text: #f4f4f1; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #5598e7;
  --good: #5fd08a; --good-bg: #173322; --bad: #ff8a80; --bad-bg: #3a1c1a;
  --warn: #f2c25b; --warn-bg: #3a2e12; --info-bg: #2a2a27;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --band: rgba(217, 89, 38, .22);
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.25rem; margin: .2rem 0 .6rem; }
h2 { font-size: 1.05rem; margin: 1.6rem 0 .5rem; }
h3 { font-size: .95rem; margin: 1rem 0 .4rem; color: var(--text-2); }
.wrap { max-width: 1180px; margin: 0 auto; padding: 0 12px 48px; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.num { text-align: right; white-space: nowrap; }

/* top bar */
header.top {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 10;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
header.top .wrap { display: flex; align-items: center; gap: 4px; padding-block: 6px; flex-wrap: wrap; padding-bottom: 6px; }
header.top .brand { font-weight: 700; margin-right: 8px; color: var(--text); }
header.top nav { display: flex; gap: 2px; flex-wrap: wrap; flex: 1; }
header.top nav a { padding: 6px 10px; border-radius: 6px; color: var(--text-2); }
header.top nav a.on { background: var(--surface-2); color: var(--text); font-weight: 600; }
header.top form { display: flex; }
header.top input[type=search] { width: 140px; }
button.theme { background: none; border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; color: var(--text-2); cursor: pointer; }

.banner { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: 8px 12px; margin: 10px 0; font-size: .9rem; }
.banner div + div { margin-top: 4px; }
.note { background: var(--info-bg); border-radius: 8px; padding: 10px 12px; margin: 10px 0; color: var(--text-2); }

input, select, button, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px;
}
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); cursor: pointer; }

/* filters */
form.filters { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; margin: 10px 0; }
form.filters label { display: flex; gap: 4px; align-items: center; color: var(--text-2); }
.count { color: var(--muted); margin-left: auto; font-size: .85rem; }

/* tables */
.tbl-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; background: var(--surface); }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; text-align: left; }
th { font-size: .78rem; color: var(--text-2); font-weight: 600; white-space: nowrap; background: var(--surface-2); position: sticky; top: 0; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable::after { content: " ↕"; color: var(--muted); font-weight: 400; }
th.asc::after { content: " ↑"; color: var(--text); }
th.desc::after { content: " ↓"; color: var(--text); }
tr:hover td { background: var(--surface-2); }

/* the avis */
.avis td.name { min-width: 220px; }
.avis .thumb { width: 36px; height: 36px; object-fit: contain; border-radius: 4px; background: #fff; float: left; margin-right: 8px; }
.avis .up { font-size: 1.25rem; font-weight: 700; white-space: nowrap; }
.avis .up small { font-size: .75rem; font-weight: 400; color: var(--muted); }
.disc { font-weight: 700; white-space: nowrap; }
.disc.pos { color: var(--good); }
.disc.neg { color: var(--bad); }
.basis { display: block; font-size: .72rem; color: var(--muted); font-weight: 400; max-width: 180px; white-space: normal; }
.badge { display: inline-block; font-size: .72rem; border-radius: 4px; padding: 1px 6px; margin: 2px 2px 0 0; white-space: nowrap; font-weight: 600; }
.b-fake { background: var(--bad-bg); color: var(--bad); }
.b-low { background: var(--good-bg); color: var(--good); }
.b-new { background: var(--info-bg); color: var(--text-2); font-weight: 500; }
.b-err { background: var(--warn-bg); color: var(--warn); }
.b-up { background: var(--info-bg); color: var(--accent); }
.store { white-space: nowrap; }
.store small { color: var(--muted); }

/* Phones: each row becomes a card. Flex with explicit order, so the cells keep
   their table order (and column sorting still works on the same DOM). */
@media (max-width: 760px) {
  .avis thead { display: none; }
  .avis, .avis tbody { display: block; }
  .avis tr {
    display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 10px;
    padding: 8px 4px; border-bottom: 1px solid var(--line); background: var(--surface);
  }
  .avis td { border: 0; padding: 0; background: none !important; }
  .avis td.name { order: 1; flex: 1 1 calc(100% - 140px); min-width: 0; }
  .avis td.c-up { order: 2; flex: 0 0 130px; text-align: right; }
  .avis td.c-store { order: 3; }
  .avis td.c-price { order: 4; }
  .avis td.c-until { order: 5; }
  .avis td.c-store, .avis td.c-price, .avis td.c-until { flex: 0 0 auto; font-size: .82rem; color: var(--text-2); }
  .avis td.c-price::before { content: "pris "; color: var(--muted); }
  .avis td.c-until::before { content: "til "; color: var(--muted); }
  .avis td.c-disc { order: 6; flex: 1 0 100%; text-align: left; white-space: normal; }
  .avis td.c-disc .basis { display: inline; margin-left: 4px; }
  .avis td.c-prot { order: 7; }
  .avis td.c-kcal { order: 8; }
  .avis td.c-prot, .avis td.c-kcal { display: none; font-size: .78rem; color: var(--muted); text-align: left; }
  .avis td.c-prot.has, .avis td.c-kcal.has { display: block; flex: 0 0 auto; }
  .avis td.c-prot::after { content: " kr/100 g protein"; }
  .avis td.c-kcal::after { content: " kr/1000 kcal"; }
  .avis td.c-badges { order: 9; flex: 1 0 100%; }
  .avis td.c-badges:empty { display: none; }
  .avis .thumb { width: 30px; height: 30px; }
  header.top input[type=search] { width: 96px; }
  header.top nav a { padding: 6px 7px; }
  form.filters select { max-width: 46%; }
}

/* cards / stats */
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin: 10px 0; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.stat .k { font-size: .75rem; color: var(--muted); }
.stat .v { font-size: 1.2rem; font-weight: 700; }
.stat .v small { font-size: .75rem; color: var(--muted); font-weight: 400; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin: 10px 0; }
.chart-box { position: relative; height: 300px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 760px) { .two { grid-template-columns: 1fr; } .chart-box { height: 240px; } }

/* bar rankings (CSS bars, no chart library) */
.bars { list-style: none; padding: 0; margin: 0; }
.bars li { display: grid; grid-template-columns: minmax(0, 1fr) 70px; gap: 6px; align-items: center; padding: 2px 0; font-size: .82rem; }
.bars .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bars .track { grid-column: 1 / -1; height: 6px; background: var(--surface-2); border-radius: 3px; margin-bottom: 4px; }
.bars .fill { height: 100%; background: var(--s1); border-radius: 3px; }
.bars .val { text-align: right; font-weight: 600; }

/* heatmap */
.heat td { text-align: center; font-size: .72rem; padding: 3px; min-width: 30px; }
.heat td.on { background: color-mix(in srgb, var(--s2) calc(var(--n) * 30%), var(--surface)); color: var(--text); }
.heat th { font-weight: 500; }

.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: .8rem; color: var(--text-2); margin: 4px 0; }
.legend i { display: inline-block; width: 12px; height: 4px; border-radius: 2px; vertical-align: middle; margin-right: 4px; }
.legend i.band { height: 10px; opacity: .5; }

pre.sql, textarea.sql { font: 12.5px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; }
textarea.sql { width: 100%; min-height: 120px; }
details summary { cursor: pointer; color: var(--text-2); }

.login { max-width: 320px; margin: 18vh auto; padding: 0 16px; }
.login input { width: 100%; margin: 8px 0; font-size: 16px; }
.login button { width: 100%; font-size: 16px; padding: 10px; }
.err { color: var(--bad); }

/* inventory / shopping */
.quickadd { display: grid; gap: 8px; }
.quickadd input, .quickadd select { font-size: 16px; min-width: 0; }  /* 16px: no iOS zoom-on-focus */
.quickadd .row { display: flex; gap: 8px; flex-wrap: wrap; }
.quickadd .row > * { flex: 1 1 120px; }
.quickadd .row > button { flex: 0 0 auto; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips label { cursor: pointer; }
.chips input { position: absolute; opacity: 0; pointer-events: none; }
.chips span { display: inline-block; padding: 6px 10px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); color: var(--text-2); font-size: .85rem; }
.chips input:checked + span { background: var(--accent); border-color: var(--accent); color: #fff; }
.chips input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
table.inv td { vertical-align: middle; padding: 8px 6px; }
table.inv td.what { width: 100%; }
table.inv td.left { white-space: nowrap; text-align: right; }
table.inv td.acts { white-space: nowrap; }
table.inv td.acts form { display: inline; }
table.inv button { padding: 7px 10px; cursor: pointer; }
table.inv tr.soon td { background: var(--warn-bg); }
table.inv tr.late td { background: var(--bad-bg); }
button.ghost { background: none; color: var(--text-2); }
