const atbotsChat=function(){const t=document.currentScript||document.querySelector("script[data-tenant-id]"),n=document.createElement("div");n.id="atbots-shadow-host",t.parentNode.insertBefore(n,t.nextSibling);const e=n.attachShadow({mode:"open"});let o={apiEndpoint:"https://bot.aaludradevelopers.com",inactivityTimeout:6e5,errorMessage:"Sorry, I'm having trouble connecting. Please try again later.",logoUrl:"logo.jpeg",clearOnReload:!1,statusCheckInterval:3e4,isActive:!1,welcomeMessage:null,hasShownContactForm:!1,botName:"AI Assistant",sounds:{sent:"https://widget.aaludradevelopers.com/static/sounds/message.mp3",received:"https://widget.aaludradevelopers.com/static/sounds/message.mp3"},formConfig:{showPhone:!1,showEmail:!0}};const a={history:"atbots_chat_history",lastActive:"atbots_last_active",initialized:"atbots_initialized",pageLoadId:"atbots_page_load_id",contactSubmitted:"atbots_contact_submitted",formSkipped:"atbots_form_skipped",userId:"atbots_user_id",formDisplayed:"atbots_form_displayed",formDisplayedAfterMsg:"atbots_form_displayed_after_msg",sessionId:"atbots_session_id",sessionLastActive:"atbots_session_last_active"},r={white:{"--primary-color":"#0000000","--primary-gradient":"linear-gradient(45deg, #ffffff, #f8fafc)","--text-color":"#000000","--bubble-sent":"#000000","--bubble-received":"#00000","--background-color":"#ffffff","--bubble-sent-color":"#000000"},black:{"--primary-color":"#111827","--primary-gradient":"linear-gradient(45deg, #1f2937, #374151)","--text-color":"#e5e7eb","--bubble-sent":"#4b5563","--bubble-received":"#1e293b","--background-color":"#0f172a","--bubble-sent-color":"#111827"},orange:{"--primary-color":"#f97316","--primary-gradient":"linear-gradient(45deg, #f97316, #ea580c)","--text-color":"#000000","--bubble-sent":"#ea580c","--bubble-received":"#ffedd5","--background-color":"#fff7ed","--bubble-sent-color":"#f97316"},yellow:{"--primary-color":"#fbbf24","--primary-gradient":"linear-gradient(45deg, #fbbf24, #f59e0b)","--text-color":"#4b5563","--bubble-sent":"#f59e0b","--bubble-received":"#fef3c7","--background-color":"#fffbeb","--bubble-sent-color":"#fbbf24"},red:{"--primary-color":"#ef4444","--primary-gradient":"linear-gradient(45deg, #ef4444, #dc2626)","--text-color":"#00000","--bubble-sent":"#dc2626","--bubble-received":"#fee2e2","--background-color":"#fef2f2","--bubble-sent-color":"#ef4444"},green:{"--primary-color":"#22c55e","--primary-gradient":"linear-gradient(45deg, #22c55e, #16a34a)","--text-color":"#1e293b","--bubble-sent":"#059669","--bubble-received":"#d1fae5","--background-color":"#f0fdf4","--bubble-sent-color":"#bcf9bc"},blue:{"--primary-color":"#17A4FB","--primary-gradient":"linear-gradient(45deg, #17A4FB, #00bfff)","--text-color":"#333","--bubble-sent":"#007bff","--bubble-received":"#f1f5f9","--background-color":"#f5f7fa","--bubble-sent-color":"#D8EDF8"},dark_blue:{"--primary-color":"#2735af","--primary-gradient":"linear-gradient(45deg, #2735af, #2735af)","--text-color":"#333","--bubble-sent":"#007bff","--bubble-received":"#f1f5f9","--background-color":"#f5f7fa","--bubble-sent-color":"#d9dcf7"},darewin_consulting:{"--primary-color":"#1b1464","--primary-gradient":"linear-gradient(45deg, #1b1464, #2e21b5)","--text-color":"#333","--bubble-sent":"#007bff","--bubble-received":"#f1f5f9","--background-color":"#f5f7fa","--bubble-sent-color":"#d9dcf7"}};function i(){return/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)||window.innerWidth<=480||window.innerHeight<=480}let s={},l={};async function c(t){try{const n=await fetch(`${o.apiEndpoint}/bot-status/${t}`);if(!n.ok)throw new Error("Failed to check bot status");const e=await n.json();return o.isActive="active"===e.status,o.isActive||s.chatContainer.classList.contains("atbots-d-none")||f(),o.isActive}catch(t){return console.error("Error checking bot status:",t),!1}}async function d(){try{const t=y(a.userId);if(!t)return console.debug("User ID not yet available"),null;const n=document.querySelector("script[data-tenant-id]"),e=n?.getAttribute("data-tenant-id"),r=await fetch(`${o.apiEndpoint}/chat-session/create`,{method:"POST",headers:{"Content-Type":"application/json","X-Tenant-ID":e},body:JSON.stringify({user_id:t})});if(!r.ok)return console.debug("Session creation deferred"),null;const i=await r.json();return i.session_id?(x(a.sessionId,i.session_id),x(a.sessionLastActive,Date.now().toString()),i.session_id):null}catch(t){return console.debug("Session creation deferred:",t),null}}async function p(t){try{if(!t)return;await fetch(`${o.apiEndpoint}/chat-session/close`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({session_id:t})}),localStorage.removeItem(a.sessionId)}catch(t){console.error("Error closing chat session:",t)}}async function m(){await async function(){let t=y(a.userId);if(!t)try{const n=document.querySelector("script[data-tenant-id]"),e=n?.getAttribute("data-tenant-id");if(!e)return;const r=await fetch(`${o.apiEndpoint}/create-anonymous-user`,{method:"POST",headers:{"Content-Type":"application/json","X-Tenant-ID":e}});if(r.ok){const n=await r.json();n.user_id&&(t=n.user_id,x(a.userId,t))}}catch(t){console.error("Error creating user on load:",t)}return t}()}function u(){if(!i())return;const t=document.querySelector(".atbots-contact-form");if((window.visualViewport?window.visualViewport.height:window.innerHeight)<.8*window.innerHeight){if(document.body.classList.add("atbots-keyboard-active"),document.body.style.overflow="hidden",document.body.style.height="100%",document.body.style.position="fixed",document.body.style.width="100%",window._atbotsTouchMoveHandler||(window._atbotsTouchMoveHandler=function(t){document.body.classList.contains("atbots-keyboard-active")&&t.preventDefault()},document.body.addEventListener("touchmove",window._atbotsTouchMoveHandler,{passive:!1})),t){t.style.overflow="hidden",t.style.position="fixed",t.style.top="0",t.style.left="0",t.style.right="0",t.style.bottom="0",t.style.width="100vw",t.style.height="100vh";const n=t.querySelector(".atbots-contact-form-content");n&&(n.style.overflowY="auto")}s.chatBody&&(s.chatBody.style.overflowY="auto",s.chatBody.style.touchAction="pan-y",s.chatBody.scrollTop=s.chatBody.scrollHeight),s.chatFooter&&s.chatFooter.classList.add("atbots-d-none")}else{if(document.body.classList.remove("atbots-keyboard-active"),document.body.style.overflow="",document.body.style.height="",document.body.style.position="",document.body.style.width="",window._atbotsTouchMoveHandler&&(document.body.removeEventListener("touchmove",window._atbotsTouchMoveHandler,{passive:!1}),window._atbotsTouchMoveHandler=null),t){t.style.overflow="hidden",t.style.position="",t.style.top="",t.style.left="",t.style.right="",t.style.bottom="",t.style.width="",t.style.height="";const n=t.querySelector(".atbots-contact-form-content");n&&(n.style.overflowY="auto")}s.chatBody&&(s.chatBody.style.overflowY="auto",s.chatBody.style.touchAction=""),s.chatFooter&&s.chatFooter.classList.remove("atbots-d-none")}b()}const b=()=>{if(i()){const t=.01*(void 0!==window.visualViewport?window.visualViewport.height:window.innerHeight);document.documentElement.style.setProperty("--vh",`${t}px`),s.chatContainer&&(s.chatContainer.style.display="none",s.chatContainer.offsetHeight,s.chatContainer.style.display="flex")}};function h(){if(!o.isActive){const t=document.createElement("div");return t.className="atbots-status-bubble",t.innerHTML="\n            <h4>Training in Progress</h4>\n            <p>The AI is still training with the data. Please wait a few minutes...</p>",document.body.appendChild(t),void setTimeout(()=>{t.parentNode===document.body&&t.remove()},3e3)}s.chatContainer.classList.remove("atbots-d-none"),s.chatIcon.classList.add("atbots-d-none"),document.body.classList.add("atbots-chat-open");const t=q();s.chatContainer.style.bottom="30px","left"===t?(s.chatContainer.style.left="30px",s.chatContainer.style.right="auto"):(s.chatContainer.style.right="30px",s.chatContainer.style.left="auto"),i()?(b(),s.chatContainer.classList.add("mobile-view")):(s.userInput.readOnly=!1,setTimeout(()=>s.userInput.focus(),100));const n=w();n&&0!==n.length?g():(s.chatBody.innerHTML="",v()),atbotsChat.onChatToggle(),D(),s.refreshBtn&&(s.refreshBtn.style.display="none")}function f(){s.chatContainer.classList.add("atbots-d-none"),s.chatIcon.classList.remove("atbots-d-none"),document.body.classList.remove("atbots-chat-open"),s.chatContainer.style.height="",document.body.classList.remove("atbots-keyboard-active"),setTimeout(()=>{s.chatIcon&&o.welcomeMessage&&!localStorage.getItem("atbots_welcome_bubble_closed")&&(!localStorage.getItem("atbots_welcome_bubble_closed_until")||Date.now()>=parseInt(localStorage.getItem("atbots_welcome_bubble_closed_until"),10))&&N()},300)}function g(){s.chatBody.innerHTML="";try{!function(){const t=parseInt(y(a.lastActive,"0"));Date.now()-t>o.inactivityTimeout&&E();I()}();const t=w();if(s.refreshBtn){const n=t.some(t=>"user"===t.role),e=t.length>1;s.refreshBtn.style.display=n&&e?"":"none",s.refreshBtn.disabled=!(n&&e)}t&&0!==t.length?(t.forEach(t=>{if("system"===t.role&&(!t.content||""===t.content))return;if(Array.isArray(t.content))return void t.content.forEach(n=>{if(n&&""!==n.trim()){const e=document.createElement("div");e.className="atbots-chat-bubble "+("user"===t.role?"sent":"received"),e.innerHTML=`${n} <span class="timestamp">${t.timestamp}</span>`,s.chatBody.appendChild(e)}});if(!t.content||""===t.content)return;const n=document.createElement("div");n.className="atbots-chat-bubble "+("user"===t.role?"sent":"received"),n.innerHTML=`${t.content} <span class="timestamp">${t.timestamp}</span>`,s.chatBody.appendChild(n)}),1===t.length&&"assistant"===t[0].role&&t[0].content===o.welcomeMessage&&o.chatSuggestions&&o.chatSuggestions.length>0&&A(o.chatSuggestions,!0)):v(),z()}catch(t){console.error("Error loading chat history:",t),v()}}function y(t,n){try{const e=localStorage.getItem(t);return null===e?n:e}catch(t){return console.error("Error accessing localStorage:",t),n}}function x(t,n){try{return localStorage.setItem(t,n),!0}catch(t){return console.error("Error setting localStorage:",t),!1}}function w(){return JSON.parse(y(a.history,"[]"))}function v(){if(!o.welcomeMessage)return void console.warn("Welcome message not set");s.refreshBtn&&(s.refreshBtn.style.display="none",s.refreshBtn.disabled=!0);const t=document.createElement("div");t.className="atbots-chat-bubble received",t.innerHTML=o.welcomeMessage,s.chatBody?(s.chatBody.appendChild(t),k("assistant",o.welcomeMessage),o.chatSuggestions&&o.chatSuggestions.length>0&&A(o.chatSuggestions,!0),z()):console.error("Chat body element not found")}function E(){try{x(a.history,"[]"),x(a.initialized,"false"),localStorage.removeItem(a.formDisplayedAfterMsg),localStorage.removeItem(a.sessionId),localStorage.removeItem(a.sessionLastActive)}catch(t){console.error("Error resetting chat history:",t)}}function k(t,n,e="message"){try{const o=w(),r=(new Date).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"});o.push({role:t,content:n,type:e,timestamp:r}),x(a.history,JSON.stringify(o)),I()}catch(t){console.error("Error saving message:",t)}}function I(){x(a.lastActive,Date.now().toString())}function C(t,n){try{const e=w(),o=(new Date).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"});e.push({role:t,content:n,timestamp:o}),x(a.history,JSON.stringify(e)),I()}catch(t){console.error("Error saving message:",t)}}function S(t){try{if(!l[t])return;const n=l[t].cloneNode();n.volume=l[t].volume;const e=n.play();e&&e["catch"](t=>{"NotAllowedError"===t.name?console.debug("Sound autoplay prevented - user interaction needed"):console.error("Error playing sound:",t)})}catch(t){console.debug("Error playing sound:",t)}}window.addEventListener("resize",()=>{if(i()){const t=.01*window.innerHeight;document.documentElement.style.setProperty("--vh",`${t}px`)}});let _=null;function B(t,n){const e=document.createElement("div");e.className="atbots-chat-bubble "+("user"===t?"sent":"received");const o=(new Date).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"});let a="";if("user"===t){if(a=n,S("sent"),s.refreshBtn){const t=w(),n=t.some(t=>"user"===t.role),e=t.length>1;s.refreshBtn.style.display=n&&e?"":"none",s.refreshBtn.disabled=!(n&&e)}}else{a=n,S("received");const t=e.querySelector(".skip-name-link");t&&(t.onclick=t=>{t.preventDefault();document.getElementById("atbotsUserInput").value="skip",T()})}return e.innerHTML=`\n            ${a}\n            <span class="timestamp">${o}</span>\n        `,s.chatBody.appendChild(e),z(),e}function L(){const t=document.createElement("div");return t.className="atbots-typing-indicator",t.innerHTML="<span></span><span></span><span></span>",s.chatBody.appendChild(t),z(),t}function z(){s.chatBody.scrollTop=s.chatBody.scrollHeight}async function T(){const t=s.userInput.value.trim();if(""===t)return;localStorage.setItem("atbots_welcome_bubble_closed","true"),localStorage.setItem("atbots_welcome_bubble_closed_until",(Date.now()+864e5).toString()),D(),s.userInput.style.borderColor="#e2e8f0",s.userInput.disabled=!0,s.sendBtn.disabled=!0,s.sendBtn.classList.add("atbots-d-none"),s.refreshBtn&&(s.refreshBtn.disabled=!0,s.refreshBtn.classList.add("atbots-d-none"));const n=s.chatBody.querySelector(".atbots-chat-suggestions");n&&n.remove(),B("user",t),C("user",t),s.userInput.value="";let e=y(a.sessionId);e||(e=await d());const r=L();await async function(t,n){try{const e=document.querySelector("script[data-tenant-id]"),r=e?.getAttribute("data-tenant-id");if(!r)throw new Error("Tenant ID not found");const i=y(a.sessionId),s=y(a.userId),l=w(),c={"Content-Type":"application/json","X-Tenant-ID":r};i&&(c["X-Session-ID"]=i),i&&await async function(t){try{const n=y(a.sessionId);if(!n)return;const e=parseInt(y(a.sessionLastActive,"0"));if(Date.now()-e>o.inactivityTimeout)return await p(n),void await d();await fetch(`${o.apiEndpoint}/chat-session/update`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({session_id:n,message:{role:t.role,content:t.content,timestamp:(new Date).toISOString()}})}),x(a.sessionLastActive,Date.now().toString())}catch(t){console.error("Error updating chat session:",t)}}({role:"user",content:t});const m={message:t,history:l,tenant_id:r,user_id:s,session_id:i},u=await fetch(`${o.apiEndpoint}/chat`,{method:"POST",headers:c,body:JSON.stringify(m)});if(!u.ok)throw new Error("Server error");const b=await u.json();if(n&&M(n),!b.response||Array.isArray(b.response)&&0===b.response.length)throw new Error("Empty response from server");if(b.is_multi_response&&Array.isArray(b.response)){for(let t=0;t<b.response.length;t++){if(!b.response[t])continue;if(t>0){const t=L();await new Promise(t=>setTimeout(t,2e3)),M(t)}B("assistant",b.response[t]);C("assistant",b.response[t])}b.suggestions&&b.suggestions.length>0&&A(b.suggestions)}else{const t=Array.isArray(b.response)?b.response[0]:b.response;if(!t)throw new Error("Invalid response format");B("assistant",t);C("assistant",t),b.suggestions&&b.suggestions.length>0&&A(b.suggestions)}}catch(t){console.error("Error:",t),M(n),B("assistant","I apologize, but I couldn't process your request properly. Could you please try rephrasing your question?")}finally{s.userInput.disabled=!1,s.sendBtn.disabled=!1,s.sendBtn.classList.add("atbots-d-none"),s.refreshBtn&&(s.refreshBtn.disabled=!1,s.refreshBtn.classList.remove("atbots-d-none")),window.innerWidth<=480?setTimeout(()=>{document.activeElement===s.userInput&&s.userInput.blur()},10):s.userInput.focus()}}(t,r);const i=w().filter(t=>"user"===t.role).length;y(a.contactSubmitted)||2!==i||function(){if("true"===y(a.contactSubmitted))return;if("true"===y(a.formDisplayedAfterMsg))return;const t=w(),n=t?.filter(t=>"user"===t.role).length||0;if(n<2)return;x(a.formDisplayedAfterMsg,"true");const e=function(){if("true"===y(a.contactSubmitted))return null;const t=document.createElement("div");t.className="atbots-contact-form",t.style.position="fixed",t.style.bottom="0",t.style.left="0",t.style.right="0",t.style.width="100%",t.style.overflow="hidden";let n='\n            <div class="atbots-input-group">\n                <input type="text" id="atbotsContactName" placeholder="Your Name" required>\n                <div id="nameError" class="atbots-validation-message"></div>\n            </div>';o.formConfig.showEmail&&(n+='\n                <div class="atbots-input-group">\n                    <input type="email" id="atbotsContactEmail" placeholder="Your Email" required>\n                    <div id="emailError" class="atbots-validation-message"></div>\n                </div>');o.formConfig.showPhone&&(n+='\n                <div class="atbots-input-group">\n                    <input type="tel" id="atbotsContactPhone" placeholder="Your Phone Number" required>\n                    <div id="phoneError" class="atbots-validation-message"></div>\n                </div>');t.innerHTML=`\n            <div class="atbots-contact-form-content" style="overflow-y: auto;">\n                <button class="atbots-close-contact-form">×</button>\n                <h3>In case we disconnect, could you provide your contact?</h3>\n                ${n}\n                <button class="atbots-contact-submit">Submit</button>\n            </div>\n        `;const e=t.querySelector(".atbots-close-contact-form"),r=t.querySelector(".atbots-contact-submit"),i=t.querySelector("#atbotsContactName"),s=o.formConfig.showEmail?t.querySelector("#atbotsContactEmail"):null,l=o.formConfig.showPhone?t.querySelector("#atbotsContactPhone"):null,c=t.querySelector("#emailError"),d=t.querySelector("#nameError"),p=t.querySelector("#phoneError");function m(t,n){t&&t.previousElementSibling&&t.previousElementSibling.classList.add("atbots-input-error"),t.textContent=n}function u(t){t&&t.previousElementSibling&&t.previousElementSibling.classList.remove("atbots-input-error"),t.textContent=""}return i.addEventListener("input",()=>{u(d),i.style.borderColor=""!==i.value.trim()?getComputedStyle(document.documentElement).getPropertyValue("--primary-color"):"#e2e8f0"}),s?.addEventListener("input",()=>{u(c),s.style.borderColor=""!==s.value.trim()?getComputedStyle(document.documentElement).getPropertyValue("--primary-color"):"#e2e8f0"}),l?.addEventListener("input",()=>{u(p),l.style.borderColor=""!==l.value.trim()?getComputedStyle(document.documentElement).getPropertyValue("--primary-color"):"#e2e8f0"}),e.addEventListener("click",async()=>{try{const t=document.querySelector("script[data-tenant-id]"),n=t?.getAttribute("data-tenant-id"),e=y(a.userId),r=await fetch(`${o.apiEndpoint}/create-anonymous-user`,{method:"POST",headers:{"Content-Type":"application/json","X-Tenant-ID":n},body:JSON.stringify({existing_user_id:e})});if(!r.ok)throw new Error("Failed to create anonymous user");const i=await r.json();i.user_id&&x(a.userId,i.user_id)}catch(t){console.error("Error handling form closure:",t)}t.remove()}),r.addEventListener("click",async()=>{const n=i.value.trim(),e=s?s.value.trim():"",u=l?l.value.trim():"";let b=!0;if(n?n.length<2&&(m(d,"Please enter a valid name (at least 2 characters)."),b=!1):(m(d,"Name is required."),b=!1),!o.formConfig.showEmail||e&&function(t){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t)}(e)||(m(c,e?"Please enter a valid email address.":"Email is required."),b=!1),!o.formConfig.showPhone||u&&/^\d{7,15}$/.test(u)||(m(p,u?"Please enter a valid phone number (7-15 digits).":"Phone number is required."),b=!1),b)try{r.disabled=!0,r.textContent="Submitting...";const i=document.querySelector("script[data-tenant-id]"),s=i?.getAttribute("data-tenant-id"),l=y(a.userId),c=await fetch(`${o.apiEndpoint}/save-contact`,{method:"POST",headers:{"Content-Type":"application/json","X-Tenant-ID":s},body:JSON.stringify({name:n,email:e,phone:u,existing_user_id:l})});if(!c.ok)throw new Error("Failed to save contact");const d=await c.json();d.user_id&&(x(a.userId,d.user_id),x(a.contactSubmitted,"true"),localStorage.removeItem(a.formDisplayed),t.remove())}catch(t){console.error("Error saving contact:",t),c&&o.formConfig.showEmail&&m(c,"Failed to save contact. Please try again."),p&&o.formConfig.showPhone&&m(p,"Failed to save contact. Please try again."),d&&m(d,"Failed to save contact. Please try again."),r.textContent="Submit",r.disabled=!1}}),t}();e&&(s.chatContainer.appendChild(e),"undefined"!=typeof window&&"undefined"!=typeof document&&setTimeout(()=>{document.activeElement&&"function"==typeof document.activeElement.blur&&document.activeElement.blur()},10))}()}function A(t,n=!1){if(!t||!t.length)return;const e=s.chatBody.querySelector(".atbots-chat-suggestions");if(e&&!n&&e.remove(),e&&n)return;const o=document.createElement("div");o.className="atbots-chat-suggestions",o.innerHTML=t.map(t=>`<button class="atbots-suggestion-btn">${t}</button>`).join(""),o.addEventListener("click",t=>{const e=t.target.closest(".atbots-suggestion-btn");if(e){const t=e.textContent.replace(/\[\[|\]\]/g,"");s.userInput.value=t,o.remove(),n&&k("system","","suggestions"),T()}}),s.chatBody.appendChild(o),z()}function M(t){t&&t.parentNode===s.chatBody&&s.chatBody.removeChild(t)}function P(t){if(!t)return;const n=document.querySelector(".atbots-chat-header img");n&&(n.src=t,o.logoUrl=t)}function N(){if(D(),!o.welcomeMessage)return;if(!s.chatIcon)return;const t=w();if(t&&t.some(t=>"user"===t.role))return;const n=localStorage.getItem("atbots_welcome_bubble_closed_until");if(n&&Date.now()<parseInt(n,10))return;if("true"===localStorage.getItem("atbots_welcome_bubble_closed")){if(n&&!(Date.now()>=parseInt(n,10)))return;localStorage.removeItem("atbots_welcome_bubble_closed"),localStorage.removeItem("atbots_welcome_bubble_closed_until")}const e=document.createElement("div");e.className="atbots-welcome-bubble-wrapper",e.style.position="fixed",e.style.zIndex=1001,e.style.pointerEvents="none",e.style.visibility="hidden",e.style.opacity="0",e.style.transition="opacity 0.5s";const a=document.createElement("div");a.className="bubble",a.style.background="ivory",a.style.borderRadius="14px",a.style.minWidth="120px",a.style.maxWidth="220px",a.style.padding="10px 14px",a.style.fontSize="13px",a.style.textAlign="center",a.style.boxShadow="0 2px 6px rgba(0,0,0,0.07)",a.style.margin="0 auto",a.style.position="relative",a.style.color="#222",a.style.fontFamily="'Poppins', sans-serif, Arial",a.style.cursor="pointer",a.tabIndex=0,a.setAttribute("role","button"),a.setAttribute("aria-label","Open chat"),a.innerHTML='<div style="font-weight:600;font-size:1em;margin-bottom:2px;">We\'re Online</div>\n<div style="font-size:0.95em;">How may we help you today?</div>';const r=document.createElement("button");r.innerHTML='\n            <svg width="18" height="18" viewBox="0 0 18 18" fill="none" style="display:block" xmlns="http://www.w3.org/2000/svg">\n                <circle cx="9" cy="9" r="9" fill="rgba(0,0,0,0)" />\n                <path d="M5.5 5.5L12.5 12.5M12.5 5.5L5.5 12.5" stroke="#888" stroke-width="2" stroke-linecap="round"/>\n            </svg>\n        ',r.style.position="absolute",r.style.top="4px",r.style.right="4px",r.style.background="transparent",r.style.border="none",r.style.padding="2px",r.style.cursor="pointer",r.style.opacity="0.6",r.style.zIndex="10",r.setAttribute("aria-label","Close welcome bubble"),r.onmouseenter=()=>{r.style.opacity="1"},r.onmouseleave=()=>{r.style.opacity="0.6"},r.onclick=function(t){t.stopPropagation(),localStorage.setItem("atbots_welcome_bubble_closed","true"),D()},a.appendChild(r);const l=document.createElement("div");l.className="pointer",l.style.width="12px",l.style.height="12px",l.style.background="ivory",l.style.position="absolute",l.style.transform="rotate(45deg)",l.style.zIndex="2";const c=document.createElement("div");c.className="bubble blurred",c.style.background="gray",c.style.filter="blur(8px)",c.style.position="absolute",c.style.top="10px",c.style.left="0",c.style.right="0",c.style.height="100%",c.style.zIndex="-1";const d=document.createElement("div");d.className="pointer blurred",d.style.background="gray",d.style.width="12px",d.style.height="12px",d.style.filter="blur(8px)",d.style.position="absolute",d.style.zIndex="-1",a.onclick=function(){h(),D()},a.onkeydown=function(t){"Enter"!==t.key&&" "!==t.key||(t.preventDefault(),h(),D())},e.appendChild(c),e.appendChild(a),e.appendChild(l),e.appendChild(d);const p=s.chatIcon.getBoundingClientRect();document.body.appendChild(e),setTimeout(()=>{const t=e.getBoundingClientRect();let n,o;const a=q();e.style.top="",e.style.left="",e.style.right="",e.style.bottom="";if(i()){const n=p.left+p.width/2,o=t.width;let a=n-o/2;a=Math.max(8,Math.min(a,window.innerWidth-o-8)),e.style.left=`${a}px`,e.style.right="auto",e.style.transform="",e.style.top="auto";const r=window.innerHeight-p.bottom;e.style.bottom=`${r+p.height+12}px`;const i=n-a;l.style.left=i-6+"px",l.style.right="auto",l.style.transform="rotate(45deg)",l.style.top="auto",l.style.bottom="-6px",l.style.marginBottom="0",d.style.left=i-6+"px",d.style.right="auto",d.style.transform="rotate(45deg)",d.style.top="auto",d.style.bottom="-6px",d.style.marginBottom="0"}else"left"===a?(n=p.left+p.width/2-10-7,n=Math.max(8,n),e.style.left=`${n}px`,e.style.right="auto",e.style.top=p.top-t.height-8+"px",l.style.left="12px",l.style.right="auto",l.style.bottom="0px",l.style.top="auto",l.style.marginBottom="-4px",d.style.left="12px",d.style.right="auto",d.style.bottom="0px",d.style.top="auto",d.style.marginBottom="-4px"):(o=window.innerWidth-p.right+p.width/2-10-7,o=Math.max(8,o),e.style.right=`${o}px`,e.style.left="auto",e.style.top=p.top-t.height-8+"px",l.style.right="12px",l.style.left="auto",l.style.bottom="0px",l.style.top="auto",l.style.marginBottom="-4px",d.style.right="12px",d.style.left="auto",d.style.bottom="0px",d.style.top="auto",d.style.marginBottom="-4px");e.style.visibility="visible",e.style.opacity="0",e.style.pointerEvents="none",setTimeout(()=>{e.style.opacity="1",e.style.pointerEvents="auto"},5e3)},0)}function D(){const t=document.querySelector(".atbots-welcome-bubble-wrapper");t&&(t._timeout&&clearTimeout(t._timeout),t.remove());const n=document.querySelector(".atbots-welcome-bubble");n&&n.parentNode&&n.parentNode.removeChild(n)}function q(){const t=s.chatIcon;if(!t)return"right";const n=window.getComputedStyle(t);return n.left&&"auto"!==n.left&&parseInt(n.left)<window.innerWidth/2?"left":"right"}return{init:async function(t={}){await m();const n=document.querySelector("script[data-tenant-id]"),i=n?.getAttribute("data-tenant-id");if(i)try{await c(i);const n=await async function(t){try{const n=window.location.href,e=encodeURIComponent(n),a=await fetch(`${o.apiEndpoint}/bot-config/${t}?url=${e}`);if(!a.ok){const t=await a.text();throw console.error("Error response:",t),new Error(`Failed to fetch bot configuration: ${a.status}`)}const r=await a.json();if(!r.bot_name||!r.welcome_message)throw new Error("Invalid bot configuration: missing required fields");return o.welcomeMessage=r.welcome_message,o.botName=r.bot_name,o.chatSuggestions=r.chat_suggestions||[],o.formConfig={showPhone:r.form_config?.show_phone||!1,showEmail:r.form_config?.show_email||!1},o.chatIconCss=r.chat_icon_css||"",r}catch(t){throw console.error("Error in fetchBotConfig:",t),t}}(i);o.welcomeMessage=n.welcome_message,o.botName=n.bot_name,o.chatSuggestions=n.chat_suggestions||[],setInterval(()=>c(i),o.statusCheckInterval);const d=n.color_theme?.toLowerCase()||"blue",m=r[d]||r.blue,w=document.createElement("style");if(w.textContent=`\n                :host, :root {\n                    ${Object.entries(m).map(([t,n])=>`${t}: ${n};`).join("\n                ")}\n                }\n            `,e.appendChild(w),Object.assign(o,t),function(){if(!e.querySelector('meta[name="viewport"]')){const t=document.createElement("meta");t.name="viewport",t.content="width=device-width, initial-scale=1.0, viewport-fit=cover",e.appendChild(t)}const t=document.createElement("style");function n(t){var n=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return n?{r:parseInt(n[1],16),g:parseInt(n[2],16),b:parseInt(n[3],16)}:null}t.textContent="\n            .atbots-widget * {\n                box-sizing: border-box;\n            }\n            .atbots-widget p {\n                margin: 0;\n                padding: 0;\n            }\n            .atbots-chat-icon {\n                position: fixed;\n                width: 50px;\n                height: 50px;\n                color: white;\n                border-radius: 50%;\n                display: flex;\n                align-items: center;\n                justify-content: center;\n                cursor: pointer;\n                box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);\n                z-index: 999;\n                transition: transform 0.3s ease;\n            }\n            .atbots-chat-icon:hover {\n                transform: scale(1.1);\n            }\n            .atbots-chat-container {\n                position: fixed;\n                background: var(--background-color);\n                box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);\n                display: flex;\n                flex-direction: column;\n                overflow: hidden;\n                z-index: 1000;\n                transition: all 0.3s ease;\n                border-radius: 10px;\n                font-family: 'Poppins', sans-serif, Arial;\n                width: 360px; /* Default width */\n                max-height: 80vh; /* Limit height */\n            }\n            .atbots-chat-button {\n                background-color: var(--primary-color);\n                border: none;\n                border-radius: 50%;\n                width: 50px;\n                height: 50px;\n                display: flex;\n                align-items: center;\n                justify-content: center;\n                cursor: pointer;\n                box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);\n                transition: background-color 0.3s ease;\n                outline: none;\n            }\n            .atbots-chat-button:focus {\n                outline: none;\n                box-shadow: none;\n            }\n            .atbots-chat-button:hover {\n                opacity: 0.9;\n            }\n            .atbots-chat-button svg {\n                stroke: white;\n            }\n            .atbots-chat-header {\n                background: var(--primary-gradient);\n                padding: 10px 15px;\n                color: white;\n                display: flex;\n                align-items: center;\n                gap: 10px;\n                border-top-left-radius: 10px;\n                border-top-right-radius: 10px;\n            }\n            .atbots-chat-header strong {\n                font-size: 16px;\n                font-weight: 500;\n                display: block;\n                margin-bottom: 2px;\n                color: white;\n            }\n            .atbots-chat-header img {\n                width: 40px;\n                height: 40px;\n                border-radius: 50%;\n                border: 2px solid white;\n            }\n            .atbots-close-btn {\n                background: transparent;\n                border: none;\n                color: white;\n                font-size: 14px;\n                padding: 5px 10px;\n                cursor: pointer;\n                margin-left: auto;\n                outline: none;\n            }\n            .atbots-close-btn:focus {\n                outline: none;\n                box-shadow: none;\n            }\n            .atbots-chat-body {\n                flex: 1;\n                padding: 15px;\n                overflow-y: auto;\n                scrollbar-width: none;\n                -ms-overflow-style: none;\n                display: flex;\n                flex-direction: column;\n                background: #fff !important; /* Pure white background */\n            }\n            .atbots-chat-body::-webkit-scrollbar {\n                display: none;\n            }\n            .atbots-chat-bubble {\n                max-width: 80%;\n                padding: 10px;\n                opacity: 0;\n                animation: atbots-fadeInUp 0.5s ease-out forwards;\n                border-radius: 12px;\n                margin: 5px 0;\n                box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);\n                border: 1px solid rgba(0, 0, 0, 0.08);\n                background: #fff;\n            }\n            .atbots-chat-bubble.sent {\n                background: var(--bubble-sent-color);\n                color: black;\n                align-self: flex-end;\n                margin-left: auto;\n                font-size: 13px;\n                border-bottom-right-radius: 0;\n                word-wrap: break-word;\n                overflow-wrap: break-word;\n                white-space: normal;\n                box-shadow: none;\n                border: none;\n            }\n            .atbots-chat-bubble.sent .timestamp {\n                color: rgba(0, 0, 0, 0.8);\n            }\n            .atbots-chat-bubble.received {\n                background: #fff !important; /* Pure white for received bubble */\n                color: var(--text-color);\n                align-self: flex-start;\n                text-align: left;\n                font-size: 13px;\n                border-top-left-radius: 0;\n                overflow-wrap: break-word;\n                word-wrap: break-word;\n                word-break: break-word;\n                max-width: 95%;\n                box-shadow: 0 2px 8px rgba(0,0,0,0.07), 0 1.5px 0.5px rgba(0,0,0,0.03);\n                border: 1.5px solid #f1f1f1;\n            }\n            .atbots-chat-footer {\n                display: flex;\n                align-items: center;\n                padding: 8px 10px 2px;\n                position: relative;\n                font-size: 13px;\n                background: var(--background-color);\n                margin-bottom: 0;\n            }\n            .atbots-chat-footer input {\n                flex: 1;\n                padding: 10px 45px 10px 20px;\n                border: 2px solid #e2e8f0;\n                border-radius: 12px;\n                background: #f1f1f1;\n                outline: none;\n                font-family: 'Poppins', sans-serif, Arial;\n                caret-color: var(--primary-color);\n                caret-shape: block;\n                -webkit-text-size-adjust: none;\n                -webkit-appearance: none;\n                appearance: none;\n                transition: border-color 0.2s ease;\n            }\n            .atbots-chat-footer input:focus:not(:placeholder-shown) {\n                border-color: var(--primary-color);\n            }\n            .atbots-send-button {\n                position: absolute;\n                right: 16px;\n                top: 57%;\n                transform: translateY(-50%);\n                background: transparent;\n                border: none;\n                cursor: pointer;\n                width: 32px;\n                height: 32px;\n                min-width: 32px;\n                min-height: 32px;\n                display: flex;\n                align-items: center;\n                justify-content: center;\n                z-index: 10;\n                transition: transform 0.2s ease;\n            }\n            .atbots-send-button svg {\n                stroke: var(--primary-color);\n            }\n            .atbots-send-button:hover {\n                transform: translateY(-50%) scale(1.05);\n            }\n            .atbots-typing-indicator {\n                display: flex;\n                align-items: center;\n                padding: 8px 12px;\n                background: #f1f1f1;\n                border-radius: 15px;\n                width: fit-content;\n                margin-bottom: 8px;\n                align-self: flex-start;\n            }\n            .atbots-typing-indicator span {\n                width: 6px;\n                height: 6px;\n                margin: 0 2px;\n                background: gray;\n                border-radius: 50%;\n                display: inline-block;\n                animation: atbots-typing 1.2s infinite ease-in-out;\n            }\n            .atbots-typing-indicator span:nth-child(1) {\n                animation-delay: 0s;\n            }\n            .atbots-typing-indicator span:nth-child(2) {\n                animation-delay: 0.2s;\n            }\n            .atbots-typing-indicator span:nth-child(3) {\n                animation-delay: 0.4s;\n            }\n            .atbots-ms-auto {\n                margin-left: auto !important;\n            }\n            .atbots-d-none {\n                display: none !important;\n            }\n            .atbots-input-error {\n                border-color: #dc2626 !important;\n            }\n            .atbots-validation-message {\n                color: #dc2626;\n                font-size: 10px;\n                margin: 2px 0 0 2px;\n                min-height: 12px;\n                display: block;\n                transition: color 0.2s;\n            }\n            .atbots-status-bubble {\n                position: fixed;\n                bottom: 140px;\n                background: #fff;\n                color: var(--text-color);\n                padding: 15px 20px;\n                border: 1px solid var(--primary-color);\n                border-radius: 10px;\n                font-size: 14px;\n                box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);\n                animation: atbots-fadeInUp 0.3s ease-out forwards;\n                z-index: 1000;\n                font-family: 'Poppins', sans-serif, Arial;\n                max-width: 280px;\n                display: flex;\n                flex-direction: column;\n                gap: 4px;\n            }\n            .atbots-status-bubble h4 {\n                margin: 0 0 2px;\n                font-size: 15px;\n                font-weight: 600;\n                color: var(--text-color);\n            }\n            .atbots-status-bubble p {\n                margin: 0;\n                font-size: 13px;\n                line-height: 1.4;\n                color: var(--text-color);\n                opacity: 0.8;\n            }\n            .atbots-status-bubble::before {\n                content: '';\n                position: absolute;\n                bottom: -8px;\n                width: 0;\n                height: 0;\n                border-left: 8px solid transparent;\n                border-right: 8px solid transparent;\n                border-top: 8px solid #fff;\n            }\n            .atbots-skip-button {\n                background: transparent;\n                border: 1px solid var(--primary-color);\n                color: var(--primary-color);\n                border-radius: 10px;\n                padding: 8px 16px;\n                margin-right: 8px;\n                cursor: pointer;\n                font-size: 13px;\n                transition: all 0.2s ease;\n            }\n            .atbots-skip-button:hover {\n                background: var(--primary-color);\n                color: white;\n            }\n            .atbots-header-actions {\n                display: flex;\n                align-items: center;\n                gap: 10px;\n                margin-left: auto;\n            }\n            .atbots-refresh-btn, .atbots-minimize-btn {\n                background: transparent;\n                border: none;\n                color: white;\n                padding: 5px;\n                cursor: pointer;\n                display: flex;\n                align-items: center;\n                justify-content: center;\n                border-radius: 50%;\n                width: 30px;\n                height: 30px;\n                transition: background-color 0.2s;\n            }\n            .atbots-refresh-btn:hover, .atbots-minimize-btn:hover {\n                background: rgba(255, 255, 255, 0.1);\n            }\n            .atbots-refresh-btn svg, .atbots-minimize-btn svg {\n                width: 16px;\n                height: 16px;\n            }\n            .atbots-powered-by {\n                padding: 0.5px 8px 4px;\n                text-align: center;\n                font-size: 11px;\n                color: #666;\n                background: var(--background-color);\n                font-family: 'Poppins', sans-serif;\n                display: flex;\n                align-items: center;\n                justify-content: center;\n                gap: 6px;\n            }\n            .atbots-powered-by img {\n                height: 20px;\n                width: auto;\n                vertical-align: middle;\n            }\n            .atbots-powered-by a {\n                color: var(--primary-color);\n                text-decoration: none;\n                display: flex;\n                align-items: center;\n                gap: 4px;\n            }\n            .atbots-contact-form {\n                position: absolute;\n                bottom: 0;\n                background: white;\n                border-radius: 20px 20px 10px 10px;\n                box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.15);\n                z-index: 1001;\n                display: flex;\n                flex-direction: column;\n                animation: atbots-slideUp 0.3s ease-out;\n            }\n            .atbots-close-contact-form {\n                position: absolute;\n                right: 10px;\n                top: 10px;\n                background: rgba(0, 0, 0, 0.1);\n                color: #666;\n                border: none;\n                width: 32px;\n                height: 32px;\n                border-radius: 50%;\n                cursor: pointer;\n                display: flex;\n                align-items: center;\n                justify-content: center;\n                font-size: 20px;\n                transition: all 0.2s ease;\n                z-index: 2;\n            }\n            .atbots-close-contact-form:hover {\n                transform: scale(1.1);\n                color: #333;\n            }\n            .atbots-contact-form-content {\n                display: flex;\n                flex-direction: column;\n                gap: 10px;\n                width: 100%;\n                padding: 20px;\n                box-sizing: border-box;\n            }\n            .atbots-contact-form h3 {\n                margin: 0 0 10px;\n                font-size: 16px;\n                color: var(--primary-color);\n                text-align: center;\n                font-weight: 600;\n            }\n            .atbots-contact-form input {\n                padding: 12px 16px;\n                border: 2px solid #e2e8f0 !important;\n                border-radius: 12px;\n                font-size: 14px;\n                outline: none;\n                background: rgba(var(--primary-color-rgb, 0, 0, 0), 0.05);\n                color: var(--text-color);\n                width: 100%;\n                box-sizing: border-box;\n            }\n            .atbots-contact-submit {\n                background: var(--primary-color);\n                color: white;\n                border: none;\n                padding: 12px;\n                border-radius: 12px;\n                cursor: pointer;\n                font-size: 14px;\n                font-weight: 600;\n                margin-top: 10px;\n                transition: all 0.2s ease;\n                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);\n            }\n            .atbots-input-group {\n                position: relative;\n                display: flex;\n                flex-direction: column;\n                gap: 2px;\n            }\n            .atbots-chat-suggestions {\n                display: flex;\n                flex-wrap: wrap;\n                gap: 6px;\n                margin: 8px 0;\n                padding: 0 8px;\n            }\n            .atbots-suggestion-btn {\n                background: #ffffff;\n                color: var(--primary-color);\n                border: 1.5px solid var(--primary-color);\n                padding: 4px 12px;\n                border-radius: 12px;\n                cursor: pointer;\n                font-size: 12px;\n                transition: all 0.2s;\n                opacity: 0.95;\n                white-space: nowrap;\n                max-width: 200px;\n                overflow: hidden;\n                text-overflow: ellipsis;\n                font-weight: 600;\n                box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);\n            }\n            .atbots-suggestion-btn:hover {\n                background: var(--primary-color);\n                color: #fff;\n                transform: translateY(-1px);\n                border: 1.5px solid var(--primary-color);\n                box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);\n            }\n            .atbots-welcome-bubble {\n                position: fixed;\n                z-index: 1001;\n                background: #fff;\n                color: var(--primary-color, #222);\n                padding: 12px 18px;\n                border-radius: 18px;\n                font-size: 14px;\n                font-family: 'Poppins', sans-serif, Arial;\n                box-shadow: 0 2px 6px rgba(0,0,0,0.07);\n                pointer-events: none;\n                opacity: 0;\n                max-width: 90vw;\n                min-width: 120px;\n                text-align: center;\n                transition: opacity 0.5s;\n            }\n            .atbots-welcome-bubble.left::after {\n                left: 24px;\n                right: auto;\n                transform: none;\n            }\n            .atbots-welcome-bubble.right::after {\n                right: 24px;\n                left: auto;\n                transform: none;\n            }\n            .atbots-welcome-bubble::after {\n                content: '';\n                position: absolute;\n                bottom: -10px;\n                border-width: 10px 10px 0 10px;\n                border-style: solid;\n                border-color: var(--primary-color) transparent transparent transparent;\n                filter: drop-shadow(0 2px 2px rgba(0,0,0,0.08));\n                left: 50%;\n                transform: translateX(-50%);\n            }\n            .timestamp {\n                font-size: 10px;\n                color: #999;\n                margin-top: 4px;\n                display: block;\n                text-align: right;\n            }\n\n            @keyframes atbots-typing {\n                0% { transform: translateY(0px); opacity: 0.3; }\n                50% { transform: translateY(-5px); opacity: 1; }\n                100% { transform: translateY(0px); opacity: 0.3; }\n            }\n            @keyframes atbots-fadeIn {\n                from { opacity: 0; transform: translateY(5px); }\n                to { opacity: 1; transform: translateY(0); }\n            }\n            @keyframes atbots-fadeInUp {\n                from { opacity: 0; transform: translateY(20px);}\n                to { opacity: 1; transform: translateY(0); }\n            }\n            @keyframes atbots-slideUp {\n                from { opacity: 0; transform: translateY(20px); }\n                to { opacity: 1; transform: translateY(0); }\n            }\n\n            @supports (height: 100dvh) {\n                :root {\n                    --vh: 1dvh;\n                }\n            }\n            @supports not (height: 100dvh) {\n                :root {\n                    --vh: 1vh;\n                }\n            }\n\n            @media screen and (max-width: 480px), screen and (max-height: 480px) {\n                .atbots-chat-container {\n                    top: 0 !important;\n                    left: 0 !important;\n                    right: 0 !important;\n                    bottom: 0 !important;\n                    width: 100% !important;\n                    height: calc(100 * var(--vh, 1vh)) !important;\n                    border-radius: 0 !important;\n                }\n                .atbots-chat-header {\n                    border-radius: 0 !important;\n                }\n                .atbots-chat-footer {\n                    border-radius: 0 !important;\n                }\n                .atbots-chat-footer input {\n                    border-radius: 0 !important;\n                    height: 48px !important;\n                    min-height: 48px !important;\n                }\n                .atbots-contact-form {\n                    position: fixed !important;\n                    bottom: 0 !important;\n                    left: 0 !important;\n                    right: 0 !important;\n                    width: 100% !important;\n                    height: calc(50 * var(--vh, 1vh)) !important;\n                    padding: 20px !important;\n                    border-radius: 20px 20px 0 0 !important;\n                    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.15) !important;\n                    display: flex !important;\n                    flex-direction: column !important;\n                    justify-content: flex-start !important;\n                    z-index: 1002 !important;\n                }\n                .atbots-keyboard-active .atbots-contact-form {\n                    top: 0 !important;\n                    bottom: auto !important;\n                    height: calc(100 * var(--vh, 1vh)) !important;\n                    border-radius: 0 !important;\n                    box-shadow: none !important;\n                    z-index: 1003 !important;\n                }\n                .atbots-contact-form-content {\n                    padding: 10px 15px !important;\n                    width: 100% !important;\n                    max-width: 400px !important;\n                    margin: 0 auto !important;\n                    flex-grow: 1 !important;\n                    display: flex !important;\n                    flex-direction: column !important;\n                    justify-content: space-between !important;\n                    gap: 8px !important;\n                    overflow-y: auto !important;\n                }\n                .atbots-contact-form h3 {\n                    font-size: 14px !important;\n                    margin: 0 0 8px !important;\n                    padding: 0 !important;\n                }\n                .atbots-contact-form input {\n                    padding: 10px 12px !important;\n                    font-size: 13px !important;\n                    margin: 0 !important;\n                    height: 40px !important;\n                    min-height: 40px !important;\n                }\n                .atbots-contact-submit {\n                    padding: 10px !important;\n                    font-size: 13px !important;\n                    margin: 0 !important;\n                    height: 40px !important;\n                    min-height: 40px !important;\n                }\n                .atbots-input-group {\n                    margin-bottom: 8px !important;\n                }\n                .atbots-validation-message {\n                    font-size: 10px !important;\n                    min-height: 12px !important;\n                    margin: 2px 0 0 2px !important;\n                }\n                .atbots-close-contact-form {\n                    right: 15px !important;\n                    top: 15px !important;\n                    width: 28px !important;\n                    height: 28px !important;\n                    font-size: 18px !important;\n                }\n                .atbots-welcome-bubble {\n                    font-size: 13px;\n                    padding: 10px 10px;\n                    max-width: 95vw;\n                    background: #fff !important;\n                }\n                .atbots-powered-by {\n                    padding: 8px;\n                }\n                .atbots-powered-by img {\n                    height: 18px;\n                }\n            }\n\n            @media screen and (min-width: 481px) and (min-height: 481px) {\n                .atbots-chat-container {\n                    border-radius: 10px;\n                }\n                .atbots-contact-form {\n                    position: absolute !important;\n                    left: 0 !important;\n                    right: 0 !important;\n                    bottom: 0 !important;\n                    width: 100% !important;\n                    max-width: none !important;\n                    margin: 0 !important;\n                    border-radius: 20px 20px 10px 10px !important;\n                    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.15) !important;\n                    z-index: 1001 !important;\n                    display: flex !important;\n                    flex-direction: column !important;\n                }\n            }\n\n            @media screen and (max-width: 768px) and (orientation: portrait) {\n                .atbots-contact-form {\n                    position: fixed !important;\n                    top: auto !important;\n                    bottom: 0 !important;\n                    left: 0 !important;\n                    right: 0 !important;\n                    width: 100% !important;\n                    height: calc(50 * var(--vh, 1vh)) !important;\n                    padding: 20px !important;\n                    border-radius: 20px 20px 0 0 !important;\n                    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.15) !important;\n                    display: flex !important;\n                    flex-direction: column !important;\n                    justify-content: flex-start !important;\n                    z-index: 1002 !important;\n                }\n                .atbots-keyboard-active .atbots-contact-form {\n                    top: 0 !important;\n                    bottom: auto !important;\n                    height: calc(100 * var(--vh, 1vh)) !important;\n                    border-radius: 0 !important;\n                    box-shadow: none !important;\n                    z-index: 1003 !important;\n                }\n                .atbots-contact-form-content {\n                    padding: 20px 20px !important;\n                    width: 100% !important;\n                    max-width: 400px !important;\n                    margin: 0 auto !important;\n                    flex-grow: 1 !important;\n                    display: flex !important;\n                    flex-direction: column !important;\n                    justify-content: space-between !important;\n                    gap: 10px !important;\n                    overflow-y: auto !important;\n                }\n                .atbots-contact-form input:focus {\n                    position: relative;\n                    z-index: 1003;\n                }\n                .atbots-contact-form h3 {\n                    font-size: 16px !important;\n                    margin: 0 0 10px !important;\n                    padding: 0 !important;\n                }\n                .atbots-contact-form input {\n                    padding: 12px 16px !important;\n                    font-size: 14px !important;\n                    height: 44px !important;\n                    min-height: 44px !important;\n                }\n                .atbots-contact-submit {\n                    padding: 12px !important;\n                    font-size: 14px !important;\n                    height: 44px !important;\n                    min-height: 44px !important;\n                }\n                .atbots-input-group {\n                    margin-bottom: 10px !important;\n                }\n                .atbots-validation-message {\n                    font-size: 10px !important;\n                    min-height: 12px !important;\n                    margin: 2px 0 0 2px !important;\n                }\n                .atbots-close-contact-form {\n                    right: 15px !important;\n                    top: 15px !important;\n                    width: 32px !important;\n                    height: 32px !important;\n                    font-size: 20px !important;\n                }\n            }\n\n            @media screen and (max-width: 768px) and (orientation: landscape) {\n                .atbots-contact-form {\n                    position: fixed !important;\n                    top: 0 !important;\n                    left: 0 !important;\n                    right: 0 !important;\n                    bottom: 0 !important;\n                    width: 100vw !important;\n                    height: calc(100 * var(--vh, 1vh)) !important;\n                    padding: 40px !important;\n                    border-radius: 0 !important;\n                    box-shadow: none !important;\n                    display: flex !important;\n                    flex-direction: column !important;\n                    justify-content: center !important;\n                    align-items: center !important;\n                    z-index: 1002 !important;\n                }\n                .atbots-contact-form-content {\n                    padding: 20px !important;\n                    width: 100% !important;\n                    max-width: 500px !important;\n                    margin: 0 auto !important;\n                    flex-grow: 0 !important;\n                    display: flex !important;\n                    flex-direction: column !important;\n                    justify-content: center !important;\n                    gap: 15px !important;\n                }\n                .atbots-contact-form h3 {\n                    font-size: 20px !important;\n                    margin: 0 0 15px !important;\n                    padding: 0 !important;\n                }\n                .atbots-contact-form input {\n                    padding: 16px 20px !important;\n                    font-size: 16px !important;\n                    height: 48px !important;\n                    min-height: 48px !important;\n                }\n                .atbots-contact-submit {\n                    padding: 16px !important;\n                    font-size: 16px !important;\n                    height: 48px !important;\n                    min-height: 48px !important;\n                }\n                .atbots-input-group {\n                    margin-bottom: 12px !important;\n                }\n                .atbots-validation-message {\n                    font-size: 12px !important;\n                    min-height: 14px !important;\n                    margin: 2px 0 0 2px !important;\n                }\n                .atbots-close-contact-form {\n                    right: 20px !important;\n                    top: 20px !important;\n                    width: 36px !important;\n                    height: 36px !important;\n                    font-size: 24px !important;\n                }\n            }\n\n            @supports (-webkit-appearance: none) or (-webkit-touch-callout: none) {\n                @media screen and (max-width: 768px) {\n                    .atbots-contact-form {\n                        height: calc(100 * var(--vh, 1vh)) !important;\n                    }\n                }\n                @media screen and (max-width: 768px) and (orientation: portrait) {\n                    .atbots-contact-form {\n                        height: calc(50 * var(--vh, 1vh)) !important;\n                        bottom: 0 !important;\n                    }\n                    .atbots-keyboard-active .atbots-contact-form {\n                        height: calc(100 * var(--vh, 1vh)) !important;\n                        top: 0 !important;\n                        bottom: auto !important;\n                    }\n                }\n            }\n        ";const o=n(getComputedStyle(document.documentElement).getPropertyValue("--primary-color").trim());o&&(t.textContent=t.textContent.replace("/* Theme color with 10% opacity */",`/* Theme color with 10% opacity */\n                --primary-color-rgb: ${o.r}, ${o.g}, ${o.b};`));if(e.appendChild(t),!e.querySelector('link[href*="poppins"]')){const t=document.createElement("link");t.rel="stylesheet",t.href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap",e.appendChild(t)}}(),o.chatIconCss){const t=document.createElement("style");t.textContent=o.chatIconCss,e.appendChild(t)}!function(){if(!e.querySelector('script[src*="dotlottie-player.mjs"]')){const t=document.createElement("script");t.type="module",t.src="https://unpkg.com/@dotlottie/player-component@2.7.12/dist/dotlottie-player.mjs",e.appendChild(t)}const t=document.createElement("div");t.className="atbots-widget",e.appendChild(t);const n=document.createElement("div");n.className="atbots-chat-icon",n.id="atbotsChatIcon",n.style.backgroundColor="var(--primary-color)",n.style.display="none",n.innerHTML='\n            <button class="atbots-chat-button" style="padding:0; background:transparent; border:none;" id="atbotsChatIconBtn">\n                <dotlottie-player\n                    src="https://lottie.host/a4ba77b1-85ac-4007-9d68-f6da1d232032/YQiXmyk0iT.lottie"\n                    background="transparent"\n                    speed="1"\n                    style="width:48px; height:48px; display:block;"\n                    loop\n                    autoplay\n                    id="atbotsLottieIcon"\n                ></dotlottie-player>\n            </button>\n        ',t.appendChild(n);const a=document.createElement("div");a.className="atbots-chat-container atbots-d-none",a.id="atbotsChatContainer";const r=document.createElement("div");r.className="atbots-chat-header",r.innerHTML=`\n            <div>\n                <strong id="botNameDisplay">${o.botName}</strong>\n            </div>\n            <div class="atbots-header-actions">\n                <button class="atbots-refresh-btn" id="atbotsRefreshBtn" title="Clear chat history">\n                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">\n                        <path d="M21 2v6h-6"></path>\n                        <path d="M3 12a9 9 0 0 1 15-6.7L21 8"></path>\n                        <path d="M3 12a9 9 0 0 0 15 6.7L21 16"></path>\n                    </svg>\n                </button>\n                <button class="atbots-minimize-btn" id="atbotsMinimizeBtn" title="Close chat">\n                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">\n                        <line x1="18" y1="6" x2="6" y2="18"></line>\n                        <line x1="6" y1="6" x2="18" y2="18"></line>\n                    </svg>\n                </button>\n            </div>\n        `,a.appendChild(r);const i=document.createElement("div");i.className="atbots-chat-body",i.id="atbotsChatBody",a.appendChild(i);const l=document.createElement("div");l.className="atbots-chat-footer",l.innerHTML='\n            <input type="text" id="atbotsUserInput" placeholder="Enter your message..." readonly>\n            <button class="atbots-send-button atbots-d-none" id="atbotsSendBtn">\n                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">\n                    <path d="M22 2L15 22L11 13L2 9L22 2Z"></path>\n                </svg>\n            </button>\n        ',a.appendChild(l);const c=document.createElement("div");c.className="atbots-powered-by",c.innerHTML=' <a href="https://aaludra.com" target="_blank" rel="noopener noreferrer">\n            <img src="https://widget.aaludradevelopers.com/static/powered.png" alt="Aaludra">\n        </a>',a.appendChild(c),t.appendChild(a);const d=n.querySelector("#atbotsLottieIcon");d&&d.addEventListener("ready",()=>{setTimeout(()=>{n.style.display="",_&&clearTimeout(_),"false"===localStorage.getItem("atbots_ding_played")&&(_=setTimeout(()=>{!function(){try{const t=new Audio("https://widget.aaludradevelopers.com/static/sounds/ding.mp3");t.volume=.5;let n=!1;const e=()=>{n||(localStorage.setItem("atbots_ding_played","true"),n=!0)};t.play().then(e)["catch"](()=>{t.addEventListener("canplaythrough",()=>{t.play().then(e)["catch"](e)})}),setTimeout(e,4e3),t.load()}catch(t){localStorage.setItem("atbots_ding_played","true")}}()},6e3)),s.chatIcon&&s.chatContainer.classList.contains("atbots-d-none")&&o.welcomeMessage&&N()},1e3)});const p=n.querySelector("#atbotsChatIconBtn");p&&p.addEventListener("click",function(t){t.preventDefault(),h(),D()})}(),s={chatIcon:e.getElementById("atbotsChatIcon"),chatContainer:e.getElementById("atbotsChatContainer"),chatBody:e.getElementById("atbotsChatBody"),userInput:e.getElementById("atbotsUserInput"),sendBtn:e.getElementById("atbotsSendBtn"),refreshBtn:e.getElementById("atbotsRefreshBtn"),minimizeBtn:e.getElementById("atbotsMinimizeBtn")},function(){try{l.sent=new Audio("https://widget.aaludradevelopers.com//static/sounds/message.mp3"),l.received=new Audio("https://widget.aaludradevelopers.com/static/sounds/message.mp3"),l.sent.volume=.2,l.received.volume=.3,l.sent.load(),l.received.load(),l.sent.onerror=()=>console.error("Error loading sent sound"),l.received.onerror=()=>console.error("Error loading received sound")}catch(t){console.error("Error initializing sounds:",t)}}(),function(){s.chatIcon.addEventListener("click",h),s.refreshBtn.addEventListener("click",function(t){if(document.querySelector(".atbots-contact-form"))return t.stopPropagation(),void t.preventDefault();!async function(){const t=y(a.sessionId);t&&await p(t);E();const n=y(a.userId);if(n){const t=document.querySelector("script[data-tenant-id]"),e=t?.getAttribute("data-tenant-id");if(e){const t=await fetch(`${o.apiEndpoint}/chat-session/create`,{method:"POST",headers:{"Content-Type":"application/json","X-Tenant-ID":e},body:JSON.stringify({user_id:n})});if(t.ok){const n=await t.json();n.session_id&&(x(a.sessionId,n.session_id),x(a.sessionLastActive,Date.now().toString()))}}}s.chatBody.innerHTML="",v()}()}),s.minimizeBtn.addEventListener("click",function(t){if(document.querySelector(".atbots-contact-form"))return t.stopPropagation(),void t.preventDefault();f()}),s.sendBtn.addEventListener("click",T),s.userInput.addEventListener("keydown",function(t){"Enter"!==t.key||t.shiftKey||(t.preventDefault(),T())}),s.userInput.addEventListener("input",function(){""!==this.value.trim()?(s.sendBtn.classList.remove("atbots-d-none"),this.style.borderColor=getComputedStyle(document.documentElement).getPropertyValue("--primary-color")):(s.sendBtn.classList.add("atbots-d-none"),this.style.borderColor="#e2e8f0")}),s.userInput.addEventListener("blur",function(){""===this.value.trim()&&(this.style.borderColor="#e2e8f0")}),s.userInput.addEventListener("click",function(t){window.innerWidth<=480&&(this.readOnly=!1,this.focus())}),s.userInput.addEventListener("blur",function(){window.innerWidth<=480?this.readOnly=!0:this.readOnly=!1}),o.clearOnReload&&window.removeEventListener("beforeunload",E);void 0!==window.visualViewport&&window.visualViewport.addEventListener("resize",u);new MutationObserver(()=>{const t=!!document.querySelector(".atbots-contact-form");s.refreshBtn&&s.minimizeBtn&&(t?(s.refreshBtn.style.display="none",s.minimizeBtn.style.display="none",s.chatBody&&(s.chatBody.style.filter="blur(3px) brightness(1.08)",s.chatBody.style.background="rgba(255,255,255,0.7)")):(s.refreshBtn.style.display="",s.minimizeBtn.style.display="",s.chatBody&&(s.chatBody.style.filter="",s.chatBody.style.background="")))}).observe(document.body,{childList:!0,subtree:!0})}(),s.chatBody.innerHTML="",g(),window.addEventListener("resize",b),window.addEventListener("orientationchange",()=>{setTimeout(b,100)}),void 0!==window.visualViewport&&(window.visualViewport.addEventListener("resize",b),window.visualViewport.addEventListener("scroll",b)),b(),setTimeout(()=>{s.chatIcon&&s.chatContainer.classList.contains("atbots-d-none")&&N()},500)}catch(t){console.error("Failed to initialize chat:",t)}else console.error("No tenant ID provided")},setConfig:function(t){Object.assign(o,t),t.logoUrl&&P(t.logoUrl)},setLogo:P,setApiEndpoint:function(t){o.apiEndpoint=t},setWelcomeMessage:function(t){o.welcomeMessage=t},clearHistory:E,open:h,close:f,setClearOnReload:function(t){o.clearOnReload=t},onChatToggle:function(){}}}();null===localStorage.getItem("atbots_ding_played")&&localStorage.setItem("atbots_ding_played","false"),function(){function t(){"false"===localStorage.getItem("atbots_ding_played")&&"function"==typeof atbotsChat.playWelcomeDing&&atbotsChat.playWelcomeDing()}null===localStorage.getItem("atbots_ding_played")&&localStorage.setItem("atbots_ding_played","false"),window.addEventListener("pointerdown",t,{once:!0,capture:!0}),window.addEventListener("keydown",t,{once:!0,capture:!0})}(),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",()=>atbotsChat.init()):atbotsChat.init();
