/* ================================================================
   V4 — Script (countdown, FAQ, sticky bar, scroll reveal, register)
   ================================================================ */
document.addEventListener('DOMContentLoaded', () => {

  /* ── Smooth scroll + hash clear ──────────────────── */
  if (window.location.hash) {
    history.replaceState(null, '', window.location.pathname);
    window.scrollTo(0, 0);
  }
  document.querySelectorAll('a[href="#register-form"]').forEach(link => {
    link.addEventListener('click', (e) => {
      e.preventDefault();
      const t = document.getElementById('register-form');
      if (t) t.scrollIntoView({ behavior: 'smooth', block: 'center' });
    });
  });

  /* ── Countdown Timer ─────────────────────────────── */
  document.querySelectorAll('.countdown').forEach(el => {
    const deadline = el.dataset.deadline;
    if (!deadline) return;
    const target = new Date(deadline.replace(' ', 'T'));
    function tick() {
      let diff = Math.max(0, target - new Date());
      const s = Math.floor(diff / 1000);
      const vals = [Math.floor(s/86400), Math.floor((s%86400)/3600), Math.floor((s%3600)/60), s%60];
      const units = el.querySelectorAll('.cd-val');
      vals.forEach((v, i) => { if (units[i]) units[i].textContent = String(v).padStart(2, '0'); });
    }
    tick();
    setInterval(tick, 1000);
  });

  /* ── Sticky Bar ──────────────────────────────────── */
  const bar = document.querySelector('.sticky-bar');
  if (bar) {
    let t = 15 * 60;
    const timerEl = bar.querySelector('.timer');
    setInterval(() => {
      const mm = Math.floor(t / 60), ss = t % 60;
      if (timerEl) timerEl.textContent = String(mm).padStart(2,'0') + ':' + String(ss).padStart(2,'0');
      if (--t < 0) t = 15 * 60;
    }, 1000);
    window.addEventListener('scroll', () => bar.classList.toggle('visible', scrollY > 500));
  }

  /* ── FAQ Accordion ───────────────────────────────── */
  document.querySelectorAll('.faq-q').forEach(q => {
    q.addEventListener('click', () => {
      const item = q.parentElement;
      const wasOpen = item.classList.contains('open');
      document.querySelectorAll('.faq-item.open').forEach(i => i.classList.remove('open'));
      if (!wasOpen) item.classList.add('open');
    });
  });

  /* ── Scroll Reveal ───────────────────────────────── */
  const sels = '.reveal, .reveal-left, .reveal-right, .reveal-scale, .stagger-children';
  const obs = new IntersectionObserver(entries => {
    entries.forEach(e => { if (e.isIntersecting) { e.target.classList.add('revealed'); obs.unobserve(e.target); }});
  }, { threshold: 0.1, rootMargin: '0px 0px -50px 0px' });
  document.querySelectorAll(sels).forEach(el => obs.observe(el));
});

/* ── Registration Handler ──────────────────────────── */
function handleRegister(e) {
  e.preventDefault();
  const n = document.getElementById('reg-name').value.trim();
  const em = document.getElementById('reg-email').value.trim();
  const p = document.getElementById('reg-phone').value.trim();
  if (!n || !em || !p) { alert('Please fill in all fields.'); return false; }
  const reg = { name: n, email: em, phone: p, ts: new Date().toISOString() };
  const arr = JSON.parse(localStorage.getItem('mr_registrations') || '[]');
  arr.push(reg); localStorage.setItem('mr_registrations', JSON.stringify(arr));
  e.target.innerHTML = '<div style="text-align:center;padding:30px 0">'
    + '<svg width="60" height="60" viewBox="0 0 60 60" fill="none"><circle cx="30" cy="30" r="30" fill="#f0faf0"/><path d="M20 30l7 7 13-13" stroke="#228B22" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/></svg>'
    + '<h3 style="margin-top:16px;color:#228B22;font-size:20px;font-family:Playfair Display,serif">You\'re In!</h3>'
    + '<p style="color:#666;margin-top:8px;font-size:14px">Check your inbox for the Zoom link and workbook.</p>'
    + '<p style="color:#999;margin-top:8px;font-size:13px">See you on April 5 at 11:00 AM IST!</p></div>';
  return false;
}
