:root {
  --bg: #f4f4f6;
  --card: #ffffff;
  --text: #1b1b1f;
  --muted: #6b6b76;
  --line: #e2e2e8;
  --brand: #d01012;
  --brand-ink: #ffffff;
  --ok: #1f8a3a;
  --ok-bg: #e5f5e9;
  --miss: #c2410c;
  --miss-bg: #fff1e6;
  --chip: #ececf1;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 1px 2px rgba(0, 0, 0, .04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121215;
    --card: #1d1d22;
    --text: #f1f1f4;
    --muted: #a0a0ab;
    --line: #2e2e36;
    --ok: #4ade80;
    --ok-bg: #11301b;
    --miss: #fb923c;
    --miss-bg: #3a2010;
    --chip: #2a2a31;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding-top: calc(56px + env(safe-area-inset-top));
  padding-bottom: calc(68px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
img { display: block; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select { font: inherit; }
.hidden { display: none !important; }

/* Top bar */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  height: calc(56px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 12px 0;
  background: var(--brand); color: var(--brand-ink);
}
.topbar h1 { font-size: 19px; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .spacer { flex: 1; }
.topbar .topbtn {
  background: rgba(255,255,255,.18); border: 0; color: inherit; font-size: 20px;
  width: 42px; height: 42px; border-radius: 10px; flex: none;
}
.icon-btn {
  background: var(--chip); border: 0; border-radius: 10px; width: 42px; height: 42px; flex: none;
  font-size: 18px; align-self: flex-start;
}
.topbar .back {
  background: none; border: 0; color: inherit; font-size: 34px; line-height: 1;
  padding: 0 8px 4px 0;
}

/* Bottom tabs */
.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  background: none; border: 0; padding: 8px 0 10px; font-size: 12px; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.tabs button .ico { font-size: 22px; filter: grayscale(1); opacity: .7; }
.tabs button.active { color: var(--brand); font-weight: 600; }
.tabs button.active .ico { filter: none; opacity: 1; }

main { padding: 12px 16px 24px; max-width: 900px; margin: 0 auto; }

/* Generic bits */
.card {
  background: var(--card); border-radius: 14px; box-shadow: var(--shadow);
  padding: 14px; margin-bottom: 12px;
}
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 12px; padding: 12px 16px; font-weight: 600;
  background: var(--chip); color: var(--text); min-height: 46px;
}
.btn.primary { background: var(--brand); color: var(--brand-ink); }
.btn.danger { background: #b91c1c; color: #fff; }
.btn.ok { background: var(--ok); color: #fff; }
.btn.block { width: 100%; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .5; }
.row { display: flex; gap: 8px; align-items: center; }
.row > .grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.center { text-align: center; }
h2 { font-size: 18px; margin: 4px 0 10px; }
p { margin: 6px 0; }
a { color: var(--brand); }

.field {
  width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); font-size: 17px;
}
label.lbl { display: block; font-weight: 600; margin: 10px 0 6px; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty .big { font-size: 48px; margin-bottom: 8px; }

/* Set list */
.set-card { display: flex; gap: 12px; align-items: center; cursor: pointer; }
.set-card img {
  width: 84px; height: 64px; object-fit: contain; background: #fff; border-radius: 8px; flex: none;
}
.set-card > .grow { flex: 1; min-width: 0; }
.set-card .name { font-weight: 600; line-height: 1.25; }
.progress { height: 8px; border-radius: 4px; background: var(--chip); overflow: hidden; margin-top: 6px; }
.progress > div { height: 100%; background: var(--ok); }
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 13px; font-weight: 600;
}
.badge.miss { background: var(--miss-bg); color: var(--miss); }
.badge.swap { background: #e0ecff; color: #1d4ed8; }
@media (prefers-color-scheme: dark) { .badge.swap { background: #172554; color: #93c5fd; } }
.badge.ok { background: var(--ok-bg); color: var(--ok); }

/* Filters */
.seg {
  display: flex; background: var(--chip); border-radius: 12px; padding: 3px; margin-bottom: 10px;
}
.seg button {
  flex: 1; border: 0; background: none; padding: 8px 4px; border-radius: 9px; font-size: 14px; font-weight: 600;
  color: var(--muted);
}
.seg button.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; margin-bottom: 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 10px; font-size: 13px;
}
.chip.active { border-color: var(--brand); box-shadow: inset 0 0 0 1px var(--brand); }
.swatch {
  display: inline-block; width: 14px; height: 14px; border-radius: 4px; border: 1px solid rgba(0,0,0,.25); flex: none;
}

/* Part grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.part {
  background: var(--card); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden;
  display: flex; flex-direction: column; border: 2px solid transparent;
}
.part.missing { border-color: var(--miss); }
.part.done { opacity: .7; }
.part .pic {
  position: relative; background: #fff; aspect-ratio: 1 / 1; display: flex; align-items: center; justify-content: center;
}
.part .pic img { width: 88%; height: 88%; object-fit: contain; }
.part .pic .noimg { color: #999; font-size: 13px; }
.part .tick {
  position: absolute; top: 6px; right: 6px; width: 30px; height: 30px; border-radius: 50%;
  background: var(--ok); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700;
}
.part .need-tag {
  position: absolute; top: 6px; left: 6px; background: var(--miss); color: #fff; font-weight: 700;
  border-radius: 999px; padding: 2px 9px; font-size: 14px;
}
.part .info { padding: 8px 10px 4px; font-size: 13px; flex: 1; }
.part .info .pname { font-weight: 600; line-height: 1.2; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.part .info .meta { color: var(--muted); margin-top: 3px; display: flex; align-items: center; gap: 5px; flex-wrap: wrap; }
.counter { display: flex; align-items: center; justify-content: space-between; padding: 6px 8px 8px; gap: 4px; }
.counter button {
  width: 42px; height: 40px; border-radius: 10px; border: 0; background: var(--chip); font-size: 22px; font-weight: 700;
}
.counter .count { font-weight: 700; font-size: 16px; text-align: center; flex: 1; }
.counter .count .of { color: var(--muted); font-weight: 500; }

.grid.compact { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.grid.compact .part { cursor: pointer; }
.grid.compact .info { padding: 4px 6px 6px; font-size: 11px; }
.grid.compact .need-tag { font-size: 12px; padding: 1px 7px; top: 4px; left: 4px; }

/* Looking-for list */
.need-item { display: flex; gap: 12px; padding: 12px; }
.need-item .pic {
  width: 104px; height: 104px; flex: none; background: #fff; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
}
.need-item .pic img { width: 92%; height: 92%; object-fit: contain; }
.need-item .body { flex: 1; min-width: 0; }
.need-item .pname { font-weight: 600; line-height: 1.25; }
.need-item .meta { color: var(--muted); font-size: 14px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin: 3px 0 6px; }
.for-set {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  border-top: 1px solid var(--line); padding-top: 6px; margin-top: 6px; font-size: 14px;
}
.for-set .sname { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Scan */
.scan-hero { text-align: center; padding: 24px 16px; }
.scan-hero .big { font-size: 64px; }
.scan-photo { width: 100%; max-height: 260px; object-fit: contain; border-radius: 12px; background: #000; }
.match-card { border: 2px solid var(--line); }
.match-card.hit { border-color: var(--ok); }
.verdict { font-size: 18px; font-weight: 700; margin: 4px 0; }
.verdict.yes { color: var(--ok); }
.verdict.no { color: var(--muted); }
.spinner {
  width: 40px; height: 40px; border-radius: 50%; border: 4px solid var(--chip); border-top-color: var(--brand);
  animation: spin 0.9s linear infinite; margin: 20px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

.thumb {
  width: 56px; height: 56px; background: #fff; border-radius: 8px; flex: none;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.thumb img { width: 90%; height: 90%; object-fit: contain; }
.thumb .noimg { font-size: 10px; }

/* Toast */
.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 30;
  background: #222; color: #fff; padding: 12px 14px; border-radius: 12px; display: flex; align-items: center; gap: 12px;
  max-width: 560px; margin: 0 auto; box-shadow: 0 6px 20px rgba(0,0,0,.25);
}
.toast .grow { flex: 1; }
.toast button { background: none; border: 0; color: #ffb4b4; font-weight: 700; padding: 4px 6px; }

/* Bottom sheet */
.sheet {
  position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet .panel {
  background: var(--bg); width: 100%; max-width: 640px; max-height: 88vh; overflow: auto;
  border-radius: 18px 18px 0 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
.sheet .big-pic { background: #fff; border-radius: 12px; display: flex; justify-content: center; padding: 10px; }
.sheet .big-pic img { width: 70%; max-width: 280px; aspect-ratio: 1/1; object-fit: contain; }

/* Values & swaps */
.value { color: var(--text); }
.value-btn { margin-top: 8px; }
.swap-box { border: 2px solid #1d4ed8; }
.swap-box h2 { font-size: 16px; }
a.btn { text-decoration: none; }
