/* ===== Dark theme (default) ===== */
:root {
    --bg: #0f0f0f;
    --bg-sidebar: #161616;
    --bg-chat: #0f0f0f;
    --bg-input: #1e1e1e;
    --bg-user: #2563eb;
    --bg-assistant: #1a1a1a;
    --bg-hover: #1e1e1e;
    --bg-active: #2563eb20;
    --border: #2a2a2a;
    --text: #e5e5e5;
    --text-dim: #888;
    --text-muted: #555;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --radius: 12px;
    --radius-sm: 8px;
    --sidebar-w: 280px;
    --footer-bg: #111;
    --footer-border: #222;
    --code-bg: #0a0a0a;
    --table-header: #ffffff08;
}

/* ===== Light theme ===== */
[data-theme="light"] {
    --bg: #ffffff;
    --bg-sidebar: #f7f7f8;
    --bg-chat: #ffffff;
    --bg-input: #f0f0f0;
    --bg-user: #2563eb;
    --bg-assistant: #f4f4f5;
    --bg-hover: #ececec;
    --bg-active: #2563eb15;
    --border: #e0e0e0;
    --text: #1a1a1a;
    --text-dim: #666;
    --text-muted: #999;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --footer-bg: #f7f7f8;
    --footer-border: #e0e0e0;
    --code-bg: #f0f0f0;
    --table-header: #00000008;
}

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

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

.app {
    display: flex;
    height: 100vh;
}

/* ===== Sidebar ===== */
.sidebar {
    width: var(--sidebar-w);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: transform 0.2s;
}

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

.sidebar-header h2 {
    font-size: 16px;
    font-weight: 600;
}

.sidebar-actions {
    display: flex;
    gap: 2px;
}

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

.session-item {
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    margin-bottom: 2px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: background 0.15s;
}

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

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

.session-item .title {
    font-size: 13px;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.session-item .time {
    font-size: 11px;
    color: var(--text-muted);
    margin-left: 8px;
    flex-shrink: 0;
}

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

.stats {
    font-size: 11px;
    color: var(--text-muted);
    line-height: 1.6;
}

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

.chat-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-sidebar);
}

.chat-title {
    flex: 1;
    font-size: 14px;
    font-weight: 500;
}

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

/* ===== Messages ===== */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-dim);
    gap: 8px;
}

.empty-icon { opacity: 0.3; margin-bottom: 8px; }
.empty-state h3 { font-size: 20px; color: var(--text); }
.empty-state p { font-size: 14px; }
.empty-state .hint { font-size: 12px; color: var(--text-muted); margin-top: 8px; }

.message {
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.message.user .bubble {
    background: var(--bg-user);
    color: white;
    align-self: flex-end;
    border-radius: var(--radius) var(--radius) 4px var(--radius);
}

.message.assistant .bubble {
    background: var(--bg-assistant);
    border: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) var(--radius) 4px;
}

.bubble {
    padding: 12px 16px;
    font-size: 14px;
    line-height: 1.7;
    word-break: break-word;
}

/* Markdown content inside assistant bubble */
.bubble h1, .bubble h2, .bubble h3, .bubble h4 {
    margin: 12px 0 6px;
    font-weight: 600;
    line-height: 1.3;
}
.bubble h1 { font-size: 18px; }
.bubble h2 { font-size: 16px; }
.bubble h3 { font-size: 15px; }

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }

.bubble ul, .bubble ol {
    margin: 6px 0;
    padding-left: 20px;
}
.bubble li { margin: 2px 0; }

.bubble code {
    background: var(--code-bg);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 13px;
    font-family: 'SF Mono', 'Consolas', 'Menlo', monospace;
}

.bubble pre {
    background: var(--code-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    margin: 8px 0;
    overflow-x: auto;
}
.bubble pre code {
    background: none;
    padding: 0;
    font-size: 12px;
    line-height: 1.5;
}

.bubble blockquote {
    border-left: 3px solid var(--accent);
    margin: 8px 0;
    padding: 4px 12px;
    color: var(--text-dim);
}

.bubble a {
    color: #2563eb;
    text-decoration: none;
}
[data-theme="dark"] .bubble a { color: #60a5fa; }
.bubble a:hover { text-decoration: underline; }

.bubble table {
    border-collapse: collapse;
    margin: 8px 0;
    width: 100%;
    font-size: 13px;
}
.bubble th, .bubble td {
    border: 1px solid var(--border);
    padding: 6px 10px;
    text-align: left;
}
.bubble th { background: var(--table-header); font-weight: 600; }

.bubble strong { font-weight: 600; }
.bubble em { font-style: italic; }

.message.user .bubble {
    white-space: pre-wrap;
}

.msg-meta {
    font-size: 11px;
    color: var(--text-muted);
    padding: 0 4px;
    display: flex;
    gap: 8px;
    align-items: center;
}

.msg-meta .timing {
    color: var(--accent);
    font-weight: 500;
}

.sources {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 4px;
}

.source-chip {
    font-size: 11px;
    padding: 3px 8px;
    border-radius: 999px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    color: var(--text-dim);
    text-decoration: none;
    transition: all 0.15s;
}

.source-chip:hover {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

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

.typing-indicator span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: bounce 1.4s infinite;
}

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

@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-4px); }
}

/* ===== Input ===== */
.input-area {
    padding: 12px 16px 12px;
    border-top: 1px solid var(--border);
    background: var(--bg-sidebar);
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px;
    transition: border-color 0.15s;
}

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

#input-message {
    flex: 1;
    background: none;
    border: none;
    color: var(--text);
    font-size: 14px;
    padding: 8px 12px;
    resize: none;
    outline: none;
    font-family: inherit;
    max-height: 120px;
    line-height: 1.5;
}

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

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

.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }

.input-hint {
    font-size: 11px;
    color: var(--text-muted);
    text-align: center;
    margin-top: 6px;
}

/* ===== Footer ===== */
.app-footer {
    padding: 10px 16px;
    text-align: center;
    font-size: 12px;
    color: var(--text-muted);
    background: var(--footer-bg);
    border-top: 1px solid var(--footer-border);
    flex-shrink: 0;
}

.app-footer a {
    color: var(--accent);
    text-decoration: none;
}

.app-footer a:hover {
    text-decoration: underline;
}

/* ===== Buttons ===== */
.btn-icon {
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    color: var(--text-dim);
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.btn-icon:hover { background: var(--bg-hover); color: var(--text); }
.btn-icon.btn-danger:hover { background: var(--danger); color: white; }

/* Theme toggle icons */
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }
[data-theme="light"] .icon-sun { display: block; }
[data-theme="light"] .icon-moon { display: none; }

/* ===== 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); }

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 100;
        transform: translateX(-100%);
    }
    .sidebar.open { transform: translateX(0); }
    .btn-menu { display: flex !important; }
}
