* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: #f5f5f5;
    height: 100vh;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* Левая панель - диалоги */
.conversations-panel {
    width: 300px;
    background: white;
    border-right: 1px solid rgba(0,0,0,0.08);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 2px 0 8px rgba(0,0,0,0.03);
}

.conversations-header {
    background: #4a76a8;
    color: white;
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    min-height: 56px;
    height: 56px;
    flex-shrink: 0;
}

.conversations-header h2 {
    font-size: 18px;
    font-weight: 600;
    margin: 0;
    line-height: 1.2;
}

.refresh-btn {
    background: rgba(255, 255, 255, 0.15);
    border: none;
    color: white;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.refresh-btn:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: rotate(90deg);
}

.refresh-btn:active {
    transform: rotate(90deg) scale(0.95);
}

.conversations-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.conversation-item {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    cursor: pointer;
    border-bottom: 1px solid rgba(0,0,0,0.05);
    transition: background 0.15s ease;
}

.conversation-item:hover {
    background: #f8f9fa;
}

.conversation-item.active {
    background: #e8f4fd;
    border-left: 3px solid #4a76a8;
}

.conversation-item.active.has-unread {
    background: #ffcdd2; /* Более темный красный для активного непрочитанного */
    border-left: 3px solid #f44336;
}

.conversation-item.active.has-unanswered {
    background: #fff9c4; /* Более темный желтый для активного неотвеченного */
    border-left: 3px solid #ffc107;
}

.conversation-item.active:not(.has-unread):not(.has-unanswered) {
    border-left: 3px solid #4a76a8;
}

.conversation-item.has-unread:not(.active) {
    background: #ffebee;
    border-left: 3px solid #f44336;
}

.conversation-item.has-unanswered:not(.active) {
    background: #fffde7;
    border-left: 3px solid #ffc107;
}

.conversation-avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: #e0e0e0;
    margin-right: 12px;
    flex-shrink: 0;
    object-fit: cover;
}

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

.conversation-name {
    font-weight: 600;
    font-size: 14px;
    color: #333;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    gap: 6px;
}

.conversation-name.unread {
    font-weight: 700;
    color: #1976d2;
}

/* Индикаторы статуса (цветные кружки) */
.status-indicator {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
    display: inline-block;
}

.status-red {
    background-color: #f44336; /* Красный для непрочитанных */
}

.status-yellow {
    background-color: #ffc107; /* Желтый для неотвеченных */
}

.status-green {
    background-color: #4caf50; /* Зеленый для прочитанных и отвеченных */
}

.conversation-preview {
    font-size: 12px;
    color: #666;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-badge {
    background: #f44336;
    color: white;
    border-radius: 12px;
    padding: 2px 8px;
    font-size: 11px;
    font-weight: 600;
    margin-left: 8px;
}

/* Правая панель - сообщения */
.messages-panel {
    flex: 1;
    display: none; /* Скрыта по умолчанию, показывается при выборе диалога */
    flex-direction: column;
    background: white;
    overflow: hidden;
    position: relative;
}

.messages-header {
    background: #4a76a8;
    color: white;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    min-height: 56px;
    height: 56px;
    flex-shrink: 0;
}

.back-btn {
    background: transparent;
    border: none;
    color: white;
    font-size: 20px;
    margin-right: 12px;
    cursor: pointer;
    padding: 0;
    display: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.back-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.back-btn:active {
    transform: scale(0.95);
}

.messages-header h3 {
    font-size: 18px;
    font-weight: 600;
    margin: 0;
    line-height: 1.2;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.messages-list {
    flex: 1;
    overflow-y: auto;
    background: #e5e5e5;
    padding: 10px;
    -webkit-overflow-scrolling: touch;
}

.messages-footer {
    padding: 12px 16px;
    background: white;
    border-top: 1px solid #e0e0e0;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-shrink: 0;
}

.mark-unread-btn {
    background: #4a76a8;
    color: white;
    border: none;
    border-radius: 8px;
    padding: 10px 20px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    width: 100%;
    max-width: 300px;
}

.mark-unread-btn:hover {
    background: #5a86b8;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.mark-unread-btn:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.mark-unread-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
    opacity: 0.6;
}

.empty-state {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #999;
    font-size: 14px;
}

/* Сообщения */
.message {
    margin-bottom: 12px;
    display: flex;
    flex-direction: column;
}

.message.outgoing {
    align-items: flex-end;
}

.message.incoming {
    align-items: flex-start;
}

.message-bubble {
    max-width: 70%;
    padding: 10px 14px;
    border-radius: 12px;
    word-wrap: break-word;
    position: relative;
}

/* Сообщения с вложениями могут быть шире */
.message-bubble.has-attachments {
    max-width: 85%;
    padding: 8px;
}

.message.outgoing .message-bubble {
    background: #dcf8c6;
    border-bottom-right-radius: 4px;
}

.message.incoming .message-bubble {
    background: white;
    border-bottom-left-radius: 4px;
}

.message.incoming.unread .message-bubble {
    background: #fff9c4;
}

.message-header {
    display: flex;
    align-items: center;
    margin-bottom: 6px;
    gap: 8px;
}

.message-sender {
    font-weight: 600;
    font-size: 13px;
    color: #0084ff;
}

.message-time {
    font-size: 11px;
    color: #999;
}

.message-text {
    font-size: 14px;
    color: #000;
    line-height: 1.5;
    margin-bottom: 4px;
    white-space: pre-wrap; /* Сохраняем переносы строк и пробелы */
    word-wrap: break-word; /* Переносим длинные слова */
    overflow-wrap: break-word; /* Дополнительная поддержка переноса */
}

/* Контейнер для фото с фиксированным квадратным размером */
.message-photo-container {
    margin: 4px 0;
    width: 450px; /* Фиксированная ширина для всех фото на десктопе (300px × 1.5) */
    height: 450px; /* Фиксированная высота для всех фото на десктопе (300px × 1.5) */
    max-width: calc(100% - 40px); /* Ограничение для узких экранов */
    max-height: calc(100% - 40px);
    position: relative;
    border-radius: 8px;
    overflow: hidden;
    display: inline-block;
    flex-shrink: 0; /* Не сжимается */
    box-sizing: border-box;
}

/* Placeholder для фото (серый блок) - занимает весь контейнер */
.message-photo-placeholder {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #e0e0e0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
}

.photo-loading {
    font-size: 48px;
    color: #999;
    opacity: 0.5;
}

/* Само фото - показывается полностью без деформации */
.message-photo {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    border-radius: 8px;
    cursor: pointer;
    display: none;
    background: #f5f5f5;
    image-rendering: auto; /* Для фото используем стандартное сглаживание */
}

/* Контейнер для стикера - меньше чем фото (половина ширины экрана) */
.message-sticker-container {
    margin: 4px 0;
    width: 225px; /* Фиксированная ширина для всех стикеров на десктопе (150px × 1.5) */
    height: 225px; /* Фиксированная высота для всех стикеров на десктопе (150px × 1.5) */
    max-width: 50vw; /* Половина ширины экрана */
    max-height: 50vw;
    position: relative;
    border-radius: 8px;
    overflow: hidden;
    display: inline-block;
    flex-shrink: 0; /* Не сжимается */
    box-sizing: border-box;
}

/* Placeholder для стикера (серый блок) - занимает весь контейнер */
.message-sticker-placeholder {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #e0e0e0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
}

.sticker-loading {
    font-size: 48px;
    color: #999;
    opacity: 0.5;
}

/* Сам стикер - показывается полностью без деформации */
.message-sticker {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    border-radius: 8px;
    display: none;
    background: #f5f5f5;
    /* Используем высокое качество рендеринга для стикеров */
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
    -ms-interpolation-mode: nearest-neighbor; /* Для IE */
}

/* Загрузка */
.loading {
    padding: 20px;
    text-align: center;
    color: #999;
    font-size: 14px;
}

.loading-more {
    padding: 15px;
    text-align: center;
    color: #999;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

.loading-spinner {
    width: 16px;
    height: 16px;
    border: 2px solid #e0e0e0;
    border-top-color: #4a76a8;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.error {
    padding: 20px;
    text-align: center;
    color: #d32f2f;
    font-size: 14px;
}

/* Адаптивность для мобильных */
@media (max-width: 768px) {
    .conversations-panel {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: 100%;
        z-index: 10;
        transform: translateX(-100%);
        transition: transform 0.3s;
    }

    /* По умолчанию панель диалогов видна на мобильных */
    .conversations-panel.visible {
        transform: translateX(0);
    }

    .messages-panel {
        width: 100%;
        display: none !important; /* Скрыта по умолчанию на мобильных */
    }
    
    /* Показываем панель сообщений только когда она явно показана через style.display */
    .messages-panel[style*="display: flex"] {
        display: flex !important;
    }

    .back-btn {
        display: flex !important;
    }
    
    /* Адаптируем размер контейнеров фото и стикеров на мобильных */
    .message-bubble {
        max-width: 85% !important;
    }
    
    /* Сообщения с вложениями занимают почти всю ширину на мобильных */
    .message-bubble.has-attachments {
        max-width: calc(100% - 40px) !important;
        padding: 8px !important;
    }
    
    /* Фото на мобильных - фиксированный размер */
    .message-photo-container {
        width: calc(100vw - 80px); /* Почти на всю ширину с отступами */
        max-width: calc(100vw - 80px);
        height: calc(100vw - 80px); /* Квадратное соотношение */
        max-height: calc(100vw - 80px);
    }
    
    /* Стикеры на мобильных - фиксированный размер (половина ширины экрана) */
    .message-sticker-container {
        width: calc(50vw - 20px); /* Половина ширины с отступами */
        max-width: calc(50vw - 20px);
        height: calc(50vw - 20px); /* Квадратное соотношение */
        max-height: calc(50vw - 20px);
        min-width: 125px;
        min-height: 125px;
    }
    
    /* Убираем ограничение ширины для фото и стикеров на мобильных */
    .message-photo,
    .message-sticker {
        width: 100%;
        height: 100%;
    }
}

/* Скроллбар */
.conversations-list::-webkit-scrollbar,
.messages-list::-webkit-scrollbar {
    width: 6px;
}

.conversations-list::-webkit-scrollbar-track,
.messages-list::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.conversations-list::-webkit-scrollbar-thumb,
.messages-list::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 3px;
}

.conversations-list::-webkit-scrollbar-thumb:hover,
.messages-list::-webkit-scrollbar-thumb:hover {
    background: #555;
}
