/* ─── Reset & Variables ───────────────────────────── */
:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-sidebar: #0f1626;
    --bg-input: #1c2541;
    --bg-message-user: #3a506b;
    --bg-message-ai: #1c2541;
    --bg-hover: #253554;
    --text-primary: #e0e0e0;
    --text-secondary: #8892a4;
    --text-muted: #5c6578;
    --accent: #4a9eff;
    --accent-hover: #3a8eef;
    --danger: #e74c3c;
    --success: #2ecc71;
    --warning: #f39c12;
    --border: #2a3a5c;
    --radius: 12px;
    --radius-sm: 8px;
    --sidebar-width: 300px;
    --header-height: 60px;
    --input-height: 80px;
}

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

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
}

/* ─── Login Page ──────────────────────────────────── */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(135deg, var(--bg-primary), var(--bg-secondary));
}

.login-container {
    width: 100%;
    max-width: 400px;
    padding: 20px;
}

.login-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 40px 30px;
    text-align: center;
}

.login-card h1 {
    font-size: 2em;
    margin-bottom: 8px;
}

.login-card .subtitle {
    color: var(--text-secondary);
    margin-bottom: 30px;
}

.form-group {
    text-align: left;
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    color: var(--text-secondary);
    font-size: 0.9em;
}

.form-group input {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 1em;
    outline: none;
    transition: border-color 0.2s;
}

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

.btn-login {
    width: 100%;
    padding: 14px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: white;
    font-size: 1.05em;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

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

.error-message {
    background: rgba(231, 76, 60, 0.15);
    color: var(--danger);
    padding: 12px;
    border-radius: var(--radius-sm);
    margin-bottom: 20px;
    border: 1px solid rgba(231, 76, 60, 0.3);
}

/* ─── App Layout ──────────────────────────────────── */
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* ─── Sidebar ─────────────────────────────────────── */
.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
    z-index: 100;
}

.sidebar.collapsed {
    transform: translateX(-100%);
    position: absolute;
    height: 100%;
}

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

.sidebar-header h2 {
    font-size: 1.15em;
    font-weight: 600;
}

.btn-new-chat {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-primary);
    font-size: 1.3em;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

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

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

.chat-item {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    margin-bottom: 2px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.15s;
    gap: 10px;
}

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

.chat-item.active {
    background: var(--bg-hover);
    border: 1px solid var(--border);
}

.chat-item-icon {
    font-size: 1.1em;
    flex-shrink: 0;
}

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

.chat-item-title {
    font-size: 0.9em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-date {
    font-size: 0.75em;
    color: var(--text-muted);
    margin-top: 2px;
}

.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 12px 16px;
}

.user-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.user-name {
    font-size: 0.9em;
    color: var(--text-secondary);
}

.btn-logout {
    text-decoration: none;
    font-size: 1.2em;
    opacity: 0.6;
    transition: opacity 0.2s;
}

.btn-logout:hover {
    opacity: 1;
}

/* ─── Main Area ───────────────────────────────────── */
.main-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* ─── Chat Header ─────────────────────────────────── */
.chat-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    padding: 0 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
    gap: 12px;
    flex-shrink: 0;
}

.btn-toggle-sidebar {
    display: none;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 1.4em;
    cursor: pointer;
    padding: 4px;
}

.chat-title {
    flex: 1;
    font-size: 1.05em;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

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

.model-selector {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 0.85em;
    cursor: pointer;
    outline: none;
    max-width: 220px;
}

.model-selector:focus {
    border-color: var(--accent);
}

.btn-icon {
    background: none;
    border: none;
    font-size: 1.1em;
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    transition: background 0.15s;
}

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

.btn-danger:hover {
    background: rgba(231, 76, 60, 0.2);
}

/* ─── Messages ────────────────────────────────────── */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    scroll-behavior: smooth;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-secondary);
    text-align: center;
}

.empty-icon {
    font-size: 4em;
    margin-bottom: 16px;
}

.empty-state h3 {
    font-size: 1.4em;
    margin-bottom: 8px;
    color: var(--text-primary);
}

.empty-state p {
    margin-bottom: 24px;
}

.btn-primary {
    padding: 12px 28px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: white;
    font-size: 1em;
    cursor: pointer;
    transition: background 0.2s;
}

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

.message {
    max-width: 800px;
    margin: 0 auto 16px;
    display: flex;
    gap: 12px;
    animation: fadeIn 0.3s ease;
}

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

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1em;
    flex-shrink: 0;
    margin-top: 4px;
}

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

.message.assistant .message-avatar {
    background: #6c5ce7;
}

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

.message-sender {
    font-size: 0.8em;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.message-content {
    padding: 12px 16px;
    border-radius: var(--radius);
    line-height: 1.6;
    font-size: 0.95em;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message.user .message-content {
    background: var(--bg-message-user);
    border-top-left-radius: 4px;
}

.message.assistant .message-content {
    background: var(--bg-message-ai);
    border-top-left-radius: 4px;
}

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

.message-content pre {
    background: #0d1117;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 16px;
    margin: 10px 0;
    overflow-x: auto;
    position: relative;
}

.message-content code {
    font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 0.88em;
}

.message-content :not(pre) > code {
    background: rgba(255, 255, 255, 0.08);
    padding: 2px 6px;
    border-radius: 4px;
}

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

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

.message-content blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 14px;
    color: var(--text-secondary);
    margin: 10px 0;
}

.message-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 10px 0;
}

.message-content th, .message-content td {
    border: 1px solid var(--border);
    padding: 8px 12px;
    text-align: left;
}

.message-content th {
    background: var(--bg-hover);
}

.message-meta {
    font-size: 0.75em;
    color: var(--text-muted);
    margin-top: 6px;
}

.copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    background: var(--bg-hover);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-secondary);
    padding: 4px 10px;
    cursor: pointer;
    font-size: 0.78em;
    opacity: 0;
    transition: opacity 0.2s;
}

.message-content pre:hover .copy-btn {
    opacity: 1;
}

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

/* ─── Input Area ──────────────────────────────────── */
.input-area {
    padding: 12px 20px 16px;
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
    flex-shrink: 0;
}

.input-wrapper {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 10px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px 12px;
    transition: border-color 0.2s;
}

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

#message-input {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 1em;
    font-family: inherit;
    resize: none;
    outline: none;
    max-height: 200px;
    line-height: 1.5;
    padding: 4px 0;
}

#message-input::placeholder {
    color: var(--text-muted);
}

.btn-send {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    font-size: 1.1em;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

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

.btn-send:disabled {
    background: var(--text-muted);
    cursor: not-allowed;
    transform: none;
}

.input-footer {
    max-width: 800px;
    margin: 6px auto 0;
    display: flex;
    justify-content: space-between;
    font-size: 0.78em;
    color: var(--text-muted);
    padding: 0 4px;
}

.typing-indicator {
    color: var(--accent);
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 1; }
}

/* ─── Scrollbar ───────────────────────────────────── */
::-webkit-scrollbar {
    width: 6px;
}

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

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

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

/* ─── Mobile ──────────────────────────────────────── */
@media (max-width: 768px) {
    .sidebar {
        position: absolute;
        height: 100%;
        transform: translateX(-100%);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .btn-toggle-sidebar {
        display: block;
    }

    .chat-header-actions {
        gap: 4px;
    }

    .model-selector {
        max-width: 140px;
        font-size: 0.8em;
    }

    .messages-container {
        padding: 12px;
    }

    .message {
        gap: 8px;
    }

    .message-avatar {
        width: 30px;
        height: 30px;
        font-size: 0.9em;
    }
}

/* ─── Overlay для мобильного сайдбара ─────────────── */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
}

.sidebar-overlay.visible {
    display: block;
}
.btn-attach {
    background: none;
    border: none;
    font-size: 1.3em;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    transition: background 0.15s;
    flex-shrink: 0;
}

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

.image-preview {
    color: var(--accent);
    font-size: 0.85em;
}