/* Переменные */
:root {
    --primary-color: #6366f1;
    --secondary-color: #8b5cf6;
    --success-color: #10b981;
    --error-color: #ef4444;
    --warning-color: #f59e0b;
    
    --bg-primary: #ffffff;
    --bg-secondary: #f9fafb;
    --bg-tertiary: #f3f4f6;
    --bg-sidebar: #1f2937;
    
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --text-tertiary: #9ca3af;
    --text-inverse: #ffffff;
    
    --border-color: #e5e7eb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    
    --transition: all 0.2s ease;
}

/* Базовые стили */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg-secondary);
    color: var(--text-primary);
    line-height: 1.6;
    overflow: hidden;
}

/* Контейнер приложения */
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* Боковая панель */
.sidebar {
    width: 280px;
    background: var(--bg-sidebar);
    color: var(--text-inverse);
    display: flex;
    flex-direction: column;
    border-right: 1px solid rgba(255, 255, 255, 0.1);
    overflow-y: auto;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    align-items: center;
    justify-content: center;
}

.sidebar-logo {
    max-width: 180px;
    height: auto;
    object-fit: contain;
    cursor: pointer;
    transition: opacity 0.3s ease;
}

.sidebar-logo:hover {
    opacity: 0.8;
}

.logo-main {
    max-width: 180px;
    height: auto;
    object-fit: contain;
    cursor: pointer;
    transition: opacity 0.3s ease;
}

.logo-main:hover {
    opacity: 0.8;
}

.btn-new-chat {
    margin: 20px;
    padding: 12px 16px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: var(--transition);
}

.btn-new-chat:hover {
    background: #4f46e5;
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-new-chat .icon {
    font-size: 16px;
}

.sidebar-section {
    padding: 20px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-section h3 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-tertiary);
    margin-bottom: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.upload-compact {
    margin-top: 8px;
}

.upload-label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
    font-size: 14px;
}

.upload-label:hover {
    background: rgba(255, 255, 255, 0.15);
}

.upload-label .icon {
    font-size: 16px;
}


.status-compact {
    margin-top: 8px;
    font-size: 12px;
    padding: 6px 8px;
    border-radius: var(--radius-sm);
    display: none;
}

.status-compact.success {
    background: rgba(16, 185, 129, 0.2);
    color: #6ee7b7;
    display: block;
}

.status-compact.error {
    background: rgba(239, 68, 68, 0.2);
    color: #fca5a5;
    display: block;
}

.status-compact.loading {
    background: rgba(99, 102, 241, 0.2);
    color: #a5b4fc;
    display: block;
}

.stats-compact {
    margin-top: 8px;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    font-size: 13px;
    color: var(--text-tertiary);
}

.stat-row strong {
    color: var(--text-inverse);
}

.sidebar-footer {
    margin-top: auto;
    padding: 20px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-link {
    color: rgba(255, 255, 255, 0.7);
    text-decoration: none;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 16px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: var(--radius-md);
    transition: var(--transition);
    font-weight: 500;
}

.sidebar-link:hover {
    color: var(--text-inverse);
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-1px);
}

.sidebar-link i {
    font-size: 16px;
}

.back-link {
    color: var(--text-tertiary);
    text-decoration: none;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: var(--transition);
}

.back-link:hover {
    color: var(--text-inverse);
}

/* Основная область чата */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    overflow: hidden;
}

.chat-header {
    padding: 24px 32px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-primary);
}

.chat-header h1 {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 4px;
    color: var(--text-primary);
}

.chat-header p {
    font-size: 14px;
    color: var(--text-secondary);
}

/* Область сообщений */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 24px 32px;
    scroll-behavior: smooth;
}

.messages-container::-webkit-scrollbar {
    width: 8px;
}

.messages-container::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

.messages-container::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
    background: var(--text-tertiary);
}

/* Обертки сообщений */
.message-wrapper {
    display: flex;
    gap: 16px;
    margin-bottom: 24px;
    max-width: 900px;
}

.user-wrapper {
    margin-left: auto;
    flex-direction: row-reverse;
}

.assistant-wrapper {
    margin-right: auto;
}

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
    background: var(--bg-tertiary);
}

.user-wrapper .message-avatar {
    background: var(--primary-color);
    color: white;
}

/* Сообщения */
.message {
    padding: 16px 20px;
    border-radius: var(--radius-lg);
    max-width: 700px;
}

.user-message {
    background: var(--primary-color);
    color: white;
    border-bottom-right-radius: 4px;
}

.assistant-message {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

.message-content p {
    margin-bottom: 12px;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content ul,
.message-content ol {
    margin: 12px 0;
    padding-left: 24px;
}

.message-content li {
    margin-bottom: 6px;
}

.message-content code {
    background: rgba(0, 0, 0, 0.05);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Monaco', 'Courier New', monospace;
    font-size: 13px;
}

.user-message code {
    background: rgba(255, 255, 255, 0.2);
}

/* Источники */
.message-sources {
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px solid var(--border-color);
}

.sources-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.source-card {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 12px;
    margin-bottom: 8px;
    transition: var(--transition);
    cursor: pointer;
}

.source-card:hover {
    border-color: var(--primary-color);
    box-shadow: var(--shadow-sm);
}

.source-card:last-child {
    margin-bottom: 0;
}

.source-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.source-snippet {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.5;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

/* Индикатор печатания */
.typing-indicator {
    display: flex;
    gap: 16px;
    margin-bottom: 24px;
    max-width: 900px;
}

.typing-dots {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 16px 20px;
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    border-bottom-left-radius: 4px;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background: var(--text-tertiary);
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

.typing-dots span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.5;
    }
    30% {
        transform: translateY(-10px);
        opacity: 1;
    }
}

/* Поле ввода */
.input-container {
    padding: 16px 32px 24px;
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
}

.input-wrapper {
    display: flex;
    gap: 12px;
    align-items: flex-end;
    max-width: 900px;
    margin: 0 auto;
}

#messageInput {
    flex: 1;
    padding: 14px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    font-family: inherit;
    font-size: 15px;
    line-height: 1.5;
    resize: none;
    max-height: 200px;
    transition: var(--transition);
    background: var(--bg-primary);
    color: var(--text-primary);
}

#messageInput:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

#messageInput::placeholder {
    color: var(--text-tertiary);
}

.btn-send {
    width: 48px;
    height: 48px;
    border: none;
    border-radius: var(--radius-lg);
    background: var(--primary-color);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
}

.btn-send:hover:not(:disabled) {
    background: #4f46e5;
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-send:disabled {
    background: var(--bg-tertiary);
    color: var(--text-tertiary);
    cursor: not-allowed;
}

.input-footer {
    text-align: center;
    margin-top: 8px;
    max-width: 900px;
    margin-left: auto;
    margin-right: auto;
}

.input-footer small {
    color: var(--text-tertiary);
    font-size: 12px;
}

/* Адаптивность */
@media (max-width: 968px) {
    .sidebar {
        display: none;
    }
    
    .messages-container {
        padding: 16px;
    }
    
    .chat-header {
        padding: 16px;
    }
    
    .input-container {
        padding: 12px 16px 16px;
    }
    
    .message-wrapper {
        max-width: 100%;
    }
}

@media (max-width: 640px) {
    .chat-header h1 {
        font-size: 20px;
    }
    
    .message {
        padding: 12px 16px;
        max-width: 100%;
    }
    
    .message-avatar {
        width: 32px;
        height: 32px;
        font-size: 18px;
    }
}

/* Утилиты */
.hidden {
    display: none !important;
}

.fade-in {
    animation: fadeIn 0.3s ease;
}

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

/* Стили для синхронизации Bitrix24 */
.btn-sync-bitrix {
    width: 100%;
    padding: 10px 16px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-bottom: 12px;
}

.btn-sync-bitrix:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn-sync-bitrix:active:not(:disabled) {
    transform: translateY(0);
}

.btn-sync-bitrix:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.sync-info {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.sync-status-block {
    background: rgba(255, 255, 255, 0.05);
    border-radius: var(--radius-sm);
    padding: 8px 12px;
}

.sync-status-block .stat-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 4px 0;
    gap: 8px;
}

.sync-status-block .stat-row span {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.6);
}

.sync-status-block .stat-row strong,
.sync-status-block .stat-row small {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.9);
    text-align: right;
    flex: 1;
    line-height: 1.4;
}

.sync-status-block .stat-row small {
    color: rgba(255, 255, 255, 0.7);
    font-size: 11px;
}

.status-compact.loading {
    color: #fbbf24;
}

.status-compact.success {
    color: #10b981;
}

.status-compact.error {
    color: #ef4444;
}

/* ============================================
   ИСТОРИЯ ДИАЛОГОВ
   ============================================ */

.chat-history-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    margin-top: 20px;
}

.chat-history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.chat-history-header h3 {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
    color: var(--text-inverse);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.chat-history-refresh {
    background: none;
    border: none;
    color: var(--text-inverse);
    font-size: 14px;
    cursor: pointer;
    padding: 6px;
    border-radius: 4px;
    transition: all 0.2s ease;
    opacity: 0.7;
}

.chat-history-refresh:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.1);
}

.chat-history-refresh i {
    display: block;
}

.chat-history-list {
    flex: 1;
    overflow-y: auto;
    padding: 12px 8px;
}

/* Scrollbar для истории */
.chat-history-list::-webkit-scrollbar {
    width: 6px;
}

.chat-history-list::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 3px;
}

.chat-history-list::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
}

.chat-history-list::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Загрузка */
.chat-history-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    gap: 12px;
}

.loading-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.chat-history-loading span {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.6);
}

/* Пустое состояние */
.chat-history-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    text-align: center;
}

.chat-history-empty i {
    font-size: 48px;
    color: rgba(255, 255, 255, 0.3);
    margin-bottom: 12px;
}

.chat-history-empty p {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.5);
    margin: 0;
}

/* Группа диалогов (по датам) */
.chat-history-group {
    margin-bottom: 20px;
}

.chat-history-group-title {
    font-size: 11px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.5);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 8px 12px;
    margin-bottom: 8px;
}

/* Элемент диалога */
.chat-history-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
    margin-bottom: 4px;
}

.chat-history-item:hover {
    background: rgba(255, 255, 255, 0.1);
}

.chat-history-item.active {
    background: var(--primary-color);
}

.chat-history-item.active .chat-history-item-icon {
    color: white;
}

.chat-history-item.active .chat-history-item-title,
.chat-history-item.active .chat-history-item-preview,
.chat-history-item.active .chat-history-item-time {
    color: white;
}

/* Иконка диалога */
.chat-history-item-icon {
    font-size: 18px;
    color: var(--primary-color);
    flex-shrink: 0;
}

/* Контент диалога */
.chat-history-item-content {
    flex: 1;
    min-width: 0;
}

.chat-history-item-title {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-inverse);
    margin-bottom: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-history-item-preview {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.6);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-history-item-time {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.5);
    margin-top: 4px;
}

/* Кнопка удаления */
.chat-history-item-delete {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.5);
    font-size: 14px;
    cursor: pointer;
    padding: 6px;
    border-radius: 4px;
    transition: all 0.2s ease;
    opacity: 0;
    flex-shrink: 0;
}

.chat-history-item:hover .chat-history-item-delete {
    opacity: 1;
}

.chat-history-item-delete:hover {
    color: var(--error-color);
    background: rgba(239, 68, 68, 0.1);
}

.chat-history-item.active .chat-history-item-delete:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Responsive */
@media (max-width: 968px) {
    .chat-history-section {
        display: none; /* Скрыть на мобильных, показывать через меню */
    }
}
