* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #0084ff;
    --secondary-color: #f0f2f5;
    --text-color: #050505;
    --text-secondary: #65676b;
    --border-color: #e4e6eb;
    --hover-color: #f2f3f5;
    --success-color: #42b72a;
    --danger-color: #ff4444;
    --message-bg-sent: #0084ff;
    --message-bg-received: #e4e6eb;
    --sidebar-width: 360px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--secondary-color);
    color: var(--text-color);
    overflow: hidden;
}

.screen {
    display: none;
    width: 100vw;
    height: 100vh;
}

.screen.active {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* === АВТОРИЗАЦИЯ === */
.auth-container {
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    padding: 40px;
    width: 100%;
    max-width: 400px;
}

.auth-header {
    text-align: center;
    margin-bottom: 30px;
}

.auth-header h1 {
    font-size: 32px;
    margin-bottom: 8px;
}

.auth-header p {
    color: var(--text-secondary);
    font-size: 14px;
}

.auth-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 24px;
    border-bottom: 2px solid var(--border-color);
}

.tab-btn {
    flex: 1;
    padding: 12px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s;
}

.tab-btn.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}

.auth-form {
    display: none;
}

.auth-form.active {
    display: block;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-color);
}

.form-group input,
.form-group textarea {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 14px;
    transition: border-color 0.2s;
}

.form-group input:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary-color);
}

.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
    width: 100%;
}

.btn-primary:hover {
    background: #0073e6;
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--secondary-color);
    color: var(--text-color);
}

.btn-secondary:hover {
    background: var(--border-color);
}

.btn-icon {
    background: none;
    padding: 8px;
    font-size: 20px;
    border-radius: 50%;
}

.btn-icon:hover {
    background: var(--hover-color);
}

.btn-small {
    padding: 8px 16px;
    font-size: 13px;
    width: 100%;
}

.error-message {
    margin-top: 12px;
    padding: 10px;
    background: #fee;
    border: 1px solid var(--danger-color);
    border-radius: 6px;
    color: var(--danger-color);
    font-size: 13px;
    display: none;
}

.error-message.show {
    display: block;
}

/* === ЧАТ === */
.chat-container {
    display: flex;
    width: 100vw;
    height: 100vh;
    background: white;
}

/* Боковая панель */
.sidebar {
    width: var(--sidebar-width);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    background: white;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.user-info {
    display: flex;
    gap: 12px;
    align-items: center;
}

.user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}

.user-details {
    flex: 1;
}

.user-name {
    font-weight: 600;
    font-size: 15px;
}

.user-status {
    font-size: 12px;
    color: var(--success-color);
}

.sidebar-tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-tab {
    flex: 1;
    padding: 12px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    transition: all 0.2s;
}

.sidebar-tab.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}

.sidebar-actions {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
}

.contacts-list {
    flex: 1;
    overflow-y: auto;
}

.contact-item {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    gap: 12px;
    cursor: pointer;
    transition: background 0.2s;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
    user-select: none;
    -webkit-user-select: none;
}

.contact-item:hover,
.contact-item:active {
    background: var(--hover-color);
}

.contact-item.active {
    background: var(--secondary-color);
}

.unread-badge {
    background: var(--danger-color);
    color: white;
    border-radius: 12px;
    padding: 3px 8px;
    font-size: 11px;
    font-weight: 700;
    margin-left: auto;
    min-width: 22px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    flex-shrink: 0;
}

.contact-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    flex-shrink: 0;
}

.contact-info {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.contact-name {
    font-weight: 600;
    font-size: 15px;
    margin-bottom: 4px;
}

.contact-last-message {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contact-badge {
    background: var(--primary-color);
    color: white;
    border-radius: 12px;
    padding: 2px 8px;
    font-size: 12px;
    font-weight: 600;
}

/* Основная область чата */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: white;
}

.chat-welcome {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: var(--text-secondary);
}

.welcome-content h2 {
    font-size: 24px;
    margin-bottom: 8px;
}

.chat-active {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
}

.chat-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chat-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}

.chat-name {
    font-weight: 600;
    font-size: 16px;
}

.chat-status {
    font-size: 12px;
    color: var(--text-secondary);
}

.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.message {
    display: flex;
    gap: 8px;
    max-width: 70%;
}

.message.sent {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message.received {
    align-self: flex-start;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.message-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.message-sender {
    font-size: 12px;
    color: var(--text-secondary);
    font-weight: 600;
}

.message.sent .message-sender {
    text-align: right;
}

.message-bubble {
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 15px;
    line-height: 1.4;
    word-wrap: break-word;
}

.message.sent .message-bubble {
    background: var(--message-bg-sent);
    color: white;
    border-bottom-right-radius: 4px;
}

.message.received .message-bubble {
    background: var(--message-bg-received);
    color: var(--text-color);
    border-bottom-left-radius: 4px;
}

.message-time {
    font-size: 11px;
    color: var(--text-secondary);
}

.message.sent .message-time {
    text-align: right;
}

.message-file-link {
    text-decoration: none;
    color: inherit;
    display: inline-block;
}

.message-file-link:hover {
    text-decoration: underline;
}

.message-input-container {
    border-top: 1px solid var(--border-color);
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    background: white;
}

.message-form {
    display: flex;
    gap: 8px;
    align-items: center;
}

.message-form input {
    flex: 1;
    padding: 10px 16px;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    font-size: 16px; /* 16px prevents iOS auto-zoom */
}

.message-form input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.btn-send {
    background: var(--primary-color);
    color: white;
}

.btn-send:hover {
    background: #0073e6;
}

.typing-indicator {
    margin-top: 8px;
    font-size: 13px;
    color: var(--text-secondary);
    font-style: italic;
}

/* Модальные окна */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: white;
    border-radius: 12px;
    width: 90%;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
    font-size: 18px;
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
}

.modal-close:hover {
    color: var(--text-color);
}

.modal-content form {
    padding: 20px;
}

.modal-actions {
    display: flex;
    gap: 12px;
    margin-top: 20px;
}

.modal-actions .btn {
    flex: 1;
}

.members-list {
    max-height: 200px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 8px;
}

.member-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    cursor: pointer;
    border-radius: 6px;
}

.member-item:hover {
    background: var(--hover-color);
}

.member-item input[type="checkbox"] {
    width: 18px;
    height: 18px;
}

.member-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
}

.member-name {
    flex: 1;
    font-size: 14px;
}

/* Скроллбар */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Кнопка "Назад" для мобильных */
.mobile-back-btn {
    display: none;
    font-size: 24px;
    margin-right: 8px;
}

/* Адаптивность */
@media (max-width: 768px) {
    /* Запрет скролла body на мобильных */
    body {
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 100vh;
        height: 100dvh;
    }

    .container {
        height: 100vh;
        height: 100dvh;
        overflow: hidden;
    }

    .sidebar {
        width: 100%;
        overflow-y: auto;
    }

    .chat-main {
        display: none;
        height: 100vh;
        height: 100dvh;
        overflow: hidden;
    }

    .sidebar.hide {
        display: none;
    }

    .chat-main.show {
        display: flex;
    }

    .message {
        max-width: 85%;
    }

    .mobile-back-btn {
        display: flex;
    }

    /* Структура чата - flex контейнер без скролла */
    .chat-active {
        height: 100%;
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }

    /* Хедер чата - фиксированная высота */
    .chat-header {
        flex-shrink: 0;
    }

    /* Контейнер сообщений - единственный элемент со скроллом */
    .messages-container {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }

    /* Поле ввода - фиксированная высота внизу */
    .message-input-container {
        flex-shrink: 0;
        padding: 12px 16px;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        background: white;
        border-top: 1px solid var(--border-color);
    }

    /* Badge для мобильных - более заметный */
    .unread-badge {
        min-width: 24px;
        height: 24px;
        font-size: 12px;
        padding: 4px 8px;
        margin-left: 8px;
    }

    /* Уменьшаем имя контакта чтобы было место для badge */
    .contact-name {
        max-width: calc(100vw - 180px);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
}

/* Upload Progress Bar */
.upload-progress-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 1000;
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    padding: 16px 20px;
    min-width: 320px;
    max-width: 90vw;
    animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
    from {
        transform: translateX(400px);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

.upload-progress-content {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.upload-progress-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
}

#upload-file-name {
    color: var(--text-color);
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 220px;
}

#upload-percentage {
    color: var(--primary-color);
    font-weight: 600;
    font-size: 16px;
}

.upload-progress-bar {
    width: 100%;
    height: 8px;
    background: #e0e0e0;
    border-radius: 4px;
    overflow: hidden;
}

.upload-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color), #5a9fd4);
    border-radius: 4px;
    transition: width 0.3s ease;
    width: 0%;
}

@media (max-width: 768px) {
    .upload-progress-container {
        top: 10px;
        right: 10px;
        left: 10px;
        min-width: auto;
    }

    #upload-file-name {
        max-width: calc(100vw - 120px);
    }
}
