// WapiBot dynamic URL bridge - SAFE VERSION
(function(){
  const MAP=[
    {keys:['panel de administrador','panel clientes','panel cliente','painel cliente','painel de administrador'],msg:'open_client_panel_url'},
    {keys:['comprar mas licencias','comprar más licencias','comprar premium','comprar licencia','checkout','comenzar ahora'],msg:'open_checkout_url'},
    {keys:['recuperar acceso','recover access','recuperar senha','recuperar contraseña'],msg:'open_recover_access_url'},
    {keys:['iniciar sesion con panel','iniciar sesión con panel','iniciar sesion panel','iniciar sesión panel','login panel','entrar al panel'],msg:'open_login_panel_url'},
    {keys:['crear una cuenta gratuita','crear cuenta gratuita','registrate aqui','regístrate aquí','registrese aqui','regístrese aquí','free demo'],msg:'open_free_account_url'},
    {keys:['instalar','install'],msg:'open_install_url'},
    {keys:['desinstalar','desistalar','uninstall','desistall'],msg:'open_uninstall_url'},
    {keys:['soporte','ayuda','support'],msg:'open_support_whatsapp'}
  ];
  function norm(t){return (t||'').toString().toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g,'').replace(/[↗→»›]/g,'').replace(/[^a-z0-9ñ\s]/gi,' ').replace(/\s+/g,' ').trim();}
  const VALID=new Map(); MAP.forEach(i=>i.keys.forEach(k=>VALID.set(norm(k),i.msg)));
  function isClose(target){let n=target;for(let i=0;n&&i<6;i++,n=n.parentElement){const a=norm(n.getAttribute?.('aria-label')||n.getAttribute?.('title')||''),c=String(n.className||'').toLowerCase(),t=norm(n.innerText||n.textContent||'');if(['cerrar','close','fechar'].includes(a))return true;if(c.includes('close')||c.includes('modal-close')||c.includes('btn-close'))return true;if(t==='x'||t==='×')return true;}return false;}
  function text(n){return norm([n.getAttribute?.('data-url-action')||'',n.getAttribute?.('aria-label')||'',n.getAttribute?.('title')||'',n.value||'',n.innerText||n.textContent||''].join(' '));}
  function find(target){if(!target||isClose(target))return null;let n=target;for(let d=0;n&&d<7;d++,n=n.parentElement){if(n===document.body||n===document.documentElement)break;const tag=(n.tagName||'').toLowerCase(),role=(n.getAttribute?.('role')||'').toLowerCase(),type=(n.getAttribute?.('type')||'').toLowerCase(),cls=String(n.className||'').toLowerCase();const hasData=n.hasAttribute?.('data-url-action')||n.hasAttribute?.('data-url-key');const interactive=hasData||tag==='button'||tag==='a'||role==='button'||(tag==='input'&&['button','submit'].includes(type));const menuLike=cls.includes('cursor-pointer')||cls.includes('menu-item');if(!interactive&&!menuLike)continue;const tx=text(n);if(!tx||tx.length>70)continue;if(VALID.has(tx))return VALID.get(tx);for(const item of MAP){for(const k of item.keys){const kk=norm(k);if(tx.length<=70&&tx.includes(kk))return item.msg;}}}return null;}
  document.addEventListener('click',function(ev){const msg=find(ev.target);if(!msg)return;ev.preventDefault();ev.stopPropagation();ev.stopImmediatePropagation();try{chrome.runtime.sendMessage({message:msg});}catch(e){}},true);
})();