/* =====================================================
   MODERN MESSAGING INTERFACE - Complete Redesign
   ===================================================== */

/* Main Layout Container */
.msg-layout {
    display: grid;
    grid-template-columns: 360px 1fr;
    height: calc(100vh - 180px);
    max-height: 700px;
    background: var(--card-background);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
    border: 1px solid var(--border-color);
}

/* =====================================================
   SIDEBAR - Conversations List
   ===================================================== */

.msg-sidebar {
    background: var(--card-background);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.msg-sidebar-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: linear-gradient(135deg, var(--card-background) 0%, rgba(99, 102, 241, 0.02) 100%);
}

.msg-sidebar-header h2 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
}

.msg-btn-icon {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    background: var(--primary-color);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.msg-btn-icon:hover {
    background: var(--primary-hover);
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.25);
}

.msg-btn-icon:active {
    transform: scale(0.95);
}

/* Search Bar */
.msg-search {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    position: relative;
}

.msg-search-icon {
    position: absolute;
    left: 2.25rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
    pointer-events: none;
    z-index: 1;
}

.msg-search-input {
    width: 100%;
    padding: 0.875rem 1rem 0.875rem 2.75rem;
    border: 2px solid transparent;
    border-radius: 12px;
    background: rgba(99, 102, 241, 0.04);
    font-size: 0.9375rem;
    color: var(--text-primary);
    transition: all 0.3s ease;
    outline: none;
}

.msg-search-input:focus {
    border-color: var(--primary-color);
    background: white;
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.08);
}

.msg-search-input::placeholder {
    color: var(--text-secondary);
}

/* Conversations List */
.msg-conversations {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
}

.msg-conversation {
    display: flex;
    align-items: center;
    padding: 1rem 1.5rem;
    cursor: pointer;
    border-bottom: 1px solid rgba(0, 0, 0, 0.04);
    transition: all 0.2s ease;
    position: relative;
}

.msg-conversation:hover {
    background: rgba(99, 102, 241, 0.04);
}

.msg-conversation.active {
    background: rgba(99, 102, 241, 0.08);
    border-left: 3px solid var(--primary-color);
}

.msg-conversation.active::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: var(--primary-color);
}

.msg-conversation-avatar {
    position: relative;
    flex-shrink: 0;
    margin-right: 1rem;
}

.msg-conversation-avatar i {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-hover) 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.375rem;
}

.msg-conversation-status {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 14px;
    height: 14px;
    border: 3px solid var(--card-background);
    border-radius: 50%;
    background: #cbd5e1;
}

.msg-conversation-status.online {
    background: #22c55e;
}

.msg-conversation-info {
    flex: 1;
    min-width: 0;
}

.msg-conversation-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.375rem;
}

.msg-conversation-name {
    font-weight: 600;
    font-size: 0.9375rem;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msg-conversation-time {
    font-size: 0.75rem;
    color: var(--text-secondary);
    flex-shrink: 0;
    margin-left: 0.5rem;
}

.msg-conversation-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.msg-conversation-preview {
    font-size: 0.875rem;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}

.msg-conversation.unread .msg-conversation-preview {
    color: var(--text-primary);
    font-weight: 500;
}

.msg-conversation-badge {
    flex-shrink: 0;
    background: var(--primary-color);
    color: white;
    font-size: 0.6875rem;
    font-weight: 600;
    padding: 0.25rem 0.5rem;
    border-radius: 10px;
    min-width: 20px;
    text-align: center;
    margin-left: 0.5rem;
}

/* =====================================================
   MAIN CHAT AREA
   ===================================================== */

.msg-main {
    display: flex;
    flex-direction: column;
    background: var(--background);
    position: relative;
}

/* Empty State */
.msg-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 3rem 2rem;
}

.msg-empty-content {
    text-align: center;
    max-width: 400px;
}

.msg-empty-icon {
    margin-bottom: 1.5rem;
}

.msg-empty-icon i {
    font-size: 5rem;
    color: var(--primary-color);
    opacity: 0.2;
}

.msg-empty-content h3 {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0 0 0.75rem 0;
}

.msg-empty-content p {
    font-size: 1rem;
    color: var(--text-secondary);
    margin: 0 0 2rem 0;
    line-height: 1.6;
}

/* Active Chat Container */
.msg-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Chat Header */
.msg-chat-header {
    padding: 1.25rem 1.5rem;
    background: var(--card-background);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.msg-btn-back {
    display: none;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary-color);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    margin-right: 1rem;
}

.msg-btn-back:hover {
    background: rgba(99, 102, 241, 0.15);
    transform: scale(1.05);
}

.msg-chat-user {
    display: flex;
    align-items: center;
    flex: 1;
}

.msg-avatar {
    position: relative;
    margin-right: 1rem;
}

.msg-avatar i {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-hover) 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
}

.msg-avatar-status {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 14px;
    height: 14px;
    border: 3px solid var(--card-background);
    border-radius: 50%;
    background: #cbd5e1;
}

.msg-avatar-status.online {
    background: #22c55e;
}

.msg-chat-user-info h3 {
    margin: 0;
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-primary);
}

.msg-chat-status {
    font-size: 0.8125rem;
    color: var(--text-secondary);
    margin-top: 0.125rem;
}

.msg-chat-status.online {
    color: #22c55e;
}

.msg-chat-actions {
    display: flex;
    gap: 0.5rem;
}

/* Chat Messages Area */
.msg-chat-messages {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 1.5rem;
    background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
    display: flex;
    flex-direction: column;
}

.msg-message {
    display: flex;
    margin-bottom: 1rem;
    animation: msgFadeIn 0.3s ease;
    flex-shrink: 0;
}

.msg-message.sent {
    justify-content: flex-end;
}

.msg-message.received {
    justify-content: flex-start;
}

.msg-bubble {
    max-width: 70%;
    padding: 0.875rem 1.125rem;
    border-radius: 18px;
    position: relative;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    overflow: hidden;
}

.msg-message.sent .msg-bubble {
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-hover) 100%);
    color: white;
    border-bottom-right-radius: 4px;
}

.msg-message.received .msg-bubble {
    background: white;
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-bottom-left-radius: 4px;
}

.msg-bubble-text {
    font-size: 0.9375rem;
    line-height: 1.5;
    margin: 0;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
}

.msg-bubble-meta {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.375rem;
    font-size: 0.6875rem;
}

.msg-message.sent .msg-bubble-meta {
    justify-content: flex-end;
    color: rgba(255, 255, 255, 0.7);
}

.msg-message.received .msg-bubble-meta {
    color: var(--text-secondary);
}

.msg-bubble-time {
    white-space: nowrap;
}

.msg-bubble-status {
    display: flex;
    align-items: center;
}

.msg-bubble-status i {
    font-size: 0.75rem;
}

.msg-bubble-status.read {
    color: #22c55e;
}

/* Typing Indicator */
.msg-typing {
    padding: 0.5rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
}

.msg-typing-dots {
    display: flex;
    gap: 3px;
}

.msg-typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--primary-color);
    animation: msgTypingBounce 1.4s infinite ease-in-out;
}

.msg-typing-dots span:nth-child(1) {
    animation-delay: 0s;
}

.msg-typing-dots span:nth-child(2) {
    animation-delay: 0.2s;
}

.msg-typing-dots span:nth-child(3) {
    animation-delay: 0.4s;
}

.msg-typing-text {
    font-size: 0.8125rem;
    color: var(--text-secondary);
    font-style: italic;
}

/* Chat Input Footer */
.msg-chat-footer {
    padding: 1.25rem 1.5rem;
    background: var(--card-background);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.msg-input-wrapper {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.msg-input {
    flex: 1;
    padding: 0.875rem 1.125rem;
    border: 2px solid var(--border-color);
    border-radius: 24px;
    font-size: 0.9375rem;
    background: rgba(248, 250, 252, 0.6);
    color: var(--text-primary);
    transition: all 0.3s ease;
    outline: none;
    font-family: inherit;
    resize: none;
    min-height: 48px;
}

.msg-input:focus {
    border-color: var(--primary-color);
    background: white;
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.08);
}

.msg-input::placeholder {
    color: var(--text-secondary);
}

.msg-btn-send {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: var(--primary-color);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    flex-shrink: 0;
}

.msg-btn-send:hover {
    background: var(--primary-hover);
    transform: scale(1.08);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.msg-btn-send:active {
    transform: scale(0.95);
}

.msg-btn-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.msg-btn-send i {
    font-size: 1.125rem;
}

/* =====================================================
   ANIMATIONS
   ===================================================== */

@keyframes msgFadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes msgTypingBounce {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.4;
    }
    30% {
        transform: translateY(-8px);
        opacity: 1;
    }
}

/* =====================================================
   SCROLLBAR STYLING
   ===================================================== */

.msg-conversations::-webkit-scrollbar,
.msg-chat-messages::-webkit-scrollbar {
    width: 6px;
}

.msg-conversations::-webkit-scrollbar-track,
.msg-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.msg-conversations::-webkit-scrollbar-thumb,
.msg-chat-messages::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

.msg-conversations::-webkit-scrollbar-thumb:hover,
.msg-chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* =====================================================
   RESPONSIVE - MOBILE
   ===================================================== */

@media (max-width: 768px) {
    /* CRITICAL: Messages tab needs special handling to prevent nested scroll issues */
    #messages-tab.tab-content {
        overflow: hidden;
        padding: 0;
        height: calc(100vh - 140px);
        min-height: unset;
    }

    .msg-layout {
        grid-template-columns: 1fr;
        height: 100%;
        max-height: none;
        border-radius: 0;
        position: relative;
    }

    .msg-sidebar {
        grid-column: 1;
        grid-row: 1;
        z-index: 2;
        transition: transform 0.3s ease;
        height: 100%;
        overflow-y: auto;
    }

    .msg-sidebar.hidden {
        transform: translateX(-100%);
    }

    .msg-main {
        grid-column: 1;
        grid-row: 1;
        z-index: 1;
        height: 100%;
        position: relative;
    }

    .msg-chat {
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        transform: translateX(100%);
        transition: transform 0.3s ease;
        background: var(--background);
        z-index: 3;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
    }

    .msg-chat.active {
        transform: translateX(0);
    }

    .msg-btn-back {
        display: flex;
    }

    .msg-sidebar-header {
        padding: 1.25rem;
    }

    .msg-sidebar-header h2 {
        font-size: 1.375rem;
    }

    .msg-search {
        padding: 0.875rem 1.25rem;
    }

    .msg-conversation {
        padding: 0.875rem 1.25rem;
    }

    .msg-conversation-avatar i {
        width: 48px;
        height: 48px;
        font-size: 1.25rem;
    }

    .msg-chat-header {
        padding: 1rem 1.25rem;
        flex-shrink: 0;
    }

    .msg-avatar i {
        width: 44px;
        height: 44px;
        font-size: 1.375rem;
    }

    .msg-chat-messages {
        padding: 1.25rem;
        flex: 1 1 0 !important;
        min-height: 0 !important;
        height: auto !important;
        max-height: none !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
    }

    .msg-chat-footer {
        flex-shrink: 0;
        padding: 1rem 1.25rem;
    }

    .msg-bubble {
        max-width: 85%;
        padding: 0.75rem 1rem;
    }

    .msg-bubble-text {
        font-size: 0.875rem;
    }

    .msg-input {
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 0.75rem 1rem;
        min-height: 44px;
    }

    .msg-btn-send {
        width: 44px;
        height: 44px;
    }

    .msg-btn-send i {
        font-size: 1rem;
    }
}

/* Tablet optimizations */
@media (max-width: 1024px) and (min-width: 769px) {
    .msg-layout {
        grid-template-columns: 320px 1fr;
    }

    .msg-sidebar-header {
        padding: 1.25rem;
    }

    .msg-conversation {
        padding: 0.875rem 1.25rem;
    }
}

/* =====================================================
   ACCESSIBILITY
   ===================================================== */

.msg-btn-icon:focus,
.msg-btn-back:focus,
.msg-btn-send:focus,
.msg-input:focus,
.msg-search-input:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

/* Reduce motion for accessibility */
@media (prefers-reduced-motion: reduce) {
    .msg-layout *,
    .msg-layout *::before,
    .msg-layout *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .msg-bubble {
        border: 2px solid currentColor;
    }

    .msg-conversation:hover,
    .msg-conversation.active {
        outline: 2px solid var(--primary-color);
    }
}
