'use strict';
/* ARFLINT v6 — script.js */

// ── Navbar ────────────────────────────────────────────────────────────
const nav = document.querySelector('.navbar');
function setNav() { if (nav) nav.classList.toggle('scrolled', window.scrollY > 20); }
setNav();
window.addEventListener('scroll', setNav, { passive: true });

// ── Mobile drawer ─────────────────────────────────────────────────────
const ham = document.querySelector('.ham');
const drawer = document.querySelector('.mob-drawer');
const scrim = document.querySelector('.scrim');
const closeDrawer = () => {
  ham?.classList.remove('open');
  drawer?.classList.remove('open');
  scrim?.classList.remove('show');
  document.body.style.overflow = '';
};
ham?.addEventListener('click', () => {
  const open = ham.classList.toggle('open');
  drawer?.classList.toggle('open', open);
  scrim?.classList.toggle('show', open);
  document.body.style.overflow = open ? 'hidden' : '';
});
scrim?.addEventListener('click', closeDrawer);
drawer?.querySelectorAll('a').forEach(a => a.addEventListener('click', closeDrawer));
document.querySelectorAll('.mob-tog').forEach(btn => {
  btn.addEventListener('click', () => {
    const sub = btn.nextElementSibling;
    const open = sub?.classList.toggle('open');
    btn.classList.toggle('tog-open', !!open);
  });
});

// ── Scroll reveal ─────────────────────────────────────────────────────
const rvs = document.querySelectorAll('.rv');
if ('IntersectionObserver' in window && rvs.length) {
  const obs = new IntersectionObserver(entries => {
    entries.forEach(e => { if (e.isIntersecting) { e.target.classList.add('on'); obs.unobserve(e.target); } });
  }, { threshold: 0.06, rootMargin: '0px 0px -30px 0px' });
  rvs.forEach(el => obs.observe(el));
} else { rvs.forEach(el => el.classList.add('on')); }

// ── FAQ ───────────────────────────────────────────────────────────────
document.querySelectorAll('.faq-item').forEach(item => {
  const tog = item.querySelector('.faq-toggle');
  const body = item.querySelector('.faq-body');
  if (!tog || !body) return;
  tog.addEventListener('click', () => {
    const open = item.classList.toggle('open');
    body.style.maxHeight = open ? body.scrollHeight + 'px' : '0';
  });
});

// ── Filter ────────────────────────────────────────────────────────────
document.querySelectorAll('.filter-row .fb').forEach(btn => {
  btn.addEventListener('click', () => {
    document.querySelectorAll('.filter-row .fb').forEach(b => b.classList.remove('act'));
    btn.classList.add('act');
    const f = btn.dataset.filter || 'all';
    document.querySelectorAll('[data-cat]').forEach(c => { c.style.display = f === 'all' || c.dataset.cat === f ? '' : 'none'; });
  });
});

// ── Form ──────────────────────────────────────────────────────────────
document.querySelectorAll('form[data-v]').forEach(form => {
  form.addEventListener('submit', e => {
    e.preventDefault();
    let ok = true;
    form.querySelectorAll('.ferr').forEach(el => el.classList.remove('show'));
    form.querySelectorAll('[required]').forEach(field => {
      const v = field.value.trim();
      const err = field.closest('.fg')?.querySelector('.ferr');
      const valid = v && (field.type !== 'email' || /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v));
      if (!valid) {
        ok = false; err?.classList.add('show');
        field.style.borderColor = '#A8472B';
        field.addEventListener('input', () => { field.style.borderColor = ''; err?.classList.remove('show'); }, { once: true });
      }
    });
    if (!ok) return;
    const btn = form.querySelector('[type="submit"]');
    const suc = form.querySelector('.fsuccess');
    if (btn) { btn.textContent = 'Sending…'; btn.disabled = true; }
    setTimeout(() => {
      suc?.classList.add('show');
      if (btn) btn.style.display = 'none';
      form.querySelectorAll('input,select,textarea').forEach(f => { if (f.type !== 'submit') f.value = ''; });
    }, 900);
  });
});

// ── Active nav ────────────────────────────────────────────────────────
const pg = location.pathname.split('/').pop() || 'index.html';
document.querySelectorAll('.nav-a, .mob-link').forEach(a => {
  if ((a.getAttribute('href') || '').split('?')[0] === pg) a.classList.add('act');
});

// ── WA pulse ─────────────────────────────────────────────────────────
const wa = document.querySelector('.float-wa');
if (wa) setTimeout(() => { wa.style.animation = 'wapulse 2.8s ease infinite'; }, 5000);
