/* ========================================
   BUILDER BEE CHAT WIDGET - ENTERPRISE GRADE v5.1
   FIXED: Text orientation, scrolling, responsive
   ======================================== */

/* Reset any problematic inherited styles */
#intercom-container *,
#intercom-container *::before,
#intercom-container *::after {
    writing-mode: horizontal-tb !important;
    text-orientation: mixed !important;
    direction: ltr !important;
    unicode-bidi: normal !important;
    box-sizing: border-box !important;
}

/* CSS Variables */
:root {
    --bbp-primary: #FF6B35;
    --bbp-primary-dark: #E55A2B;
    --bbp-secondary: #F7931E;
    --bbp-dark-bg: rgba(15, 23, 42, 0.98);
    --bbp-card-bg: rgba(30, 41, 59, 0.95);
    --bbp-text-light: #ffffff;
    --bbp-text-muted: rgba(255, 255, 255, 0.7);
    --bbp-border-subtle: rgba(255, 255, 255, 0.1);
    --bbp-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
    --bbp-radius-lg: 20px;
    --bbp-radius-md: 16px;
    --bbp-radius-sm: 12px;
}

/* ========================================
   CHAT CONTAINER
   ======================================== */
#intercom-container.intercom-namespace {
    position: fixed !important;
    bottom: 20px !important;
    right: 20px !important;
    z-index: 999999 !important;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
    font-size: 14px !important;
    line-height: 1.5 !important;
    -webkit-font-smoothing: antialiased !important;
    -moz-osx-font-smoothing: grayscale !important;
}

/* ========================================
   CHAT FRAME - The chat window
   ======================================== */
#intercom-frame.intercom-frame {
    position: fixed !important;
    bottom: 90px !important;
    right: 20px !important;
    width: 380px !important;
    max-width: calc(100vw - 40px) !important;
    height: 520px !important;
    max-height: calc(100vh - 120px) !important;
    background: var(--bbp-dark-bg) !important;
    border-radius: var(--bbp-radius-lg) !important;
    box-shadow: var(--bbp-shadow) !important;
    display: none !important;
    flex-direction: column !important;
    overflow: hidden !important;
    backdrop-filter: blur(20px) !important;
    border: 1px solid var(--bbp-border-subtle) !important;
}

#intercom-frame.intercom-frame.active {
    display: flex !important;
}

/* ========================================
   MESSENGER STRUCTURE
   ======================================== */
.intercom-messenger-frame {
    display: flex !important;
    flex-direction: column !important;
    height: 100% !important;
    overflow: hidden !important;
}

.intercom-messenger {
    display: flex !important;
    flex-direction: column !important;
    height: 100% !important;
    overflow: hidden !important;
}

/* ========================================
   CHAT HEADER
   ======================================== */
.intercom-messenger-header {
    background: linear-gradient(135deg, var(--bbp-primary) 0%, var(--bbp-secondary) 100%) !important;
    padding: 16px 20px !important;
    flex-shrink: 0 !important;
    position: relative !important;
    display: flex !important;
    align-items: center !important;
    min-height: 70px !important;
}

.intercom-messenger-header-content {
    display: flex !important;
    align-items: center !important;
    gap: 12px !important;
    flex: 1 !important;
}

.intercom-avatar {
    width: 44px !important;
    height: 44px !important;
    background: rgba(255, 255, 255, 0.2) !important;
    border-radius: 50% !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    flex-shrink: 0 !important;
}

.intercom-avatar .bee-icon {
    font-size: 24px !important;
    line-height: 1 !important;
}

.intercom-header-text {
    flex: 1 !important;
    min-width: 0 !important;
}

.intercom-header-title {
    color: var(--bbp-text-light) !important;
    font-size: 16px !important;
    font-weight: 600 !important;
    margin: 0 !important;
    line-height: 1.3 !important;
    display: block !important;
}

.intercom-header-subtitle {
    color: rgba(255, 255, 255, 0.85) !important;
    font-size: 13px !important;
    font-weight: 400 !important;
    margin: 2px 0 0 0 !important;
    line-height: 1.3 !important;
    display: block !important;
}

/* Close Button */
#close-chat.intercom-close-button {
    position: absolute !important;
    top: 12px !important;
    right: 12px !important;
    width: 32px !important;
    height: 32px !important;
    background: rgba(255, 255, 255, 0.15) !important;
    border: none !important;
    border-radius: 8px !important;
    color: var(--bbp-text-light) !important;
    font-size: 20px !important;
    font-weight: 300 !important;
    cursor: pointer !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    transition: all 0.2s ease !important;
    line-height: 1 !important;
    padding: 0 !important;
}

#close-chat.intercom-close-button:hover {
    background: rgba(255, 255, 255, 0.25) !important;
    transform: scale(1.05) !important;
}

/* ========================================
   CHAT BODY - Messages container
   ======================================== */
.intercom-messenger-body {
    display: flex !important;
    flex-direction: column !important;
    flex: 1 !important;
    min-height: 0 !important;
    overflow: hidden !important;
    background: var(--bbp-card-bg) !important;
}

/* ========================================
   CONVERSATION - Scrollable messages
   ======================================== */
#chat-messages.intercom-conversation {
    flex: 1 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    padding: 20px !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
    scroll-behavior: smooth !important;
    min-height: 0 !important;
}

/* Scrollbar */
#chat-messages.intercom-conversation::-webkit-scrollbar {
    width: 6px !important;
}

#chat-messages.intercom-conversation::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05) !important;
    border-radius: 3px !important;
}

#chat-messages.intercom-conversation::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2) !important;
    border-radius: 3px !important;
}

#chat-messages.intercom-conversation::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3) !important;
}

/* ========================================
   CHAT MESSAGES - Bubbles
   ======================================== */
.intercom-comment {
    max-width: 85% !important;
    display: flex !important;
    flex-direction: column !important;
    animation: messageSlideIn 0.3s ease-out !important;
}

@keyframes messageSlideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.intercom-comment-body {
    display: flex !important;
    width: 100% !important;
}

.intercom-comment-text {
    padding: 12px 16px !important;
    border-radius: var(--bbp-radius-md) !important;
    font-size: 14px !important;
    line-height: 1.5 !important;
    word-wrap: break-word !important;
    word-break: break-word !important;
    white-space: pre-wrap !important;
    display: block !important;
    width: 100% !important;
    /* CRITICAL: Force horizontal text */
    writing-mode: horizontal-tb !important;
    text-orientation: mixed !important;
    direction: ltr !important;
}

/* Admin/Bot Messages - Left aligned */
.intercom-comment.intercom-comment-from-admin {
    align-self: flex-start !important;
}

.intercom-comment.intercom-comment-from-admin .intercom-comment-text {
    background: linear-gradient(135deg, var(--bbp-primary) 0%, var(--bbp-secondary) 100%) !important;
    color: var(--bbp-text-light) !important;
    border-radius: var(--bbp-radius-md) var(--bbp-radius-md) var(--bbp-radius-md) 4px !important;
    box-shadow: 0 2px 8px rgba(255, 107, 53, 0.3) !important;
}

/* User Messages - Right aligned */
.intercom-comment.intercom-comment-from-user {
    align-self: flex-end !important;
}

.intercom-comment.intercom-comment-from-user .intercom-comment-text {
    background: rgba(100, 116, 139, 0.6) !important;
    color: var(--bbp-text-light) !important;
    border-radius: var(--bbp-radius-md) var(--bbp-radius-md) 4px var(--bbp-radius-md) !important;
}

/* Error Messages */
.intercom-comment.error-message .intercom-comment-text {
    background: rgba(239, 68, 68, 0.2) !important;
    color: #fca5a5 !important;
    border: 1px solid rgba(239, 68, 68, 0.3) !important;
}

/* ========================================
   TYPING INDICATOR
   ======================================== */
.typing-indicator {
    align-self: flex-start !important;
}

.typing-indicator .intercom-comment-text {
    background: rgba(100, 116, 139, 0.4) !important;
    padding: 14px 18px !important;
}

.typing-dots {
    display: flex !important;
    gap: 5px !important;
    align-items: center !important;
    justify-content: center !important;
}

.typing-dots span {
    width: 8px !important;
    height: 8px !important;
    background: rgba(255, 255, 255, 0.6) !important;
    border-radius: 50% !important;
    animation: typingBounce 1.4s infinite ease-in-out both !important;
    display: inline-block !important;
}

.typing-dots span:nth-child(1) { animation-delay: -0.32s !important; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s !important; }
.typing-dots span:nth-child(3) { animation-delay: 0s !important; }

@keyframes typingBounce {
    0%, 80%, 100% {
        transform: scale(0.6);
        opacity: 0.4;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ========================================
   COMPOSER - Input area
   ======================================== */
.intercom-composer {
    padding: 16px 20px !important;
    background: var(--bbp-dark-bg) !important;
    border-top: 1px solid var(--bbp-border-subtle) !important;
    flex-shrink: 0 !important;
    display: flex !important;
    align-items: center !important;
}

.intercom-composer-input-wrapper {
    display: flex !important;
    align-items: flex-end !important;
    gap: 12px !important;
    background: rgba(100, 116, 139, 0.2) !important;
    border: 2px solid rgba(100, 116, 139, 0.3) !important;
    border-radius: var(--bbp-radius-sm) !important;
    padding: 8px 12px !important;
    transition: all 0.2s ease !important;
    flex: 1 !important;
}

.intercom-composer-input-wrapper:focus-within {
    border-color: var(--bbp-primary) !important;
    background: rgba(100, 116, 139, 0.3) !important;
    box-shadow: 0 0 0 3px rgba(255, 107, 53, 0.15) !important;
}

/* Text Input */
#chat-input.intercom-composer-textarea {
    flex: 1 !important;
    background: transparent !important;
    border: none !important;
    color: var(--bbp-text-light) !important;
    font-size: 14px !important;
    font-family: inherit !important;
    line-height: 1.5 !important;
    resize: none !important;
    outline: none !important;
    min-height: 24px !important;
    max-height: 120px !important;
    padding: 4px 0 !important;
    margin: 0 !important;
    /* CRITICAL: Force horizontal text */
    writing-mode: horizontal-tb !important;
    text-orientation: mixed !important;
    direction: ltr !important;
}

#chat-input.intercom-composer-textarea::placeholder {
    color: var(--bbp-text-muted) !important;
}

#chat-input.intercom-composer-textarea:disabled {
    opacity: 0.5 !important;
    cursor: not-allowed !important;
}

/* Send Button */
#send-message.intercom-composer-send-button {
    width: 36px !important;
    height: 36px !important;
    min-width: 36px !important;
    min-height: 36px !important;
    background: linear-gradient(135deg, var(--bbp-primary) 0%, var(--bbp-secondary) 100%) !important;
    border: none !important;
    border-radius: 10px !important;
    color: var(--bbp-text-light) !important;
    cursor: pointer !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    transition: all 0.2s ease !important;
    flex-shrink: 0 !important;
    padding: 0 !important;
}

#send-message.intercom-composer-send-button:hover {
    transform: scale(1.08) !important;
    box-shadow: 0 4px 12px rgba(255, 107, 53, 0.4) !important;
}

#send-message.intercom-composer-send-button:active {
    transform: scale(0.95) !important;
}

#send-message.intercom-composer-send-button.loading {
    opacity: 0.6 !important;
    pointer-events: none !important;
}

#send-message.intercom-composer-send-button svg {
    width: 16px !important;
    height: 16px !important;
}

/* ========================================
   LAUNCHER BUTTON
   ======================================== */
#intercom-launcher.intercom-launcher {
    position: fixed !important;
    bottom: 20px !important;
    right: 20px !important;
    width: 60px !important;
    height: 60px !important;
    background: linear-gradient(135deg, var(--bbp-primary) 0%, var(--bbp-secondary) 100%) !important;
    border: none !important;
    border-radius: 50% !important;
    cursor: pointer !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    box-shadow: 0 4px 20px rgba(255, 107, 53, 0.4) !important;
    transition: all 0.3s ease !important;
    z-index: 999998 !important;
    padding: 0 !important;
}

#intercom-launcher.intercom-launcher:hover {
    transform: scale(1.1) !important;
    box-shadow: 0 6px 28px rgba(255, 107, 53, 0.5) !important;
}

.intercom-launcher-icon {
    font-size: 26px !important;
    line-height: 1 !important;
}

/* Badge */
.intercom-launcher-badge {
    position: absolute !important;
    top: -4px !important;
    right: -4px !important;
    width: 22px !important;
    height: 22px !important;
    background: #ef4444 !important;
    color: white !important;
    font-size: 12px !important;
    font-weight: 600 !important;
    border-radius: 50% !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    border: 2px solid var(--bbp-dark-bg) !important;
}

/* ========================================
   MOBILE RESPONSIVE - Tablet
   ======================================== */
@media (max-width: 768px) {
    #intercom-frame.intercom-frame {
        bottom: 0 !important;
        right: 0 !important;
        left: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        height: 70vh !important;
        max-height: 70vh !important;
        border-radius: var(--bbp-radius-lg) var(--bbp-radius-lg) 0 0 !important;
    }

    #intercom-launcher.intercom-launcher {
        bottom: 16px !important;
        right: 16px !important;
        width: 56px !important;
        height: 56px !important;
    }

    .intercom-launcher-icon {
        font-size: 24px !important;
    }

    #chat-messages.intercom-conversation {
        padding: 16px !important;
    }

    .intercom-composer {
        padding: 12px 16px !important;
        padding-bottom: max(12px, env(safe-area-inset-bottom)) !important;
    }
}

/* ========================================
   MOBILE RESPONSIVE - Small phones
   ======================================== */
@media (max-width: 480px) {
    #intercom-frame.intercom-frame {
        height: 80vh !important;
        max-height: 80vh !important;
    }

    .intercom-messenger-header {
        padding: 14px 16px !important;
    }

    .intercom-header-title {
        font-size: 15px !important;
    }

    .intercom-header-subtitle {
        font-size: 12px !important;
    }

    .intercom-avatar {
        width: 40px !important;
        height: 40px !important;
    }

    .intercom-comment-text {
        font-size: 14px !important;
        padding: 10px 14px !important;
    }

    .intercom-comment {
        max-width: 90% !important;
    }

    #intercom-launcher.intercom-launcher {
        bottom: 12px !important;
        right: 12px !important;
        width: 52px !important;
        height: 52px !important;
    }
}

/* ========================================
   ACCESSIBILITY
   ======================================== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Focus styles */
#intercom-launcher.intercom-launcher:focus-visible,
#send-message.intercom-composer-send-button:focus-visible,
#close-chat.intercom-close-button:focus-visible {
    outline: 2px solid var(--bbp-primary) !important;
    outline-offset: 2px !important;
}

/* Screen reader only */
.sr-only {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

/* ========================================
   PRINT - Hide chat
   ======================================== */
@media print {
    #intercom-container.intercom-namespace {
        display: none !important;
    }
}
