class SettingsApplier {
  static apply() {
    const S = window.__SETTINGS;
    if (!S) return;

    const root = document.documentElement;
    const cssMap = {
      primary_color: '--silver', secondary_color: '--slate-light', accent_color: '--accent',
      background_color: '--base-deep', surface_color: '--base-mid', text_primary_color: '--ivory',
      text_muted_color: '--slate-warm', border_color: '--panel-border', border_radius: '--radius',
      font_size_base: '--font-size'
    };
    for (const key in cssMap) { if (S[key]) root.style.setProperty(cssMap[key], S[key]); }

    if (S.heading_font || S.body_font) {
      const families = [];
      if (S.heading_font) families.push(S.heading_font + ':wght@300;400;500;600;700');
      if (S.body_font && S.body_font !== S.heading_font) families.push(S.body_font + ':wght@300;400;500;600');
      if (S.arabic_font) families.push(S.arabic_font + ':wght@300;400;500;600');
      if (families.length) {
        const fl = document.getElementById('fontLink');
        if (fl) fl.href = 'https://fonts.googleapis.com/css2?' + families.join('&') + '&display=swap';
      }
      const fonts = [S.body_font || 'Montserrat'];
      if (S.arabic_font) fonts.push("'" + S.arabic_font + "'");
      fonts.push('sans-serif');
      document.body.style.fontFamily = fonts.join(',');
    }

    document.querySelectorAll('[data-setting]').forEach(el => {
      const key = el.getAttribute('data-setting');
      if (S[key]) el.textContent = S[key];
    });

    document.querySelectorAll('[data-price-yearly]').forEach(el => {
      const yk = el.getAttribute('data-price-yearly');
      if (S[yk]) el.textContent = parseInt(S[yk]) * 12;
    });

    if (S.currency_symbol) {
      document.querySelectorAll('.price-currency').forEach(el => el.textContent = S.currency_symbol);
    }

    document.querySelectorAll('[data-features]').forEach(list => {
      const featKey = list.getAttribute('data-features');
      if (S[featKey]) {
        const items = S[featKey].split('|');
        const lis = list.querySelectorAll('li');
        for (let li = 0; li < lis.length && li < items.length; li++) {
          lis[li].textContent = items[li].trim();
        }
      }
    });

    document.querySelectorAll('[data-setting-img]').forEach(el => {
      const imgKey = el.getAttribute('data-setting-img');
      if (S[imgKey]) el.src = S[imgKey];
    });

    document.querySelectorAll('[data-href]').forEach(el => {
      const hk = el.getAttribute('data-href');
      if (hk && S[hk]) el.href = S[hk];
    });

    const toggleMap = {
      hero_enabled: '.hero', features_enabled: '#features', pricing_enabled: '#pricing',
      testimonials_enabled: '#testimonials', integrations_enabled: '#integrations',
      faq_enabled: '#faq', footer_enabled: '.footer-cta', nav_cta_visible: '.nav-cta'
    };
    for (const tKey in toggleMap) {
      if (S[tKey] === '0') {
        const tEl = document.querySelector(toggleMap[tKey]);
        if (tEl) tEl.style.display = 'none';
      }
    }

    if (S.site_status === 'maintenance') {
      document.body.innerHTML = '<div style="display:flex;align-items:center;justify-content:center;min-height:100vh;background:#0F1115;color:#E5E7EB;font-family:Montserrat,sans-serif;flex-direction:column;gap:16px;text-align:center;padding:40px;"><h1 style="font-size:2rem;font-weight:300;">Under Maintenance</h1><p style="color:#9CA3AF;">' + (S.site_maintenance_message || 'We\'ll be back soon!') + '</p></div>';
    }

    if (S.custom_css) {
      const t = document.createElement('style');
      t.textContent = S.custom_css;
      document.head.appendChild(t);
    }
  }
}
