(() => {
 'use strict';
 const CONFIG={
  engineUrl:'https://matricentre.ru/pl/fileservice/user/file/download/h/0b8dbfe70ba4b90bc382908cf4d7b51a.js',
  adminUserIds:[427328640],
  allowedAccountIds:[842325],
  logoUrl:'https://matricentre.ru/pl/fileservice/user/file/download/h/8665c7d7c19031eb9d5757412a7b0a42.jpg'
 };
 const KEY='DAZY_EVENT_PANEL_V10',BUTTON='dazy-event-launcher-v10';
 let loading=null;
 const allowed=()=>window.top===window&&location.origin==='https://matricentre.ru'&&/^\/pl\/cms\/page\/?$/.test(location.pathname)&&CONFIG.adminUserIds.map(String).includes(String(window.accountUserId))&&CONFIG.allowedAccountIds.map(String).includes(String(window.accountId));
 function message(text){
  let el=document.getElementById('dazy-launcher-message-v10');
  if(!el){el=document.createElement('div');el.id='dazy-launcher-message-v10';el.setAttribute('role','status');el.style.cssText='position:fixed;left:18px;bottom:96px;z-index:2147483646;max-width:360px;padding:14px 17px;border:1px solid #8050a5;border-radius:12px;background:#1b1425;color:#f0dcff;font:13px/1.6 Arial,sans-serif;box-shadow:0 10px 35px #0008;';document.body.append(el);}
  el.textContent=text;return el;
 }
 async function load(){
  if(!allowed())return;
  if(window[KEY]){document.getElementById('dazy-launcher-message-v10')?.remove();window[KEY].open();return;}
  if(loading)return loading;
  if(!CONFIG.engineUrl){message('DAZY: укажи engineUrl в загрузчике темы — HTTPS-ссылку на JS из архива.');return;}
  let url;
  try{url=new URL(CONFIG.engineUrl);if(url.protocol!=='https:'||url.username||url.password)throw Error();}catch{message('DAZY: в engineUrl нужна полная HTTPS-ссылка на файл программы.');return;}
  message('Загружаю панель DAZY…');
  loading=new Promise((resolve,reject)=>{
   const script=document.createElement('script');script.src=url.href;script.async=true;script.referrerPolicy='no-referrer';
   const timer=setTimeout(()=>{script.remove();reject(Error('Панель не загрузилась за 30 секунд. Проверь ссылку и доступность файла.'));},30000);
   script.onload=()=>{clearTimeout(timer);if(!window[KEY]){script.remove();reject(Error('Файл загружен, но панель не найдена. Проверь, что это JS из архива и доступ разрешён.'));return;}resolve();};
   script.onerror=()=>{clearTimeout(timer);script.remove();reject(Error('Не удалось загрузить JS. Проверь engineUrl и разрешения файлового хранилища.'));};
   document.head.append(script);
  }).then(()=>{document.getElementById('dazy-launcher-message-v10')?.remove();if(allowed())window[KEY].open();}).catch(error=>message('DAZY: '+error.message)).finally(()=>{loading=null;});
  return loading;
 }
 function mount(){
  if(!document.body||!allowed())return false;
  if(document.getElementById(BUTTON))return true;
  const button=document.createElement('button');button.id=BUTTON;button.type='button';button.title='DAZY · Подготовка мероприятия';button.setAttribute('aria-label','Открыть DAZY');
  button.style.cssText='position:fixed;left:18px;bottom:18px;width:64px;height:64px;padding:0;border:1px solid #8651a7;border-radius:17px;overflow:hidden;cursor:pointer;z-index:2147483645;background:#131019;box-shadow:0 5px 24px #6d319566;transition:box-shadow .2s,transform .2s;';
  const img=document.createElement('img');img.src=CONFIG.logoUrl;img.alt='DAZY';img.style.cssText='display:block;width:100%;height:100%;object-fit:contain';button.append(img);
  button.addEventListener('mouseenter',()=>{button.style.boxShadow='0 5px 30px #aa5cee88';button.style.transform='translateY(-2px)';});
  button.addEventListener('mouseleave',()=>{button.style.boxShadow='0 5px 24px #6d319566';button.style.transform='';});
  button.addEventListener('click',load);document.body.append(button);return true;
 }
 if(!mount()){let tries=0;const timer=setInterval(()=>{if(mount()||++tries>=100)clearInterval(timer);},150);}
})();
