(() => {
  const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  const header = document.querySelector('.site-header');

  if (header) {
    header.classList.add('tm-site-header');
    const updateHeader = () => header.classList.toggle('is-scrolled', window.scrollY > 32);
    updateHeader();
    window.addEventListener('scroll', updateHeader, { passive: true });
  }

  const introSelectors = [
    'main>section:first-child .approach-eyebrow', 'main>section:first-child h1',
    'main>section:first-child h1 + p', 'main>section:first-child [class*="hero-copy"]',
    'main>section:first-child [class*="hero-grid"]>div', 'main>section:first-child [class*="hero-layout"]>div',
    'main>section:first-child .legal-meta', 'main>section:first-child .hero-stage-cards',
    'main>section:first-child .pi-topic-strip',
    '.approach-page .approach-hero-intro>*'
  ];
  const introItems = [...new Set(document.querySelectorAll(introSelectors.join(',')))]
    .filter(item => !item.matches('.approach-page .approach-hero-intro'));
  introItems.forEach((item, index) => {
    item.classList.add('tm-page-intro');
    item.style.setProperty('--tm-delay', `${80 + Math.min(index, 5) * 90}ms`);
  });
  requestAnimationFrame(() => {
    document.documentElement.classList.add('tm-motion-ready');
    introItems.forEach(item => item.classList.add('is-visible'));
  });

  if (reduceMotion || !('IntersectionObserver' in window)) {
    document.documentElement.classList.add('motion-reduced');
    return;
  }

  const revealSelectors = [
    '.approach-page .approach-discipline>.approach-shell>header', '.approach-page .service-grid>*',
    '.approach-page .hero-lifecycle-copy>*', '.approach-page .hero-stage-cards>article',
    '.approach-page .review-groups>article',
    '.approach-page .capability-grid>li', '.approach-page .question-grid>li',
    '.approach-page .portfolio-dashboard>*', '.approach-page blockquote',
    '.approach-page .ownership-heading>*', '.approach-page .ownership-story>*', '.approach-page .relationship-map>span',
    '.approach-page .service-pair:not(.manage-pair)>.compact-service',
    '.who-page .who-section-grid>*', '.who-page .philosophy-inner>*',
    '.who-page .philosophy-steps>div', '.who-page .beliefs-heading',
    '.who-page .beliefs-grid>article', '.who-page .ambition-content>*', '.who-page .ambition-visual',
    '.intelligence-page .pi-question-grid>article', '.intelligence-page .pi-closing-v2 .approach-shell>*',
    '.contact-page .assist-selector', '.contact-page .assist-groups>div',
    '.contact-page .enquiry-fields', '.contact-page .form-guide', '.contact-page .form-main',
    '.legal-page .legal-nav', '.legal-page .disclaimer-summary',
    '.legal-page .legal-document>section', '.legal-page .disclaimer-document>section',
    '.legal-page .legal-note', '.legal-page .disclaimer-contact'
  ];
  const revealItems = [...new Set(document.querySelectorAll(revealSelectors.join(',')))]
    .filter(item => !item.matches('.intelligence-page .pi-wide-card'));
  const groupCounts = new Map();
  revealItems.forEach(item => {
    const group = item.closest('.hero-stage-cards,.review-groups,.capability-grid,.question-grid,.philosophy-steps,.beliefs-grid,.pi-question-grid,.assist-groups,.legal-document,.disclaimer-document,.footer-main') || item.parentElement;
    const index = groupCounts.get(group) || 0;
    groupCounts.set(group, index + 1);
    item.classList.add('tm-site-reveal');
    item.style.setProperty('--tm-delay', `${Math.min(index, 6) * 65}ms`);
  });

  const addVariant = (selector, variant) => {
    document.querySelectorAll(selector).forEach(item => item.classList.add(variant));
  };

  addVariant('.approach-page .service-grid>*:first-child,.approach-page .approach-discipline>.approach-shell>header', 'tm-from-left');
  addVariant('.approach-page .service-grid>*:last-child,.approach-page blockquote', 'tm-from-right');
  addVariant('.approach-page .hero-stage-cards>article,.approach-page .review-groups>article', 'tm-scale-in');
  addVariant('.approach-page .portfolio-dashboard>*', 'tm-clip-in');
  addVariant('.approach-page .ownership-heading>div,.approach-page .ownership-copy', 'tm-from-left');
  addVariant('.approach-page .ownership-heading>h2,.approach-page .relationship-map', 'tm-from-right');
  addVariant('.approach-page .relationship-map>span', 'tm-scale-in');
  addVariant('.approach-page .capability-grid>li:nth-child(odd),.approach-page .question-grid>li:nth-child(odd)', 'tm-from-left-soft');
  addVariant('.approach-page .capability-grid>li:nth-child(even),.approach-page .question-grid>li:nth-child(even)', 'tm-from-right-soft');
  addVariant('.approach-page .service-pair:not(.manage-pair)>.compact-service:first-child', 'tm-from-left');
  addVariant('.approach-page .service-pair:not(.manage-pair)>.compact-service:last-child', 'tm-from-right');

  addVariant('.who-page .who-section-grid>header,.who-page .beliefs-heading,.who-page .ambition-content>*', 'tm-from-left');
  addVariant('.who-page .who-section-grid>.who-content,.who-page .ambition-visual', 'tm-from-right');
  addVariant('.who-page .philosophy-steps>div', 'tm-scale-in');
  addVariant('.who-page .beliefs-grid>article:nth-child(odd)', 'tm-tilt-left');
  addVariant('.who-page .beliefs-grid>article:nth-child(even)', 'tm-tilt-right');

  addVariant('.intelligence-page .pi-question-grid>article:nth-child(odd)', 'tm-from-left-soft');
  addVariant('.intelligence-page .pi-question-grid>article:nth-child(even)', 'tm-from-right-soft');
  addVariant('.intelligence-page .pi-closing-v2 .approach-shell', 'tm-clip-in');

  addVariant('.contact-page .assist-selector,.contact-page .form-guide', 'tm-from-left');
  addVariant('.contact-page .enquiry-fields,.contact-page .form-main', 'tm-from-right');
  addVariant('.contact-page .assist-groups>div', 'tm-scale-in');
  document.querySelectorAll('.contact-page .assist-groups>div').forEach(group => {
    group.querySelectorAll('label').forEach((label, index) => label.style.setProperty('--co-item-delay', `${180 + index * 55}ms`));
  });
  document.querySelectorAll('.contact-page .form-main .field-grid>label').forEach((label, index) => {
    label.style.setProperty('--co-field-delay', `${160 + index * 48}ms`);
  });

  addVariant('.legal-page .legal-nav,.legal-page .disclaimer-summary', 'tm-from-left');
  addVariant('.legal-page .legal-document>section:nth-child(odd),.legal-page .disclaimer-document>section:nth-child(odd)', 'tm-from-right-soft');
  addVariant('.legal-page .legal-document>section:nth-child(even),.legal-page .disclaimer-document>section:nth-child(even)', 'tm-from-left-soft');
  addVariant('.legal-page .legal-note,.legal-page .disclaimer-contact', 'tm-scale-in');

  document.querySelectorAll('.beliefs-grid>article,.pi-question-grid>article,.hero-stage-cards>article,.review-groups>article').forEach(item => item.classList.add('tm-motion-card'));
  document.querySelectorAll('.primary-dark-btn,.outline-light-btn,.footer-cta-button,.who-ambition a,.pi-closing-v2 a,.enquiry-fields button,.site-header>div>a:last-of-type').forEach(item => item.classList.add('tm-site-cta'));

  const revealObserver = new IntersectionObserver(entries => {
    entries.forEach(entry => {
      if (!entry.isIntersecting) return;
      entry.target.classList.add('is-visible');
      revealObserver.unobserve(entry.target);
    });
  }, { threshold: 0.1, rootMargin: '0px 0px -48px' });
  revealItems.forEach(item => revealObserver.observe(item));

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