/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ── Theme Variables ── */
:root, [data-theme="dark"] {
  --bg: #14141a;
  --surface: #1f1f24;
  --elevated: #29292e;
  --gold: #d9bf73;
  --text: #e8e8ed;
  --text-secondary: #8e8e93;
  --red: #ff453a;
  --green: #30d158;
  --orange: #ff9f0a;
  --purple: #bf5af2;
  --blue: #0a84ff;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 12px rgba(0,0,0,0.3);
  --shadow-hover: 0 12px 32px rgba(0,0,0,0.5);
  --modal-bg: rgba(0,0,0,0.75);
}

[data-theme="light"] {
  --bg: #f2f2f7;
  --surface: #ffffff;
  --elevated: #e5e5ea;
  --gold: #9a7d2e;
  --text: #1c1c1e;
  --text-secondary: #6c6c70;
  --red: #ff3b30;
  --green: #34c759;
  --orange: #ff9500;
  --purple: #af52de;
  --blue: #007aff;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --shadow-hover: 0 12px 32px rgba(0,0,0,0.15);
  --modal-bg: rgba(0,0,0,0.5);
}

html { font-size: 15px; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  transition: background 0.3s, color 0.3s;
}

/* ── Header ── */
header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg);
  border-bottom: 1px solid var(--elevated);
  padding: 14px 20px 12px;
  transition: background 0.3s;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

h1 {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--gold);
  letter-spacing: -0.03em;
}

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--elevated);
  color: var(--text);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s, transform 0.15s;
}

.icon-btn:hover { background: var(--elevated); transform: scale(1.05); }

/* ── Stats Banner ── */
.stats-banner {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.stat-box {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 10px 8px;
  text-align: center;
  border: 1px solid var(--elevated);
}

.stat-num {
  display: block;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--gold);
  line-height: 1.2;
}

.stat-label {
  display: block;
  font-size: 0.65rem;
  color: var(--text-secondary);
  margin-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
}

/* ── Filter Chips ── */
.filter-chips {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.filter-chips::-webkit-scrollbar { display: none; }

.chip {
  flex-shrink: 0;
  padding: 5px 14px;
  border-radius: 20px;
  border: 1px solid var(--elevated);
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.chip:hover { border-color: var(--gold); color: var(--text); }
.chip.active { background: var(--gold); color: #14141a; border-color: var(--gold); }

/* ── Controls ── */
.controls {
  display: flex;
  gap: 8px;
}

#search {
  flex: 1;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--elevated);
  background: var(--surface);
  color: var(--text);
  font-size: 0.87rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s, background 0.3s;
}

#search::placeholder { color: var(--text-secondary); }
#search:focus { border-color: var(--gold); }

#sort {
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--elevated);
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  transition: background 0.3s;
}

/* ── Gallery ── */
main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px;
}

/* Grid layout gives every card the same-sized slot regardless of image
 * aspect ratio or text length. auto-fill + minmax scales column count
 * to the viewport; `grid-auto-rows` with an explicit pixel height
 * guarantees every row is exactly that tall, independent of image
 * loading state, letterboxing, or any browser quirks with aspect-ratio.
 * Media queries below override these values at smaller breakpoints. */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  grid-auto-rows: 420px;
  gap: 12px;
}

/* ── Card ── */
/* Fills the grid cell exactly. card-body is a fixed 84px footer and
 * card-image flexes to take everything above it, so the split is
 * deterministic per row height. */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.3s;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(20px);
  display: flex;
  flex-direction: column;
  height: 100%;
}

.card.visible {
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.4s ease, transform 0.4s ease, box-shadow 0.2s ease;
}

.card:hover {
  transform: translateY(-3px) scale(1.015);
  box-shadow: var(--shadow-hover);
}

.card:hover .card-image img {
  transform: scale(1.04);
}

/* Touch devices: hover fires-on-tap and the lift/zoom animation lingers
 * during scroll, which feels bad. Disable hover-only effects there. */
@media (hover: none) {
  .card:hover {
    transform: none;
    box-shadow: var(--shadow);
  }
  .card:hover .card-image img { transform: none; }
}

/* Suppress the iOS / Android tap-flash so card taps feel intentional. */
.card {
  -webkit-tap-highlight-color: transparent;
}

/* Flex-1 takes whatever space remains above the fixed-height body.
 * object-fit: contain letterboxes portrait, landscape, and slab
 * photos all into the same box without stretching or cropping. */
.card-image {
  position: relative;
  width: 100%;
  flex: 1;
  min-height: 0;
  background: var(--elevated);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  transition: transform 0.3s ease;
}

.card-image .placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  font-size: 2rem;
  opacity: 0.3;
}

/* ── Card Body ── */
/* Fixed 84px footer — height + text truncation keep this region
 * uniform regardless of player/set/badge content. */
.card-body {
  padding: 10px 12px 12px;
  height: 84px;
  flex: 0 0 auto;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.card-player {
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 0;
}

.card-set {
  font-size: 0.75rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 0;
  margin-bottom: 4px;
}

.card-badges {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px;
  overflow: hidden;
  flex-shrink: 0;
}

.badge {
  display: inline-block;
  font-size: 0.65rem;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 10px;
  line-height: 1.4;
}

.badge-grade { background: rgba(255, 69, 58, 0.15); color: var(--red); }
.badge-parallel { background: rgba(191, 90, 242, 0.15); color: var(--purple); }
.badge-number { background: rgba(142, 142, 147, 0.12); color: var(--text-secondary); font-weight: 500; }
.badge-auto { background: rgba(255, 159, 10, 0.15); color: var(--orange); }

/* ── Loading Skeletons ── */
.loading { padding: 0; }

.skeleton-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  grid-auto-rows: 420px;
  gap: 12px;
}

.skeleton-card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  animation: shimmer 1.5s ease-in-out infinite;
}

.skel-img {
  width: 100%;
  aspect-ratio: 3/4;
  background: var(--elevated);
}

.skel-body { padding: 12px; }

.skel-line {
  height: 12px;
  background: var(--elevated);
  border-radius: 6px;
  margin-bottom: 8px;
}
.skel-line.w70 { width: 70%; }
.skel-line.w50 { width: 50%; }

.skel-badges { display: flex; gap: 4px; }
.skel-badge {
  width: 50px;
  height: 16px;
  background: var(--elevated);
  border-radius: 8px;
}

@keyframes shimmer {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 0.35; }
}

/* ── Empty ── */
.empty {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-secondary);
  font-size: 1rem;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: var(--modal-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.modal-overlay.open { opacity: 1; }

.modal {
  background: var(--surface);
  border-radius: 16px;
  max-width: 720px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
  box-shadow: 0 24px 64px rgba(0,0,0,0.5);
  transform: scale(0.95) translateY(10px);
  transition: transform 0.25s ease;
}

.modal-overlay.open .modal {
  transform: scale(1) translateY(0);
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 10;
  width: 32px;
  height: 32px;
  font-size: 1.2rem;
  line-height: 1;
  background: var(--elevated);
  border: none;
}

.modal-body {
  display: flex;
  flex-direction: column;
}

.modal-images {
  display: flex;
  gap: 2px;
  background: var(--elevated);
}

.modal-images img {
  flex: 1;
  min-width: 0;
  max-height: 55vh;
  object-fit: contain;
  display: block;
  background: var(--bg);
}

.modal-info {
  padding: 20px;
}

.modal-player {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 4px;
}
.modal-player .player-link {
  color: var(--gold);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  cursor: pointer;
  transition: opacity 120ms ease;
}
.modal-player .player-link:hover {
  opacity: 0.75;
}
.modal-player .player-link-sep {
  color: var(--text-secondary);
  font-weight: 400;
  margin: 0 4px;
  text-decoration: none;
}

.modal-set {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.modal-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;
}

.modal-badges .badge {
  font-size: 0.8rem;
  padding: 4px 10px;
}

.modal-details {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.modal-detail {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.modal-detail-label {
  font-size: 0.65rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
  margin-bottom: 2px;
}

.modal-detail-value {
  font-size: 0.95rem;
  font-weight: 600;
}

.modal-share {
  margin-top: 16px;
  text-align: right;
}

.share-btn {
  background: var(--elevated);
  border: none;
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-family: inherit;
  font-weight: 500;
  padding: 6px 14px;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.15s;
}

.share-btn:hover { background: var(--gold); color: #14141a; }
.share-btn.copied { background: var(--green); color: #fff; }

/* ── Footer ── */
footer {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-secondary);
  font-size: 0.75rem;
  transition: color 0.3s;
}

/* ── Responsive ── */
/* Grid's auto-fill handles column count automatically; these breakpoints
 * adjust the minmax floor so cards don't get too narrow to read on
 * smaller screens. */
@media (max-width: 800px) {
  .stats-banner { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 600px) {
  .gallery, .skeleton-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    grid-auto-rows: 300px;
    gap: 8px;
  }
  .card-body {
    padding: 8px 10px 10px;
    height: 72px;
  }
  .card-player { font-size: 0.82rem; }
  .card-set { font-size: 0.68rem; }
  .badge { font-size: 0.6rem; padding: 1px 5px; }
  header { padding: 10px 12px; }
  main { padding: 8px; }
  .stats-banner { gap: 6px; }
  .stat-num { font-size: 0.95rem; }
  .stat-box { padding: 8px 4px; }
  /* font-size 16px keeps iOS Safari from auto-zooming on focus */
  #search { font-size: 16px; }
  .modal { border-radius: 12px; }
  .modal-images { flex-direction: column; }
  .modal-images img { max-height: 40vh; }
  .modal-detail { padding: 8px 10px; }
}

/* Very narrow screens (iPhone SE, etc.) — keep two readable columns */
@media (max-width: 380px) {
  .gallery, .skeleton-grid {
    grid-template-columns: repeat(2, 1fr);
    grid-auto-rows: 280px;
  }
}

/* Player-route banner — appears at top of <main> when /p/<slug> is active */
.player-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 16px;
  padding: 14px 18px;
  background: linear-gradient(135deg, var(--accent-bg, rgba(80, 120, 200, 0.12)), transparent);
  border: 1px solid var(--accent-border, rgba(80, 120, 200, 0.25));
  border-radius: 12px;
}
.player-banner-name {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.player-banner-count {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 2px;
}
.player-banner-clear {
  font-size: 0.85rem;
  color: var(--text-secondary);
  text-decoration: none;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--border, rgba(255,255,255,0.15));
  white-space: nowrap;
}
.player-banner-clear:hover {
  background: rgba(255,255,255,0.05);
  color: var(--text);
}

@media (max-width: 600px) {
  .player-banner { padding: 10px 12px; margin-bottom: 10px; }
  .player-banner-name { font-size: 1rem; }
}

/* Category tabs — top-level segmentation (Baseball / Disney / Pokemon / All) */
.category-tabs {
  display: flex;
  gap: 6px;
  margin: 0 0 10px;
  padding: 6px;
  background: var(--surface);
  border-radius: 10px;
  overflow-x: auto;
  scrollbar-width: none;
}
.category-tabs::-webkit-scrollbar { display: none; }
.category-tab {
  flex: 1;
  text-align: center;
  padding: 8px 14px;
  border-radius: 8px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 120ms ease, color 120ms ease;
}
.category-tab:hover { color: var(--text); }
.category-tab.active {
  background: var(--gold);
  color: #14141a;
}
