/* ===========================================
   نظام المراسلة - Messenger-like UI
   =========================================== */

.messaging-container {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 0;
    width: 100%;
    max-width: 100%;

    background: var(--white, #fff);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: var(--shadow, 0 5px 30px rgba(0, 0, 0, 0.1));
}

/* القائمة الجانبية - المحادثات */
.conversations-sidebar {
    background: var(--light, #f8f9fa);
    border-left: 1px solid rgba(0, 0, 0, 0.08);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h3 {
    margin: 0;
    font-size: 1.2rem;
    color: var(--dark, #1e293b);
}

.new-conv-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary, #1a73e8);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.new-conv-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 15px rgba(26, 115, 232, 0.4);
}

.conversations-list {
    flex: 1;
    overflow-y: auto;
}

.conversation-item {
    display: flex;
    gap: 12px;
    padding: 15px 20px;
    cursor: pointer;
    transition: all 0.2s ease;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    position: relative;
}

.conversation-item:hover {
    background: rgba(26, 115, 232, 0.08);
}

.conversation-item.active {
    background: rgba(26, 115, 232, 0.15);
    border-right: 3px solid var(--primary, #1a73e8);
}

.conversation-item.has-unread {
    background: rgba(26, 115, 232, 0.05);
}

.conv-avatar img {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    object-fit: cover;
}

.conv-info {
    flex: 1;
    min-width: 0;
}

.conv-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.conv-name {
    font-weight: 600;
    color: var(--dark, #1e293b);
    font-size: 0.95rem;
}

.conv-time {
    font-size: 0.75rem;
    color: #ffffff;
}

.conv-preview {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.conv-message {
    font-size: 0.85rem;
    color: #ffffff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 180px;
}

.unread-badge {
    background: var(--primary, #1a73e8);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 2px 6px;
    border-radius: 10px;
    min-width: 18px;
    text-align: center;
}

.conv-status {
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 10px;
    margin-top: 5px;
    display: inline-block;
}

.conv-status.status-open {
    background: #fef3c7;
    color: #d97706;
}

.conv-status.status-answered {
    background: #d1fae5;
    color: #059669;
}

.conv-status.status-closed {
    background: #f1f5f9;
    color: #64748b;
}

.no-conversations {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #94a3b8;
    padding: 40px;
    text-align: center;
}

.no-conversations i {
    font-size: 3rem;
    margin-bottom: 15px;
    opacity: 0.5;
}


/* نافذة المحادثة */
.chat-window {
    display: flex;
    flex-direction: column;
    background: #f0f2f5;
    height: 100%;
    overflow: hidden;
}

.active-chat {
    display: none;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.chat-header {
    padding: 15px 25px;
    background: var(--white, #fff);
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    display: flex;
    align-items: center;
    gap: 15px;
}

.chat-header .back-btn {
    display: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--light, #f1f5f9);
    border: none;
    cursor: pointer;
    align-items: center;
    justify-content: center;
}

.chat-header-info h4 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--dark, #1e293b);
}

.chat-header-info span {
    font-size: 0.8rem;
    color: #64748b;
}

.chat-actions {
    margin-right: auto;
}

.chat-actions button {
    padding: 8px 15px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-size: 0.85rem;
    background: var(--light, #f1f5f9);
    color: var(--dark, #1e293b);
    transition: all 0.2s;
}

.chat-actions button:hover {
    background: #e2e8f0;
}

.close-conv-btn {
    color: #dc2626 !important;
}

/* منطقة الرسائل */
.messages-area {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.message {
    max-width: 75%;
    animation: messageIn 0.3s ease;
}

@keyframes messageIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message-sent {
    align-self: flex-start;
}

.message-received {
    align-self: flex-end;
}

.message-content {
    padding: 12px 18px;
    border-radius: 18px;
    font-size: 0.95rem;
    line-height: 1.5;
}

.message-content p {
    margin: 0;
}

.message-sent .message-content {
    background: var(--primary, #1a73e8);
    color: #fff;
    border-bottom-left-radius: 4px;
}

.message-received .message-content {
    background: var(--white, #fff);
    color: var(--dark, #1e293b);
    border-bottom-right-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.message-meta {
    display: flex;
    gap: 8px;
    padding: 4px 8px;
    font-size: 0.7rem;
    color: #94a3b8;
}

.message-sent .message-meta {
    justify-content: flex-start;
}

.message-received .message-meta {
    justify-content: flex-end;
}

/* حقل الإرسال */
.message-input-area {
    padding: 15px 20px;
    background: var(--white, #fff);
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    display: flex;
    gap: 12px;
    align-items: flex-end;
    flex-shrink: 0;
}

.message-input-area textarea {
    flex: 1;
    padding: 12px 18px;
    border: 1px solid #e2e8f0;
    border-radius: 24px;
    resize: none;
    max-height: 120px;
    font-size: 0.95rem;
    background: #f8fafc;
    transition: all 0.2s;
}

.message-input-area textarea:focus {
    outline: none;
    border-color: var(--primary, #1a73e8);
    background: #fff;
}

.send-btn {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary, #1a73e8);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.send-btn:hover {
    background: #1557b0;
    transform: scale(1.05);
}

.send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* حالة فارغة */
.empty-chat {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #94a3b8;
    text-align: center;
    padding: 40px;
}

.empty-chat i {
    font-size: 4rem;
    margin-bottom: 20px;
    opacity: 0.3;
}

.empty-chat h4 {
    margin: 0 0 10px;
    color: #64748b;
}

/* مودال محادثة جديدة */
.new-conv-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    backdrop-filter: blur(4px);
    overflow-y: auto;
}

.new-conv-modal.show {
    display: flex;
}

.new-conv-content {
    background: var(--white, #fff);
    border-radius: 16px;
    width: 90%;
    max-width: 450px;
    padding: 25px;
    animation: modalIn 0.3s ease;
    margin: auto;
}

@keyframes modalIn {
    from {
        opacity: 0;
        transform: scale(0.9);
    }
}

.new-conv-content h3 {
    margin: 0 0 20px;
    color: var(--dark, #1e293b);
}

.new-conv-content .form-group {
    margin-bottom: 15px;
}

.new-conv-content label {
    display: block;
    margin-bottom: 6px;
    font-weight: 500;
    color: #475569;
}

.new-conv-content input,
.new-conv-content textarea {
    width: 100%;
    padding: 12px 15px;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    font-size: 0.95rem;
}

.new-conv-content textarea {
    min-height: 100px;
    resize: vertical;
}

.new-conv-content .modal-actions {
    display: flex;
    gap: 10px;
    margin-top: 20px;
}

.new-conv-content .btn-cancel {
    flex: 1;
    padding: 12px;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    background: #fff;
    cursor: pointer;
}

.new-conv-content .btn-send {
    flex: 2;
    padding: 12px;
    background: var(--primary, #1a73e8);
    color: #fff;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    font-weight: 500;
}

/* ============================================
   الموبايل - Responsive
   ============================================ */

@media (max-width: 768px) {
    .messaging-container {
        grid-template-columns: 1fr;
        height: calc(100vh - 200px);
    }

    .conversations-sidebar {
        position: absolute;
        inset: 0;
        z-index: 10;
        transition: transform 0.3s ease;
    }

    .messaging-container.chat-open .conversations-sidebar {
        transform: translateX(100%);
    }

    .chat-header .back-btn {
        display: flex;
    }

    .messaging-container {
        position: relative;
    }

    .chat-window {
        position: absolute;
        inset: 0;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
    }

    .messaging-container.chat-open .chat-window {
        transform: translateX(0);
    }
}

/* ============================================
   إشعارات الرسائل - Message Notifications
   ============================================ */

.msg-notification-popup {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(-150px);
    background: linear-gradient(135deg, #1a73e8 0%, #0d47a1 100%);
    color: #fff;
    padding: 20px 25px;
    border-radius: 16px;
    box-shadow: 0 15px 50px rgba(26, 115, 232, 0.5);
    z-index: 999999;
    display: flex;
    align-items: center;
    gap: 15px;
    width: auto;
    min-width: 0;
    max-width: calc(100vw - 40px);
    opacity: 0;
    transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.msg-notification-popup.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

.msg-notification-popup .notif-icon {
    width: 50px;
    height: 50px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    animation: pulse 1s ease-in-out infinite;
}

.msg-notification-popup .notif-icon i {
    font-size: 1.5rem;
}

@keyframes pulse {

    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.1);
    }
}

.msg-notification-popup .notif-content {
    flex: 1;
    cursor: pointer;
}

.msg-notification-popup .notif-content strong {
    display: block;
    font-size: 1.1rem;
    margin-bottom: 4px;
}

.msg-notification-popup .notif-content p {
    margin: 0;
    font-size: 0.9rem;
    opacity: 0.9;
}

.msg-notification-popup .notif-close {
    width: 30px;
    height: 30px;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 50%;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.2s;
}

.msg-notification-popup .notif-close:hover {
    background: rgba(255, 255, 255, 0.3);
}

body.dark-mode .msg-notification-popup {
    background: linear-gradient(135deg, #3b82f6 0%, #1e40af 100%);
}

/* ============================================
   Messages Widget in Overview
   ============================================ */

.messages-widget {
    background: linear-gradient(135deg, #1a73e8 0%, #0d47a1 100%) !important;
    color: #fff !important;
    border: none !important;
}

.messages-widget h3 {
    color: #fff !important;
    display: flex;
    align-items: center;
    gap: 10px;
}

.messages-widget h3 i {
    font-size: 1.2rem;
}

.messages-alert {
    display: flex;
    align-items: center;
    gap: 20px;
    padding: 15px 0;
}

.messages-alert .alert-icon {
    width: 60px;
    height: 60px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    animation: pulse 1.5s ease-in-out infinite;
}

.messages-alert .alert-icon i {
    font-size: 1.8rem;
    color: #fff;
}

.messages-alert .alert-content {
    flex: 1;
}

.messages-alert .alert-content strong {
    display: block;
    font-size: 1.1rem;
    margin-bottom: 5px;
}

.messages-alert .alert-content p {
    margin: 0;
    opacity: 0.9;
    font-size: 0.9rem;
}

.messages-alert .btn-primary {
    background: #fff !important;
    color: #1a73e8 !important;
    padding: 12px 25px;
    border-radius: 25px;
    text-decoration: none;
    font-weight: 600;
    transition: all 0.3s;
    flex-shrink: 0;
}

.messages-alert .btn-primary:hover {
    transform: scale(1.05);
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
}

@media (max-width: 768px) {
    .messages-alert {
        flex-direction: column;
        text-align: center;
    }

    .messages-alert .btn-primary {
        width: 100%;
    }
}

/* Message Card in Overview */
.message-card {
    position: relative;
}

.message-card .msg-preview {
    display: block;
    color: #64748b;
    font-size: 0.85rem;
    margin-top: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 280px;
}

.message-card .msg-badge {
    background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
    color: #fff;
    font-size: 0.75rem;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 12px;
    min-width: 22px;
    text-align: center;
    animation: pulse 1.5s ease-in-out infinite;
}

body.dark-mode .message-card .msg-preview {
    color: #94a3b8;
}