.app {
    display: flex;
    height: 100vh;
}

/* Сайдбар */
.sidebar {
    width: 340px;
    min-width: 340px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-header h2 {
    font-size: 18px;
    color: var(--primary);
    display: flex;
    align-items: center;
    gap: 8px;
}

.sidebar-header .logo-sm { font-size: 24px; }

.sidebar-actions {
    display: flex;
    gap: 6px;
}

.sidebar-actions button {
    padding: 6px 10px;
    font-size: 18px;
    background: transparent;
    color: var(--text-secondary);
    border-radius: var(--radius-sm);
}
.sidebar-actions button:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.sidebar-search {
    padding: 10px 16px;
}

.sidebar-search input {
    width: 100%;
    padding: 8px 12px;
    border-radius: 20px;
    font-size: 13px;
}

.sidebar-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
}

.sidebar-tabs button {
    flex: 1;
    padding: 10px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 13px;
    border-radius: 0;
    border-bottom: 2px solid transparent;
    transition: all var(--transition);
}
.sidebar-tabs button.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 0;
}

.chat-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    cursor: pointer;
    transition: background var(--transition);
    position: relative;
}
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-hover); border-left: 3px solid var(--primary); }

.chat-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--bg-chat);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: 600;
    color: var(--primary);
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}
.chat-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.online-dot {
    position: absolute;
    bottom: 2px;
    right: 2px;
    width: 12px;
    height: 12px;
    background: var(--online);
    border-radius: 50%;
    border: 2px solid var(--bg-sidebar);
}

.chat-info {
    flex: 1;
    min-width: 0;
}

.chat-info-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.chat-name {
    font-weight: 600;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Галочка верификации */
.verified-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    background: #1d9bf0;
    border-radius: 50%;
    margin-left: 4px;
    flex-shrink: 0;
    vertical-align: middle;
}
.verified-badge svg {
    width: 10px;
    height: 10px;
    fill: white;
}

.chat-time {
    font-size: 11px;
    color: var(--text-secondary);
    flex-shrink: 0;
}

.chat-preview {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.unread-badge {
    background: var(--primary);
    color: white;
    font-size: 11px;
    font-weight: 700;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
}

/* Главная область */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-main);
}

/* Хедер чата */
.chat-header {
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-sidebar);
}

.chat-header .chat-avatar { width: 40px; height: 40px; font-size: 16px; }

.chat-header-info h3 {
    font-size: 15px;
    font-weight: 600;
}

.chat-header-status {
    font-size: 12px;
    color: var(--text-secondary);
}
.chat-header-status.online { color: var(--online); }

.chat-header-actions {
    margin-left: auto;
    display: flex;
    gap: 8px;
}

.chat-header-actions button {
    padding: 6px 10px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 16px;
}
.chat-header-actions button:hover { color: var(--text-primary); background: var(--bg-hover); }

/* Область сообщений */
.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.message {
    display: flex;
    flex-direction: column;
    max-width: 65%;
    animation: msgIn 0.2s ease;
}

@keyframes msgIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.message.own { align-self: flex-end; }
.message.other { align-self: flex-start; }

.message-sender {
    font-size: 12px;
    font-weight: 600;
    color: var(--primary-light);
    margin-bottom: 2px;
    padding-left: 12px;
}

.message-bubble {
    padding: 10px 14px;
    border-radius: var(--radius);
    line-height: 1.4;
    font-size: 14px;
    word-wrap: break-word;
    position: relative;
}

.message.own .message-bubble {
    background: var(--bg-message-own);
    color: var(--text-on-primary);
    border-bottom-right-radius: 4px;
}

.message.other .message-bubble {
    background: var(--bg-message-other);
    border-bottom-left-radius: 4px;
}

.message-meta {
    display: flex;
    justify-content: flex-end;
    gap: 6px;
    margin-top: 4px;
    font-size: 11px;
    color: rgba(255,255,255,0.6);
}

.message.other .message-meta { color: var(--text-secondary); }

.message-edited { font-style: italic; }

.message-actions {
    display: none;
    position: absolute;
    top: -8px;
    right: 4px;
    background: var(--bg-sidebar);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 2px;
    gap: 2px;
    box-shadow: 0 4px 12px var(--shadow);
}

.message-bubble:hover .message-actions { display: flex; }

.message-actions button {
    padding: 4px 6px;
    font-size: 12px;
    background: transparent;
    color: var(--text-secondary);
}
.message-actions button:hover { color: var(--text-primary); background: var(--bg-hover); }

/* Reply */
.message-reply {
    background: rgba(0,0,0,0.2);
    border-left: 3px solid var(--primary);
    padding: 6px 10px;
    margin-bottom: 6px;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
}
.message-reply-sender { font-weight: 600; color: var(--primary-light); }
.message-reply-text { color: var(--text-secondary); margin-top: 2px; }

/* Файл в сообщении */
.message-file {
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(0,0,0,0.2);
    padding: 10px;
    border-radius: var(--radius-sm);
    margin-top: 6px;
    cursor: pointer;
}
.message-file-icon { font-size: 24px; }
.message-file-name { font-size: 13px; font-weight: 500; }
.message-file-size { font-size: 11px; color: var(--text-secondary); }

.message-image {
    max-width: 300px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    margin-top: 6px;
}
.message-image img {
    width: 100%;
    border-radius: var(--radius-sm);
    display: block;
}

/* Системное сообщение */
.message-system {
    align-self: center;
    max-width: 80%;
}
.message-system .message-bubble {
    background: transparent;
    color: var(--text-secondary);
    font-size: 12px;
    text-align: center;
    padding: 6px 12px;
}

/* Разделитель по дате */
.date-separator {
    text-align: center;
    margin: 16px 0;
    font-size: 12px;
    color: var(--text-secondary);
}
.date-separator span {
    background: var(--bg-main);
    padding: 4px 16px;
    border-radius: 12px;
    border: 1px solid var(--border);
}

/* Typing indicator */
.typing-indicator {
    padding: 4px 20px;
    font-size: 12px;
    color: var(--text-secondary);
    font-style: italic;
    min-height: 24px;
}

/* Input area */
.input-area {
    padding: 12px 20px;
    border-top: 1px solid var(--border);
    background: var(--bg-sidebar);
    display: flex;
    align-items: flex-end;
    gap: 10px;
}

.reply-bar {
    display: none;
    padding: 8px 14px;
    margin: 0 20px;
    background: var(--bg-sidebar);
    border-top: 1px solid var(--border);
    border-left: 3px solid var(--primary);
    font-size: 13px;
    align-items: center;
    justify-content: space-between;
}
.reply-bar.visible { display: flex; }
.reply-bar-info { flex: 1; }
.reply-bar-sender { font-weight: 600; color: var(--primary); }
.reply-bar-text { color: var(--text-secondary); font-size: 12px; }
.reply-bar-close {
    background: transparent;
    color: var(--text-secondary);
    padding: 4px 8px;
    font-size: 16px;
}

.input-btn {
    padding: 10px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 20px;
    border-radius: 50%;
    flex-shrink: 0;
}
.input-btn:hover { color: var(--text-primary); background: var(--bg-hover); }

#messageInput {
    flex: 1;
    resize: none;
    min-height: 20px;
    max-height: 120px;
    border-radius: 20px;
    padding: 10px 16px;
    font-size: 14px;
    line-height: 1.4;
    overflow-y: auto;
}

.send-btn {
    padding: 10px 16px;
    border-radius: 50%;
    font-size: 18px;
    flex-shrink: 0;
}

/* Пустое состояние */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    gap: 16px;
}
.empty-state .big-icon { font-size: 80px; opacity: 0.3; }
.empty-state h3 { font-size: 20px; color: var(--text-primary); }
.empty-state p { font-size: 14px; }

/* Модалка */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.6);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}
.modal-overlay.visible { display: flex; }

.modal {
    background: var(--bg-sidebar);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    width: 100%;
    max-width: 440px;
    box-shadow: 0 8px 32px var(--shadow);
}

.modal h3 {
    font-size: 18px;
    margin-bottom: 16px;
}

.modal .field {
    margin-bottom: 12px;
}
.modal .field label {
    display: block;
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 4px;
}
.modal .field input { width: 100%; }

.modal-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 16px;
}
.modal-buttons .cancel-btn {
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

/* Emoji picker */
.emoji-picker-wrapper {
    position: absolute;
    bottom: 70px;
    left: 20px;
    display: none;
    z-index: 100;
}
.emoji-picker-wrapper.visible { display: block; }

/* Lightbox для картинок */
.lightbox {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.9);
    z-index: 2000;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
.lightbox.visible { display: flex; }
.lightbox img { max-width: 90%; max-height: 90%; border-radius: 8px; }

/* Search results */
.search-results {
    display: none;
    position: absolute;
    top: 100%;
    left: 0; right: 0;
    background: var(--bg-sidebar);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    max-height: 300px;
    overflow-y: auto;
    z-index: 50;
    box-shadow: 0 4px 12px var(--shadow);
}
.search-results.visible { display: block; }

.search-result-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    cursor: pointer;
    transition: background var(--transition);
}
.search-result-item:hover { background: var(--bg-hover); }

/* Drag-and-drop */
.drop-zone {
    display: none;
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(224, 123, 57, 0.1);
    border: 3px dashed var(--primary);
    z-index: 100;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: var(--primary);
    font-weight: 600;
    border-radius: var(--radius);
}
.drop-zone.visible { display: flex; }

/* Контакт-лист */
.contact-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    cursor: pointer;
    transition: background var(--transition);
}
.contact-item:hover { background: var(--bg-hover); }
.contact-item .contact-remove {
    margin-left: auto;
    background: transparent;
    color: var(--danger);
    padding: 4px 8px;
    font-size: 14px;
    opacity: 0;
    transition: opacity var(--transition);
}
.contact-item:hover .contact-remove { opacity: 1; }

/* Члены группы */
.member-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 0;
}
.member-role {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 4px;
    background: var(--primary);
    color: white;
}

/* Checkbox для выбора юзеров */
.user-checkbox {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 14px;
    cursor: pointer;
    transition: background var(--transition);
}
.user-checkbox:hover { background: var(--bg-hover); }
.user-checkbox input[type="checkbox"] { accent-color: var(--primary); width: 18px; height: 18px; }

/* Профиль в сайдбаре */
.sidebar-profile {
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}
.sidebar-profile .profile-name {
    font-weight: 600;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 0;
}
.sidebar-profile .profile-username {
    font-size: 12px;
    color: var(--text-secondary);
}

/* Кнопка «назад» — скрыта на десктопе */
.back-btn {
    display: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 20px;
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    flex-shrink: 0;
}
.back-btn:hover {
    background: var(--bg-hover);
}

/* ========== Мобильная версия ========== */
@media (max-width: 768px) {
    .sidebar {
        width: 100%;
        min-width: 100%;
    }

    .main {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        display: none;
    }

    /* Когда чат открыт — показываем чат, прячем сайдбар */
    .app.chat-open .sidebar {
        display: none;
    }
    .app.chat-open .main {
        display: flex;
    }

    /* Кнопка «назад» видна на мобилке */
    .back-btn {
        display: block;
    }

    /* Увеличенные тач-зоны */
    .sidebar-actions button {
        min-width: 44px;
        min-height: 44px;
        font-size: 20px;
    }

    .sidebar-tabs button {
        min-height: 44px;
    }

    .chat-item {
        padding: 14px 16px;
    }

    .input-btn {
        min-width: 44px;
        min-height: 44px;
    }

    .send-btn {
        min-width: 44px;
        min-height: 44px;
    }

    #messageInput {
        font-size: 16px;
    }

    .sidebar-search input {
        font-size: 16px;
        padding: 10px 14px;
    }

    .chat-header-actions button {
        min-width: 44px;
        min-height: 44px;
    }

    /* Модалки на весь экран */
    .modal {
        width: 95%;
        max-width: 95%;
    }

    /* Emoji-пикер подстраиваем */
    .emoji-picker-wrapper {
        left: 10px;
        right: 10px;
    }

    /* Сообщения пошире на мобилке */
    .message {
        max-width: 85%;
    }

    .messages-area {
        padding: 12px;
    }

    .input-area {
        padding: 10px 12px;
    }
}
