:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface-2: #21262d;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #2f81f7;
  --accent-hover: #4493f8;
  --ok: #2ea043;
  --radius: 10px;
}

* { box-sizing: border-box; }

/* Sin scroll-behavior: smooth a propósito: el salto a arriba es instantáneo
   para no atravesar toda la rejilla cargando miniaturas al pasar. */

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(13, 17, 23, 0.96);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 14px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: -0.02em;
}
.subtitle {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}
.controls {
  display: flex;
  gap: 10px;
  align-items: center;
  flex: 1 1 320px;
  justify-content: flex-end;
}
#search {
  flex: 1 1 220px;
  max-width: 320px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 12px;
  border-radius: var(--radius);
  font-size: 0.92rem;
  outline: none;
}
#search:focus { border-color: var(--accent); }

.btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 9px 14px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover { background: #2d333b; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; }

/* Botón de descargar favoritas: icono de descarga + estrella (+ texto) */
#downloadFavs {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
#downloadFavs .ic { display: block; }
#downloadFavs .star { color: #e3b341; }
.btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Selection bar */
.selbar {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 18px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.selbar-actions { display: flex; gap: 10px; }
#selCount { color: var(--muted); font-size: 0.9rem; }

/* Grid */
main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 18px;
}

/* Texto introductorio (SEO + contexto) */
.intro {
  max-width: 70ch;
  margin: 0 0 16px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--muted, #9aa0a6);
}
.intro strong { color: inherit; }

/* Filtros por categoría (sticky bajo la cabecera) */
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  position: sticky;
  top: var(--topbar-h, 56px);
  z-index: 15;
  background: var(--bg);
  /* padding simétrico: el hueco va dentro del fondo para que se mantenga al
     hacerse sticky (el margin no se ve porque no tiene fondo) */
  padding: 14px 0;
  margin-bottom: 0;
}
.filters .fbtn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.filters .fbtn:hover { background: var(--surface-2); }
.filters .fbtn.active { background: #fff; color: #111; border-color: #fff; }
.filters .fbtn .cnt { opacity: 0.6; font-weight: 400; margin-left: 4px; }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 8px;
}
.tile {
  position: relative;
  aspect-ratio: 1 / 1;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  /* El navegador se salta el render de los tiles fuera de pantalla: abrir/cerrar
     el visor y el scroll no recalculan miles de nodos. */
  content-visibility: auto;
  contain-intrinsic-size: 170px;
}
.tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  opacity: 0;
  transition: opacity 0.35s ease;
}
.tile img.loaded { opacity: 1; }

.tile .num {
  position: absolute;
  left: 6px;
  bottom: 6px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 0.72rem;
  padding: 2px 7px;
  border-radius: 999px;
  pointer-events: none;
}
.tile.failed { display: none; }

/* Selection mode */
body.selecting .tile { cursor: pointer; }
.tile .check {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid #fff;
  background: rgba(0,0,0,0.35);
  display: none;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 0.85rem;
  pointer-events: none;
}
body.selecting .tile .check { display: flex; }
.tile.selected { border-color: var(--accent); }
.tile.selected .check { background: var(--accent); border-color: var(--accent); }

/* Estrella de favorita en cada tile (arriba-izquierda) */
.tile .fav {
  position: absolute;
  top: 6px;
  left: 6px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 0.95rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.55;
  transition: opacity 0.15s, transform 0.1s;
}
.tile:hover .fav,
.tile .fav.on { opacity: 1; }
.tile .fav.on { color: #e3b341; } /* dorado suave, no chillón */
.tile .fav:hover { transform: scale(1.12); }

/* Botón de recargar miniatura cuando falla la carga */
.tile .reload {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.4);
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
}
.tile.errored { background: var(--surface-2); }
.tile.errored .reload { display: flex; }

/* Botón de filtro "Favoritas" (amarillo elegante) */
.filters .fbtn-fav {
  border-color: rgba(227, 179, 65, 0.5);
  color: #e3b341;
}
.filters .fbtn-fav.active {
  background: #e3b341;
  border-color: #e3b341;
  color: #1a1a1a;
}

/* Botón favorita en el visor */
#lbFav.on {
  color: #e3b341;
  border-color: rgba(227, 179, 65, 0.5);
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 60px 0;
}
.hidden { display: none !important; }

/* Lightbox */
.lb {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0,0,0,0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  /* mismo respiro que a los lados (≈14px) para que la botonera no quede pegada
     al borde inferior; suma el safe-area de móviles con notch */
  padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  overflow: hidden; /* nada se sale de la pantalla */
}
/* Carrusel: viewport recorta y el track lleva 3 slides (anterior/actual/siguiente).
   Medidas en % (no vw) para no contar la barra de scroll ni desbordar. */
.lb-viewport {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  overflow: hidden;
}
.lb-track {
  display: flex;
  width: 300%;
  height: 100%;
  transform: translateX(-33.3333%); /* centra el slide del medio (actual) */
}
.lb-slide {
  position: relative;
  width: 33.3333%;
  height: 100%;
  overflow: hidden; /* el blur de la miniatura no se filtra al slide vecino */
}
/* Blur-up: miniatura difuminada de fondo (instantánea) + original que se funde encima */
.lb-thumb {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: blur(18px);
  transform: scale(1.08); /* tapa los bordes blandos del desenfoque */
  user-select: none;
  -webkit-user-drag: none;
}
.lb-full {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  opacity: 0;
  transition: opacity 0.3s ease;
  user-select: none;
  -webkit-user-drag: none;
  /* admite zoom (transform propio, independiente del track); cualquiera de los 3
     slides puede ser el central tras rotar, por eso aplica a todos */
  transform-origin: center center;
  touch-action: none;
  will-change: transform;
}
.lb-full.shown { opacity: 1; }
/* Al cambiar de foto: la original anterior se oculta al instante (sin fundido de
   salida que dejaría verla mientras la nueva carga). El fundido de entrada sigue. */
.lb-full.instant { transition: none; }
/* Botón de recargar cuando falla la original */
.lb-reload {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  display: none;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 0.9rem;
  cursor: pointer;
}
.lb-slide.errored .lb-reload { display: flex; }
.lb-caption {
  position: relative;
  z-index: 55; /* por encima del viewport/imagen */
  display: flex;
  align-items: center;
  gap: 16px;
  color: var(--text);
}
.lb-close {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 60; /* siempre por encima de la imagen (si no, en fotos altas la tapa) */
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.55); /* visible sobre cualquier foto */
  border: none;
  border-radius: 50%;
  color: #fff;
  font-size: 1.9rem;
  line-height: 1;
  cursor: pointer;
}
.lb-close:hover { background: rgba(0, 0, 0, 0.75); }
.lb-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 55; /* por encima del viewport (si no, la imagen los tapa) */
  background: rgba(255,255,255,0.08);
  border: none;
  color: #fff;
  font-size: 2rem;
  width: 54px;
  height: 80px;
  cursor: pointer;
  border-radius: 8px;
}
.lb-nav:hover { background: rgba(255,255,255,0.18); }
.lb-prev { left: 14px; }
.lb-next { right: 14px; }

/* Toast */
.toast {
  position: fixed;
  bottom: calc(var(--footer-h, 56px) + 16px); /* por encima del footer fijo */
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 18px;
  border-radius: var(--radius);
  z-index: 60;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
  font-size: 0.9rem;
}

/* Botón volver arriba */
.to-top {
  position: fixed;
  right: 18px;
  bottom: calc(var(--footer-h, 56px) + 14px); /* por encima del footer fijo */
  z-index: 40;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--accent);
  color: #fff;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
}
.to-top:hover { background: var(--accent-hover); }

/* Footer fijo siempre visible */
.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 30;
  background: rgba(13, 17, 23, 0.96);
  border-top: 1px solid var(--border);
  padding: 8px 16px;
  color: var(--muted);
  font-size: 0.72rem;
  text-align: center;
}
.footer p { margin: 0; }
.footer a { color: var(--muted); text-decoration: underline; }
/* hueco para que el footer fijo no tape la última fila.
   --footer-h lo fija el JS con la altura real (varía según se ajuste el texto). */
body { padding-bottom: calc(var(--footer-h, 64px) + 10px); }

@media (max-width: 640px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px; }
  .tile { contain-intrinsic-size: 140px; }

  /* Controles: el buscador ocupa la fila y el botón de favoritas se vuelve
     compacto (solo ★) para no comerse el ancho. */
  .controls { flex-basis: 100%; gap: 8px; }
  #search { max-width: none; flex: 1 1 auto; }
  #downloadFavs .btn-text { display: none; }
  #downloadFavs { flex: 0 0 auto; padding: 9px 13px; }

  /* Categorías en una sola fila con scroll horizontal (no ocupan tanto alto).
     Conserva el padding simétrico de la base para que el sticky no pierda el hueco. */
  .filters {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding: 10px 0; /* un poco menos que en escritorio, sigue simétrico */
  }
  .filters::-webkit-scrollbar { display: none; }
  .filters .fbtn { flex: 0 0 auto; padding: 7px 12px; }

  /* Visor: las flechas se ocultan (se navega con swipe) y se da más espacio a
     la imagen; los botones del pie van apilados a ancho completo. */
  .lb-nav { display: none; }
  .lb-caption {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    width: 92vw;
  }
  .lb-caption #lbLabel { text-align: center; }
  .lb-caption .btn { width: 100%; text-align: center; }
}
