/* ========== ENHANCED CHATBOT UI/UX ========== */

/* ========== PERFORMANCE OPTIMIZATIONS ========== */
/* Force GPU acceleration for smoother animations */
.message, #chatInterface, .chat-container, .chat-messages, .message-content,
.typing-indicator, .dot, .message-text, #sendMessage, .floating-chat-btn {
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -webkit-transform-style: preserve-3d;
    transform-style: preserve-3d;
    transform: translateZ(0);
    will-change: transform, opacity;
}

/* Improve performance and prevent overflow */
body.has-chat-open {
    overflow: hidden;
    overscroll-behavior: none;
}

/* ========== CHAT INTERFACE CONTAINER ========== */
#chatInterface {
    max-width: 480px;
    width: 100%;
    min-width: 320px;
    border-radius: 1.5rem 1.5rem 0 0;
    transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
    transform-origin: bottom right;
    box-shadow: 0 8px 40px 0 rgba(255, 109, 0, 0.3), 0 2px 12px 0 rgba(255, 109, 0, 0.3);
    overflow: hidden;
    position: fixed;
    height: auto;
    max-height: 80vh;
    z-index: 999;
}

#chatInterface.hidden {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
    transition: all 0.3s ease-out;
    visibility: hidden;
}

/* ========== CHAT CONTAINER ========== */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-height: 650px;
    overflow: hidden;
    background: rgba(26, 26, 26, 0.95);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    box-shadow: inset 0 0 0 1px rgba(255, 109, 0, 0.3);
    transition: all 0.3s ease;
    position: relative;
}

/* ========== CHAT MESSAGES AREA ========== */
.chat-messages {
    flex: 1;
    height: 420px;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 1.25rem;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    scroll-padding-bottom: 12px;
    contain: content;
    position: relative;
    will-change: scroll-position;
    transform: translateZ(0);
    backface-visibility: hidden;
    min-height: 300px;
    max-height: calc(100vh - 200px);
    padding-bottom: 80px;
}

/* Prevent elastic scroll issues on iOS */
.chat-messages::before,
.chat-messages::after {
    content: '';
    display: block;
    height: 0.5px;
    width: 100%;
    visibility: hidden;
}

/* Ensure last message is visible */
.chat-messages::after {
    height: 20px;
}

/* ========== MESSAGE COMPONENTS ========== */
.message {
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.3s ease, transform 0.3s ease;
    display: flex;
    margin-bottom: 16px;
    align-items: flex-start;
    position: relative;
    contain: content layout;
}

.message.visible {
    opacity: 1;
    transform: translateY(0);
}

/* Staggered animation delays */
.message:nth-child(2n) { transition-delay: 0.05s; }
.message:nth-child(3n) { transition-delay: 0.1s; }

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.message-content {
    padding: 0.875rem 1.125rem;
    border-radius: 1rem;
    max-width: 85%;
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
    line-height: 1.6;
    font-size: 1rem;
    margin-bottom: 4px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s ease, opacity 0.2s ease;
    contain: content;
}

.bot-content {
    background-color: rgba(39, 39, 42, 0.9);
    border: 1px solid rgba(255, 109, 0, 0.2);
    border-bottom-left-radius: 0.25rem;
}

.user-content {
    background: linear-gradient(135deg, rgba(255, 109, 0, 0.15), rgba(255, 171, 64, 0.15));
    border: 1px solid rgba(255, 109, 0, 0.2);
    border-bottom-right-radius: 0.25rem;
}

.user-message {
    justify-content: flex-end;
}

.message-text {
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.6;
    font-size: 1rem;
    color: #ffffff;
    opacity: 0.95;
    transition: opacity 0.3s ease;
    min-height: 1.2em;
}

.message-time {
    font-size: 0.75rem;
    opacity: 0.7;
    margin-top: 4px;
}

/* ========== TYPING INDICATOR ========== */
.typing-indicator {
    margin-bottom: 0.5rem;
    padding: 8px 12px;
    background: rgba(30, 30, 30, 0.6);
    border: 1px solid rgba(255, 109, 0, 0.2);
    border-radius: 1rem;
    display: inline-flex;
    align-items: center;
    max-width: 80px;
    min-width: 60px;
    height: 36px;
}

.typing-dots {
    display: flex;
    align-items: center;
    justify-content: center;
    column-gap: 5px;
    height: 100%;
}

.dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: #ff6d00;
    opacity: 0.6;
    animation: dotPulseImproved 1.4s infinite ease-in-out both;
}

.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }
.dot:nth-child(3) { animation-delay: 0s; }

/* ========== CHAT INPUT AREA ========== */
.chat-input {
    display: flex;
    gap: 10px;
    position: sticky;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(26, 26, 26, 0.95);
    padding: 16px;
    border-top: 1px solid rgba(255, 109, 0, 0.2);
    z-index: 20;
    min-height: 64px;
    max-height: 120px;
    transition: all 0.3s ease;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
    visibility: visible;
}

#chatInput {
    flex: 1;
    padding: 12px 16px;
    background: rgba(45, 45, 45, 0.8);
    border: 1px solid rgba(255, 109, 0, 0.3);
    border-radius: 16px;
    color: white;
    font-size: 16px;
    font-family: 'Exo 2', sans-serif;
    resize: none;
    outline: none;
    transition: all 0.3s ease;
    min-height: 48px;
    max-height: 120px;
    overflow-y: auto;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
    position: relative;
    z-index: 5;
    visibility: visible;
    opacity: 1;
    pointer-events: auto;
    display: block;
    width: calc(100% - 60px);
}

#chatInput:focus {
    border-color: rgba(255, 109, 0, 0.8);
    background: rgba(55, 55, 55, 0.9);
    box-shadow: 0 0 0 2px rgba(255, 109, 0, 0.2);
}

#sendMessage {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(to bottom right, #ff6d00, #ff9500);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 6px rgba(255, 109, 0, 0.3);
    position: relative;
    z-index: 5;
    flex-shrink: 0;
    visibility: visible;
    opacity: 1;
}

#sendMessage:hover:not([disabled]) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(255, 109, 0, 0.5);
}

#sendMessage:disabled {
    background: #555;
    cursor: not-allowed;
    opacity: 0.5;
}

/* ========== FLOATING CHAT BUTTON ========== */
.floating-chat-btn {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 64px;
    height: 64px;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    will-change: transform;
    z-index: 1000;
    box-shadow: 0 4px 16px rgba(255, 109, 0, 0.5);
}

.floating-chat-btn:hover {
    transform: translateY(-5px) scale(1.05);
}

.floating-chat-btn-shifted {
    transform: scale(0.85);
    opacity: 0.85;
    bottom: 30px;
    right: 30px;
}

/* ========== QUICK REPLIES ========== */
.quick-replies {
    margin-top: 12px;
    margin-bottom: 12px;
    width: 100%;
}

.quick-replies-container {
    background: rgba(30, 30, 30, 0.4);
    border-radius: 12px;
    padding: 12px;
    border: 1px solid rgba(255, 109, 0, 0.2);
}

.quick-replies-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
}

.quick-reply-btn {
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275),
                background-color 0.2s ease,
                border-color 0.2s ease;
    will-change: transform;
    margin: 6px 6px 6px 0;
    padding: 8px 16px;
    border-radius: 20px;
    background: rgba(255, 109, 0, 0.15);
    border: 1px solid rgba(255, 109, 0, 0.3);
    color: #ffffff;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
}

.quick-reply-btn:hover {
    transform: translateY(-2px) scale(1.03);
    background: rgba(255, 109, 0, 0.25);
    border-color: rgba(255, 109, 0, 0.5);
}

.quick-reply-btn:active {
    transform: translateY(1px) scale(0.98);
}

/* ========== FEEDBACK AND CONTACT ========== */
.feedback-box {
    margin-top: 10px;
    opacity: 0;
    transform: translateY(5px);
    animation: fadeInUp 0.5s forwards;
    animation-delay: 0.5s;
}

.feedback-btn {
    background: rgba(30, 30, 30, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #888;
    transition: all 0.2s ease;
}

.feedback-btn:hover {
    background: rgba(255, 109, 0, 0.2);
    color: #ff6d00;
    border-color: rgba(255, 109, 0, 0.4);
    transform: translateY(-2px);
}

.contact-link {
    display: inline-flex;
    align-items: center;
    padding: 8px 16px;
    border-radius: 20px;
    background: linear-gradient(135deg, #ff6d00, #ffab40);
    color: white;
    font-weight: bold;
    text-decoration: none;
    transition: all 0.2s ease;
    box-shadow: 0 4px 12px rgba(255, 109, 0, 0.3);
}

.contact-link:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(255, 109, 0, 0.4);
}

/* ========== AVATAR EFFECTS ========== */
.bot-avatar {
    position: relative;
}

.bot-avatar::after {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: rgba(255, 109, 0, 0.4);
    z-index: -1;
    animation: pulse 2s infinite;
    opacity: 0;
}

/* ========== SCROLLBAR CUSTOMIZATION ========== */
.custom-scrollbar::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

.custom-scrollbar::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    margin: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
    background: linear-gradient(135deg, #ff6d00, #ffab40);
    border-radius: 8px;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(135deg, #ff8f00, #ffc107);
}

/* ========== ANIMATIONS ========== */
@keyframes messageAppearImproved {
    0% { 
        opacity: 0;
        transform: translateY(8px);
    }
    100% { 
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes dotPulseImproved {
    0%, 80%, 100% { 
        transform: scale(0.8);
        opacity: 0.6;
    }
    40% { 
        transform: scale(1.2);
        opacity: 1;
        box-shadow: 0 0 6px rgba(255, 109, 0, 0.6);
    }
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes pulse {
    0% {
        transform: scale(0.95);
        opacity: 0;
    }
    50% {
        transform: scale(1.2);
        opacity: 0.3;
    }
    100% {
        transform: scale(0.95);
        opacity: 0;
    }
}

/* ========== TYPING EFFECT STABILIZATION ========== */
.message-text.typing-effect {
    min-height: 24px;
    position: relative;
    width: 100%;
    will-change: contents;
    transition: height 0.3s ease;
    display: block;
    contain: content;
    opacity: 1;
}

.message-text.typing-effect::before {
    content: "";
    display: block;
    height: 4px;
    visibility: hidden;
    pointer-events: none;
}

/* ========== MOBILE RESPONSIVENESS ========== */
@media (max-width: 600px) {
    #chatInterface {
        max-width: 100%;
        width: 100%;
        right: 0;
        left: 0;
        bottom: 0;
        border-radius: 1.5rem 1.5rem 0 0;
        margin: 0;
        max-height: 85vh;
    }
    
    .chat-container {
        border-radius: 1.5rem 1.5rem 0 0;
        max-height: 85vh;
        height: auto;
    }
    
    .chat-messages {
        height: calc(65vh - 120px);
        padding: 12px;
        padding-bottom: 24px;
        max-height: calc(100vh - 170px);
    }

    .message-content {
        max-width: 90%;
        padding: 10px 14px;
    }
    
    .floating-chat-btn {
        bottom: 20px;
        right: 20px;
        width: 56px;
        height: 56px;
    }
    
    .floating-chat-btn i {
        font-size: 1.5rem;
    }
    
    #chatInput {
        font-size: 16px;
        padding: 10px 14px;
        min-height: 44px;
    }

    .chat-input {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        padding: 12px;
        background: rgba(26, 26, 26, 0.95);
        z-index: 20;
    }
}

/* Full screen mobile chat */
@media screen and (max-width: 600px) {
    body.has-chat-open {
        position: fixed;
        width: 100%;
        height: 100%;
        overflow: hidden;
    }
    
    #chatInterface:not(.hidden) {
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 100%;
        max-width: none;
        max-height: none;
        border-radius: 0;
        transform: none;
    }
    
    #chatInterface:not(.hidden) .chat-container {
        border-radius: 0;
        height: 100%;
    }
}

/* ========== MOBILE SCROLLBAR OPTIMIZATION ========== */
@media (max-width: 768px) and (pointer: coarse) {
    .chat-messages {
        scrollbar-width: none;
        -ms-overflow-style: none;
    }
    
    .chat-messages::-webkit-scrollbar {
        display: none;
        width: 0px;
        background: transparent;
    }
    
    #chatInput {
        scrollbar-width: thin;
    }
}

/* ========== FALLBACK ANIMATIONS ========== */
@supports not (IntersectionObserver in window) {
    .message {
        animation: messageAppearImproved 0.3s ease forwards;
    }
}

/* ========== Z-INDEX MANAGEMENT ========== */
#main-header {
    z-index: 1000;
}

/* ========== PERFORMANCE OPTIMIZATION CLASSES ========== */
.chat-transition-in-progress {
    overflow: hidden;
}

.chat-resizing .chat-messages,
.chat-resizing .chat-container,
.chat-resizing .chat-input {
    transition: none;
}
