:root {
  --bg: #faf7f2;
  --surface: #ffffff;
  --surface-2: #f3eee6;
  --text: #1f2421;
  --muted: #6b716c;
  --border: #e4ddd2;
  --accent: #2f6b3f;
  --accent-soft: #e3efe5;
  --deal: #b3261e;
  --deal-soft: #fbe9e7;
  --estimate: #8a6d1d;
  --estimate-soft: #f7efd9;
  --member: #3b5bab;
  --member-soft: #e6ecf8;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  --radius: 14px;
  --chart: #2f8a4a;       /* validated vs white: lightness, chroma, contrast */
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #151816; --surface: #1d211f; --surface-2: #252a27; --text: #e8ebe8; --muted: #9aa29c;
    --border: #323935; --accent: #7cc48e; --accent-soft: #23372a; --deal: #ff8a80; --deal-soft: #3a2220;
    --estimate: #e0c070; --estimate-soft: #36301d; --member: #9db4f0; --member-soft: #232c42;
    --shadow: none; color-scheme: dark; --chart: #3f9e5c;
  }
}
:root[data-theme="dark"] {
  --bg: #151816; --surface: #1d211f; --surface-2: #252a27; --text: #e8ebe8; --muted: #9aa29c;
  --border: #323935; --accent: #7cc48e; --accent-soft: #23372a; --deal: #ff8a80; --deal-soft: #3a2220;
  --estimate: #e0c070; --estimate-soft: #36301d; --member: #9db4f0; --member-soft: #232c42;
  --shadow: none; color-scheme: dark; --chart: #3f9e5c;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: 88px;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: clamp(1.6rem, 4vw, 2.2rem); letter-spacing: -.01em; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px;
  padding: 10px max(16px, calc((100vw - 1100px) / 2)); background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; }
.tabs a {
  text-decoration: none; color: var(--muted); padding: 6px 12px; border-radius: 999px; font-weight: 500; white-space: nowrap;
}
.tabs a.active { background: var(--accent-soft); color: var(--accent); }
#week-select {
  font: inherit; font-size: .9rem; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); max-width: 190px;
}
@media (max-width: 560px) {
  .topbar { gap: 8px; }
  .brand { display: none; }
  .tabs { gap: 2px; }
  .tabs a { padding: 6px 9px; }
}

main { max-width: 1100px; margin: 0 auto; padding: 24px 16px; }
.empty { color: var(--muted); text-align: center; padding: 48px 0; }

/* Intro */
.intro { display: grid; gap: 10px; margin-bottom: 20px; }
.intro p { margin: 0; max-width: 70ch; }
.store-summary { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; font-size: .88rem; }
.store-summary .num { margin-right: 6px; }
.notice { background: var(--estimate-soft); color: var(--estimate); padding: 8px 12px; border-radius: 10px; font-size: .9rem; }

.deal-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); margin-bottom: 28px; }
.hl {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px;
  font-size: .88rem; display: grid; gap: 4px; align-content: start;
}
.hl strong { font-size: .95rem; }
.hl-add { margin-top: 6px; justify-self: start; font-size: .82rem; padding: 5px 10px; }
.hl-title { margin-bottom: 12px; }
.hl .src { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.used-in { font-size: .82rem; margin-top: 4px; }
.watch-label { font-size: .8rem; font-weight: 600; color: var(--member); }
.hist { font-size: .8rem; font-weight: 600; }
.hist small { font-weight: 400; color: var(--muted); font-size: .78rem; }
.hist-low { color: var(--accent); }
.hist-near { color: var(--text); }
.hist-higher { color: var(--estimate); }
.watchlist { margin-bottom: 24px; }
.watchlist h2 .muted { font-weight: 400; font-size: .85rem; }
.watchlist table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.watchlist th { text-align: left; font-size: .78rem; font-weight: 600; color: var(--muted); padding: 0 8px 6px 0; }
.watchlist td { padding: 8px 8px 8px 0; border-top: 1px solid var(--border); vertical-align: top; }
@media (max-width: 600px) {
  .watchlist thead { display: none; }
  .watchlist tr { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 8px 0; border-top: 1px solid var(--border); }
  .watchlist td { border: 0; padding: 0; }
  .watchlist td:nth-child(3) { grid-row: 1; grid-column: 2; }
  .watchlist td:nth-child(2), .watchlist td:nth-child(4) { grid-column: 1 / -1; font-size: .82rem; }
}
.hl-title .muted { font-weight: 400; font-size: .95rem; }
.hl .offer { color: var(--deal); font-weight: 600; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.chip {
  font: inherit; font-size: .9rem; padding: 6px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--bg); }

/* Recipe grid */
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
@media (max-width: 400px) { .grid { grid-template-columns: 1fr; } }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; display: flex; flex-direction: column; gap: 10px; position: relative;
}
.card.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft), var(--shadow); }
.card h3 a { color: var(--text); text-decoration: none; }
.card h3 a::after { content: ""; position: absolute; inset: 0; border-radius: var(--radius); }
.card p { margin: 0; color: var(--muted); font-size: .92rem; }
.card-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: auto; padding-top: 6px; }
.card-foot .btn { position: relative; z-index: 1; }
.cost { font-weight: 600; }
.cost small { font-weight: 400; color: var(--muted); }

.tags { display: flex; gap: 6px; flex-wrap: wrap; }
.tag { font-size: .78rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.tag.snabb { background: #fff1c2; color: #7a5b00; }
.tag.frys { background: #dcecfb; color: #1d4f80; }
.tag.vegetarisk { background: var(--accent-soft); color: var(--accent); }
.tag.lyx { background: #f3e6f7; color: #6b2a80; }
.tag.budget { background: #e2f3ea; color: #1f6b44; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .tag.snabb { background: #3a3218; color: #f0d57a; }
  :root:not([data-theme="light"]) .tag.frys { background: #1c2e40; color: #9cc8f0; }
  :root:not([data-theme="light"]) .tag.lyx { background: #35223d; color: #e0b4ef; } }
:root[data-theme="dark"] .tag.snabb { background: #3a3218; color: #f0d57a; }
:root[data-theme="dark"] .tag.frys { background: #1c2e40; color: #9cc8f0; }
:root[data-theme="dark"] .tag.lyx { background: #35223d; color: #e0b4ef; }

.stores { display: flex; gap: 4px; flex-wrap: wrap; }
.store {
  display: inline-flex; align-items: center; gap: 5px; font-size: .78rem; font-weight: 600; white-space: nowrap;
  padding: 2px 8px 2px 6px; border-radius: 6px; background: var(--surface-2);
}
.store::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--store-color); }

.btn {
  font: inherit; font-size: .9rem; font-weight: 600; padding: 8px 14px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); text-decoration: none;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary, .btn[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--bg); }
.btn.ghost { background: transparent; }

/* Selection bar */
.selection-bar {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 16px; z-index: 20;
  display: flex; align-items: center; gap: 14px; padding: 10px 10px 10px 18px; border-radius: 16px;
  background: var(--text); color: var(--bg); box-shadow: 0 8px 30px rgba(0,0,0,.25);
  width: max-content; max-width: calc(100vw - 32px);
}
.selection-bar .btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .selection-bar .btn.primary { color: #10150f; } }

/* Recipe detail */
.back { display: inline-block; margin-bottom: 14px; text-decoration: none; font-weight: 500; }
.detail { display: grid; gap: 24px; grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); align-items: start; }
@media (max-width: 820px) { .detail { grid-template-columns: 1fr; } }
.detail-head { display: grid; gap: 10px; margin-bottom: 20px; }
.detail-head p { margin: 0; max-width: 70ch; }
.meta { display: flex; gap: 18px; flex-wrap: wrap; color: var(--muted); font-size: .92rem; }
.meta b { color: var(--text); }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.panel + .panel { margin-top: 16px; }
.panel h2 { margin-bottom: 12px; }
.ing { list-style: none; margin: 0; padding: 0; }
.ing li { display: grid; grid-template-columns: 5.5em 1fr; gap: 8px; padding: 7px 0; border-top: 1px solid var(--border); }
.ing li:first-child { border-top: 0; }
.ing .amt { color: var(--muted); font-size: .92rem; }
.ing .src { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 3px; }
.steps { margin: 0; padding-left: 1.3em; display: grid; gap: 10px; }
.steps li::marker { font-weight: 700; color: var(--accent); }
.freezer { background: #dcecfb; color: #173e63; border-radius: 12px; padding: 12px 14px; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .freezer { background: #1c2e40; color: #cfe3f7; } }
:root[data-theme="dark"] .freezer { background: #1c2e40; color: #cfe3f7; }
.tips { background: var(--surface-2); border-radius: 12px; padding: 12px 14px; }

.badge { font-size: .72rem; font-weight: 600; padding: 1px 7px; border-radius: 6px; white-space: nowrap; }
.badge.deal { background: var(--deal-soft); color: var(--deal); }
.badge.estimate { background: var(--estimate-soft); color: var(--estimate); }
.badge.member { background: var(--member-soft); color: var(--member); }
.buy-window { font-size: .82rem; color: var(--text); }
.buy-window .expired { color: var(--deal); font-weight: 600; }
.store-group > .buy-window { display: block; margin-top: 6px; }
.ref-price { font-size: .76rem; color: var(--muted); white-space: nowrap; }
.badge.shelf { background: var(--accent-soft); color: var(--accent); }
.badge.pantry { background: var(--accent-soft); color: var(--accent); }

/* Shopping list */
.store-group { margin-bottom: 18px; }
.store-group > header {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 10px 0 8px;
  border-bottom: 2px solid var(--store-color);
}
.store-group > header h3 { display: flex; align-items: center; gap: 8px; }
.store-group > header h3::before { content: ""; width: 12px; height: 12px; border-radius: 50%; background: var(--store-color); }
.rows { list-style: none; margin: 0; padding: 0; }
.row { display: grid; grid-template-columns: auto 44px 1fr auto; gap: 4px 12px; align-items: start; padding: 10px 0; border-bottom: 1px solid var(--border); }
.section-head { font-size: .72rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); padding: 14px 0 2px; }
.row input { width: 20px; height: 20px; margin: 2px 0 0; accent-color: var(--accent); }
.row .name { font-weight: 600; }
.row .sub { color: var(--muted); font-size: .85rem; grid-column: 3 / 4; display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; }
.row .price { text-align: right; font-weight: 600; }
.row .price small { display: block; font-weight: 400; color: var(--muted); font-size: .78rem; }
.row.done .name, .row.done .price { text-decoration: line-through; color: var(--muted); }
.row label { display: contents; cursor: pointer; }
.hint { color: var(--estimate); font-size: .8rem; }

.totals { display: grid; gap: 4px; }
.totals div { display: flex; justify-content: space-between; gap: 12px; }
.totals .grand { font-size: 1.2rem; font-weight: 700; border-top: 1px solid var(--border); padding-top: 8px; margin-top: 4px; }
.list-layout { display: grid; gap: 24px; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); align-items: start; }
.list-layout aside { position: sticky; top: 70px; display: grid; gap: 16px; }
@media (max-width: 820px) { .list-layout { grid-template-columns: 1fr; } .list-layout aside { position: static; order: -1; } }
.picked { display: flex; flex-wrap: wrap; gap: 6px; }
.picked button { font: inherit; font-size: .85rem; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); cursor: pointer; }
.picked button::after { content: " ×"; color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 12px; font-size: .82rem; color: var(--muted); align-items: center; }

/* Nutrition */
.nutri-line { font-size: .82rem; color: var(--muted); }
.nutri { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 8px; text-align: center; }
.nutri div { background: var(--surface-2); border-radius: 10px; padding: 10px 4px; display: grid; }
.nutri b { font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.nutri span { font-size: .75rem; color: var(--muted); }
@media (max-width: 420px) { .nutri { grid-template-columns: repeat(3, minmax(0, 1fr)); } }

/* Product photos: packshots on white, so tiles stay white in dark mode too */
.photos { display: flex; gap: 6px; }
.photo { flex: 1; min-width: 0; height: 104px; background: #fff; border-radius: 10px; border: 1px solid var(--border); overflow: hidden; display: flex; align-items: center; justify-content: center; padding: 6px; }
.photo img { display: block; max-width: 100%; max-height: 100%; object-fit: contain; }
.detail-head .photos { max-width: 560px; }
.detail-head .photo { height: 140px; }
.hl-photo { height: 130px; flex: none; margin-bottom: 4px; }
.thumb { width: 44px; height: 44px; border-radius: 8px; background: #fff; overflow: hidden; display: flex; align-items: center; justify-content: center; padding: 2px; }
.thumb:empty { background: none; }
.thumb img { display: block; max-width: 100%; max-height: 100%; object-fit: contain; }

/* Price history dialog */
.hist-btn { font: inherit; font-size: .78rem; font-weight: 600; color: var(--accent); background: none; border: 0; padding: 0; cursor: pointer; justify-self: start; }
.hist-btn:hover { text-decoration: underline; }
#hist-dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  width: min(420px, calc(100vw - 32px)); padding: 16px; box-shadow: 0 12px 40px rgba(0,0,0,.25); }
#hist-dialog::backdrop { background: rgba(0,0,0,.35); }
#hist-dialog header { display: flex; justify-content: space-between; align-items: start; gap: 8px; margin-bottom: 10px; }
#hist-dialog h2 { font-size: 1.05rem; }
#hist-dialog header .muted { font-size: .82rem; }
.hist-chart { width: 100%; height: auto; display: block; }
.hist-chart .grid { stroke: var(--border); stroke-width: 1; }
.hist-chart .axis { fill: var(--muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.hist-chart .val { fill: var(--text); font-size: 10.5px; font-weight: 600; font-variant-numeric: tabular-nums; }
.hist-chart .bar { fill: var(--chart); }
.hist-chart .none { stroke: var(--muted); stroke-width: 2; stroke-linecap: round; opacity: .5; }
.hist-chart .hit { fill: transparent; }
.hist-chart .slot { cursor: pointer; outline: none; }
.hist-chart:has(.slot.active) .slot:not(.active) .bar { opacity: .45; }
.hist-chart .slot:focus-visible .hit { stroke: var(--accent); stroke-width: 1.5; }
.hist-caption { margin: 8px 0 6px; font-size: .88rem; min-height: 1.3em; }
.hist-table { width: 100%; font-size: .85rem; border-collapse: collapse; margin-top: 6px; }
.hist-table td { padding: 4px 8px 4px 0; border-top: 1px solid var(--border); }
#hist-dialog summary { font-size: .85rem; cursor: pointer; color: var(--accent); }

/* Print: shopping list and recipes on paper */
@media print {
  :root { --bg: #fff; --surface: #fff; --surface-2: #f2f2f2; --text: #000; --muted: #444; --border: #bbb; --shadow: none; color-scheme: light; }
  body { padding: 0; font-size: 11pt; background: #fff; }
  .topbar, .selection-bar, .actions, .back, .picked, .legend, .filters, .btn, .hl-add, .photos, .hist-btn { display: none !important; }
  .thumb { width: 28px; height: 28px; }
  .row { grid-template-columns: auto 28px 1fr auto; }
  main { max-width: none; padding: 0; }
  .panel { box-shadow: none; border: 0; padding: 0; margin-top: 12pt; }
  .detail, .list-layout { display: block; }
  .list-layout aside { position: static; }
  .list-layout aside .panel { border: 1px solid #bbb; padding: 8pt; margin: 0 0 12pt; }
  .store-group, .row, .steps li, .ing li { break-inside: avoid; }
  .row { padding: 4pt 0; }
  .row input { appearance: none; -webkit-appearance: none; width: 11pt; height: 11pt; border: 1.2pt solid #000; border-radius: 2pt; }
  .row.done .name, .row.done .price { text-decoration: none; color: #000; }
  .badge, .tag, .store { border: 1px solid #999; background: none !important; color: #000 !important; }
  .freezer, .tips { background: none; border: 1px solid #bbb; color: #000; }
  a { color: #000; text-decoration: none; }
  h1 { font-size: 18pt; }
}
