(() => {
  const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  const hero = document.querySelector('.dark-hero');
  const header = document.querySelector('.site-header');
  if (hero) requestAnimationFrame(() => hero.classList.add('is-ready'));
  if (header) {
    requestAnimationFrame(() => header.classList.add('is-ready', 'tm-header-motion'));
    const updateHeader = () => header.classList.toggle('is-scrolled', window.scrollY > 36);
    updateHeader();
    window.addEventListener('scroll', updateHeader, { passive: true });
  }
  if (reduceMotion || !('IntersectionObserver' in window)) {
    document.documentElement.classList.add('motion-reduced');
    return;
  }

  const selectors = [
    '.focus-section .center-heading', '.focus-card',
    '.industries-section .center-heading', '.highlight-item', '.industry-card',
    '.approach-home .center-heading', '.discipline-intro article', '.discipline-card', '.signature-band',
    '.editorial-section .portfolio-lead', '.editorial-section .portfolio-content', '#clearance .clearance-heading',
    '.split-feature>div', '.clearance-grid>div', '.question-list li', '.closing-question',
    '.ownership-inner>p', '.ownership-inner>h2', '.ownership-inner>h3', '.ownership-inner>.ownership-copy',
    '.stats-grid>div', '#ecosystem .center-heading', '.ecosystem-grid>div',
    '.why-grid>div', '.why-grid>ul', '.why-list li', '.asset-statement', '.closing-cta'
  ];

  const items = [...document.querySelectorAll(selectors.join(','))];
  const delayGroups = new Map();
  items.forEach(item => {
    const group = item.closest('.focus-grid,.industry-highlights,.industry-grid,.discipline-intro,.discipline-grid,.question-list,.stats-grid,.ecosystem-grid,.why-list,.footer-main') || item.parentElement;
    const groupIndex = delayGroups.get(group) || 0;
    delayGroups.set(group, groupIndex + 1);
    item.classList.add('tm-reveal');
    item.style.setProperty('--reveal-delay', `${Math.min(groupIndex, 5) * 70}ms`);
  });

  document.querySelectorAll('.split-feature>div:first-child,.clearance-grid>div:first-child,.why-grid>div').forEach(item => item.classList.add('tm-reveal-left'));
  document.querySelectorAll('.split-feature>div:last-child,.clearance-grid>div:last-child,.why-grid>ul').forEach(item => item.classList.add('tm-reveal-right'));
  document.querySelectorAll('.focus-card,.industry-card').forEach((item, index) => item.classList.add(index % 2 ? 'tm-tilt-right' : 'tm-tilt-left'));
  document.querySelectorAll('.highlight-item,.discipline-intro article,.stats-grid>div').forEach(item => item.classList.add('tm-scale-in'));
  document.querySelectorAll('.discipline-card:first-child,.portfolio-lead').forEach(item => item.classList.add('tm-from-left'));
  document.querySelectorAll('.discipline-card:last-child,.portfolio-content').forEach(item => item.classList.add('tm-from-right'));
  document.querySelectorAll('.ecosystem-grid>div').forEach((item, index) => {
    item.classList.add('tm-ecosystem-card', index % 2 ? 'tm-from-right-soft' : 'tm-from-left-soft');
    item.style.setProperty('--reveal-delay', `${index * 90}ms`);
  });
  document.querySelectorAll('.closing-cta,.asset-statement').forEach(item => item.classList.add('tm-scale-in'));
  document.querySelectorAll('.primary-dark-btn,.outline-light-btn,.footer-cta-button').forEach(item => item.classList.add('tm-animated-cta'));

  const observer = new IntersectionObserver(entries => {
    entries.forEach(entry => {
      if (!entry.isIntersecting) return;
      entry.target.classList.add('is-visible');
      observer.unobserve(entry.target);
    });
  }, { threshold: 0.12, rootMargin: '0px 0px -55px' });

  items.forEach(item => observer.observe(item));

  const sectionObserver = new IntersectionObserver(entries => {
    entries.forEach(entry => entry.target.classList.toggle('tm-section-active', entry.isIntersecting));
  }, { threshold: 0.08, rootMargin: '-12% 0px -12%' });
  document.querySelectorAll('main>section').forEach(section => sectionObserver.observe(section));

  const statsSection = document.querySelector('.stats-section');
  if (statsSection) {
    const easeOutQuart = progress => 1 - Math.pow(1 - progress, 4);
    const animateCount = element => {
      const target = Number(element.dataset.count);
      const suffix = element.dataset.suffix || '';
      const duration = 1400;
      let startTime;

      const update = timestamp => {
        if (!startTime) startTime = timestamp;
        const progress = Math.min((timestamp - startTime) / duration, 1);
        element.textContent = `${Math.round(target * easeOutQuart(progress))}${suffix}`;
        if (progress < 1) requestAnimationFrame(update);
      };

      requestAnimationFrame(update);
    };

    const statsObserver = new IntersectionObserver(entries => {
      entries.forEach(entry => {
        if (!entry.isIntersecting) return;
        statsSection.classList.add('stats-active');
        statsSection.querySelectorAll('.stat-number').forEach(animateCount);
        statsObserver.unobserve(entry.target);
      });
    }, { threshold: 0.45 });

    statsObserver.observe(statsSection);
  }
})();
