/* Основные стили */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --bg-primary: #fff;
    --bg-secondary: #f7f7f8;
    --bg-hover: #ececf1;
    --text-primary: #2c3e50;
    --text-secondary: #6c757d;
    --border-color: #e5e7eb;
    --accent-color: #0066ff;
    --accent-hover: #0052cc;
    --sidebar-width: 280px;
    --danger-color: #dc3545;
    --success-color: #28a745;
}

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

/* === LAYOUT === */
.app-container {
    display: flex;
    height: 100vh;
    height: 100dvh;
    width: 100vw;
    overflow: hidden;
}

/* === SIDEBAR === */
.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
    flex-shrink: 0;
}

.sidebar.hidden {
    transform: translateX(-100%);
    width: 0;
    overflow: hidden;
    border: none;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.new-chat-btn {
    flex: 1;
    background: var(--accent-color);
    color: white;
    border: none;
    padding: 10px 16px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

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

.chats-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    -webkit-overflow-scrolling: touch;
}

.chat-item {
    padding: 12px;
    margin-bottom: 4px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

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

.chat-item.active {
    background: var(--bg-hover);
}

.chat-item-content {
    flex: 1;
    min-width: 0;
}

.chat-item-title {
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-meta {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 2px;
}

.chat-item-actions {
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.2s;
}

.chat-item:hover .chat-item-actions {
    opacity: 1;
}

.chat-action-btn {
    background: transparent;
    border: none;
    padding: 4px 8px;
    cursor: pointer;
    color: var(--text-secondary);
    border-radius: 4px;
    font-size: 14px;
}

.chat-action-btn:hover {
    background: var(--bg-primary);
}

.sidebar-footer {
    padding: 16px;
    border-top: 1px solid var(--border-color);
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
}

.user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
}

.user-info:hover {
    background: var(--bg-hover);
}

.user-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--accent-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 16px;
}

.user-details {
    flex: 1;
    min-width: 0;
}

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

.user-balance {
    font-size: 12px;
    color: var(--text-secondary);
}

/* === MAIN CHAT AREA === */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    max-width: 100%;
    position: relative;
    min-width: 0;
    height: 100%;
}

.chat-header {
    padding: 16px 24px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-primary);
    flex-shrink: 0;
    padding-top: calc(16px + env(safe-area-inset-top)); 
    position: relative;
    z-index: 10;
}

.mobile-toggle {
    display: none;
    background: transparent;
    border: none;
    padding: 8px;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 20px;
    margin-right: 8px;
}

/* --- MODEL SELECTOR STYLES --- */
.model-selector {
    position: relative;
    margin-left: 0;
}

.model-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 12px;
    transition: background 0.2s;
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
}

.model-btn:hover {
    background: var(--bg-hover);
}

.model-btn span {
    max-width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.model-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    margin-top: 8px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.1);
    width: 280px;
    z-index: 100;
    padding: 6px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.model-option {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
}

.model-option:hover {
    background: var(--bg-hover);
}

.model-option.selected {
    background: var(--bg-secondary);
}

.model-icon {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    background: white;
    border: 1px solid var(--border-color);
    font-size: 16px;
    color: var(--text-primary);
}

.model-info {
    flex: 1;
}

.model-name {
    font-weight: 600;
    font-size: 14px;
    color: var(--text-primary);
}

.model-desc {
    font-size: 12px;
    color: var(--text-secondary);
}

.model-check {
    color: var(--accent-color);
    font-size: 14px;
}

.chat-actions {
    display: flex;
    gap: 8px;
}

.header-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    padding: 8px 16px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.header-btn:hover {
    background: var(--bg-hover);
}

.mobile-new-chat {
    display: none;
    width: 36px;
    height: 36px;
    padding: 0;
    justify-content: center;
    border: none;
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

/* SEO Content Styles */
.seo-content {
    margin-top: 48px;
    border-top: 1px solid var(--border-color);
    padding-top: 32px;
    text-align: left;
    color: var(--text-secondary);
}

.seo-content h2 {
    font-size: 18px;
    color: var(--text-primary);
    margin-bottom: 16px;
    font-weight: 600;
}

.seo-content h3 {
    font-size: 16px;
    color: var(--text-primary);
    margin: 20px 0 10px 0;
    font-weight: 600;
}

.seo-content p {
    font-size: 14px;
    line-height: 1.6;
    margin-bottom: 12px;
}

.seo-content ul {
    font-size: 14px;
    line-height: 1.6;
    margin-bottom: 16px;
    padding-left: 20px;
}

.message {
    max-width: 800px;
    margin: 0 auto 24px auto;
    display: flex;
    gap: 16px;
}

.message.user {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 16px;
}

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

.message.assistant .message-avatar {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.message-content {
    flex: 1;
    min-width: 0;
}

.message.user .message-content {
    background: var(--accent-color);
    color: white;
    padding: 12px 16px;
    border-radius: 16px 16px 4px 16px;
}

.message.assistant .message-content {
    padding: 12px 0;
    overflow-x: hidden;
}

.message-text {
    word-wrap: break-word;
    line-height: 1.6;
}

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

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

.message-text pre {
    background: #1e1e1e;
    padding: 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 12px 0;
}

.message-text code {
    background: rgba(0,0,0,0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 0.9em;
}

/* === MATHJAX STYLES === */
mjx-container {
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    vertical-align: middle;
    -webkit-overflow-scrolling: touch;
    padding: 2px 0;
}

mjx-container[display="true"] {
    display: block;
    margin: 12px 0;
    padding: 8px 0;
}

.mjx-table {
    margin: 0 !important;
}

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

.message-text pre code {
    background: transparent;
    padding: 0;
}

.message-image {
    max-width: 400px;
    width: 100%;
    border-radius: 8px;
    margin-top: 8px;
}

.message-time {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 6px;
}

.message.user .message-time {
    color: rgba(255,255,255,0.8);
}

.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
}

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

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

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

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

/* === INPUT AREA === */
.input-container {
    padding: 16px 24px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-primary);
    flex-shrink: 0;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
}

.input-wrapper {
    max-width: 800px;
    margin: 0 auto;
    position: relative;
}

.input-area {
    display: flex;
    gap: 12px;
    align-items: center !important; /* Центрируем элементы по вертикали */
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 12px;
}

.input-area.focused {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(0,102,255,0.1);
}

.image-upload-btn {
    background: transparent;
    border: none;
    padding: 8px;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 20px;
    border-radius: 8px;
    transition: all 0.2s;
}

.image-upload-btn:hover {
    background: var(--bg-hover);
    color: var(--accent-color);
}

.message-input {
    flex: 1;
    border: none;
    background: transparent;
    font-family: inherit;
    font-size: 15px;
    resize: none;
    outline: none;
    max-height: 200px;
    min-height: 24px;
}

.send-btn {
    background: var(--accent-color);
    color: white;
    border: none;
    padding: 10px 16px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 18px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.send-btn:hover:not(:disabled) {
    background: var(--accent-hover);
    transform: scale(1.05);
}

.send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.image-preview {
    margin-top: 12px;
    position: relative;
    display: inline-block;
}

.image-preview img {
    max-width: 200px;
    border-radius: 8px;
}

.remove-image-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(0,0,0,0.7);
    color: white;
    border: none;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* === WELCOME SCREEN === */
.welcome-screen {
    max-width: 800px;
    margin: 0 auto;
    padding: 48px 24px;
    text-align: center;
}

.welcome-title {
    font-size: 36px;
    font-weight: 700;
    margin-bottom: 16px;
    background: linear-gradient(135deg, var(--accent-color), #7c3aed);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.welcome-subtitle {
    font-size: 18px;
    color: var(--text-secondary);
    margin-bottom: 48px;
}

.suggestions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-top: 32px;
}

.suggestion-card {
    background: var(--bg-secondary);
    padding: 20px;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid var(--border-color);
}

.suggestion-card:hover {
    background: var(--bg-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.suggestion-icon {
    font-size: 24px;
    margin-bottom: 12px;
}

.suggestion-text {
    font-size: 14px;
    font-weight: 500;
}

/* === ALERTS === */
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    gap: 12px;
}

.alert-warning {
    background: #fff3cd;
    border: 1px solid #ffc107;
    color: #856404;
}

.alert-info {
    background: #d1ecf1;
    border: 1px solid #17a2b8;
    color: #0c5460;
}

/* === RESPONSIVE === */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        height: 100dvh;
        z-index: 1000;
        box-shadow: 2px 0 8px rgba(0,0,0,0.1);
        transform: translateX(-100%);
        width: 280px !important;
    }
    
    .sidebar.active {
        transform: translateX(0);
    }
    
    .sidebar.hidden {
        transform: translateX(-100%);
    }
    
    .mobile-toggle {
        display: block;
    }

    .mobile-new-chat {
        display: flex;
    }
    
    .messages-container {
        padding: 16px;
    }
    
    .input-container {
        padding: 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
    }
    
    .welcome-title {
        font-size: 28px;
    }
    
    .suggestions-grid {
        grid-template-columns: 1fr;
    }
    
    /* ОБНОВЛЕННАЯ СТРУКТУРА ШАПКИ ДЛЯ МОБИЛЬНЫХ */
    .chat-header {
        padding: 12px 16px;
        padding-top: calc(12px + env(safe-area-inset-top));
        flex-wrap: wrap; /* Разрешаем перенос элементов */
        gap: 12px; /* Отступ между строками */
        height: auto; /* Разрешаем увеличение высоты */
    }

    /* "Растворяем" обертку (первый div с inline стилями), чтобы элементы стали прямыми потомками .chat-header */
    .chat-header > div:first-child {
        display: contents !important;
    }

    /* Кнопка меню (бургер) */
    .mobile-toggle {
        order: 1;
        margin-right: 0;
    }

    /* Кнопки действий (справа) */
    .chat-actions {
        order: 2;
        margin-left: auto; /* Прижимаем вправо */
        gap: 8px;
    }

    /* Селектор режима (на новую строку) */
    .model-selector {
        order: 3;
        width: 100%; /* Занимает всю ширину */
        margin: 0;
    }

    .model-btn {
        width: 100%;
        justify-content: space-between;
        background: var(--bg-secondary);
        padding: 10px 12px;
    }

    .model-dropdown {
        width: 100%;
    }
    
    .sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.5);
        z-index: 999;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s;
        height: 100dvh;
    }
    
    .sidebar-overlay.active {
        opacity: 1;
        visibility: visible;
    }
    
    mjx-container {
        font-size: 85% !important; 
    }
    
    .message-text {
        overflow-x: hidden;
    }
    
    mjx-container[display="true"] {
            margin: 0.5em 0;
            max-width: 100%;
            overflow-x: auto;
    }

    /* СКРЫТИЕ ТЕКСТА КНОПОК НА МОБИЛЬНЫХ */
    .header-btn span {
        display: none !important;
    }
    
    .header-btn {
        padding: 8px 10px; /* Уменьшенные отступы для квадратных кнопок */
    }
}

/* === SCROLLBAR === */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

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

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

.hidden {
    display: none !important;
}

.hidden-desktop {
    display: none !important;
}

/* === ERROR DISPLAY STYLE (НОВОЕ) === */
.error-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 40px;
}

.error-icon {
    font-size: 64px;
    margin-bottom: 20px;
}

.error-title {
    color: var(--text-primary);
    margin-bottom: 10px;
    font-size: 24px;
    font-weight: 600;
}

.error-message {
    color: var(--text-secondary);
    font-size: 16px;
}

/* === SHARE CONTROLS === */
.share-controls {
    display: flex;
    gap: 8px;
    align-items: center;
    padding: 12px 16px;
    background: var(--bg-secondary);
    border-radius: 8px;
    margin-top: 12px;
}

.share-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
}

.share-toggle input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.share-link-container {
    display: flex;
    gap: 8px;
    flex: 1;
}

.share-link-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 14px;
    background: var(--bg-primary);
}

.copy-link-btn {
    background: var(--accent-color);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    white-space: nowrap;
    transition: background 0.2s;
}

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

.copy-link-btn.copied {
    background: var(--success-color);
}

.public-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: var(--success-color);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 500;
}

.private-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: var(--text-secondary);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 500;
}

/* === MODAL === */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    padding: 20px;
}

.modal-content {
    background: var(--bg-primary);
    border-radius: 12px;
    max-width: 500px;
    width: 100%;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
    overflow: hidden;
}

.modal-header {
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
}

.modal-close {
    background: transparent;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: var(--text-secondary);
    padding: 4px;
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
}

/* Анимации для уведомлений */
@keyframes slideIn {
    from {
        transform: translateX(400px);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes slideOut {
    from {
        transform: translateX(0);
        opacity: 1;
    }
    to {
        transform: translateX(400px);
        opacity: 0;
    }
}

/* === QUICK CHIPS (Стили для чипсов) === */
.quick-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 24px;
    justify-content: center;
}

.chip {
    padding: 10px 18px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 25px;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.02);
}

.chip:hover {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.2);
}

/* === FILE PREVIEW (Превью файлов в чате) === */
.file-preview-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: rgba(255,255,255,0.1);
    border-radius: 8px;
    max-width: 200px;
}
.file-preview-icon {
    font-size: 24px;
    color: var(--accent-color);
}
.file-preview-name {
    font-size: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-primary);
}

/* === Подвал сайдбара: Документы и Контакты === */

.sidebar-bottom-links {
    padding: 12px 16px;
    border-top: 1px solid var(--border-color, rgba(255,255,255,0.1));
    background: var(--bg-secondary, #131316);
    /* Если sidebar flex, это прижмет блок к низу */
    margin-top: auto; 
}

.sidebar-links-row {
    display: flex;
    justify-content: center;
    gap: 24px;
    margin-bottom: 8px;
}

.sidebar-link-mini {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--text-secondary, #888);
    text-decoration: none;
    font-size: 12px;
    transition: color 0.2s ease;
}

.sidebar-link-mini:hover {
    color: var(--text-primary, #fff);
}

.sidebar-link-mini i {
    font-size: 11px;
    opacity: 0.7;
}

.sidebar-copyright {
    text-align: center;
    font-size: 10px;
    color: var(--text-muted, #555);
    margin-top: 8px;
    user-select: none;
}

/* Адаптив */
@media (max-width: 768px) {
    .sidebar-bottom-links {
        background: var(--bg-primary, #0b0c10);
        padding-bottom: 20px; /* Безопасная зона для мобильных */
    }
}