/* Phoenix Smart Solutions — script.js v5 */
(function(){
'use strict';

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

/* ── Products dropdown — click toggle (not hover) ── */
/* Hover-based dropdowns close when mouse crosses the gap.
   Click toggle is reliable on both desktop and touch. */
var ddItems = document.querySelectorAll('.nav-has-dd');
ddItems.forEach(function(dd){
  var btn = dd.querySelector('.nav-dd-btn');
  var panel = dd.querySelector('.nav-dd-panel');
  if(!btn || !panel) return;

  btn.addEventListener('click', function(e){
    e.stopPropagation();
    var isOpen = dd.classList.contains('dd-open');
    // Close all dropdowns first
    ddItems.forEach(function(d){ d.classList.remove('dd-open'); d.querySelector('.nav-dd-btn') && d.querySelector('.nav-dd-btn').setAttribute('aria-expanded','false'); });
    // Toggle this one
    if(!isOpen){
      dd.classList.add('dd-open');
      btn.setAttribute('aria-expanded','true');
    }
  });
});

// Close dropdown when clicking outside
document.addEventListener('click', function(){
  ddItems.forEach(function(d){
    d.classList.remove('dd-open');
    var b = d.querySelector('.nav-dd-btn');
    if(b) b.setAttribute('aria-expanded','false');
  });
});

// Close on Escape
document.addEventListener('keydown', function(e){
  if(e.key === 'Escape'){
    ddItems.forEach(function(d){
      d.classList.remove('dd-open');
    });
    closeMob();
  }
});

/* ── Mobile menu ── */
var ham = document.querySelector('.hamburger');
var mob = document.getElementById('mob-nav-id');
var ov  = document.querySelector('.mob-overlay');

function openMob(){
  if(!mob) return;
  mob.classList.add('open');
  mob.setAttribute('aria-hidden','false');
  ov  && ov.classList.add('vis');
  ham && ham.classList.add('open');
  ham && ham.setAttribute('aria-expanded','true');
  document.body.style.overflow = 'hidden';
}
function closeMob(){
  if(!mob) return;
  mob.classList.remove('open');
  mob.setAttribute('aria-hidden','true');
  ov  && ov.classList.remove('vis');
  ham && ham.classList.remove('open');
  ham && ham.setAttribute('aria-expanded','false');
  document.body.style.overflow = '';
}

if(ham) ham.addEventListener('click', function(e){
  e.stopPropagation();
  mob && mob.classList.contains('open') ? closeMob() : openMob();
});
if(ov) ov.addEventListener('click', closeMob);
if(mob) mob.querySelectorAll('a').forEach(function(a){
  a.addEventListener('click', function(){ setTimeout(closeMob, 80); });
});

/* ── Scroll reveal ── */
var ro = new IntersectionObserver(function(entries){
  entries.forEach(function(e){
    if(e.isIntersecting){ e.target.classList.add('in'); ro.unobserve(e.target); }
  });
}, { threshold: 0.07, rootMargin: '0px 0px -24px 0px' });
document.querySelectorAll('.fade,.fl,.fr').forEach(function(el){ ro.observe(el); });

/* ── Animated counters ── */
function countUp(el){
  var t = parseInt(el.dataset.target, 10) || 0;
  var s = el.dataset.suffix || '';
  var dur = 1400, st = null;
  function ease(p){ return p<.5 ? 2*p*p : -1+(4-2*p)*p; }
  function step(ts){
    if(!st) st = ts;
    var p = Math.min((ts-st)/dur, 1);
    el.textContent = Math.round(ease(p)*t).toLocaleString() + s;
    if(p < 1) requestAnimationFrame(step);
  }
  requestAnimationFrame(step);
}
var co = new IntersectionObserver(function(entries){
  entries.forEach(function(e){ if(e.isIntersecting){ countUp(e.target); co.unobserve(e.target); } });
}, {threshold: 0.5});
document.querySelectorAll('.counter').forEach(function(el){ co.observe(el); });

/* ── Hero card 3D tilt — desktop hover only ── */
var hcard = document.querySelector('.hero-card');
if(hcard && window.matchMedia('(hover:hover) and (pointer:fine)').matches){
  hcard.addEventListener('mousemove', function(e){
    var r = hcard.getBoundingClientRect();
    var x = ((e.clientX - r.left)/r.width  - .5) * 14;
    var y = ((e.clientY - r.top) /r.height - .5) * -10;
    hcard.style.animation = 'none';
    hcard.style.transform = 'rotateX('+y+'deg) rotateY('+x+'deg)';
  });
  hcard.addEventListener('mouseleave', function(){
    hcard.style.animation = '';
    hcard.style.transform = '';
  });
}

/* ── Product card 3D tilt — desktop only ── */
if(window.matchMedia('(hover:hover) and (pointer:fine)').matches){
  document.querySelectorAll('.pc').forEach(function(card){
    card.addEventListener('mousemove', function(e){
      var r = card.getBoundingClientRect();
      var x = ((e.clientX - r.left)/r.width  - .5) * 10;
      var y = ((e.clientY - r.top) /r.height - .5) * -7;
      card.style.transform = 'translateY(-7px) rotateX('+y+'deg) rotateY('+x+'deg)';
    });
    card.addEventListener('mouseleave', function(){ card.style.transform = ''; });
  });
}

/* ── Product tabs ── */
function switchTab(id){
  document.querySelectorAll('.tab').forEach(function(b){
    b.classList.toggle('on', b.dataset.tab === id);
    b.setAttribute('aria-selected', b.dataset.tab === id ? 'true' : 'false');
  });
  document.querySelectorAll('.tab-panel').forEach(function(p){
    p.classList.toggle('on', p.id === id);
  });
}
document.querySelectorAll('.tab').forEach(function(btn){
  btn.addEventListener('click', function(){ switchTab(btn.dataset.tab); });
});
var hash = location.hash.slice(1);
if(hash && document.querySelector('.tab[data-tab="'+hash+'"]')) switchTab(hash);

/* ── Brand filter ── */
document.querySelectorAll('.bf').forEach(function(btn){
  btn.addEventListener('click', function(){
    document.querySelectorAll('.bf').forEach(function(b){ b.classList.remove('on'); });
    btn.classList.add('on');
    var f = btn.dataset.filter;
    document.querySelectorAll('.brand-tile').forEach(function(t){
      t.style.display = (f === 'all' || t.dataset.cat === f) ? '' : 'none';
    });
  });
});

/* ── WhatsApp FAB ── */
var fab    = document.getElementById('wa-fab');
var fabBtn = fab && fab.querySelector('.wa-btn');
var fabForm= document.getElementById('wa-form');
var fabOk  = fab && fab.querySelector('.wa-ok');

if(fab && fabBtn){
  fabBtn.addEventListener('click', function(e){
    e.stopPropagation();
    fab.classList.toggle('open');
  });
  document.addEventListener('click', function(e){
    if(fab && !fab.contains(e.target)) fab.classList.remove('open');
  });
}
if(fabForm){
  fabForm.addEventListener('submit', function(e){
    e.preventDefault();
    var n = fabForm.querySelector('[name="name"]').value.trim();
    var p = fabForm.querySelector('[name="phone"]').value.trim();
    var t = fabForm.querySelector('[name="type"]').value;
    var m = (fabForm.querySelector('[name="message"]') || {value:''}).value.trim();
    if(!n || !p || !t) return;
    var msg = 'Hi Phoenix Smart Solutions! %F0%9F%91%8B'
      + '%0AName: '    + encodeURIComponent(n)
      + '%0APhone: '   + encodeURIComponent(p)
      + '%0AProject: ' + encodeURIComponent(t)
      + (m ? '%0AMessage: ' + encodeURIComponent(m) : '');
    window.open('https://wa.me/919786000666?text=' + msg, '_blank');
    fabForm.style.display = 'none';
    if(fabOk) fabOk.style.display = 'block';
  });
}

/* ── Contact form ── */
var cf = document.getElementById('contact-form');
var cd = document.getElementById('contact-done');
if(cf){
  cf.addEventListener('submit', function(e){
    e.preventDefault();
    var n  = cf.querySelector('[name="name"]').value.trim();
    var p  = cf.querySelector('[name="phone"]').value.trim();
    var sv = cf.querySelector('[name="service"]').value;
    var mg = (cf.querySelector('[name="message"]') || {value:''}).value.trim();
    if(!n || !p) return;
    var msg = 'Hi Phoenix Smart Solutions! %F0%9F%91%8B'
      + '%0AName: '  + encodeURIComponent(n)
      + '%0APhone: ' + encodeURIComponent(p)
      + (sv ? '%0AService: '  + encodeURIComponent(sv) : '')
      + (mg ? '%0AMessage: ' + encodeURIComponent(mg) : '');
    window.open('https://wa.me/919786000666?text=' + msg, '_blank');
    cf.style.display = 'none';
    if(cd) cd.style.display = 'block';
  });
}

/* ── Active nav link ── */
var pg = location.pathname.split('/').pop() || 'index.html';
document.querySelectorAll('[data-page]').forEach(function(a){
  if(pg.indexOf(a.dataset.page) !== -1) a.classList.add('active');
});

/* ── Smooth anchor scroll ── */
var navH = nav ? nav.offsetHeight : 68;
document.querySelectorAll('a[href^="#"]').forEach(function(a){
  a.addEventListener('click', function(e){
    var target = document.getElementById(a.getAttribute('href').slice(1));
    if(target){
      e.preventDefault();
      window.scrollTo({top: target.getBoundingClientRect().top + window.scrollY - navH - 8, behavior:'smooth'});
    }
  });
});

})();
