// Header scroll
const header = document.getElementById('header');
window.addEventListener('scroll', () => {
  header.classList.toggle('scrolled', window.scrollY > 30);
});

// Mobile menu
const toggle = document.getElementById('menuToggle');
const navLinks = document.getElementById('navLinks');
toggle.addEventListener('click', () => {
  navLinks.classList.toggle('open');
  toggle.classList.toggle('open');
});
navLinks.querySelectorAll('a').forEach(a => {
  a.addEventListener('click', () => {
    navLinks.classList.remove('open');
    toggle.classList.remove('open');
  });
});

// FAQ accordion
document.querySelectorAll('.faq-item').forEach(item => {
  const q = item.querySelector('.faq-q');
  const a = item.querySelector('.faq-a');
  q.addEventListener('click', () => {
    const isOpen = item.classList.contains('open');
    document.querySelectorAll('.faq-item').forEach(i => {
      i.classList.remove('open');
      i.querySelector('.faq-a').style.maxHeight = null;
    });
    if (!isOpen) {
      item.classList.add('open');
      a.style.maxHeight = a.scrollHeight + 'px';
    }
  });
});

// Scroll reveal
const reveals = document.querySelectorAll('.section-head, .card, .step, .plan, .table-wrap, .faq-item, .cta-inner');
reveals.forEach(el => el.classList.add('reveal'));
const io = new IntersectionObserver((entries) => {
  entries.forEach(e => {
    if (e.isIntersecting) {
      e.target.classList.add('in');
      io.unobserve(e.target);
    }
  });
}, { threshold: 0.12 });
reveals.forEach(el => io.observe(el));

// Terminal typing animation
(function(){
  const el = document.getElementById('termType');
  if(!el) return;
  const lines = [
    'dns-query netflix.com  →  rota Control D (Chicago, US)',
    'block ads.tracker.com  →  BLOQUEADO (NextDNS)',
    'block telemetry.samsung.com  →  BLOQUEADO',
    'resolve google.com  →  6ms (Anycast BR) OK',
    'status: protegido • 12.540 ameaças bloqueadas hoje'
  ];
  let li = 0, ci = 0, deleting = false;
  function tick(){
    const full = lines[li];
    if(!deleting){
      el.textContent = full.slice(0, ci++);
      if(ci > full.length){ deleting = true; setTimeout(tick, 1600); return; }
    } else {
      el.textContent = full.slice(0, ci--);
      if(ci < 0){ deleting = false; li = (li+1)%lines.length; ci = 0; }
    }
    setTimeout(tick, deleting ? 22 : 42);
  }
  tick();
})();
