/**
 * Chatbot Widget Styles
 * Global Attaché Branding - Premium Corporate Design
 * Fully Responsive & Adaptive
 */

/* Widget Container - variable definitions */
:root {
    --cb-primary: #7D0C0C;
    --cb-primary-gradient: linear-gradient(135deg, #7D0C0C 0%, #5A0909 100%);
    --cb-primary-hover: linear-gradient(135deg, #8D1C1C 0%, #7D0C0C 100%);
    --cb-bg-light: #F5F5F7;
    --cb-white: #ffffff;
    --cb-text: #1C1C1E;
    --cb-text-light: rgba(28, 28, 30, 0.6);
    --cb-border: rgba(22, 32, 45, 0.08);
    --cb-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    --cb-shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
    --cb-z-index: 9999997; /* Below WhatsApp button (9999998) since WhatsApp is at bottom */
    --cb-safe-bottom: env(safe-area-inset-bottom, 20px);
}

/* Container */
.chatbot-widget-container {
    position: fixed;
    bottom: 96px; /* Above WhatsApp button: 20px (WhatsApp bottom) + 56px (WhatsApp height) + 20px (gap) = 96px */
    right: 20px;
    z-index: var(--cb-z-index);
    font-family: 'Inter', 'Lato', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    pointer-events: none; /* Allow clicking through the empty area */
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 16px;
}

/* Reset all internal elements */
.chatbot-widget-container * {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

.chatbot-widget-container > * {
    pointer-events: auto;
}

/* Chat Button */
.chatbot-button {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: var(--cb-primary-gradient);
    border: none;
    color: white;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(125, 12, 12, 0.3), 0 2px 4px rgba(125, 12, 12, 0.2);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    z-index: 2;
}

.chatbot-button svg {
    width: 32px;
    height: 32px;
    transition: transform 0.3s ease;
}

.chatbot-button:hover {
    transform: scale(1.05);
    background: var(--cb-primary-hover);
    box-shadow: 0 8px 24px rgba(125, 12, 12, 0.4), 0 4px 8px rgba(125, 12, 12, 0.3);
}

.chatbot-button:active {
    transform: scale(0.95);
}

.chatbot-panel.open + .chatbot-button svg {
    transform: rotate(90deg); /* Rotate icon when open if needed */
}

/* Chat Panel */
.chatbot-panel {
    position: absolute;
    bottom: 80px;
    right: 0;
    width: 360px;
    height: min(600px, calc(80vh - 100px));
    background: var(--cb-white);
    border-radius: 16px;
    box-shadow: var(--cb-shadow);
    display: flex;
    flex-direction: column;
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px) scale(0.95);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: hidden;
    border: 1px solid var(--cb-border);
    transform-origin: bottom right;
}

.chatbot-panel.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

/* Header */
.chatbot-header {
    background: var(--cb-primary-gradient);
    color: white;
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.chatbot-header-content {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chatbot-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid rgba(255, 255, 255, 0.3);
    background: white;
}

.chatbot-header-text {
    display: flex;
    flex-direction: column;
}

.chatbot-title {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    font-family: 'Playfair Display', serif;
}

.chatbot-status {
    font-size: 12px;
    opacity: 0.9;
}

.chatbot-close {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 32px;
    height: 32px;
    min-width: 32px;
    min-height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
    position: relative;
}

.chatbot-close svg {
    width: 20px;
    height: 20px;
    stroke: white;
    stroke-width: 2.5;
    color: white;
    fill: none;
    display: block;
    flex-shrink: 0;
}

.chatbot-close:hover {
    background: rgba(255, 255, 255, 0.35);
}

.chatbot-close:active {
    background: rgba(255, 255, 255, 0.4);
}

/* Messages Area */
.chatbot-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--cb-bg-light);
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

.chatbot-welcome {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    font-size: 14px;
    padding: 16px;
    background: white;
    border-radius: 12px;
    border: 1px solid var(--cb-border);
    color: var(--cb-text);
    line-height: 1.5;
}

.chatbot-welcome p {
    margin: 0;
}

/* Action Buttons Container */
.chatbot-action-buttons {
    display: block;
    padding: 0;
    width: 100%;
}

.chatbot-action-buttons-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
    padding: 0 4px;
}

.chatbot-action-buttons-title {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: var(--cb-text);
    font-family: var(--font-body);
}

.chatbot-action-buttons-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    width: 100%;
}

/* Action Button */
.chatbot-action-button {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 16px 20px;
    background: white;
    border: 2px solid var(--cb-border);
    border-radius: 12px;
    color: var(--cb-text);
    font-size: 15px;
    font-weight: 500;
    font-family: var(--font-body);
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    text-align: left;
    box-shadow: var(--cb-shadow-sm);
    position: relative;
    overflow: hidden;
}

.chatbot-action-button::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--cb-primary-gradient);
    opacity: 0;
    transition: opacity 0.3s ease;
    z-index: 0;
}

.chatbot-action-button > * {
    position: relative;
    z-index: 1;
}

.chatbot-action-button svg {
    width: 20px;
    height: 20px;
    min-width: 20px;
    flex-shrink: 0;
    stroke: currentColor;
    stroke-width: 2.5;
    transition: transform 0.3s ease;
}

.chatbot-action-button span {
    flex: 1;
    line-height: 1.4;
}

.chatbot-action-button:hover {
    border-color: var(--cb-primary);
    color: white;
    transform: translateY(-2px);
    box-shadow: var(--cb-shadow);
}

.chatbot-action-button:hover::before {
    opacity: 1;
}

.chatbot-action-button:hover svg {
    transform: translateX(4px);
    stroke: white;
}

.chatbot-action-button:active {
    transform: translateY(0);
    box-shadow: var(--cb-shadow-sm);
}

/* Special styling for "Ask a Question" button */
.chatbot-action-button-chat {
    background: var(--cb-primary-gradient);
    color: white;
    border-color: var(--cb-primary);
    margin-top: 8px;
}

.chatbot-action-button-chat::before {
    background: var(--cb-primary-hover);
}

.chatbot-action-button-chat svg {
    stroke: white;
}

.chatbot-action-button-chat:hover {
    background: var(--cb-primary-hover);
    border-color: var(--cb-primary);
    color: white;
}

.chatbot-action-button-chat:hover svg {
    stroke: white;
}

/* Individual Messages */
.chatbot-message {
    display: flex;
    max-width: 85%;
    animation: messageSlideIn 0.3s ease forwards;
}

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

.chatbot-message-user {
    align-self: flex-end;
}

.chatbot-message-assistant {
    align-self: flex-start;
}

.chatbot-message-wrapper {
    display: flex;
    align-items: flex-start;
    gap: 8px;
}

.chatbot-message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.chatbot-text {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
}

.chatbot-message-user .chatbot-text {
    background: var(--cb-primary-gradient);
    color: white;
    border-bottom-right-radius: 4px;
    box-shadow: var(--cb-shadow-sm);
}

.chatbot-message-assistant .chatbot-text {
    background: white;
    color: var(--cb-text);
    border-bottom-left-radius: 4px;
    box-shadow: var(--cb-shadow-sm);
    border: 1px solid var(--cb-border);
}

/* Media & Loading */
.chatbot-media img {
    max-width: 100%;
    border-radius: 12px;
    display: block;
}

.chatbot-voice {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background: var(--cb-primary-gradient);
    color: white;
    border-radius: 12px;
}

.chatbot-loading span {
    display: inline-block;
    width: 6px;
    height: 6px;
    background: #7D0C0C;
    border-radius: 50%;
    margin: 0 2px;
    animation: loadingBounce 1.4s infinite ease-in-out;
}

.chatbot-loading span:nth-child(1) { animation-delay: -0.32s; }
.chatbot-loading span:nth-child(2) { animation-delay: -0.16s; }

@keyframes loadingBounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* Input Area */
.chatbot-input-area {
    padding: 12px 16px;
    background: white;
    border-top: 1px solid var(--cb-border);
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.chatbot-input-wrapper {
    flex: 1;
    display: flex;
    align-items: center;
    background: var(--cb-bg-light);
    border-radius: 24px;
    padding: 8px 12px;
    border: 1px solid var(--cb-border);
    min-height: 44px;
}

.chatbot-input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    font-size: 15px; /* 16px prevents zoom on iOS, but 15 is okay if we handle meta */
    color: var(--cb-text);
    min-width: 0;
}

.chatbot-input::placeholder {
    color: var(--cb-text-light);
}

.chatbot-input-actions {
    display: flex;
    gap: 4px;
}

.chatbot-action-btn {
    width: 32px;
    height: 32px;
    min-width: 32px;
    min-height: 32px;
    border: none;
    background: transparent;
    color: var(--cb-text-light);
    cursor: pointer;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
    padding: 0;
    box-sizing: border-box;
}

.chatbot-action-btn svg {
    width: 20px;
    height: 20px;
    min-width: 20px;
    min-height: 20px;
    max-width: 20px;
    max-height: 20px;
    stroke: currentColor;
    stroke-width: 2.5;
    fill: none;
    display: block;
    flex-shrink: 0;
    color: inherit;
}

.chatbot-action-btn:hover {
    background: rgba(125, 12, 12, 0.1);
    color: var(--cb-primary);
}

.chatbot-action-btn:hover svg {
    stroke: var(--cb-primary);
    color: var(--cb-primary);
}

.chatbot-action-btn.recording {
    color: var(--cb-primary);
    background: rgba(125, 12, 12, 0.1);
    animation: pulse 1.5s infinite;
}

.chatbot-action-btn.recording svg {
    stroke: var(--cb-primary);
    color: var(--cb-primary);
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.chatbot-send {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    border: none;
    background: var(--cb-primary-gradient);
    color: white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
    box-shadow: var(--cb-shadow-sm);
    padding: 0;
    box-sizing: border-box;
    overflow: visible;
}

.chatbot-send:hover {
    transform: scale(1.05);
    box-shadow: var(--cb-shadow);
}

.chatbot-send-icon {
    width: 20px;
    height: 20px;
    min-width: 20px;
    min-height: 20px;
    max-width: 20px;
    max-height: 20px;
    stroke: white;
    color: white;
    display: block;
    flex-shrink: 0;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* ==========================================
   RESPONSIVENESS & MOBILE ADAPTATIONS
   ========================================== */

/* Mobile Devices (Landscape & Portrait) */
@media screen and (max-width: 768px) {
    .chatbot-widget-container {
        bottom: 0;
        right: 0;
        left: 0;
        padding: 0;
        z-index: 9999999;
        align-items: center; /* Center the button if panel closed */
        pointer-events: none;
        max-width: 100vw; /* Reset default desktop max-width */
    }

    .chatbot-button {
        position: fixed !important;
        bottom: 92px !important; /* Above WhatsApp: 20px (WhatsApp bottom) + 52px (WhatsApp height mobile) + 20px (gap) = 92px */
        right: 15px !important;
        width: 52px !important; /* Match WhatsApp button size on mobile */
        height: 52px !important; /* Match WhatsApp button size on mobile */
        pointer-events: auto;
        z-index: 9999999; /* Below WhatsApp button (WhatsApp is at bottom) */
    }
    
    .chatbot-button svg {
        width: 28px !important; /* Larger icon for 52px button */
        height: 28px !important; /* Larger icon for 52px button */
    }

    /* Full Screen Panel on Mobile */
    .chatbot-panel {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100dvh; /* Use dynamic viewport height */
        max-height: none;
        border-radius: 0;
        transform-origin: center bottom;
        display: flex;
        flex-direction: column;
        z-index: 10000000; /* High z-index for modal */
        overflow: hidden; /* Prevent horizontal scroll */
    }

    /* Animation for mobile: Slide up */
    .chatbot-panel {
        transform: translateY(100%);
        opacity: 0;
    }

    .chatbot-panel.open {
        transform: translateY(0);
        opacity: 1;
    }

    .chatbot-header {
        padding-top: max(16px, env(safe-area-inset-top));
        padding-bottom: 16px;
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
    }

    .chatbot-messages {
        padding: 16px;
        padding-bottom: 20px;
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
    }

    /* Mobile action buttons */
    .chatbot-action-buttons-grid {
        gap: 10px;
    }

    .chatbot-action-button {
        padding: 14px 18px;
        font-size: 14px;
    }

    .chatbot-action-button svg {
        width: 18px;
        height: 18px;
        min-width: 18px;
    }

    .chatbot-action-buttons-title {
        font-size: 15px;
    }

    .chatbot-input-area {
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
        width: 100%;
        box-sizing: border-box;
    }

    /* Ensure input wrapper doesn't overflow */
    .chatbot-input-wrapper {
        min-width: 0; /* Critical for flex items inside flex container */
    }

    /* Adjust font sizes for readability */
    .chatbot-input {
        font-size: 16px; /* Prevents zoom on iOS */
    }

    /* Mobile-specific action button enhancements */
    .chatbot-action-btn {
        width: 36px;
        height: 36px;
        min-width: 36px;
        min-height: 36px;
        color: rgba(28, 28, 30, 0.8); /* Darker color for better visibility on mobile */
    }

    .chatbot-action-btn svg {
        width: 22px;
        height: 22px;
        min-width: 22px;
        min-height: 22px;
        max-width: 22px;
        max-height: 22px;
        stroke-width: 3;
        stroke: rgba(28, 28, 30, 0.8); /* Explicit stroke color for mobile */
        color: rgba(28, 28, 30, 0.8);
    }

    .chatbot-button {
        /* Hide button when panel is open on mobile */
        transition: opacity 0.2s, transform 0.2s;
    }
    
    .chatbot-panel.open ~ .chatbot-button {
        opacity: 0;
        pointer-events: none;
        transform: scale(0.8);
    }
}

/* Very Small Mobile - Align chatbot button with WhatsApp */
@media screen and (max-width: 480px) {
    .chatbot-button {
        right: 10px !important; /* Match WhatsApp button alignment on small screens */
        width: 48px !important; /* Match WhatsApp button size (48px on 361px-480px) */
        height: 48px !important; /* Match WhatsApp button size (48px on 361px-480px) */
        bottom: 88px !important; /* Above WhatsApp: 20px (WhatsApp bottom) + 48px (WhatsApp height) + 20px (gap) = 88px */
    }
    
    .chatbot-button svg {
        width: 26px !important; /* Larger icon for 48px button */
        height: 26px !important; /* Larger icon for 48px button */
    }
}

/* Fix for very small screens (iPhone SE etc) */
@media screen and (max-width: 360px) {
    .chatbot-header-text .chatbot-title {
        font-size: 14px;
    }
    .chatbot-avatar {
        width: 32px;
        height: 32px;
    }
    .chatbot-close {
        width: 36px;
        height: 36px;
        min-width: 36px;
        min-height: 36px;
        background: rgba(255, 255, 255, 0.25);
    }
    .chatbot-close svg {
        width: 22px;
        height: 22px;
        stroke-width: 3;
    }
    .chatbot-button {
        right: 10px !important; /* Match WhatsApp button alignment on very small screens */
        width: 44px !important; /* Match WhatsApp button size (44px on max-width 360px) */
        height: 44px !important; /* Match WhatsApp button size (44px on max-width 360px) */
        bottom: 84px !important; /* Above WhatsApp: 20px (WhatsApp bottom) + 44px (WhatsApp height) + 20px (gap) = 84px */
    }
    
    .chatbot-button svg {
        width: 24px !important; /* Larger icon for 44px button */
        height: 24px !important; /* Larger icon for 44px button */
    }

    /* Very small screens - action buttons */
    .chatbot-action-button {
        padding: 12px 16px;
        font-size: 13px;
    }

    .chatbot-action-buttons-title {
        font-size: 14px;
    }
}

/* Tablet (768px and up) - Position chatbot above WhatsApp */
@media (min-width: 768px) {
    .chatbot-widget-container {
        bottom: 100px; /* Above WhatsApp: 20px (WhatsApp bottom) + 60px (WhatsApp height tablet) + 20px (gap) = 100px */
    }
}

/* Desktop (992px and up) - Position chatbot above WhatsApp */
@media (min-width: 992px) {
    .chatbot-widget-container {
        bottom: 104px; /* Above WhatsApp: 20px (WhatsApp bottom) + 64px (WhatsApp height desktop) + 20px (gap) = 104px */
    }
}

/* Mobile-specific close button enhancements */
@media screen and (max-width: 768px) {
    .chatbot-close {
        width: 36px;
        height: 36px;
        min-width: 36px;
        min-height: 36px;
        background: rgba(255, 255, 255, 0.25);
    }
    .chatbot-close svg {
        width: 22px;
        height: 22px;
        stroke-width: 3;
    }
}
