document.addEventListener('DOMContentLoaded', () => {
    const root = document.documentElement;
    const siteHeader = document.querySelector('[data-site-header]');
    const menuToggle = document.querySelector('[data-menu-toggle]');
    const mobileMenu = document.querySelector('[data-mobile-menu]');
    const openIcon = document.querySelector('[data-menu-icon="open"]');
    const closeIcon = document.querySelector('[data-menu-icon="close"]');
    const mobileLinks = document.querySelectorAll('[data-mobile-nav-link]');
    const themeToggle = document.querySelector('[data-theme-toggle]');

    const applyTheme = (theme) => {
        root.setAttribute('data-theme', theme);
        localStorage.setItem('site-theme', theme);
    };

    const preferredTheme = localStorage.getItem('site-theme')
        || (window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark');

    applyTheme(preferredTheme);

    const syncHeader = () => {
        if (!siteHeader) {
            return;
        }

        siteHeader.classList.toggle('is-scrolled', window.scrollY > 16);
    };

    const setMenuState = (isOpen) => {
        if (!menuToggle || !mobileMenu) {
            return;
        }

        menuToggle.setAttribute('aria-expanded', String(isOpen));
        mobileMenu.classList.toggle('hidden', !isOpen);

        if (openIcon) {
            openIcon.classList.toggle('hidden', isOpen);
        }

        if (closeIcon) {
            closeIcon.classList.toggle('hidden', !isOpen);
        }
    };

    syncHeader();
    window.addEventListener('scroll', syncHeader, { passive: true });

    if (menuToggle) {
        menuToggle.addEventListener('click', () => {
            const isOpen = menuToggle.getAttribute('aria-expanded') === 'true';
            setMenuState(!isOpen);
        });
    }

    if (themeToggle) {
        themeToggle.addEventListener('click', () => {
            const nextTheme = root.getAttribute('data-theme') === 'light' ? 'dark' : 'light';
            applyTheme(nextTheme);
        });
    }

    mobileLinks.forEach((link) => {
        link.addEventListener('click', () => setMenuState(false));
    });

    document.querySelectorAll('[data-contact-form]').forEach((form) => {
        if (form.dataset.bound === 'true') {
            return;
        }

        form.dataset.bound = 'true';

        form.addEventListener('submit', (event) => {
            const siteKey = form.dataset.recaptchaSiteKey;
            const tokenField = form.querySelector('input[name="g-recaptcha-response"]');

            if (!siteKey || !window.grecaptcha || !tokenField) {
                return;
            }

            if (form.dataset.recaptchaVerified === 'true') {
                form.dataset.recaptchaVerified = 'false';
                return;
            }

            event.preventDefault();

            window.grecaptcha.ready(() => {
                window.grecaptcha.execute(siteKey, { action: 'submit' }).then((token) => {
                    tokenField.value = token;
                    form.dataset.recaptchaVerified = 'true';
                    form.requestSubmit();
                });
            });
        });
    });
});
