// app.js
(function () {
  // 1) Auto-update footer year
  const yearEl = document.getElementById("year");
  if (yearEl) {
    yearEl.textContent = new Date().getFullYear();
  }

  // 2) Scroll-to-top button show/hide
  const toTop = document.getElementById("toTop");
  let ticking = false;

  function onScroll() {
    if (!ticking) {
      window.requestAnimationFrame(() => {
        const showAt = 400; // px
        if (toTop) {
          if (window.scrollY > showAt) {
            toTop.classList.add("is-visible");
          } else {
            toTop.classList.remove("is-visible");
          }
        }
        ticking = false;
      });
      ticking = true;
    }
  }

  window.addEventListener("scroll", onScroll, { passive: true });
  onScroll(); // initial state

  // 3) Smooth scroll to top on click
  if (toTop) {
    toTop.addEventListener("click", (e) => {
      e.preventDefault();
      // Use native smooth behavior when allowed; CSS fallback already set
      try {
        window.scrollTo({ top: 0, behavior: "smooth" });
      } catch {
        window.scrollTo(0, 0);
      }
    });
  }
})();
