:root {
  --bg: #fafafa;
  --fg: #1a1a1a;
  --muted: #666;
  --card: #fff;
  --border: #e5e5e5;
  --accent: #2563eb;
  --accent-fg: #fff;
  --chip-bg: #eef2ff;
  --chip-fg: #1e3a8a;
  --chip-active-bg: #1e3a8a;
  --chip-active-fg: #fff;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f10;
    --fg: #ededed;
    --muted: #999;
    --card: #1a1a1c;
    --border: #2a2a2d;
    --accent: #3b82f6;
    --accent-fg: #fff;
    --chip-bg: #1e293b;
    --chip-fg: #cbd5e1;
    --chip-active-bg: #cbd5e1;
    --chip-active-fg: #0f172a;
    --shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  /* Hard guard: nothing should be wider than the viewport. Long <option>
     text and intrinsic <select> widths used to push the page sideways on
     iOS Safari. min-width: 0 on the flex/grid descendants below is the
     real fix; this is the safety net. */
  overflow-x: hidden;
  max-width: 100vw;
}

.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(140%) blur(8px);
  -webkit-backdrop-filter: saturate(140%) blur(8px);
  border-bottom: 1px solid var(--border);
  padding: clamp(8px, 2vw, 16px);
  display: grid;
  gap: 10px;
  grid-template-columns: 1fr;
}

.toolbar-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
}

.toolbar-row > * { min-width: 0; }

.toolbar h1 {
  margin: 0;
  font-size: clamp(1.1rem, 3vw, 1.4rem);
  letter-spacing: -0.01em;
}

.count {
  color: var(--muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#search {
  width: 100%;
  padding: 10px 12px;
  font-size: 1rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--fg);
  min-height: 44px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

#search:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent);
}

.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.sort-toggle {
  display: inline-flex;
  gap: 2px;
  padding: 2px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--chip-bg) 70%, transparent);
  margin-left: auto;
}

.sort-toggle .chip {
  background: transparent;
  font-size: 0.8rem;
  min-height: 28px;
  padding: 4px 10px;
}

.sort-toggle .chip[aria-pressed="true"] {
  background: var(--chip-active-bg);
  color: var(--chip-active-fg);
}

.chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: thin;
  flex: 1 1 auto;
  min-width: 0;
}

@media (min-width: 720px) {
  .chips { flex-wrap: wrap; overflow-x: visible; }
}

.chip {
  flex: 0 0 auto;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--chip-bg);
  color: var(--chip-fg);
  border: 1px solid transparent;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  min-height: 32px;
  transition: background-color 0.15s ease, color 0.15s ease,
    transform 0.05s ease;
}

.chip:hover {
  background: color-mix(in srgb, var(--chip-bg) 70%, var(--accent) 12%);
}

.chip:active {
  transform: scale(0.97);
}

.chip:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent);
}

.chip[aria-pressed="true"] {
  background: var(--chip-active-bg);
  color: var(--chip-active-fg);
}

main {
  padding: clamp(12px, 3vw, 24px);
  max-width: 1600px;
  margin: 0 auto;
}

.status {
  text-align: center;
  color: var(--muted);
  margin: 48px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.status-text {
  font-size: 0.95rem;
}

.status-reset {
  padding: 8px 16px;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  min-height: 36px;
  transition: filter 0.15s ease;
}

.status-reset:hover { filter: brightness(1.1); }

.status-reset:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent);
}

.grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(260px, 100%), 1fr));
  gap: clamp(8px, 2vw, 20px);
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
  transition: transform 0.18s ease, box-shadow 0.18s ease,
    border-color 0.18s ease;
  /* Grid items default to min-content width — without this the longest
     <option> string inside .stl-select can force the card past viewport
     width and trigger horizontal page scroll on mobile. */
  min-width: 0;
}

@media (hover: hover) {
  .card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.10);
    border-color: color-mix(in srgb, var(--border) 40%, var(--accent) 60%);
  }
}

.card:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.card .thumb-wrap {
  display: block;
  background: var(--border);
  overflow: hidden;
  /* Mobile: image dictates height (full-bleed portrait without bars).
     Desktop: see media query below — fixed 4:3 box for uniform alignment. */
}

.card img {
  width: 100%;
  height: auto;
  display: block;
  transition: transform 0.4s ease;
}

@media (hover: hover) {
  .card:hover img {
    transform: scale(1.03);
  }
}

@media (min-width: 720px) {
  .card .thumb-wrap {
    aspect-ratio: 4 / 3;
  }
  .card img {
    height: 100%;
    object-fit: contain;
  }
}

.card .no-thumb {
  width: 100%;
  aspect-ratio: 4 / 3;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(2rem, 8vw, 4rem);
  font-weight: 700;
  color: var(--muted);
  background: linear-gradient(135deg, var(--card) 0%, var(--border) 100%);
}

.card .body {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1;
}

.card h2 {
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.005em;
  margin: 0;
  word-break: break-word;
  line-height: 1.25;
}

.card .release-chip {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--chip-bg);
  color: var(--chip-fg);
  font-size: 0.7rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.card .card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
}

.saturn-badge {
  padding: 2px 8px;
  border-radius: 999px;
  background: #f59e0b;
  color: #1a1300;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.chip-saturn {
  background: #f59e0b;
  color: #1a1300;
  font-weight: 500;
}

.chip-saturn[aria-pressed="true"] {
  background: #b45309;
  color: #fff;
}

@media (prefers-color-scheme: dark) {
  .saturn-badge {
    background: #fbbf24;
    color: #1a1300;
  }
  .chip-saturn {
    background: #fbbf24;
    color: #1a1300;
  }
  .chip-saturn[aria-pressed="true"] {
    background: #d97706;
    color: #fff;
  }
}

.card .actions {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.card a.button {
  display: inline-block;
  padding: 10px 12px;
  background: var(--accent);
  color: var(--accent-fg);
  border-radius: 8px;
  text-decoration: none;
  text-align: center;
  font-weight: 500;
  min-height: 44px;
}

.card a.button:hover { filter: brightness(1.1); }

.stl-picker {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
  align-items: stretch;
  min-width: 0;
}

.stl-select {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.85rem;
  font-family: inherit;
}

.stl-select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.stl-download {
  padding: 0 14px;
  min-height: 40px;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}

.stl-download:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.stl-download:not(:disabled):hover {
  filter: brightness(1.1);
}

.card a.folder-link {
  display: inline-block;
  padding: 6px 0;
  font-size: 0.8rem;
  color: var(--muted);
  text-decoration: none;
  align-self: flex-start;
}

.card a.folder-link:hover {
  color: var(--accent);
  text-decoration: underline;
}

.card.skeleton {
  pointer-events: none;
}

.card.skeleton:hover {
  transform: none;
  box-shadow: var(--shadow);
  border-color: var(--border);
}

.skeleton-thumb {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: var(--border);
  position: relative;
  overflow: hidden;
}

.skeleton-body {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1;
}

.skeleton-line {
  height: 14px;
  border-radius: 4px;
  background: var(--border);
  position: relative;
  overflow: hidden;
}

.skeleton-line--short { width: 40%; height: 10px; }
.skeleton-line--btn { height: 40px; margin-top: auto; border-radius: 8px; }

.skeleton-thumb::after,
.skeleton-line::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(
    90deg,
    transparent 0%,
    color-mix(in srgb, var(--card) 50%, transparent) 50%,
    transparent 100%
  );
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite linear;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

footer {
  text-align: center;
  padding: 24px 16px 32px;
  color: var(--muted);
  font-size: 0.8rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.legend {
  font-size: 0.75rem;
  opacity: 0.85;
}

.legend-tag {
  display: inline-block;
  padding: 1px 5px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--muted) 15%, transparent);
  color: var(--fg);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.7rem;
}
