/* ============================================================
   MESSENGER UI - SSN Assistant IA
   Full-screen, app-like chat interface
   ============================================================ */

/* ── Hide footer and announcement bar on the messenger page ── */
body.messenger-body #main-footer,
body.messenger-body .announcement-bar { display: none !important; }

/* The header stays visible, messenger takes remaining space */
body.messenger-body {
    overflow: hidden; /* prevent body scroll */
}

/* ── Full page layout ── */
.messenger-page {
    display: flex;
    position: fixed;
    top: var(--header-height, 72px); /* sits just below header */
    left: 0; right: 0; bottom: 0;
    background: #f0f2f5;
    overflow: hidden;
    z-index: 10;
}

/* ── Desktop sidebar ── */
.messenger-page .app-sidebar {
    width: 240px;
    flex-shrink: 0;
    height: 100%;
    overflow-y: auto;
    position: static !important;
    border-radius: 0 !important;
    border-right: 1px solid var(--border) !important;
    border-top: none !important; border-bottom: none !important; border-left: none !important;
    box-shadow: none !important;
}

/* ── Messenger window (takes remaining space) ── */
.messenger-window {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: white;
    height: 100%;
    overflow: hidden;
}

/* ── Header bar ── */
.messenger-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1.25rem;
    background: white;
    border-bottom: 1px solid #e4e6eb;
    flex-shrink: 0;
}
.messenger-header-left { display: flex; align-items: center; gap: 0.85rem; }
.messenger-avatar-wrap { position: relative; }
.messenger-avatar-icon {
    width: 40px; height: 40px;
    background: linear-gradient(135deg, var(--primary), #8B5CF6);
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    color: white; font-size: 1rem;
}
.online-dot {
    position: absolute; bottom: 1px; right: 1px;
    width: 10px; height: 10px;
    background: #31a24c; border-radius: 50%;
    border: 2px solid white;
}
.messenger-header-name { font-weight: 700; font-size: 0.95rem; line-height: 1.2; }
.messenger-header-status { font-size: 0.75rem; color: #65676b; }

/* Quota chip */
.quota-chip {
    display: flex; align-items: center; gap: 0.35rem;
    background: #e8f4fd; color: var(--primary);
    padding: 0.3rem 0.75rem; border-radius: 999px;
    font-size: 0.8rem; font-weight: 700;
}
.quota-chip.quota-empty { background: #fee2e2; color: #991b1b; }

/* Reset Chat Button */
.btn-reset-chat {
    background: none;
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--primary);
    cursor: pointer;
    transition: background 0.2s;
    margin-right: 0.5rem;
}
.btn-reset-chat:hover {
    background: #f0f2f5;
}

/* ── Context bar ── */
.context-bar {
    display: flex;
    gap: 0.5rem;
    padding: 0.45rem 1rem;
    background: #f8f9fa;
    border-bottom: 1px solid #e4e6eb;
    overflow-x: auto;
    scrollbar-width: none;
    flex-shrink: 0;
}
.context-bar::-webkit-scrollbar { display: none; }
.ctx-select {
    border: 1px solid #e4e6eb;
    background: white;
    border-radius: 999px;
    padding: 0.25rem 0.75rem;
    font-size: 0.8rem; font-weight: 600;
    color: #050505; cursor: pointer;
    white-space: nowrap; flex-shrink: 0;
    outline: none; appearance: none; -webkit-appearance: none;
}
.ctx-select:focus { border-color: var(--primary); }

/* ── Messages scrollable area ── */
.messenger-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

/* ── Message bubbles ── */
.msg { display: flex; gap: 0.75rem; max-width: 80%; margin-bottom: 0.5rem; animation: msg-appear 0.3s ease-out; }
@keyframes msg-appear {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.msg-ai  { align-self: flex-start; }
.msg-user { align-self: flex-end; flex-direction: row-reverse; }

.msg-avatar {
    width: 32px; height: 32px; border-radius: 50%;
    background: linear-gradient(135deg, #6366f1, #a855f7); color: white;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.85rem; flex-shrink: 0; align-self: flex-end;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
.msg-user .msg-avatar { background: #e4e6eb; color: #050505; }

.msg-bubble {
    padding: 0.75rem 1.1rem;
    border-radius: 1.5rem;
    font-size: 0.95rem; line-height: 1.6;
    word-break: break-word; max-width: 100%;
    position: relative;
}

.msg-ai .msg-bubble   { 
    background: #ffffff; 
    color: #1e293b; 
    border-bottom-left-radius: 4px;
    border: 1px solid #e2e8f0;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
.msg-user .msg-bubble { 
    background: linear-gradient(135deg, #2563eb, #3b82f6); 
    color: white;   
    border-bottom-right-radius: 4px;
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.2);
}

/* Markdown Styling inside bubbles */
.markdown-body p { margin-bottom: 0.75rem; }
.markdown-body p:last-child { margin-bottom: 0; }
.markdown-body ul, .markdown-body ol { margin-bottom: 0.75rem; padding-left: 1.5rem; }
.markdown-body li { margin-bottom: 0.25rem; }
.markdown-body h1, .markdown-body h2, .markdown-body h3 { 
    font-size: 1.1rem; font-weight: 700; margin: 1rem 0 0.5rem; color: inherit;
}
.markdown-body h1:first-child, .markdown-body h2:first-child, .markdown-body h3:first-child { margin-top: 0; }
.markdown-body code { 
    font-family: 'Fira Code', monospace;
    padding: 0.2rem 0.4rem;
    border-radius: 6px;
    font-size: 0.9em;
}

.msg-user .msg-bubble code { background: rgba(255,255,255,0.2); color: white; }
.msg-ai .msg-bubble code { background: #f1f5f9; color: #0f172a; border: 1px solid #e2e8f0; }

/* Image/file in bubble */
.msg-img-preview { 
    max-width: 280px; max-height: 240px; 
    border-radius: 12px; display: block; 
    margin-bottom: 0.5rem; 
    cursor: zoom-in;
    transition: transform 0.2s;
}
.msg-img-preview:hover { transform: scale(1.02); }

.msg-file-chip { 
    display: inline-flex; align-items: center; gap: 0.5rem; 
    font-size: 0.85rem; padding: 0.4rem 0.8rem; 
    border-radius: 10px; margin-bottom: 0.4rem;
    font-weight: 500;
}
.msg-user .msg-file-chip { background: rgba(255,255,255,0.2); }
.msg-ai .msg-file-chip  { background: #f1f5f9; border: 1px solid #e2e8f0; }

/* Typing animation */
.typing-indicator { display: flex; gap: 4px; }
.typing-dot { width: 7px; height: 7px; background: #8a8d91; border-radius: 50%; animation: typing-bounce 1.4s infinite ease-in-out; }
.typing-dot:nth-child(1) { animation-delay: 0s; }
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-bounce { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-5px); } }

/* ── Compose area (always at bottom) ── */
.messenger-compose-area {
    flex-shrink: 0;
    background: white;
    border-top: 1px solid #e4e6eb;
    padding: 0.5rem 1rem 0.35rem;
}
.file-preview-zone {
    display: flex; flex-wrap: wrap; gap: 0.5rem;
    padding-bottom: 0.4rem;
}
.file-chip {
    display: flex; align-items: center; gap: 0.5rem;
    background: #f0f2f5; border-radius: 10px;
    padding: 0.3rem 0.7rem; font-size: 0.8rem; max-width: 240px;
}
.file-thumb { width: 36px; height: 36px; object-fit: cover; border-radius: 6px; }
.file-icon { font-size: 1.4rem; }
.remove-file { background: none; border: none; cursor: pointer; color: #65676b; font-size: 0.85rem; }

.messenger-compose {
    display: flex;
    align-items: flex-end;
    gap: 0.4rem;
}
.attach-btn {
    width: 36px; height: 36px; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    color: #0084ff; font-size: 1.2rem; cursor: pointer;
    border-radius: 50%; transition: background 0.15s;
}
.attach-btn:hover { background: #f0f2f5; }

#chat-textarea {
    flex: 1; border: none;
    background: #f0f2f5; border-radius: 1.2rem;
    padding: 0.55rem 1rem;
    font-family: inherit; font-size: 0.9rem;
    resize: none; min-height: 36px; max-height: 120px;
    line-height: 1.4; outline: none; transition: background 0.15s;
}
#chat-textarea:focus { background: #e4e6eb; }

.send-btn {
    width: 36px; height: 36px; flex-shrink: 0;
    background: none; border: none;
    color: #0084ff; font-size: 1.2rem;
    cursor: pointer; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.15s, transform 0.1s;
}
.send-btn:hover { background: #f0f2f5; transform: scale(1.1); }
.compose-disclaimer { font-size: 0.68rem; color: #8a8d91; text-align: center; padding-top: 0.2rem; }

/* Quota exhausted */
.quota-exhausted {
    flex-shrink: 0; background: #fee2e2; color: #991b1b;
    font-weight: 600; text-align: center;
    padding: 0.85rem; font-size: 0.88rem;
    border-top: 1px solid #fca5a5;
}

/* ═══════════════════════════════════
   MOBILE ( ≤ 1024px )
   ═══════════════════════════════════ */
@media (max-width: 1024px) {
    /* Work out header height dynamically via JS, or approximate */
    .messenger-page {
        top: 65px; /* approx header without announce-bar */
        flex-direction: column;
    }

    /* Sidebar → compact horizontal tab strip */
    .messenger-page .app-sidebar {
        width: 100% !important;
        height: auto !important;
        flex-direction: row !important;
        border-right: none !important;
        border-bottom: 1px solid var(--border) !important;
        overflow-x: auto;
        padding: 0 !important;
        background: white;
    }
    .messenger-page .sidebar-user,
    .messenger-page .sidebar-quota { display: none !important; }
    .messenger-page .sidebar-nav {
        flex-direction: row !important;
        gap: 0 !important;
        flex-grow: 0 !important;
        width: 100%;
    }
    .messenger-page .sidebar-nav li { flex: 1; }
    .messenger-page .sidebar-nav a {
        flex-direction: column;
        justify-content: center;
        align-items: center;
        padding: 0.5rem 0.4rem;
        border-radius: 0;
        font-size: 0.65rem;
        gap: 0.15rem;
        border-bottom: 3px solid transparent;
        color: var(--text-muted);
    }
    .messenger-page .sidebar-nav a.active {
        background: none; color: var(--primary);
        border-bottom-color: var(--primary);
    }
    .messenger-page .sidebar-nav a i { margin: 0; font-size: 1rem; width: auto; }
    .messenger-page .sidebar-nav a span.badge-new { display: none; }

    .messenger-window { height: auto; flex: 1; }
    .msg { max-width: 88%; }
    .messenger-messages { padding: 0.75rem; }
}

@media (max-width: 480px) {
    .messenger-header { padding: 0.5rem 0.75rem; }
    .context-bar { padding: 0.35rem 0.75rem; gap: 0.3rem; }
    .ctx-select { font-size: 0.75rem; padding: 0.2rem 0.6rem; }
    .msg { max-width: 92%; }
    .msg-bubble { font-size: 0.87rem; padding: 0.5rem 0.8rem; }
    .quota-chip { font-size: 0.73rem; padding: 0.25rem 0.6rem; }
}

/* Keep the existing sidebar but make it not conflict */
.messenger-page .app-sidebar {
    position: static !important;
    height: 100% !important;
    overflow-y: auto;
    flex-shrink: 0;
    border-radius: 0;
    border-right: 1px solid var(--border);
    border-top: none;
    border-bottom: none;
    border-left: none;
    box-shadow: none;
}

/* ── Messenger Window ── */
.messenger-window {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: white;
}

/* ── Header bar ── */
.messenger-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.85rem 1.25rem;
    background: white;
    border-bottom: 1px solid #e4e6eb;
    flex-shrink: 0;
}
.messenger-header-left { display: flex; align-items: center; gap: 0.85rem; }
.messenger-avatar-wrap { position: relative; }
.messenger-avatar-icon {
    width: 42px; height: 42px;
    background: linear-gradient(135deg, var(--primary), #8B5CF6);
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    color: white; font-size: 1.1rem;
}
.online-dot {
    position: absolute; bottom: 1px; right: 1px;
    width: 10px; height: 10px;
    background: #31a24c; border-radius: 50%;
    border: 2px solid white;
}
.messenger-header-name { font-weight: 700; font-size: 1rem; line-height: 1.2; }
.messenger-header-status { font-size: 0.78rem; color: #65676b; }

/* Quota chip */
.quota-chip {
    display: flex; align-items: center; gap: 0.4rem;
    background: #e8f4fd; color: var(--primary);
    padding: 0.35rem 0.85rem; border-radius: 999px;
    font-size: 0.82rem; font-weight: 700;
}
.quota-chip.quota-empty { background: #fee2e2; color: #991b1b; }

/* ── Context bar (selectors) ── */
.context-bar {
    display: flex;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: #f0f2f5;
    border-bottom: 1px solid #e4e6eb;
    overflow-x: auto;
    scrollbar-width: none;
    flex-shrink: 0;
}
.context-bar::-webkit-scrollbar { display: none; }
.ctx-select {
    border: none;
    background: white;
    border-radius: 999px;
    padding: 0.3rem 0.85rem;
    font-size: 0.82rem;
    font-weight: 600;
    color: #050505;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    outline: none;
    appearance: none;
    -webkit-appearance: none;
}
.ctx-select:focus { box-shadow: 0 0 0 2px var(--primary); }

/* ── Messages area ── */
.messenger-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    background: #ffffff;
}

/* ── Message bubbles ── */
.msg { display: flex; gap: 0.5rem; max-width: 75%; }
.msg-ai { align-self: flex-start; }
.msg-user { align-self: flex-end; flex-direction: row-reverse; max-width: 75%; }

.msg-avatar {
    width: 32px; height: 32px; border-radius: 50%;
    background: #e4e6eb; color: #050505;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.85rem; flex-shrink: 0;
    align-self: flex-end;
}

.msg-bubble {
    padding: 0.65rem 1rem;
    border-radius: 1.25rem;
    font-size: 0.93rem;
    line-height: 1.5;
    word-break: break-word;
    max-width: 100%;
}
.msg-ai .msg-bubble {
    background: #f0f2f5;
    color: #050505;
    border-bottom-left-radius: 4px;
}
.msg-user .msg-bubble {
    background: #0084ff;
    color: white;
    border-bottom-right-radius: 4px;
}
.msg-user .msg-bubble code { background: rgba(255,255,255,0.2); color: white; }
.msg-ai .msg-bubble code { background: #e2e8f0; color: #1e293b; padding: 0.1em 0.3em; border-radius: 4px; font-size: 0.88em; }

/* Message image/file */
.msg-img-preview { max-width: 220px; max-height: 200px; border-radius: 12px; display: block; margin-bottom: 0.3rem; }
.msg-file-chip { background: rgba(255,255,255,0.15); border-radius: 8px; padding: 0.4rem 0.7rem; font-size: 0.85rem; display: inline-flex; align-items: center; gap: 0.4rem; margin-bottom: 0.25rem; }
.msg-user .msg-file-chip { background: rgba(255,255,255,0.2); }
.msg-ai .msg-file-chip { background: #e2e8f0; }

/* Typing */
.typing-indicator { display: flex; gap: 4px; }
.typing-dot { width: 7px; height: 7px; background: #8a8d91; border-radius: 50%; animation: typing 1.4s infinite ease-in-out; }
.typing-dot:nth-child(1) { animation-delay: 0s; }
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-5px); } }

/* ── Compose area ── */
.messenger-compose-area {
    flex-shrink: 0;
    background: white;
    border-top: 1px solid #e4e6eb;
    padding: 0.5rem 1rem 0.4rem;
}

/* File preview */
.file-preview-zone {
    padding: 0.4rem 0;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.file-chip {
    display: flex; align-items: center; gap: 0.5rem;
    background: #f0f2f5; border-radius: 12px;
    padding: 0.35rem 0.75rem; font-size: 0.82rem;
    max-width: 250px;
}
.file-thumb { width: 40px; height: 40px; object-fit: cover; border-radius: 8px; }
.file-icon { font-size: 1.5rem; }
.remove-file { background: none; border: none; cursor: pointer; color: #65676b; font-size: 0.9rem; padding: 0 0.25rem; }

/* Compose row */
.messenger-compose {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
}

.attach-btn {
    width: 38px; height: 38px; flex-shrink: 0;
    background: none; border: none;
    display: flex; align-items: center; justify-content: center;
    color: #0084ff; font-size: 1.25rem; cursor: pointer;
    border-radius: 50%; transition: background 0.15s;
}
.attach-btn:hover { background: #f0f2f5; }

#chat-textarea {
    flex: 1;
    border: none;
    background: #f0f2f5;
    border-radius: 1.25rem;
    padding: 0.6rem 1rem;
    font-family: inherit;
    font-size: 0.93rem;
    resize: none;
    min-height: 38px;
    max-height: 130px;
    line-height: 1.4;
    outline: none;
    transition: background 0.15s;
}
#chat-textarea:focus { background: #e4e6eb; }

.send-btn {
    width: 38px; height: 38px; flex-shrink: 0;
    background: none; border: none;
    color: #0084ff; font-size: 1.3rem;
    cursor: pointer; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.15s, transform 0.1s;
}
.send-btn:hover { background: #f0f2f5; transform: scale(1.1); }

.compose-disclaimer {
    font-size: 0.7rem; color: #8a8d91;
    text-align: center; padding: 0.25rem 0 0;
}

/* Quota exhausted */
.quota-exhausted {
    flex-shrink: 0;
    background: #fee2e2; color: #991b1b;
    font-weight: 600; text-align: center;
    padding: 1rem; font-size: 0.9rem;
    border-top: 1px solid #fca5a5;
}

/* ═══════════════════════════════════
   RESPONSIVE — mobile
   ═══════════════════════════════════ */
@media (max-width: 1024px) {
    .messenger-page {
        flex-direction: column;
        height: auto;
        min-height: calc(100vh - 80px);
    }

    /* Sidebar becomes horizontal tab strip on top */
    .messenger-page .app-sidebar {
        height: auto !important;
        flex-direction: row !important;
        border-right: none !important;
        border-bottom: 1px solid var(--border) !important;
        overflow-x: auto;
        padding: 0 !important;
        background: white;
        box-shadow: 0 2px 4px rgba(0,0,0,0.04);
    }
    .messenger-page .sidebar-user,
    .messenger-page .sidebar-quota { display: none !important; }
    .messenger-page .sidebar-nav {
        flex-direction: row !important;
        gap: 0 !important;
        flex-grow: 0 !important;
        width: 100%;
    }
    .messenger-page .sidebar-nav li { flex: 1; }
    .messenger-page .sidebar-nav a {
        flex-direction: column;
        justify-content: center;
        align-items: center;
        padding: 0.6rem 0.5rem;
        border-radius: 0;
        font-size: 0.68rem;
        gap: 0.2rem;
        border-bottom: 3px solid transparent;
    }
    .messenger-page .sidebar-nav a.active {
        background: none;
        color: var(--primary);
        border-bottom-color: var(--primary);
    }
    .messenger-page .sidebar-nav a i { margin: 0; font-size: 1.1rem; width: auto; }

    /* Messenger window goes full width */
    .messenger-window {
        height: calc(100vh - 80px - 60px); /* minus header and sidebar tab bar */
    }

    .msg { max-width: 88%; }
    .messenger-messages { padding: 0.75rem; }
}

@media (max-width: 480px) {
    .messenger-header { padding: 0.6rem 0.75rem; }
    .messenger-header-name { font-size: 0.9rem; }
    .quota-chip { font-size: 0.75rem; padding: 0.25rem 0.65rem; }
    .context-bar { padding: 0.4rem 0.75rem; gap: 0.35rem; }
    .ctx-select { font-size: 0.78rem; padding: 0.25rem 0.65rem; }
    .msg { max-width: 92%; }
    .msg-bubble { font-size: 0.88rem; padding: 0.55rem 0.85rem; }
}

/* ── Custom Modal ── */
.ssn-modal {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.5);
    display: flex; align-items: center; justify-content: center;
    z-index: 9999;
    opacity: 0; visibility: hidden;
    transition: all 0.25s ease;
    backdrop-filter: blur(4px);
}
.ssn-modal.active {
    opacity: 1; visibility: visible;
}
.ssn-modal .modal-content {
    background: white;
    width: 90%; max-width: 400px;
    padding: 2rem;
    border-radius: var(--radius-xl);
    text-align: center;
    transform: scale(0.9);
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1), 0 10px 10px -5px rgba(0,0,0,0.04);
}
.ssn-modal.active .modal-content {
    transform: scale(1);
}
.ssn-modal .modal-icon {
    font-size: 3rem; color: #EF4444; margin-bottom: 1rem;
}
.ssn-modal h3 { margin-bottom: 0.75rem; font-size: 1.25rem; color: var(--text-main); }
.ssn-modal p { color: var(--text-muted); font-size: 0.95rem; margin-bottom: 1.5rem; line-height: 1.5; }
.ssn-modal .modal-actions { display: flex; gap: 1rem; justify-content: center; }
.ssn-modal .btn { padding: 0.65rem 1.25rem; }
