/* Industry-Standard Mobile Chat CSS - KEYBOARD FIX VERSION */

/* Base reset for chat widget */
#ai-chat-widget * {
    box-sizing: border-box;
}

#ai-chat-widget {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 10000;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
}

/* Chat Toggle Button */
.chat-toggle {
    position: relative;
    width: 60px;
    height: 60px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 24px;
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border: none;
    outline: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 12px 35px rgba(102, 126, 234, 0.4);
}

.chat-toggle.active {
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
}

.chat-close-icon {
    display: none;
    position: absolute;
}

.chat-toggle.active .chat-icon {
    display: none;
}

.chat-toggle.active .chat-close-icon {
    display: block;
}

/* Pulse Animation */
.chat-pulse {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: rgba(102, 126, 234, 0.4);
    animation: pulse 2s infinite;
    pointer-events: none;
}

.chat-toggle.active .chat-pulse {
    display: none;
}

@keyframes pulse {
    0% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(1.3);
        opacity: 0.5;
    }
    100% {
        transform: scale(1.6);
        opacity: 0;
    }
}

/* Chat Container - KEYBOARD FIX APPLIED */
.chat-container {
    position: absolute;
    bottom: 80px;
    right: 0;
    width: 380px;
    height: 500px;
    background: white;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
    display: none;
    flex-direction: column;
    overflow: hidden;
    transform: translateY(20px) scale(0.95);
    opacity: 0;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid rgba(0, 0, 0, 0.1);
    
    /* Industry standard constraints */
    max-width: 100vw;
    max-height: 100vh;
    min-width: 280px;
    min-height: 300px;
}

.chat-container.open {
    display: flex;
    transform: translateY(0) scale(1);
    opacity: 1;
}

/* KEYBOARD FIX: Add class when keyboard is detected */
.chat-container.keyboard-open {
    position: fixed !important;
    bottom: 10px !important;
    height: calc(100vh - 120px) !important;
    max-height: calc(100vh - 120px) !important;
}

/* Dark mode support */
.dark-mode .chat-container {
    background: #1a1a1a;
    border: 1px solid #333;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

/* Chat Header */
.chat-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
    border-radius: 20px 20px 0 0;
    min-height: 60px;
}

.chat-avatar {
    width: 36px;
    height: 36px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.chat-info {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.chat-info h4 {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-status {
    font-size: 11px;
    opacity: 0.9;
    margin-top: 2px;
}

.chat-minimize {
    background: none;
    border: none;
    color: white;
    font-size: 16px;
    cursor: pointer;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.chat-minimize:hover {
    background: rgba(255, 255, 255, 0.1);
}

/* Chat Messages - KEYBOARD FIX */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    background: #f8f9fa;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    
    /* Industry standard message container */
    display: flex;
    flex-direction: column;
    gap: 12px;
    
    /* KEYBOARD FIX: Ensure messages don't get cut off */
    min-height: 0;
}

.dark-mode .chat-messages {
    background: #111;
}

/* Message Layout - WhatsApp/Telegram Style */
.message {
    display: flex;
    gap: 8px;
    animation: slideIn 0.3s ease-out;
    width: 100%;
    align-items: flex-end;
}

.message-avatar {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    flex-shrink: 0;
}

.bot-message .message-avatar {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.user-message {
    flex-direction: row-reverse;
}

.user-message .message-avatar {
    background: #e3f2fd;
    color: #1976d2;
}

.dark-mode .user-message .message-avatar {
    background: #1565c0;
    color: white;
}

/* Message Content - Industry Standard Bubble */
.message-content {
    background: white;
    padding: 8px 12px;
    border-radius: 16px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    position: relative;
    
    /* Critical: Proper width constraints like WhatsApp */
    max-width: calc(100% - 40px);
    min-width: 0;
    width: fit-content;
    
    /* Smart text handling - preserve words and links */
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: normal; /* Changed: only break at normal points */
    hyphens: none; /* Changed: disable auto-hyphenation */
}

.dark-mode .message-content {
    background: #222;
    color: #e0e0e0;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.user-message .message-content {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.message-content p {
    margin: 0;
    font-size: 14px;
    line-height: 1.3;
    
    /* Smart text constraints - keep words intact */
    word-break: normal; /* Changed: preserve word integrity */
    overflow-wrap: break-word; /* Only break if absolutely necessary */
}

/* Links - Industry Standard */
.message-content a {
    color: #667eea;
    text-decoration: underline;
    font-weight: 500;
    word-break: break-all; /* Only URLs can break anywhere */
    display: inline-block;
    max-width: 100%;
}

.user-message .message-content a {
    color: rgba(255, 255, 255, 0.9);
}

.dark-mode .message-content a {
    color: #8fa2ff;
}

.message-time {
    font-size: 10px;
    opacity: 0.6;
    margin-top: 4px;
}

/* Suggested Questions */
.suggested-questions {
    margin-top: 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.suggestion-btn {
    background: white;
    border: 1px solid #e0e0e0;
    border-radius: 16px;
    padding: 8px 12px;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.2s;
    text-align: left;
    color: #666;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.dark-mode .suggestion-btn {
    background: #222;
    border-color: #333;
    color: #ccc;
}

.suggestion-btn:hover {
    background: #f5f5f5;
    border-color: #667eea;
    transform: translateY(-1px);
}

.dark-mode .suggestion-btn:hover {
    background: #333;
    border-color: #667eea;
}

/* Chat Input - KEYBOARD FIX APPLIED */
.chat-input-container {
    padding: 16px;
    background: white;
    border-top: 1px solid #e0e0e0;
    flex-shrink: 0;
    border-radius: 0 0 20px 20px;
    
    /* KEYBOARD FIX: Ensure input container stays visible */
    position: relative;
    z-index: 1;
}

.dark-mode .chat-input-container {
    background: #1a1a1a;
    border-top-color: #333;
}

.chat-input-wrapper {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

#chat-input {
    flex: 1;
    border: 1px solid #e0e0e0;
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
    background: #f8f9fa;
    resize: none;
    min-height: 36px;
    max-height: 100px;
    font-family: inherit;
    line-height: 1.4;
    
    /* KEYBOARD FIX: Prevent zoom on iOS */
    font-size: 16px;
    -webkit-appearance: none;
}

.dark-mode #chat-input {
    background: #111;
    border-color: #333;
    color: #e0e0e0;
}

#chat-input:focus {
    border-color: #667eea;
}

#chat-input::placeholder {
    color: #999;
}

#chat-send {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s;
    flex-shrink: 0;
    font-size: 14px;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

#chat-send:hover {
    transform: scale(1.05);
}

#chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Typing Indicator */
.chat-typing {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
    font-size: 11px;
    color: #666;
}

.typing-indicator {
    display: flex;
    gap: 3px;
}

.typing-indicator span {
    width: 5px;
    height: 5px;
    background: #667eea;
    border-radius: 50%;
    animation: typing 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.3;
    }
    30% {
        transform: translateY(-8px);
        opacity: 1;
    }
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* MOBILE RESPONSIVE - KEYBOARD FIX VERSION */
@media (max-width: 480px) {
    #ai-chat-widget {
        bottom: 16px;
        right: 16px;
    }
    
    .chat-toggle {
        width: 56px;
        height: 56px;
        font-size: 22px;
        bottom: 16px;
        right: 16px;
    }
    
    /* KEYBOARD FIX: Mobile fullscreen with keyboard handling */
    .chat-container {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw;
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for keyboard */
        border-radius: 0;
        transform: translateX(100%);
        opacity: 1;
    }
    
    .chat-container.open {
        transform: translateX(0);
    }
    
    /* KEYBOARD FIX: Adjust when keyboard is open */
    .chat-container.keyboard-open {
        height: 100vh !important;
        height: 100dvh !important;
        bottom: 0 !important;
    }
    
    .chat-header {
        border-radius: 0;
        padding: 12px 16px;
        padding-top: max(12px, env(safe-area-inset-top));
    }
    
    .chat-messages {
        padding: 12px;
        /* KEYBOARD FIX: Ensure proper scrolling */
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .message-content {
        max-width: calc(100% - 32px);
        font-size: 14px;
    }
    
    .chat-input-container {
        padding: 12px;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        border-radius: 0;
        
        /* KEYBOARD FIX: Ensure input stays above keyboard */
        position: sticky;
        bottom: 0;
        background: white;
    }
    
    .dark-mode .chat-input-container {
        background: #1a1a1a;
    }
    
    /* KEYBOARD FIX: Larger touch target for mobile */
    #chat-input {
        font-size: 16px; /* Prevents zoom on iOS */
        min-height: 40px;
        padding: 10px 16px;
    }
    
    #chat-send {
        width: 40px;
        height: 40px;
        font-size: 16px;
    }
}

@media (min-width: 481px) and (max-width: 768px) {
    .chat-container {
        position: absolute;
        bottom: 80px;
        right: 0;
        width: 360px;
        height: 480px;
        max-width: calc(100vw - 40px);
        max-height: calc(100vh - 120px);
        border-radius: 20px;
        transform: translateY(20px) scale(0.95);
        opacity: 0;
    }
    
    .chat-container.open {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
    
    /* KEYBOARD FIX: Tablet keyboard handling */
    .chat-container.keyboard-open {
        position: fixed !important;
        bottom: 10px !important;
        height: calc(100vh - 120px) !important;
        max-height: calc(100vh - 120px) !important;
    }
}

/* Small phones - KEYBOARD FIX */
@media (max-width: 360px) {
    .message-content {
        max-width: calc(100% - 28px);
        font-size: 13px;
        padding: 6px 10px;
    }
    
    .chat-messages {
        padding: 8px;
    }
    
    .chat-input-container {
        padding: 8px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }
    
    #chat-input {
        font-size: 16px; /* Critical: Prevents zoom */
        min-height: 38px;
    }
}

/* KEYBOARD FIX: Landscape orientation on mobile */
@media (max-width: 850px) and (orientation: landscape) {
    .chat-container {
        height: 100vh !important;
        height: 100dvh !important;
    }
    
    .chat-container.keyboard-open {
        height: calc(100vh - 60px) !important;
        height: calc(100dvh - 60px) !important;
    }
    
    .chat-messages {
        max-height: calc(100vh - 180px);
        max-height: calc(100dvh - 180px);
    }
}

/* Scrollbar */
.chat-messages::-webkit-scrollbar {
    width: 3px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

.dark-mode .chat-messages::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
}

/* KEYBOARD FIX: Accessibility improvements */
@media (prefers-reduced-motion: reduce) {
    .chat-container {
        transition: none;
    }
    
    .message {
        animation: none;
    }
}

/* KEYBOARD FIX: Support for fold phones */
@media (max-width: 480px) and (max-height: 600px) {
    .chat-container.keyboard-open {
        height: calc(100vh - 40px) !important;
    }
}
