/* ==================== Переменные и базовые настройки ==================== */
:root {
    --primary-color: #3390ec;
    --primary-hover: #2a7ac0;
    --bg-color: #f0f2f5;
    --sidebar-bg: #ffffff;
    --chat-bg: #efeae2;            /* фон чата как в Telegram (светлая тема) */
    --header-bg: #ffffff;
    --text-primary: #111111;
    --text-secondary: #666666;
    --text-muted: #999999;
    --border-color: #e0e0e0;
    --message-sent: #e1ffc7;
    --message-received: #ffffff;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-md: 0 2px 5px rgba(0,0,0,0.15);
    --shadow-lg: 0 5px 15px rgba(0,0,0,0.2);
    --transition: all 0.2s ease;
}

[data-theme="dark"] {
    --primary-color: #5f9cea;
    --bg-color: #1f2c33;
    --sidebar-bg: #2a3942;
    --chat-bg: #0e1621;            /* тёмный фон чата */
    --header-bg: #202c33;
    --text-primary: #e9edef;
    --text-secondary: #8696a0;
    --text-muted: #6b7a84;
    --border-color: #374248;
    --message-sent: #005d4b;
    --message-received: #202c33;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-md: 0 2px 5px rgba(0,0,0,0.4);
    --shadow-lg: 0 5px 15px rgba(0,0,0,0.5);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Segoe UI', 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

body {
    background: #0a0c0e;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* ==================== Главный контейнер ==================== */
#app {
    width: 100%;
    max-width: 1600px;
    height: 100vh;
    background: var(--bg-color);
    overflow: hidden;
    box-shadow: var(--shadow-lg);
}

.screen {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.hidden {
    display: none !important;
}

/* ==================== Экран авторизации ==================== */
.auth-card {
    text-align: center;
    padding: 40px;
    background: var(--sidebar-bg);
    border-radius: 20px;
    box-shadow: var(--shadow-md);
    max-width: 400px;
    width: 90%;
}

.logo {
    font-size: 48px;
    font-weight: 700;
    color: var(--primary-color);
    margin-bottom: 20px;
}

.auth-card p {
    color: var(--text-secondary);
    margin-bottom: 30px;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
    padding: 12px 30px;
    border: none;
    border-radius: 30px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    transition: background 0.2s, transform 0.1s;
}

.btn-primary:hover {
    background: var(--primary-hover);
    transform: scale(1.02);
}

.btn-secondary {
    background: transparent;
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    padding: 8px 16px;
    border-radius: 20px;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-secondary:hover {
    background: var(--border-color);
}

/* ==================== Основной экран чата (flex) ==================== */
#chat-screen {
    display: flex;
    height: 100%;
}

/* ==================== Боковая панель (список чатов) ==================== */
.sidebar {
    width: 360px;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
}

.user-header {
    padding: 15px 20px;
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border-color);
    flex-wrap: wrap;
}

.avatar {
    width: 45px;
    height: 45px;
    border-radius: 50%;
    object-fit: cover;
    cursor: pointer;
    border: 2px solid var(--primary-color);
}

.user-info {
    flex: 1;
    overflow: hidden;
}

#user-name {
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 16px;
}

.user-handle {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.icon-btn {
    background: none;
    border: none;
    font-size: 20px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    transition: background 0.2s, color 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
}

.icon-btn:hover {
    background: var(--border-color);
    color: var(--text-primary);
}

/* Поиск */
.search-box {
    padding: 10px 15px;
    position: relative;
}

.search-box i {
    position: absolute;
    left: 25px;
    top: 20px;
    color: var(--text-secondary);
    font-size: 14px;
}

#search-input {
    width: 100%;
    padding: 10px 15px 10px 40px;
    border: none;
    border-radius: 20px;
    background: var(--bg-color);
    color: var(--text-primary);
    font-size: 14px;
    outline: none;
    border: 1px solid transparent;
    transition: border 0.2s;
}

#search-input:focus {
    border-color: var(--primary-color);
}

#search-input::placeholder {
    color: var(--text-secondary);
}

/* Список чатов */
.chats-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.chat-item {
    padding: 12px 15px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    transition: background 0.1s;
    margin-bottom: 2px;
    border: 1px solid transparent;
}

.chat-item:hover {
    background: var(--border-color);
}

.chat-item.active {
    background: var(--primary-color);
    color: white;
}

.chat-item.active .chat-name,
.chat-item.active .chat-time,
.chat-item.active .last-message-text {
    color: white;
}

.chat-avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    object-fit: cover;
}

.chat-info {
    flex: 1;
    overflow: hidden;
}

.chat-name-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.chat-name {
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 180px;
    font-size: 15px;
}

.chat-time {
    font-size: 11px;
    color: var(--text-secondary);
    white-space: nowrap;
}

.chat-last-message {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.last-message-text {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 180px;
}

.unread-badge {
    background: var(--primary-color);
    color: white;
    font-size: 11px;
    font-weight: 600;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
}

/* Пустые состояния */
.no-chats, .no-messages {
    text-align: center;
    color: var(--text-secondary);
    padding: 40px 20px;
    font-style: italic;
}

/* ==================== Область чата ==================== */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--chat-bg);
    position: relative;
    min-width: 0;
    height: 100%;
}

.chat-header {
    padding: 12px 20px;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: var(--shadow-sm);
    z-index: 5;
}

.chat-user-info {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
}

.small-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
}

#current-chat-name {
    font-weight: 600;
    color: var(--text-primary);
    font-size: 16px;
}

.chat-handle {
    font-size: 13px;
    color: var(--text-secondary);
    margin-left: 5px;
}

.typing-indicator {
    font-size: 13px;
    color: var(--primary-color);
    font-style: italic;
    padding: 0 10px;
    transition: opacity 0.2s;
    background: var(--header-bg);
    border-radius: 12px;
    padding: 4px 12px;
    box-shadow: var(--shadow-sm);
}

/* ==================== Сообщения ==================== */
.messages {
    flex: 1 1 auto;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    background-color: var(--chat-bg);
    /* паттерн как в Telegram */
    background-image: 
        radial-gradient(circle at 25px 25px, rgba(0,0,0,0.02) 1px, transparent 1px),
        radial-gradient(circle at 75px 75px, rgba(0,0,0,0.02) 1px, transparent 1px);
    background-size: 50px 50px, 100px 100px;
    min-height: 0; /* важно для flex */
}

[data-theme="dark"] .messages {
    background-image: 
        radial-gradient(circle at 25px 25px, rgba(255,255,255,0.02) 1px, transparent 1px),
        radial-gradient(circle at 75px 75px, rgba(255,255,255,0.02) 1px, transparent 1px);
}

/* Пузырьки сообщений с хвостиками */
.message {
    position: relative;
    max-width: 65%;
    margin-bottom: 12px;
    padding: 8px 12px;
    border-radius: 18px;
    word-wrap: break-word;
    box-shadow: var(--shadow-sm);
    font-size: 14px;
    line-height: 1.4;
    animation: slideIn 0.2s ease;
    transition: opacity 0.2s, transform 0.2s;
}

.message.sent {
    align-self: flex-end;
    background: var(--message-sent);
    color: var(--text-primary);
    border-bottom-right-radius: 4px;
}

.message.sent::after {
    content: '';
    position: absolute;
    bottom: 0;
    right: -8px;
    width: 0;
    height: 0;
    border: 8px solid transparent;
    border-left-color: var(--message-sent);
    border-right: 0;
    border-bottom: 0;
    transform: rotate(45deg);
}

.message.received {
    align-self: flex-start;
    background: var(--message-received);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

.message.received::before {
    content: '';
    position: absolute;
    bottom: 0;
    left: -8px;
    width: 0;
    height: 0;
    border: 8px solid transparent;
    border-right-color: var(--message-received);
    border-left: 0;
    border-bottom: 0;
    transform: rotate(-45deg);
}

.message.sent .message-sender {
    color: rgba(255,255,255,0.8);
}

.message.received .message-sender {
    color: var(--primary-color);
    font-weight: 500;
}

.message img, .message video, .message audio {
    max-width: 100%;
    border-radius: 12px;
    margin-top: 4px;
    cursor: pointer;
}

.message audio {
    width: 250px;
    height: 40px;
}

.message-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    margin-top: 4px;
    font-size: 11px;
    color: var(--text-secondary);
}

.message.sent .message-footer {
    color: rgba(255,255,255,0.7);
}

.message-status {
    font-size: 12px;
    margin-left: 4px;
}

.message.sent .message-status i.fa-check-double {
    color: #4fc3f7;
}

/* Анимации */
@keyframes slideIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.fade-out {
    animation: fadeOut 0.2s forwards;
}

@keyframes fadeOut {
    to { opacity: 0; transform: scale(0.9); }
}

/* Контекстное меню сообщения */
.message-context-menu {
    position: absolute;
    background: var(--sidebar-bg);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 5px;
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    min-width: 150px;
}

.message-context-menu div {
    padding: 10px 15px;
    cursor: pointer;
    border-radius: 8px;
    transition: background 0.1s;
    color: var(--text-primary);
}

.message-context-menu div:hover {
    background: var(--primary-color);
    color: white;
}

/* ==================== Панель ввода сообщения ==================== */
.message-input-area {
    flex-shrink: 0; /* не даёт сжиматься */
    padding: 15px 20px;
    background: var(--header-bg);
    border-top: 1px solid var(--border-color);
    display: flex;
    gap: 10px;
    align-items: center;
}

#message-text {
    flex: 1;
    padding: 12px 18px;
    border: none;
    border-radius: 30px;
    background: var(--bg-color);
    color: var(--text-primary);
    font-size: 15px;
    outline: none;
    border: 1px solid transparent;
    transition: border 0.2s;
}

#message-text:focus {
    border-color: var(--primary-color);
}

#message-text::placeholder {
    color: var(--text-secondary);
}

.send-btn {
    background: var(--primary-color);
    color: white;
    border: none;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.1s;
}

.send-btn:hover {
    background: var(--primary-hover);
    transform: scale(1.05);
}

/* Эмодзи пикер */
.emoji-picker {
    position: absolute;
    bottom: 80px;
    left: 20px;
    background: var(--sidebar-bg);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 10px;
    display: flex;
    flex-wrap: wrap;
    width: 300px;
    z-index: 100;
    box-shadow: var(--shadow-lg);
}

.emoji-picker span {
    font-size: 24px;
    cursor: pointer;
    transition: transform 0.1s;
    margin: 5px;
}

.emoji-picker span:hover {
    transform: scale(1.2);
}

/* Кнопка прокрутки вниз */
#scroll-to-bottom {
    position: absolute;
    bottom: 80px;
    right: 20px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary-color);
    color: white;
    border: none;
    box-shadow: var(--shadow-md);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, opacity 0.2s;
    z-index: 10;
}

#scroll-to-bottom:hover {
    transform: scale(1.1);
}

#scroll-to-bottom.hidden {
    opacity: 0;
    pointer-events: none;
}

/* ==================== Модальные окна ==================== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(5px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    animation: fadeIn 0.2s;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: var(--sidebar-bg);
    border-radius: 24px;
    width: 90%;
    max-width: 450px;
    box-shadow: var(--shadow-lg);
    animation: scaleIn 0.3s;
    max-height: 90vh;
    overflow-y: auto;
}

@keyframes scaleIn {
    from { transform: scale(0.9); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.modal-header {
    padding: 20px 25px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h2 {
    color: var(--text-primary);
    font-size: 22px;
    font-weight: 600;
}

.close-btn {
    background: none;
    border: none;
    font-size: 30px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: color 0.2s;
    line-height: 1;
}

.close-btn:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 25px;
}

.avatar-section {
    text-align: center;
    margin-bottom: 20px;
    position: relative;
    display: inline-block;
    width: 100%;
}

.large-avatar {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    object-fit: cover;
    border: 3px solid white;
    box-shadow: var(--shadow-md);
}

.avatar-upload-label {
    position: absolute;
    bottom: 5px;
    right: calc(50% - 60px);
    background: var(--primary-color);
    color: white;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    border: 2px solid white;
    transition: background 0.2s;
}

.avatar-upload-label:hover {
    background: var(--primary-hover);
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-weight: 500;
    color: var(--text-primary);
    font-size: 14px;
}

.form-group input,
.form-group textarea {
    width: 100%;
    padding: 12px 15px;
    border: 1px solid var(--border-color);
    border-radius: 16px;
    font-size: 14px;
    outline: none;
    background: var(--bg-color);
    color: var(--text-primary);
    transition: border 0.2s;
}

.form-group input:focus,
.form-group textarea:focus {
    border-color: var(--primary-color);
}

.form-group input:disabled,
.form-group textarea:disabled {
    background: var(--border-color);
    color: var(--text-secondary);
}

.form-group small {
    display: block;
    margin-top: 5px;
    color: var(--text-secondary);
    font-size: 11px;
}

.modal-footer {
    padding: 15px 25px 25px;
    text-align: right;
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

/* Список участников группы */
.members-list {
    max-height: 200px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 10px;
}

.member-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px;
    border-bottom: 1px solid var(--border-color);
}

.member-item:last-child {
    border-bottom: none;
}

.member-item .small-avatar {
    width: 30px;
    height: 30px;
}

.member-item i.fa-crown {
    color: gold;
    margin-left: auto;
}

.remove-member-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
}

.remove-member-btn:hover {
    color: red;
    background: rgba(255,0,0,0.1);
}

/* ==================== Стили для звонков ==================== */
#call-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    backdrop-filter: blur(10px);
    z-index: 3000;
    display: flex;
    justify-content: center;
    align-items: center;
}

.call-container {
    width: 90%;
    max-width: 800px;
    background: var(--sidebar-bg);
    border-radius: 24px;
    overflow: hidden;
    box-shadow: var(--shadow-lg);
    animation: scaleIn 0.3s;
}

.call-header {
    padding: 20px;
    text-align: center;
    border-bottom: 1px solid var(--border-color);
}

.call-avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    object-fit: cover;
    border: 3px solid var(--primary-color);
    margin-bottom: 10px;
}

#call-name {
    display: block;
    font-size: 24px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 5px;
}

#call-status {
    font-size: 14px;
    color: var(--text-secondary);
}

.call-videos {
    display: flex;
    flex-wrap: wrap;
    background: #1a1a1a;
    position: relative;
    min-height: 300px;
}

#local-video {
    position: absolute;
    bottom: 20px;
    right: 20px;
    width: 150px;
    height: 200px;
    object-fit: cover;
    border-radius: 12px;
    border: 2px solid var(--primary-color);
    background: #000;
    z-index: 2;
}

#remote-video {
    width: 100%;
    height: 400px;
    object-fit: cover;
    background: #000;
}

.call-controls {
    display: flex;
    justify-content: center;
    gap: 20px;
    padding: 20px;
    border-top: 1px solid var(--border-color);
}

.call-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: none;
    background: var(--primary-color);
    color: white;
    font-size: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.call-btn:hover {
    transform: scale(1.1);
    background: var(--primary-hover);
}

.call-btn.end {
    background: #dc3545;
}

.call-btn.end:hover {
    background: #c82333;
}

@media screen and (max-width: 768px) {
    .call-videos {
        flex-direction: column;
        min-height: auto;
    }
    
    #remote-video {
        height: 300px;
    }
    
    #local-video {
        width: 100px;
        height: 150px;
        bottom: 10px;
        right: 10px;
    }
    
    .call-btn {
        width: 50px;
        height: 50px;
        font-size: 20px;
    }
}

/* ==================== Адаптивность ==================== */
#menu-toggle {
    display: none;
}

@media screen and (max-width: 1024px) {
    .sidebar {
        width: 300px;
    }
}

@media screen and (max-width: 768px) {
    .sidebar {
        position: absolute;
        left: -100%;
        top: 0;
        bottom: 0;
        width: 300px;
        z-index: 100;
        transition: left 0.3s ease;
        box-shadow: var(--shadow-lg);
    }

    .sidebar.open {
        left: 0;
    }

    .chat-area {
        width: 100%;
    }

    #menu-toggle {
        display: inline-flex;
    }

    .message {
        max-width: 80%;
    }

    .user-header {
        padding: 10px 15px;
    }

    .avatar {
        width: 40px;
        height: 40px;
    }

    .icon-btn {
        width: 36px;
        height: 36px;
        font-size: 18px;
    }
}

@media screen and (max-width: 480px) {
    .sidebar {
        width: 100%;
    }

    .chat-avatar {
        width: 45px;
        height: 45px;
    }

    .chat-name {
        max-width: 140px;
        font-size: 14px;
    }

    .last-message-text {
        max-width: 140px;
    }

    .message {
        max-width: 85%;
        font-size: 13px;
    }

    .message-input-area {
        padding: 10px 15px;
    }

    #message-text {
        padding: 10px 15px;
        font-size: 14px;
    }

    .send-btn {
        width: 44px;
        height: 44px;
    }

    .modal-content {
        width: 95%;
        margin: 10px;
    }

    .large-avatar {
        width: 100px;
        height: 100px;
    }
}

/* ==================== Скроллбар ==================== */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.2);
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(0,0,0,0.3);
}

[data-theme="dark"] ::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.2);
}

[data-theme="dark"] ::-webkit-scrollbar-thumb:hover {
    background: rgba(255,255,255,0.3);
}