/* --- Messages Page Layout --- */
#messages-page-container {
    display: grid;
    grid-template-columns: 340px 1fr;
    grid-template-rows: 1fr;
    height: 100%;
    width: 100%;
    gap: 0;
    padding: 0 !important; /* Override page-container padding */
    position: relative;
    overflow: hidden;
    background-color: rgba(0,0,0,0.2);
}

#conversations-list-container {
    background-color: rgba(0,0,0,0.5);
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
    border-right: 1px solid var(--border-base);
    display: flex;
    flex-direction: column;
    height: 100%;
    transition: transform 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    will-change: transform;
}

#active-chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    background: transparent;
    position: relative;
    transition: transform 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    will-change: transform;
}


#messages-welcome-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    font-family: 'Share Tech Mono', monospace;
    color: var(--text-muted);
    padding: 2rem;
    position: absolute;
    inset: 0;
    background-color: rgba(0,0,0,0.3);
    z-index: 5;
}
#messages-welcome-screen i {
    font-size: 5rem;
    color: var(--theme-primary-color);
    opacity: 0.3;
    margin-bottom: 1.5rem;
    animation: welcome-icon-float 5s ease-in-out infinite;
    text-shadow: 0 0 25px var(--theme-primary-color);
}
@keyframes welcome-icon-float {
    0%, 100% { transform: translateY(0) scale(1); }
    50% { transform: translateY(-15px) scale(1.05); }
}

/* --- Conversations List --- */
.conversations-list-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-base);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}
.conversations-list-header h3 {
    font-family: 'Share Tech Mono', monospace;
    font-size: 1.5rem;
    color: var(--theme-primary-color);
    text-shadow: 0 0 10px var(--theme-primary-color);
}

#new-conversation-btn {
    background: none;
    border: 2px solid var(--theme-primary-color);
    color: var(--theme-primary-color);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    text-shadow: 0 0 5px var(--theme-primary-color);
}
#new-conversation-btn:hover {
    background-color: var(--theme-primary-color);
    color: #000;
    transform: rotate(180deg) scale(1.1);
    box-shadow: 0 0 15px var(--theme-primary-color);
}

#conversations-list {
    overflow-y: auto;
    flex-grow: 1;
}

.conversation-list-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1.25rem 1.5rem;
    cursor: pointer;
    border-bottom: 1px solid var(--border-base);
    transition: background-color 0.3s ease, border-left-color 0.3s ease;
    position: relative;
    border-left: 4px solid transparent;
}
.conversation-list-item:hover {
    background-color: rgba(var(--theme-primary-rgb), 0.1);
    border-left-color: rgba(var(--theme-primary-rgb), 0.5);
}
.conversation-list-item.active {
    background-color: rgba(var(--theme-primary-rgb), 0.15);
    border-left-color: var(--theme-primary-color);
}
.conversation-list-item.unread {
    background-color: rgba(var(--theme-primary-rgb), 0.05);
}
.conversation-list-item.unread .conversation-name {
    font-weight: bold;
    color: var(--text-base);
}
.conversation-list-item.unread .conversation-last-message {
    color: var(--text-muted);
    font-weight: 500;
}

.conversation-avatar .avatar {
    width: 55px;
    height: 55px;
    border: 2px solid var(--border-base);
}
.conversation-details {
    flex-grow: 1;
    min-width: 0;
}
.conversation-name {
    font-weight: 600;
    color: var(--text-base);
    font-size: 1.05em;
}
.conversation-last-message {
    font-size: 0.9em;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 0.25rem;
}
.conversation-last-message .fa-reply {
    margin-right: 4px;
    color: var(--theme-primary-color);
    opacity: 0.8;
}
.conversation-meta {
    text-align: right;
    font-size: 0.8em;
    color: var(--text-muted);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.5rem;
}
.unread-indicator {
    background-color: var(--theme-primary-color);
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 2px solid #000;
    box-shadow: 0 0 10px var(--theme-primary-color);
    animation: pulse-badge 1.5s infinite;
}

/* --- Active Chat --- */
.active-chat-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-base);
    display: flex;
    align-items: center;
    gap: 1rem;
    background-color: rgba(0,0,0,0.6);
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
    z-index: 10;
    animation: fade-in-down 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards;
}
@keyframes fade-in-down {
    from { opacity: 0; transform: translateY(-30px); }
    to { opacity: 1; transform: translateY(0); }
}

.chat-avatar .avatar {
    width: 50px;
    height: 50px;
    border: 2px solid var(--theme-primary-color);
}
.chat-info .chat-name {
    font-weight: 700;
    font-size: 1.2em;
    color: var(--text-base);
}
.chat-info .chat-status {
    font-size: 0.85em;
    color: var(--text-muted);
    font-family: 'Share Tech Mono', monospace;
    height: 1.2em;
    transition: all 0.3s ease;
}
.online-indicator {
    color: var(--theme-primary-color);
    text-shadow: 0 0 8px var(--theme-primary-color);
}
.typing-indicator {
    color: var(--theme-primary-color);
}
.typing-indicator span {
    display: inline-block;
    animation: typing-dots 1.4s infinite;
    animation-fill-mode: both;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-dots {
    0% { opacity: 0.2; transform: translateY(0); }
    20% { opacity: 1; transform: translateY(-3px); }
    40%, 100% { opacity: 0.2; transform: translateY(0); }
}


#messages-container {
    flex-grow: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
}

.messages-inner-container {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-top: auto; /* Push content to the bottom */
}

.chat-message-item {
    display: flex;
    flex-direction: column;
    max-width: 80%;
    animation: message-pop-in 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    animation-delay: calc(var(--animation-order) * 50ms);
    opacity: 0;
}
@keyframes message-pop-in {
    from { transform: scale(0.8) translateY(25px); opacity: 0; }
    to { transform: scale(1) translateY(0); opacity: 1; }
}

.chat-message-item.own {
    align-self: flex-end;
    align-items: flex-end;
}
.chat-message-item.other {
    align-self: flex-start;
    align-items: flex-start;
}

.message-bubble-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 0.75rem;
    margin-top: 0.25rem;
    position: relative;
}
.chat-message-item.own .message-bubble-wrapper {
    flex-direction: row-reverse;
}

.message-author-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    overflow: hidden;
    border: 2px solid var(--border-base);
}
.message-author-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.message-bubble {
    padding: 0.85rem 1.25rem;
    border-radius: 24px;
    word-break: break-word;
    position: relative;
    color: var(--text-base);
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}

.chat-message-item.own .message-bubble {
    background: linear-gradient(45deg, var(--theme-primary-color), color-mix(in srgb, var(--theme-primary-color) 70%, #0ea5e9));
    color: #000;
    font-weight: 500;
    border-bottom-right-radius: 6px;
}
.chat-message-item.other .message-bubble {
    background-color: var(--bg-base-300);
    border-bottom-left-radius: 6px;
}

.message-meta {
    font-size: 0.75em;
    color: var(--text-muted);
    padding: 0 0.25rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    opacity: 0;
    transition: opacity 0.3s ease;
    height: 16px;
    margin-top: 0.25rem;
}
.chat-message-item:hover .message-meta {
    opacity: 1;
}

.delete-message-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    transition: color 0.3s, transform 0.3s;
    cursor: pointer;
    font-size: 0.9em;
    padding: 0.5rem;
}
.delete-message-btn:hover {
    color: var(--red);
    transform: scale(1.1);
}


#chat-input-area {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border-base);
    background-color: rgba(0,0,0,0.5);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    animation: fade-in-up 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards;
    z-index: 5;
}
@keyframes fade-in-up {
    from { opacity: 0; transform: translateY(30px); }
    to { opacity: 1; transform: translateY(0); }
}

#chat-input-form {
    display: flex;
    align-items: center;
    gap: 1rem;
    background-color: var(--bg-base-200);
    border: 1px solid var(--border-base);
    border-radius: 25px;
    padding: 0.5rem 0.5rem 0.5rem 1.25rem;
    transition: all 0.3s ease;
}
#chat-input-form:focus-within {
    border-color: var(--theme-primary-color);
    box-shadow: 0 0 15px rgba(var(--theme-primary-rgb), 0.3);
}

#chat-message-input {
    flex-grow: 1;
    background-color: transparent;
    border: none;
    color: var(--text-base);
    resize: none;
    max-height: 120px;
    overflow-y: auto;
    font-family: 'Inter', sans-serif;
    padding: 0.5rem 0;
}
#chat-message-input:focus {
    outline: none;
}

#send-message-btn {
    background: var(--theme-primary-color);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    color: #000;
    font-size: 1.2rem;
    flex-shrink: 0;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    border: none;
    box-shadow: 0 0 10px rgba(var(--theme-primary-rgb), 0.5);
}
#send-message-btn:hover {
    transform: scale(1.15) rotate(-30deg);
    box-shadow: 0 0 20px var(--theme-primary-color);
}
#send-message-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: scale(1);
    background-color: var(--text-muted);
    box-shadow: none;
}

/* New Conversation Modal */
.new-conversation-header {
    font-family: 'Share Tech Mono', monospace;
    font-size: 1.5rem;
    color: var(--theme-primary-color);
    margin-bottom: 1.5rem;
    text-align: center;
}
#user-search-input {
    width: 100%;
    padding: 0.75rem 1rem;
    background-color: var(--bg-base-200);
    border: 1px solid var(--border-base);
    border-radius: 6px;
    color: var(--text-base);
    margin-bottom: 1rem;
}
#selected-users-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1rem;
    min-height: 40px;
}
.selected-user-pill {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background-color: var(--theme-primary-color);
    color: #000;
    padding: 0.25rem 0.75rem;
    border-radius: 16px;
    font-size: 0.9em;
    font-weight: 500;
    animation: message-pop-in 0.3s;
}
.selected-user-pill button {
    background: none;
    border: none;
    color: rgba(0,0,0,0.7);
    font-size: 1rem;
    cursor: pointer;
}
#user-search-results {
    max-height: 250px;
    overflow-y: auto;
    border-top: 1px solid var(--border-base);
    border-bottom: 1px solid var(--border-base);
}
.user-search-result-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem;
    cursor: pointer;
    transition: background-color 0.2s ease;
}
.user-search-result-item:hover {
    background-color: rgba(var(--theme-primary-rgb), 0.1);
}
.user-search-result-item.disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

#create-conversation-btn {
    width: 100%;
    padding: 0.75rem;
    background-color: var(--theme-primary-color);
    color: #000;
    font-weight: bold;
    font-family: 'Share Tech Mono', monospace;
    border-radius: 6px;
    margin-top: 1.5rem;
    transition: all 0.2s ease;
    border: none;
}
#create-conversation-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Responsive Overrides */
@media (max-width: 767px) {
    #messages-page-container {
        grid-template-columns: 100% 100%;
    }
    #conversations-list-container, #active-chat-container {
        grid-row: 1;
        grid-column: 1;
        width: 100%;
        background-color: var(--bg-base-100); /* Give them a solid bg on mobile */
    }

    #active-chat-container {
        transform: translateX(100%);
    }

    #messages-page-container.show-chat #conversations-list-container {
        transform: translateX(-100%);
    }
    #messages-page-container.show-chat #active-chat-container {
        transform: translateX(0);
    }
    
    .active-chat-header {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }
    #back-to-conversations-btn {
        display: block;
        margin-right: 0.5rem;
        background: none;
        border: none;
        color: var(--text-base);
        font-size: 1.2rem;
    }

     #chat-input-area, #messages-container {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .chat-message-item {
        max-width: 90%;
    }
}