:root {
    --bg: #0d0d0d;
    --panel: #1a1a1a;
    --panel-2: #141414;
    --border: #2a2a2a;
    --text: #e5e5e5;
    --text-dim: #888;
    --accent: #4f6ef7;
    --accent-hover: #3d5ce5;
    --user-bg: #1e3a5f;
    --code-bg: #0a0a0a;
    --danger: #dc2626;
}
[data-theme="light"] {
    --bg: #f5f5f5;
    --panel: #ffffff;
    --panel-2: #fafafa;
    --border: #e0e0e0;
    --text: #1a1a1a;
    --text-dim: #666;
    --accent: #4f6ef7;
    --accent-hover: #3d5ce5;
    --user-bg: #dbeafe;
    --code-bg: #f0f0f0;
    --danger: #dc2626;
}
* { box-sizing: border-box; }
html, body {
    height: 100%; margin: 0; padding: 0;
    background: var(--bg); color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    overflow: hidden;
}
.app { display: flex; height: 100vh; }

/* ===== Иконки ===== */
.icon {
    width: 16px;
    height: 16px;
    vertical-align: middle;
    pointer-events: none;
    display: inline-block;
    object-fit: contain;
}
[data-theme="dark"] .icon {
    filter: invert(1);
}
.theme-btn .icon,
.toggle-btn .icon {
    width: 18px;
    height: 18px;
}
.send-btn .icon,
.attach-btn .icon {
    width: 18px;
    height: 18px;
}
.copy-btn .icon {
    width: 12px;
    height: 12px;
}
.avatar .icon {
    width: 18px;
    height: 18px;
}

/* ===== Боковая панель ===== */
.sidebar {
    width: 280px; background: var(--panel-2);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    flex-shrink: 0;
    transition: margin-left 0.25s ease;
}
.sidebar.collapsed { margin-left: -280px; }
.sidebar-header {
    padding: 14px;
    border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
}
.sidebar-header .logo {
    font-size: 0.95rem; font-weight: 600;
    display: flex; align-items: center; gap: 8px;
}
.search-box {
    padding: 8px 14px;
    border-bottom: 1px solid var(--border);
}
.search-box input {
    width: 100%; background: var(--bg);
    border: 1px solid var(--border); color: var(--text);
    padding: 6px 10px; border-radius: 8px;
    font-size: 0.8rem; outline: none;
}
.new-chat-btn {
    padding: 10px 14px;
    background: var(--accent); color: white;
    border: none; border-radius: 10px;
    cursor: pointer; font-weight: 600; font-size: 0.85rem;
    display: flex; align-items: center; justify-content: center; gap: 6px;
    margin: 12px 14px; width: calc(100% - 28px);
    transition: background 0.2s;
}
.new-chat-btn:hover { background: var(--accent-hover); }
.new-chat-btn .icon { filter: invert(1); }
.chat-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.chat-item {
    padding: 10px 12px; border-radius: 8px;
    cursor: pointer; margin-bottom: 4px;
    display: flex; align-items: center; justify-content: space-between;
    gap: 8px; transition: background 0.15s; font-size: 0.85rem;
}
.chat-item:hover { background: rgba(127,127,127,0.1); }
.chat-item.active { background: rgba(79,110,247,0.15); }
.chat-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .del {
    background: transparent; border: none;
    color: var(--text-dim); cursor: pointer;
    font-size: 0.9rem; padding: 0 4px;
    opacity: 0; transition: opacity 0.15s;
}
.chat-item:hover .del { opacity: 1; }
.chat-item .del:hover { color: var(--danger); }
.sidebar-footer {
    padding: 12px 14px;
    border-top: 1px solid var(--border);
    font-size: 0.75rem; color: var(--text-dim);
}
.status-dot {
    display: inline-block; width: 6px; height: 6px;
    border-radius: 50%; background: #ef4444;
    margin-right: 5px; vertical-align: middle;
}
.status-dot.online { background: #22c55e; }
.sidebar-footer .settings-link {
    display: block; margin-top: 8px;
    color: var(--text-dim); cursor: pointer;
    text-decoration: none; font-size: 0.75rem;
}
.sidebar-footer .settings-link:hover { color: var(--accent); }

/* ===== Основная область ===== */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    display: flex; align-items: center; gap: 12px;
    flex-shrink: 0;
}
.topbar .toggle-btn {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-dim); width: 32px; height: 32px;
    border-radius: 8px; cursor: pointer; font-size: 1rem;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s;
}
.topbar .toggle-btn:hover { color: var(--accent); border-color: var(--accent); }
.topbar .current-title {
    font-size: 0.9rem; font-weight: 600;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    cursor: pointer;
}
.topbar .current-title:hover { color: var(--accent); }
.topbar .spacer { flex: 1; }
.topbar .token-info { font-size: 0.72rem; color: var(--text-dim); }
.topbar .theme-btn {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-dim); width: 32px; height: 32px;
    border-radius: 8px; cursor: pointer; font-size: 1rem;
    display: flex; align-items: center; justify-content: center;
}

/* ===== Область чата ===== */
.chat-area {
    flex: 1; overflow-y: auto;
    padding: 24px 0;
    display: flex; flex-direction: column; align-items: center;
}
.chat-inner {
    width: 100%; max-width: 780px;
    padding: 0 20px;
    display: flex; flex-direction: column; gap: 20px;
}
.welcome {
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    flex: 1; padding: 40px 20px; text-align: center;
}
.welcome .title {
    font-size: 1.5rem; font-weight: 600;
    display: flex; align-items: center; gap: 10px;
    margin-bottom: 8px;
}
.welcome .subtitle { color: var(--text-dim); font-size: 0.9rem; }

/* ===== Сообщения ===== */
.message { display: flex; gap: 12px; animation: fadeIn 0.2s ease; position: relative; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.message .avatar {
    width: 30px; height: 30px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.9rem; flex-shrink: 0;
    background: var(--panel); border: 1px solid var(--border);
}
.message.user .avatar { background: var(--user-bg); border-color: var(--user-bg); }
.message .body { flex: 1; min-width: 0; padding-top: 4px; }
.message .name {
    font-size: 0.75rem; color: var(--text-dim);
    margin-bottom: 4px; font-weight: 600;
    display: flex; align-items: center; gap: 8px;
}
.message .name .actions {
    opacity: 0; transition: opacity 0.15s;
    display: flex; gap: 4px;
}
.message:hover .name .actions { opacity: 1; }
.message .name .actions button {
    background: transparent; border: none;
    color: var(--text-dim); cursor: pointer;
    font-size: 0.7rem; padding: 0 2px;
}
.message .name .actions button:hover { color: var(--accent); }
.message .content { line-height: 1.65; font-size: 0.94rem; word-wrap: break-word; }

/* ===== Markdown ===== */
.content p { margin: 0 0 10px 0; }
.content p:last-child { margin-bottom: 0; }
.content h1, .content h2, .content h3 { margin: 16px 0 8px; font-weight: 600; }
.content h1 { font-size: 1.3em; }
.content h2 { font-size: 1.15em; }
.content h3 { font-size: 1.05em; }
.content ul, .content ol { margin: 8px 0 8px 22px; padding: 0; }
.content li { margin: 3px 0; }
.content code {
    background: rgba(127,127,127,0.15); padding: 2px 6px;
    border-radius: 4px; font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.88em; color: #fbbf24;
}

/* ===== Блок кода ===== */
.code-block-wrapper {
    position: relative; margin: 10px 0;
    border: 1px solid var(--border);
    border-radius: 0; overflow: hidden;
    background: var(--code-bg);
}
.code-header {
    position: absolute; top: 0; left: 0; right: 0;
    display: flex; justify-content: space-between; align-items: center;
    padding: 4px 10px;
    background: rgba(0,0,0,0.55);
    backdrop-filter: blur(4px);
    border-bottom: 1px solid var(--border);
    font-size: 0.72rem; color: var(--text-dim);
    z-index: 2;
}
.code-header .lang { text-transform: lowercase; }
.code-header .copy-btn {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-dim); padding: 3px 8px;
    border-radius: 0; cursor: pointer; font-size: 0.7rem;
    transition: all 0.2s;
    display: flex; align-items: center; gap: 4px;
}
.code-header .copy-btn:hover { color: var(--accent); border-color: var(--accent); }
.code-header .copy-btn.copied { color: #22c55e; border-color: #22c55e; }
.code-header .copy-btn .icon { filter: invert(1); }
[data-theme="light"] .code-header .copy-btn .icon { filter: none; }
.content pre {
    background: var(--code-bg); border: none;
    border-radius: 0; padding: 0; margin: 0;
    overflow-x: auto;
}
.content pre code {
    display: block; padding: 34px 14px 14px 14px;
    background: transparent; color: var(--text);
    font-size: 0.85em; font-family: 'Consolas', 'Monaco', monospace;
    line-height: 1.5;
}
.content blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 12px; margin: 8px 0; color: var(--text-dim);
}
.content table { border-collapse: collapse; margin: 10px 0; font-size: 0.85em; }
.content th, .content td { border: 1px solid var(--border); padding: 5px 10px; }
.content th { background: rgba(127,127,127,0.08); }
.content a { color: #6ea8fe; text-decoration: none; }
.content a:hover { text-decoration: underline; }
.cursor::after {
    content: "▋"; animation: blink 1s steps(2) infinite; color: var(--accent);
}
@keyframes blink { 50% { opacity: 0; } }

.msg-attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.msg-attachments img {
    max-width: 200px; max-height: 200px;
    border-radius: 8px; cursor: zoom-in;
    border: 1px solid var(--border);
}
.msg-attachments .file-chip {
    display: inline-flex; align-items: center; gap: 6px;
    background: rgba(127,127,127,0.1);
    padding: 6px 10px; border-radius: 6px;
    font-size: 0.78rem; border: 1px solid var(--border);
}

/* ===== Поле ввода ===== */
.input-wrap { width: 100%; max-width: 780px; margin: 0 auto; padding: 0 20px 20px; }
.input-box {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 12px 14px;
    transition: border-color 0.2s;
    position: relative;
}
.input-box:focus-within { border-color: var(--accent); }
textarea#user-input {
    width: 100%; background: transparent; border: none;
    color: var(--text); font-size: 0.95rem;
    resize: none; outline: none; font-family: inherit;
    min-height: 24px; max-height: 200px;
    line-height: 1.5; padding: 0;
}
textarea#user-input::placeholder { color: var(--text-dim); }
.input-actions {
    display: flex; justify-content: space-between; align-items: center;
    margin-top: 10px; gap: 8px;
}
.input-actions .left { display: flex; gap: 6px; align-items: center; }
.icon-btn {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-dim); width: 32px; height: 32px;
    border-radius: 8px; cursor: pointer; font-size: 0.9rem;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s;
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); }
.icon-btn.has-files { color: var(--accent); border-color: var(--accent); }
.send-btn {
    background: var(--accent); color: white;
    border: none; width: 36px; height: 36px;
    border-radius: 10px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    font-size: 1rem; transition: background 0.2s;
}
.send-btn:hover:not(:disabled) { background: var(--accent-hover); }
.send-btn:disabled { background: #333; cursor: not-allowed; }
.send-btn.stop { background: var(--danger); }
.send-btn .icon { filter: invert(1); }
[data-theme="light"] .send-btn .icon { filter: none; }

.attachments-preview { display: none; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.attachments-preview.visible { display: flex; }
.att-preview-item {
    position: relative; width: 56px; height: 56px;
    border-radius: 8px; overflow: hidden;
    border: 1px solid var(--border); background: #000;
}
.att-preview-item img { width: 100%; height: 100%; object-fit: cover; }
.att-preview-item.file {
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    background: var(--panel); color: var(--text-dim);
    font-size: 0.55rem; text-align: center; padding: 2px;
}
.att-preview-item.file .icon { font-size: 1.2rem; }
.att-preview-item .remove {
    position: absolute; top: 2px; right: 2px;
    width: 16px; height: 16px; background: rgba(0,0,0,0.8);
    color: white; border-radius: 50%; border: none;
    cursor: pointer; font-size: 10px; line-height: 1;
    display: flex; align-items: center; justify-content: center; padding: 0;
}
.att-preview-item .remove:hover { background: var(--danger); }

/* ===== Предложка ===== */
.suggestions-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 10px;
    padding: 0 2px;
}
.suggestion-btn {
    background: var(--panel);
    border: 1px solid var(--border);
    color: var(--text-dim);
    padding: 6px 12px;
    border-radius: 16px;
    cursor: pointer;
    font-size: 0.78rem;
    transition: all 0.2s;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.suggestion-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
}

/* ===== Модалка настроек ===== */
.settings-overlay {
    position: fixed; inset: 0; z-index: 200;
    background: rgba(0,0,0,0.7);
    display: none; align-items: center; justify-content: center;
    padding: 20px;
}
.settings-overlay.visible { display: flex; }
.settings-panel {
    background: var(--panel); border: 1px solid var(--border);
    border-radius: 16px; padding: 24px;
    width: 100%; max-width: 460px;
    max-height: 85vh; overflow-y: auto;
}
.settings-panel h3 {
    margin: 0 0 16px; font-size: 1rem;
    display: flex; justify-content: space-between; align-items: center;
}
.settings-panel h3 button {
    background: transparent; border: none;
    color: var(--text-dim); cursor: pointer; font-size: 1.2rem;
}
.settings-panel .field { margin-bottom: 14px; }
.settings-panel .field label {
    display: flex; justify-content: space-between;
    font-size: 0.82rem; color: var(--text-dim); margin-bottom: 6px;
}
.settings-panel .field label span { color: var(--accent); font-weight: 600; }
.settings-panel input[type="text"],
.settings-panel input[type="number"],
.settings-panel textarea,
.settings-panel select {
    width: 100%; background: var(--bg);
    border: 1px solid var(--border); color: var(--text);
    padding: 8px 10px; border-radius: 8px;
    font-size: 0.85rem; outline: none; font-family: inherit;
}
.settings-panel textarea { resize: vertical; min-height: 70px; }
.settings-panel input[type="range"] { width: 100%; accent-color: var(--accent); }
.settings-panel .hint { font-size: 0.7rem; color: var(--text-dim); opacity: 0.7; margin-top: 4px; }
.settings-panel .divider { height: 1px; background: var(--border); margin: 16px 0; }
.settings-panel .btn-primary {
    width: 100%; background: var(--accent); color: white;
    border: none; padding: 10px; border-radius: 8px;
    cursor: pointer; font-weight: 600; font-size: 0.9rem;
}
.settings-panel .btn-primary:hover { background: var(--accent-hover); }
.settings-panel .btn-secondary {
    width: 100%; background: transparent; color: var(--text-dim);
    border: 1px solid var(--border); padding: 8px;
    border-radius: 8px; cursor: pointer; font-size: 0.8rem;
    margin-top: 8px;
    display: flex; align-items: center; justify-content: center; gap: 6px;
}
.settings-panel .btn-secondary:hover { color: var(--text); border-color: var(--text-dim); }
.settings-panel .btn-row { display: flex; gap: 8px; margin-top: 8px; }
.settings-panel .btn-row button { flex: 1; }

.lightbox {
    position: fixed; inset: 0; z-index: 1000;
    background: rgba(0,0,0,0.92);
    display: none; align-items: center; justify-content: center;
    cursor: zoom-out;
}
.lightbox.visible { display: flex; }
.lightbox img { max-width: 95vw; max-height: 95vh; border-radius: 8px; }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #666; }

@media (max-width: 700px) {
    .sidebar {
        position: absolute; top: 0; bottom: 0; left: 0;
        z-index: 100; box-shadow: 4px 0 20px rgba(0,0,0,0.5);
    }
    .chat-inner, .input-wrap { padding-left: 14px; padding-right: 14px; }
}