/* marketing-css-fix.js — Workaround for Chrome's <link rel="stylesheet">
 * rule-count truncation. Some Chrome versions silently drop rules past ~257
 * when loading a CSS file via <link>, even though the file is valid.
 * This loader fetches the CSS and injects it as a <style> tag to bypass
 * the issue. Idempotent: if the link already provides all rules, no-op. */
(function () {
  if (typeof window === 'undefined' || !window.document) return;
  function inject() {
    var link = document.querySelector('link[rel="stylesheet"][href*="marketing.css"]');
    if (link && link.sheet && link.sheet.cssRules && link.sheet.cssRules.length >= 320) {
      return; // Browser already parsed all rules; nothing to do.
    }
    if (document.querySelector('style[data-marketing-css]')) return; // Already injected.
    var href = link ? link.href : '/public/css/marketing.css?v=3.11-skyclean';
    fetch(href, { credentials: 'same-origin' })
      .then(function (r) { return r.text(); })
      .then(function (css) {
        if (document.querySelector('style[data-marketing-css]')) return;
        var style = document.createElement('style');
        style.setAttribute('data-marketing-css', '1');
        style.textContent = css;
        if (link && link.parentNode) {
          link.parentNode.insertBefore(style, link.nextSibling);
        } else {
          document.head.appendChild(style);
        }
      })
      .catch(function () { /* link fallback */ });
  }
  function bootstrap() {
    // Wait for link to load (or fail) before checking
    var link = document.querySelector('link[rel="stylesheet"][href*="marketing.css"]');
    if (link && !link.sheet) {
      link.addEventListener('load', inject, { once: true });
      link.addEventListener('error', inject, { once: true });
      // Hard timeout fallback in case neither fires
      setTimeout(inject, 1500);
    } else {
      inject();
    }
  }
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', bootstrap);
  } else {
    bootstrap();
  }
})();