(function () {
    const activeClasses = ['text-[#ccff00]', 'font-semibold'];
    const navLinks = Array.from(document.querySelectorAll('nav a[href]')).filter(link =>
        ['How It Works', 'Mobile Apps', 'Business Websites', 'Shopify Stores', 'Why Us', 'Our Work', 'About', 'Contact'].includes(link.textContent.trim())
    );

    if (!navLinks.length) return;

    const pageClusters = {
        'mobile-apps.html': 'mobile-apps.html',
        'mobile-app-branding-ui-ux.html': 'mobile-apps.html',
        'backend-api-integration.html': 'mobile-apps.html',
        'app-store-launch-support.html': 'mobile-apps.html',
        'influencer-marketing-social-media.html': 'mobile-apps.html',

        'websites.html': 'business-websites.html',
        'business-websites.html': 'business-websites.html',
        'small-business-website-design.html': 'business-websites.html',
        'service-business-website-design.html': 'business-websites.html',
        'fort-lauderdale-business-website-design.html': 'business-websites.html',
        'custom-business-website-development.html': 'business-websites.html',
        'custom-web-development-ecommerce.html': 'business-websites.html',
        'ecommerce-business-websites.html': 'business-websites.html',
        'booking-website-for-business.html': 'business-websites.html',
        'online-bookings-appointments-memberships.html': 'business-websites.html',
        'business-website-seo.html': 'business-websites.html',
        'seo-performance-optimization.html': 'business-websites.html',
        'brand-positioning-creative-strategy.html': 'business-websites.html',
        'generative-engine-optimization.html': 'business-websites.html',

        'shopify-stores.html': 'shopify-stores.html',
        'shopify-store-design.html': 'shopify-stores.html',
        'shopify-seo-optimization.html': 'shopify-stores.html',
        'shopify-migration-optimization.html': 'shopify-stores.html',

        'why-not-wix.html': 'about.html',
        'why-not-squarespace.html': 'about.html',
        'why-not-godaddy-website-builder.html': 'about.html',
        'why-not-webflow.html': 'about.html',
        'why-not-shopify.html': 'about.html',
        'why-not-wordpress.html': 'about.html',

        'about.html': 'about.html',
    };

    const homeSections = [
        { id: 'how-it-works', href: 'index.html#how-it-works' },
        { id: 'work', href: 'index.html#work' },
        { id: 'about', href: 'index.html#about' },
    ];

    function setActive(href) {
        navLinks.forEach(link => {
            const isActive = link.getAttribute('href') === href;
            link.classList.toggle(activeClasses[0], isActive);
            link.classList.toggle(activeClasses[1], isActive);

            if (isActive) {
                link.setAttribute('aria-current', 'page');
            } else {
                link.removeAttribute('aria-current');
            }
        });
    }

    function normalizedPage() {
        const page = window.location.pathname.split('/').pop();
        return page || 'index.html';
    }

    function updateHomeActive() {
        const offset = window.innerHeight * 0.35;
        let currentHref = '';

        for (const section of homeSections) {
            const element = document.getElementById(section.id);
            if (!element) continue;

            if (element.getBoundingClientRect().top <= offset) {
                currentHref = section.href;
            }
        }

        setActive(currentHref);
    }

    function hrefForCurrentHash() {
        const section = homeSections.find(item => `#${item.id}` === window.location.hash);
        return section ? section.href : '';
    }

    const page = normalizedPage();

    if (page === 'index.html') {
        setActive(hrefForCurrentHash());
        requestAnimationFrame(updateHomeActive);
        window.addEventListener('load', updateHomeActive);
        window.addEventListener('scroll', updateHomeActive, { passive: true });
        window.addEventListener('hashchange', () => {
            setActive(hrefForCurrentHash());
            requestAnimationFrame(updateHomeActive);
        });
        return;
    }

    setActive(pageClusters[page] || '');
})();
