<script>
  document.addEventListener("DOMContentLoaded", function () {
    var yearEl = document.getElementById("year");
    if (yearEl) {
      yearEl.textContent = new Date().getFullYear();
    }

    var sections = document.querySelectorAll("section[id]");
    var navLinks = document.querySelectorAll(".nav-links a");

    if (sections.length && navLinks.length && "IntersectionObserver" in window) {
      var observer = new IntersectionObserver(function(entries) {
        entries.forEach(function(entry) {
          if (entry.isIntersecting) {
            navLinks.forEach(function(link) {
              link.classList.remove("active");
            });
            var active = document.querySelector('.nav-links a[href="#' + entry.target.id + '"]');
            if (active) active.classList.add("active");
          }
        });
      }, {
        rootMargin: "-40% 0px -45% 0px",
        threshold: 0.01
      });

      sections.forEach(function(section) {
        observer.observe(section);
      });
    }
  });
</script>