/* Chat Button */
.addonlab-chat-button {
    position: fixed;
    bottom: 40px;
    right: 60px;
    width: auto;
    height: 70px;
    padding: 0 24px;
    border-radius: 35px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    transition: all 0.3s ease;
    z-index: 1000;
    font-size: 16px;
    font-weight: 600;
}

.addonlab-chat-button svg {
    flex-shrink: 0;
}

.addonlab-chat-button span {
    color: white;
    white-space: nowrap;
}

.addonlab-chat-button:hover {
    box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
    transform: translateY(-2px);
}

.addonlab-chat-button.faded-out {
    opacity: 0;
    pointer-events: none;
    transform: translateY(20px);
}

/* Sparkle Icon Animation */
.addonlab-sparkle-icon {
    animation: sparkle-pulse 2.5s ease-in-out infinite;
}

@keyframes sparkle-pulse {
    0%, 100% {
        opacity: 1;
        transform: scale(1) rotate(0deg);
    }
    25% {
        opacity: 0.8;
        transform: scale(1.2) rotate(-5deg);
    }
    50% {
        opacity: 0.6;
        transform: scale(1.3) rotate(5deg);
        filter: brightness(1.3);
    }
    75% {
        opacity: 0.8;
        transform: scale(1.2) rotate(-5deg);
    }
}

/* Chat Widget */
.addonlab-chat-widget {
    position: fixed;
    bottom: 40px;
    right: 40px;
    width: 650px;
    max-width: calc(100vw - 80px);
    height: 800px;
    max-height: calc(100vh - 80px);
    background: white;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    z-index: 2147483647;
    overflow: hidden;
}

.addonlab-chat-widget.hidden {
    display: none !important;
}

/* Chat Header */
.addonlab-chat-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.addonlab-chat-header-title {
    display: flex;
    align-items: center;
    gap: 10px;
}

.addonlab-chat-header h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: white !important;
}

.addonlab-chat-close {
    background: transparent;
    border: none;
    color: white;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    padding: 0;
    font-size: 24px;
}

.addonlab-chat-close:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.1);
}

/* Chat Messages */
.addonlab-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: #f8f9fa;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.addonlab-chat-messages::-webkit-scrollbar {
    width: 6px;
}

.addonlab-chat-messages::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.addonlab-chat-messages::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 3px;
}

.addonlab-chat-messages::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* Chat Message Wrapper */
.addonlab-chat-message {
    display: flex;
    width: 100%;
}

.addonlab-chat-message.user {
    justify-content: flex-end;
}

.addonlab-chat-message.assistant {
    justify-content: flex-start;
}

/* Chat Bubbles */
.addonlab-chat-bubble {
    max-width: 90%;
    padding: 12px 16px;
    border-radius: 16px;
    word-wrap: break-word;
    line-height: 1.5;
}

.addonlab-chat-bubble.user {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-bottom-right-radius: 4px;
}

.addonlab-chat-bubble.assistant {
    background: #ffffff;
    border: 1px solid #e0e0e0;
    border-bottom-left-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    color: #333;
}

/* Bubble Content Formatting */
.addonlab-chat-bubble p {
    margin: 0 0 8px 0;
    line-height: 1.6;
}

.addonlab-chat-bubble p:last-child {
    margin-bottom: 0;
}

.addonlab-chat-bubble strong {
    font-weight: 600;
}

.addonlab-chat-bubble.user strong {
    color: white;
}

.addonlab-chat-bubble.assistant strong {
    color: #333;
}

.addonlab-chat-bubble h3 {
    font-size: 16px;
    margin: 12px 0 8px 0;
    font-weight: 600;
}

.addonlab-chat-bubble ul {
    margin: 8px 0;
    padding-left: 20px;
    list-style: none;
}

.addonlab-chat-bubble li {
    margin: 6px 0;
    line-height: 1.6;
    position: relative;
    padding-left: 0;
}

.addonlab-chat-bubble li:before {
    content: "•";
    position: absolute;
    left: -15px;
    font-weight: bold;
}

.addonlab-chat-bubble a {
    color: #667eea;
    text-decoration: underline;
    font-weight: 500;
}

.addonlab-chat-bubble.user a {
    color: white;
}

.addonlab-chat-bubble a:hover {
    opacity: 0.8;
}

/* Product Links Formatting */
.addonlab-product-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    background: rgba(102, 126, 234, 0.1);
    border-radius: 6px;
    margin: 2px 4px;
    text-decoration: none;
    transition: all 0.2s;
}

.addonlab-chat-bubble.user .addonlab-product-link {
    background: rgba(255, 255, 255, 0.2);
}

.addonlab-product-link:hover {
    background: rgba(102, 126, 234, 0.2);
    transform: translateX(2px);
}

.addonlab-chat-bubble.user .addonlab-product-link:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Chat Form */
.addonlab-chat-form {
    padding: 16px;
    border-top: 1px solid #e5e7eb;
    display: flex;
    gap: 8px;
    background: white;
}

.addonlab-chat-input {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid #e5e7eb;
    border-radius: 24px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
    font-family: inherit;
}

.addonlab-chat-input:focus {
    border-color: #667eea;
}

.addonlab-chat-send {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white !important;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.addonlab-chat-send svg {
    color: white !important;
    stroke: white !important;
}

.addonlab-chat-send:hover {
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
    transform: scale(1.05);
}

.addonlab-chat-send:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Typing Indicator */
.addonlab-typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
}

.addonlab-typing-dot {
    width: 8px;
    height: 8px;
    background: #94a3b8;
    border-radius: 50%;
    animation: typing-bounce 1.4s infinite;
}

.addonlab-typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.addonlab-typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing-bounce {
    0%, 60%, 100% {
        transform: translateY(0);
    }
    30% {
        transform: translateY(-10px);
    }
}

/* Mobile Responsive */
@media (max-width: 768px) {
    .addonlab-chat-button {
        bottom: 20px;
        right: 20px;
    }

    .addonlab-chat-widget {
        bottom: 20px;
        right: 20px;
        left: 20px;
        width: auto;
        max-width: 100%;
        max-height: calc(100vh - 40px);
    }
}

/* Mobile backdrop overlay - only shows on mobile */
.addonlab-chat-backdrop {
    display: none;
}

@media (max-width: 480px) {
    /* Body scroll lock when chat is open - mobile only */
    body.addonlab-chat-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 100%;
    }

    /* Backdrop overlay - mobile only */
    .addonlab-chat-backdrop {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 2147483646;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
    }

    .addonlab-chat-backdrop.visible {
        opacity: 1;
        visibility: visible;
    }

    .addonlab-chat-button {
        width: auto;
        height: 50px;
        padding: 0 16px;
        bottom: 10px;
        right: 10px;
        font-size: 14px;
    }

    /* Full-screen modal on mobile */
    .addonlab-chat-widget {
        position: fixed !important;
        top: auto !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        /* Use CSS custom property for dynamic height (keyboard handling), fallback to 100% */
        height: var(--addonlab-chat-height, 100%) !important;
        max-height: 100% !important;
        border-radius: 0 !important;
        transform: none !important;
    }

    /* When keyboard is NOT open, fill the screen from top */
    .addonlab-chat-widget:not(.keyboard-open) {
        top: 0 !important;
        bottom: 0 !important;
        height: 100% !important;
        /* Safe area for notched devices */
        padding-top: env(safe-area-inset-top, 0);
    }

    /* When keyboard IS open, anchor to bottom and shrink */
    .addonlab-chat-widget.keyboard-open {
        top: auto !important;
        bottom: 0 !important;
        padding-top: 0;
    }

    .addonlab-chat-header {
        padding: 12px 16px;
        flex-shrink: 0;
    }

    .addonlab-chat-widget:not(.keyboard-open) .addonlab-chat-header {
        padding-top: calc(12px + env(safe-area-inset-top, 0));
    }

    .addonlab-chat-header h3 {
        font-size: 16px;
    }

    .addonlab-chat-close {
        width: 36px;
        height: 36px;
    }

    .addonlab-chat-messages {
        padding: 16px;
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    .addonlab-chat-form {
        flex-shrink: 0;
        padding: 12px 16px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0));
        background: white;
        position: relative;
        z-index: 1;
    }

    /* When keyboard is open, remove bottom safe area padding */
    .addonlab-chat-widget.keyboard-open .addonlab-chat-form {
        padding-bottom: 12px;
    }

    .addonlab-chat-bubble {
        font-size: 14px;
    }

    .addonlab-chat-input {
        font-size: 16px; /* Prevents iOS zoom on focus */
    }
}
