(function () {
  "use strict";

  function generateConfetti() {
    const bg = document.getElementById("confettiBg");
    if (!bg) return;

    // Clear old confetti (important!)
    bg.innerHTML = "";

    for (let i = 0; i < 30; i++) {
      const confetti = document.createElement("div");
      confetti.className = "confetti";

      confetti.style.left = Math.random() * 100 + "%";
      confetti.style.animationDelay = Math.random() * 5 + "s";
      confetti.style.animationDuration = Math.floor(Math.random() * 6 + 6) + "s";

      const size = Math.floor(Math.random() * 14 + 8);
      confetti.style.width = size + "px";
      confetti.style.height = size + "px";

      bg.appendChild(confetti);
    }
  }

  function initCursorMagic() {
    const trail = document.getElementById("cursorTrail");
    if (!trail) return;

    let mouseX = window.innerWidth / 2;
    let mouseY = window.innerHeight / 2;
    let targetX = mouseX;
    let targetY = mouseY;

    document.addEventListener("mousemove", (e) => {
      targetX = e.clientX;
      targetY = e.clientY;
    });

    document.addEventListener("touchmove", (e) => {
      if (e.touches && e.touches[0]) {
        targetX = e.touches[0].clientX;
        targetY = e.touches[0].clientY;
      }
    });

    function updateTrail() {
      mouseX += (targetX - mouseX) * 0.15;
      mouseY += (targetY - mouseY) * 0.15;

      trail.style.left = mouseX + "px";
      trail.style.top = mouseY + "px";

      requestAnimationFrame(updateTrail);
    }

    requestAnimationFrame(updateTrail);
  }

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