(function() {
    'use strict';

    var sidebarLinks = document.querySelectorAll('.sidebar-nav a[href^="#"]');

    function onSidebarLinkClick(e) {
        e.preventDefault();
        var targetId = this.getAttribute('href').substring(1);
        var target = document.getElementById(targetId);
        if (target) {
            window.scrollTo({
                top: target.offsetTop - 100,
                behavior: 'smooth'
            });
        }
    }

    for (var i = 0; i < sidebarLinks.length; i++) {
        sidebarLinks[i].addEventListener('click', onSidebarLinkClick);
    }

    var sections = document.querySelectorAll('.api-section');
    var navLinks = document.querySelectorAll('.sidebar-nav a[href^="#"]');

    function highlightSidebarOnScroll() {
        var scrollPos = window.scrollY + 120;
        var activeId = null;

        for (var i = 0; i < sections.length; i++) {
            var section = sections[i];
            if (section.offsetTop <= scrollPos) {
                activeId = section.id;
            }
        }

        for (var j = 0; j < navLinks.length; j++) {
            var link = navLinks[j];
            var href = link.getAttribute('href').substring(1);
            if (href === activeId) {
                link.style.color = '#e09145';
                link.style.fontWeight = '600';
                link.style.borderLeftColor = '#e09145';
            } else {
                link.style.color = '#555';
                link.style.fontWeight = 'normal';
                link.style.borderLeftColor = 'transparent';
            }
        }
    }

    window.addEventListener('scroll', highlightSidebarOnScroll);

    highlightSidebarOnScroll();

})();