:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #5b6477;
  --line: #e2e6ef;
  --brand: #1d4ed8;
  --brand-ink: #ffffff;
  --accent: #fde047;
  --good: #15803d;
  --good-bg: #dcfce7;
  --warn: #a16207;
  --warn-bg: #fef9c3;
  --bad: #b91c1c;
  --bad-bg: #fee2e2;
  --shadow: 0 1px 2px rgb(15 23 42 / 6%), 0 4px 16px rgb(15 23 42 / 6%);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --panel: #121a2b;
    --ink: #e8edf7;
    --muted: #97a3b8;
    --line: #243049;
    --brand: #3b82f6;
    --good: #4ade80;
    --good-bg: #12331f;
    --warn: #facc15;
    --warn-bg: #3a3210;
    --bad: #f87171;
    --bad-bg: #3b1515;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 6px 20px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
body.modal-open { overflow: hidden; }
a { color: var(--brand); }
h1 { font-size: 1.5rem; margin: 0 0 .5rem; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 .5rem; }
h3 { font-size: 1rem; margin: 1.25rem 0 .5rem; }
code { font: 0.85em ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.empty { color: var(--muted); padding: 1rem 0; }
.error { color: var(--bad); }
.up { color: var(--good); }
.down { color: var(--bad); }

/* ---------- top bar ---------- */
.topbar { background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 10; }
.topbar-inner { max-width: 1100px; margin: 0 auto; padding: .5rem 16px; display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 800; font-size: 1.15rem; color: var(--ink); text-decoration: none; }
nav { display: flex; gap: .25rem; overflow-x: auto; scrollbar-width: none; max-width: 100%; }
nav a { padding: .4rem .6rem; border-radius: 999px; color: var(--muted); text-decoration: none; white-space: nowrap; font-weight: 600; font-size: .95rem; }
nav a[aria-current] { background: var(--brand); color: var(--brand-ink); }
nav a[aria-current] .count { color: var(--brand-ink); }
.count { font-size: .75rem; color: var(--muted); margin-left: 2px; }

@media (max-width: 440px) {
  .topbar-inner { gap: .25rem; }
  nav { gap: 0; }
  nav a { padding: .35rem .45rem; font-size: .85rem; }
  .panel { padding: 1rem .85rem; }
}
main { flex: 1; width: 100%; max-width: 1100px; margin: 0 auto; padding: 16px; outline: none; }
footer { max-width: 1100px; margin: 0 auto; padding: 1.5rem 16px 2rem; color: var(--muted); font-size: .8rem; text-align: center; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.25rem; box-shadow: var(--shadow); }
.panel.narrow { max-width: 680px; margin: 0 auto; }
.head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: .75rem; }

/* ---------- controls ---------- */
.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin: .5rem 0; }
.row.tight { margin: .25rem 0 0; gap: .35rem; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  min-height: 42px; padding: .5rem 1rem; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel); color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:hover { border-color: var(--brand); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.small { min-height: 34px; padding: .3rem .7rem; font-size: .85rem; }
.btn:disabled { opacity: .55; cursor: progress; }
.icon-btn { border: 0; background: transparent; color: var(--muted); font-size: 1.5rem; line-height: 1; width: 40px; height: 40px; border-radius: 8px; cursor: pointer; }
.icon-btn:hover { background: var(--bg); color: var(--ink); }
input, select {
  font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: .5rem .7rem; min-height: 42px; width: 100%; max-width: 100%;
}
input:focus-visible, select:focus-visible, .btn:focus-visible, a:focus-visible, .tile:focus-visible { outline: 3px solid var(--brand); outline-offset: 2px; }
label { display: block; font-weight: 600; font-size: .9rem; margin: .75rem 0 .25rem; }
label.check { display: flex; align-items: center; gap: .5rem; font-weight: 500; }
label.check input { width: 20px; min-height: 20px; height: 20px; }
label.btn { margin: 0; display: inline-flex; font-size: 1rem; }
.filters { display: grid; grid-template-columns: 1fr; gap: .5rem; margin: .75rem 0; }
@media (min-width: 720px) { .filters { grid-template-columns: 2fr repeat(3, 1fr); } }
.form-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 .75rem; }
.form-grid label { min-width: 0; }
.form-grid label { margin-top: .25rem; }
.form-grid label input, .form-grid label select { margin-top: .25rem; font-weight: 400; }
input.qty { width: 84px; }
.item-side select { width: auto; }

/* ---------- badges ---------- */
.badge { display: inline-block; font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; margin-left: .35rem; vertical-align: middle; }
.badge.good { background: var(--good-bg); color: var(--good); }
nav a[aria-current] .badge.good { background: var(--brand-ink); }
.match { font-size: .75rem; font-weight: 700; }
.match.good { color: var(--good); }
.match.warn { color: var(--warn); }
.match.bad { color: var(--bad); }
.price { font-weight: 700; }
.price.muted { font-weight: 400; color: var(--muted); }

/* ---------- scanner ---------- */
.camera { position: relative; width: 100%; max-width: 560px; margin: .75rem 0; background: #000; border-radius: var(--radius); overflow: hidden; aspect-ratio: 3 / 4; }
.camera video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.guide { position: absolute; border: 3px solid var(--accent); border-radius: 12px; box-shadow: 0 0 0 9999px rgb(0 0 0 / 45%); left: 20%; top: 7%; width: 60%; height: 86%; }
.status { margin: .75rem 0; padding: .6rem .8rem; border-radius: 10px; background: var(--bg); min-height: 1.5em; }
.status:empty { display: none; }
.status.ok { background: var(--good-bg); color: var(--good); }
.status.error { background: var(--bad-bg); color: var(--bad); }
.preview { display: flex; gap: 1rem; align-items: flex-start; flex-wrap: wrap; }
.shot { max-width: 140px; border-radius: 8px; border: 1px solid var(--line); }
.best { border: 2px solid var(--brand); border-radius: var(--radius); padding: 1rem .75rem; margin: .75rem 0; min-width: 0; }

/* ---------- card grid ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .75rem; margin: .75rem 0; }
.tile {
  display: flex; flex-direction: column; gap: 2px; text-align: left; padding: .5rem; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink); font: inherit; cursor: pointer; position: relative;
}
.tile:hover { border-color: var(--brand); }
.tile.owned { border-color: var(--good); }
.tile .badge { position: absolute; top: .9rem; right: .9rem; margin: 0; }
.tile-name { font-weight: 700; font-size: .9rem; line-height: 1.25; margin-top: .35rem; }
.tile-meta { color: var(--muted); font-size: .75rem; }
.card-img { width: 100%; aspect-ratio: 63 / 88; object-fit: contain; border-radius: 8px; background: var(--bg); display: block; }
.card-img.placeholder { display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: .8rem; }

/* ---------- detail sheet ---------- */
.modal { position: fixed; inset: 0; background: rgb(2 6 23 / 60%); z-index: 50; display: flex; align-items: flex-end; justify-content: center; padding: 0; }
@media (min-width: 720px) { .modal { align-items: center; padding: 16px; } }
.sheet { background: var(--panel); width: 100%; max-width: 860px; max-height: 92vh; overflow-y: auto; border-radius: 18px 18px 0 0; padding: 1.25rem 16px 1.5rem; position: relative; }
@media (min-width: 720px) { .sheet { border-radius: 18px; padding: 1.5rem; } }
.sheet .close { position: absolute; top: .5rem; right: .5rem; }
.card-detail { display: grid; grid-template-columns: minmax(0, 1fr); gap: 1.25rem; }
.detail-body { min-width: 0; }
@media (min-width: 720px) { .card-detail { grid-template-columns: 300px minmax(0, 1fr); } }
.detail-img .card-img { max-width: 300px; margin: 0 auto; }
.detail-body h2 { margin-top: 0; padding-right: 2rem; }
table.prices { width: 100%; border-collapse: collapse; font-size: .85rem; table-layout: fixed; }
table.prices th:first-child, table.prices td:first-child { width: 34%; overflow-wrap: anywhere; }
table.prices th, table.prices td { text-align: left; padding: .35rem .4rem; border-bottom: 1px solid var(--line); }
table.prices th { color: var(--muted); font-weight: 600; }

/* ---------- lists ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: .75rem; margin: .75rem 0 1rem; }
.stat { background: var(--bg); border-radius: 12px; padding: .75rem; display: flex; flex-direction: column; }
.stat-label { color: var(--muted); font-size: .8rem; }
.stat-value { font-size: 1.3rem; font-weight: 800; }
.list { display: flex; flex-direction: column; gap: .5rem; }
.item { display: grid; grid-template-columns: 64px 1fr; gap: .75rem; align-items: center; padding: .6rem; border: 1px solid var(--line); border-radius: 12px; }
.item.deal { border-color: var(--good); background: var(--good-bg); }
.item-main { display: flex; flex-direction: column; min-width: 0; }
.item-main strong { overflow-wrap: anywhere; }
.item-side { grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: .25rem .75rem; }
@media (min-width: 640px) {
  .item { grid-template-columns: 64px 1fr auto; }
  .item-side { grid-column: auto; flex-direction: column; align-items: flex-end; }
}
.thumb { padding: 0; border: 0; background: none; cursor: pointer; width: 64px; }

/* ---------- sets ---------- */
.sets { display: flex; flex-direction: column; gap: .5rem; }
.sets.compact { flex-direction: row; flex-wrap: wrap; }
.set-row { display: flex; gap: .75rem; align-items: center; padding: .6rem; border: 1px solid var(--line); border-radius: 12px; text-decoration: none; color: var(--ink); }
.set-row:hover { border-color: var(--brand); }
.set-logo { width: 88px; height: 40px; object-fit: contain; flex-shrink: 0; }
.set-banner { max-height: 60px; width: auto; aspect-ratio: auto; background: none; }
.set-@media (max-width: 440px) {
  .topbar-inner { gap: .25rem; }
  nav { gap: 0; }
  nav a { padding: .35rem .45rem; font-size: .85rem; }
  .panel { padding: 1rem .85rem; }
}
main { flex: 1; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.chip { padding: .3rem .7rem; border: 1px solid var(--line); border-radius: 999px; font-size: .85rem; text-decoration: none; color: var(--ink); }
.chip:hover { border-color: var(--brand); }
.bar { height: 8px; background: var(--bg); border-radius: 999px; overflow: hidden; border: 1px solid var(--line); min-width: 160px; }
.bar .fill { display: block; height: 100%; width: 0; background: var(--good); }

/* ---------- toast ---------- */
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--panel); padding: .65rem 1rem; border-radius: 10px; z-index: 60; max-width: calc(100% - 32px); box-shadow: var(--shadow); }

@media (prefers-reduced-motion: no-preference) {
  .btn, .tile, .set-row { transition: border-color .15s; }
}
