: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; }

  .brandacts { display: flex; align-items: center; gap: 12px; }
  a.switch { color: var(--accent); text-decoration: none; font-weight: 600; font-size: 14px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; white-space: nowrap; }
  @media (max-width: 420px) { a.switch { font-size: 13px; padding: 8px 8px; } }

  .storechip { text-align: left; white-space: normal; line-height: 1.3; }

  /* ---- action buttons vs pills: solid accent = does something; pill = a filter/toggle ---- */
  button.primary { background: var(--accent); color: var(--accent-ink); border: 1px solid var(--accent); font-weight: 700; letter-spacing: .2px;
                   box-shadow: 0 2px 6px rgba(139,74,31,.35); }
  button.primary:hover { filter: brightness(1.08); }
  .chip, .chip.on { box-shadow: none; font-weight: 500; border-radius: 999px; }
  .chip.on { background: color-mix(in srgb, var(--accent) 18%, var(--bg)); color: var(--accent); border-color: var(--accent); font-weight: 600; }
  .chip.on::before { content: "✓ "; }
  a.switch { display: inline-flex; align-items: center; gap: 8px; background: var(--accent); color: var(--accent-ink); border: 1px solid var(--accent);
             font-weight: 700; padding: 8px 14px 8px 8px; border-radius: 999px; box-shadow: 0 2px 6px rgba(139,74,31,.35); }
  a.switch:hover { filter: brightness(1.08); }
  .switchmark { width: 26px; height: 26px; }
  .switchlabel { font-weight: 400; opacity: .85; }
  @media (max-width: 600px) { .switchlabel { display: none; } a.switch { padding: 6px 12px 6px 6px; font-size: 13px; } }
  /* ---- widths on large screens ---- */
  @media (min-width: 761px) {
    main { max-width: 1100px; }
    .panel.form, section.panel:not(.wide) form.stack, section.panel form#prefsForm, section.panel form#pwForm, section.panel form#smtpForm, section.panel form#siteForm, section.panel form#newUser { max-width: 620px; }
    form.searchbox { max-width: 900px; }
    .filters { max-width: 900px; }
    .frow .grow { flex: 1 1 200px; }
    .frow select.grow { flex: 0 1 240px; }
    form.searchbox .grow { flex: 0 0 auto; }
    .frow.packs .chip { flex: 0 0 auto; }
    .frow button.primary.grow { flex: 0 0 auto; padding-left: 26px; padding-right: 26px; }
    #totpOff .frow .grow, #totpOn .frow .grow, #vabcList .frow .grow { flex: 0 0 auto; }
    .storechip { flex: 0 0 auto; }
  }
  /* ---- desktop search row: input | Search | Full details, help text below ---- */
  #deephelp { margin: 2px 0 0; }
  @media (min-width: 761px) {
    form.searchbox { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px; max-width: none; }
    form.searchbox input[type=search] { flex: 1 1 380px; width: auto; min-width: 280px; }
    form.searchbox .frow { flex: 0 0 auto; flex-wrap: nowrap; margin: 0; gap: 10px; }
    form.searchbox .frow button { min-height: 48px; }
    form.searchbox #deephelp { flex: 1 1 100%; }
    .filters .frow { max-width: 900px; }
  }
  /* ---- Bourbon Watch palette: always dark, red wax + copper + mint ---- */
  body[data-catalog="bourbon"], body[data-catalog="bourbon"][data-theme="light"] {
    --bg: #17101a; --panel: #23161c; --ink: #f6ebe4; --muted: #cfa686; --line: #4b2c35;
    --accent: #d6232f; --accent-ink: #ffffff; --hover: #2f1c25;
    --ok: #6fe3b0; --ok-bg: #123b2d; --bad: #ff9c5a; --bad-bg: #4a2a12; --unk: #cfa686; --unk-bg: #2f2228;
  }
  body[data-catalog="bourbon"] header.top .brand { color: #e39a5c; }
  body[data-catalog="bourbon"] nav.tabs a { background: #2a1a20; border-color: #5a343d; color: #e0c3ad; }
  body[data-catalog="bourbon"] nav.tabs a.on { background: var(--accent); border-color: var(--accent); color: #fff; }
  body[data-catalog="bourbon"] .panel { border-color: #5a343d; box-shadow: inset 0 1px 0 #3a2229; }
  body[data-catalog="bourbon"] input, body[data-catalog="bourbon"] select, body[data-catalog="bourbon"] button:not(.primary):not(.chip) { border-color: #5a343d; }
  body[data-catalog="bourbon"] .chip { border-color: #6a3f49; color: #e0c3ad; }
  body[data-catalog="bourbon"] .chip.on { background: #4a1b22; color: #ffb4a2; border-color: var(--accent); }
  body[data-catalog="bourbon"] .card { background: #1c1218; border-color: #4b2c35; }
  body[data-catalog="bourbon"] .card .price, body[data-catalog="bourbon"] td.num { color: #ffd39a; }
  body[data-catalog="bourbon"] th { color: #cfa686; }
  body[data-catalog="bourbon"] .progress { background: #3a2229; }
  body[data-catalog="bourbon"] .label, body[data-catalog="bourbon"] .note { color: #b8917a; }
  body[data-catalog="bourbon"] a.switch { background: #e39a5c; border-color: #e39a5c; color: #1a0f08; }
  button.primary, a.switch { box-shadow: 0 2px 6px color-mix(in srgb, var(--accent) 40%, transparent); }

  .editbox { min-width: 260px; }
  .editbox .frow { margin-top: 6px; }
  .actions .editbox { white-space: normal; }

  body[data-catalog="cigars"] a.switch { background: #d6232f; border-color: #d6232f; color: #fff; box-shadow: 0 2px 6px rgba(214,35,47,.4); }
  body[data-catalog="cigars"] a.switch .switchmark { background: #17101a; border-radius: 50%; }
  body[data-catalog="bourbon"] a.switch { background: #d08a4f; border-color: #d08a4f; color: #1a120a; box-shadow: 0 2px 6px rgba(208,138,79,.4); }

  .hp { position: absolute; left: -9999px; top: -9999px; }
  textarea { font: inherit; font-size: 16px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--ink); width: 100%; }

  /* admin users table: fit the panel on desktop, no sideways scrolling */
  #users { font-size: 13px; table-layout: auto; }
  #users th, #users td { padding: 6px 8px; white-space: normal; vertical-align: top; }
  #users td { word-break: break-word; }
  #users td.actions { white-space: normal; }
  #users td.actions button { margin: 0 4px 4px 0; padding: 4px 8px; min-height: 30px; min-width: 0; font-size: 12px; }
  #users td .badge { white-space: nowrap; }
  #users .note { font-size: 11px; }
  #users td.retailer { white-space: normal; }
  @media (min-width: 761px) { #users td.actions { width: 190px; } }

  button.small.link { border: none; background: none; color: var(--accent); text-decoration: underline; padding: 0 2px; min-height: 0; font-weight: 600; }
  #watchDetail .events { max-height: 320px; }
  #watchDetail .events > div { padding: 6px 0; }
  .panel.inner { margin-top: 10px; }

  /* ---- three-way switch: each pill wears the colors of the side it leads to ---- */
  .brandacts { gap: 8px; }
  a.switch .switchlabel { font-weight: 700; }
  body a.switch.switch-cigars { background: #d08a4f; border-color: #d08a4f; color: #1a120a; box-shadow: 0 2px 6px rgba(208,138,79,.4); }
  body a.switch.switch-bourbon { background: #d6232f; border-color: #d6232f; color: #fff; box-shadow: 0 2px 6px rgba(214,35,47,.4); }
  body a.switch.switch-parts { background: #2f8be0; border-color: #2f8be0; color: #fff; box-shadow: 0 2px 6px rgba(47,139,224,.4); }
  body a.switch.switch-parts .switchmark, body a.switch.switch-bourbon .switchmark { background: transparent; }
  @media (max-width: 600px) { a.switch .switchlabel { display: inline; font-size: 12px; } a.switch { padding: 5px 10px 5px 5px; } .switchmark { width: 22px; height: 22px; } }
  /* ---- Parts Watch palette: always dark, steel blue + safety yellow ---- */
  body[data-catalog="parts"], body[data-catalog="parts"][data-theme="light"] {
    --bg: #0f1620; --panel: #172230; --ink: #e8eef5; --muted: #93a4b8; --line: #2a3a4f;
    --accent: #2f8be0; --accent-ink: #ffffff; --hover: #1e2b3c;
    --ok: #4fd68b; --ok-bg: #0f3b2a; --bad: #ffb020; --bad-bg: #4a3608; --unk: #93a4b8; --unk-bg: #22303f;
  }
  body[data-catalog="parts"] header.top .brand { color: #ffc63d; }
  body[data-catalog="parts"] nav.tabs a { background: #1a2635; border-color: #34475f; color: #b9c8d8; }
  body[data-catalog="parts"] nav.tabs a.on { background: var(--accent); border-color: var(--accent); color: #fff; }
  body[data-catalog="parts"] .chip { border-color: #3d5270; color: #b9c8d8; }
  body[data-catalog="parts"] .chip.on { background: #163a5f; color: #8cc4ff; border-color: var(--accent); }
  body[data-catalog="parts"] .card { background: #121b27; border-color: #2a3a4f; }
  body[data-catalog="parts"] .card .price, body[data-catalog="parts"] td.num { color: #ffc63d; }

  .quicklinks { margin-top: 10px; }
  .quicklinks .qlink { text-decoration: none; }
  @media (max-width: 760px) { .quicklinks .qlink { flex: 1 1 30%; text-align: center; } }
