/* ===== Tokens ===== */
:root {
  --dex-red: #c8102e;
  --dex-red-dark: #8f0b20;
  --dex-blue: #3fa9f5;
  --bg: #eef0f3;
  --surface: #ffffff;
  --surface-2: #f5f6f8;
  --text: #1d2228;
  --muted: #6b7380;
  --line: #dde1e6;
  --ok: #2f9e5b;
  --danger: #c8102e;
  --shadow: 0 1px 2px rgb(0 0 0 / .08), 0 4px 14px rgb(0 0 0 / .06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171b;
    --surface: #1e2227;
    --surface-2: #262b31;
    --text: #eef1f4;
    --muted: #9aa3ae;
    --line: #333a42;
    --shadow: 0 1px 2px rgb(0 0 0 / .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14171b; --surface: #1e2227; --surface-2: #262b31; --text: #eef1f4;
  --muted: #9aa3ae; --line: #333a42; --shadow: 0 1px 2px rgb(0 0 0 / .4); color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
a { color: var(--dex-blue); }
[hidden] { display: none !important; }

/* ===== Top bar (Pokédex) ===== */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: linear-gradient(180deg, var(--dex-red), #b00d27);
  color: #fff;
  padding: calc(env(safe-area-inset-top) + 10px) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  box-shadow: 0 2px 0 var(--dex-red-dark), 0 6px 16px rgb(0 0 0 / .2);
}
.topbar-row { display: flex; align-items: center; gap: 12px; }
.lens {
  flex: none; width: 46px; height: 46px; border-radius: 50%;
  background: #fff; display: grid; place-items: center;
  box-shadow: inset 0 -2px 0 rgb(0 0 0 / .15);
}
.lens span {
  width: 36px; height: 36px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #bfe6ff 0 18%, var(--dex-blue) 40%, #1b6fb0 100%);
  border: 2px solid #1a4f7a;
}
.lights { display: flex; gap: 5px; align-self: flex-start; padding-top: 2px; }
.lights i { width: 10px; height: 10px; border-radius: 50%; border: 1px solid rgb(0 0 0 / .35); }
.lights i:nth-child(1) { background: #ff5a5a; }
.lights i:nth-child(2) { background: #ffd23f; }
.lights i:nth-child(3) { background: #4cd964; }
.brand { flex: 1; min-width: 0; }
.brand h1 { margin: 0; font-size: 20px; letter-spacing: .3px; line-height: 1.1; }
.brand h1 span { font-weight: 500; opacity: .85; }
.progress { display: flex; align-items: center; gap: 8px; margin-top: 4px; font-size: 12px; }
.progress .bar { flex: 1; max-width: 220px; height: 6px; border-radius: 3px; background: rgb(0 0 0 / .25); overflow: hidden; }
.progress .fill { height: 100%; width: 0; background: #ffd23f; transition: width .3s; }
.icon-btn {
  flex: none; width: 40px; height: 40px; border-radius: 12px; border: 0;
  background: rgb(255 255 255 / .16); color: #fff; display: grid; place-items: center;
}
.icon-btn:active { background: rgb(255 255 255 / .3); }
.icon-btn.dark { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: .35; }

.search-row { display: flex; gap: 8px; margin-top: 10px; }
.search {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px;
  background: #fff; color: #444; border-radius: 12px; padding: 0 10px; min-height: 42px;
}
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: #1d2228; font-size: 16px; padding: 8px 0; }
.filter-btn {
  flex: none; border: 0; border-radius: 12px; padding: 0 14px; min-height: 42px;
  background: var(--dex-red-dark); color: #fff; font-weight: 600; display: flex; align-items: center; gap: 6px;
}
.filter-btn[aria-expanded="true"] { background: #fff; color: var(--dex-red); }
.badge { background: #ffd23f; color: #222; border-radius: 10px; padding: 0 7px; font-size: 12px; font-weight: 700; }

.filters {
  margin-top: 10px; background: var(--surface); color: var(--text);
  border-radius: var(--radius); padding: 12px; max-height: 70vh; overflow: auto;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 2px solid transparent; border-radius: 999px; padding: 4px 10px; font-size: 13px; font-weight: 600;
  background: var(--tc); color: #fff; opacity: .55; text-shadow: 0 1px 1px rgb(0 0 0 / .3);
}
.chip[aria-pressed="true"] { opacity: 1; border-color: var(--text); }
.filter-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.filter-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 600; }
.filter-grid .wide { grid-column: 1 / -1; }
.filter-grid select, .filter-grid input, .set-group select, .set-group input[type="number"] {
  width: 100%; min-height: 40px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--text); padding: 6px 10px; font-size: 16px;
}
.filter-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

.btn {
  border: 0; border-radius: 12px; padding: 10px 16px; min-height: 42px; font-weight: 600;
  background: var(--dex-red); color: #fff; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
.btn.ok { background: var(--ok); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid currentColor; }
.btn.small { min-height: 34px; padding: 6px 12px; font-size: 14px; }
.btn:active { filter: brightness(.92); }

/* ===== Grid / album pages ===== */
main { padding: 14px max(16px, env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
.status { color: var(--muted); text-align: center; margin: 8px 0 14px; font-size: 13px; }
.pages {
  display: grid; gap: 16px;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
}
.page { background: var(--surface); border-radius: 18px; padding: 10px; box-shadow: var(--shadow); }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin: 0 4px 8px; font-size: 13px; }
.page-head b { font-size: 14px; }
.page-head span { color: var(--muted); }
.page-head .done { color: var(--ok); font-weight: 700; }
.page-grid { display: grid; grid-template-columns: repeat(var(--cols, 3), 1fr); gap: 8px; }
.flat { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); }

/* Tile = mini card (63×88) */
.tile {
  position: relative; aspect-ratio: 63 / 88; border-radius: 10px; padding: 4px;
  background: linear-gradient(145deg, var(--c1), var(--c2));
  box-shadow: 0 1px 3px rgb(0 0 0 / .25); cursor: pointer; overflow: hidden;
  display: flex; flex-direction: column; container-type: inline-size;
  -webkit-tap-highlight-color: transparent; user-select: none;
}
.tile:active { transform: scale(.97); }
.tile-in {
  flex: 1; min-height: 0; display: flex; flex-direction: column;
  background: #fbfaf5; color: #1d2228; border-radius: 7px; padding: 4px 5px 4px; overflow: hidden;
}
.tile-top { display: flex; justify-content: space-between; align-items: center; gap: 2px; }
.tile-no { font-weight: 700; font-size: clamp(9px, 9cqi, 13px); color: #555; font-variant-numeric: tabular-nums; }
.tile-img {
  flex: 1; min-height: 0; margin: 2px 0; border-radius: 5px;
  background: radial-gradient(circle at 50% 55%, #fff 0 30%, color-mix(in srgb, var(--c1) 35%, #fff) 100%);
  display: grid; place-items: center; overflow: hidden;
}
.tile-img img { width: 100%; height: 100%; object-fit: contain; image-rendering: pixelated; }
.tile-img img.hd { image-rendering: auto; padding: 6%; }
.tile-name { font-weight: 700; font-size: clamp(10px, 10.5cqi, 15px); line-height: 1.15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-types { display: flex; gap: 2px; margin-top: 2px; }
.tile-types i { font-style: normal; flex: 1; text-align: center; border-radius: 4px; background: var(--tc); color: #fff; font-size: clamp(7px, 7.5cqi, 11px); font-weight: 700; padding: 1px 0; text-shadow: 0 1px 1px rgb(0 0 0 / .3); overflow: hidden; }
.tile-meta { font-size: clamp(7px, 7.5cqi, 11px); color: #555; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-foot { display: flex; justify-content: space-between; font-size: clamp(7px, 7.5cqi, 11px); color: #777; white-space: nowrap; gap: 3px; }
.tile-foot b { color: #333; overflow: hidden; text-overflow: ellipsis; }
.tile-check {
  width: 24px; height: 24px; margin: -3px -3px 0 0; border-radius: 50%; border: 2px solid #c9ccd1; background: #fff;
  display: grid; place-items: center; padding: 0; color: transparent; font-size: 13px; font-weight: 900; flex: none;
}
.tile.owned .tile-check { background: var(--ok); border-color: var(--ok); color: #fff; }
.tile.owned::after {
  content: ""; position: absolute; inset: 0; border-radius: 10px; box-shadow: inset 0 0 0 3px var(--ok); pointer-events: none;
}
.tile.ghost {
  background: transparent; box-shadow: none; border: 2px dashed var(--line); cursor: default;
  display: grid; place-items: center; color: var(--muted); font-size: 12px; font-weight: 600;
}
.tile.ghost:active { transform: none; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }

/* ===== Detail overlay ===== */
.detail {
  position: fixed; inset: 0; z-index: 40; background: var(--bg);
  display: flex; flex-direction: column;
}
.detail-bar {
  display: flex; align-items: center; gap: 10px; color: #fff;
  background: var(--dex-red);
  padding: calc(env(safe-area-inset-top) + 8px) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
}
.detail-pos { flex: 1; text-align: center; font-size: 13px; opacity: .9; font-variant-numeric: tabular-nums; }
.detail-nav { display: flex; gap: 6px; }
.detail-scroll { flex: 1; overflow-y: auto; overflow-x: hidden; touch-action: pan-y; -webkit-overflow-scrolling: touch; }
.detail-inner {
  max-width: 720px; margin: 0 auto;
  padding: 16px max(16px, env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  will-change: transform;
}
.detail-inner.anim { transition: transform .22s ease, opacity .22s ease; }

/* Big card template */
.bigcard {
  width: min(100%, 360px); margin: 0 auto; aspect-ratio: 63 / 88; border-radius: 18px; padding: 10px;
  background: linear-gradient(145deg, var(--c1), var(--c2));
  box-shadow: 0 6px 24px rgb(0 0 0 / .25); display: flex; flex-direction: column;
  container-type: inline-size;
}
.bigcard-in {
  flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 2.4cqi;
  background: #fbfaf5; color: #1d2228; border-radius: 12px; padding: 3.5cqi;
}
.bc-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.bc-head h2 { margin: 0; font-size: 7.4cqi; line-height: 1.05; }
.bc-no { font-weight: 800; font-size: 5.2cqi; color: #666; font-variant-numeric: tabular-nums; }
.bc-art {
  flex: 1; min-height: 0; border-radius: 8px; border: 3px solid color-mix(in srgb, var(--c1) 70%, #000);
  background: radial-gradient(circle at 50% 60%, #fff 0 25%, color-mix(in srgb, var(--c1) 45%, #fff) 100%);
  display: grid; place-items: center; overflow: hidden; position: relative;
}
.bc-art img { width: 92%; height: 92%; object-fit: contain; }
.bc-art .gen { position: absolute; left: 6px; bottom: 4px; font-size: 3.4cqi; font-weight: 700; color: rgb(0 0 0 / .45); }
.bc-types { display: flex; gap: 6px; }
.bc-types i { font-style: normal; flex: 1; text-align: center; border-radius: 6px; padding: .8cqi 0; background: var(--tc); color: #fff; font-weight: 800; font-size: 4.2cqi; text-shadow: 0 1px 1px rgb(0 0 0 / .3); }
.bc-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin: 0; }
.bc-stats div { background: #efede4; border-radius: 6px; padding: 1cqi 1.6cqi; text-align: center; }
.bc-stats dt { font-size: 3.1cqi; color: #777; text-transform: uppercase; letter-spacing: .3px; }
.bc-stats dd { margin: 0; font-size: 4.4cqi; font-weight: 800; }
.bc-foot { display: flex; justify-content: space-between; font-size: 3.4cqi; color: #666; border-top: 1px solid #e3e0d4; padding-top: 1.6cqi; }

.own-toggle {
  display: flex; width: min(100%, 360px); margin: 14px auto 0; min-height: 50px; font-size: 16px;
}
.own-toggle.is-owned { background: var(--ok); }

.section { background: var(--surface); border-radius: 16px; padding: 12px; margin-top: 16px; box-shadow: var(--shadow); }
.section > h3 { margin: 0 0 8px; font-size: 16px; display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.section > h3 small { font-weight: 500; color: var(--muted); font-size: 13px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.summary-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.summary-chips button {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 4px 10px; font-size: 13px;
}
.summary-chips button b { margin-left: 4px; }
.summary-chips button[aria-pressed="true"] { background: var(--dex-red); border-color: var(--dex-red); color: #fff; }
.card-filter { width: 100%; min-height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); padding: 6px 10px; font-size: 16px; margin-bottom: 8px; }

.clist { list-style: none; margin: 0; padding: 0; }
.clist .series { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); padding: 10px 2px 4px; }
.crow { display: flex; align-items: center; gap: 10px; padding: 6px 2px; border-top: 1px solid var(--line); }
.crow:first-child, .series + .crow { border-top: 0; }
.crow .thumb {
  width: 46px; aspect-ratio: 63 / 88; flex: none; border-radius: 4px; background: var(--surface-2); object-fit: cover; border: 0; padding: 0;
}
.crow .info { flex: 1; min-width: 0; background: none; border: 0; padding: 0; text-align: left; }
.crow .info b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crow .info > span { display: block; font-size: 12px; color: var(--muted); }
.crow .num { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--text); }
.crow .have { flex: none; background: var(--ok); color: #fff; border-radius: 999px; font-size: 12px; font-weight: 700; padding: 2px 8px; }
.crow .act { flex: none; width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); font-size: 20px; line-height: 1; }
.crow .act.rm { color: var(--danger); font-size: 18px; }
.crow select { max-width: 130px; min-height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface-2); font-size: 14px; }
.crow.hl { background: color-mix(in srgb, #ffd23f 22%, transparent); border-radius: 8px; }
.more { width: 100%; margin-top: 8px; }

/* ===== Sheets (dialog) ===== */
dialog.sheet {
  border: 0; padding: 0; background: var(--surface); color: var(--text);
  width: min(560px, 100vw); max-width: 100vw; max-height: 92dvh; border-radius: 20px 20px 0 0;
  margin: auto auto 0; box-shadow: 0 -8px 30px rgb(0 0 0 / .3);
}
@media (min-width: 640px) { dialog.sheet { margin: auto; border-radius: 20px; } }
dialog.sheet::backdrop { background: rgb(0 0 0 / .5); }
.sheet-body { padding: 16px max(16px, env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
.sheet-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 8px; }
.sheet-head h2 { margin: 0; font-size: 19px; }
.set-group { border-top: 1px solid var(--line); padding: 12px 0; }
.set-group h3 { margin: 0 0 8px; font-size: 15px; }
.row { display: flex; flex-direction: column; gap: 4px; margin: 8px 0; font-size: 14px; }
.row.check { flex-direction: row; align-items: center; gap: 10px; }
.row.check input { width: 22px; height: 22px; accent-color: var(--dex-red); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.btn.file { position: relative; overflow: hidden; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.card-view { display: grid; gap: 12px; }
.card-view .big { width: min(100%, 300px); margin: 0 auto; aspect-ratio: 63 / 88; border-radius: 12px; background: var(--surface-2); display: block; object-fit: contain; }
.card-view h2 { margin: 0; font-size: 18px; }
.card-view .meta { color: var(--muted); font-size: 14px; }
.card-view .add-row { display: flex; gap: 8px; }
.card-view .add-row select { flex: 1; min-height: 44px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2); padding: 0 10px; font-size: 16px; }

/* ===== Toast ===== */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translate(-50%, 30px);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 100;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 380px) {
  .lights { display: none; }
  .page { padding: 8px; }
  .page-grid { gap: 6px; }
}

/* ===== Widoki: Pokédex / Mój album ===== */
.views {
  display: flex; gap: 4px; margin-top: 10px; padding: 3px; border-radius: 12px;
  background: rgb(0 0 0 / .22); width: fit-content;
}
.views button {
  border: 0; border-radius: 9px; padding: 6px 14px; font-weight: 600; font-size: 14px;
  background: transparent; color: #fff; opacity: .85;
}
.views button[aria-selected="true"] { background: #fff; color: var(--dex-red); opacity: 1; }
body.album-mode .filter-btn, body.album-mode #filters { display: none !important; }

/* Przycisk czyszczenia wyszukiwania */
.clear-btn {
  flex: none; border: 0; background: transparent; color: #9aa0a8; padding: 0;
  width: 32px; height: 32px; margin-right: -6px; display: grid; place-items: center;
}
.clear-btn:active { color: #555; }

/* Licznik posiadanych kart na kafelku */
.cnt {
  margin-left: auto; margin-right: 3px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: #ffd23f; color: #222; font-size: 11px; font-weight: 800; line-height: 18px; text-align: center;
  box-shadow: 0 0 0 1.5px #fff;
}

/* Mój album: brakujące wyszarzone */
.tile.missing { filter: grayscale(.9); opacity: .45; }
.tile.missing:hover { opacity: .7; }

/* Mój album: kafelek z obrazkiem posiadanej karty (nakładka na zwykły kafelek) */
.tile .cardimg-img { position: absolute; inset: 0; z-index: 1; width: 100%; height: 100%; object-fit: cover; border-radius: 10px; background: #2a2f36; }
.tile .cardimg-no { display: none; }
.tile.cardimg .cardimg-no {
  display: block; position: absolute; z-index: 2; left: 4px; bottom: 4px; background: rgb(0 0 0 / .65); color: #fff;
  font-size: 10px; font-weight: 700; padding: 1px 5px; border-radius: 6px; font-variant-numeric: tabular-nums;
}
.tile.cardimg .tile-top { position: absolute; z-index: 2; top: 7px; left: 7px; right: 7px; }
.tile.cardimg .tile-no { visibility: hidden; }

/* Skok do stworka w albumie */
.tile.flash { animation: flash 1.4s ease; }
@keyframes flash {
  0%, 60% { box-shadow: 0 0 0 4px #ffd23f, 0 0 18px 4px #ffd23f; transform: scale(1.04); }
  100% { box-shadow: 0 1px 3px rgb(0 0 0 / .25); transform: none; }
}

/* Sortowanie po typie: grupy */
.group { background: var(--surface); border-radius: 18px; padding: 10px; box-shadow: var(--shadow); margin-bottom: 16px; }
.type-head { background: var(--tc); color: #fff; padding: 2px 10px; border-radius: 8px; text-shadow: 0 1px 1px rgb(0 0 0 / .3); }
