(function () {
  "use strict";

  // ── Lightbox para capturas de pantalla ──────────────────────────────────
  function initLightbox() {
    const screenshots = document.querySelectorAll(".game-detail__screenshot");
    if (!screenshots.length) return;

    const overlay = document.createElement("div");
    overlay.className = "lightbox-overlay";
    overlay.setAttribute("role", "dialog");
    overlay.setAttribute("aria-modal", "true");
    overlay.setAttribute("aria-label", "Vista ampliada de captura");

    const img = document.createElement("img");
    const closeBtn = document.createElement("button");
    closeBtn.className = "lightbox-close";
    closeBtn.textContent = "\u00D7";
    closeBtn.setAttribute("aria-label", "Cerrar");

    overlay.appendChild(img);
    overlay.appendChild(closeBtn);

    function open(src, alt) {
      img.src = src;
      img.alt = alt;
      document.body.appendChild(overlay);
      document.body.style.overflow = "hidden";
      closeBtn.focus();
    }

    function close() {
      if (overlay.parentNode) {
        document.body.removeChild(overlay);
        document.body.style.overflow = "";
      }
    }

    screenshots.forEach(function (el) {
      el.addEventListener("click", function () {
        open(el.src, el.alt);
      });
    });

    overlay.addEventListener("click", function (e) {
      if (e.target === overlay) close();
    });

    closeBtn.addEventListener("click", close);

    document.addEventListener("keydown", function (e) {
      if (e.key === "Escape") close();
    });
  }

  document.addEventListener("DOMContentLoaded", function () {
    initLightbox();
  });
})();
