/* Professional Aurora Theme */
:root {
    --primary: #4f7cff;      /* Indigo Blue */
    --primary-hover: #3f6be8;
    --bg-main: #eef3ff;      /* Soft blue-gray */
    --bg-card: #FFFFFF;
    --border: #d9e3f7;
    --text-main: #2f3b55;
    --text-muted: #7082a6;
    --online: #31c48d;       /* Green online indicator */
    --sent-bg: #4f7cff;
    --received-bg: #f3f6ff;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Quicksand', sans-serif;
}

body {
    height: 100vh;
    width: 100vw;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #e8eeff 0%, #f6f9ff 45%, #eef7ff 100%);
    background-image: 
        radial-gradient(rgba(79, 124, 255, 0.08) 1px, transparent 1px),
        radial-gradient(rgba(109, 149, 255, 0.08) 1px, transparent 1px);
    background-position: 0 0, 25px 25px;
    background-size: 50px 50px;
    color: var(--text-main);
    overflow: hidden;
    position: relative;
    perspective: 1200px;
}

/* Soft decorative gradients */
body::before, body::after {
    content: '';
    position: absolute;
    width: 400px;
    height: 400px;
    border-radius: 50%;
    filter: blur(30px);
    opacity: 0.25;
    z-index: 0;
    pointer-events: none;
}

body::before {
    top: -120px;
    left: -100px;
    background: radial-gradient(circle, rgba(79,124,255,0.6) 0%, rgba(79,124,255,0) 70%);
}

body::after {
    bottom: -140px;
    right: -110px;
    background: radial-gradient(circle, rgba(49,196,141,0.45) 0%, rgba(49,196,141,0) 70%);
}

.bg-3d-scene {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    overflow: hidden;
}

.orb {
    position: absolute;
    border-radius: 50%;
    transform-style: preserve-3d;
    animation: floatOrb 9s ease-in-out infinite;
}

.orb-1 {
    width: 230px;
    height: 230px;
    top: 12%;
    left: 8%;
    background: radial-gradient(circle at 30% 30%, #ffffff, #84a7ff 65%, #5e84f0 100%);
    box-shadow: inset -20px -25px 45px rgba(0, 0, 0, 0.18), 0 40px 80px rgba(79, 124, 255, 0.28);
    animation-delay: 0.2s;
}

.orb-2 {
    width: 170px;
    height: 170px;
    top: 68%;
    right: 11%;
    background: radial-gradient(circle at 35% 30%, #ffffff, #8ce9c2 60%, #43c996 100%);
    box-shadow: inset -16px -20px 38px rgba(0, 0, 0, 0.15), 0 32px 65px rgba(49, 196, 141, 0.26);
    animation-delay: 1.1s;
}

.orb-3 {
    width: 120px;
    height: 120px;
    bottom: 18%;
    left: 18%;
    background: radial-gradient(circle at 30% 30%, #ffffff, #bbcbff 58%, #7f9bf2 100%);
    box-shadow: inset -10px -14px 28px rgba(0, 0, 0, 0.14), 0 20px 45px rgba(79, 124, 255, 0.22);
    animation-delay: 2s;
}

.ring {
    position: absolute;
    border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.65);
    box-shadow: 0 18px 36px rgba(79, 124, 255, 0.12);
    transform: rotateX(62deg) rotateZ(-20deg);
}

.ring-1 {
    width: 360px;
    height: 170px;
    right: -60px;
    top: 16%;
}

.ring-2 {
    width: 290px;
    height: 145px;
    left: -90px;
    bottom: 10%;
    transform: rotateX(65deg) rotateZ(22deg);
}

.app-container {
    width: 95%;
    max-width: 1100px;
    height: 85vh;
    background: rgba(255, 255, 255, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.68);
    border-radius: 22px;
    box-shadow: 0 26px 50px -20px rgba(31, 52, 103, 0.4);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    overflow: hidden;
    display: flex;
    z-index: 1;
    transform: rotateX(1.3deg);
}

/* Screen Toggle Logic */
.screen {
    display: none;
    width: 100%;
    height: 100%;
}

.screen.active {
    display: flex;
}

/* Join Screen Styles */
#join-screen {
    justify-content: center;
    align-items: center;
    text-align: center;
    flex-direction: column;
}

.card {
    position: relative;
    padding: 4rem;
    width: 100%;
    max-width: 500px;
    background: linear-gradient(155deg, rgba(255,255,255,0.95), rgba(246,250,255,0.85));
    border-radius: 20px;
    border: 1px solid rgba(255,255,255,0.9);
    box-shadow: 0 22px 42px rgba(26, 44, 89, 0.18);
    overflow: hidden;
}

.card-glow {
    position: absolute;
    width: 240px;
    height: 240px;
    right: -70px;
    top: -110px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(79,124,255,0.24) 0%, rgba(79,124,255,0) 70%);
    pointer-events: none;
}

.brand-badge {
    display: inline-block;
    margin-bottom: 0.8rem;
    padding: 0.4rem 0.8rem;
    border-radius: 999px;
    background: #edf2ff;
    color: var(--primary);
    font-weight: 700;
    font-size: 0.82rem;
    letter-spacing: 0.03em;
    text-transform: uppercase;
}

h1 {
    font-size: 2.5rem;
    margin-bottom: 0.5rem;
    font-weight: 700;
    color: #2f3b55;
}

p {
    color: var(--text-muted);
    margin-bottom: 2.5rem;
    font-size: 1.1rem;
    font-weight: 500;
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

input {
    padding: 1.25rem;
    background: #FAFBFC;
    border: 2px solid var(--border);
    border-radius: 20px;
    font-size: 1.1rem;
    color: var(--text-main);
    outline: none;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

select {
    padding: 1rem 1.1rem;
    background: #FAFBFC;
    border: 2px solid var(--border);
    border-radius: 14px;
    font-size: 1rem;
    color: var(--text-main);
    outline: none;
    cursor: pointer;
}

input:focus {
    border-color: var(--primary);
    background: white;
    box-shadow: 0 0 0 4px rgba(137, 207, 240, 0.2);
}

select:focus {
    border-color: var(--primary);
    background: white;
    box-shadow: 0 0 0 4px rgba(137, 207, 240, 0.2);
}

button {
    padding: 1.25rem 2rem;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 20px;
    font-weight: 700;
    font-size: 1.1rem;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 4px 10px rgba(137, 207, 240, 0.4);
}

button:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 6px 15px rgba(137, 207, 240, 0.5);
}

/* Chat Screen Layout */
.sidebar {
    width: 280px;
    background: linear-gradient(180deg, rgba(247,250,255,0.95), rgba(240,246,255,0.92));
    border-right: 1px solid rgba(214, 226, 252, 0.8);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 2rem 1.5rem;
    border-bottom: 2px solid var(--border);
    background: white;
}

.sidebar-header h3 {
    font-size: 1.2rem;
    color: #2f3b55;
}

.sidebar-subtitle {
    margin-top: 0.35rem;
    display: block;
    font-size: 0.82rem;
    color: var(--text-muted);
}

#user-list {
    list-style: none;
    padding: 1.5rem 1rem;
    overflow-y: auto;
    flex: 1;
}

#user-list li {
    padding: 0.8rem 1rem;
    margin-bottom: 0.5rem;
    background: white;
    border-radius: 15px;
    font-size: 1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    box-shadow: 0 2px 5px rgba(0,0,0,0.02);
    transition: transform 0.2s;
}

#user-list li:hover {
    transform: translateX(4px);
}

#user-list li::before {
    content: '';
    width: 10px;
    height: 10px;
    background: var(--online);
    border-radius: 50%;
    box-shadow: 0 0 5px var(--online);
}

.main-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: rgba(255, 255, 255, 0.78);
}

.chat-header {
    padding: 1.2rem 2rem;
    border-bottom: 2px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: white;
}

.chat-header h2 {
    color: var(--primary);
}

.chat-title-wrap {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.chat-subtitle {
    color: var(--text-muted);
    font-size: 0.84rem;
    font-weight: 600;
}

.header-actions {
    display: flex;
    gap: 0.5rem;
}

.stats-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    padding: 0.7rem 2rem;
    border-bottom: 1px solid var(--border);
    background: #f8faff;
    font-size: 0.9rem;
    color: var(--text-main);
}

.search-bar {
    padding: 0.7rem 2rem;
    border-bottom: 1px solid var(--border);
    background: #ffffff;
}

#search-input {
    width: 100%;
    padding: 0.8rem 1rem;
    border-radius: 14px;
    border: 1px solid var(--border);
    font-size: 0.95rem;
    background: #f8faff;
}

#search-input:focus {
    box-shadow: 0 0 0 3px rgba(79, 124, 255, 0.15);
}

/* THE FIX: Ensure messages container scrolls correctly */
.messages {
    flex: 1;
    padding: 2rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    min-height: 0; /* Important trick for flex child scrolling */
}

.typing-indicator {
    min-height: 26px;
    padding: 0.2rem 2rem 0.7rem;
    color: var(--text-muted);
    font-size: 0.88rem;
    font-style: italic;
}

/* Message Bubbles */
.message {
    max-width: 75%;
    padding: 1rem 1.5rem;
    border-radius: 24px; /* Very bubbly */
    font-size: 1.05rem;
    font-weight: 500;
    line-height: 1.5;
    position: relative;
    word-wrap: break-word; /* Allows long text to break safely */
    overflow-wrap: break-word;
    box-shadow: 0 10px 20px rgba(34, 56, 104, 0.12);
    flex-shrink: 0; /* Prevents flexbox from hiding the message */
    width: fit-content;
}

.message.sent {
    align-self: flex-end;
    background: linear-gradient(140deg, #5c86ff, #4f7cff 55%, #3f66dd);
    color: white;
    border-bottom-right-radius: 4px;
}

.message.received {
    align-self: flex-start;
    background: linear-gradient(155deg, #ffffff, #edf3ff);
    color: var(--text-main);
    border-bottom-left-radius: 4px;
}

.message .user {
    font-size: 0.8rem;
    font-weight: 700;
    margin-bottom: 0.3rem;
    display: block;
    opacity: 0.9;
    color: #4f7cff;
}

.message.sent .user {
    color: white; /* Keep white for sent */
}

.message .time {
    font-size: 0.75rem;
    opacity: 0.6;
    margin-top: 0.5rem;
    display: block;
    text-align: right;
    font-weight: 600;
}

.status-msg {
    text-align: center;
    font-size: 0.9rem;
    color: var(--text-muted);
    font-style: italic;
    font-weight: 600;
    margin: 1rem 0;
    background: #FDF9FB;
    padding: 0.5rem 1rem;
    border-radius: 20px;
    align-self: center;
}

/* Input Area */
.chat-input-area {
    padding: 1.5rem 2rem;
    border-top: 2px solid var(--border);
    display: flex;
    gap: 1rem;
    background: white;
}

#message-input {
    flex: 1;
    background: #FAFBFC;
    border-radius: 24px;
}

#send-btn {
    padding: 0;
    width: 55px;
    height: 55px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 10px 24px rgba(79, 124, 255, 0.42);
}

#send-btn svg {
    transform: translateX(1px);
}

#leave-btn {
    background: #edf2ff;
    color: #4f7cff;
    padding: 0.6rem 1.2rem;
    font-size: 0.9rem;
    border-radius: 15px;
    box-shadow: none;
}

#leave-btn:hover {
    background: #4f7cff;
    color: white;
    transform: none;
    box-shadow: none;
}

#refresh-stats-btn {
    background: #edf2ff;
    color: #334262;
    padding: 0.6rem 1rem;
    font-size: 0.85rem;
    border-radius: 12px;
    box-shadow: none;
}

#refresh-stats-btn:hover {
    background: #dce7ff;
    transform: none;
    box-shadow: none;
}

#clear-history-btn {
    background: #ffecef;
    color: #b24a62;
    padding: 0.6rem 1rem;
    font-size: 0.85rem;
    border-radius: 12px;
    box-shadow: none;
}

#clear-history-btn:hover {
    background: #ffdce4;
    transform: none;
    box-shadow: none;
}

@keyframes floatOrb {
    0%, 100% {
        transform: translateY(0px) translateZ(0px);
    }
    50% {
        transform: translateY(-18px) translateZ(30px);
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: #E8EBF2;
    border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--primary);
}

/* Mobile */
@media (max-width: 768px) {
    .bg-3d-scene {
        opacity: 0.45;
    }
    .sidebar { display: none; }
    .app-container {
        width: 100vw;
        height: 100vh;
        border-radius: 0;
        border: none;
        transform: none;
    }
    .chat-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.8rem;
    }
    .header-actions {
        width: 100%;
        flex-wrap: wrap;
    }
    .stats-bar {
        flex-direction: column;
        align-items: flex-start;
    }
}
