function initScrollSpy() {
  const observer = new IntersectionObserver(function (entries) {
    entries.forEach(function (e) {
      if (e.isIntersecting) {
        document.querySelectorAll('.nav-link').forEach(function (l) { l.classList.remove('active'); });
        var el = document.querySelector('.nav-link[href="#' + e.target.id + '"]');
        if (el) el.classList.add('active');
      }
    });
  }, { rootMargin: '-80px 0px -50% 0px' });
  document.querySelectorAll('section[id]').forEach(function (s) { observer.observe(s); });
}
