/* Mobile first: the base styles are the phone layout; wider screens add
   columns in the media queries at the end. */

/* Palette shared with the RafaDex Pokédex: warm cream paper, white cards and
   its red used sparingly for the active/selected state. */
:root {
  --bg: #fdf6ee;
  --surface: #ffffff;
  --surface-2: #f6eee4;
  --text: #333333;
  --text-2: #51493f;
  --muted: #776c60;
  --border: #eee3d5;
  --accent: #e3350d;
  --accent-text: #ffffff;
  --bar: #3567c9;
  --track: #f1e8dc;
  --trade: #1d5bbf;
  --trade-bg: #dde8fb;
  --want: #25d366;
  --want-text: #07361b;
  --shadow: 0 2px 0 rgba(120, 84, 40, 0.07), 0 4px 14px rgba(120, 84, 40, 0.06);
  --nav-height: 64px;
  --font-display: "Baloo 2", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16120e;
    --surface: #211c17;
    --surface-2: #2b241e;
    --text: #f3ece4;
    --text-2: #d6cdc2;
    --muted: #a39787;
    --border: #342c24;
    --accent: #f2603f;
    --bar: #7aa2f0;
    --track: #342c24;
    --trade: #7fb0ff;
    --trade-bg: #16274a;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  --bg: #16120e;
  --surface: #211c17;
  --surface-2: #2b241e;
  --text: #f3ece4;
  --text-2: #d6cdc2;
  --muted: #a39787;
  --border: #342c24;
  --accent: #f2603f;
  --bar: #7aa2f0;
  --track: #342c24;
  --trade: #7fb0ff;
  --trade-bg: #16274a;
  --shadow: none;
}

@font-face {
  font-family: "Baloo 2";
  src: url("assets/baloo2.woff2") format("woff2");
  font-weight: 400 800;
  font-display: swap;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0 16px calc(var(--nav-height) + 24px + env(safe-area-inset-bottom));
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

main,
.hero {
  max-width: 1100px;
  margin: 0 auto;
}

button {
  font: inherit;
  color: inherit;
}

/* ---- Hero ---------------------------------------------------------------- */

.hero {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 24px 0 12px;
}

.hero-icon {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.hero h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.7rem;
  font-weight: 800;
  line-height: 1.1;
}

.hero-updated {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

/* Headings and big numbers speak RafaDex's rounded Baloo 2; running text stays
   in the system font for legibility at small sizes. */
.stat-value,
.panel-title,
.set-name,
.group-name,
.sheet-info h2 {
  font-family: var(--font-display);
  font-weight: 800;
}

/* ---- Home ---------------------------------------------------------------- */

.stat-tiles {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.stat-tile {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 14px;
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.stat-value {
  font-size: 1.9rem;
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  color: var(--muted);
  font-size: 0.82rem;
}

.panel {
  margin-bottom: 16px;
  padding: 16px;
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel-title {
  margin: 0 0 4px;
  font-size: 1.1rem;
}

.panel-hint {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.8rem;
}

.set-list {
  display: flex;
  flex-direction: column;
}

.set-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 0;
  border: none;
  border-top: 1px solid var(--border);
  background: none;
  text-align: left;
  cursor: pointer;
}

.set-row:first-child {
  border-top: none;
}

.set-row[hidden],
.set-more-btn[hidden] {
  display: none;
}

.set-more-btn {
  width: 100%;
  margin-top: 4px;
  padding: 12px;
  border: none;
  border-top: 1px solid var(--border);
  background: none;
  color: var(--accent);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 650;
  cursor: pointer;
}

.set-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 88px;
  height: 48px;
  color: var(--muted);
  font-weight: 700;
  font-size: 0.85rem;
}

.set-logo img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.set-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
  min-width: 0;
}

.set-name {
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.set-meta,
.set-counts {
  color: var(--muted);
  font-size: 0.78rem;
}

.set-progress {
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress-track,
.bar-track {
  position: relative;
  flex: 1;
  height: 8px;
  border-radius: 4px;
  background: var(--track);
  overflow: hidden;
}

.progress-fill,
.bar-fill {
  position: absolute;
  inset: 0 auto 0 0;
  border-radius: 4px;
  background: var(--bar);
}

.set-progress-text {
  min-width: 48px;
  color: var(--text-2);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.bar-chart {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(0, 9.5rem) 1fr 2.6rem;
  align-items: center;
  gap: 10px;
}

.bar-row .bar-track {
  height: 12px;
}

.bar-label {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  font-size: 0.85rem;
}

.bar-label-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bar-icon {
  display: inline-flex;
  flex-shrink: 0;
}

.bar-value {
  color: var(--text-2);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.type-icon-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border-radius: 50%;
}

/* ---- Cards --------------------------------------------------------------- */

.filters {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0 -16px;
  padding: 10px 16px;
  background: var(--bg);
}

.filters input {
  width: 100%;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px; /* 16px keeps iOS from zooming into the field */
}

.filter-bar {
  display: flex;
  gap: 8px;
}

.filter-bar input {
  flex: 1;
}

.filter-toggle-btn,
.clear-filters-btn {
  display: inline-flex;
  flex-shrink: 0;
  align-items: center;
  gap: 6px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.size-toggle-btn {
  display: inline-flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  width: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.size-icon {
  width: 18px;
  height: 18px;
}

/* The icon shows the current size: a 3x3 grid when small, 2x2 when large. */
.size-toggle-btn[aria-pressed="true"] .size-icon-small,
.size-toggle-btn[aria-pressed="false"] .size-icon-large {
  display: none;
}

.clear-filters-btn {
  border-color: transparent;
  background: transparent;
  color: var(--accent);
}

.clear-filters-btn[hidden],
.filter-panel[hidden] {
  display: none;
}

.filter-toggle-btn[aria-expanded="true"],
.filter-toggle-active {
  border-color: var(--accent);
}

.filter-icon {
  width: 16px;
  height: 16px;
}

.filter-count:not(:empty) {
  min-width: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.72rem;
  line-height: 18px;
  text-align: center;
}

/* Long collection lists scroll inside the panel instead of pushing the
   cards off a phone screen. */
.filter-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 60vh;
  overflow-y: auto;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.filter-group {
  margin: 0;
  padding: 0;
  border: 0;
}

.filter-group legend {
  margin-bottom: 6px;
  padding: 0;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip-list-sets {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
}

.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
  padding: 4px 8px;
  border: 2px solid transparent;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: opacity 0.12s, border-color 0.12s;
}

.chip-list-filtered .chip {
  opacity: 0.4;
}

.chip-list-filtered .chip[aria-pressed="true"] {
  border-color: var(--accent);
  opacity: 1;
}

@media (hover: hover) {
  .chip:hover {
    opacity: 1;
  }
}

.chip:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.chip-set {
  height: 48px;
}

.chip-set img {
  max-width: 100%;
  max-height: 34px;
  object-fit: contain;
}

.chip-rarity .rarity-badge {
  font-size: 0.8rem;
  line-height: 1.6;
}

.chip-icon {
  display: inline-flex;
}

.chip-text {
  font-size: 0.8rem;
  font-weight: 600;
}

.result-count {
  margin: 4px 0 8px;
  color: var(--muted);
  font-size: 0.82rem;
}

.result-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 0 8px;
}

.result-bar .result-count {
  flex: 1;
  margin: 0;
}

.sort-control {
  display: inline-flex;
  flex-shrink: 0;
  align-items: center;
  gap: 4px;
  color: var(--muted);
}

.sort-icon {
  width: 15px;
  height: 15px;
}

.sort-control select {
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.82rem;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.group-toggle {
  position: relative;
  display: inline-flex;
  flex-shrink: 0;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.82rem;
  cursor: pointer;
  user-select: none;
}

.group-toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.group-toggle-switch {
  position: relative;
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: var(--track);
  box-shadow: inset 0 0 0 1px var(--border);
  transition: background 0.15s;
}

.group-toggle-switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  transition: transform 0.15s;
}

.group-toggle input:checked + .group-toggle-switch {
  background: var(--accent);
  box-shadow: none;
}

.group-toggle input:checked + .group-toggle-switch::after {
  transform: translateX(14px);
}

.group-toggle input:focus-visible + .group-toggle-switch {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Small images are the default (3 per row on a phone); .card-grid-large is
   the toggle's bigger view. */
.card-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.card-grid-large {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.card-grid:not(.card-grid-large) .card {
  padding: 5px;
  border-radius: 9px;
}

.card-grid:not(.card-grid-large) .card-name {
  font-size: 0.76rem;
}

.card-grid:not(.card-grid-large) .card-meta,
.card-grid:not(.card-grid-large) .card-trade {
  font-size: 0.66rem;
}

.card-grid:not(.card-grid-large) .card-trade {
  padding: 1px 6px;
}

.group-header {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.group-header:first-child {
  margin-top: 0;
}

.group-symbol {
  width: 22px;
  height: 22px;
  object-fit: contain;
}

.group-name {
  flex: 1;
  min-width: 0;
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.group-count {
  color: var(--muted);
  font-size: 0.8rem;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 8px;
  border: none;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: left;
  cursor: pointer;
}

.card-image {
  width: 100%;
  aspect-ratio: 0.716;
  object-fit: cover;
  border-radius: 7px;
  background: var(--surface-2);
  margin-bottom: 4px;
}

.card-name-row {
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
}

.card-name {
  font-weight: 600;
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-meta {
  color: var(--muted);
  font-size: 0.76rem;
}

.card-trade {
  align-self: flex-start;
  font-size: 0.76rem;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--trade-bg);
  color: var(--trade);
  font-weight: 650;
}

.empty {
  grid-column: 1 / -1;
  padding: 40px 0;
  color: var(--muted);
  text-align: center;
}

.rarity-badge {
  flex-shrink: 0;
  min-width: 1.6em;
  padding: 0 4px;
  border-radius: 4px;
  font-size: 0.66rem;
  font-weight: 700;
  line-height: 1.6;
  text-align: center;
  color: #111;
  background: #9ca3af;
}

.rarity-badge.rarity-uncommon { background: #4ade80; }
.rarity-badge.rarity-rare { background: #60a5fa; }
.rarity-badge.rarity-holo { background: #818cf8; }
.rarity-badge.rarity-ultra { background: #c084fc; }
.rarity-badge.rarity-ir,
.rarity-badge.rarity-sir { background: #f472b6; }
.rarity-badge.rarity-acespec { background: #fb923c; }
.rarity-badge.rarity-secret { background: #fbbf24; }
.rarity-badge.rarity-promo { background: #374151; color: #eee; }

/* ---- Bottom navigation ---------------------------------------------------- */

.bottom-nav {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  height: calc(var(--nav-height) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.nav-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.75rem;
  cursor: pointer;
}

.nav-icon {
  font-size: 1.3rem;
  line-height: 1;
}

.nav-active {
  color: var(--accent);
  font-weight: 650;
}

.nav-badge {
  position: absolute;
  top: 8px;
  left: calc(50% + 10px);
  min-width: 20px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 18px;
}

.nav-badge:empty {
  display: none;
}

/* ---- Detail sheet ---------------------------------------------------------- */

.sheet {
  width: 100%;
  max-width: 100%;
  max-height: 92vh;
  margin: auto 0 0;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  border: none;
  border-radius: 20px 20px 0 0;
  background: var(--surface);
  color: var(--text);
  overflow: auto;
}

.sheet::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

.sheet-close {
  position: absolute;
  top: 6px;
  right: 10px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 2rem;
  line-height: 1;
  cursor: pointer;
}

.sheet-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.sheet-image {
  width: min(280px, 78vw);
  aspect-ratio: 0.716;
  object-fit: cover;
  border-radius: 12px;
  background: var(--surface-2);
}

.sheet-info {
  width: 100%;
}

.sheet-info h2 {
  margin: 0 0 4px;
}

.sheet-owned {
  margin: 0 0 12px;
  color: var(--muted);
  font-weight: 600;
}

.sheet-owned-trade {
  color: var(--trade);
}

.sheet-owned[hidden] {
  display: none;
}

.want-btn {
  display: block;
  margin-bottom: 14px;
  padding: 13px 18px;
  border-radius: 12px;
  background: var(--want);
  color: var(--want-text);
  font-weight: 700;
  text-align: center;
  text-decoration: none;
}

.want-btn[hidden] {
  display: none;
}

.sheet-facts {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 5px 14px;
  margin: 0 0 12px;
  font-size: 0.9rem;
}

.sheet-facts dt {
  color: var(--muted);
}

.sheet-facts dd {
  margin: 0;
}

.sheet-description {
  margin: 0;
  color: var(--text-2);
  font-size: 0.86rem;
  line-height: 1.45;
}

/* ---- Wider screens ------------------------------------------------------------ */

@media (min-width: 600px) {
  .card-grid {
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 8px;
  }

  .card-grid-large {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 12px;
  }
}

@media (min-width: 900px) {
  body {
    padding-bottom: 32px;
  }

  .bottom-nav {
    position: sticky;
    top: 0;
    max-width: 1100px;
    height: auto;
    margin: 0 auto 16px;
    padding: 0;
    border: none;
    border-radius: 999px;
    grid-template-columns: repeat(3, max-content);
    justify-content: start;
    gap: 6px;
    background: none;
  }

  .nav-btn {
    flex-direction: row;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 999px;
    font-size: 0.95rem;
  }

  .nav-active {
    background: var(--accent);
    color: var(--accent-text);
  }

  .nav-badge {
    position: static;
    background: var(--surface);
    color: var(--accent);
  }

  #homeView {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    align-items: start;
  }

  #homeView[hidden] {
    display: none;
  }

  .stat-tiles {
    grid-column: 1 / -1;
    margin-bottom: 0;
  }

  .panel {
    margin-bottom: 0;
  }

  .panel:first-of-type {
    grid-row: span 2;
  }

  .filters {
    top: 56px;
  }

  .sheet {
    width: min(860px, calc(100vw - 32px));
    margin: auto;
    padding: 24px;
    border-radius: 20px;
  }

  .sheet-body {
    flex-direction: row;
    align-items: flex-start;
  }

  .sheet-image {
    width: 340px;
    flex-shrink: 0;
  }
}
