:root {
  --bg: #f3f4f6; --surface: #ffffff; --text: #14171a; --muted: #667085; --line: #e1e4e9;
  --accent: #1c8a4b; --danger: #d92d20;
  --wm: #0053e2; --tgt: #cc0000; --aldi: #ffc20e; --aldi-ink: #1b1b1b;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0e1013; --surface: #1a1d22; --text: #eceef1; --muted: #98a2b3; --line: #2b3038;
  --accent: #2fae66; --danger: #f97066;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #0e1013; --surface: #1a1d22; --text: #eceef1; --muted: #98a2b3; --line: #2b3038;
    --accent: #2fae66; --danger: #f97066;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  overscroll-behavior-y: contain;
}
button, input, select { font: inherit; color: inherit; touch-action: manipulation; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- header ---------- */
#header {
  position: sticky; top: 0; z-index: 5; background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: calc(env(safe-area-inset-top) + 8px) calc(env(safe-area-inset-right) + 10px) 8px calc(env(safe-area-inset-left) + 10px);
  transition: background-color .15s;
}
#header[data-store="walmart"] { background: var(--wm); color: #fff; border-color: transparent; }
#header[data-store="target"]  { background: var(--tgt); color: #fff; border-color: transparent; }
#header[data-store="aldi"]    { background: var(--aldi); color: var(--aldi-ink); border-color: transparent; }
.bar { display: flex; align-items: center; gap: 8px; }
.bar + .bar, .bar + form, form + .suggest { margin-top: 8px; }
.title { font-size: 20px; font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lbl { font-size: 13px; font-weight: 700; white-space: nowrap; }

/* ---------- controls ---------- */
.btn {
  min-height: 44px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font-weight: 600; flex: none;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.wide { display: block; width: 100%; }
.btn:disabled { opacity: .45; }
.btn:active:not(:disabled), .qty button:active, .seg button:active { filter: brightness(.9); }

input[type="text"], input[type="search"], select {
  min-height: 44px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); padding: 0 12px; font-size: 16px;
  min-width: 0; flex: 1; -webkit-appearance: none; appearance: none;
}
select {
  font-weight: 700; padding-right: 30px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 17px) 55%, calc(100% - 11px) 55%;
  background-size: 6px 6px; background-repeat: no-repeat;
}

.seg { display: flex; flex: 1; gap: 2px; padding: 3px; border-radius: 10px; background: var(--bg); min-width: 0; }
.seg button {
  flex: 1; min-height: 38px; border: 0; border-radius: 8px; background: none; color: var(--text);
  font-size: 14px; font-weight: 600; padding: 0 2px; white-space: nowrap;
}
.seg button.on { background: var(--surface); box-shadow: 0 1px 3px rgba(0, 0, 0, .25); }
.seg button.on[data-store="walmart"] { background: var(--wm); color: #fff; }
.seg button.on[data-store="target"]  { background: var(--tgt); color: #fff; }
.seg button.on[data-store="aldi"]    { background: var(--aldi); color: var(--aldi-ink); }

.qty { display: flex; align-items: center; flex: none; }
.qty button {
  width: 42px; height: 44px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 10px; font-size: 22px; font-weight: 600; line-height: 1; padding: 0;
}
.qty b { min-width: 30px; text-align: center; font-variant-numeric: tabular-nums; }
.qty.big button { width: 52px; height: 52px; font-size: 28px; }
.qty.big b { min-width: 40px; font-size: 20px; }

/* ---------- autocomplete ---------- */
.suggest { border-radius: 10px; overflow: hidden; border: 1px solid var(--line); background: var(--surface); color: var(--text); }
.suggest button {
  display: flex; justify-content: space-between; gap: 10px; width: 100%; min-height: 44px; padding: 0 12px;
  border: 0; border-bottom: 1px solid var(--line); background: none; text-align: left; align-items: center;
}
.suggest button:last-child { border-bottom: 0; }
.suggest small { color: var(--muted); white-space: nowrap; }

/* ---------- rows ---------- */
.row {
  display: flex; align-items: center; gap: 6px; background: var(--surface);
  border-bottom: 1px solid var(--line); padding: 4px 10px 4px 4px; min-height: 62px;
}
.row.inv { padding-left: 10px; }
.mid { flex: 1; min-width: 0; }
.name {
  display: block; width: 100%; min-height: 44px; padding: 6px 4px; border: 0; background: none;
  text-align: left; color: inherit;
}
.nm { display: block; font-size: 17px; font-weight: 600; overflow-wrap: anywhere; }
.sub { display: block; font-size: 13px; color: var(--muted); }
.row.checked .nm { text-decoration: line-through; color: var(--muted); }
.row.checked .badge { opacity: .5; }
.row.zero .nm, .row.zero .qty b { color: var(--muted); }

.chk { flex: none; width: 52px; height: 52px; display: grid; place-items: center; }
.chk input {
  -webkit-appearance: none; appearance: none; margin: 0; width: 30px; height: 30px;
  border: 2px solid var(--muted); border-radius: 8px; display: grid; place-items: center; background: var(--surface);
}
.chk input:checked { background: var(--accent); border-color: var(--accent); }
.chk input:checked::after {
  content: ""; width: 8px; height: 15px; border: solid #fff; border-width: 0 3px 3px 0;
  transform: translateY(-2px) rotate(45deg);
}

.badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 3px; }
.badge { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; color: #fff; white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.b-walmart { background: var(--wm); }
.b-target { background: var(--tgt); }
.b-aldi { background: var(--aldi); color: var(--aldi-ink); }

input.aisle { flex: none; width: 100%; max-width: 190px; min-height: 40px; margin: 0 4px 6px; font-size: 16px; }

.section { padding: 16px 14px 6px; font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 36px 24px; margin: 0; }
.foot { padding: 16px 12px; }
.addrow {
  display: block; width: 100%; min-height: 56px; border: 0; border-bottom: 1px solid var(--line);
  background: var(--surface); color: var(--accent); font-weight: 700; text-align: left; padding: 0 14px;
}

/* ---------- cards (item detail, settings) ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; margin: 12px; padding: 14px; }
.card h2 { margin: 0 0 10px; font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.card .bar + .bar { margin-top: 10px; }
.card .btn.wide + .btn.wide { margin-top: 10px; }
.hint { color: var(--muted); font-size: 14px; margin: 10px 0 0; }
.grow { flex: 1; min-width: 0; font-weight: 600; overflow-wrap: anywhere; }
.storetag { flex: none; width: 86px; text-align: center; font-weight: 700; font-size: 14px; padding: 12px 0; border-radius: 10px; color: #fff; }
.backup { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-top: 1px solid var(--line); }
.backup:first-of-type { margin-top: 12px; }
.backup .grow small { display: block; color: var(--muted); font-weight: 400; }
.backup a.btn { display: inline-flex; align-items: center; text-decoration: none; }
input[type="file"] { width: 100%; min-height: 44px; font-size: 16px; }

/* ---------- bottom nav ---------- */
#nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; display: flex;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#nav a {
  flex: 1; height: 58px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 600;
}
#nav a[aria-current="page"] { color: var(--accent); }
#nav svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- toast + dialog ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(76px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: #22262c; color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 600;
  max-width: calc(100vw - 32px); text-align: center; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
#toast.err { background: #b42318; }

dialog {
  border: 0; border-radius: 16px; padding: 0; width: min(92vw, 380px);
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog form { padding: 18px; }
dialog h3 { margin: 0 0 8px; font-size: 18px; }
dialog p { margin: 0 0 12px; color: var(--muted); }
dialog input[type="text"] { width: 100%; margin-bottom: 12px; }
dialog .opts .btn { margin-bottom: 8px; }
.dlg-btns { display: flex; gap: 8px; margin-top: 4px; }
.dlg-btns .btn { flex: 1; }
