/* AI Project Universe V6.4 — shared Helpdesk design runtime. */
(() => {
  'use strict';
  const root = document.documentElement;
  const project = root.dataset.scrollTheme || 'it-helpdesk';
  const accents = {
    'it-helpdesk':'#2563eb','computer-vision':'#0f766e','incident-monitoring':'#dc2626',
    'kotlin-policy':'#7c3aed','malaysia-cost':'#0f766e','network-subnet':'#0369a1',
    'sentiment-analysis':'#7c3aed','url-shortener':'#4f46e5','academic-evidence':'#1d4ed8',
    'ai-architect':'#4f46e5','student-performance':'#6d28d9'
  };

  root.dataset.apuUnifiedDesign = 'v6.4-helpdesk';

  function updateThemeColor(){
    const meta = document.querySelector('meta[name="theme-color"]') || (() => {
      const node = document.createElement('meta');
      node.name = 'theme-color';
      document.head.appendChild(node);
      return node;
    })();
    meta.content = root.dataset.apuTheme === 'dark' ? '#07101f' : (accents[project] || '#2563eb');
  }

  function removeSentimentDuplicate(){
    if(project !== 'sentiment-analysis') return;
    document.querySelectorAll('#apuSentimentStudio,.apu-studio-jump,#apuSentimentToast').forEach(node => node.remove());
  }

  function initialize(){
    updateThemeColor();
    removeSentimentDuplicate();

    if(project === 'sentiment-analysis'){
      const observer = new MutationObserver(removeSentimentDuplicate);
      observer.observe(document.documentElement,{childList:true,subtree:true});
      window.addEventListener('pagehide',() => observer.disconnect(),{once:true});
    }
  }

  new MutationObserver(updateThemeColor).observe(root,{attributes:true,attributeFilter:['data-apu-theme']});
  window.addEventListener('apu:themechange',updateThemeColor);
  if(document.readyState === 'loading') document.addEventListener('DOMContentLoaded',initialize,{once:true});
  else initialize();
})();
