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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #0e1621;
    color: #fff;
    /* Изменено: используем dvh для учета панелей браузера */
    height: 100vh;
    height: 100dvh; 
    overflow: hidden;
}

/* Класс для скрытия контента при загрузке */
body.loading * {
    visibility: hidden;
}
/* Показываем только фон или лоадер, если нужно */
body.loading {
    background: #0e1621; 
}

/* Login & Password Screens */
#passwordSetup, #passwordLogin, #loginScreen, #lockedScreen {
    display: none;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 2000;
}

.screenBox {
    background: rgba(255,255,255,0.95);
    padding: 40px;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    width: 400px;
    max-width: 90%;
}

.screenBox h2 {
    color: #333;
    margin-bottom: 10px;
    text-align: center;
}

.screenBox p {
    color: #666;
    margin-bottom: 30px;
    text-align: center;
    font-size: 14px;
}

.screenBox input {
    width: 100%;
    padding: 12px;
    margin: 10px 0;
    border: 2px solid #e0e0e0;
    border-radius: 10px;
    font-size: 14px;
    transition: 0.3s;
}

.screenBox input:focus {
    outline: none;
    border-color: #667eea;
}

.screenBox button {
    width: 100%;
    padding: 12px;
    margin-top: 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 16px;
    cursor: pointer;
    transition: 0.3s;
}

.screenBox button:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 20px rgba(102,126,234,0.4);
}

.screenBox button.secondary {
    background: #e0e0e0;
    color: #333;
    margin-top: 10px;
}

.lockIcon {
    font-size: 60px;
    text-align: center;
    margin-bottom: 20px;
}

.strengthMeter {
    height: 4px;
    background: #e0e0e0;
    border-radius: 2px;
    margin: 10px 0;
    overflow: hidden;
}

.strengthBar {
    height: 100%;
    width: 0%;
    transition: 0.3s;
}

/* Chat Screen */
#chatScreen {
    display: none;
    /* Изменено: 100dvh чтобы чат влез ровно в видимую область */
    height: 100vh; 
    height: 100dvh; 
    flex-direction: row;
    position: relative;
}

#sidebar {
    width: 320px;
    background: #1a2332;
    border-right: 1px solid #2d3748;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    z-index: 100;
}

#header {
    padding: 20px;
    background: #151f2e;
    border-bottom: 1px solid #2d3748;
}

#header h3 {
    margin-bottom: 5px;
}

#myId {
    font-size: 11px;
    color: #718096;
    cursor: pointer;
}

#myId:hover {
    color: #667eea;
}

.connectionStatus {
    font-size: 11px;
    padding: 4px 8px;
    border-radius: 12px;
    margin: 5px 0;
    display: none;
    text-align: center;
}

.connectionStatus.connecting { background: #ed8936; color: white; display: block; }
.connectionStatus.connected { background: #48bb78; color: white; display: block; }
.connectionStatus.p2p { background: #4299e1; color: white; display: block; }

.headerButtons {
    display: flex;
    gap: 10px;
    margin-top: 15px;
}

.headerButtons button {
    flex: 1;
    padding: 8px;
    background: #667eea;
    border: none;
    border-radius: 8px;
    color: white;
    cursor: pointer;
    font-size: 13px;
    transition: 0.2s;
}

.headerButtons button:hover { background: #5568d3; }
.headerButtons button.settings { flex: 0 0 40px; font-size: 16px; }

#usersList {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.userItem {
    padding: 12px;
    background: #1e2936;
    border-radius: 10px;
    margin-bottom: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
    position: relative;
    transition: 0.2s;
}

.userItem:hover { background: #2d3748; }
.userItem.active { background: #7b66ea33; }

.userItem.pending { opacity: 0.6; }
.userItem.pending::after {
    content: "Ожидание";
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 11px;
    color: #ed8936;
    background: rgba(237, 137, 54, 0.2);
    padding: 4px 8px;
    border-radius: 4px;
}

.avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea, #764ba2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: bold;
    flex-shrink: 0;
}

.userInfo { flex: 1; min-width: 0; }
.username { font-weight: 600; font-size: 14px; margin-bottom: 3px; }
.lastMessage { font-size: 12px; color: #718096; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.status { font-size: 11px; color: #718096; }
.status.online { color: #48bb78; }
.status.p2p { color: #4299e1; }

.deleteChat {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    background: #e53e3e;
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 11px;
    cursor: pointer;
    opacity: 0;
    transition: 0.2s;
}

.userItem:hover .deleteChat { opacity: 1; }

/* Chat Area */
#chatArea {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #0e1621;
    overflow: hidden;
    width: 100%;
}

#chatHeader {
    padding: 20px;
    background: #151f2e;
    border-bottom: 1px solid #2d3748;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 70px; /* Fixed height for consistency */
    flex-shrink: 0;
}

#mobileMenuBtn {
    display: none; /* Hidden on desktop */
    background: none;
    border: none;
    color: #667eea;
    font-size: 26px;
    cursor: pointer;
    padding: 0 15px 0 0;
    line-height: 1;
}

#chatHeaderInfo {
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 0;
}

#chatWith { font-size: 18px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chatStatus { font-size: 12px; color: #718096; }

#messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    box-sizing: border-box;
    background-color: #0e1621;
}

.message {
    display: flex;
    flex-direction: column;
    max-width: 65%;
    /* Добавьте это свойство, чтобы блок "облегал" контент, а не растягивался */
    width: fit-content;
    animation: slideIn 0.2s ease;
    box-sizing: border-box;
}

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

.message.sent { align-self: flex-end; }
.message.received { align-self: flex-start; }

.messageBubble {
    padding: 12px 16px;
    border-radius: 16px;
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    white-space: pre-wrap;
    max-width: 100%;
    position: relative;
}

.message.sent .messageBubble { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; }
.message.received .messageBubble { background: #1e2936; color: #e2e8f0; }

.messageTime {
    font-size: 10px;
    color: #718096;
    margin-top: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.deleteMessage {
    background: none;
    border: none;
    color: #e53e3e;
    font-size: 10px;
    cursor: pointer;
    opacity: 0;
    transition: 0.2s;
}
.message:hover .deleteMessage { opacity: 1; }

#inputArea {
    padding: 20px;
    background: #151f2e;
    border-top: 1px solid #2d3748;
    flex-shrink: 0;
    /* Добавлено: отступ снизу для iPhone и других телефонов */
    padding-bottom: calc(20px + env(safe-area-inset-bottom)); 
}

#messageInput {
    width: 100%;
    padding: 12px;
    background: #1e2936;
    border: 1px solid #2d3748;
    border-radius: 20px;
    color: white;
    font-size: 14px;
    resize: none;
    font-family: inherit;
    max-height: 150px;
    box-sizing: border-box;
}

#messageInput:focus { outline: none; border-color: #667eea; }

#sendBtn {
    width: 44px;
    height: 44px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    border-radius: 50%;
    color: white;
    font-size: 18px;
    cursor: pointer;
    transition: 0.2s;
    flex-shrink: 0;
}

#sendBtn:hover {
    transform: scale(1.05);
}

#sendBtn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

#charCounter { font-size: 11px; color: #718096; text-align: right; margin-top: 5px; }

.emptyState { text-align: center; padding: 40px 20px; color: #718096; }
.systemMsg { text-align: center; color: #718096; font-size: 13px; padding: 10px; background: rgba(255,255,255,0.05); border-radius: 8px; }

/* Modals */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.7);
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.modalContent {
    background: #1a2332;
    padding: 30px;
    border-radius: 16px;
    width: 500px;
    max-width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.modalContent h2 { margin-bottom: 20px; }
.modalContent input, .modalContent select {
    width: auto;
    padding: 12px;
    margin: 10px 0;
    background: #0e1621;
    border: 1px solid #2d3748;
    border-radius: 10px;
    color: white;
    font-size: 14px;
}

.modalContent button {
    width: auto;
    padding: 12px;
    margin-top: 10px;
    background: #667eea;
    border: none;
    border-radius: 10px;
    color: white;
    cursor: pointer;
    font-size: 14px;
}
.modalContent button.secondary { background: #2d3748; }
.modalContent button.danger { background: #e53e3e; }

#clearChatBtn {
    padding: 6px 12px;
    background: #e53e3e;
    border: none;
    border-radius: 6px;
    color: white;
    font-size: 12px;
    cursor: pointer;
    display: none;
    white-space: nowrap;
}

/* Mobile Overlay */
.mobile-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.6);
    z-index: 150;
    backdrop-filter: blur(2px);
}

.mobile-overlay.active { display: block; }

/* ============= MOBILE STYLES ============= */
@media (max-width: 768px) {
    /* Show Menu Button */
    #mobileMenuBtn { display: block; }

    /* Fix Sidebar Position */
    #sidebar {
        position: fixed;
        left: -100%; /* Hide offscreen */
        top: 0;
        bottom: 0;
        width: 280px; /* Standard drawer width */
        height: 100%;
        z-index: 200; /* Above chat */
        transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 2px 0 15px rgba(0,0,0,0.5);
    }

    #sidebar.mobile-open {
        left: 0; /* Slide in */
    }

    /* Adjust Chat Header */
    #chatHeader {
        padding: 10px 15px;
        height: 60px;
    }

    #chatWith { font-size: 16px; }

    /* Adjust Messages */
    #messages { padding: 10px; }
    .message { max-width: 65%; }
    .messageBubble { padding: 10px 14px; font-size: 15px; }

    /* Adjust Input Area */
    #inputArea { 
        padding: 10px;
        /* Важно: сохраняем отступ снизу на мобильных */
        padding-bottom: calc(10px + env(safe-area-inset-bottom));
    }
    #sendBtn { width: 44px; height: 44px; font-size: 18px; }

    /* User List Items */
    .userItem { padding: 12px 15px; }
    .userItem:hover .deleteChat { opacity: 1; } /* Always show delete on mobile tap logic */
    .deleteChat { opacity: 1; background: rgba(229, 62, 62, 0.2); color: #fc8181; }

    /* Buttons & Inputs */
    button, input, textarea { font-size: 16px; } /* Prevents iOS zoom */

    /* Modals */
    .modalContent { width: 95%; padding: 20px; }
}

/* Emoji Picker */
#emojiPicker {
    position: absolute;
    bottom: 70px;
    left: 10px;
    background: #1e2936;
    border: 1px solid #2d3748;
    border-radius: 12px;
    padding: 10px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.3);
    max-width: 280px;
    max-height: 200px;
    overflow-y: auto;
    z-index: 1000;
}

#emojiPicker .emojiGrid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 4px;
}

#emojiPicker .emoji {
    font-size: 22px;
    padding: 6px;
    cursor: pointer;
    text-align: center;
    border-radius: 6px;
    transition: 0.2s;
}

#emojiPicker .emoji:hover {
    background: #2d3748;
    transform: scale(1.15);
}

#emojiBtn, #fileBtn {
    width: 44px;
    height: 44px;
    background: #2d3748;
    border: none;
    border-radius: 8px;
    color: white;
    cursor: pointer;
    font-size: 20px;
    transition: 0.2s;
    flex-shrink: 0;
}

#emojiBtn:hover, #fileBtn:hover {
    background: #3d4758;
}

#emojiBtn:disabled, #fileBtn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Contact Info Modal */
.contactInfoSection {
    text-align: center;
    padding: 20px;
    margin-bottom: 20px;
}

.contactInfoAvatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 36px;
    font-weight: bold;
    color: white;
    margin: 0 auto 15px;
}

.contactInfoName {
    font-size: 22px;
    font-weight: bold;
    color: white;
    margin-bottom: 5px;
}

.contactInfoStatus {
    font-size: 14px;
    color: #718096;
}

.contactInfoDetails {
    background: #0e1621;
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 15px;
}

.contactInfoItem {
    display: flex;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid #2d3748;
}

.contactInfoItem:last-child {
    border-bottom: none;
}

.contactInfoLabel {
    color: #718096;
    font-size: 13px;
}

.contactInfoValue {
    color: white;
    font-size: 13px;
    font-weight: 500;
    text-align: right;
    max-width: 60%;
    word-wrap: break-word;
}

/* File Messages */
.messageFile {
    max-width: 300px;
    border-radius: 12px;
    overflow: hidden;
}

.messageFile img {
    max-width: 100%;
    display: block;
    border-radius: 8px;
}

.messageFile video {
    max-width: 100%;
    display: block;
    border-radius: 8px;
}

.messageFileInfo {
    background: rgba(45, 55, 72, 0.5);
    padding: 12px;
    margin-top: 8px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 12px;
/* added fix 03122025	*/
    width: 280px;      /* Фиксируем ширину, чтобы было компактно */
    max-width: 100%;   /* На случай очень маленьких экранов */
    box-sizing: border-box;
}

.messageFileIcon {
    font-size: 28px;
}

.messageFileDetails {
    flex: 1;
    min-width: 0;
}

.messageFileName {
    font-size: 13px;
    font-weight: 500;
    color: white;
    word-break: break-all;
    margin-bottom: 4px;
}

.messageFileSize {
    font-size: 11px;
    color: #a0aec0;
}

.messageFileDownload {
    padding: 8px 14px;
    background: #667eea;
    border: none;
    border-radius: 8px;
    color: white;
    cursor: pointer;
    font-size: 12px;
    white-space: nowrap;
    transition: 0.2s;
}

.messageFileDownload:hover {
    background: #5568d3;
    transform: translateY(-1px);
}

/* File Upload Preview */
#filePreview {
    display: none;
    padding: 12px;
    background: #1e2936;
    border: 1px solid #2d3748;
    border-radius: 10px;
    margin-bottom: 10px;
}

.filePreviewItem {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px;
    background: #0e1621;
    border-radius: 8px;
    margin-bottom: 8px;
    border: 1px solid #2d3748;
}

.filePreviewItem:last-child {
    margin-bottom: 0;
}

.filePreviewThumb {
    width: 50px;
    height: 50px;
    border-radius: 6px;
    object-fit: cover;
    background: #2d3748;
    flex-shrink: 0;
}

.filePreviewIcon {
    width: 50px;
    height: 50px;
    border-radius: 6px;
    background: #2d3748;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    flex-shrink: 0;
}

.filePreviewInfo {
    flex: 1;
    overflow: hidden;
    min-width: 0;
}

.filePreviewName {
    font-size: 13px;
    color: white;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 500;
}

.filePreviewSize {
    font-size: 11px;
    color: #718096;
    margin-top: 4px;
}

.filePreviewRemove {
    padding: 6px 12px;
    background: #e53e3e;
    border: none;
    border-radius: 6px;
    color: white;
    cursor: pointer;
    font-size: 11px;
    flex-shrink: 0;
    transition: 0.2s;
}

.filePreviewRemove:hover {
    background: #c53030;
    transform: translateY(-1px);
}

@media screen and (max-width: 768px) {
    #emojiPicker {
        left: 5px;
        right: 5px;
        max-width: calc(100% - 10px);
    }
    
    .contactInfoAvatar {
        width: 60px;
        height: 60px;
        font-size: 28px;
    }
    
    .messageFile {
        max-width: 250px;
    }
}

/* ============= Message Status Icons ============= */
.status-icon {
    display: inline-block;
    margin-left: 5px;
    font-size: 11px;
    font-weight: bold; /* Жирные галочки */
}

/* 1. Sending (Часики) - Серый */
.status-sending {
    color: #718096; 
}

/* 2. Sent/Queued (Одна галочка) - Серый */
/* Сервер принял сообщение */
.status-sent, .status-queued {
    color: #718096;
}

/* 3. Delivered (Две галочки) - Серый */
/* Доставлено получателю, но не прочитано */
.status-delivered {
    color: #718096; 
}

/* 4. Read (Две галочки) - Синий */
/* Прочитано */
.status-read {
    color: #4299e1; 
}

/* Для сообщений с ошибкой */
.status-error {
    color: #e53e3e;
}

.messageTime {
    display: flex;
    align-items: center;
    gap: 5px;
}

/* ============= Resend Button (Только для ошибок) ============= */
.resendButton {
    background: none;
    border: none;
    color: #e53e3e; /* Красный цвет */
    cursor: pointer;
    font-size: 14px;
    margin-left: 5px;
    padding: 0;
    line-height: 1;
    animation: pulseError 2s infinite;
    transition: transform 0.2s;
}

.resendButton:hover {
    transform: scale(1.2);
    color: #c53030;
}

@keyframes pulseError {
    0% { opacity: 1; }
    50% { opacity: 0.6; }
    100% { opacity: 1; }
}
