// ===== Mobile menu =====
const mobileToggle = document.querySelector('[data-mobile-toggle]');
const mobileMenu = document.querySelector('[data-mobile-menu]');
const iconOpen = document.querySelector('[data-icon-open]');
const iconClose = document.querySelector('[data-icon-close]');

if (mobileToggle && mobileMenu) {
    mobileToggle.addEventListener('click', () => {
        const isHidden = mobileMenu.classList.toggle('hidden');
        iconOpen?.classList.toggle('hidden', !isHidden);
        iconClose?.classList.toggle('hidden', isHidden);
    });
}

// ===== Resources dropdown (desktop) =====
document.querySelectorAll('[data-dropdown]').forEach((wrap) => {
    const trigger = wrap.querySelector('[data-dropdown-trigger]');
    const menu = wrap.querySelector('[data-dropdown-menu]');
    const caret = wrap.querySelector('[data-dropdown-caret]');
    if (!trigger || !menu) return;

    const open = () => {
        menu.classList.remove('invisible', 'opacity-0', 'translate-y-2');
        caret?.classList.add('rot');
    };
    const close = () => {
        menu.classList.add('invisible', 'opacity-0', 'translate-y-2');
        caret?.classList.remove('rot');
    };

    let inside = false;
    const mark = (v) => { inside = v; setTimeout(() => (inside ? open() : close()), 60); };

    wrap.addEventListener('mouseenter', () => mark(true));
    wrap.addEventListener('mouseleave', () => mark(false));
    trigger.addEventListener('click', (e) => {
        e.stopPropagation();
        menu.classList.contains('invisible') ? open() : close();
    });
    document.addEventListener('click', (e) => {
        if (!wrap.contains(e.target)) close();
    });
});

// ===== Sticky header shadow =====
const header = document.getElementById('site-header');
if (header) {
    const onScroll = () => header.classList.toggle('shadow-lg', window.scrollY > 8);
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
}

// ===== Help widget (WhatsApp) =====
const helpCard = document.querySelector('[data-help-card]');
const helpClose = document.querySelector('[data-help-close]');
if (helpCard) {
    // Auto-reveal the prompt card after a short delay
    setTimeout(() => {
        helpCard.classList.remove('hidden');
        helpCard.classList.add('flex');
    }, 1600);
    helpClose?.addEventListener('click', () => {
        helpCard.classList.add('hidden');
        helpCard.classList.remove('flex');
    });
}

// ===== Accordion (FAQ) =====
document.querySelectorAll('[data-accordion-item]').forEach((item) => {
    const trigger = item.querySelector('[data-accordion-trigger]');
    const panel = item.querySelector('[data-accordion-panel]');
    const icon = item.querySelector('[data-accordion-icon]');
    if (!trigger || !panel) return;
    trigger.addEventListener('click', () => {
        const isOpen = panel.classList.contains('grid-rows-[1fr]');
        // close siblings
        item.parentElement.querySelectorAll('[data-accordion-panel]').forEach((p) => {
            p.classList.remove('grid-rows-[1fr]');
            p.classList.add('grid-rows-[0fr]');
        });
        item.parentElement.querySelectorAll('[data-accordion-icon]').forEach((i) => i.classList.remove('rot'));
        if (!isOpen) {
            panel.classList.remove('grid-rows-[0fr]');
            panel.classList.add('grid-rows-[1fr]');
            icon?.classList.add('rot');
        }
    });
});

// ===== Admin sidebar (mobile) =====
const adminToggle = document.querySelector('[data-admin-toggle]');
const adminSidebar = document.querySelector('[data-admin-sidebar]');
const adminBackdrop = document.querySelector('[data-admin-backdrop]');
if (adminToggle && adminSidebar) {
    const openSidebar = () => {
        adminSidebar.classList.remove('hidden', '-translate-x-full');
        adminSidebar.classList.add('flex');
        adminBackdrop?.classList.remove('hidden');
    };
    const closeSidebar = () => {
        adminSidebar.classList.add('-translate-x-full');
        adminBackdrop?.classList.add('hidden');
    };
    adminToggle.addEventListener('click', () => {
        adminSidebar.classList.contains('-translate-x-full') ? openSidebar() : closeSidebar();
    });
    adminBackdrop?.addEventListener('click', closeSidebar);
}

// ===== Reveal on scroll =====
const revealEls = document.querySelectorAll('.reveal');
if ('IntersectionObserver' in window && revealEls.length) {
    const io = new IntersectionObserver((entries) => {
        entries.forEach((entry) => {
            if (entry.isIntersecting) {
                entry.target.classList.add('is-visible');
                io.unobserve(entry.target);
            }
        });
    }, { threshold: 0.12, rootMargin: '0px 0px -40px 0px' });
    revealEls.forEach((el) => io.observe(el));
} else {
    revealEls.forEach((el) => el.classList.add('is-visible'));
}
