:root {
    --bg: #f6f3ee; --panel: #fffdfa; --ink: #241c14; --muted: #7a6c5d; --line: #e4dccf;
    --accent: #8b4a1f; --accent-ink: #fff; --ok: #2e7d4f; --ok-bg: #e3f3e8; --bad: #9a3b2c; --bad-bg: #f8e4df;
    --unk: #8a7f72; --unk-bg: #eee9e1; --hover: #f3ede3;
  }
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --bg: #17130f; --panel: #211b16; --ink: #efe7dc; --muted: #a2947f; --line: #3a3027;
      --accent: #d08a4f; --accent-ink: #1a120a; --ok: #7fd39b; --ok-bg: #1e3a2a; --bad: #f09a86; --bad-bg: #44231c;
      --unk: #b3a693; --unk-bg: #2c251e; --hover: #2a231c;
    }
  }
  * { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; }
  body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; padding-bottom: env(safe-area-inset-bottom); }
  main { max-width: 1400px; margin: 0 auto; padding: 0 12px 70px; }
  .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-top: 12px; }
  form.search { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
  input[type=search], input[type=text], select { font: inherit; font-size: 16px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--ink); min-height: 44px; }
  input[type=search] { flex: 1 1 240px; min-width: 0; }
  button { font: inherit; font-size: 15px; padding: 10px 14px; min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); cursor: pointer; }
  button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; flex: 0 0 auto; }
  button.small { padding: 6px 10px; min-height: 36px; font-size: 13px; }
  button:disabled { opacity: .5; cursor: default; }
  label.chk { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); font-size: 14px; min-height: 36px; }
  label.chk input { width: 18px; height: 18px; }
  .progress { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: 10px; }
  .progress > div { height: 100%; background: var(--accent); width: 0; transition: width .3s; }
  .status { color: var(--muted); font-size: 13px; margin-top: 6px; display: flex; gap: 10px; flex-wrap: wrap; }
  .filters { margin-top: 10px; }
  .frow { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 8px; }
  .frow input[type=text] { flex: 1 1 160px; min-width: 0; }
  .frow.packs .chip { flex: 1 1 0; text-align: center; min-height: 40px; font-size: 14px; }
  @media (min-width: 761px) { .frow.packs .chip { flex: 0 0 auto; padding: 8px 18px; } }
  .chip { padding: 8px 12px; border-radius: 20px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); cursor: pointer; font-size: 13px; user-select: none; font: inherit; }
  .chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
  .sortbar { display: none; gap: 8px; align-items: center; margin-top: 10px; font-size: 13px; color: var(--muted); }
  .sortbar select { flex: 1; min-height: 40px; padding: 6px 10px; }

  /* table (desktop) */
  .tablewrap { overflow-x: auto; margin-top: 10px; -webkit-overflow-scrolling: touch; }
  table { border-collapse: collapse; width: 100%; font-size: 14px; }
  th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
  th { cursor: pointer; user-select: none; white-space: nowrap; color: var(--muted); font-weight: 600; font-size: 13px; position: sticky; top: 0; background: var(--panel); }
  th.sorted::after { content: " ▾"; } th.sorted.asc::after { content: " ▴"; }
  tr:hover td { background: var(--hover); }
  td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
  .badge { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 12px; font-weight: 600; white-space: nowrap; }
  .badge.ok { background: var(--ok-bg); color: var(--ok); } .badge.bad { background: var(--bad-bg); color: var(--bad); } .badge.unk { background: var(--unk-bg); color: var(--unk); }
  .title a { color: inherit; text-decoration: none; } .title a:hover { text-decoration: underline; }
  .retailer { white-space: nowrap; font-weight: 600; }
  .note { color: var(--muted); font-size: 12px; }
  .was { color: var(--muted); text-decoration: line-through; font-size: 12px; margin-left: 4px; }
  .actions { white-space: nowrap; }
  .actions button { margin-right: 4px; }

  /* cards (mobile) */
  .cards { display: none; margin-top: 10px; }
  .card { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; background: var(--bg); }
  .card .top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
  .card .retailer { font-size: 13px; }
  .card .price { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
  .card .per { color: var(--muted); font-size: 13px; font-weight: 500; }
  .card .name { margin: 4px 0 6px; font-size: 15px; line-height: 1.3; }
  .card .name a { color: inherit; text-decoration: none; }
  .card .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 13px; }
  .card .row .pack { font-weight: 600; }
  .card .row .sp { flex: 1; }
  .card .row button { min-height: 36px; }

  details.stores summary { cursor: pointer; color: var(--muted); font-size: 13px; padding: 6px 0; }
  .storelist { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 4px 14px; font-size: 13px; margin-top: 8px; }
  .storelist .err { color: var(--bad); } .storelist .ok { color: var(--ok); }
  h2 { font-size: 17px; margin: 0 0 8px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
  h2 .sp { flex: 1; }
  .events { font-size: 13px; color: var(--muted); max-height: 220px; overflow: auto; }
  .events div { padding: 4px 0; border-bottom: 1px dashed var(--line); }
  .empty { color: var(--muted); padding: 14px 4px; }
  .toast { position: fixed; bottom: calc(18px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: 92vw; text-align: center; }
  .toast.show { opacity: 1; }

  @media (max-width: 760px) {
    .tablewrap { display: none; }
    .cards { display: block; }
    .sortbar { display: flex; }
    header { padding: 12px 12px 4px; }
    header .sub { display: none; }
    label.chk.long span { display: none; }
    .storelist { grid-template-columns: 1fr; }
  }

  header.top { max-width: 1400px; margin: 0 auto; padding: 10px 12px 2px; display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; }
  header.top .brand { font-weight: 700; font-size: 20px; color: var(--ink); text-decoration: none; }
  header.top nav { display: flex; gap: 4px 12px; flex-wrap: wrap; align-items: center; margin-left: auto; font-size: 14px; }
  header.top nav a { color: var(--muted); text-decoration: none; padding: 8px 4px; }
  header.top nav a.on { color: var(--accent); font-weight: 600; }
  form.inline { display: inline; }
  button.link { background: none; border: none; color: var(--muted); padding: 8px 4px; min-height: 0; font-size: 14px; }
  .panel.narrow { max-width: 420px; margin: 24px auto; }
  .panel h1 { margin: 0 0 12px; font-size: 20px; }
  .stack { display: flex; flex-direction: column; gap: 12px; }
  .stack label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
  .stack label.chk { flex-direction: row; align-items: center; color: var(--ink); }
  .stack input:not([type=checkbox]), .stack select { width: 100%; }
  input.inl { width: auto !important; flex: 1; margin-left: 6px; }
  input.short { width: 90px !important; }
  fieldset { border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; display: flex; flex-wrap: wrap; gap: 4px 16px; }
  legend { color: var(--muted); font-size: 13px; padding: 0 4px; }
  .row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
  .row.wrap label { flex: 1 1 180px; display: flex; flex-direction: column; font-size: 13px; color: var(--muted); }
  .row.wrap input, .row.wrap select { width: 100%; }
  .error { background: var(--bad-bg); color: var(--bad); padding: 10px 12px; border-radius: 10px; margin: 0 0 12px; }
  .hidden { display: none !important; }
  .mt { margin-top: 14px; }
  code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; background: var(--bg); padding: 2px 6px; border-radius: 6px; word-break: break-all; }
  #qr svg { width: 180px; height: 180px; background: #fff; padding: 6px; border-radius: 8px; }
  .events .type { font-weight: 600; }
  .events .type.in_stock { color: var(--ok); } .events .type.price_drop { color: var(--ok); } .events .type.out_of_stock, .events .type.price_up { color: var(--bad); }
  .cards .card .name a { word-break: break-word; }
  main { padding-bottom: 80px; }

  header.top .brand { display: inline-flex; align-items: center; gap: 8px; }
  .brandmark { width: 30px; height: 30px; }
  .loginlogo { width: 100%; height: auto; display: block; margin: 0 auto 16px; }
  .panel.narrow { margin-top: 40px; }

  .card .row { flex-wrap: nowrap; align-items: flex-start; }
  .card .row .info { flex: 1 1 auto; min-width: 0; display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; }
  .card .row .acts { flex: 0 0 auto; display: flex; gap: 6px; margin-left: 8px; }
  .actions button, .card .acts button { min-width: 62px; text-align: center; }

  /* header: brand row + full-width tab row */
  header.top { display: block; padding: 10px 12px 0; }
  .brandrow { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
  nav.tabs { display: flex; gap: 6px; margin: 10px 0 2px; margin-left: 0; }
  nav.tabs a { flex: 1 1 0; text-align: center; padding: 10px 6px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--muted); font-size: 14px; white-space: nowrap; }
  nav.tabs a.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
  @media (min-width: 761px) { nav.tabs { display: inline-flex; } nav.tabs a { flex: 0 0 auto; padding: 8px 16px; } }
  /* search box: input row, then button row */
  form.searchbox { display: flex; flex-direction: column; gap: 8px; }
  form.searchbox input[type=search] { width: 100%; }
  .frow .grow { flex: 1 1 0; min-width: 0; }
  .frow select { min-height: 44px; }
  .frow .chip, .frow button { min-height: 44px; }
  .frow.packs .chip { min-height: 44px; }
  .frow .count { flex: 0 0 auto; min-width: 70px; text-align: right; }
  details.stores { margin-top: 8px; }
  details.stores summary { list-style: none; }
  details.stores summary::before { content: "▸ "; color: var(--muted); }
  details.stores[open] summary::before { content: "▾ "; }
  #deep { flex: 0 0 auto; padding: 8px 18px; }
  .chip { border-radius: 12px; }
  .filters { margin-top: 12px; }
  @media (min-width: 761px) {
    form.searchbox { flex-direction: row; align-items: center; }
    form.searchbox input[type=search] { flex: 1 1 320px; width: auto; }
    form.searchbox .frow { margin-top: 0; }
    form.searchbox .grow { flex: 0 0 auto; padding: 10px 22px; }
    .frow.packs .chip { flex: 0 0 auto; padding: 8px 18px; }
    .frow select.grow { flex: 0 1 260px; }
  }
  /* fixes: input height, active tab text, filter box width */
  form.searchbox input[type=search] { flex: 0 0 auto; height: 48px; min-height: 48px; }
  header.top nav.tabs a.on { color: var(--accent-ink); }
  header.top nav.tabs a { padding: 10px 6px; }
  .frow .count:empty { display: none; }
  .frow input[type=text].grow { width: auto; }

  /* shared form system for settings/admin */
  .label { font-size: 13px; color: var(--muted); margin-top: 6px; }
  .stack .frow { margin-top: 0; }
  .stack input, .stack select { min-height: 44px; }
  .frow input.grow, .frow select.grow { width: auto; }
  .stack details summary { cursor: pointer; color: var(--muted); font-size: 14px; padding: 6px 0; }
  .stack details .frow { margin-top: 8px; }
  h2 button.small { min-height: 40px; }
  .panel h2 { font-size: 18px; }
  @media (max-width: 760px) { .frow .chip:not(.grow) { flex: 0 0 auto; } .frow.packs .chip { flex: 1 1 0; } }

  details.stores summary .badge { margin-left: 6px; vertical-align: middle; }
