/* =========================================
   1. GLOBAL MODAL & ANIMATIONS
   ========================================= */
#ai-chat-modal.chat-modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background-color: rgba(0, 0, 0, 0.4);
    display: none; /* Hidden by default */
    align-items: center; justify-content: center;
    z-index: 10000; backdrop-filter: blur(5px);
    transition: opacity 0.2s ease;
}

/* Ensure text reveal modal stacks on top */
#passage-reveal-modal { z-index: 20000 !important; }

/* Glassmorphism Card Container */
.chat-modal-card {
    background: rgba(255, 255, 255, 0.85); /* Slightly more opaque for better contrast */
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.8);
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1), 0 5px 15px rgba(0, 0, 0, 0.05);
    border-radius: 24px;
    width: 90%; max-width: 450px; height: 750px; max-height: 90vh;
    display: flex; flex-direction: column; overflow: hidden; position: relative;
}

/* Background Decoration Blobs */
.chat-blob {
    position: absolute; width: 300px; height: 300px;
    background: radial-gradient(#818cf8, transparent 70%);
    opacity: 0.3; border-radius: 50%; z-index: -1; pointer-events: none;
}
.blob-1 { top: -100px; left: -100px; }
.blob-2 { bottom: -100px; right: -100px; background: radial-gradient(#f472b6, transparent 70%); }

/* =========================================
   2. HEADER & MODE SWITCHER
   ========================================= */
.chat-header-modern {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px 15px; background: rgba(255, 255, 255, 0.6);
    border-bottom: 1px solid rgba(255, 255, 255, 0.5); flex-shrink: 0;
    height: 70px;
}

/* The Toggle Pill */
.chat-mode-switcher {
    display: flex; gap: 12px; align-items: center;
    margin-right: 10px;
    background: rgba(255, 255, 255, 0.5);
    padding: 4px; border-radius: 30px;
    border: 1px solid rgba(255,255,255,0.6);
}

/* Avatars for Modes */
.mode-avatar {
    width: 40px; height: 40px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 1.2rem; cursor: pointer;
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    opacity: 0.5; filter: grayscale(100%);
    border: 2px solid transparent;
    position: relative; /* For badge positioning */
}
.mode-avatar:hover { opacity: 0.8; filter: grayscale(50%); transform: scale(1.05); }

.mode-avatar.active {
    opacity: 1; filter: grayscale(0%);
    transform: scale(1.1);
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    border-color: white;
}

.mode-avatar-human { background: linear-gradient(135deg, #10b981 0%, #059669 100%); color: white; }
.mode-avatar-ai { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; }

/* GLOBAL NOTIFICATION DOT (Blinking Green) */
.mode-notification-dot {
    position: absolute; top: 0; right: 0;
    width: 12px; height: 12px;
    background-color: #22c55e; /* Bright Green */
    border: 2px solid #fff;
    border-radius: 50%;
    display: none; /* Hidden by default */
    box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7);
    animation: pulse-green-dot 2s infinite;
    z-index: 20;
}

@keyframes pulse-green-dot {
    0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7); }
    70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); }
    100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}

/* Header Text Info */
.header-text { flex-grow: 1; overflow: hidden; display: flex; flex-direction: column; justify-content: center; }
.header-text h2 { margin: 0; font-size: 0.95rem; color: #333; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.2; }
.header-text p { margin: 0; font-size: 0.75rem; color: #22c55e; display: flex; align-items: center; gap: 4px; font-weight: 500; line-height: 1.2; }
.status-dot { width: 6px; height: 6px; background: #22c55e; border-radius: 50%; display: inline-block; }

/* Cost Badge (AI Only) */
.chat-cost-badge {
    display: flex; align-items: center; gap: 6px; background: rgba(255, 255, 255, 0.6);
    padding: 6px 12px; border-radius: 20px; border: 1px solid rgba(0, 0, 0, 0.1);
    font-size: 0.6rem; color: #666; margin-left: auto; margin-right: 10px;
}
.chat-cost-badge i { color: #f59e0b; font-size: 1rem; }

/* Header Action Buttons */
.header-actions { display: flex; gap: 8px; }
.action-btn-modern, .close-btn-modern {
    background: none; border: none; font-size: 1.2rem; cursor: pointer; color: #666;
    padding: 0; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
    transition: all 0.2s ease;
}
.close-btn-modern:hover { color: #000; background-color: rgba(0,0,0,0.05); }
.action-btn-modern:hover { color: #dc3545; background-color: rgba(220, 53, 69, 0.1); }

/* =========================================
   3. VIEW CONTAINER (SLIDING TABS)
   ========================================= */
.view-container { flex-grow: 1; display: flex; flex-direction: column; overflow: hidden; position: relative; }

.view-section {
    flex-grow: 1; display: flex; flex-direction: column; overflow: hidden;
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: transparent;
    transition: transform 0.3s cubic-bezier(0.25, 1, 0.5, 1), opacity 0.2s ease;
}

.view-section.hidden-left { transform: translateX(-100%); opacity: 0; pointer-events: none; }
.view-section.hidden-right { transform: translateX(100%); opacity: 0; pointer-events: none; }
.view-section.active { transform: translateX(0); opacity: 1; pointer-events: auto; z-index: 10; }

/* =========================================
   4. HUMAN CHAT STYLES
   ========================================= */

/* Search Bar */
.human-search-wrapper {
    padding: 15px 15px 5px 15px;
    flex-shrink: 0;
}
.human-search-input {
    width: 100%;
    padding: 10px 15px;
    border-radius: 20px;
    border: 1px solid rgba(255,255,255,0.6);
    background: rgba(255,255,255,0.5);
    font-size: 0.9rem;
    color: #333;
    outline: none;
    transition: all 0.2s ease;
}
.human-search-input:focus {
    background: #fff;
    border-color: #007bff;
    box-shadow: 0 4px 12px rgba(0,123,255,0.1);
}
.human-search-input::placeholder { color: #888; }

/* Contact List */
.human-list-wrapper { padding: 10px; overflow-y: auto; height: 100%; }

.conversation-item {
    display: flex; align-items: center; gap: 12px; padding: 12px;
    margin-bottom: 8px; border-radius: 12px;
    background: rgba(255, 255, 255, 0.5); border: 1px solid rgba(255,255,255,0.6);
    cursor: pointer; transition: background 0.2s;
}
.conversation-item:hover { background: rgba(255, 255, 255, 0.8); }

.conv-avatar {
    width: 44px; height: 44px; border-radius: 50%; object-fit: cover;
    background: #ddd; flex-shrink: 0; border: 1px solid rgba(0,0,0,0.1);
    display: flex; align-items: center; justify-content: center; font-weight: bold; color: white;
}
.conv-info { flex-grow: 1; min-width: 0; }

/* BOLD NAME for Unread Items */
.conv-name { font-weight: 500; font-size: 0.9rem; color: #333; }
.conv-name.unread-bold {
    font-weight: 800 !important;
    color: #000 !important;
}

.conv-preview { font-size: 0.8rem; color: #666; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.conv-badge {
    background: #22c55e; /* Green */
    color: white; font-size: 0.65rem; font-weight: bold;
    padding: 2px 6px; border-radius: 10px;
}

/* Human Conversation Detail View */
.human-chat-header {
    display: flex; align-items: center; gap: 10px; padding: 10px 15px;
    background: rgba(255,255,255,0.3); border-bottom: 1px solid rgba(255,255,255,0.3);
    flex-shrink: 0;
}
.back-btn-human {
    background: none; border: none; font-size: 1.2rem; cursor: pointer; color: #555;
    padding: 4px; display: flex; align-items: center;
}

/* =========================================
   5. SHARED CHAT BODY & BUBBLES
   ========================================= */
.chat-content-modern {
    flex-grow: 1; padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 15px;
    scrollbar-width: thin; scrollbar-color: rgba(0, 0, 0, 0.1) transparent;
    /* IMPORTANT: Allow flex-end alignment for bubbles */
    align-items: stretch; 
}

.message-row { display: flex; width: 100%; margin-bottom: 2px; }

/* ALIGNMENT FIXES - IMPORTANT! */
.message-row.bot { justify-content: flex-start !important; }
.message-row.user { justify-content: flex-end !important; }

.message-bubble {
    max-width: 80%; padding: 12px 16px; font-size: 0.95rem; line-height: 1.5; position: relative;
    display: flex; flex-direction: column; gap: 8px; word-wrap: break-word;
}

/* Incoming (Gray/Left) */
.message-row.bot .message-bubble {
    background: rgba(255, 255, 255, 0.8); border: 1px solid rgba(255, 255, 255, 0.5); color: #333;
    border-radius: 18px 18px 18px 4px;
    /* Ensure text aligns left inside bubble */
    align-items: flex-start; 
}

/* Outgoing (Blue/Right) */
.message-row.user .message-bubble {
    background: linear-gradient(135deg, #007bff 0%, #0056b3 100%); color: white;
    border-radius: 18px 18px 4px 18px; box-shadow: 0 4px 15px rgba(0, 123, 255, 0.3);
    /* Ensure text aligns left inside bubble (standard for messaging) */
    align-items: flex-start;
}

.chat-image-preview { 
    max-width: 100%; 
    border-radius: 8px; 
    border: 1px solid rgba(255,255,255,0.2); 
    display: block; 
    cursor: zoom-in; /* Indicate it's clickable */
}

.message-content strong { font-weight: 700; }
.message-row.bot .message-content strong { color: #0056b3; }
.message-row.user .message-content strong { color: #fff; text-decoration: underline; text-decoration-color: rgba(255,255,255,0.3); }

/* TTS Controls (AI Only) */
.message-controls { display: flex; gap: 8px; padding-top: 5px; border-top: 1px solid rgba(0, 0, 0, 0.05); width: 100%; min-height: 34px; }
.tts-action-btn {
    background: none; border: 1px solid rgba(0, 0, 0, 0.15); color: #007bff;
    width: 32px; height: 32px; display: flex; align-items: center; justify-content: center;
    border-radius: 4px; cursor: pointer; transition: all 0.2s ease;
}
.tts-action-btn:hover { background-color: #e6f0ff; transform: scale(1.05); }
.tts-action-btn.playing { color: white !important; background-color: #28a745 !important; border-color: #28a745 !important; animation: pulse-green 1.5s infinite; }
.tts-action-btn.ready { color: #28a745 !important; border-color: #28a745 !important; }
.tts-action-btn.loading { color: #6c757d !important; cursor: wait; }
@keyframes pulse-green { 0% {box-shadow: 0 0 0 0 rgba(40,167,69, 0.4);} 70% {box-shadow: 0 0 0 10px rgba(40,167,69, 0);} 100% {box-shadow: 0 0 0 0 rgba(40,167,69, 0);} }

.timestamp { font-size: 0.65rem; margin-top: 4px; opacity: 0.7; text-align: right; width: 100%; }

/* Typing Animation */
.typing-indicator-modern { display: flex; gap: 4px; padding: 12px 16px; background: rgba(255, 255, 255, 0.6); border-radius: 18px; width: fit-content; margin-bottom: 10px; }
.typing-dot { width: 6px; height: 6px; background: #94a3b8; border-radius: 50%; animation: bounce 1.4s infinite ease-in-out both; }
.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }
@keyframes bounce { 0%, 80%, 100% { transform: scale(0); } 40% { transform: scale(1); } }

/* =========================================
   6. INPUT AREAS
   ========================================= */
.input-area-modern { padding: 15px; background: rgba(255, 255, 255, 0.9); backdrop-filter: blur(30px); border-top: 1px solid rgba(255, 255, 255, 0.5); display: flex; flex-direction: column; gap: 12px; flex-shrink: 0; }
.input-row { display: flex; gap: 8px; align-items: center; width: 100%; }

.attach-btn-modern { width: 44px; height: 44px; background: white; border: 1px solid rgba(0,0,0,0.15); border-radius: 50%; color: #555; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; transition: all 0.2s ease; flex-shrink: 0; box-shadow: 0 2px 6px rgba(0,0,0,0.05); }
.attach-btn-modern:hover { background-color: #f8f9fa; color: #007bff; transform: scale(1.05); border-color: #007bff; }
.attach-btn-modern.active { color: #fff; background-color: #007bff; border-color: #007bff; }

.input-wrapper-modern { position: relative; display: flex; align-items: center; background: #f0f2f5; border-radius: 24px; padding: 0 20px; border: 1px solid transparent; transition: all 0.2s ease; flex: 1; overflow: hidden; }
.input-wrapper-modern:focus-within { background: #fff; border-color: #007bff; box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.15); }
#screenshot-preview { display: none; height: 32px; width: auto; border-radius: 4px; border: 1px solid #ccc; margin-right: 8px; cursor: pointer; }
#screenshot-preview:hover { opacity: 0.7; }

#ai-chat-input, #human-chat-input { flex-grow: 1; border: none; background: transparent; padding: 12px 0; outline: none; font-size: 0.95rem; color: #333; width: 100%; }

.send-btn-modern, .reveal-btn-modern { width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; cursor: pointer; transition: all 0.2s; }
.send-btn-modern { background: linear-gradient(135deg, #007bff 0%, #0056b3 100%); border: none; color: white; box-shadow: 0 4px 10px rgba(0, 123, 255, 0.3); }
.send-btn-modern:disabled { background: #ccc; cursor: not-allowed; opacity: 0.7; box-shadow: none; }
.reveal-btn-modern { background: white; border: 1px solid rgba(0,0,0,0.15); color: #555; box-shadow: 0 2px 6px rgba(0,0,0,0.05); }
.reveal-btn-modern:hover { background-color: #f8f9fa; color: #007bff; transform: scale(1.05); }

/* AI SLIDER */
.model-selector-container { width: 100%; background: rgba(255, 255, 255, 0.5); border-radius: 16px; padding: 10px 15px; margin-top: 5px; border: 1px solid rgba(255,255,255,0.6); display: block; }
.model-labels { display: flex; justify-content: space-between; font-size: 0.75rem; font-weight: 600; color: #555; margin-bottom: 5px; cursor: pointer; }
.model-labels span { transition: color 0.2s; }
.model-labels span.active { color: #007bff; text-shadow: 0 0 10px rgba(0,123,255,0.2); }
.range-slider { -webkit-appearance: none; width: 100%; height: 6px; border-radius: 5px; background: linear-gradient(to right, #22c55e 0%, #3b82f6 50%, #a855f7 100%); outline: none; opacity: 0.9; transition: opacity .2s; }
.range-slider::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 18px; height: 18px; border-radius: 50%; background: #007bff; cursor: pointer; box-shadow: 0 2px 6px rgba(0,0,0,0.2); transition: transform 0.1s; }
.range-slider::-webkit-slider-thumb:hover { transform: scale(1.2); }

/* FOOTER CONTROLS */
.settings-row-modern { display: flex; gap: 10px; width: 100%; }
.chat-style-select { appearance: none; width: 100%; padding: 10px 15px; border-radius: 12px; border: 1px solid rgba(200, 200, 200, 0.5); background-color: rgba(255, 255, 255, 0.8); font-size: 0.9rem; color: #444; cursor: pointer; }
.chat-disclaimer-modern { text-align: center; font-size: 0.7rem; color: #94a3b8; padding: 8px; background: rgba(255, 255, 255, 0.5); flex-shrink: 0; }

/* MOBILE OPTIMIZATION */
@media (max-width: 768px) {
    #ai-chat-modal.chat-modal-overlay { align-items: flex-start !important; padding: 0 !important; height: 100dvh; }
    .chat-modal-card { width: 100% !important; height: 100dvh !important; max-height: none !important; border-radius: 0 !important; box-shadow: none !important; display: flex; flex-direction: column; }
    .chat-header-modern { flex-shrink: 0; }
    .input-area-modern { flex-shrink: 0; padding-bottom: 5px; }
    .chat-content-modern { flex-grow: 1; height: auto !important; overflow-y: auto; }
    .chat-blob { display: none; }
}
body.chat-open { overflow: hidden; overscroll-behavior: none; }
.chat-content-modern { overscroll-behavior: contain; }

/* =========================================
   7. ATTACHMENT MENU (Camera vs Gallery)
   ========================================= */
.attach-options-menu {
    position: absolute;
    bottom: 80px; /* Sits cleanly above the input bar */
    left: 20px;   /* Aligns with the paperclip icon */
    
    background: #ffffff;
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    
    display: none; /* Hidden by default */
    flex-direction: column; /* Force VERTICAL stacking */
    gap: 0; /* No gaps, we use padding inside buttons */
    
    z-index: 99999;
    min-width: 180px;
    overflow: hidden;
    
    /* Animation */
    transform-origin: bottom left;
    animation: menuPop 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.attach-options-menu.active {
    display: flex !important; /* Force flex to apply column direction */
}

.attach-option-btn {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    
    background: white;
    border: none;
    border-bottom: 1px solid #f0f0f0; /* Divider line */
    padding: 14px 16px;
    
    text-align: left;
    cursor: pointer;
    
    font-size: 0.95rem;
    font-weight: 500;
    color: #333;
    transition: background 0.2s;
}

/* Remove border from last item */
.attach-option-btn:last-child {
    border-bottom: none;
}

.attach-option-btn:active {
    background-color: #f5f7fa;
}

.attach-option-btn i {
    font-size: 1.2rem;
    color: #007bff; /* Blue icons */
}

@keyframes menuPop {
    from { 
        opacity: 0; 
        transform: scale(0.9) translateY(10px); 
    }
    to { 
        opacity: 1; 
        transform: scale(1) translateY(0); 
    }
}
/* File Attachment Bubble */
.file-attachment-bubble {
    display: flex;
    align-items: center;
    gap: 12px;
    background: rgba(0,0,0,0.05);
    border: 1px solid rgba(0,0,0,0.1);
    padding: 10px;
    border-radius: 12px;
    margin-top: 5px;
    text-decoration: none;
    transition: background 0.2s;
    
    /* FIXES START */
    max-width: 100%;       /* Prevent growing wider than parent */
    box-sizing: border-box; /* Include padding in width calculation */
    overflow: hidden;       /* Clip corners/content if needed */
    /* FIXES END */
}
.file-attachment-bubble:hover {
    background: rgba(0,0,0,0.1);
}
.message-row.user .file-attachment-bubble {
    background: rgba(255,255,255,0.2);
    border-color: rgba(255,255,255,0.3);
}
.file-icon {
    font-size: 24px;
    color: #555;
    flex-shrink: 0;
}
.message-row.user .file-icon { color: white; }

.file-info {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    
    /* CRITICAL FIX: Allows text truncation to work in Flexbox */
    min-width: 0; 
}
.file-name {
    font-size: 0.85rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: #333;
}
.message-row.user .file-name { color: white; }

.file-action {
    font-size: 0.7rem;
    color: #666;
}
.message-row.user .file-action { color: rgba(255,255,255,0.8); }

/* =========================================
   8. IMAGE VIEWER (FULLSCREEN)
   ========================================= */
.image-viewer-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.95);
    z-index: 999999; /* Top of everything */
    display: flex; flex-direction: column;
    opacity: 0; pointer-events: none; transition: opacity 0.3s ease;
}
.image-viewer-overlay.active { opacity: 1; pointer-events: auto; }

.image-viewer-toolbar {
    display: flex; align-items: center; padding: 15px 20px;
    background: rgba(0,0,0,0.4); backdrop-filter: blur(5px);
    position: absolute; top: 0; left: 0; width: 100%; z-index: 10;
}
.img-bar-btn {
    background: none; border: none; color: white; font-size: 1.5rem;
    cursor: pointer; padding: 8px; border-radius: 50%;
    transition: background 0.2s; display: flex; align-items: center; justify-content: center;
}
.img-bar-btn:hover { background: rgba(255,255,255,0.2); }
.img-bar-spacer { flex-grow: 1; }

.image-viewer-container {
    flex-grow: 1; display: flex; align-items: center; justify-content: center;
    overflow: hidden; padding: 10px; width: 100%; height: 100%;
}
#image-viewer-full {
    max-width: 100%; max-height: 100%;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
}
/* ai_chat_modal.css */

.message-bubble {
    max-width: 80%;
    padding: 12px 16px;
    font-size: 0.95rem;
    line-height: 1.5;
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 8px;
    
    /* ADD THESE LINES */
    word-wrap: break-word;       /* Legacy support */
    overflow-wrap: break-word;   /* Standard property */
    word-break: break-word;      /* Webkit specific fallback */
    hyphens: auto;               /* Optional: adds hyphens to long words */
}

.message-content {
    /* Ensure the content itself respects the parent width */
    display: block;
    width: 100%;
    overflow-wrap: break-word;
    word-break: break-word;
}
/* --- MESSAGE META & STATUS TICKS --- */
.message-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    margin-top: 2px;
    width: 100%;
}

/* Reset the top margin on timestamp since it's now in the flex row */
.message-bubble .timestamp {
    margin-top: 0; 
}

.message-status-tick {
    font-size: 0.95rem;
    display: flex;
    align-items: center;
}

/* Outgoing Bubble (Blue) Tick Colors */
.message-row.user .tick-sent, 
.message-row.user .tick-delivered {
    color: rgba(255, 255, 255, 0.6); /* Faded white */
}

.message-row.user .tick-read {
    color: #4ade80; /* Bright green to pop on the blue background */
    text-shadow: 0 0 5px rgba(74, 222, 128, 0.3);
}