:root {
    --bg-color: #0e1621;
    --sidebar-bg: #17212b;
    --chat-bg: #17212b;
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.05);
    --accent-color: #38bdf8;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --message-own: #2a779e;
    --message-other: #212d3b;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

/* Custom Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.2);
}

body {
    font-family: 'Inter', -apple-system, sans-serif;
    background: var(--bg-color);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    display: flex;
    justify-content: center;
}

.glass {
    border: 1px solid var(--glass-border);
    border-radius: 0;
}

/* Auth Pages */
.auth-container {
    display: flex; justify-content: center; align-items: center; min-height: 100vh; width: 100vw;
    background: url('https://images.unsplash.com/photo-1618005182384-a83a8bd57fbe?q=80&w=2564&auto=format&fit=crop') no-repeat center center/cover;
    position: relative;
    padding: 20px;
}
.auth-container::before { content: ''; position: absolute; inset: 0; background: rgba(15, 23, 42, 0.8); }
.auth-card { 
    position: relative; 
    width: 100%; 
    max-width: 400px; 
    padding: 30px 25px; 
    z-index: 1; 
    border-radius: 16px; 
    background: rgba(23, 33, 43, 0.95); 
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
    text-align: center;
}

@media (max-width: 480px) {
    .auth-card {
        padding: 25px 20px;
    }
    .auth-card h1 {
        font-size: 2rem;
    }
}

.auth-card h1 {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 8px;
    color: var(--accent-color);
}

.auth-card .subtitle {
    color: var(--text-secondary);
    margin-bottom: 30px;
    font-size: 1rem;
}

.auth-card form {
    text-align: left;
}

.auth-card .input-group {
    margin-bottom: 20px;
}

.auth-card button[type="submit"] {
    width: 100%;
    margin-top: 10px;
    padding: 14px;
    font-size: 1rem;
}

.auth-footer {
    margin-top: 25px;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.auth-footer a {
    color: var(--accent-color);
    text-decoration: none;
    font-weight: 600;
    transition: 0.2s;
}

.auth-footer a:hover {
    text-decoration: underline;
}

/* Main Layout */
#app { 
    display: flex; 
    height: 100vh; 
    width: 100%; 
    max-width: 1200px; 
    gap: 0; 
    background: var(--sidebar-bg);
}

/* Sidebar */
#sidebar { 
    width: 320px; 
    display: flex; 
    flex-direction: column; 
    overflow: hidden; 
    background: var(--sidebar-bg);
    border-right: 1px solid var(--glass-border);
}

#search-bar { padding: 20px 20px 20px; }
#chats-list { flex: 1; overflow-y: auto; padding: 0; }

.chat-item { 
    padding: 15px 20px; 
    cursor: pointer; 
    transition: 0.2s; 
    display: flex; 
    justify-content: space-between; 
    align-items: center;
    position: relative;
}
.chat-item:hover { background: rgba(255, 255, 255, 0.03); }
.chat-item.active { background: #2b5278; }
.chat-item.pinned { background: rgba(56, 189, 248, 0.05); }

.chat-item-info { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
.chat-item-name { font-weight: 600; font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-username { font-size: 0.8rem; color: var(--text-secondary); }
.chat-item-last-msg { 
    font-size: 0.85rem; 
    color: var(--text-secondary); 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    margin-top: 2px;
}

.unread-dot { width: 10px; height: 10px; background: var(--accent-color); border-radius: 50%; box-shadow: 0 0 10px var(--accent-color); margin-left: 10px; flex-shrink: 0; }

#chat-context-menu, #message-context-menu {
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(10px);
    background: rgba(23, 33, 43, 0.95);
    overflow: hidden;
    animation: fadeIn 0.1s ease-out;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-5px); }
    to { opacity: 1; transform: translateY(0); }
}

.search-results-title {
    padding: 15px 20px 5px;
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--accent-color);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Avatar Styles */
#current-avatar-container, .chat-item-avatar-container, .avatar-settings-preview-container {
    width: 40px;
    height: 40px;
    min-width: 40px;
    margin-right: 12px;
    flex-shrink: 0;
    position: relative;
}

.chat-item-avatar-container {
    width: 48px;
    height: 48px;
}

#current-avatar-container > img, #current-avatar-container > .avatar-placeholder,
.chat-item-avatar-container > img, .chat-item-avatar-container > .avatar-placeholder,
#chat-avatar, .chat-item-avatar, #settings-avatar-preview, #chat-settings-avatar-preview, .avatar-placeholder {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 700;
    text-transform: uppercase;
    user-select: none;
    overflow: hidden;
    background: #2b5278;
}

#chat-avatar {
    width: 45px;
    height: 45px;
    margin-right: 15px;
}
#chat-avatar.hidden { display: none; }

#chat-header-info {
    display: flex;
    align-items: center;
}

.avatar-settings {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    margin-bottom: 20px;
}
#settings-avatar-preview, #chat-settings-avatar-preview {
    width: 80px !important;
    height: 80px !important;
    min-width: 80px !important;
    min-height: 80px !important;
    border: 2px solid var(--accent-color);
    border-radius: 50% !important;
    object-fit: cover !important;
    flex-shrink: 0;
}

#chat-members-list::-webkit-scrollbar {
    width: 4px;
}

#chat-members-list .member-item:hover {
    background: rgba(255, 255, 255, 0.05);
}

/* Status dots in list */
.status-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    position: absolute;
    right: -2px; 
    bottom: 2px;
    display: none;
    border: 2px solid var(--sidebar-bg);
    z-index: 10;
}
.status-dot.online { 
    background-color: #4ade80; 
    display: block; 
}
#current-avatar-container .status-dot {
    right: -1px;
    bottom: 0px;
}


/* Bottom User Info */
#user-info { 
    padding: 15px 20px; 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    background: rgba(0, 0, 0, 0.2);
    border-top: 1px solid var(--glass-border); 
}
#user-info-text { display: flex; flex-direction: column; overflow: hidden; flex: 1; }
#current-display-name { font-weight: 600; font-size: 0.9rem; color: var(--text-primary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#current-user { font-size: 0.75rem; color: var(--text-secondary); }
#user-actions { display: flex; gap: 15px; align-items: center; padding-left: 10px; }
.action-link { font-size: 1.2rem; color: var(--text-secondary); cursor: pointer; text-decoration: none; transition: 0.2s; }
.action-link:hover { color: var(--accent-color); }
.logout-link { font-size: 1.1rem; color: var(--text-secondary); cursor: pointer; text-decoration: none; transition: 0.2s; }
.logout-link:hover { color: #ef4444; }

/* Chat Window */
#chat-window { 
    flex: 1; 
    display: flex; 
    flex-direction: column; 
    overflow: hidden; 
    background: var(--chat-bg);
}
#chat-header { 
    position: relative;
    padding: 15px 20px; 
    border-bottom: 1px solid var(--glass-border); 
    background: var(--chat-bg); 
    min-height: 70px; 
    display: flex; 
    align-items: center; 
}
#chat-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

#chat-status { font-size: 0.8rem; margin-top: 2px; transition: color 0.2s; }
#chat-status.online, #chat-status.typing { color: var(--accent-color); }
#chat-status.offline { color: var(--text-secondary); }

#messages { 
    flex: 1; 
    padding: 20px; 
    overflow-y: auto; 
    display: flex; 
    flex-direction: column; 
    gap: 8px; 
    background: var(--chat-bg);
    position: relative;
}

.initial-message {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--text-secondary);
    font-size: 1.1rem;
    opacity: 0.7;
    text-align: center;
    width: 100%;
}

.message { max-width: 75%; padding: 8px 12px; border-radius: 12px; position: relative; box-shadow: 0 1px 2px rgba(0,0,0,0.2); }
.message.own { align-self: flex-end; background: var(--message-own); border-bottom-right-radius: 4px; }
.message:not(.own) { align-self: flex-start; background: var(--message-other); border-bottom-left-radius: 4px; }

.msg-meta { 
    display: flex; justify-content: flex-end; align-items: center; gap: 5px; 
    font-size: 0.65rem; margin-top: 4px; color: rgba(255, 255, 255, 0.6);
}
.time { opacity: 0.8; }
.status-icon { color: rgba(255, 255, 255, 0.5); font-weight: bold; }
.status-icon.read { color: #fff; }
.edited { font-style: italic; opacity: 0.6; margin-right: 3px; }

#typing-indicator { 
    padding: 5px 20px; 
    font-size: 0.8rem; 
    color: var(--text-secondary); 
    font-style: italic;
    height: 25px;
}
#typing-indicator.hidden { visibility: hidden; }

#message-input-container { padding: 20px; display: flex; gap: 12px; background: var(--chat-bg); border-top: 1px solid var(--glass-border); align-items: center; }
#send-button { 
    width: 44px; 
    height: 44px; 
    min-width: 44px; 
    padding: 0; 
    border-radius: 50%; 
    background: var(--message-own); 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1); 
}
#send-button:hover { background: #338dbb; transform: scale(1.05); }
#send-button svg { margin-left: -2px; margin-top: 1px; }

.icon-button { 
    background: transparent; 
    border: none; 
    color: var(--text-secondary); 
    cursor: pointer; 
    padding: 0 5px; 
    transition: 0.2s; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
}
.icon-button:hover { color: var(--accent-color); }

.message-image {
    max-width: 100%;
    max-height: 400px;
    border-radius: 8px;
    display: block;
    margin-bottom: 5px;
    cursor: pointer;
    transition: 0.2s;
}
.message-image:hover { opacity: 0.9; }

.message.own .message-image { border-bottom-right-radius: 0; }
.message:not(.own) .message-image { border-bottom-left-radius: 0; }


input { 
    width: 100%; 
    padding: 12px 16px; 
    background: rgba(255, 255, 255, 0.05); 
    border: 1px solid var(--glass-border); 
    border-radius: 8px; 
    color: white; 
    outline: none; 
    transition: 0.3s; 
}
input:focus { border-color: var(--accent-color); background: rgba(255, 255, 255, 0.1); }

button { 
    padding: 12px 24px; 
    background: var(--accent-color); 
    color: white; 
    border: none; 
    border-radius: 8px; 
    font-weight: 600; 
    cursor: pointer; 
    transition: 0.2s; 
}
button:hover { background: #0ea5e9; transform: translateY(-1px); }

/* Modal System */
.modal {
    position: fixed; 
    inset: 0; 
    background: rgba(0, 0, 0, 0.85);
    display: flex; 
    justify-content: center; 
    align-items: center; 
    z-index: 1000;
    opacity: 0; 
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.3s ease, visibility 0.3s;
    backdrop-filter: blur(5px);
}
.modal.active { 
    opacity: 1;
    visibility: visible;
    pointer-events: all;
}

.modal-content { 
    width: 90%; 
    max-width: 450px; 
    padding: 30px; 
    border-radius: 16px; 
    background: #1c2733; 
    transform: translateY(20px) scale(0.95);
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.3s ease;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
    opacity: 0;
}
.modal.active .modal-content {
    transform: translateY(0) scale(1);
    opacity: 1;
}

.modal-content h3 { margin-bottom: 20px; }
.input-group { margin-bottom: 15px; }
.input-group label { display: block; margin-bottom: 5px; font-size: 0.85rem; color: var(--text-secondary); }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }

.primary-action {
    background: var(--message-own) !important;
}
.primary-action:hover {
    background: #338dbb !important;
}

button.secondary { background: rgba(255, 255, 255, 0.1); }
button.secondary:hover { background: rgba(255, 255, 255, 0.15); }

/* Preview Modal Specific */
#preview-container {
    width: 100%;
    max-height: 300px;
    overflow: hidden;
    border-radius: 8px;
    margin-bottom: 15px;
    display: flex;
    justify-content: center;
    background: #000;
}
#preview-img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

/* Image Viewer (Lightbox) */
#image-viewer {
    background: rgba(0, 0, 0, 0.95);
    flex-direction: column;
}
.viewer-content {
    max-width: 90%;
    max-height: 80%;
    box-shadow: 0 0 30px rgba(0,0,0,0.5);
    border-radius: 4px;
    transform: scale(0.9);
    transition: transform 0.3s ease;
    object-fit: contain;
}
.modal.active .viewer-content {
    transform: scale(1);
}
.close-viewer {
    position: absolute;
    top: 20px;
    right: 30px;
    color: white;
    font-size: 40px;
    font-weight: bold;
    cursor: pointer;
    z-index: 1001;
}
#viewer-caption {
    margin-top: 20px;
    color: white;
    font-size: 1.1rem;
    background: rgba(0,0,0,0.5);
    padding: 10px 20px;
    border-radius: 20px;
    max-width: 80%;
    text-align: center;
}

/* Cropping Modal Styles */
.crop-container {
    width: 100%;
    height: 350px;
    background: #000;
    position: relative;
    overflow: hidden;
    border-radius: 8px;
    margin-bottom: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: grab;
}
.crop-container:active { cursor: grabbing; }

#crop-target {
    position: absolute;
    max-width: none;
    user-select: none;
    pointer-events: none;
}
.crop-overlay {
    position: absolute;
    width: 250px;
    height: 250px;
    border: 2px solid var(--accent-color);
    border-radius: 50%;
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.7);
    pointer-events: none;
    z-index: 10;
}

.error-message { background: rgba(239, 68, 68, 0.1); color: #ef4444; padding: 10px; border-radius: 8px; font-size: 0.85rem; margin-bottom: 15px; border: 1px solid rgba(239, 68, 68, 0.2); display: none; }

/* Animations */
@keyframes slideUpFade {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Responsive Design */
@media (max-width: 768px) {
    #app {
        flex-direction: row;
        width: 100vw;
        height: 100vh;
        overflow: hidden;
        position: relative;
    }

    #sidebar {
        width: 100%;
        position: absolute;
        inset: 0;
        z-index: 20;
        transition: transform 0.3s ease;
        background: var(--sidebar-bg);
    }

    #chat-window {
        width: 100%;
        position: absolute;
        inset: 0;
        z-index: 10;
        transform: translateX(100%);
        transition: transform 0.3s ease;
    }

    #app.chat-active #sidebar {
        transform: translateX(-100%);
    }

    #app.chat-active #chat-window {
        transform: translateX(0);
        z-index: 25;
    }

    #chat-header {
        padding-left: 50px; /* Space for back button */
    }

    .back-button {
        display: flex;
        position: absolute;
        left: 15px;
        top: 50%;
        transform: translateY(-50%);
        z-index: 30;
        background: transparent;
        border: none;
        color: var(--text-primary);
        padding: 8px;
        cursor: pointer;
        transition: color 0.2s, background 0.2s;
        border-radius: 50%;
    }

    .back-button:hover {
        background: rgba(255, 255, 255, 0.1);
        color: var(--accent-color);
        transform: translateY(-50%); /* Maintain vertical centering without downward shift */
    }

    #chat-header-info {
        padding-left: 10px;
    }
}

@media (min-width: 769px) {
    .back-button {
        display: none;
    }
}

.new-message {
    animation: slideUpFade 0.2s ease-out forwards;
}

@keyframes pulse {
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(56, 189, 248, 0.7); }
    70% { transform: scale(1.2); box-shadow: 0 0 0 10px rgba(56, 189, 248, 0); }
    100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(56, 189, 248, 0); }
}

.pulse {
    animation: pulse 0.5s ease-in-out;
}
