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

:root[data-theme="dark"] {
    --bg: #212121;
    --bg-sidebar: #181818;
    --bg-elevated: #2f2f2f;
    --bg-hover: #2a2a2a;
    --bg-input: #2f2f2f;
    --border: #3a3a3a;
    --text: #ececec;
    --text-dim: #a0a0a0;
    --text-faint: #6b6b6b;
    --accent: #6366f1;
    --accent-hover: #4f46e5;
    --bubble-user: #2f2f2f;
    --danger: #ef4444;
    --shadow: 0 10px 40px rgba(0,0,0,0.5);
}
:root[data-theme="light"] {
    --bg: #ffffff;
    --bg-sidebar: #f7f7f8;
    --bg-elevated: #ffffff;
    --bg-hover: #ececf1;
    --bg-input: #f4f4f5;
    --border: #e5e5e5;
    --text: #202123;
    --text-dim: #676767;
    --text-faint: #9a9a9a;
    --accent: #6366f1;
    --accent-hover: #4f46e5;
    --bubble-user: #f0f0f3;
    --danger: #ef4444;
    --shadow: 0 10px 40px rgba(0,0,0,0.12);
}

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

/* ===== App shell ===== */
.app-shell { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
    width: 270px;
    min-width: 270px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: margin-left .2s ease;
}
.sidebar.closed { margin-left: -270px; }

.sidebar-top { display: flex; align-items: center; gap: 8px; padding: 12px; }
.icon-btn {
    background: transparent; border: none; color: var(--text); cursor: pointer;
    width: 36px; height: 36px; border-radius: 8px; display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg-hover); }

.new-chat-btn {
    flex: 1; display: flex; align-items: center; gap: 8px; justify-content: flex-start;
    background: transparent; border: 1px solid var(--border); color: var(--text);
    padding: 9px 12px; border-radius: 10px; cursor: pointer; font-size: 14px; font-weight: 500;
}
.new-chat-btn:hover { background: var(--bg-hover); }

.chat-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.chat-item {
    display: flex; align-items: center; justify-content: space-between; gap: 6px;
    padding: 9px 10px; border-radius: 8px; cursor: pointer; color: var(--text-dim);
    font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 2px;
}
.chat-item:hover { background: var(--bg-hover); color: var(--text); }
.chat-item.active { background: var(--bg-hover); color: var(--text); }
.chat-item .chat-item-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.chat-item .del-chat-btn { opacity: 0; background: none; border: none; color: var(--text-faint); cursor: pointer; font-size: 15px; flex-shrink: 0; }
.chat-item:hover .del-chat-btn { opacity: 1; }
.chat-item .del-chat-btn:hover { color: var(--danger); }

.sidebar-bottom { border-top: 1px solid var(--border); padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.sidebar-link {
    display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
    background: transparent; border: none; color: var(--text); text-decoration: none;
    cursor: pointer; font-size: 14px; width: 100%; text-align: left;
}
.sidebar-link:hover { background: var(--bg-hover); }
.avatar-sm {
    width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff;
    display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; flex-shrink: 0; overflow: hidden;
}
.avatar-sm img { width: 100%; height: 100%; object-fit: cover; }
.user-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

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

.topbar {
    display: flex; align-items: center; gap: 10px; padding: 10px 16px;
    border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.topbar-title { font-weight: 600; font-size: 15px; flex: 1; }

.chat-window { flex: 1; overflow-y: auto; padding: 24px 0; }

.welcome { max-width: 700px; margin: 8vh auto; text-align: center; padding: 0 20px; }
.welcome-logo {
    width: 64px; height: 64px; border-radius: 50%; background: var(--accent); color: #fff;
    display: flex; align-items: center; justify-content: center; font-size: 30px; font-weight: 700; margin: 0 auto 18px;
    overflow: hidden;
}
.welcome-logo img { width: 100%; height: 100%; object-fit: cover; }
.welcome h2 { font-size: 24px; margin-bottom: 8px; }
.welcome p { color: var(--text-dim); }

.msg-row { max-width: 760px; margin: 0 auto; padding: 14px 20px; display: flex; gap: 14px; }
.msg-avatar {
    width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700; color: #fff;
}
.msg-avatar.bot { background: var(--accent); overflow: hidden; }
.msg-avatar.bot img { width: 100%; height: 100%; object-fit: cover; }
.msg-avatar.user { background: #10a37f; }

.msg-content { flex: 1; padding-top: 4px; line-height: 1.65; font-size: 15px; white-space: pre-wrap; word-wrap: break-word; }
.msg-row.user-row { justify-content: flex-end; }
.msg-row.user-row .msg-content { background: var(--bubble-user); padding: 10px 14px; border-radius: 14px; display: inline-block; max-width: 80%; flex: 0 1 auto; }
.msg-content.error-text { color: var(--danger); }

/* ===== Markdown content ===== */
.msg-content p { margin: 0 0 10px; }
.msg-content p:last-child { margin-bottom: 0; }
.msg-content h3, .msg-content h4, .msg-content h5, .msg-content h6 { margin: 16px 0 8px; line-height: 1.3; }
.msg-content h3:first-child, .msg-content h4:first-child { margin-top: 0; }
.msg-content ul, .msg-content ol { margin: 0 0 10px; padding-left: 22px; }
.msg-content li { margin-bottom: 4px; }
.msg-content blockquote {
    margin: 0 0 10px; padding: 4px 14px; border-left: 3px solid var(--accent);
    color: var(--text-dim); background: var(--bg-hover); border-radius: 0 8px 8px 0;
}
.msg-content hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
.msg-content a { color: var(--accent); text-decoration: underline; }
.msg-content strong { font-weight: 700; }
.msg-content code.inline-code {
    background: var(--bg-hover); border: 1px solid var(--border); border-radius: 5px;
    padding: 1px 6px; font-size: 13px; font-family: "SFMono-Regular", Consolas, Menlo, monospace;
}

/* ===== Code blocks ===== */
.code-block {
    margin: 4px 0 12px; border: 1px solid var(--border); border-radius: 10px; overflow: hidden;
    background: #0d1117;
}
:root[data-theme="light"] .code-block { background: #f6f8fa; }
.code-block-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 7px 12px; background: var(--bg-elevated); border-bottom: 1px solid var(--border);
}
.code-lang { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-dim); font-weight: 700; }
.code-block-actions { display: flex; gap: 6px; }
.code-copy-btn, .code-preview-btn {
    background: transparent; border: 1px solid var(--border); color: var(--text-dim);
    font-size: 11.5px; padding: 3px 9px; border-radius: 6px; cursor: pointer; font-weight: 600;
}
.code-copy-btn:hover, .code-preview-btn:hover { background: var(--bg-hover); color: var(--text); }
.code-copy-btn.copied { color: #10a37f; border-color: #10a37f; }
.code-block pre { margin: 0; padding: 14px; overflow-x: auto; }
.code-block code {
    font-family: "SFMono-Regular", Consolas, Menlo, "Liberation Mono", monospace;
    font-size: 13px; line-height: 1.55; color: #e6edf3; white-space: pre;
}
:root[data-theme="light"] .code-block code { color: #24292f; }

/* ===== Code preview modal ===== */
.preview-modal { width: 90vw; max-width: 960px; }
.preview-modal-body { padding: 0; max-height: 78vh; }
.preview-frame { width: 100%; height: 70vh; border: none; background: #fff; display: block; }

.typing-indicator { max-width: 760px; margin: 0 auto; padding: 4px 20px 14px; display: flex; gap: 14px; align-items: center; }
.typing-dots { display: flex; gap: 4px; }
.typing-dots span { width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); animation: blink 1.2s infinite ease-in-out; }
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }

.composer { max-width: 760px; margin: 0 auto 8px; padding: 0 20px; display: flex; flex-direction: column; }
.composer-box {
    display: flex; align-items: flex-end; gap: 8px; background: var(--bg-input);
    border: 1.5px solid var(--border); border-radius: 20px; padding: 8px 8px 8px 18px;
    transition: border-color .15s ease, box-shadow .15s ease;
}
.composer-box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(99,102,241,0.15); }
.composer-box textarea {
    flex: 1; background: transparent; border: none; color: var(--text); resize: none;
    padding: 8px 0; font-size: 15px; outline: none; font-family: inherit; line-height: 1.4;
    max-height: 200px; overflow-y: auto;
}
.composer-box textarea::placeholder { color: var(--text-faint); }

.send-btn {
    width: 40px; height: 40px; border-radius: 50%; border: none; background: var(--accent); color: #fff;
    cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
    transition: background .15s ease, transform .1s ease;
}
.send-btn:hover:not(:disabled) { background: var(--accent-hover); transform: scale(1.06); }
.send-btn:active:not(:disabled) { transform: scale(0.94); }
.send-btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }
.send-btn .send-spinner { display: none; }
.send-btn.loading .send-icon { display: none; }
.send-btn.loading .send-spinner {
    display: block; width: 16px; height: 16px; border-radius: 50%;
    border: 2px solid rgba(255,255,255,.4); border-top-color: #fff;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.composer-hint { text-align: center; font-size: 11px; color: var(--text-faint); margin: 6px 0 12px; }
@media (max-width: 600px) { .composer-hint { display: none; } }

/* ===== Modal ===== */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,.5); display: none; align-items: center; justify-content: center; z-index: 100;
}
.modal-overlay.open { display: flex; }
.modal { background: var(--bg-elevated); border-radius: 14px; width: 440px; max-width: 92vw; box-shadow: var(--shadow); border: 1px solid var(--border); }
.modal-wide { width: 720px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-header h3 { font-size: 16px; }
.modal-body { padding: 16px 20px; max-height: 70vh; overflow-y: auto; }

.settings-tabs { display: flex; gap: 4px; padding: 10px 20px 0; border-bottom: 1px solid var(--border); }
.settings-tab {
    background: transparent; border: none; color: var(--text-dim); cursor: pointer;
    padding: 9px 14px; font-size: 13.5px; font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.settings-tab:hover { color: var(--text); }
.settings-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.memory-add-form { display: flex; gap: 8px; margin-bottom: 14px; }
.memory-add-form input {
    flex: 1; background: var(--bg-input); border: 1px solid var(--border); color: var(--text);
    padding: 9px 12px; border-radius: 8px; font-size: 13.5px; outline: none;
}
.memory-add-form input:focus { border-color: var(--accent); }
.memory-list { display: flex; flex-direction: column; gap: 6px; max-height: 320px; overflow-y: auto; }
.memory-item {
    display: flex; align-items: center; gap: 8px; padding: 9px 10px; border: 1px solid var(--border);
    border-radius: 9px; font-size: 13px;
}
.memory-fact-text { flex: 1; line-height: 1.4; }
.memory-badge {
    font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
    padding: 2px 7px; border-radius: 20px; flex-shrink: 0;
}
.memory-badge.auto { background: #eef2ff; color: #4338ca; }
.memory-badge.manual { background: #f0fdf4; color: #166534; }
.memory-del-btn { background: none; border: none; color: var(--text-faint); cursor: pointer; font-size: 16px; flex-shrink: 0; }
.memory-del-btn:hover { color: var(--danger); }

.settings-row { display: flex; align-items: center; justify-content: space-between; padding: 14px 0; border-bottom: 1px solid var(--border); gap: 12px; }
.settings-row:last-of-type { border-bottom: none; }
.settings-label { font-size: 14px; font-weight: 600; }
.settings-desc { font-size: 12.5px; color: var(--text-dim); margin-top: 2px; }
.danger-row .settings-label { color: var(--danger); }

.switch { position: relative; width: 42px; height: 24px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; inset: 0; background: var(--border); border-radius: 24px; transition: .2s; }
.slider::before { content: ""; position: absolute; height: 18px; width: 18px; left: 3px; bottom: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(18px); }

.danger-btn { background: var(--danger); color: #fff; border: none; padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600; flex-shrink: 0; }
.logout-btn { margin-top: 16px; width: 100%; padding: 11px; border-radius: 8px; border: 1px solid var(--border); background: transparent; color: var(--text); cursor: pointer; font-weight: 600; }
.logout-btn:hover { background: var(--bg-hover); }

/* ===== Auth pages ===== */
.auth-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: var(--bg, #f7f7f8); }
:root { --bg: #f7f7f8; --text: #202123; --border: #e5e5e5; --accent: #6366f1; --accent-hover: #4f46e5; --text-dim: #676767; }
.auth-card { background: #fff; border: 1px solid var(--border); border-radius: 16px; padding: 40px; width: 380px; max-width: 92vw; box-shadow: 0 10px 40px rgba(0,0,0,.08); text-align: center; }
.auth-logo { width: 52px; height: 52px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 24px; font-weight: 700; margin: 0 auto 14px; overflow: hidden; }
.auth-logo img { width: 100%; height: 100%; object-fit: cover; }
.auth-card h1 { font-size: 20px; color: #111; margin-bottom: 4px; }
.auth-sub { color: var(--text-dim); font-size: 13.5px; margin-bottom: 20px; }
.auth-form { text-align: left; }
.auth-form label { display: block; font-size: 12.5px; font-weight: 600; color: #333; margin: 12px 0 5px; }
.auth-form input { width: 100%; padding: 10px 12px; border: 1px solid #d5d5d5; border-radius: 8px; font-size: 14px; outline: none; }
.auth-form input:focus { border-color: var(--accent); }
.auth-form button { margin-top: 18px; width: 100%; padding: 12px; background: var(--accent); color: #fff; border: none; border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 14.5px; }
.auth-form button:hover { background: var(--accent-hover); }
.auth-errors { background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c; padding: 10px 14px; border-radius: 8px; font-size: 13px; margin-bottom: 14px; text-align: left; }
.auth-errors ul { margin: 0 0 0 16px; padding: 0; }
.auth-switch { margin-top: 18px; font-size: 13.5px; color: var(--text-dim); }
.auth-switch a { color: var(--accent); text-decoration: none; font-weight: 600; }
.auth-divider { display: flex; align-items: center; gap: 10px; color: #999; font-size: 12px; margin: 4px 0 16px; }
.auth-divider::before, .auth-divider::after { content: ""; flex: 1; height: 1px; background: #e5e5e5; }

/* ===== Admin ===== */
.admin-shell { min-height: 100vh; background: var(--bg); color: var(--text); }
.admin-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 24px; border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-logo { width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; overflow: hidden; }
.brand-logo img { width: 100%; height: 100%; object-fit: cover; }
.brand-name { font-weight: 700; font-size: 16px; }
.admin-header-actions { display: flex; gap: 10px; }
.ghost-btn { border: 1px solid var(--border); color: var(--text); text-decoration: none; padding: 8px 14px; border-radius: 8px; font-size: 13.5px; }
.ghost-btn:hover { background: var(--bg-hover); }

.admin-stats { display: flex; gap: 16px; padding: 20px 24px; flex-wrap: wrap; }
.stat-card { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 12px; padding: 16px 20px; min-width: 150px; }
.stat-card .stat-value { font-size: 26px; font-weight: 700; }
.stat-card .stat-label { font-size: 12.5px; color: var(--text-dim); margin-top: 4px; }

.admin-body { padding: 0 24px 40px; }
.admin-panel { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 12px; padding: 18px; }
.admin-panel h3 { margin-bottom: 14px; font-size: 15px; }
.table-wrap { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.admin-table th, .admin-table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.admin-table th { color: var(--text-dim); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.role-badge, .status-badge { padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 700; }
.role-badge.admin { background: #eef2ff; color: #4338ca; }
.role-badge.user { background: var(--bg-hover); color: var(--text-dim); }
.status-badge.active { background: #f0fdf4; color: #166534; }
.status-badge.banned { background: #fef2f2; color: #b91c1c; }
.mini-btn { border: 1px solid var(--border); background: transparent; color: var(--text); padding: 4px 9px; border-radius: 6px; font-size: 11.5px; cursor: pointer; margin-right: 4px; }
.mini-btn:hover { background: var(--bg-hover); }
.mini-btn.danger { color: var(--danger); border-color: #fca5a5; }

.branding-row { display: flex; align-items: center; gap: 20px; flex-wrap: wrap; }
.branding-preview {
    width: 72px; height: 72px; border-radius: 16px; background: var(--accent); color: #fff;
    display: flex; align-items: center; justify-content: center; font-size: 28px; font-weight: 700;
    overflow: hidden; flex-shrink: 0; border: 1px solid var(--border);
}
.branding-preview img { width: 100%; height: 100%; object-fit: cover; }
.branding-controls { flex: 1; min-width: 260px; }
.logo-upload-form { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.chat-preview-item { border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin-bottom: 10px; cursor: pointer; }
.chat-preview-item:hover { background: var(--bg-hover); }
.chat-preview-title { font-weight: 600; font-size: 13.5px; }
.chat-preview-meta { font-size: 12px; color: var(--text-dim); margin-top: 3px; }
.login-log-item { font-size: 12.5px; color: var(--text-dim); border-bottom: 1px solid var(--border); padding: 8px 0; }

@media (max-width: 760px) {
    .sidebar { position: fixed; z-index: 50; height: 100%; box-shadow: var(--shadow); }
    .sidebar.closed { margin-left: -270px; }
}
