:root {
    /* Светлая тема */
    --bg-primary: #f5f5f5;
    --bg-secondary: #ffffff;
    --bg-tertiary: #fafafa;
    --text-primary: #333333;
    --text-secondary: #666666;
    --text-tertiary: #999999;
    --border-color: #e0e0e0;
    --border-light: #f0f0f0;
    --shadow: rgba(0,0,0,0.1);
    --accent: #2196F3;
    --accent-hover: #1976D2;
    --message-outgoing: #dcf8c6;
    --message-incoming: #ffffff;
    --dialog-hover: #f5f5f5;
    --dialog-active: #e3f2fd;
}

[data-theme="dark"] {
    /* Темная тема */
    --bg-primary: #0e1621;
    --bg-secondary: #17212b;
    --bg-tertiary: #1e2a35;
    --text-primary: #e4e6eb;
    --text-secondary: #b1b3b8;
    --text-tertiary: #8a8d91;
    --border-color: #2b2b2b;
    --border-light: #242424;
    --shadow: rgba(0,0,0,0.3);
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --message-outgoing: #056162;
    --message-incoming: #182533;
    --dialog-hover: #1e2a35;
    --dialog-active: #2b3a4a;
}

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

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100%;
    max-height: 100vh;
    overflow: hidden;
    transition: background-color 0.3s ease, color 0.3s ease;
}

.container {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-height: 100vh;
    min-width: 600px; /* Минимальная ширина */
    overflow: hidden;
}

.header {
    background: var(--bg-secondary);
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 2px 4px var(--shadow);
    flex-shrink: 0; /* Не сжимается */
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.btn-theme-toggle {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    transition: background-color 0.3s ease, transform 0.2s ease;
}

.btn-theme-toggle:hover {
    background: var(--dialog-hover);
    transform: scale(1.1);
}

.theme-icon {
    transition: transform 0.3s ease;
}

.connection-status {
    display: flex;
    align-items: center;
    gap: 8px;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #ccc;
}

.status-dot.connected {
    background: #4caf50;
}

.account-info {
    display: flex;
    gap: 15px;
    font-size: 14px;
    color: var(--text-secondary);
}

.btn-disconnect {
    padding: 8px 16px;
    background: #f44336;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.3s ease;
}

.btn-disconnect:hover {
    background: #d32f2f;
}

.auth-section {
    max-width: 400px;
    margin: 50px auto;
    padding: 30px;
    background: var(--bg-secondary);
    border-radius: 8px;
    box-shadow: 0 2px 8px var(--shadow);
    transition: background-color 0.3s ease;
}

.auth-section h2 {
    margin-bottom: 20px;
    text-align: center;
}

.form-group {
    margin-bottom: 15px;
}

.form-group input {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 14px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease;
}

.form-group input::placeholder {
    color: var(--text-tertiary);
}

.form-group input:focus {
    outline: none;
    border-color: var(--accent);
}

.btn-primary {
    width: 100%;
    padding: 12px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
    font-weight: 500;
    transition: background-color 0.3s ease;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
    min-height: 0; /* Важно для flex контейнера */
}

.dialogs-section {
    width: 280px;
    min-width: 280px;
    max-width: 280px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex-shrink: 0; /* Не сжимается */
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
    transition: border-color 0.3s ease;
}

.tab-button {
    flex: 1;
    padding: 12px 20px;
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 14px;
    transition: all 0.3s ease;
}

.tab-button:hover {
    background: var(--dialog-hover);
    color: var(--text-primary);
}

.tab-button.active {
    color: var(--text-primary);
    border-bottom-color: var(--accent);
    font-weight: 500;
}

.dialogs-section h3 {
    padding: 15px 20px;
    border-bottom: 1px solid var(--border-color);
    font-size: 18px;
    transition: border-color 0.3s ease;
    display: none; /* Скрываем, так как теперь используем вкладки */
}

.dialogs-list {
    flex: 1;
    overflow-y: auto;
}

.dialog-item {
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-light);
    cursor: pointer;
    display: flex;
    align-items: center;
    transition: background 0.2s ease;
}

.dialog-item:hover {
    background: var(--dialog-hover);
}

.dialog-item.active {
    background: var(--dialog-active);
}

.dialog-content {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    overflow: hidden;
}

.dialog-avatar,
.dialog-avatar-placeholder {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    flex-shrink: 0;
}

.dialog-avatar-placeholder {
    background: #2196F3;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 18px;
}

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

.dialog-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.dialog-name {
    font-weight: 500;
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.dialog-time {
    font-size: 12px;
    color: var(--text-tertiary);
    flex-shrink: 0;
    margin-left: 8px;
}

.dialog-preview {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-secondary);
    overflow: hidden;
}

.dialog-last-message {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
}

.media-icon {
    flex-shrink: 0;
}

.unread-badge {
    background: var(--accent);
    color: white;
    border-radius: 12px;
    padding: 2px 8px;
    font-size: 12px;
    font-weight: 500;
    margin-left: auto;
}

.chat-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-tertiary);
    transition: background-color 0.3s ease;
    min-width: 0; /* Важно для корректного сжатия */
    overflow: hidden;
}

.chat-header {
    background: var(--bg-secondary);
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0; /* Не сжимается */
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.chat-header h3 {
    font-size: 18px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.welcome-message {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-tertiary);
    font-size: 16px;
    min-height: 0;
}

.messages-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    min-height: 0; /* Важно для flex */
}

.messages-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 100%;
}

.message {
    display: flex;
    gap: 10px;
    max-width: 75%;
    align-self: flex-start;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message:has(.message-photo) {
    max-width: 90%;
}

.message.outgoing {
    align-self: flex-end;
    flex-direction: row-reverse;
}

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

.message-avatar-placeholder {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #2196F3;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 14px;
    flex-shrink: 0;
}

.message-content {
    background: var(--message-incoming);
    padding: 10px 14px;
    border-radius: 12px;
    box-shadow: 0 1px 2px var(--shadow);
    transition: background-color 0.3s ease;
    min-width: 80px;
    max-width: 100%;
    overflow: hidden;
}

.message.outgoing .message-content {
    background: var(--message-outgoing);
}

.message-header {
    font-weight: 500;
    font-size: 13px;
    color: #2196F3;
    margin-bottom: 4px;
}

.message-text {
    font-size: 15px;
    line-height: 1.4;
    word-wrap: break-word;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    max-width: 100%;
}

.message-date {
    font-size: 11px;
    color: var(--text-tertiary);
    margin-top: 4px;
    text-align: right;
}

.message-media {
    margin: 8px 0;
    border-radius: 8px;
    overflow: hidden;
    max-width: 250px;
    width: 100%;
}

.message-photo {
    max-width: 100%;
    max-height: 800px;
    width: auto;
    height: auto;
    border-radius: 8px;
    display: block;
    cursor: default;
    transition: transform 0.2s ease;
    object-fit: contain;
}

.message-photo:hover {
    transform: scale(1.02);
}

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

.message-audio {
    width: 100%;
    margin: 8px 0;
}

.message-file {
    padding: 12px;
    background: var(--bg-tertiary);
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.message-media-placeholder {
    padding: 20px;
    text-align: center;
    background: var(--bg-tertiary);
    border-radius: 8px;
    border: 1px dashed var(--border-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.media-icon-large {
    font-size: 32px;
}

.media-label {
    font-size: 14px;
    color: var(--text-secondary);
}

.btn-load-media {
    padding: 8px 16px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.3s ease;
}

.btn-load-media:hover {
    background: var(--accent-hover);
}

.btn-load-media:disabled {
    background: var(--text-tertiary);
    cursor: not-allowed;
}

.input-section {
    background: var(--bg-secondary);
    padding: 12px 15px;
    border-top: 1px solid var(--border-color);
    display: flex;
    gap: 10px;
    flex-shrink: 0; /* Не сжимается */
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.input-section input {
    flex: 1;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    font-size: 14px;
    outline: none;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease;
}

.input-section input::placeholder {
    color: var(--text-tertiary);
}

.input-section input:focus {
    border-color: var(--accent);
}

.btn-send {
    padding: 12px 24px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 20px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: background-color 0.3s ease;
}

.btn-send:hover {
    background: var(--accent-hover);
}

/* Скроллбар */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-tertiary);
}

::-webkit-scrollbar-thumb {
    background: var(--text-tertiary);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Модальное окно для просмотра изображений */
.image-modal {
    display: none;
    position: fixed;
    z-index: 10000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    cursor: pointer;
    animation: fadeIn 0.3s ease;
}

.image-modal.active {
    display: flex !important;
    align-items: center;
    justify-content: center;
}

.image-modal img {
    max-width: 90%;
    max-height: 90%;
    object-fit: contain;
    border-radius: 8px;
    animation: zoomIn 0.3s ease;
    pointer-events: none; /* Предотвращаем закрытие при клике на само изображение */
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes zoomIn {
    from {
        transform: scale(0.8);
    }
    to {
        transform: scale(1);
    }
}

/* Адаптивность для маленьких окон */
@media (max-width: 700px) {
    .container {
        min-width: 400px;
    }
    
    .dialogs-section {
        width: 200px;
        min-width: 200px;
        max-width: 200px;
    }
    
    .dialog-avatar-placeholder {
        width: 40px;
        height: 40px;
        font-size: 16px;
    }
    
    .dialog-item {
        padding: 10px 12px;
    }
    
    .message {
        max-width: 85%;
    }
    
    .input-section {
        padding: 10px;
    }
    
    .btn-send {
        padding: 10px 16px;
    }
}

@media (max-width: 500px) {
    .container {
        min-width: 320px;
    }
    
    .dialogs-section {
        width: 60px;
        min-width: 60px;
        max-width: 60px;
    }
    
    .dialog-text {
        display: none;
    }
    
    .unread-badge {
        position: absolute;
        top: 5px;
        right: 5px;
        font-size: 10px;
        padding: 1px 5px;
    }
    
    .dialog-item {
        position: relative;
        justify-content: center;
    }
}
