(function () {
  const script = document.currentScript;
  const form = script?.dataset?.form || 'demo';
  const title = script?.dataset?.title || 'Répondre au sondage';
  const description = script?.dataset?.description || 'Donnez votre avis en moins de 2 minutes.';
  const base = script?.dataset?.base || (script?.src ? script.src.split('/').slice(0, -1).join('/') : '.');
  const surveyUrl = `${base}/survey.php?form=${encodeURIComponent(form)}&embed=1`;

  const css = `
    .mk-survey-bubble{position:fixed;right:18px;bottom:18px;z-index:2147483000;display:flex;align-items:center;gap:10px;background:linear-gradient(135deg,#c8a84b,#e8c870);color:#08121d;border:none;border-radius:999px;padding:14px 18px;box-shadow:0 12px 30px rgba(0,0,0,.25);font:800 14px Inter,system-ui,sans-serif;cursor:pointer}
    .mk-survey-toast{position:fixed;right:18px;bottom:84px;width:320px;max-width:calc(100vw - 24px);z-index:2147483000;border-radius:22px;padding:18px;background:rgba(10,26,40,.98);color:#fff;border:1px solid rgba(255,255,255,.08);box-shadow:0 18px 50px rgba(0,0,0,.35);transform:translateY(16px);opacity:0;pointer-events:none;transition:.25s ease}
    .mk-survey-toast.show{transform:none;opacity:1;pointer-events:auto}
    .mk-survey-toast h3{margin:0 0 8px;font:800 20px Inter,system-ui,sans-serif}
    .mk-survey-toast p{margin:0 0 14px;color:rgba(255,255,255,.72);font:400 14px/1.6 Inter,system-ui,sans-serif}
    .mk-survey-actions{display:flex;gap:10px;flex-wrap:wrap}
    .mk-survey-btn{display:inline-flex;align-items:center;justify-content:center;min-height:42px;padding:0 14px;border-radius:999px;border:none;cursor:pointer;font:700 14px Inter,system-ui,sans-serif}
    .mk-survey-btn.primary{background:linear-gradient(135deg,#c8a84b,#e8c870);color:#08121d}
    .mk-survey-btn.secondary{background:rgba(255,255,255,.08);color:#fff;border:1px solid rgba(255,255,255,.08)}
    .mk-survey-overlay{position:fixed;inset:0;display:none;place-items:center;background:rgba(3,8,14,.78);backdrop-filter:blur(4px);z-index:2147483640;padding:18px}
    .mk-survey-overlay.show{display:grid}
    .mk-survey-modal{width:min(920px,100%);height:min(88vh,860px);border-radius:26px;overflow:hidden;background:#07111c;box-shadow:0 28px 80px rgba(0,0,0,.45);position:relative}
    .mk-survey-close{position:absolute;top:14px;right:14px;z-index:2;width:42px;height:42px;border-radius:999px;border:none;background:rgba(7,17,28,.8);color:#fff;font-size:20px;cursor:pointer}
    .mk-survey-frame{width:100%;height:100%;border:0;background:#07111c}
    @media (max-width:640px){.mk-survey-toast{right:12px;left:12px;bottom:78px;width:auto}.mk-survey-overlay{padding:0}.mk-survey-modal{width:100vw;height:100vh;max-width:none;max-height:none;border-radius:0}}
  `;

  const style = document.createElement('style');
  style.textContent = css;
  document.head.appendChild(style);

  const bubble = document.createElement('button');
  bubble.className = 'mk-survey-bubble';
  bubble.type = 'button';
  bubble.innerHTML = '💬 Répondre au sondage';

  const toast = document.createElement('div');
  toast.className = 'mk-survey-toast';
  toast.innerHTML = `
    <h3>${title}</h3>
    <p>${description}</p>
    <div class="mk-survey-actions">
      <button type="button" class="mk-survey-btn primary" data-action="start">Commencer</button>
      <button type="button" class="mk-survey-btn secondary" data-action="close">Plus tard</button>
    </div>
  `;

  const overlay = document.createElement('div');
  overlay.className = 'mk-survey-overlay';
  overlay.innerHTML = `
    <div class="mk-survey-modal" role="dialog" aria-modal="true" aria-label="Sondage">
      <button type="button" class="mk-survey-close" aria-label="Fermer">×</button>
      <iframe class="mk-survey-frame" src="about:blank" loading="lazy" title="Sondage"></iframe>
    </div>
  `;

  const frame = overlay.querySelector('.mk-survey-frame');
  const openToast = () => toast.classList.add('show');
  const closeToast = () => toast.classList.remove('show');
  const openSurvey = () => {
    closeToast();
    overlay.classList.add('show');
    frame.src = surveyUrl;
  };
  const closeSurvey = () => {
    overlay.classList.remove('show');
    frame.src = 'about:blank';
  };

  bubble.addEventListener('click', () => {
    if (window.innerWidth <= 640) {
      openSurvey();
      return;
    }
    toast.classList.toggle('show');
  });
  toast.addEventListener('click', (event) => {
    const action = event.target?.dataset?.action;
    if (action === 'start') openSurvey();
    if (action === 'close') closeToast();
  });
  overlay.querySelector('.mk-survey-close').addEventListener('click', closeSurvey);
  overlay.addEventListener('click', (event) => {
    if (event.target === overlay) closeSurvey();
  });
  document.addEventListener('keydown', (event) => {
    if (event.key === 'Escape') {
      closeToast();
      closeSurvey();
    }
  });

  document.body.appendChild(bubble);
  document.body.appendChild(toast);
  document.body.appendChild(overlay);
})();
