(() => {
  "use strict";
  const dialog = document.getElementById("zipAccessDialog");
  const closeButton = document.getElementById("zipAccessClose");
  const downloadButton = document.getElementById("zipAccessDownload");
  const requestLink = document.getElementById("zipAccessRequest");
  const emailLink = document.getElementById("zipAccessEmail");
  const policyAgree = document.getElementById("zipPolicyAgree");
  const contactConfirm = document.getElementById("zipContactConfirm");
  const projectNode = document.getElementById("zipAccessProject");
  let pendingUrl = "";
  let pendingName = "";
  let pendingProject = "Protected Project";

  function syncButton() {
    if (!downloadButton) return;
    downloadButton.disabled = !(policyAgree?.checked && contactConfirm?.checked);
    downloadButton.setAttribute("aria-disabled", String(downloadButton.disabled));
  }
  function mailto() {
    const subject = `Password Request - ${pendingProject} - AI Project Universe`;
    const body = `Hello,

I would like to request authorized access to the protected project ZIP.

Project: ${pendingProject}
Intended use: [please describe]
Identity/organization: [optional]

I have read the Privacy Policy and Terms of Service.

Thank you.`;
    return `mailto:yirassssindaba@gmail.com?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
  }
  function openAccess(anchor) {
    pendingUrl = anchor.href;
    pendingName = anchor.getAttribute("download") || pendingUrl.split("/").pop() || "protected-project.zip";
    pendingProject = anchor.closest("[data-title]")?.getAttribute("data-title") || anchor.querySelector("b")?.textContent?.trim() || pendingName.replace(/^[0-9]+_|_protected-project\.zip$/g, "").replaceAll("-", " ");
    if (projectNode) projectNode.textContent = pendingProject;
    const href = mailto();
    if (requestLink) requestLink.href = href;
    if (emailLink) emailLink.href = href;
    if (policyAgree) policyAgree.checked = false;
    if (contactConfirm) contactConfirm.checked = false;
    syncButton();
    if (dialog?.showModal) dialog.showModal();
    else window.location.href = href;
  }
  function triggerDownload() {
    if (!pendingUrl || downloadButton?.disabled) return;
    const link = document.createElement("a");
    link.href = pendingUrl; link.download = pendingName; link.hidden = true;
    document.body.appendChild(link); link.click(); link.remove(); dialog?.close();
  }
  document.addEventListener("click", event => {
    const anchor = event.target.closest("a.locked-download, #viewerDownload");
    if (!anchor || !anchor.href || !anchor.href.includes("/downloads/")) return;
    event.preventDefault(); openAccess(anchor);
  });
  closeButton?.addEventListener("click", () => dialog.close());
  downloadButton?.addEventListener("click", triggerDownload);
  policyAgree?.addEventListener("change", syncButton);
  contactConfirm?.addEventListener("change", syncButton);
  requestLink?.addEventListener("click", () => { if (contactConfirm) contactConfirm.checked = true; syncButton(); });
  dialog?.addEventListener("click", event => { if (event.target === dialog) dialog.close(); });
})();
