:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --ink: #17201c;
  --muted: #64716b;
  --line: #e2e7e4;
  --brand: #0b8a5a;
  --brand-ink: #ffffff;
  --brand-soft: #e3f4ec;
  --offer: #d93a2b;
  --best: #f2b705;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 14px rgb(0 0 0 / 5%);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1412;
    --card: #182019;
    --ink: #e8efeb;
    --muted: #94a39b;
    --line: #26312b;
    --brand: #22b07a;
    --brand-soft: #173326;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}
button { font: inherit; color: inherit; cursor: pointer; }

/* Cabecera */
.top {
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--brand); color: var(--brand-ink);
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 19px; margin-right: auto; letter-spacing: -.2px; }
.brand img { border-radius: 8px; background: #fff; }
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  border: 0; border-radius: 99px; padding: 6px 11px;
  background: rgb(255 255 255 / 18%); color: inherit; font-weight: 600; font-size: 14px;
}
.pill:active { background: rgb(255 255 255 / 30%); }

/* Buscador */
.searchbar { background: var(--brand); padding: 0 16px 14px; position: sticky; top: 0; z-index: 5; }
#form {
  display: flex; align-items: center; gap: 4px;
  background: var(--card); border-radius: 12px; padding: 0 6px 0 12px;
  box-shadow: 0 2px 10px rgb(0 0 0 / 15%);
}
.lupa { color: var(--muted); flex: none; }
#q {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--ink);
  font-size: 16px; padding: 13px 6px;
}
#q::-webkit-search-cancel-button { display: none; }
.icon { border: 0; background: transparent; padding: 8px; border-radius: 8px; color: var(--muted); line-height: 0; font-size: 16px; }
.icon.scan { color: var(--brand); }
.suggest { display: flex; gap: 6px; overflow-x: auto; padding-top: 10px; color: rgb(255 255 255 / 85%); font-size: 13px; align-items: center; scrollbar-width: none; }
.suggest::-webkit-scrollbar { display: none; }
.suggest button { flex: none; border: 1px solid rgb(255 255 255 / 40%); background: transparent; color: #fff; border-radius: 99px; padding: 4px 10px; font-size: 13px; }
.suggest span { flex: none; }
body.searching .suggest { display: none; }

/* Chips de supermercados */
.stores { display: flex; gap: 8px; overflow-x: auto; padding: 12px 16px 4px; scrollbar-width: none; }
.stores::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: var(--card); border-radius: 99px;
  padding: 6px 12px 6px 9px; font-size: 13.5px; font-weight: 600;
}
.chip .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c); }
.chip .st { color: var(--muted); font-weight: 500; font-size: 12.5px; min-width: 14px; text-align: center; }
.chip.off { opacity: .45; }
.chip.off .dot { background: transparent; border: 2px solid var(--c); }
.chip.err .st { color: var(--offer); }
.spin { width: 12px; height: 12px; border: 2px solid var(--line); border-top-color: var(--c); border-radius: 50%; display: inline-block; animation: spin .7s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }

main { padding: 6px 16px 40px; max-width: 980px; margin: 0 auto; }

/* Ranking */
.ranking { background: var(--card); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); margin: 8px 0 14px; }
.ranking h2 { margin: 0; font-size: 16px; }
.hint { margin: 2px 0 10px; color: var(--muted); font-size: 13px; }
.ranking ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 7px; }
.rank { display: grid; grid-template-columns: 22px 92px 1fr auto; align-items: center; gap: 8px; font-size: 14px; }
.rank .pos { color: var(--muted); font-weight: 700; text-align: center; }
.rank:first-child .pos { color: var(--best); }
.rank .bar { height: 9px; border-radius: 9px; background: var(--line); overflow: hidden; }
.rank .bar i { display: block; height: 100%; background: var(--c); border-radius: 9px; }
.rank b { font-variant-numeric: tabular-nums; }
.rank .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }

/* Barra de orden */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 4px 0 10px; flex-wrap: wrap; }
#count { color: var(--muted); font-size: 13px; }
.sort { display: inline-flex; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.sort button { border: 0; background: transparent; padding: 5px 11px; border-radius: 7px; font-size: 13px; font-weight: 600; color: var(--muted); }
.sort button.on { background: var(--brand); color: #fff; }

/* Resultados */
.results { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
.item {
  position: relative; display: grid; grid-template-columns: 74px 1fr auto; gap: 12px; align-items: center;
  background: var(--card); border-radius: var(--radius); padding: 10px 12px 10px 10px; box-shadow: var(--shadow);
  border-left: 4px solid var(--c);
}
.item.new { animation: pop .25s ease-out both; }
@keyframes pop { from { opacity: 0; transform: translateY(4px); } }
.item.weak { opacity: .62; }
.item .img { width: 74px; height: 74px; border-radius: 10px; background: #fff; display: grid; place-items: center; overflow: hidden; }
.item .img img { max-width: 100%; max-height: 100%; object-fit: contain; }
.item .info { min-width: 0; }
.item .store { font-size: 11.5px; font-weight: 700; color: var(--c); text-transform: uppercase; letter-spacing: .3px; }
.item .name {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  font-weight: 600; font-size: 14.5px; line-height: 1.25; margin: 1px 0 2px; color: inherit; text-decoration: none;
}
.item .meta { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .prices { text-align: right; display: grid; justify-items: end; gap: 2px; }
.item .price { font-size: 19px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -.3px; }
.item .old { font-size: 12px; color: var(--muted); text-decoration: line-through; }
.item .unit { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.item .unit.est::after { content: " ≈"; }
.item .badge { position: absolute; top: -7px; left: 12px; font-size: 10.5px; font-weight: 800; padding: 2px 7px; border-radius: 99px; color: #fff; }
.item .badge.best { background: var(--best); color: #3a2c00; }
.item .badge.offer { background: var(--offer); }
.item .add {
  border: 1.5px solid var(--brand); background: transparent; color: var(--brand);
  width: 30px; height: 30px; border-radius: 50%; font-size: 19px; line-height: 1; font-weight: 700; margin-top: 4px;
}
.item .add.in { background: var(--brand); color: #fff; }

.more { display: block; margin: 14px auto 0; border: 1px solid var(--line); background: var(--card); padding: 9px 16px; border-radius: 10px; color: var(--muted); }

.empty { text-align: center; padding: 40px 10px; color: var(--muted); }
.empty h1 { font-size: 20px; color: var(--ink); margin: 10px 0 6px; }
.empty-art { font-size: 54px; }
.barcode-info { background: var(--brand-soft); border-radius: var(--radius); padding: 10px 14px; margin: 8px 0; font-size: 14px; display: flex; gap: 10px; align-items: center; }
.barcode-info img { width: 44px; height: 44px; object-fit: contain; background: #fff; border-radius: 8px; }

/* Hojas (dialog) */
dialog { border: 0; padding: 0; color: var(--ink); background: var(--card); }
dialog::backdrop { background: rgb(0 0 0 / 45%); }
.sheet { width: min(560px, 100vw - 24px); max-height: 86vh; border-radius: 18px; box-shadow: 0 10px 40px rgb(0 0 0 / 30%); }
.sheet header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; position: sticky; top: 0; background: var(--card); }
.sheet h2 { margin: 0; font-size: 18px; }
.sheet footer { padding: 10px 16px 16px; text-align: right; }
.sheet.small form { padding-bottom: 16px; }
.sheet.small .hint, .sheet.small input, .sheet.small .primary { margin-left: 16px; margin-right: 16px; }
.sheet.small input { display: block; width: calc(100% - 32px); font-size: 22px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--ink); letter-spacing: 3px; margin-bottom: 12px; }
.primary { border: 0; background: var(--brand); color: #fff; font-weight: 700; border-radius: 10px; padding: 11px 18px; }
.sheet.small .primary { width: calc(100% - 32px); }
.ghost { border: 1px solid var(--line); background: transparent; border-radius: 10px; padding: 8px 14px; color: var(--muted); }

#listBody { padding: 0 16px; }
.lgroup { margin: 10px 0 14px; }
.lgroup h3 { display: flex; justify-content: space-between; margin: 0 0 6px; font-size: 14px; color: var(--c); text-transform: uppercase; letter-spacing: .3px; }
.lrow { display: grid; grid-template-columns: 1fr auto auto; gap: 10px; align-items: center; padding: 7px 0; border-top: 1px solid var(--line); font-size: 14px; }
.lrow a { color: inherit; text-decoration: none; }
.lrow .icon { padding: 4px; }
.ltotal { display: flex; justify-content: space-between; font-weight: 800; font-size: 17px; padding: 12px 0 4px; border-top: 2px solid var(--ink); }

.scanner { width: 100vw; height: 100vh; max-width: none; max-height: none; background: #000; color: #fff; text-align: center; }
.scanner video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scanner .frame { position: absolute; left: 10%; right: 10%; top: 38%; height: 22%; border: 3px solid #fff; border-radius: 16px; box-shadow: 0 0 0 100vmax rgb(0 0 0 / 45%); }
.scanner p { position: absolute; left: 0; right: 0; top: 28%; font-weight: 700; font-size: 17px; margin: 0; }
.scanner .primary { position: absolute; left: 50%; bottom: 8%; transform: translateX(-50%); }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: #17201c; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%); }

@media (max-width: 420px) {
  .results { grid-template-columns: 1fr; }
  .item { grid-template-columns: 62px 1fr auto; gap: 10px; }
  .item .img { width: 62px; height: 62px; }
  .rank { grid-template-columns: 20px 78px 1fr auto; }
}

/* ---------- Lista de la compra ---------- */
.sheet.big { width: min(640px, 100vw - 16px); max-height: 92vh; overflow-y: auto; }
.sheet.big > section { padding: 0 16px 16px; }
.addrow { display: flex; gap: 6px; align-items: center; margin-top: 6px; }
.addrow input { flex: 1; min-width: 0; font-size: 16px; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--ink); }
.primary.sm, .ghost.sm { padding: 8px 12px; font-size: 14px; }
.mic { border: 0; width: 44px; height: 44px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; flex: none; }
.mic.on { background: var(--offer); color: #fff; animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 8px rgb(217 58 43 / 20%); } }
.litems { list-style: none; margin: 4px 0 12px; padding: 0; }
.litems li { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
.litems li.lempty { display: block; color: var(--muted); font-size: 14px; padding: 14px 0; }
.qty { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 99px; }
.qty button { border: 0; background: transparent; width: 30px; height: 30px; font-size: 17px; font-weight: 700; color: var(--brand); }
.qty span { min-width: 18px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.lq { font-weight: 600; min-width: 0; }
.lq::first-letter, .sinfo small::first-letter { text-transform: uppercase; }
.lq small { display: block; text-transform: none; font-weight: 400; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lactions { display: flex; justify-content: space-between; gap: 8px; align-items: center; flex-wrap: wrap; }
.lactions .primary { flex: 1; text-align: center; text-decoration: none; }
.primary:disabled { opacity: .45; }
.back { border: 0; background: transparent; color: var(--brand); font-weight: 700; padding: 4px 0 10px; }

.cprog { background: var(--brand-soft); border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; font-weight: 600; }
.cprog small { display: block; font-weight: 400; color: var(--muted); margin-top: 4px; font-size: 12.5px; }
.bestcard { display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; border-left: 5px solid var(--c); background: var(--brand-soft); border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; }
.bestcard b { display: block; font-size: 19px; margin: 2px 0; }
.bestcard small { color: var(--muted); font-size: 12.5px; }
.crank { list-style: none; padding: 0; margin: 0 0 8px; }
.crow { display: grid; grid-template-columns: 22px 1fr auto auto; gap: 10px; align-items: center; padding: 10px 0 10px 10px; border-top: 1px solid var(--line); border-left: 4px solid var(--c); }
.crow .pos { color: var(--muted); font-weight: 800; text-align: center; }
.crow:first-child .pos { color: var(--best); }
.cinfo small { display: block; color: var(--muted); font-size: 12.5px; }
.ctotal { font-weight: 800; font-size: 17px; font-variant-numeric: tabular-nums; }
.cfail { color: var(--muted); font-size: 12.5px; padding: 10px 0; border-top: 1px solid var(--line); }

.steps { margin: 0 0 12px; padding-left: 20px; font-size: 14px; color: var(--muted); }
.steps li { margin: 3px 0; }
.sitems { list-style: none; padding: 0; margin: 0 0 12px; }
.srow { display: grid; grid-template-columns: 22px 1fr auto; gap: 10px; align-items: center; padding: 9px 0; border-top: 1px solid var(--line); }
.srow input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--brand); }
.srow.done .sinfo { opacity: .5; text-decoration: line-through; }
.srow.miss { color: var(--muted); }
.srow.miss small, .sinfo small { display: block; color: var(--muted); font-size: 12px; }
.sinfo { min-width: 0; }
.sinfo b { font-size: 14px; }
.sinfo select { width: 100%; max-width: 100%; font: inherit; font-size: 14px; font-weight: 600; padding: 4px 6px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
.sprice { text-align: right; display: grid; gap: 2px; justify-items: end; }
.sprice b { font-variant-numeric: tabular-nums; }
.openlink { font-size: 13px; font-weight: 700; color: var(--brand); text-decoration: none; white-space: nowrap; }

.foot { max-width: 980px; margin: 0 auto; padding: 8px 16px calc(24px + env(safe-area-inset-bottom)); color: var(--muted); font-size: 12px; text-align: center; }
.foot a { color: inherit; }

/* Página legal */
.legal { max-width: 760px; line-height: 1.6; padding-bottom: 50px; }
.legal h1 { font-size: 24px; margin: 18px 0 2px; }
.legal h2 { font-size: 19px; margin: 28px 0 6px; padding-top: 6px; border-top: 1px solid var(--line); }
.legal h3 { font-size: 15.5px; margin: 16px 0 4px; }
.legal ul { padding-left: 20px; }
.legal li { margin: 4px 0; }
.legal a { color: var(--brand); }
.legal .toc { font-size: 14px; margin: 8px 0 4px; }
