/* ========================================
   MESSAGING HUB — Messenger + WhatsApp
   Shared styles for chat tabs
   ======================================== */

/* Section: full-bleed like email */
#section-messenger,
#section-whatsapp {
    padding: 0;
    max-width: 100%;
    height: calc(100vh - 49px);
    flex-direction: column;
    overflow: hidden;
}

/* ============ TOOLBAR ============ */
.msg-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border-light);
    background: var(--bg-card);
    flex-shrink: 0;
}

.msg-toolbar-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    margin-right: auto;
}

.msg-filter-group {
    display: flex;
    gap: 4px;
}

.msg-filter-btn {
    padding: 4px 10px;
    border: 1px solid var(--border-light);
    border-radius: 6px;
    background: var(--bg-card);
    font-size: 12px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s;
}

.msg-filter-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.msg-filter-btn.active {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #fff;
    border-color: transparent;
}

.msg-filter-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 8px;
    font-size: 10px;
    font-weight: 700;
    background: var(--danger);
    color: #fff;
    margin-left: 4px;
}

/* ============ 2-PANEL LAYOUT ============ */
.msg-layout {
    display: grid;
    grid-template-columns: 320px 1fr;
    flex: 1;
    overflow: hidden;
    background: var(--bg-card);
}

/* ============ LEFT: CONVERSATION LIST ============ */
.msg-list {
    border-right: 1px solid var(--border-light);
    overflow-y: auto;
    background: var(--bg-body);
}

.msg-list-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 13px;
}

.msg-list-empty-icon {
    font-size: 40px;
    margin-bottom: 12px;
    opacity: 0.5;
}

.msg-conv-item {
    display: flex;
    gap: 10px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border-light);
    cursor: pointer;
    transition: background 0.15s;
}

.msg-conv-item:hover {
    background: rgba(102, 126, 234, 0.04);
}

.msg-conv-item.active {
    background: rgba(102, 126, 234, 0.08);
    border-left: 3px solid #667eea;
    padding-left: 11px;
}

.msg-conv-item.unread {
    background: rgba(102, 126, 234, 0.03);
}

.msg-conv-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 600;
    flex-shrink: 0;
    overflow: hidden;
}

.msg-conv-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.msg-conv-body {
    flex: 1;
    min-width: 0;
}

.msg-conv-header {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 2px;
}

.msg-conv-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-conv-name.unread {
    font-weight: 700;
}

.msg-conv-time {
    font-size: 11px;
    color: var(--text-muted);
    margin-left: auto;
    flex-shrink: 0;
}

.msg-conv-snippet {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-conv-snippet.unread {
    color: var(--text-primary);
    font-weight: 500;
}

.msg-conv-meta {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: 3px;
}

.msg-account-badge {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 600;
    line-height: 16px;
}

.msg-account-badge.expert {
    background: #dbeafe;
    color: #1d4ed8;
}

.msg-account-badge.sales {
    background: #fef3c7;
    color: #b45309;
}

.msg-status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
}

.msg-status-dot.ai { background: #8b5cf6; }
.msg-status-dot.agent { background: #22c55e; }
.msg-status-dot.new { background: #3b82f6; }

.msg-unread-count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    font-size: 10px;
    font-weight: 700;
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
    margin-left: auto;
}

/* ============ RIGHT: THREAD VIEW ============ */
.msg-thread {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.msg-thread-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    font-size: 14px;
}

.msg-thread-empty-icon {
    font-size: 48px;
    margin-bottom: 12px;
    opacity: 0.3;
}

/* Thread Header */
.msg-thread-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border-light);
    background: var(--bg-card);
    flex-shrink: 0;
}

.msg-thread-back {
    display: none;
    padding: 4px 8px;
    border: 1px solid var(--border-light);
    border-radius: 6px;
    background: var(--bg-card);
    cursor: pointer;
    font-size: 14px;
}

.msg-thread-info {
    flex: 1;
    min-width: 0;
}

.msg-thread-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
}

.msg-thread-subtitle {
    font-size: 11px;
    color: var(--text-muted);
}

.msg-thread-actions {
    display: flex;
    gap: 6px;
}

.msg-action-btn {
    padding: 5px 10px;
    border: 1px solid var(--border-light);
    border-radius: 6px;
    background: var(--bg-card);
    font-size: 11px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}

.msg-action-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.msg-action-btn.active {
    background: #8b5cf6;
    color: #fff;
    border-color: transparent;
}

.msg-action-btn.danger:hover {
    border-color: var(--danger);
    color: var(--danger);
}

/* Messages Area */
.msg-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: #f8f9fb;
}

/* Chat Bubbles */
.msg-bubble-wrap {
    display: flex;
    max-width: 70%;
}

.msg-bubble-wrap.inbound {
    align-self: flex-start;
}

.msg-bubble-wrap.outbound {
    align-self: flex-end;
}

.msg-bubble {
    padding: 8px 12px;
    border-radius: 12px;
    font-size: 13px;
    line-height: 1.45;
    word-break: break-word;
    position: relative;
}

.msg-bubble-wrap.inbound .msg-bubble {
    background: var(--bg-card);
    color: var(--text-primary);
    border: 1px solid var(--border-light);
    border-bottom-left-radius: 4px;
}

.msg-bubble-wrap.outbound .msg-bubble {
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.msg-bubble-wrap.outbound.ai .msg-bubble {
    background: linear-gradient(135deg, #8b5cf6, #a855f7);
}

.msg-bubble-sender {
    font-size: 10px;
    font-weight: 600;
    margin-bottom: 2px;
    opacity: 0.7;
}

.msg-bubble-time {
    font-size: 10px;
    opacity: 0.6;
    margin-top: 3px;
    text-align: right;
}

.msg-bubble-status {
    font-size: 10px;
    margin-left: 4px;
}

/* System messages */
.msg-system {
    text-align: center;
    font-size: 11px;
    color: var(--text-muted);
    padding: 8px 0;
}

/* CRM Mini Card */
.msg-crm-card {
    margin: 8px 16px;
    padding: 8px 12px;
    background: var(--bg-body);
    border: 1px solid var(--border-light);
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 12px;
    flex-shrink: 0;
}

.msg-crm-card-name {
    font-weight: 600;
    color: var(--text-primary);
}

.msg-crm-card-stage {
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 600;
}

.msg-crm-card-link {
    margin-left: auto;
    color: var(--primary);
    cursor: pointer;
    text-decoration: none;
    font-weight: 500;
}

/* Input Bar */
.msg-input-bar {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 16px;
    border-top: 1px solid var(--border-light);
    background: var(--bg-card);
    flex-shrink: 0;
}

.msg-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border-light);
    border-radius: 10px;
    font-size: 13px;
    font-family: inherit;
    resize: none;
    max-height: 100px;
    outline: none;
    transition: border-color 0.15s;
}

.msg-input:focus {
    border-color: #667eea;
}

.msg-send-btn {
    padding: 8px 16px;
    border: none;
    border-radius: 10px;
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.15s;
    white-space: nowrap;
}

.msg-send-btn:hover {
    opacity: 0.9;
}

.msg-send-btn:disabled {
    opacity: 0.5;
    cursor: default;
}

/* ============ RESPONSIVE ============ */
@media (max-width: 768px) {
    .msg-layout {
        grid-template-columns: 1fr;
    }

    .msg-layout.thread-open .msg-list {
        display: none;
    }

    .msg-layout:not(.thread-open) .msg-thread {
        display: none;
    }

    .msg-thread-back {
        display: block;
    }
}
