(function(){
var C={"key":"0b9ac3dc1b62303001d29ca9","title":"Chat with us","color":"#d4a855","greeting":"Hi! How can we help?","base":"https:\/\/bot.cremonkey.com\/"};var open=false;var sk=localStorage.getItem('mb_wc_'+C.key)||'';var lastId=0;
var btn=document.createElement('div');btn.innerHTML='💬';
btn.style.cssText='position:fixed;bottom:20px;right:20px;width:56px;height:56px;border-radius:50%;background:'+C.color+';color:#fff;font-size:26px;display:flex;align-items:center;justify-content:center;cursor:pointer;box-shadow:0 4px 12px rgba(0,0,0,.25);z-index:99999';
var box=document.createElement('div');
box.style.cssText='position:fixed;bottom:88px;right:20px;width:330px;max-width:92vw;height:440px;background:#fff;border-radius:12px;box-shadow:0 8px 30px rgba(0,0,0,.25);display:none;flex-direction:column;overflow:hidden;z-index:99999;font-family:sans-serif';
box.innerHTML='
'+C.title+'
';
document.body.appendChild(btn);document.body.appendChild(box);
var msgs=box.querySelector('#mbwc_msgs');
function add(t,who){var d=document.createElement('div');d.style.cssText='margin:6px 0;padding:8px 10px;border-radius:10px;max-width:80%;'+(who=='user'?'background:'+C.color+';color:#fff;margin-left:auto':'background:#f1f1f1;color:#222');d.textContent=t;msgs.appendChild(d);msgs.scrollTop=msgs.scrollHeight;}
btn.onclick=function(){open=!open;box.style.display=open?'flex':'none';if(open&&!msgs.children.length){add(C.greeting,'bot');poll();}};
function send(){var i=box.querySelector('#mbwc_in');var t=i.value.trim();if(!t)return;i.value='';add(t,'user');
var fd=new FormData();fd.append('widget_key',C.key);fd.append('session_key',sk);fd.append('message',t);
fetch(C.base+'webchat/send',{method:'POST',body:fd}).then(r=>r.json()).then(function(r){if(r.session_key){sk=r.session_key;localStorage.setItem('mb_wc_'+C.key,sk);}if(r.reply)add(r.reply,'bot');if(r.last_id)lastId=r.last_id;});}
box.querySelector('#mbwc_send').onclick=send;
box.querySelector('#mbwc_in').addEventListener('keydown',function(e){if(e.key=='Enter')send();});
function poll(){if(!sk)return;fetch(C.base+'webchat/poll?widget_key='+C.key+'&session_key='+sk+'&since_id='+lastId).then(r=>r.json()).then(function(r){(r.messages||[]).forEach(function(m){add(m.message_content,m.sender=='user'?'user':'bot');lastId=m.id;});});}
setInterval(function(){if(open)poll();},4000);
})();