:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f0f0f3;
  --border: #e3e3e7;
  --text: #16181d;
  --text-dim: #6a6f78;
  --accent: #1f6feb;
  --accent-fg: #ffffff;
  --good: #1a7f37;
  --bad: #d1242f;
  --warn: #b54708;
  --shadow: 0 1px 2px rgba(20, 22, 28, 0.04), 0 4px 12px rgba(20, 22, 28, 0.06);
  --radius: 14px;
  --radius-sm: 8px;
  --maxw: 1240px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1017;
    --surface: #161a22;
    --surface-2: #1d212b;
    --border: #2a2f3a;
    --text: #e6e8ec;
    --text-dim: #9aa0aa;
    --accent: #4493f8;
    --good: #3fb950;
    --bad: #ff6b6b;
    --warn: #fdba74;
    --shadow: 0 1px 2px rgba(0,0,0,0.4), 0 4px 18px rgba(0,0,0,0.5);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

/* `[hidden]` must beat .modal/.stats/.compare-bar display rules below */
[hidden] { display: none !important; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  line-height: 1.45;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* ---------- Top bar ---------- */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: saturate(180%) blur(8px);
}
.topbar__inner {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 16px 20px 8px;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  flex-wrap: wrap;
}
.topbar h1 { margin: 0; font-size: 22px; letter-spacing: -0.01em; font-weight: 700; }
.topbar__sub { margin: 2px 0 0; color: var(--text-dim); font-size: 13px; }
.topbar__meta {
  display: flex; align-items: center; gap: 14px;
  font-size: 13px; color: var(--text-dim);
}
.updated::before { content: "Updated "; }
.repo-link { color: var(--text-dim); text-decoration: none; }
.repo-link:hover { color: var(--text); }

/* ---------- Stats strip ---------- */
.stats {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0 20px 8px;
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  font-size: 13px;
  color: var(--text-dim);
}
.stat b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.stat--cheapest b { color: var(--good); }

/* ---------- Controls ---------- */
.controls {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 6px 20px 14px;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
}
.controls__right {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
}
.filters { display: flex; gap: 8px; flex-wrap: wrap; }

.chip {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.chip:hover { border-color: var(--text-dim); }
.chip.is-active {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
}
.chip--ghost { background: transparent; }
.chip[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}

.search input, .select select {
  width: auto;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  outline: none;
  font-family: inherit;
}
.search input { width: 220px; max-width: 100%; }
.search input:focus, .select select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

main {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 18px 20px 100px;
}

/* ---------- Grid + Cards ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 18px;
}

.card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
  transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease;
  cursor: pointer;
  outline: none;
}
.card:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 4px rgba(20,22,28,0.06), 0 12px 28px rgba(20,22,28,0.08);
}
.card:focus-visible { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.card.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 35%, transparent), var(--shadow);
}

.card__select {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  display: none;
  background: rgba(0,0,0,0.45);
  padding: 4px 6px;
  border-radius: 6px;
}
body.compare-mode .card__select { display: flex; }
.card__check { width: 16px; height: 16px; cursor: pointer; accent-color: var(--accent); }

.card__imgwrap {
  display: block;
  aspect-ratio: 4 / 3;
  background: var(--surface-2);
  position: relative;
  overflow: hidden;
}
.card__img {
  width: 100%; height: 100%;
  object-fit: contain;
  padding: 8px;
  transition: transform .25s ease;
}
.card:hover .card__img { transform: scale(1.03); }
.card__img[data-missing="1"] { opacity: 0; }
.card__imgwrap[data-missing="1"]::after {
  content: "no image";
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: var(--text-dim);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.card__cat {
  position: absolute;
  top: 8px; left: 8px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  background: rgba(0,0,0,0.6);
  color: #fff;
  padding: 3px 7px;
  border-radius: 999px;
}

.card__body {
  padding: 12px 14px 14px;
  display: flex; flex-direction: column; gap: 8px; flex: 1;
}

.card__title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.005em;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card__meta { margin: 0; font-size: 12px; color: var(--text-dim); }

.offers {
  list-style: none; padding: 0; margin: 4px 0 0;
  display: flex; flex-direction: column; gap: 4px;
}

.offer a {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 10px;
  text-decoration: none;
  color: var(--text);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  transition: border-color .15s, background .15s;
}
.offer a:hover { border-color: var(--border); }

.offer--best a {
  background: color-mix(in srgb, var(--good) 12%, var(--surface-2));
  border-color: color-mix(in srgb, var(--good) 35%, transparent);
}
.offer__retailer {
  font-size: 12px; font-weight: 500; color: var(--text-dim);
  display: inline-flex; align-items: center; gap: 6px;
}
.offer--best .offer__retailer::before {
  content: "BEST";
  font-size: 9px; letter-spacing: 0.08em;
  background: var(--good); color: #fff;
  padding: 2px 5px; border-radius: 4px;
}
.offer__price { font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.offer--oos .offer__price { text-decoration: line-through; color: var(--text-dim); }
.offer--oos .offer__retailer::after {
  content: "out of stock";
  color: var(--bad);
  font-size: 10px; text-transform: uppercase;
  letter-spacing: 0.05em; margin-left: 4px;
}

.empty { text-align: center; color: var(--text-dim); padding: 60px 20px; font-size: 14px; }

.setup {
  max-width: 680px;
  margin: 40px auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  box-shadow: var(--shadow);
}
.setup h2 { margin-top: 0; font-size: 18px; }
.setup code {
  background: var(--surface-2);
  padding: 1px 6px; border-radius: 4px; font-size: 12.5px;
}

/* ---------- Compare floating bar ---------- */
.compare-bar {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 8px 8px 18px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.18);
  z-index: 50;
  font-size: 13px;
}
.compare-bar b { font-weight: 700; font-variant-numeric: tabular-nums; }

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}
.btn:hover { border-color: var(--text-dim); }
.btn--primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
.btn--primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
}
.modal__backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(2px);
}
.modal__panel {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  width: min(960px, 96vw);
  max-height: 92vh;
  overflow: auto;
  padding: 18px 22px 28px;
  box-shadow: 0 24px 60px rgba(0,0,0,0.4);
  animation: modalIn .18s ease-out;
}
@keyframes modalIn {
  from { transform: translateY(8px) scale(0.99); opacity: 0; }
  to { transform: none; opacity: 1; }
}
.modal__close {
  position: absolute;
  top: 10px;
  right: 14px;
  background: transparent;
  border: none;
  font-size: 28px;
  line-height: 1;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px 10px;
  border-radius: 999px;
}
.modal__close:hover { color: var(--text); background: var(--surface-2); }

/* ---------- Detail view in modal ---------- */
.detail {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr);
  gap: 22px;
}
.detail__img {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  aspect-ratio: 1 / 1;
  display: grid; place-items: center;
  overflow: hidden;
}
.detail__img img {
  max-width: 100%; max-height: 100%;
  object-fit: contain;
  padding: 16px;
}
.detail__title { margin: 0 0 4px; font-size: 22px; letter-spacing: -0.01em; }
.detail__meta { color: var(--text-dim); font-size: 13px; margin: 0 0 14px; }

.kpis {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin: 0 0 16px;
}
.kpi {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.kpi__label {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--text-dim);
}
.kpi__value {
  font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums;
  margin-top: 2px;
}
.kpi__sub { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.kpi__value--good { color: var(--good); }

.retailer-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-size: 13.5px;
}
.retailer-table th, .retailer-table td {
  text-align: left;
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
}
.retailer-table th {
  font-weight: 600;
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.retailer-table td.num { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.retailer-table tr.is-best td { background: color-mix(in srgb, var(--good) 8%, transparent); }
.retailer-table tr.is-best td.num { color: var(--good); }
.retailer-table tr.is-oos td.num { color: var(--text-dim); text-decoration: line-through; }
.retailer-table a { color: var(--accent); text-decoration: none; }
.retailer-table a:hover { text-decoration: underline; }

.chart {
  margin-top: 18px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 14px;
}
.chart h4 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); }
.chart__svg { width: 100%; height: 110px; display: block; }
.chart__empty { color: var(--text-dim); font-size: 12.5px; text-align: center; padding: 14px 0; }

/* Comparison view (multi-product) */
.cmp { overflow-x: auto; }
.cmp-table { border-collapse: collapse; width: 100%; min-width: 540px; }
.cmp-table th, .cmp-table td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  text-align: left;
  font-size: 13.5px;
}
.cmp-table th.cmp-axis {
  width: 130px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}
.cmp-table .cmp-img {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  aspect-ratio: 1 / 1;
  display: grid; place-items: center;
  overflow: hidden;
  max-width: 180px;
}
.cmp-table .cmp-img img { max-width: 100%; max-height: 100%; padding: 10px; object-fit: contain; }
.cmp-table .cmp-name { font-weight: 600; font-size: 15px; }
.cmp-table .cmp-price-best { color: var(--good); font-weight: 700; font-variant-numeric: tabular-nums; font-size: 18px; }
.cmp-table .cmp-retailers { font-size: 12.5px; color: var(--text-dim); }

.footer {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 30px 20px 50px;
  color: var(--text-dim);
  font-size: 12px;
  text-align: center;
}

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  .detail { grid-template-columns: 1fr; }
  .detail__img { max-width: 100%; aspect-ratio: 4/3; }
  .modal__panel { padding: 14px 16px 20px; border-radius: 16px 16px 0 0; width: 100vw; max-height: 95vh; position: fixed; bottom: 0; }
}
@media (max-width: 540px) {
  .topbar__inner { padding: 14px 16px 8px; }
  .controls { padding: 4px 16px 12px; }
  main { padding: 14px 16px 90px; }
  .grid { gap: 14px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .card__title { font-size: 14px; }
  .offer__price { font-size: 13px; }
  .search input { width: 130px; }
  .controls__right { width: 100%; justify-content: space-between; }
}
