/* =========================================
   БАЗОВЫЕ НАСТРОЙКИ
   ========================================= */
html, body { margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background-color: #f0f2f5; color: #333; }
* { box-sizing: border-box; }

/* === АВТОРИЗАЦИЯ === */
.login-box { background: white; padding: 40px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); width: 100%; max-width: 360px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center; z-index: 100; }
.login-box h2 { margin-top: 0; margin-bottom: 24px; }
.input-group { margin-bottom: 16px; text-align: left; }
.input-group label { display: block; margin-bottom: 6px; font-size: 14px; color: #666; }
.input-group input, .input-group select { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 6px; font-size: 16px; background: #fff; }
.btn { background: #0088cc; color: white; border: none; padding: 12px; width: 100%; border-radius: 6px; font-size: 16px; cursor: pointer; display: inline-block; text-decoration: none; text-align: center; transition: 0.2s; }
.btn:hover { opacity: 0.9; }
.btn-danger { background: #e74c3c; } .btn-success { background: #2ecc71; }
#error-msg { color: #d93025; font-size: 14px; margin-bottom: 15px; display: none; }

/* =========================================
   КАРКАС ПРИЛОЖЕНИЯ
   ========================================= */
#app-container { display: none; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: #fff; }
.app-layout { display: flex; height: 100%; width: 100%; }

/* ЛЕВАЯ ПАНЕЛЬ НАВИГАЦИИ */
.nav-col { width: 70px; height: 100%; background: #1e2b3c; display: flex; flex-direction: column; align-items: center; padding-top: 20px; z-index: 10; flex-shrink: 0; }
.nav-item { color: #8a9bb0; font-size: 24px; margin-bottom: 30px; cursor: pointer; transition: 0.2s; position: relative; }
.nav-item:hover, .nav-item.active { color: #fff; }
.nav-bottom { margin-top: auto; margin-bottom: 20px; display: flex; flex-direction: column; }
.badge { position: absolute; top: -5px; right: -10px; background: #e74c3c; color: white; font-size: 10px; padding: 2px 6px; border-radius: 10px; font-weight: bold; }
.btn-ellipsis { display: none; }

/* КОЛОНКА СПИСКА ЧАТОВ */
.list-col { width: 320px; height: 100%; border-right: 1px solid #e0e0e0; background: #f8f9fa; display: flex; flex-direction: column; flex-shrink: 0; position: relative; }
.list-header { flex-shrink: 0; padding: 15px; border-bottom: 1px solid #e0e0e0; display: flex; align-items: center; justify-content: space-between; position: relative; }
.search-bar { flex: 1; padding: 8px 12px; border: 1px solid #ccc; border-radius: 20px; outline: none; }
.list-items { flex: 1; overflow-y: auto; }
.list-item { display: flex; padding: 15px; border-bottom: 1px solid #eee; cursor: pointer; align-items: center; transition: 0.2s; }
.list-item:hover, .list-item.active-chat { background: #ebebeb; }
.item-avatar { width: 45px; height: 45px; border-radius: 50%; background: #0088cc; color: white; display: flex; align-items: center; justify-content: center; font-weight: bold; margin-right: 15px; flex-shrink: 0; font-size: 18px; }
.item-info { flex: 1; overflow: hidden; }
.item-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 3px; }
.item-name { font-weight: bold; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread-badge { background: #25d366; color: white; border-radius: 10px; padding: 2px 6px; font-size: 11px; font-weight: bold; min-width: 20px; text-align: center; display: inline-block; }
.item-preview { font-size: 13px; color: #777; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* КОЛОНКА ПЕРЕПИСКИ (ЧАТ) */
.chat-col { flex: 1; height: 100%; display: flex; flex-direction: column; background: #ffffff; min-width: 0; position: relative; }
.chat-header { flex-shrink: 0; height: 65px; border-bottom: 1px solid #e0e0e0; display: flex; align-items: center; padding: 0 20px; background: #f8f9fa; }
.btn-back { display: none; margin-right: 15px; font-size: 20px; color: #333; cursor: pointer; }
.chat-title { flex: 1; font-weight: bold; font-size: 16px; }
.chat-messages { flex: 1; overflow-y: auto; padding: 20px; background: #e5ddd5; display: flex; flex-direction: column; }

/* =========================================
   ИДЕАЛЬНЫЙ ТЕЛЕГРАМ-СТИЛЬ СООБЩЕНИЙ
   ========================================= */

/* Строка сообщения (на всю ширину) */
.msg-row { 
    display: flex; 
    width: 100%; 
    margin-bottom: 8px; 
    align-items: flex-end; /* Иконки и пузырь по нижнему краю */
}
.msg-row.my-msg { justify-content: flex-end; }
.msg-row.other-msg { justify-content: flex-start; }

/* Сам пузырь */
.msg-bubble { 
    position: relative;
    width: fit-content; 
    max-width: 75%; 
    min-width: 80px; 
    padding: 8px 12px !important; /* Убрали огромный нижний отступ */
    border-radius: 12px; 
    line-height: 1.4; 
    box-shadow: 0 1px 2px rgba(0,0,0,0.15);
}
.my-msg .msg-bubble { background: #efffde; border-bottom-right-radius: 4px; }
.other-msg .msg-bubble { background: #fff; border-bottom-left-radius: 4px; }

/* Имя отправителя в группе */
.msg-sender-name {
    font-weight: 500;
    font-size: 13px;
    margin-bottom: 4px; /* Плотный отступ до текста */
    line-height: 1.1;
}

/* Текст сообщения */
.msg-text {
    font-size: 14px;
    white-space: pre-wrap; 
    word-break: break-word;
}

/* Настоящий хак Телеграма: обтекание */
.msg-time { 
    float: right; /* Прижимает к правому краю текста */
    margin: 6px -4px -4px 12px; /* 12px - это зазор между текстом и временем */
    font-size: 11px; 
    color: #8fa0a8; 
    display: flex; 
    align-items: center; 
    gap: 3px; 
    line-height: 1;
}
.my-msg .msg-time { color: #5ebd74; }

/* Цитаты (ответы на сообщения) */
.msg-quote { 
    background: rgba(0,0,0,0.04); 
    border-left: 3px solid #3498db; 
    padding: 4px 8px; 
    margin-bottom: 6px; 
    border-radius: 4px; 
    font-size: 13px; 
    cursor: pointer; 
}
.my-msg .msg-quote { background: rgba(46, 204, 113, 0.15); border-left-color: #27ae60; }
.msg-quote-sender { font-weight: 500; color: #3498db; margin-bottom: 2px; }
.my-msg .msg-quote-sender { color: #27ae60; }
.msg-quote-text { color: #555; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 250px; }

/* Флажок "изменено" */
.msg-edited-flag { font-size: 10px; color: #999; font-style: italic; margin-left: 5px; }

/* =========================================
   ИКОНКИ УПРАВЛЕНИЯ (УДАЛИТЬ / ЗАКРЕПИТЬ)
   ========================================= */
.msg-actions-hover { 
    display: flex;
    gap: 10px; 
    padding-bottom: 6px; /* Чуть приподнимаем */
    opacity: 0; 
    transition: 0.2s; 
}
.msg-row:hover .msg-actions-hover { opacity: 1; } /* Показываем при наведении на строку */

/* МАГИЯ ОРИЕНТАЦИИ: */
.msg-row.my-msg .msg-actions-hover { 
    order: -1; /* Для своих сообщений ставим иконки СЛЕВА от пузыря */
    margin-right: 8px; 
}
.msg-row.other-msg .msg-actions-hover { 
    /* Для чужих они по умолчанию будут СПРАВА */
    margin-left: 8px; 
}

.msg-actions-hover i { color: #999; font-size: 15px; cursor: pointer; transition: 0.2s; }
.msg-actions-hover i:hover { color: #333; }
.msg-actions-hover i.fa-trash:hover { color: #e74c3c; }


/* =========================================
   ВЛОЖЕНИЯ (ФОТО И ФАЙЛЫ В ЧАТЕ)
   ========================================= */
.msg-attachments-grid { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 6px; }
.msg-attachments-grid .media-wrapper { flex: 1 1 calc(50% - 4px); max-width: 220px; }
.media-wrapper { position: relative; display: block; overflow: hidden; border-radius: 8px; background: rgba(0,0,0,0.05); }
.msg-image, .msg-video-thumb video { width: 100%; height: 100%; max-height: 250px; object-fit: cover; cursor: pointer; display: block; transition: 0.2s; }
.msg-image:hover { opacity: 0.9; }
.msg-video-thumb { position: relative; cursor: pointer; display: block; height: 100%; }
.play-icon { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: white; font-size: 40px; text-shadow: 0 2px 4px rgba(0,0,0,0.5); pointer-events: none; }
.media-download-icon { position: absolute; bottom: 8px; right: 8px; background: rgba(255,255,255,0.85); color: #333; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; text-decoration: none; font-size: 16px; box-shadow: 0 2px 5px rgba(0,0,0,0.3); transition: 0.2s; z-index: 5; }
.media-download-icon:hover { background: #fff; color: #000; transform: scale(1.1); }
.msg-doc { display: flex; align-items: center; background: rgba(0,0,0,0.05); padding: 10px; border-radius: 8px; text-decoration: none; color: inherit; font-size: 14px; word-break: break-all; cursor: pointer; transition: 0.2s; }
.msg-doc:hover { background: rgba(0,0,0,0.1); }
.msg-doc i { font-size: 24px; margin-right: 10px; color: #0088cc; }


/* =========================================
   СТРОКА ВВОДА СООБЩЕНИЯ
   ========================================= */
.chat-input-area { flex-shrink: 0; background: #f0f2f5; padding: 10px 15px; display: flex; align-items: flex-end; gap: 10px; position: relative; }
.emoji-trigger { font-size: 26px; color: #888; cursor: pointer; padding-bottom: 8px; transition: color 0.2s; }
.emoji-trigger:hover { color: #0088cc; }
.input-wrapper { flex: 1; background: #fff; border-radius: 24px; display: flex; align-items: flex-end; padding: 6px 12px; box-shadow: 0 1px 2px rgba(0,0,0,0.1); min-height: 44px; }
.message-input { flex: 1; border: none; outline: none; background: transparent; font-size: 16px; font-family: inherit; line-height: 20px; resize: none; max-height: 120px; padding: 6px 8px 6px 0; margin: 0; }
.input-actions { display: flex; align-items: center; gap: 12px; padding-bottom: 6px; }
.input-actions .btn-icon { font-size: 20px; color: #888; cursor: pointer; transition: color 0.2s; }
.input-actions .btn-icon:hover { color: #0088cc; }
.btn-send-wrapper { width: 44px; height: 44px; border-radius: 50%; background: #3390ec; color: #fff; display: flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0; transition: background 0.2s; }
.btn-send-wrapper:hover { background: #2b7bc0; }
.btn-send-wrapper i { font-size: 18px; margin-left: -2px; margin-top: 2px;}
#upload-progress { position: absolute; top: -25px; left: 20px; font-size: 12px; color: #0088cc; display: none; background: white; padding: 2px 10px; border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }

/* ПАНЕЛЬ ЭМОДЗИ */
.emoji-picker { display: none; position: absolute; bottom: 60px; left: 10px; background: white; border: 1px solid #ccc; border-radius: 8px; padding: 10px; width: 260px; box-shadow: 0 4px 15px rgba(0,0,0,0.15); z-index: 100; grid-template-columns: repeat(6, 1fr); gap: 5px; }
.emoji-picker.active { display: grid; }
.emoji-item { font-size: 24px; text-align: center; cursor: pointer; transition: 0.1s; user-select: none; }
.emoji-item:hover { transform: scale(1.2); }


/* =========================================
   ПРАВАЯ КОЛОНКА (ФАЙЛЫ И УЧАСТНИКИ)
   ========================================= */
.files-col { width: 280px; height: 100%; border-left: 1px solid #e0e0e0; background: #f8f9fa; display: flex; flex-direction: column; flex-shrink: 0; }
.files-header { flex-shrink: 0; height: 65px; border-bottom: 1px solid #e0e0e0; display: flex; align-items: center; padding: 0 20px; font-weight: bold; }
.files-tabs { display: flex; border-bottom: 1px solid #e0e0e0; background: #fff; flex-shrink: 0; }
.file-tab { flex: 1; text-align: center; padding: 12px 0; cursor: pointer; font-size: 14px; font-weight: bold; color: #666; border-bottom: 2px solid transparent; }
.file-tab.active { color: #0088cc; border-bottom: 2px solid #0088cc; }
.files-content { flex: 1; overflow-y: auto; padding: 15px; }
.gallery-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.gallery-img { width: 100%; height: 100px; object-fit: cover; border-radius: 8px; cursor: pointer; border: 1px solid #ddd; }
.gallery-doc { display: flex; align-items: center; background: #fff; padding: 10px; border: 1px solid #ddd; border-radius: 6px; text-decoration: none; color: #333; margin-bottom: 10px; font-size: 12px; cursor: pointer; transition: 0.2s; }
.gallery-doc:hover { background: #eee; }
.gallery-doc i { font-size: 18px; margin-right: 8px; }


/* =========================================
   ПАНЕЛЬ АДМИНА
   ========================================= */
.admin-col { display: none; flex: 1; height: 100%; background: #fff; flex-direction: column; overflow-y: auto; min-width: 0; }
.admin-header { flex-shrink: 0; height: 65px; border-bottom: 1px solid #e0e0e0; display: flex; align-items: center; padding: 0 30px; background: #f8f9fa; font-weight: bold; font-size: 18px; }
.admin-content { flex: 1; padding: 30px; display: flex; gap: 30px; flex-wrap: wrap; }
.admin-form-box { flex: 1; min-width: 300px; max-width: 400px; background: #f8f9fa; padding: 25px; border-radius: 8px; border: 1px solid #e0e0e0; height: fit-content; }
.admin-table-box { flex: 2; min-width: 300px; overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; text-align: left; }
.admin-table th { background: #eee; padding: 12px; border-bottom: 2px solid #ddd; }
.admin-table td { padding: 12px; border-bottom: 1px solid #eee; }


/* =========================================
   МОДАЛЬНЫЕ ОКНА И ВЫПАДАЮЩИЕ МЕНЮ
   ========================================= */
.mobile-menu { display: none; position: absolute; top: 50px; right: 10px; background: white; border: 1px solid #ccc; border-radius: 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.15); z-index: 1000; width: 210px; padding: 10px; }
.mobile-menu.active { display: block !important; }
.menu-item { display: flex; align-items: center; padding: 10px; color: #333; text-decoration: none; cursor: pointer; transition: 0.2s; border-radius: 6px; }
.menu-item:hover { background: #f0f2f5; }

.lightbox { display: none; position: fixed; z-index: 2000; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.9); align-items: center; justify-content: center; flex-direction: column; }
.lightbox-close { position: absolute; top: 20px; right: 30px; color: white; font-size: 40px; font-weight: bold; cursor: pointer; z-index: 2005; }
.lightbox-content { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; position: relative; }
#lightbox-img { max-width: 95%; max-height: 90vh; border-radius: 4px; display: none; transform-origin: center center; transition: transform 0.1s; box-shadow: 0 5px 20px rgba(0,0,0,0.5); }
#lightbox-video { max-width: 95%; max-height: 90vh; border-radius: 4px; display: none; outline: none; box-shadow: 0 5px 20px rgba(0,0,0,0.5); }
.lightbox-dl-btn { position: absolute; top: 30px; right: 90px; color: white; font-size: 28px; text-decoration: none; z-index: 2010; transition: 0.2s; opacity: 0.8; }
.lightbox-dl-btn:hover { opacity: 1; transform: scale(1.1); }
.preview-box { background: white; padding: 25px; border-radius: 12px; width: 90%; max-width: 400px; box-shadow: 0 10px 25px rgba(0,0,0,0.2); }

/* Выделение (Мультиселект - мобильная версия) */
.msg-row.selected .msg-bubble { background: #d0e8ff !important; border: 1px solid #0088cc; }
.msg-row.selected { background: rgba(0, 136, 204, 0.15) !important; }

/* Анимация подсветки сообщения при переходе по цитате */
@keyframes highlight-msg {
    0% { background-color: #d0e8ff; }
    100% { background-color: transparent; }
}
.highlight-anim { animation: highlight-msg 2s ease-out; }

/* Прячем плавающий плюсик на больших экранах */
.fab-add-chat { display: none; }


/* =========================================
   МОБИЛЬНАЯ АДАПТАЦИЯ (Ширина экрана до 768px)
   ========================================= */
@media (max-width: 768px) {
    .app-layout { display: block; }
    .nav-bottom { display: none !important; }
    
    .nav-col { 
        position: fixed; bottom: 0; left: 0; right: 0; height: 60px; width: 100%; 
        flex-direction: row; justify-content: space-around; align-items: center; 
        padding: 0 !important; border-top: 1px solid #111; z-index: 1000; background: #1e2b3c; 
    }
    .nav-item { margin: 0 !important; padding: 0 !important; display: flex; align-items: center; }

    .list-col, .chat-col, .admin-col, .files-col { position: fixed !important; top: 0; left: 0; right: 0; bottom: 60px; width: 100% !important; height: auto !important; padding-bottom: 0 !important; }
    .list-col { display: flex !important; z-index: 10; }
    .list-col.hidden-mobile { display: none !important; }
    
    /* Скрываем окна на мобилке, пока они не активны */
    .chat-col, .admin-col, .files-col { display: none !important; z-index: 20; }
    .chat-col.active-mobile, .admin-col.active-mobile, .files-col.active-mobile { display: flex !important; }
    
    .btn-back { display: block; } 
    .search-bar { margin-right: 10px; }
    .btn-ellipsis { display: block; font-size: 20px; color: #666; cursor: pointer; padding: 5px; z-index: 10; }
    
    .admin-content { padding: 15px; } 
    .admin-header { padding: 0 15px; }

    /* Плавающий плюсик для мобилок */
    .fab-add-chat {
        display: flex; position: fixed; bottom: 80px; right: 20px; width: 56px; height: 56px;
        border-radius: 50%; background-color: #0088cc; color: white; align-items: center;
        justify-content: center; font-size: 24px; box-shadow: 0 4px 10px rgba(0,0,0,0.3);
        cursor: pointer; z-index: 1000; transition: transform 0.2s, background-color 0.2s;
    }
    .fab-add-chat:active { transform: scale(0.95); background-color: #0077b3; }
}