
(function () {
    var ICFG = window.InstinctorChatConfig || {};

    (function() {
        var lang = (document.documentElement.lang || '').toLowerCase().split('-')[0];
        var rtlLangs = ['ar','he','fa','ur','yi','ku','ps','sd','ug'];
        var isRtl = document.documentElement.dir === 'rtl' || rtlLangs.indexOf(lang) !== -1;
        var widget = document.getElementById('instinctor-chat');
        if (widget && isRtl) widget.setAttribute('data-position', 'left');
    })();
    var InstinctorChat = {
        isOpen: false,
        visitorId: ICFG.visitorId || '',
        lastMessageId: 0,
        pollInterval: null,
        visitorName: '',
        visitorEmail: '',
        visitorPhone: '',
        chatStarted: false,
        collectName: !!ICFG.collectName,
        collectEmail: !!ICFG.collectEmail,
        collectPhone: !!ICFG.collectPhone,
        ghostInfoStep: 0,
        pendingScreenshot: null,
        pendingFile: null,
        pendingFileName: null,
        isSending: false,
        ajaxurl: ICFG.ajaxurl || '',
        html2canvasUrl: ICFG.html2canvasUrl || '',
        brandColor: ICFG.brandColor || '#1d2327',
        nonce: ICFG.nonce || '',

        ghostTrigger: function() {
            var widget = document.getElementById('instinctor-chat');
            if (!widget) return;
            if (!this.isOpen) {
                this.isOpen = true;
                widget.classList.add('open');
                if (InstinctorChat._barExpand) InstinctorChat._barExpand();
                
                (function(){
                    var p = document.getElementById('instinctor-ghost-pill');
                    if (!p) return;
                    var luma = 255;
                    [document.documentElement, document.body].forEach(function(el){
                        if (luma !== 255) return;
                        var bg = window.getComputedStyle(el).backgroundColor;
                        var m = bg.replace(/[rgba()]/g,'').split(',');
                        var a = m[3] !== undefined ? parseFloat(m[3]) : 1;
                        if (m.length >= 3 && a > 0.1)
                            luma = 0.299*+m[0] + 0.587*+m[1] + 0.114*+m[2];
                    });
                    p.classList.toggle('pill-on-dark', luma < 128);
                })();
                var pill = document.getElementById('instinctor-ghost-pill');
                var bubbles = document.getElementById('instinctor-ghost-bubbles');
                
                var pillWrap0 = document.getElementById('instinctor-ghost-pill-wrap');
                if (pill) {
                    if (pillWrap0) pillWrap0.style.setProperty('display','flex','important');
                    pill.style.setProperty('display','flex','important');

                    

                    
                    void pill.offsetHeight;
                    pill.classList.add('ghost-open');
                    var inner = pill.querySelector('.instinctor-ghost-pill-inner');
                    if (inner) inner.addEventListener('animationend', function() {
                        inner.style.transform = 'translateX(0)';
                        inner.style.animation = 'none';
                    }, { once: true });
                }
                
                if (bubbles) {
                    bubbles.style.setProperty('display','flex','important');
                    void bubbles.offsetHeight;
                    bubbles.classList.add('ghost-visible');
                }
                var gf = document.getElementById('instinctor-ghost-input');
                if (gf) setTimeout(function(){ gf.focus(); }, 220);
            } else {
                if (this.hasText || this.pendingScreenshot || this.pendingFile) {
                    if (this._sendLock) return;
                    this._sendLock = true;
                    var self = this;
                    setTimeout(function(){ self._sendLock = false; }, 600);
                    this.ghostSend();
                } else {
                    this.close();
                }
            }
        },

        ghostFieldInput: function(field) {
            var widget = document.getElementById('instinctor-chat');
            var hasContent = field.value.trim().length > 0 || !!this.pendingScreenshot || !!this.pendingFile;
            this.hasText = hasContent;
            if (widget) widget.classList.toggle('ghost-has-text', hasContent);

            

            
            var _cb = document.getElementById('instinctor-chat-bar');
            if (_cb) _cb.classList.toggle('bar-has-text', hasContent);
            var pill = document.getElementById('instinctor-ghost-pill');
            var pillWrap = document.getElementById('instinctor-ghost-pill-wrap');
            if (pill) {
                var maxW = 300, minW = 160;
                
                var mirror = document.getElementById('instinctor-text-mirror');
                var textW = minW;
                if (mirror) {
                    mirror.textContent = field.value || '';
                    textW = Math.max(minW, mirror.offsetWidth);
                }
                if (textW < maxW) {
                    
                    if (pillWrap) pillWrap.style.setProperty('width', textW + 'px', 'important');
                    pill.style.setProperty('width', '100%', 'important');
                    field.style.whiteSpace = 'nowrap';
                    field.style.height = '';
                } else {
                    
                    if (pillWrap) pillWrap.style.setProperty('width', maxW + 'px', 'important');
                    pill.style.setProperty('width', '100%', 'important');
                    field.style.whiteSpace = 'normal';
                    field.style.height = 'auto';
                    field.style.height = Math.min(120, field.scrollHeight) + 'px';
                }
            }
            this.signalTyping();
        },

        ghostSend: function() {
            this._greetingSent = true;
            var _greeting = document.getElementById('instinctor-ghost-greeting');
            if (_greeting) _greeting.classList.add('greeting-gone');
            var field = document.getElementById('instinctor-ghost-input');
            if (!field) return;
            var text = field.value.trim();
            
            if (!text && !this.pendingScreenshot && !this.pendingFile) return;
            field.value = '';
            this.hasText = false;
            var widget = document.getElementById('instinctor-chat');
            if (widget) widget.classList.remove('ghost-has-text');
            
            var gp = document.getElementById('instinctor-ghost-preview');
            if (gp) { gp.style.display = 'none'; gp.innerHTML = ''; }
            
            if (this.pendingScreenshot) {
                this.sendScreenshot(this.pendingScreenshot);
                this.pendingScreenshot = null;
            }
            
            if (this.pendingFile) {
                this.sendFile(this.pendingFile, this.pendingFileName);
                this.pendingFile = null; this.pendingFileName = null;
            }
            if (!text) return;
            
            if (this.ghostInfoStep > 0) { this.ghostInfoCollect(text); return; }
            this.ghostAddBubble(text, 'visitor');
            this.ghostSendText(text);
            
            if (!this.chatStarted) {
                this.chatStarted = true;
                this.ghostStartInfoCollection();
            }
        },

        ghostSendText: function(text) {
            var self = this;

            

            
            
            this.isSending = true;
            var data = new FormData();
            data.append('action', 'instinctor_chat_send');
            data.append('nonce', this.nonce);
            data.append('message', text);
            data.append('visitor_id', this.visitorId);
            data.append('name', this.visitorName);
            data.append('email', this.visitorEmail);
            data.append('phone', this.visitorPhone);
            data.append('page_url', window.location.href);
            
            try {
                var cartEl = document.getElementById('instinctor-cart-data');
                if (cartEl) data.append('cart', cartEl.value);
            } catch(e) {}
            fetch(this.ajaxurl, { method: 'POST', body: data })
            .then(function(r){ return r.json(); })
            .then(function(resp){
                if (resp.success && resp.data) {
                    self.conversationId = resp.data.conversation_id;
                    self.lastMessageId = resp.data.message_id || self.lastMessageId;
                    if (!self.pollInterval) self.startPolling();
                    if (resp.data.ai_reply) {
                        self.ghostShowTyping();
                        var delayMs = resp.data.ai_delay_ms || 0; 
                        setTimeout(function(){
                            self.ghostHideTyping();
                            self.ghostAddBubble(resp.data.ai_reply, 'admin');
                        }, delayMs);
                        if (resp.data.ai_message_id) self.lastMessageId = resp.data.ai_message_id;
                    }
                    if (resp.data.needs_handoff) self.startHandoffCheck();
                }

                self.isSending = false;
            }).catch(function() { self.isSending = false; });
        },

        ghostStartInfoCollection: function() {
            var fields = [];
            if (this.collectName  && !this.visitorName)  fields.push('name');
            if (this.collectEmail && !this.visitorEmail) fields.push('email');
            if (this.collectPhone && !this.visitorPhone) fields.push('phone');
            if (!fields.length) return;
            this._infoFields = fields;
            this.ghostInfoStep = 1;
            var prompts = { name: 'What’s your name?', email: 'What’s your email address?', phone: 'What’s your phone number?' };
            var self = this;
            setTimeout(function() {
                self.ghostShowTyping();
                setTimeout(function() {
                    self.ghostHideTyping();
                    self.ghostAddBubble(prompts[fields[0]], 'admin');
                }, 700);
            }, 400);
        },

        ghostInfoCollect: function(answer) {
            var field = this._infoFields[this.ghostInfoStep - 1];
            if (field === 'name')  { this.visitorName  = answer; localStorage.setItem('instinctor_visitor_name',  answer); }
            if (field === 'email') { this.visitorEmail = answer; localStorage.setItem('instinctor_visitor_email', answer); }
            if (field === 'phone') { this.visitorPhone = answer; localStorage.setItem('instinctor_visitor_phone', answer); }
            this.ghostAddBubble(answer, 'visitor');
            var prompts = { name: 'What’s your name?', email: 'What’s your email address?', phone: 'What’s your phone number?' };
            this.ghostInfoStep++;
            var self = this;
            if (this.ghostInfoStep - 1 < this._infoFields.length) {
                var next = this._infoFields[this.ghostInfoStep - 1];
                setTimeout(function() {
                    self.ghostShowTyping();
                    setTimeout(function() { self.ghostHideTyping(); self.ghostAddBubble(prompts[next], 'admin'); }, 700);
                }, 300);
            } else {
                this.ghostInfoStep = 0;
                
                var data = new FormData();
                data.append('action', 'instinctor_chat_send');
                data.append('nonce', this.nonce);
                data.append('message', '');
                data.append('visitor_id', this.visitorId);
                data.append('name', this.visitorName);
                data.append('email', this.visitorEmail);
                data.append('phone', this.visitorPhone);
                fetch(this.ajaxurl, { method: 'POST', body: data }).catch(function(){});
            }
        },

        ghostAddBubble: function(text, side) {
            var container = document.getElementById('instinctor-ghost-bubbles');
            if (!container) return;
            if (!container.classList.contains('ghost-visible')) container.classList.add('ghost-visible');
            var el = document.createElement('div');
            el.className = 'instinctor-ghost-bubble ' + side;
            
            var imageMatch = typeof text === 'string' && text.match(/\[image\](.*?)\[\/image\]/);
            if (imageMatch) {
                var imgEl = document.createElement('img');
                imgEl.src = imageMatch[1];
                imgEl.style.cssText = 'max-width:160px;border-radius:4px;display:block;cursor:pointer;';
                imgEl.onclick = function() { window.open(this.src); };
                el.appendChild(imgEl);
            } else if (typeof text === 'string' && text.charAt(0) === '<') {
                
                el.innerHTML = text;
            } else {
                el.textContent = text;
            }
            container.appendChild(el);
            container.scrollTop = container.scrollHeight;

            container.classList.toggle('has-overflow', container.scrollHeight > container.clientHeight + 2);

            
            
        },

        ghostShowTyping: function() {
            var container = document.getElementById('instinctor-ghost-bubbles');
            if (!container || container.querySelector('.instinctor-ghost-typing')) return;
            var el = document.createElement('div');
            el.className = 'instinctor-ghost-typing';
            el.innerHTML = '<span></span><span></span><span></span>';
            container.appendChild(el);
        },

        ghostHideTyping: function() {
            var container = document.getElementById('instinctor-ghost-bubbles');
            if (!container) return;
            var t = container.querySelector('.instinctor-ghost-typing');
            if (t) t.parentNode.removeChild(t);
        },

        close: function() {
            var widget = document.getElementById('instinctor-chat');
            this.isOpen = false;
            if (InstinctorChat._barCollapse) InstinctorChat._barCollapse();
            this.hasText = false;
            widget.classList.remove('open', 'ghost-has-text');
            
            var gf = document.getElementById('instinctor-ghost-input');
            if (gf) gf.value = '';
            
            var pill = document.getElementById('instinctor-ghost-pill');
            if (pill) {
                var pillWrap2 = document.getElementById('instinctor-ghost-pill-wrap');
                if (pillWrap2) pillWrap2.style.setProperty('width', '160px', 'important');
                pill.style.setProperty('width', '100%', 'important');
                var gf = document.getElementById('instinctor-ghost-input');
                if (gf) { gf.style.height = ''; gf.style.whiteSpace = 'nowrap'; gf.value = ''; }
                pill.classList.remove('ghost-open');
                var _greeting2 = document.getElementById('instinctor-ghost-greeting');
                if (_greeting2 && !this._greetingSent) _greeting2.classList.remove('greeting-gone');
                requestAnimationFrame(function() {
                    pill.classList.add('ghost-closing');
                    setTimeout(function() {
                        pill.classList.remove('ghost-closing');
                        pill.style.removeProperty('display');
                    }, 300);
                });
            }
            var ring = document.getElementById('instinctor-ghost-ring');
            if (ring) { ring.classList.remove('ghost-open'); ring.style.removeProperty('display'); }
            
            var bubbles = document.getElementById('instinctor-ghost-bubbles');
            if (bubbles) {
                bubbles.classList.remove('ghost-visible');
                setTimeout(function() {
                    bubbles.style.removeProperty('display');
                    bubbles.innerHTML = '';
                }, 300);
            }
            var preview = document.getElementById('instinctor-ghost-preview');
            if (preview) { preview.style.display = 'none'; preview.innerHTML = ''; }
            
            this.conversationId = null;
            this.lastMessageId = 0;
            this.chatStarted = false;
            this.ghostInfoStep = 0;
            if (this.pollInterval) { clearInterval(this.pollInterval); this.pollInterval = null; }
        },

        togglePage: function(pageId) {
            var data = new FormData();
            data.append('action', 'instinctor_chat_toggle_page');
            data.append('post_id', pageId);
            data.append('_wpnonce', this.nonce);
            fetch(this.ajaxurl, { method: 'POST', body: data })
            .then(function(r) { return r.json(); })
            .then(function(resp) {
                if (resp.success) {
                    var widget = document.getElementById('instinctor-chat');
                    var showBtn = document.getElementById('instinctor-vis-show');
                    var hideBtn = document.getElementById('instinctor-vis-hide');
                    if (resp.data.hidden) {
                        widget.classList.add('chat-hidden');
                    } else {
                        widget.classList.remove('chat-hidden');
                    }
                    
                    if (showBtn) showBtn.classList.toggle('active', !resp.data.hidden);
                    if (hideBtn) hideBtn.classList.toggle('active', resp.data.hidden);
                }
            });
        },

        handoffCheckInterval: null,
        startHandoffCheck: function() {
            var self = this;
            if (this.handoffCheckInterval) clearInterval(this.handoffCheckInterval);
            var checkCount = 0;
            this.handoffCheckInterval = setInterval(function() {
                checkCount++;
                if (checkCount > 20) { clearInterval(self.handoffCheckInterval); return; }
                var data = new FormData();
                data.append('action', 'instinctor_chat_check_handoff');
                data.append('nonce', self.nonce);
                data.append('conversation_id', self.conversationId);
                fetch(self.ajaxurl, {method: 'POST', body: data}).then(function(r) { return r.json(); }).then(function(response) {
                    if (response.success && response.data) {
                        if (response.data.human_responded || response.data.needs_followup) {
                            clearInterval(self.handoffCheckInterval);
                            if (response.data.needs_followup && response.data.followup_message) {
                                self.ghostAddBubble(response.data.followup_message, 'admin');
                            }
                        }
                    }
                }).catch(function() {});
            }, 5000);
        },

        screenshot: function() {
            var self = this;
            var widget = document.getElementById('instinctor-chat');
            widget.style.display = 'none';
            var overlay = document.createElement('div');
            overlay.id = 'instinctor-screenshot-overlay';
            overlay.innerHTML = '<div class="instinctor-screenshot-hint">Click and drag to select area. Press ESC to cancel.</div>';
            document.body.appendChild(overlay);
            var startX, startY, selectionBox, isSelecting = false;
            overlay.onmousedown = function(e) { isSelecting = true; startX = e.clientX; startY = e.clientY; selectionBox = document.createElement('div'); selectionBox.className = 'instinctor-selection-box'; selectionBox.style.left = startX + 'px'; selectionBox.style.top = startY + 'px'; overlay.appendChild(selectionBox); };
            overlay.onmousemove = function(e) { if (!isSelecting || !selectionBox) return; var width = e.clientX - startX; var height = e.clientY - startY; selectionBox.style.width = Math.abs(width) + 'px'; selectionBox.style.height = Math.abs(height) + 'px'; selectionBox.style.left = (width < 0 ? e.clientX : startX) + 'px'; selectionBox.style.top = (height < 0 ? e.clientY : startY) + 'px'; };
            overlay.onmouseup = function(e) { if (!isSelecting) return; isSelecting = false; var rect = selectionBox.getBoundingClientRect(); overlay.remove(); if (rect.width < 10 || rect.height < 10) { widget.style.display = ''; return; } self.captureArea(rect, function(dataUrl) { widget.style.display = ''; if (dataUrl) self.showScreenshotPreview(dataUrl); }); };
            var escHandler = function(e) { if (e.key === 'Escape') { overlay.remove(); widget.style.display = ''; document.removeEventListener('keydown', escHandler); } };
            document.addEventListener('keydown', escHandler);
        },

        captureArea: function(rect, callback) {
            if (typeof html2canvas === 'undefined') {
                
                var s = document.createElement('script');
                s.src = this.html2canvasUrl || '';
                if (!s.src) { callback(null); return; }
                s.onload = function() { html2canvas(document.body, { x: rect.left + window.scrollX, y: rect.top + window.scrollY, width: rect.width, height: rect.height, useCORS: true, logging: false, scale: 1 }).then(function(canvas) { callback(canvas.toDataURL('image/png')); }).catch(function() { callback(null); }); };
                document.head.appendChild(s);
            } else {
                html2canvas(document.body, { x: rect.left + window.scrollX, y: rect.top + window.scrollY, width: rect.width, height: rect.height, useCORS: true, logging: false, scale: 1 }).then(function(canvas) { callback(canvas.toDataURL('image/png')); }).catch(function() { callback(null); });
            }
        },

        showScreenshotPreview: function(dataUrl) {
            this.pendingScreenshot = dataUrl;
            this.hasText = true;
            var widget = document.getElementById('instinctor-chat');
            if (widget) widget.classList.add('ghost-has-text');
            var preview = document.getElementById('instinctor-ghost-preview');
            if (!preview) return;
            preview.innerHTML = '<div class="instinctor-preview-inner" style="position:relative;display:inline-block;"><img src="' + dataUrl + '" style="max-width:160px;max-height:100px;border-radius:3px;display:block;"><button onclick="InstinctorChat.removeScreenshot()" style="position:absolute;top:2px;right:2px;background:#1d2327;color:#fff;border:none;border-radius:50%;width:16px;height:16px;font-size:10px;cursor:pointer;line-height:1;padding:0;">×</button></div>';
            preview.style.display = 'block';
            
            var _f = document.getElementById('instinctor-ghost-input');
            if (_f) setTimeout(function(){ _f.focus(); }, 30);
        },
        removeScreenshot: function() {
            this.pendingScreenshot = null;
            var el = document.getElementById('instinctor-ghost-preview');
            if (el) { el.style.display = 'none'; el.innerHTML = ''; }
        },
        sendScreenshot: function(dataUrl) { var self = this; this.isSending = true; this.ghostAddBubble('<img src="' + dataUrl + '" style="max-width:160px;border-radius:4px;display:block;" onclick="window.open(this.src)">', 'visitor'); var data = new FormData(); data.append('action', 'instinctor_chat_send_screenshot'); data.append('nonce', this.nonce); data.append('visitor_id', this.visitorId); data.append('visitor_name', this.visitorName); data.append('image', dataUrl); fetch(this.ajaxurl, {method: 'POST', body: data}).then(function(r) { return r.json(); }).then(function(response) { self.isSending = false; if (response.success && response.data) { self.lastMessageId = response.data.message_id || self.lastMessageId; } }).catch(function() { self.isSending = false; }); },

        handleFile: function(input) { var self = this; var file = input.files[0]; if (!file) return; if (file.size > 5 * 1024 * 1024) { this.showToast('File too large. Maximum size is 5MB.'); input.value = ''; return; } var reader = new FileReader(); reader.onload = function(e) { self.pendingFile = e.target.result; self.pendingFileName = file.name; self.showFilePreview(file.name); }; reader.readAsDataURL(file); input.value = ''; },
        showFilePreview: function(fileName) {
            this.hasText = true;
            var widget = document.getElementById('instinctor-chat');
            if (widget) widget.classList.add('ghost-has-text');
            var preview = document.getElementById('instinctor-ghost-preview');
            if (!preview) return;
            preview.innerHTML = '<div style="display:flex;align-items:center;gap:6px;font-size:12px;color:#1d2327;">&#128196; <span style="max-width:120px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">' + this.escapeHtml(fileName) + '</span><button onclick="InstinctorChat.removeFile()" style="background:none;border:none;cursor:pointer;font-size:14px;color:#999;padding:0;line-height:1;">&times;</button></div>';
            preview.style.display = 'block';
            
            var _f = document.getElementById('instinctor-ghost-input');
            if (_f) setTimeout(function(){ _f.focus(); }, 30);
        },
        removeFile: function() {
            this.pendingFile = null; this.pendingFileName = null;
            var el = document.getElementById('instinctor-ghost-preview');
            if (el) { el.style.display = 'none'; el.innerHTML = ''; }
        },
        sendFile: function(dataUrl, fileName) { var self = this; this.isSending = true; var shortName = fileName.length > 30 ? fileName.substring(0, 27) + '...' : fileName; this.ghostAddBubble('File sent: ' + shortName, 'visitor'); var data = new FormData(); data.append('action', 'instinctor_chat_send_file'); data.append('nonce', this.nonce); data.append('visitor_id', this.visitorId); data.append('visitor_name', this.visitorName); data.append('file', dataUrl); data.append('filename', fileName); fetch(this.ajaxurl, {method: 'POST', body: data}).then(function(r) { return r.json(); }).then(function(response) { self.isSending = false; if (response.success && response.data) { self.lastMessageId = response.data.message_id || self.lastMessageId; } }).catch(function() { self.isSending = false; }); },

        startPolling: function() { var self = this; this.pollInterval = setInterval(function() { self.pollMessages(); }, 3000); },
        signalTyping: function() { if (!this.visitorId || this._typingThrottle) return; var self = this; this._typingThrottle = true; setTimeout(function() { self._typingThrottle = false; }, 3000); var data = new FormData(); data.append('action', 'instinctor_chat_set_typing'); data.append('visitor_id', this.visitorId); fetch(this.ajaxurl, {method:'POST', body:data}).catch(function(){}); },
        pollMessages: function() { if (this.isSending) return; var self = this; var data = new FormData(); data.append('action', 'instinctor_chat_poll_messages'); data.append('nonce', this.nonce); data.append('visitor_id', this.visitorId); data.append('last_id', this.lastMessageId); fetch(this.ajaxurl, {method: 'POST', body: data}).then(function(r) { return r.json(); }).then(function(response) { if (response.success && response.data && response.data.messages) { response.data.messages.forEach(function(msg) { if (parseInt(msg.id) > self.lastMessageId) { self.ghostHideTyping(); self.ghostAddBubble(msg.message, msg.sender_type === 'admin' ? 'admin' : 'visitor'); self.lastMessageId = parseInt(msg.id); } }); } }).catch(function() {}); },

        showToast: function(message) { var existing = document.querySelector('.instinctor-toast'); if (existing) existing.remove(); var toast = document.createElement('div'); toast.className = 'instinctor-toast'; toast.textContent = message; document.body.appendChild(toast); setTimeout(function() { toast.remove(); }, 3000); },
        escapeHtml: function(text) { var div = document.createElement('div'); div.textContent = text; return div.innerHTML; }
    };
    
    window.InstinctorChat = InstinctorChat;

    document.addEventListener('DOMContentLoaded', function() { if (window.self !== window.top) return; InstinctorChat.visitorName = localStorage.getItem('instinctor_visitor_name') || ''; InstinctorChat.visitorEmail = localStorage.getItem('instinctor_visitor_email') || ''; InstinctorChat.visitorPhone = localStorage.getItem('instinctor_visitor_phone') || '';  });

    document.addEventListener('click', function(e) {
        var t = e.target.closest('a[href="#chat-open"], a[href="#chat"], [data-chat-trigger]');
        if (!t) return;
        e.preventDefault();
        if (window.InstinctorChat && InstinctorChat.ghostTrigger) InstinctorChat.ghostTrigger();
    });

    if (ICFG.isStandalone) {
        
        (function(){
            var bpInit = function() {
                var b = document.querySelector('.instinctor-branding');
                if (b) {
                    b.style.cssText = 'display:inline-block !important;padding:6px 10px !important;background:#fff !important;text-align:left !important;border-radius:0 12px 0 12px !important;';
                }
            };
            if (document.readyState === 'loading') {
                document.addEventListener('DOMContentLoaded', bpInit);
            } else {
                setTimeout(bpInit, 100);
            }
        })();
    }
})();
