(function () {
    'use strict';

    var header  = document.querySelector('.site-header');
    var toggle  = document.querySelector('.nav-toggle');
    var drawer  = document.getElementById('nav-drawer');
    var overlay = document.getElementById('nav-overlay');
    var close   = document.querySelector('.nav-close');
    var yearEl  = document.getElementById('footer-year');

    if (yearEl) yearEl.textContent = String(new Date().getFullYear());

    /* Scroll: header shadow */
    function onScroll() {
        if (header) header.classList.toggle('scrolled', window.scrollY > 30);
    }
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();

    /* Mobile drawer */
    function openDrawer() {
        if (!drawer) return;
        drawer.classList.add('open');
        overlay.classList.add('show');
        document.body.style.overflow = 'hidden';
        toggle && toggle.setAttribute('aria-expanded', 'true');
    }

    function closeDrawer() {
        if (!drawer) return;
        drawer.classList.remove('open');
        overlay.classList.remove('show');
        document.body.style.overflow = '';
        toggle && toggle.setAttribute('aria-expanded', 'false');
    }

    if (toggle) toggle.addEventListener('click', openDrawer);
    if (close)  close.addEventListener('click', closeDrawer);
    if (overlay) overlay.addEventListener('click', closeDrawer);

    if (drawer) {
        drawer.querySelectorAll('a').forEach(function (link) {
            link.addEventListener('click', closeDrawer);
        });
    }

    /* Smooth anchor scroll with header offset */
    document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
        anchor.addEventListener('click', function (e) {
            var id = anchor.getAttribute('href');
            if (!id || id === '#') return;
            var target = document.querySelector(id);
            if (!target) return;
            e.preventDefault();
            var offset = header ? header.offsetHeight + 8 : 0;
            var top = target.getBoundingClientRect().top + window.scrollY - offset;
            window.scrollTo({ top: top, behavior: 'smooth' });
        });
    });

    /* Contact form */
    var form = document.getElementById('contact-form');
    if (form) {
        form.addEventListener('submit', function (e) {
            e.preventDefault();
            var btn = form.querySelector('button[type="submit"]');
            var original = btn.innerHTML;
            btn.disabled = true;
            btn.innerHTML = '<i class="fa-solid fa-check"></i> Thank you — we will be in touch';
            setTimeout(function () {
                form.reset();
                btn.disabled = false;
                btn.innerHTML = original;
            }, 4000);
        });
    }
})();
