/* Performance Optimizations and Smooth Transitions */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Optimize rendering for smoother animations */
.mobile-tab,
.gesture-btn,
#sendBtn,
#enterAgoraBtn,
.color-option,
.user-item {
    will-change: transform;
    backface-visibility: hidden;
}

/* Smooth scrolling for better UX */
html {
    scroll-behavior: smooth;
}

/* Focus styles for keyboard navigation */
button:focus-visible,
input:focus-visible,
textarea:focus-visible {
    outline: 2px solid #667eea;
    outline-offset: 2px;
}

/* Loading states for interactive elements */
.loading {
    opacity: 0.6;
    pointer-events: none;
}

.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid #667eea;
    border-radius: 50%;
    border-top-color: transparent;
    animation: spin 1s linear infinite;
}

html, body {
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    font-size: 14px;
    line-height: 1.42857143;
    color: #333;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    height: 100%;
    overflow: hidden;
    -webkit-text-size-adjust: 100%;
}

/* Mobile-first approach: allow scrolling and safe-area padding on all small screens */
@media (max-width: 1023px) {
    html, body {
        height: auto;
        min-height: 100vh;
        min-height: 100dvh;
        overflow: auto;
        padding-bottom: env(safe-area-inset-bottom);
    }
}

.hidden {
    display: none !important;
}

/* Loading Screen */
#loadingScreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.loading-content {
    text-align: center;
    color: white;
}

.loading-avatar {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    border: 4px solid white;
    margin-bottom: 20px;
    animation: float 3s ease-in-out infinite;
}

.loading-text {
    font-size: 24px;
    margin-bottom: 20px;
    font-weight: 300;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255,255,255,0.3);
    border-top: 4px solid white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto;
}

@keyframes float {
    0%, 100% { transform: translateY(0px); }
    50% { transform: translateY(-20px); }
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Modal Styles - Enhanced for Mobile */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.modal-content {
    background: white;
    padding: 30px;
    border-radius: 15px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    max-width: 500px;
    width: 90%;
    max-height: 90vh;
    max-height: 90svh;
    max-height: 90dvh;
    overflow-y: auto;
}

@media (max-width: 768px) {
    .modal-content {
        padding: 20px;
        width: 95%;
        max-width: none;
        margin: 20px;
        border-radius: 12px;
    }
}

.modal-content h2 {
    text-align: center;
    margin-bottom: 20px;
    color: #667eea;
    font-size: 28px;
}

@media (max-width: 768px) {
    .modal-content h2 {
        font-size: 24px;
        margin-bottom: 16px;
    }
}

.modal-content p {
    text-align: center;
    margin-bottom: 30px;
    color: #666;
    font-size: 16px;
}

@media (max-width: 768px) {
    .modal-content p {
        font-size: 14px;
        margin-bottom: 20px;
    }
}

.close {
    float: right;
    font-size: 28px;
    font-weight: bold;
    color: #aaa;
    cursor: pointer;
    line-height: 1;
}

.close:hover {
    color: #667eea;
}

/* Login Form - Enhanced for Mobile */
.login-form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

#nicknameInput {
    padding: 15px;
    font-size: 16px;
    border: 2px solid #ddd;
    border-radius: 8px;
    outline: none;
    transition: border-color 0.3s ease;
}

#nicknameInput:focus {
    border-color: #667eea;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

.appearance-selector h3 {
    margin-bottom: 15px;
    color: #333;
    font-size: 18px;
}

.color-selector {
    margin-bottom: 20px;
}

.color-selector label {
    display: block;
    margin-bottom: 12px;
    font-weight: 500;
    font-size: 14px;
}

.color-options {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    justify-content: center;
}

.color-option {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    cursor: pointer;
    border: 3px solid transparent;
    transition: all 0.3s ease;
    position: relative;
}

.color-option:hover,
.color-option.selected {
    border-color: #333;
    transform: scale(1.1);
}

.color-option:active {
    transform: scale(0.95);
}

/* Add accessibility indicator for selected color */
.color-option.selected::after {
    content: '✓';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-weight: bold;
    font-size: 16px;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}

#enterAgoraBtn {
    padding: 16px 30px;
    font-size: 18px;
    background: linear-gradient(45deg, #667eea, #764ba2);
    color: white;
    border: none;
    border-radius: 25px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-weight: 500;
    min-height: 50px;
}

#enterAgoraBtn:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

#enterAgoraBtn:active {
    transform: translateY(0);
}

@media (max-width: 768px) {
    .color-options {
        gap: 10px;
    }
    
    .color-option {
        width: 40px;
        height: 40px;
    }
    
    #enterAgoraBtn {
        padding: 14px 24px;
        font-size: 16px;
    }
}

/* Main Metaverse Interface - Complete Rewrite for Better Responsiveness */
#metaverse {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 10px;
    gap: 10px;
}

/* Desktop Layout: Show all elements, override mobile hiding */
@media (min-width: 1024px) {
    #metaverse {
        display: grid;
        height: 100vh;
        height: 100dvh;
        grid-template-areas: 
            "hud hud"
            "world chat"
            "world users"
            "world gestures";
        grid-template-rows: auto 2fr 1fr 0.8fr;
        grid-template-columns: 1fr 380px;
        gap: 12px;
        padding: 12px;
        overflow: hidden;
    }

    #topHUD {
        grid-area: hud;
    }

    #worldContainer {
        grid-area: world;
    }

    #chatInterface {
        grid-area: chat;
        display: flex !important; /* Override mobile hiding */
        height: auto;
        max-height: none;
        min-height: 0;
    }

    #usersList {
        grid-area: users;
        display: block !important; /* Override mobile hiding */
        position: static;
        width: auto;
        height: auto;
    }

    #gestureControls {
        grid-area: gestures;
        display: block !important; /* Override mobile hiding */
        position: static;
        width: auto;
        height: auto;
    }

    /* Hide mobile navigation on desktop */
    #mobileNav {
        display: none !important;
    }
}

/* Very large screens: give more space to chat and optimize layout */
@media (min-width: 1440px) {
    #metaverse {
        grid-template-columns: 1fr 450px;
        gap: 16px;
        padding: 16px;
    }
    
    #topHUD {
        padding: 18px 24px;
    }
    
    #topHUD h1 {
        font-size: 26px;
    }
}

/* Ultra-wide screens: cap the maximum width for better readability */
@media (min-width: 1920px) {
    #metaverse {
        max-width: 1800px;
        margin: 0 auto;
        grid-template-columns: 1fr 480px;
    }
}

/* High-DPI displays: ensure crisp rendering */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .color-option,
    .user-avatar,
    .loading-avatar {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* Top HUD - Responsive Design */
#topHUD {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    flex-shrink: 0;
}

/* Mobile HUD adjustments */
@media (max-width: 768px) {
    #topHUD {
        flex-direction: column;
        padding: 12px 15px;
        gap: 10px;
        text-align: center;
    }

    .hud-section {
        flex-wrap: wrap;
        justify-content: center;
    }

    #topHUD h1 {
        font-size: 20px;
    }
}

@media (min-width: 1024px) {
    #topHUD {
        grid-area: hud;
    }
}

#topHUD h1 {
    color: #667eea;
    font-size: 24px;
    margin: 0;
}

#userCount {
    color: #666;
    font-size: 14px;
}

.hud-section {
    display: flex;
    align-items: center;
    gap: 15px;
}

#topHUD button {
    padding: 8px 16px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.3s ease;
}

#topHUD button:hover {
    background: #764ba2;
    transform: translateY(-1px);
}

/* 3D World Container - Responsive Design */
#worldContainer {
    background: #87CEEB;
    border-radius: 10px;
    overflow: hidden;
    position: relative;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    min-height: 250px;
    height: 40vh;
    max-height: 400px;
    flex-shrink: 0;
}

/* Desktop world container */
@media (min-width: 1024px) {
    #worldContainer {
        grid-area: world;
        height: auto;
        min-height: 300px;
        max-height: none;
        flex: 1;
    }
}

/* Mobile world adjustments */
@media (max-width: 768px) {
    #worldContainer {
        min-height: 200px;
        height: 35vh;
        max-height: 300px;
    }
}

#worldCanvas {
    width: 100%;
    height: 100%;
    display: block;
}

/* Users List - Responsive Design */
#usersList {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    padding: 15px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    min-height: 150px;
    max-height: 300px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 10px;
}

/* Mobile: hide by default, show via JS tabs */
@media (max-width: 1023px) {
    #usersList {
        display: none;
        max-height: 40vh;
        margin-bottom: 0;
    }

    #usersList.active {
        display: block;
    }
}

#usersList h3 {
    color: #667eea;
    margin-bottom: 15px;
    font-size: 16px;
}

.user-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px;
    margin-bottom: 5px;
    border-radius: 8px;
    transition: background 0.3s ease;
}

.user-item:hover {
    background: rgba(102, 126, 234, 0.1);
}

.user-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-size: cover;
    background-position: center;
    border: 2px solid;
    flex-shrink: 0;
}

.user-info {
    flex: 1;
}

.user-nickname {
    font-weight: 500;
    color: #333;
    font-size: 14px;
}

.user-status {
    font-size: 12px;
    color: #666;
}

/* Chat Interface - Complete Redesign for All Screen Sizes */
#chatInterface {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    min-height: 200px;
    height: 50vh;
    max-height: 600px;
}

/* Desktop chat: controlled sizing in sidebar */
@media (min-width: 1024px) {
    #chatInterface {
        height: auto;
        max-height: none;
        min-height: 300px;
    }
}

/* Mobile: hide by default, show via JS tabs */
@media (max-width: 1023px) {
    #chatInterface {
        display: none;
        height: 60vh;
        min-height: 250px;
        max-height: 70vh;
    }

    #chatInterface.active {
        display: flex;
    }
}

#chatMessages {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    border-bottom: 1px solid #eee;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
    min-height: 0;
    scrollbar-width: thin;
    scrollbar-color: #ccc #f5f5f5;
}

/* Webkit scrollbar styling */
#chatMessages::-webkit-scrollbar {
    width: 6px;
}

#chatMessages::-webkit-scrollbar-track {
    background: #f5f5f5;
}

#chatMessages::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 3px;
}

#chatMessages::-webkit-scrollbar-thumb:hover {
    background: #999;
}

.chat-message {
    margin-bottom: 10px;
    padding: 8px 12px;
    border-radius: 8px;
    background: rgba(102, 126, 234, 0.1);
    border-left: 3px solid #667eea;
}

.chat-message .nickname {
    font-weight: 500;
    color: #667eea;
    margin-right: 8px;
}

.chat-message .timestamp {
    font-size: 11px;
    color: #999;
    float: right;
}

.chat-message .text {
    color: #333;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    word-break: break-word;
}

/* Chat Input Area - Fixed for All Screen Sizes */
#chatInput {
    display: flex;
    padding: 15px;
    gap: 10px;
    background: rgba(255, 255, 255, 0.98);
    border-top: 1px solid #eee;
    flex-shrink: 0;
    align-items: flex-end;
}

@media (max-width: 768px) {
    #chatInput {
        padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
    }
}

#messageInput {
    flex: 1;
    padding: 12px 15px;
    border: 1px solid #ddd;
    border-radius: 8px;
    outline: none;
    font-size: 14px;
    font-family: inherit;
    min-height: 44px;
    max-height: 120px;
    resize: none;
    line-height: 1.4;
    transition: border-color 0.3s ease;
}

#messageInput:focus {
    border-color: #667eea;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

#sendBtn {
    padding: 12px 24px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.3s ease;
    min-height: 44px;
    flex-shrink: 0;
}

#sendBtn:hover {
    background: #764ba2;
    transform: translateY(-1px);
    box-shadow: 0 3px 10px rgba(102, 126, 234, 0.3);
}

#sendBtn:active {
    transform: translateY(0);
}

/* Gesture Controls - Responsive Design */
#gestureControls {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    padding: 15px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    max-height: 250px;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: thin;
    scrollbar-color: #888 #f1f1f1;
    -webkit-overflow-scrolling: touch;
}

/* Mobile: hide by default, show via JS tabs */
@media (max-width: 1023px) {
    #gestureControls {
        display: none;
        max-height: 40vh;
    }

    #gestureControls.active {
        display: block;
    }
}

/* Webkit browsers scrollbar styling */
#gestureControls::-webkit-scrollbar {
    width: 8px;
}

#gestureControls::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
}

#gestureControls::-webkit-scrollbar-track {
    background: #f1f1f1;
}

/* Mobile Navigation - Improved Responsive Design */
#mobileNav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: none;
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(10px);
    box-shadow: 0 -4px 12px rgba(0,0,0,0.08);
    border-top: 1px solid rgba(0,0,0,0.06);
    padding: 10px env(safe-area-inset-right) calc(10px + env(safe-area-inset-bottom)) env(safe-area-inset-left);
    gap: 8px;
    z-index: 1002;
}

@media (max-width: 1023px) {
    #mobileNav {
        display: flex;
    }
    
    /* Add bottom padding to metaverse container to account for mobile nav */
    #metaverse {
        padding-bottom: calc(70px + env(safe-area-inset-bottom));
    }
}

.mobile-tab {
    flex: 1;
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    color: #495057;
    border-radius: 12px;
    padding: 12px 8px;
    font-size: 13px;
    font-weight: 600;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s ease;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.mobile-tab.active {
    background: linear-gradient(45deg, #667eea, #764ba2);
    color: white;
    border-color: transparent;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.mobile-tab:hover:not(.active) {
    background: #e9ecef;
    border-color: #667eea;
}

/* Small-screen panel switching and layout adjustments */
@media (max-width: 1023px) {
    /* Default: show chat, hide others; JS toggles with .active class */
    #chatInterface:not(.active),
    #usersList:not(.active),
    #gestureControls:not(.active) {
        display: none !important;
    }

    /* Show active panel */
    #chatInterface.active,
    #usersList.active,
    #gestureControls.active {
        display: flex !important;
    }

    /* Ensure active panels use flex layout correctly */
    #usersList.active,
    #gestureControls.active {
        display: block !important;
    }
}

/* Gesture Controls Content */
#gestureControls h4 {
    color: #667eea;
    margin-bottom: 15px;
    font-size: 14px;
}

.gesture-btn {
    display: block;
    width: 100%;
    padding: 12px;
    margin-bottom: 8px;
    background: linear-gradient(45deg, #667eea, #764ba2);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.3s ease;
    min-height: 44px;
}

.gesture-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 3px 10px rgba(102, 126, 234, 0.3);
}

.gesture-btn:active {
    animation: pulse 0.3s ease;
    transform: translateY(0);
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(0.95); }
    100% { transform: scale(1); }
}

/* Rooms Modal */
.rooms-section {
    margin-bottom: 25px;
    padding-bottom: 20px;
    border-bottom: 1px solid #eee;
}

.rooms-section:last-child {
    border-bottom: none;
}

.rooms-section h3 {
    color: #667eea;
    margin-bottom: 15px;
    font-size: 18px;
}

#roomsList {
    max-height: 200px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.room-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    margin-bottom: 8px;
    background: #f8f9fa;
    border-radius: 8px;
    transition: background 0.3s ease;
    border: 1px solid #e9ecef;
}

.room-item:hover {
    background: rgba(102, 126, 234, 0.1);
    border-color: rgba(102, 126, 234, 0.2);
}

.room-info {
    flex: 1;
}

.room-name {
    font-weight: 500;
    color: #333;
    margin-bottom: 2px;
}

.room-users {
    font-size: 12px;
    color: #666;
}

.join-room-btn {
    padding: 8px 16px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    transition: all 0.3s ease;
    min-height: 36px;
}

.join-room-btn:hover {
    background: #764ba2;
    transform: translateY(-1px);
}

#roomNameInput,
#maxUsersInput {
    width: 100%;
    padding: 12px;
    margin-bottom: 12px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-size: 14px;
    transition: border-color 0.3s ease;
}

#roomNameInput:focus,
#maxUsersInput:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

#createRoomBtn {
    width: 100%;
    padding: 12px;
    background: linear-gradient(45deg, #667eea, #764ba2);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 16px;
    font-weight: 500;
    transition: all 0.3s ease;
    min-height: 44px;
}

#createRoomBtn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}
@media (max-width: 480px) {
    #topHUD h1 { 
        font-size: 18px; 
    }
    
    #enterAgoraBtn, 
    #topHUD button { 
        padding: 12px 16px; 
        min-height: 44px; 
        font-size: 14px;
    }
    
    .modal-content { 
        padding: 16px; 
        margin: 10px;
    }
    
    #chatInput {
        padding: 10px;
        gap: 8px;
    }
    
    #messageInput {
        font-size: 16px; /* Prevents zoom on iOS */
    }
}

/* Add sidebar container to DOM via CSS for desktop layout - Removed as not needed with grid areas */

/* Responsive Design tweaks for extra-small screens */