/* =========================================
   1. GLOBAL VARIABLES & THEME
   ========================================= */
:root {
    /* Dimensions */
    --mobile-top-bar-height: 56px;
    --world-header-height: 60px;
    --mobile-bottom-nav-height: 60px;
    --input-bar-height: 70px;

    /* Colors */
    --surface-color: #ffffff;
    --surface-alt: #f8f9fa;
    --border-color: #e5e7eb;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --primary-color: #10B981;
    --incoming-bubble: #f3f4f6;
    --outgoing-bubble: #10B981;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

[data-theme="dark"] {
    --surface-color: #111827;
    --surface-alt: #1f2937;
    --border-color: #374151;
    --text-primary: #f9fafb;
    --text-secondary: #d1d5db;
    --incoming-bubble: #374151;
    --shadow-sm: none;
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.5);
}

/* =========================================
   2. DESKTOP LAYOUT (Default)
   ========================================= */
/* Ensure main content fills the area next to sidebar properly */
.main-content {
    display: flex;
    flex-direction: column;
    height: 100vh; /* Full viewport height */
    overflow: hidden; /* Prevent double scrollbars */
    background: var(--surface-color);
    position: relative;
}

/* The Wrapper for the whole chat interface */
.world-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    background: var(--surface-color);
}

/* --- Header (Desktop) --- */
.world-header {
    flex-shrink: 0;
    height: var(--world-header-height);
    padding: 0 24px;
    background: var(--surface-color);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: var(--shadow-sm);
    z-index: 10;
}

.world-header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

.online-status {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: var(--surface-alt);
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

/* Ensure links inside online-status inherit color properly */
.online-status a {
    color: inherit;
    text-decoration: none;
}

/* === FIX: Force White Text for Channels Button in Dark Mode === */
[data-theme="dark"] .online-status,
[data-theme="dark"] .online-status a {
    color: #ffffff !important;
}

.status-dot {
    width: 8px;
    height: 8px;
    background: #10B981;
    border-radius: 50%;
    box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.2);
    animation: pulse 2s infinite;
}

/* --- Messages Area (Desktop) --- */
.world-content {
    flex-grow: 1;
    position: relative;
    overflow: hidden;
    background: var(--surface-color);
}

.world-messages {
    height: 100%;
    overflow-y: auto;
    padding: 20px 40px; /* Generous padding on desktop */
    display: flex;
    flex-direction: column;
    gap: 4px;
    scroll-behavior: smooth;
}

/* --- Input Area (Desktop) --- */
.world-input {
    flex-shrink: 0;
    background: var(--surface-color);
    border-top: 1px solid var(--border-color);
    padding: 15px 40px;
    z-index: 20;
}

.input-container {
    display: flex;
    align-items: flex-end;
    background: var(--surface-alt);
    border-radius: 24px;
    padding: 8px 12px 8px 20px;
    border: 1px solid var(--border-color);
    max-width: 1000px;
    margin: 0 auto; /* Center input on big screens */
    box-shadow: var(--shadow-sm);
}

.textarea-wrapper {
    flex-grow: 1;
    margin-right: 12px;
}

textarea#world-message-input {
    width: 100%;
    background: transparent;
    border: none;
    resize: none;
    max-height: 120px;
    font-family: inherit;
    font-size: 0.95rem;
    color: var(--text-primary);
    padding: 4px 0;
}

textarea:focus { outline: none; }

.send-btn {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: var(--primary-color);
    color: white;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 5px rgba(16, 185, 129, 0.3);
}

.send-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(16, 185, 129, 0.4);
}

/* =========================================
   3. MESSAGE BUBBLE STYLES (Shared)
   ========================================= */
.world-message {
    display: flex;
    align-items: flex-end;
    margin-bottom: 8px;
    max-width: 100%;
}

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
}

.message-content {
    display: flex;
    flex-direction: column;
    max-width: 65%; /* Constrain width for readability */
}

.message-header {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-bottom: 2px;
    margin-left: 12px;
    font-weight: 500;
}

.message-bubble {
    padding: 10px 16px;
    border-radius: 18px;
    font-size: 0.95rem;
    line-height: 1.5;
    word-wrap: break-word;
    box-shadow: var(--shadow-sm);
    position: relative;
}

/* Incoming */
.world-message.incoming { justify-content: flex-start; }
.world-message.incoming .message-avatar { margin-right: 10px; }
.world-message.incoming .message-bubble {
    background: var(--incoming-bubble);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

/* Outgoing */
.world-message.outgoing {
    justify-content: flex-end;
    flex-direction: row-reverse;
}
.world-message.outgoing .message-avatar { margin-left: 10px; }
.world-message.outgoing .message-content { align-items: flex-end; }
.world-message.outgoing .message-header { margin-right: 12px; margin-left: 0; }
.world-message.outgoing .message-bubble {
    background: var(--outgoing-bubble);
    color: #ffffff;
    border-bottom-right-radius: 4px;
    text-align: left;
}

/* Message Meta (Time/Delete) */
.message-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 4px;
    font-size: 0.7rem;
    opacity: 0.7;
    float: right;
    margin-left: 10px;
    color: inherit;
}

.delete-msg-btn {
    background: none;
    border: none;
    color: inherit;
    cursor: pointer;
    padding: 0;
    opacity: 0.6;
}
.delete-msg-btn:hover { opacity: 1; }

/* =========================================
   4. MOBILE SPECIFIC STYLES (The Fix)
   ========================================= */
@media (max-width: 992px) {
    /* Hard Reset for Mobile Container */
    .main-content {
        margin: 0 !important;
        padding: 0 !important;
        height: 100vh !important;
        display: block !important; /* Stack elements naturally */
    }

    .world-container {
        display: block; /* Disable flex column, we use fixed positioning */
        height: 100%;
        width: 100%;
    }

    /* 1. TOP BAR (Logo/Hamburger) - Fixed */
    /* Assuming .mobile-top-bar exists in HTML or main.css */
    .mobile-top-bar {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: var(--mobile-top-bar-height);
        background: var(--surface-color);
        z-index: 1000;
        border-bottom: 1px solid var(--border-color);
        display: flex;
        align-items: center;
        padding: 0 16px;
    }

    /* 2. WORLD HEADER (Count/Channels) - Fixed BELOW Top Bar */
    .world-header {
        position: fixed;
        /* Sit exactly under the top bar */
        top: var(--mobile-top-bar-height); 
        left: 0;
        right: 0;
        height: var(--world-header-height);
        z-index: 999;
        background: var(--surface-color); /* Opaque bg */
        padding: 0 16px;
        box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        border-bottom: 1px solid var(--border-color);
    }

    /* 3. MESSAGES AREA - Scrollable Middle Zone */
    .world-messages {
        position: fixed;
        left: 0;
        right: 0;
        /* Start below both headers */
        top: calc(var(--mobile-top-bar-height) + var(--world-header-height));
        /* End above input + nav */
        bottom: calc(var(--input-bar-height) + var(--mobile-bottom-nav-height));
        padding: 15px 12px;
        overflow-y: auto;
        height: auto; /* Override desktop height:100% */
        width: auto;
    }
    
    .message-content { max-width: 80%; } /* Wider bubbles on mobile */

    /* 4. INPUT AREA - Fixed Above Bottom Nav */
    .world-input {
        position: fixed;
        left: 0;
        right: 0;
        bottom: var(--mobile-bottom-nav-height); /* Sit on top of nav */
        height: var(--input-bar-height);
        z-index: 1000;
        padding: 8px 12px;
        border-top: 1px solid var(--border-color);
    }

    .input-container {
        max-width: 100%;
        border-radius: 20px;
    }

    /* 5. BOTTOM NAV - Fixed Bottom */
    .mobile-bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: var(--mobile-bottom-nav-height);
        background: var(--surface-color);
        border-top: 1px solid var(--border-color);
        z-index: 1001;
        display: flex !important; /* Force show */
    }
}


/* =========================================
   5. PROFILE POPUP (Glassmorphism)
   ========================================= */
.profile-popup {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}
.profile-popup.active { display: flex; }
.profile-popup-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
}
.profile-popup-card {
    position: relative;
    background: var(--surface-color);
    width: 90%;
    max-width: 350px;
    border-radius: 24px;
    padding: 30px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    text-align: center;
    animation: popupPop 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
@keyframes popupPop {
    from { transform: scale(0.9); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}
.profile-avatar {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    border: 4px solid var(--primary-color);
    margin-bottom: 15px;
    object-fit: cover;
}
.profile-name {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
}
.profile-popup-close {
    position: absolute;
    top: 15px;
    right: 15px;
    background: transparent;
    border: none;
    font-size: 1.2rem;
    color: var(--text-secondary);
    cursor: pointer;
}
.profile-popup.loading .profile-popup-card::after {
    content: '';
    position: absolute; top: 50%; left: 50%;
    width: 30px; height: 30px; margin: -15px;
    border: 3px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* =========================================
   6. SCROLLBARS
   ========================================= */
.world-messages::-webkit-scrollbar { width: 6px; }
.world-messages::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.2); border-radius: 3px; }
.world-messages::-webkit-scrollbar-track { background: transparent; }

.profile-meta {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    margin-top: 14px;
}
.profile-bio {
    grid-column: 1 / -1; /* span full width */
}


/* =========================================
   7. WORLD GATEWAY (Selection Screen)
   ========================================= */
.world-gateway {
    position: absolute;
    inset: 0;
    z-index: 50; /* Above chat but below header/nav */
    background: var(--surface-color);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.gateway-content {
    text-align: center;
    max-width: 800px;
    width: 100%;
}

.gateway-title {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 10px;
}

.gateway-subtitle {
    color: var(--text-secondary);
    margin-bottom: 40px;
    font-size: 1.1rem;
}

.gateway-cards {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}

.gateway-card {
    background: var(--surface-alt);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 30px;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    overflow: hidden;
}

.gateway-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 30px rgba(0,0,0,0.1);
    border-color: var(--primary-color);
}

.card-icon {
    width: 70px;
    height: 70px;
    background: rgba(16, 185, 129, 0.1);
    color: var(--primary-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 30px;
    margin-bottom: 20px;
    transition: 0.3s;
}

.gateway-card:hover .card-icon {
    background: var(--primary-color);
    color: white;
}

.gateway-card h2 {
    font-size: 1.5rem;
    color: var(--text-primary);
    margin-bottom: 10px;
}

.gateway-card p {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 25px;
    line-height: 1.5;
}

.btn-enter {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 10px 24px;
    border-radius: 50px;
    font-weight: 600;
    cursor: pointer;
    transition: 0.2s;
}

/* Mobile Responsiveness for Gateway */
@media (max-width: 768px) {
    .gateway-cards {
        grid-template-columns: 1fr;
        gap: 15px;
    }
    .gateway-title { font-size: 1.8rem; }
    .world-gateway { padding-bottom: 80px; /* Space for bottom nav */ }
}