/* Nebulix Cloud — static site JS (no dependencies) */
(function () {
  // Mobile menu
  const toggle = document.querySelector('[data-menu-toggle]');
  const menu = document.querySelector('[data-menu]');
  if (toggle && menu) {
    toggle.addEventListener('click', () => {
      menu.classList.toggle('open');
      toggle.setAttribute('aria-expanded', menu.classList.contains('open'));
    });
  }

  // Pricing billing toggle
  document.querySelectorAll('[data-pricing]').forEach((root) => {
    const btns = root.querySelectorAll('[data-billing]');
    const prices = root.querySelectorAll('[data-price]');
    function set(period) {
      btns.forEach((b) => b.classList.toggle('active', b.dataset.billing === period));
      prices.forEach((el) => {
        const v = parseFloat(el.dataset[period]);
        if (!isNaN(v)) el.textContent = '$' + v.toFixed(2);
      });
    }
    btns.forEach((b) => b.addEventListener('click', () => set(b.dataset.billing)));
  });

  // FAQ
  document.querySelectorAll('[data-faq]').forEach((item) => {
    const btn = item.querySelector('button');
    btn && btn.addEventListener('click', () => item.classList.toggle('open'));
  });

  // Testimonials carousel
  document.querySelectorAll('[data-testimonials]').forEach((root) => {
    const cards = Array.from(root.querySelectorAll('.tcard'));
    const per = 3;
    let page = 0;
    const pages = Math.max(1, Math.ceil(cards.length / per));
    function render() {
      cards.forEach((c, i) => {
        const inRange = i >= page * per && i < page * per + per;
        c.style.display = inRange ? '' : 'none';
      });
    }
    render();
    root.querySelector('[data-prev]')?.addEventListener('click', () => { page = (page - 1 + pages) % pages; render(); });
    root.querySelector('[data-next]')?.addEventListener('click', () => { page = (page + 1) % pages; render(); });
  });

  // Auth tabs
  const tabs = document.querySelector('[data-auth-tabs]');
  if (tabs) {
    const slider = tabs.querySelector('.slider');
    const btns = tabs.querySelectorAll('button');
    const panes = document.querySelectorAll('[data-auth-pane]');
    btns.forEach((b, i) => b.addEventListener('click', () => {
      btns.forEach((x) => x.classList.toggle('active', x === b));
      slider.style.left = i === 0 ? '4px' : 'calc(50% + 0px)';
      panes.forEach((p) => p.style.display = (p.dataset.authPane === b.dataset.tab) ? '' : 'none');
    }));
  }

  // Password show/hide
  document.querySelectorAll('[data-toggle-pw]').forEach((btn) => {
    btn.addEventListener('click', () => {
      const target = document.querySelector(btn.dataset.togglePw);
      if (!target) return;
      const showing = target.type === 'text';
      target.type = showing ? 'password' : 'text';
      btn.innerHTML = showing
        ? '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>'
        : '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.5 18.5 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"/><line x1="1" y1="1" x2="23" y2="23"/></svg>';
    });
  });

  // Contact form fake submit
  document.querySelectorAll('[data-contact-form]').forEach((form) => {
    form.addEventListener('submit', (e) => {
      e.preventDefault();
      const btn = form.querySelector('[type="submit"]');
      if (btn) { btn.textContent = 'Message Sent ✓'; btn.disabled = true; }
    });
  });

  // Reveal on scroll
  if ('IntersectionObserver' in window) {
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => { if (e.isIntersecting) { e.target.classList.add('in'); io.unobserve(e.target); } });
    }, { rootMargin: '-50px' });
    document.querySelectorAll('.reveal').forEach((el) => io.observe(el));
  } else {
    document.querySelectorAll('.reveal').forEach((el) => el.classList.add('in'));
  }

  // Counters
  document.querySelectorAll('[data-counter]').forEach((el) => {
    const target = parseFloat(el.dataset.counter);
    const decimals = parseInt(el.dataset.decimals || '0', 10);
    const suffix = el.dataset.suffix || '';
    let start = null;
    const duration = 1500;
    function step(t) {
      if (!start) start = t;
      const p = Math.min((t - start) / duration, 1);
      const eased = 1 - Math.pow(1 - p, 3);
      el.textContent = (target * eased).toFixed(decimals) + suffix;
      if (p < 1) requestAnimationFrame(step);
    }
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => { if (e.isIntersecting) { requestAnimationFrame(step); io.unobserve(el); } });
    });
    io.observe(el);
  });

  // Year
  document.querySelectorAll('[data-year]').forEach((el) => el.textContent = new Date().getFullYear());
})();
