:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --accent: #2f6fdb;
  --accent-soft: rgba(47, 111, 219, .12);
  --up: #c2410c;        /* hausse de prix = mauvais */
  --down: #15803d;      /* baisse de prix = bon */
  --promo: #b45309;
  --promo-band: rgba(245, 158, 11, .16);
  --out: #b91c1c;
  --out-band: rgba(220, 38, 38, .12);
  --grid: #e7eaee;
  --mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #161a20;
    --surface-2: #1d222a;
    --border: #2a313b;
    --text: #e6e9ee;
    --muted: #8b96a5;
    --accent: #6ea0ff;
    --accent-soft: rgba(110, 160, 255, .14);
    --up: #fb923c;
    --down: #4ade80;
    --promo: #fbbf24;
    --promo-band: rgba(251, 191, 36, .12);
    --out: #f87171;
    --out-band: rgba(248, 113, 113, .12);
    --grid: #232a33;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--sans);
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.up { color: var(--up); }
.down { color: var(--down); }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; color: var(--text); }
.brand svg { width: 18px; height: 18px; fill: none; stroke: var(--accent); stroke-width: 2; }
.health { font-size: 12px; text-align: right; }

main { max-width: 1280px; margin: 0 auto; padding: 24px; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 15px; margin: 0 0 12px; }
.section { margin-top: 28px; }

/* --- Barre d'outils ----------------------------------------------------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 20px 0 12px; }
.toolbar input, .toolbar select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px;
}
.toolbar input[type=search] { flex: 1 1 220px; }
.chip {
  font: inherit; font-size: 12px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  border-radius: 999px; padding: 5px 11px;
}
.chip[aria-pressed=true] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* --- Cartes produit ----------------------------------------------------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.card {
  display: grid; grid-template-columns: 64px 1fr; gap: 10px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; color: inherit; transition: border-color .15s;
}
.card:hover { border-color: var(--accent); text-decoration: none; }
.thumb {
  width: 64px; height: 64px; border-radius: 8px; object-fit: contain;
  background: #fff; border: 1px solid var(--border);
}
.thumb.placeholder { display: grid; place-items: center; color: var(--muted); font-size: 22px; background: var(--surface-2); }
.card { position: relative; }
/* Le titre couvre toute la carte (lien vers la fiche) ; les liens externes passent au-dessus. */
.card .stretched { color: inherit; }
.card .stretched::after { content: ""; position: absolute; inset: 0; border-radius: var(--radius); }
.card .stretched:hover { text-decoration: none; }
.card-foot { grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: flex-end; gap: 8px; }
.card-foot .ext { position: relative; z-index: 1; font-size: 12px; white-space: nowrap; }
.ext { font-size: 12px; white-space: nowrap; }
.board li .ext { flex: none; }
tr.current td { background: var(--accent-soft); }
.product-row { align-items: center; }
.product-row .rule-main { flex: 1; }
.product-row > .thumb { width: 56px; height: 56px; flex: none; }
.store-checks { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; margin-top: 4px; }
.check { display: inline-flex; align-items: center; gap: 4px; }
.detail-actions { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.search-row { display: flex; gap: 8px; margin: 8px 0; }
.search-row input {
  flex: 1; font: inherit; font-size: 14px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px;
}
.inline-label { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; margin-top: 4px; }
.inline-label input {
  font: inherit; font-size: 13px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 3px 8px; width: 220px;
}
.btn:disabled { opacity: .6; cursor: progress; }
.store-picker { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.store-picker select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 7px; padding: 4px 8px; max-width: 260px;
}
.card .title { font-weight: 600; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .meta { font-size: 12px; color: var(--muted); }
.card .bottom { grid-column: 1 / -1; display: flex; align-items: end; justify-content: space-between; gap: 10px; }
.price { font: 650 20px/1 var(--mono); }
.price small { font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 4px; }
.delta { font: 12px var(--mono); }
.spark { width: 110px; height: 34px; flex: none; }

.badges { display: flex; flex-wrap: wrap; gap: 4px; }
.badge {
  font-size: 11px; line-height: 1; padding: 4px 6px; border-radius: 5px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.badge.promo { color: var(--promo); border-color: currentColor; background: var(--promo-band); }
.badge.out { color: var(--out); border-color: currentColor; background: var(--out-band); }
.badge.low { color: var(--down); border-color: currentColor; }
.badge.score { font-weight: 700; color: #fff; border: 0; }
.score-A { background: #038141 !important; } .score-B { background: #85bb2f !important; }
.score-C { background: #e6a800 !important; } .score-D { background: #ee8100 !important; }
.score-E { background: #e63e11 !important; }

/* --- Classements -------------------------------------------------------- */
.boards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; }
.board { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.board h3 { font-size: 13px; margin: 0 0 2px; }
.board p { font-size: 12px; margin: 0 0 8px; color: var(--muted); }
.board ol { margin: 0; padding: 0; list-style: none; counter-reset: rank; }
.board li { counter-increment: rank; display: flex; gap: 8px; align-items: baseline; padding: 4px 0; border-top: 1px solid var(--grid); }
.board li:first-child { border-top: 0; }
.board li::before { content: counter(rank); font: 11px var(--mono); color: var(--muted); width: 14px; }
.board li a { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.board li .num { flex: none; }

/* --- Détail ------------------------------------------------------------- */
.back { display: inline-block; margin-bottom: 14px; }
.hero { display: grid; grid-template-columns: 160px 1fr; gap: 20px; align-items: start; }
.hero img, .hero .thumb { width: 160px; height: 160px; }
.hero .facts { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 10px; font-size: 13px; }
.hero .facts span b { font-weight: 600; }
.hero .bigprice { font: 700 34px/1 var(--mono); margin-top: 12px; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 12px; }

.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font: 11px var(--mono); }
.chart .gridline { stroke: var(--grid); }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; }
.chart .area { fill: var(--accent-soft); }
.chart .dot { fill: var(--surface); stroke: var(--accent); stroke-width: 1.5; }
.chart .dot.promo { stroke: var(--promo); fill: var(--promo); }
.chart .band-promo { fill: var(--promo-band); }
.chart .band-out { fill: var(--out-band); }
.chart .mean { stroke: var(--muted); stroke-dasharray: 4 4; }
.chart .cursor { stroke: var(--muted); stroke-width: 1; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }

.statgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; }
.stat { background: var(--surface); padding: 9px 11px; border: 1px solid var(--grid); border-radius: 7px; }
.stat .label { font-size: 11px; color: var(--muted); }
.stat .value { font: 600 15px/1.3 var(--mono); }

.drive-panel.has-out { border-color: var(--out); }
.drive-counts { display: flex; flex-wrap: wrap; gap: 8px 18px; margin: 4px 0 2px; font-size: 14px; }
.drive-count b { font: 650 22px/1 var(--mono); margin-right: 4px; }
.drive-count.ok b { color: var(--down); }
.drive-count.out b { color: var(--out); }
.drive-count.warn b { color: var(--promo); }
.drive-list { margin: 10px 0 0; padding-left: 18px; }
.drive-list li { padding: 2px 0; }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.small { font-size: 12px; }

/* --- Navigation ---------------------------------------------------------- */
.nav { display: flex; gap: 4px; margin-right: auto; margin-left: 12px; }
.nav a { color: var(--muted); padding: 5px 10px; border-radius: 7px; }
.nav a:hover { text-decoration: none; color: var(--text); background: var(--surface-2); }
.nav a[aria-current=true] { color: var(--accent); background: var(--accent-soft); }

/* --- Comptes ------------------------------------------------------------- */
.account { display: flex; align-items: center; gap: 8px; font-size: 13px; white-space: nowrap; }
.logged-out .nav, .logged-out .store-picker, .logged-out .health { display: none; }
.login { max-width: 380px; margin: 8vh auto 0; }
.login h1 { margin-bottom: 8px; }

/* --- Boutons & formulaires ---------------------------------------------- */
.btn {
  font: inherit; font-size: 13px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 7px; padding: 6px 11px;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--out); }
.btn.danger:hover { border-color: var(--out); }
.rule-form { margin-top: 16px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px 16px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.form-grid label.wide { grid-column: span 2; }
.form-grid label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 13px; }
.form-grid label[hidden] { display: none; }
.form-grid input:not([type=checkbox]), .form-grid select {
  font: inherit; font-size: 14px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; width: 100%;
}
.with-unit { display: flex; align-items: center; gap: 8px; }
.form-actions { display: flex; align-items: center; gap: 12px; margin-top: 14px; }
.form-msg { margin: 0; font-size: 13px; }

/* --- Règles -------------------------------------------------------------- */
.rules { display: grid; gap: 8px; }
.rule {
  display: flex; justify-content: space-between; gap: 16px; align-items: flex-start;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px;
}
.rule.disabled > :not(.rule-actions) { opacity: .55; }
.rule-main { display: grid; gap: 3px; min-width: 0; }
.rule-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; flex: none; }
.rule-out { font-size: 13px; }
.rule-out ul { margin: 4px 0 0; padding-left: 18px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--grid); }
th { font-weight: 600; color: var(--muted); font-size: 12px; }
td.num, th.num { text-align: right; }
.scroll { max-height: 360px; overflow: auto; }

.tooltip {
  position: fixed; z-index: 10; pointer-events: none;
  background: var(--text); color: var(--bg); font: 12px/1.4 var(--mono);
  padding: 6px 8px; border-radius: 6px; white-space: nowrap;
}
.error { color: var(--out); }
.empty { padding: 40px; text-align: center; color: var(--muted); border: 1px dashed var(--border); border-radius: var(--radius); }

@media (max-width: 640px) {
  main { padding: 16px; }
  .topbar { padding: 10px 16px; }
  .hero { grid-template-columns: 1fr; }
  .hero img, .hero .thumb { width: 120px; height: 120px; }
  .health { display: none; }
  .form-grid label.wide { grid-column: auto; }
  .rule { flex-direction: column; }
  .rule-actions { justify-content: flex-start; }
}
