* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    height: 100vh;
}

.screen {
    width: 100%;
    height: 100%;
}

.hidden {
    display: none !important;
}

/* Стили окна входа */
.login-box {
    background: white;
    padding: 40px;
    border-radius: 15px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    text-align: center;
    max-width: 400px;
    margin: 0 auto;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

.login-box h1 {
    margin-bottom: 30px;
    color: #333;
}

.login-box input {
    width: 100%;
    padding: 12px;
    margin-bottom: 15px;
    border: 2px solid #ddd;
    border-radius: 8px;
    font-size: 16px;
}

.login-box button {
    width: 100%;
    padding: 12px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.3s;
}

.login-box button:hover {
    background: #764ba2;
}

.error {
    color: #ff6b6b;
    margin-top: 10px;
    min-height: 20px;
}

/* Стили основного чата */
#chatScreen {
    display: flex;
    flex-direction: column;
}

.header {
    background: white;
    padding: 15px 20px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    min-height: 70px;
}

.header h2 {
    font-size: 20px;
    color: #333;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 15px;
    font-size: 14px;
    color: #666;
}

.user-info button {
    padding: 8px 16px;
    background: #ff6b6b;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 14px;
    white-space: nowrap;
}

.chat-container {
    display: flex;
    flex: 1;
    height: calc(100vh - 70px);
}

.users-sidebar {
    width: 250px;
    background: #f8f9fa;
    padding: 20px;
    border-right: 1px solid #ddd;
    overflow-y: auto;
    flex-shrink: 0;
}

.users-sidebar h3 {
    margin-bottom: 15px;
    color: #333;
    font-size: 16px;
}

.user-item {
    padding: 12px;
    margin-bottom: 8px;
    background: white;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.3s;
    border-left: 4px solid #667eea;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.user-item:hover {
    background: #e9ecef;
}

.user-item.active {
    background: #667eea;
    color: white;
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0; /* Предотвращает переполнение */
}

.chat-header {
    padding: 15px 20px;
    background: white;
    border-bottom: 1px solid #ddd;
    font-weight: bold;
    font-size: 16px;
}

.messages {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    background: white;
}

.message {
    margin-bottom: 15px;
    padding: 10px 15px;
    border-radius: 10px;
    max-width: 70%;
    word-wrap: break-word;
}

.message.own {
    background: #667eea;
    color: white;
    margin-left: auto;
}

.message.other {
    background: #f1f3f4;
    color: #333;
}

.message-header {
    font-size: 12px;
    opacity: 0.7;
    margin-bottom: 5px;
}

.message-image {
    max-width: 300px;
    max-height: 300px;
    border-radius: 8px;
    margin-top: 5px;
}

.message-input {
    padding: 20px;
    background: #f8f9fa;
    border-top: 1px solid #ddd;
    display: flex;
    gap: 10px;
    align-items: center;
}

.message-input input {
    flex: 1;
    padding: 12px;
    border: 1px solid #ddd;
    border-radius: 25px;
    font-size: 16px;
    min-width: 0; /* Предотвращает переполнение */
}

.message-input button {
    padding: 12px 20px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 25px;
    cursor: pointer;
    transition: background 0.3s;
    flex-shrink: 0;
}

.message-input button:hover {
    background: #764ba2;
}

/* Темная тема */
[data-theme="dark"] {
    --bg-primary: #1a1a1a;
    --bg-secondary: #2d2d2d;
    --bg-tertiary: #3d3d3d;
    --text-primary: #ffffff;
    --text-secondary: #cccccc;
    --accent-color: #667eea;
    --border-color: #444444;
}

/* Светлая тема (по умолчанию) */
[data-theme="light"] {
    --bg-primary: #ffffff;
    --bg-secondary: #f8f9fa;
    --bg-tertiary: #e9ecef;
    --text-primary: #333333;
    --text-secondary: #666666;
    --accent-color: #667eea;
    --border-color: #dddddd;
}

/* Применяем CSS переменные */
body {
    background: linear-gradient(135deg, var(--accent-color) 0%, #764ba2 100%);
    color: var(--text-primary);
    transition: all 0.3s ease;
}

.login-box {
    background: var(--bg-primary);
    color: var(--text-primary);
}

.header {
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
}

.header h2 {
    color: var(--text-primary);
}

.user-info {
    color: var(--text-secondary);
}

.users-sidebar {
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
}

.users-sidebar h3 {
    color: var(--text-primary);
}

.user-item {
    background: var(--bg-primary);
    color: var(--text-primary);
    border-left: 4px solid var(--accent-color);
}

.user-item:hover {
    background: var(--bg-tertiary);
}

.user-item.active {
    background: var(--accent-color);
    color: white;
}

.chat-header {
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
    color: var(--text-primary);
}

.messages {
    background: var(--bg-primary);
}

.message.own {
    background: var(--accent-color);
    color: white;
}

.message.other {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.message-input {
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
}

.message-input input {
    background: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

/* Переключатель темы - ИСПРАВЛЕНА ПОЗИЦИЯ */
.theme-switcher {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 1000;
}

.theme-btn {
    background: var(--bg-primary);
    border: 2px solid var(--border-color);
    border-radius: 50%;
    width: 50px;
    height: 50px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    transition: all 0.3s ease;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.theme-btn:hover {
    transform: scale(1.1);
}

/* Кнопка звонка в списке контактов */
.call-btn {
    background: var(--accent-color);
    color: white;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
    margin-left: 8px;
}

.call-btn:hover {
    background: #764ba2;
    transform: scale(1.05);
}

/* Окно звонка */
.call-screen {
    background: var(--bg-primary);
    padding: 40px;
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    text-align: center;
    max-width: 400px;
    margin: 0 auto;
    position: relative;
    transform: translateY(-50%);
}

.caller-info h2 {
    margin-bottom: 10px;
    color: var(--text-primary);
}

.caller-info p {
    font-size: 18px;
    color: var(--text-secondary);
    margin-bottom: 30px;
}

.call-info h3 {
    margin-bottom: 10px;
    color: var(--text-primary);
}

#callTimer {
    font-size: 24px;
    font-weight: bold;
    color: var(--accent-color);
    margin-bottom: 30px;
}

/* Кнопки управления звонком */
.call-controls {
    display: flex;
    gap: 20px;
    justify-content: center;
}

.btn-accept, .btn-reject, .btn-mute, .btn-end {
    padding: 15px 25px;
    border: none;
    border-radius: 50%;
    font-size: 24px;
    cursor: pointer;
    transition: all 0.3s ease;
    width: 70px;
    height: 70px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-accept {
    background: #4CAF50;
    color: white;
}

.btn-reject {
    background: #f44336;
    color: white;
}

.btn-mute {
    background: #ff9800;
    color: white;
}

.btn-end {
    background: #f44336;
    color: white;
}

.btn-accept:hover, .btn-reject:hover, .btn-mute:hover, .btn-end:hover {
    transform: scale(1.1);
}

/* Видео элементы */
video {
    max-width: 100%;
    border-radius: 10px;
    background: #000;
}

#localVideo {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 200px;
    height: 150px;
}

#remoteVideo {
    width: 100%;
    max-height: 400px;
}

/* =============================== */
/* МОБИЛЬНАЯ АДАПТАЦИЯ - ИСПРАВЛЕНА */
/* =============================== */

@media (max-width: 768px) {
    /* Исправляем переключатель темы для мобилок */
    .theme-switcher {
        top: 15px;
        right: 15px;
    }
    
    .theme-btn {
        width: 44px;
        height: 44px;
        font-size: 18px;
    }
    
    /* Исправляем хедер для мобилок */
    .header {
        padding: 12px 15px;
        min-height: 60px;
        flex-wrap: wrap;
        gap: 10px;
    }
    
    .header h2 {
        font-size: 18px;
        order: 1;
        flex: 1;
        text-align: left;
    }
    
    .user-info {
        order: 3;
        width: 100%;
        justify-content: space-between;
        margin-top: 5px;
    }
    
    .user-info button {
        padding: 6px 12px;
        font-size: 12px;
    }
    
    /* Исправляем контейнер чата */
    .chat-container {
        flex-direction: column;
        height: calc(100vh - 60px);
    }
    
    /* Исправляем сайдбар пользователей */
    .users-sidebar {
        width: 100%;
        height: 140px;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
        overflow-x: auto;
        padding: 15px;
        flex-shrink: 0;
    }
    
    .users-sidebar h3 {
        display: none;
    }
    
    #usersList {
        display: flex;
        overflow-x: auto;
        gap: 12px;
        padding-bottom: 5px;
        height: 100%;
        align-items: center;
    }
    
    .user-item {
        min-width: 100px;
        text-align: center;
        padding: 10px 8px;
        margin-bottom: 0;
        border-radius: 12px;
        border-left: none;
        border-bottom: 3px solid var(--accent-color);
        flex-direction: column;
        gap: 5px;
        height: 80px;
        justify-content: center;
    }
    
    .user-item .call-btn {
        margin-left: 0;
        width: 32px;
        height: 32px;
        font-size: 13px;
    }
    
    /* Исправляем основную область чата */
    .chat-main {
        flex: 1;
        min-height: 0;
    }
    
    .chat-header {
        padding: 12px 15px;
        font-size: 16px;
        background: var(--bg-secondary);
    }
    
    .message {
        max-width: 90%;
        padding: 12px 15px;
        margin-bottom: 12px;
        border-radius: 18px;
    }
    
    .message.own {
        border-bottom-right-radius: 8px;
    }
    
    .message.other {
        border-bottom-left-radius: 8px;
    }
    
    .message-header {
        font-size: 11px;
        margin-bottom: 4px;
    }
    
    .message-text {
        font-size: 15px;
        line-height: 1.4;
    }
    
    .message-input {
        padding: 15px;
        gap: 8px;
    }
    
    .message-input input {
        padding: 14px;
        font-size: 16px;
        border-radius: 25px;
    }
    
    .message-input button {
        padding: 14px 18px;
        border-radius: 25px;
        font-size: 18px;
    }
    
    /* Улучшаем скролл на iOS */
    .messages {
        -webkit-overflow-scrolling: touch;
        padding: 15px;
    }
    
    /* Скрываем скроллбар на мобилках */
    .users-sidebar::-webkit-scrollbar {
        display: none;
    }
    
    #usersList::-webkit-scrollbar {
        display: none;
    }
    
    /* Адаптируем окно звонка для мобилок */
    .call-screen {
        margin: 20px;
        padding: 30px 20px;
        max-width: none;
    }
    
    .call-controls {
        gap: 15px;
    }
    
    .btn-accept, .btn-reject, .btn-mute, .btn-end {
        width: 60px;
        height: 60px;
        font-size: 20px;
    }
}

/* Для очень маленьких экранов */
@media (max-width: 480px) {
    .login-box {
        margin: 15px;
        padding: 30px 20px;
        border-radius: 20px;
    }
    
    .header {
        padding: 10px 12px;
        gap: 8px;
    }
    
    .header h2 {
        font-size: 16px;
    }
    
    .user-info {
        font-size: 12px;
    }
    
    .user-info button {
        padding: 5px 10px;
        font-size: 11px;
    }
    
    .message-input {
        padding: 12px;
        gap: 6px;
    }
    
    .message-input input {
        padding: 12px;
        font-size: 14px;
    }
    
    .message-input button {
        padding: 12px 16px;
        font-size: 16px;
    }
    
    .users-sidebar {
        height: 110px;
        padding: 12px;
    }
    
    .user-item {
        min-width: 85px;
        padding: 8px 6px;
        font-size: 13px;
        height: 70px;
    }
    
    .theme-switcher {
        top: 12px;
        right: 12px;
    }
    
    .theme-btn {
        width: 40px;
        height: 40px;
        font-size: 16px;
    }
    
    /* Еще более компактные кнопки звонка на очень маленьких экранах */
    .call-controls {
        gap: 10px;
    }
    
    .btn-accept, .btn-reject, .btn-mute, .btn-end {
        width: 55px;
        height: 55px;
        font-size: 18px;
    }
}

/* Для горизонтальной ориентации на мобильных */
@media (max-width: 768px) and (orientation: landscape) {
    .users-sidebar {
        height: 100px;
    }
    
    .user-item {
        min-width: 90px;
        height: 70px;
        padding: 6px 4px;
    }
    
    .chat-container {
        height: calc(100vh - 60px);
    }
}

/* Исправление для десктопной версии - чтобы кнопка выхода не перекрывалась */
@media (min-width: 769px) {
    .header {
        padding-right: 80px; /* Оставляем место для переключателя темы */
    }
    
    .user-info {
        margin-right: 10px;
    }
}

/* Гарантируем, что переключатель темы всегда поверх других элементов */
.theme-switcher {
    z-index: 10000;
}

/* Гарантируем, что кнопка выхода всегда доступна */
.user-info button {
    position: relative;
    z-index: 1;
}

/* Полноэкранный режим для звонков */
body.call-active {
    background: var(--bg-primary) !important;
}

body.call-active .theme-switcher {
    display: none;
}

/* Улучшаем экраны звонков для полноэкранного режима */
#incomingCallScreen,
#activeCallScreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--bg-primary);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.call-screen {
    background: var(--bg-secondary);
    padding: 40px;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    text-align: center;
    max-width: 500px;
    width: 90%;
    border: 2px solid var(--accent-color);
}

/* Информация о звонке */
.caller-info h2 {
    margin-bottom: 15px;
    color: var(--text-primary);
    font-size: 28px;
}

.caller-info p {
    font-size: 22px;
    color: var(--accent-color);
    margin-bottom: 40px;
    font-weight: bold;
}

.call-info h3 {
    margin-bottom: 15px;
    color: var(--text-primary);
    font-size: 24px;
}

#callTimer {
    font-size: 32px;
    font-weight: bold;
    color: var(--accent-color);
    margin-bottom: 40px;
    font-family: 'Courier New', monospace;
}

/* Улучшаем кнопки управления звонком */
.call-controls {
    display: flex;
    gap: 30px;
    justify-content: center;
    flex-wrap: wrap;
}

.btn-accept, .btn-reject, .btn-mute, .btn-end {
    padding: 20px;
    border: none;
    border-radius: 50%;
    font-size: 28px;
    cursor: pointer;
    transition: all 0.3s ease;
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8px 20px rgba(0,0,0,0.2);
}

.btn-accept {
    background: #4CAF50;
    color: white;
}

.btn-reject {
    background: #f44336;
    color: white;
}

.btn-mute {
    background: #ff9800;
    color: white;
}

.btn-end {
    background: #f44336;
    color: white;
}

.btn-accept:hover, .btn-reject:hover, .btn-mute:hover, .btn-end:hover {
    transform: scale(1.15);
    box-shadow: 0 12px 25px rgba(0,0,0,0.3);
}

/* Анимация пульсации для входящего звонка */
@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

#incomingCallScreen .call-screen {
    animation: pulse 2s infinite;
}

/* Статус звонка */
.call-status {
    margin-top: 20px;
    font-size: 16px;
    color: var(--text-secondary);
}

/* Индикатор микрофона */
.mic-indicator {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #4CAF50;
    margin-right: 8px;
}

.mic-indicator.muted {
    background: #f44336;
}

/* Мобильная адаптация для полноэкранных звонков */
@media (max-width: 768px) {
    .call-screen {
        margin: 20px;
        padding: 30px 20px;
        max-width: none;
        border-radius: 25px;
    }
    
    .caller-info h2 {
        font-size: 24px;
    }
    
    .caller-info p {
        font-size: 20px;
        margin-bottom: 30px;
    }
    
    .call-info h3 {
        font-size: 22px;
    }
    
    #callTimer {
        font-size: 28px;
        margin-bottom: 30px;
    }
    
    .call-controls {
        gap: 20px;
    }
    
    .btn-accept, .btn-reject, .btn-mute, .btn-end {
        width: 70px;
        height: 70px;
        font-size: 24px;
    }
    
    /* На очень маленьких экранах делаем кнопки еще больше для удобства */
    @media (max-width: 480px) {
        .call-controls {
            gap: 15px;
        }
        
        .btn-accept, .btn-reject, .btn-mute, .btn-end {
            width: 65px;
            height: 65px;
            font-size: 22px;
        }
        
        .caller-info h2 {
            font-size: 22px;
        }
        
        .caller-info p {
            font-size: 18px;
        }
        
        #callTimer {
            font-size: 26px;
        }
    }
}

/* Горизонтальная ориентация на мобильных */
@media (max-width: 768px) and (orientation: landscape) {
    .call-screen {
        padding: 20px;
        max-width: 400px;
    }
    
    .caller-info p {
        margin-bottom: 20px;
    }
    
    #callTimer {
        margin-bottom: 20px;
    }
}

/* Анимация появления звонка */
@keyframes callAppear {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

#incomingCallScreen,
#activeCallScreen {
    animation: callAppear 0.3s ease-out;
}