:root {
  --bg: #16131a;
  --panel: #1f1b25;
  --panel-2: #29242f;
  --line: #3a3342;
  --text: #ece6f2;
  --muted: #a79fb2;
  --accent: #d9a441;
  --accent-ink: #1b1508;
  --good: #5fb67a;
  --danger: #e0685f;
  --r-common: #9aa0a6;
  --r-uncommon: #58b368;
  --r-rare: #4d96e6;
  --r-veryrare: #a574e8;
  --r-unique: #e39a3b;
  --r-promo: #3cb7b0;
  --radius: 12px;
  --topbar-h: 60px;
  /* The top bar grows by the device's top safe area (notch/status bar) instead of squeezing its buttons into it,
     where iOS Safari swallows taps. The inset is 0 unless the page extends under the notch. */
  --safe-top: env(safe-area-inset-top, 0px);
  --header-h: calc(var(--topbar-h) + var(--safe-top));
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; overflow-x: clip; } /* never scroll sideways; clip (unlike hidden) keeps sticky headers working */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body.noscroll { overflow: hidden; }
img { max-width: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; } /* no double-tap-zoom delay on phones */
[hidden] { display: none !important; }

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  height: var(--header-h);
  padding: var(--safe-top) 16px 0;
  background: var(--bg); /* solid, no backdrop-filter: suspected cause of taps not reaching this sticky bar on iPhone Safari */
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 { font-size: 17px; margin: 0; letter-spacing: .3px; }
.brand .sub { margin: 0; font-size: 12px; color: var(--muted); white-space: nowrap; }
.d20 { width: 30px; height: 30px; color: var(--accent); flex: none; }
.overall { margin-left: auto; white-space: nowrap; display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
.overall strong { color: var(--text); }
.overall .bar { width: 120px; }
.overall .promo-count { padding-left: 10px; border-left: 1px solid var(--line); }
.overall .promo-count .ico { display: none; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 10px; background: transparent; color: var(--text);
}
.icon-btn:hover { background: var(--panel-2); }

.menu {
  position: absolute; right: 12px; top: calc(var(--header-h) - 4px);
  display: flex; flex-direction: column; min-width: 220px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0,0,0,.45); overflow: hidden;
}
.menu button { text-align: left; padding: 12px 16px; background: none; border: 0; }
.menu button:hover { background: var(--panel-2); }
.menu .danger { color: var(--danger); }

/* ---------- layout ---------- */
.layout { display: flex; align-items: flex-start; }
.sidebar {
  position: sticky; top: var(--header-h);
  width: 300px; flex: none;
  height: calc(100vh - var(--header-h)); height: calc(100dvh - var(--header-h));
  overflow-y: auto; overscroll-behavior: contain;
  border-right: 1px solid var(--line); background: var(--panel);
}
.sidebar-head { position: sticky; top: 0; z-index: 2; display: flex; flex-direction: column; gap: 8px; padding: 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.sidebar-row { display: flex; gap: 8px; align-items: center; }
.sidebar-row input, .sidebar-row select { flex: 1; }
.sidebar-row .chip { height: 40px; }
.chip[aria-pressed=true] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.set-empty { padding: 20px 16px; color: var(--muted); font-size: 14px; }
.main { flex: 1; min-width: 0; padding: 16px 20px 40px; }

input[type=search], select {
  height: 40px; padding: 0 12px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px;
  outline: none; min-width: 0;
}
input[type=search]:focus, select:focus { border-color: var(--accent); }

/* ---------- set list ---------- */
.set-item {
  display: block; width: 100%; text-align: left;
  padding: 10px 14px; border: 0; border-left: 3px solid transparent;
  background: none; border-bottom: 1px solid rgba(58, 51, 66, .5);
}
.set-item:hover { background: var(--panel-2); }
.set-item.active { background: var(--panel-2); border-left-color: var(--accent); }
.set-item .row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.set-item .name { font-weight: 600; font-size: 14px; }
.set-item .pct { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.set-item .meta { font-size: 12px; color: var(--muted); margin: 2px 0 6px; }
.set-item.complete .pct { color: var(--good); font-weight: 600; }
.set-item.all .name { color: var(--accent); }

.cat { border-bottom: 1px solid var(--line); }
.cat-head { position: relative; }
.cat-head .set-item { padding-right: 52px; }
.cat-item .name { font-size: 15px; letter-spacing: .2px; }
.cat-toggle {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 40px; height: 40px; display: grid; place-items: center;
  border: 0; border-radius: 10px; background: none; color: var(--muted);
}
.cat-toggle:hover { background: var(--line); color: var(--text); }
.cat-toggle svg { transition: transform .15s; }
.cat.open .cat-toggle svg { transform: rotate(180deg); }
.cat-head .pct { margin-right: 4px; }
.grp-item { padding-left: 24px; background: rgba(0,0,0,.12); }
.grp-item .name { font-size: 13px; color: var(--accent); }
.cat-sets .set-item { padding-left: 24px; }
.grp-sets .set-item { padding-left: 36px; }
/* A release's product sets, listed under it */
.child-sets .set-item.child { padding-left: 40px; position: relative; }
.grp-sets .child-sets .set-item.child { padding-left: 52px; }
.child-sets .set-item.child::before { content: "↳"; position: absolute; left: 26px; top: 10px; color: var(--muted); font-size: 12px; }
.grp-sets .child-sets .set-item.child::before { left: 38px; }
.child-sets .set-item.child .name { font-weight: 500; font-size: 13px; }

.bar { height: 6px; border-radius: 99px; background: var(--line); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: inherit; transition: width .25s; }
.complete .bar > span, .bar.done > span { background: var(--good); }

/* ---------- set header ---------- */
.set-header { display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: flex-end; justify-content: space-between; margin-bottom: 14px; }
.set-header h2 { margin: 0 0 4px; font-size: 24px; }
.set-header .meta { color: var(--muted); font-size: 13px; }
.set-header .meta a { font-size: 13px; }
.set-stats { display: flex; gap: 18px; align-items: center; }
.stat { text-align: center; }
.stat b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.ring { --p: 0; width: 64px; height: 64px; border-radius: 50%; display: grid; place-items: center;
  background: conic-gradient(var(--accent) calc(var(--p) * 1%), var(--line) 0); }
.ring.done { background: conic-gradient(var(--good) calc(var(--p) * 1%), var(--line) 0); }
.ring::before { content: attr(data-label); display: grid; place-items: center; width: 50px; height: 50px; border-radius: 50%; background: var(--bg); font-weight: 700; font-size: 14px; }
.set-actions { display: flex; gap: 8px; }
.btn { height: 36px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel-2); font-size: 13px; }
.btn:hover { border-color: var(--accent); }

/* ---------- filters ---------- */
.filters {
  position: sticky; top: var(--header-h); z-index: 10;
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  padding: 10px 0; margin-bottom: 4px; background: var(--bg);
}
.filters .main-search { flex: 1 1 220px; }
.search-wrap { position: relative; display: flex; flex: 1; min-width: 0; }
.search-wrap input { flex: 1; width: 100%; padding-right: 40px; }
.search-wrap input::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; }
.clear-search {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 32px; height: 32px; display: grid; place-items: center;
  border: 0; border-radius: 8px; background: none; color: var(--muted);
}
.clear-search:hover { background: var(--line); color: var(--text); }
.clear-search svg { width: 18px; height: 18px; }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; height: 34px; padding: 0 14px; border-radius: 99px; border: 1px solid var(--line); background: var(--panel); font-size: 13px; }
.chip.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.selects { display: flex; gap: 8px; }
.result-count { color: var(--muted); font-size: 13px; margin: 4px 0 12px; }

/* ---------- grid / cards ---------- */
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
.grid-more { display: block; margin: 18px auto 0; }
.group-title { grid-column: 1 / -1; margin: 18px 0 0; font-size: 16px; color: var(--muted); display: flex; justify-content: space-between; border-bottom: 1px solid var(--line); padding-bottom: 6px; }
.group-title:first-child { margin-top: 0; }
.group-title small { font-size: 12px; font-weight: 400; margin-left: 6px; }
.card {
  position: relative; display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; content-visibility: auto; contain-intrinsic-size: 180px 330px;
}
.card.owned { border-color: rgba(95, 182, 122, .55); }
.card .img {
  position: relative; aspect-ratio: 1; background: #fff; border: 0; padding: 0; display: block; width: 100%; overflow: hidden;
}
.card .img img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; }
.card .img.broken img { display: none; }
.card .img.broken::after { content: "No image"; position: absolute; inset: 0; display: grid; place-items: center; color: #888; font-size: 13px; }
.card:not(.owned) .img img { filter: grayscale(.85) opacity(.55); }
.card .num {
  position: absolute; top: 8px; left: 8px;
  min-width: 30px; height: 26px; padding: 0 8px; border-radius: 99px;
  display: grid; place-items: center;
  background: rgba(22, 19, 26, .85); color: var(--text); font-size: 12px; font-weight: 600; border: 0;
}
.card.owned .num { background: var(--good); color: #0b1a10; }
.card .wish {
  position: absolute; top: 4px; right: 4px; width: 36px; height: 36px; border: 0; border-radius: 50%;
  display: grid; place-items: center; background: none; color: #6b6275;
}
.card .wish svg { width: 20px; height: 20px; }
.card .wish.on { color: var(--accent); }
.card .wish.on svg { fill: currentColor; }
.card .body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.card .name { font-weight: 600; font-size: 14px; line-height: 1.25; color: var(--text); text-decoration: none; }
.card .name:hover { text-decoration: underline; }
.ext { width: 12px; height: 12px; margin-left: 4px; vertical-align: -1px; color: var(--muted); }
.source-link { font-size: 14px; text-decoration: none; }
.source-link:hover { text-decoration: underline; }
.source-link .ext { color: currentColor; }
.card .tags { display: flex; flex-wrap: wrap; gap: 4px; font-size: 11px; }
.tag { padding: 2px 7px; border-radius: 6px; background: var(--panel-2); color: var(--muted); }
.tag.rarity { color: #111; font-weight: 600; }
.tag.set { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.counter { margin-top: auto; display: flex; align-items: center; justify-content: space-between; background: var(--panel-2); border-radius: 10px; }
.counter button { width: 44px; height: 38px; border: 0; background: none; font-size: 20px; line-height: 1; border-radius: 10px; }
.counter button:hover { background: var(--line); }
.counter button:disabled { opacity: .3; cursor: default; background: none; }
.counter output { font-weight: 700; font-variant-numeric: tabular-nums; }
.card.owned .counter output { color: var(--good); }

.r-common { background: var(--r-common); }
.r-uncommon { background: var(--r-uncommon); }
.r-rare { background: var(--r-rare); color: #fff !important; }
.r-veryrare { background: var(--r-veryrare); color: #fff !important; }
.r-unique { background: var(--r-unique); }
.r-promo { background: var(--r-promo); }
.r-other { background: var(--muted); }

.empty { text-align: center; color: var(--muted); padding: 40px 0; }
.foot { margin-top: 40px; color: var(--muted); font-size: 12px; }

/* ---------- detail overlay ---------- */
.detail-overlay {
  position: fixed; inset: 0; z-index: 60; overflow-y: auto; overscroll-behavior: contain;
  display: flex; align-items: flex-start; justify-content: center;
  padding: calc(12px + var(--safe-top)) 12px 12px; background: rgba(0,0,0,.6);
}
.detail {
  position: relative; width: min(520px, 100%); margin: auto 0; overflow: hidden;
  border: 1px solid var(--line); border-radius: 16px; background: var(--panel); color: var(--text);
}
.detail .close { position: absolute; top: 8px; right: 8px; background: rgba(22,19,26,.8); z-index: 1; }
.detail img { width: 100%; max-height: 60vh; object-fit: contain; background: #fff; display: block; }
.detail .info { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.detail h3 { margin: 0; font-size: 20px; }
.also-listed { margin: 0; font-size: 13px; color: var(--muted); }
.corrected-note { margin: 0; font-size: 13px; color: var(--muted); border-left: 3px solid var(--accent); padding-left: 8px; }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 50; padding: 10px 16px; border-radius: 10px;
  background: var(--panel-2); border: 1px solid var(--line); box-shadow: 0 8px 24px rgba(0,0,0,.4);
  display: flex; gap: 12px; align-items: center; font-size: 14px; max-width: calc(100vw - 32px);
}
.toast button { background: none; border: 0; color: var(--accent); font-weight: 600; }

.only-mobile { display: none; }
.scrim { display: none; }

/* ---------- accounts ---------- */
.login-btn {
  height: 36px; padding: 0 16px; border-radius: 99px; flex: none;
  border: 1px solid var(--accent); background: none; color: var(--accent); font-weight: 600; font-size: 14px;
}
.login-btn:hover { background: var(--accent); color: var(--accent-ink); }
.avatar {
  position: relative; width: 36px; height: 36px; flex: none; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 15px;
}
.sync-dot { position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--bg); background: var(--good); }
.sync-dot.saving { background: var(--accent); animation: pulse 1s infinite alternate; }
.sync-dot.error { background: var(--danger); }
@keyframes pulse { to { opacity: .35; } }
.menu-account { padding: 12px 16px; border-bottom: 1px solid var(--line); font-size: 13px; }
.menu-account .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.menu-account .sync-text { color: var(--muted); margin-top: 2px; }
.menu-auth { display: flex; gap: 8px; margin-top: 8px; }
.menu .menu-auth button { flex: 1; padding: 8px; border: 1px solid var(--line); border-radius: 8px; text-align: center; font-size: 13px; }

.guest-banner {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 12px 8px 16px; font-size: 14px;
  background: linear-gradient(90deg, rgba(217,164,65,.18), rgba(217,164,65,.06));
  border-bottom: 1px solid rgba(217,164,65,.35);
}
.guest-banner span { flex: 1; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.block { width: 100%; height: 44px; font-size: 15px; }
.btn:disabled { opacity: .6; cursor: default; }

/* ---------- log in / sign up / delete account pages ---------- */
.auth-page .brand { color: var(--text); text-decoration: none; }
.auth-main { max-width: 420px; margin: 0 auto; padding: 20px 16px 40px; }
.back-link { display: inline-block; margin-bottom: 14px; font-size: 14px; text-decoration: none; }
.auth-card {
  display: flex; flex-direction: column; gap: 14px; padding: 20px;
  border: 1px solid var(--line); border-radius: 16px; background: var(--panel);
}
.auth-tabs { display: flex; gap: 4px; padding: 4px; background: var(--panel-2); border-radius: 10px; }
.auth-tabs a {
  flex: 1; display: grid; place-items: center; height: 36px; border-radius: 8px;
  color: var(--muted); font-size: 14px; text-decoration: none; white-space: nowrap;
}
.auth-tabs a[aria-current=page] { background: var(--bg); color: var(--text); font-weight: 600; }
.auth-card h2 { margin: 4px 0 0; font-size: 22px; }
.auth-sub { margin: -8px 0 0; color: var(--muted); font-size: 14px; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.auth-card input {
  height: 44px; padding: 0 12px; font-size: 16px; color: var(--text);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; outline: none;
}
.auth-card input:focus { border-color: var(--accent); }
.auth-error { margin: 0; color: var(--danger); font-size: 14px; }

/* ---------- mobile ---------- */
@media (max-width: 860px) {
  .only-mobile { display: inline-grid; }
  .topbar { padding: var(--safe-top) 8px 0; gap: 6px; }
  .brand .sub { display: none; }
  .overall .bar { display: none; }
  .overall .promo-count { padding-left: 8px; }
  .overall .promo-count .lbl { display: none; }
  .overall .promo-count .ico { display: inline; margin-right: 3px; color: var(--accent); }
  .overall { font-size: 12px; }
  .login-btn { height: 34px; padding: 0 12px; font-size: 13px; }
  .guest-banner { flex-wrap: wrap; font-size: 13px; padding: 10px 8px 10px 16px; }
  .guest-banner span { flex-basis: calc(100% - 52px); }
  .guest-banner .btn { order: 3; margin-bottom: 2px; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
    width: min(340px, 86vw); height: 100%;
    transform: translateX(-100%); transition: transform .2s ease;
    padding-top: var(--safe-top);
  }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.6); }
  .scrim { display: block; position: fixed; inset: 0; z-index: 35; background: rgba(0,0,0,.5); }
  .main { padding: 12px 16px 32px; }
  .set-header h2 { font-size: 20px; }
  /* Too tall to pin on a phone; scrolls away with the page instead. */
  .filters { gap: 8px; position: static; }
  .filters .main-search { flex-basis: 100%; }
  .chips { width: 100%; }
  .selects { width: 100%; }
  .selects select { flex: 1; padding: 0 8px; font-size: 13px; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .card { contain-intrinsic-size: 160px 300px; }
  .card .body { padding: 8px 10px 10px; }
  .card .name { font-size: 13px; }
  .counter button { width: 44px; height: 40px; }
}
@media (max-width: 480px) {
  .brand h1 { font-size: 15px; white-space: nowrap; }
}
@media (max-width: 400px) {
  .brand h1 { display: none; }
}
@media (min-width: 560px) and (max-width: 860px) {
  .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
