:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --text: #1c1b19;
  --muted: #6b675f;
  --line: #e2ded5;
  --accent: #1f6f4a;
  --accent-ink: #ffffff;
  --walmart: #0071dc;
  --walmart-soft: #e6f1fc;
  --jewel: #c8102e;
  --jewel-soft: #fbe9ec;
  --good: #1f7a46;
  --good-soft: #e3f3e9;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 14px rgb(0 0 0 / 5%);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161513;
    --surface: #211f1c;
    --surface-2: #2a2824;
    --text: #f1eee8;
    --muted: #a8a399;
    --line: #36332e;
    --accent: #3fa874;
    --accent-ink: #0d1a12;
    --walmart: #5aa9f2;
    --walmart-soft: #15283b;
    --jewel: #f0687c;
    --jewel-soft: #3a1a20;
    --good: #59c286;
    --good-soft: #17301f;
    --warn: #e8a94a;
    --warn-soft: #33270f;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(110px + env(safe-area-inset-bottom));
}
button, input, textarea { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.6rem; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  position: sticky; top: 0; z-index: 5;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand {
  background: none; border: 0; padding: 0; cursor: pointer;
  font-weight: 700; font-size: 1.05rem; display: inline-flex; gap: 8px; align-items: center;
}

main { max-width: 640px; margin: 0 auto; padding: 18px 16px; }
.stack > * + * { margin-top: 14px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; }
.spacer { flex: 1; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
}

textarea {
  width: 100%; min-height: 168px; resize: vertical;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; line-height: 1.5;
}
textarea:focus, input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=text], input[inputmode] {
  width: 100%; background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 11px 12px;
}

.btn {
  border: 1px solid transparent; border-radius: 12px; padding: 12px 16px;
  font-weight: 600; cursor: pointer; min-height: 46px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  text-decoration: none;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.walmart { background: var(--walmart); color: #fff; }
.btn.jewel { background: var(--jewel); color: #fff; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px;
  padding: 7px 12px; cursor: pointer; max-width: 100%;
}
.chip .x { border: 0; background: none; color: var(--muted); padding: 0 0 0 4px; cursor: pointer; font-size: 1rem; }
.recent {
  display: block; width: 100%; text-align: left; cursor: pointer;
  background: none; border: 0; border-top: 1px solid var(--line); padding: 10px 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.section-label { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; }

/* Progress */
.steps { list-style: none; padding: 0; margin: 0; }
.steps li { display: flex; gap: 12px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); }
.steps li:last-child { border-bottom: 0; }
.dot { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line); flex: none; display: grid; place-items: center; font-size: 0.75rem; }
.steps .done .dot { background: var(--good); border-color: var(--good); color: #fff; }
.steps .active .dot { border-color: var(--accent); border-top-color: transparent; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); transition: width 0.3s; }

/* Plan */
.summary {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
}
.total { border-radius: var(--radius); padding: 12px 14px; }
.total.walmart { background: var(--walmart-soft); }
.total.jewel { background: var(--jewel-soft); }
.total .store { font-weight: 700; font-size: 0.9rem; }
.total.walmart .store { color: var(--walmart); }
.total.jewel .store { color: var(--jewel); }
.total .amount { font-size: 1.45rem; font-weight: 700; letter-spacing: -0.01em; }

.item { padding: 14px 14px 12px; }
.item-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.item-head h3 { font-size: 1.02rem; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.qty { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 999px; }
.qty button { width: 34px; height: 34px; border: 0; background: none; font-size: 1.15rem; cursor: pointer; }
.qty span { min-width: 22px; text-align: center; font-weight: 600; }

.option {
  display: grid; grid-template-columns: 48px 1fr auto; gap: 10px; align-items: start;
  width: 100%; text-align: left; cursor: pointer;
  background: var(--surface); border: 1.5px solid var(--line); border-radius: 12px;
  padding: 10px 12px 10px 10px; margin-top: 8px;
}
.option.walmart { border-left: 4px solid var(--walmart); }
.option.jewel { border-left: 4px solid var(--jewel); }
.thumb { width: 48px; height: 48px; border-radius: 8px; background: #fff; border: 1px solid var(--line); overflow: hidden; display: block; flex: none; }
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.thumb.big { width: 64px; height: 64px; }
@media (prefers-color-scheme: dark) { .thumb { background: #f4f2ee; } }
.option.selected.walmart { border-color: var(--walmart); background: var(--walmart-soft); }
.option.selected.jewel { border-color: var(--jewel); background: var(--jewel-soft); }
.option.unavailable { cursor: default; opacity: 0.7; }
.option .store-name { font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; }
.option.walmart .store-name { color: var(--walmart); }
.option.jewel .store-name { color: var(--jewel); }
.option .pname { font-size: 0.92rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.option .price { text-align: right; white-space: nowrap; }
.option .price strong { font-size: 1.08rem; display: block; }
.option .price .unit { font-size: 0.78rem; color: var(--muted); }
.option .price s { font-size: 0.78rem; color: var(--muted); }
.check { width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--line); display: inline-grid; place-items: center; font-size: 0.7rem; color: #fff; vertical-align: middle; margin-right: 6px; }
.selected.walmart .check { background: var(--walmart); border-color: var(--walmart); }
.selected.jewel .check { background: var(--jewel); border-color: var(--jewel); }

.badges { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 5px; }
.badge { font-size: 0.72rem; font-weight: 600; padding: 2px 7px; border-radius: 6px; background: var(--surface-2); color: var(--muted); }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }

.item-foot { display: flex; justify-content: flex-end; align-items: center; gap: 14px; margin-top: 10px; }
.store-note { font-size: 0.8rem; color: var(--muted); margin-top: 5px; }
.guess { font-size: 0.82rem; margin: -2px 0 2px; padding: 6px 9px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); }
.linkbtn { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: 0.85rem; white-space: nowrap; }
.tip { font-size: 0.82rem; margin-top: 6px; padding: 6px 9px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); }
.skipped { opacity: 0.6; }

.banner { padding: 10px 12px; border-radius: 12px; font-size: 0.9rem; }
.banner.good { background: var(--good-soft); color: var(--good); }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.error { background: var(--jewel-soft); color: var(--jewel); }

.footer-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.footer-bar .inner { max-width: 640px; margin: 0 auto; }

/* Results */
.result { display: flex; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 0.92rem; }
.result:last-child { border-bottom: 0; }
.result .status { margin-left: auto; text-align: right; white-space: nowrap; font-weight: 600; }
.result .status.ok { color: var(--good); }
.result .status.bad { color: var(--jewel); }
.result .status.manual { color: var(--warn); }

dialog {
  border: 0; border-radius: 18px; padding: 0; width: min(92vw, 420px);
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / 30%);
}
dialog::backdrop { background: rgb(0 0 0 / 40%); }
.dialog-body { padding: 20px; }
.dialog-body h2 { margin-bottom: 8px; }
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; }

/* Favorites */
.star { color: #e0a100; }
.linkbtn.fav { text-decoration: none; color: var(--text); border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; }
.linkbtn.fav.on { color: #a57600; border-color: #e0a100; background: color-mix(in srgb, #e0a100 12%, transparent); }
.fav-row { display: flex; gap: 10px; align-items: center; padding: 9px 2px; border-top: 1px solid var(--line); }
.fav-row .fav-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.fav-row .x { border: 0; background: none; color: var(--muted); font-size: 1.2rem; cursor: pointer; padding: 4px 6px; }
@media (prefers-color-scheme: dark) { .linkbtn.fav.on { color: #f0c24a; } }

/* Tabs */
.tabs { margin-left: auto; display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.tab { border: 0; background: none; border-radius: 999px; padding: 6px 14px; font-weight: 600; font-size: 0.9rem; color: var(--muted); cursor: pointer; }
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }

/* Deals */
.deal { margin-top: 10px; border-left: 5px solid var(--line); }
.deal.walmart { border-left-color: var(--walmart); }
.deal.jewel { border-left-color: var(--jewel); }
.deal .store-name { font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; }
.deal .store-name.walmart { color: var(--walmart); }
.deal .store-name.jewel { color: var(--jewel); }
.deal .pname { font-size: 0.95rem; margin-top: 2px; }
.deal .price { text-align: right; white-space: nowrap; }
.deal .price strong { display: block; font-size: 1.25rem; }
.deal .price s, .deal .price .unit { font-size: 0.78rem; color: var(--muted); }
.deal-compare { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); }
.add-deal { min-height: 38px; padding: 8px 14px; font-size: 0.9rem; flex: none; }
.open-cart { display: block; margin-top: 6px; text-align: right; font-weight: 600; color: var(--text); }
.deal-compare .add-deal + .open-cart { flex-basis: 100%; }
.deal-compare { flex-wrap: wrap; }
.cart-links { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
