/* === Reset & Variables === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg-primary: #f5f5f5;
    --bg-secondary: #ffffff;
    --bg-sidebar: #ffffff;
    --bg-input: #ffffff;
    --bg-user-msg: #007aff;
    --bg-assistant-msg: #e9ecef;
    --bg-tool: #fff3cd;
    --text-primary: #1a1a1a;
    --text-secondary: #6c757d;
    --text-user-msg: #ffffff;
    --text-assistant-msg: #1a1a1a;
    --border: #dee2e6;
    --accent: #007aff;
    --accent-hover: #0056cc;
    --shadow: rgba(0,0,0,0.08);
    --radius: 12px;
    --code-bg: #f1f3f5;
}

.theme-dark {
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-sidebar: #0d1117;
    --bg-input: #21262d;
    --bg-user-msg: #238636;
    --bg-assistant-msg: #21262d;
    --bg-tool: #1c1e23;
    --text-primary: #e6edf3;
    --text-secondary: #8b949e;
    --text-user-msg: #ffffff;
    --text-assistant-msg: #e6edf3;
    --border: #30363d;
    --accent: #238636;
    --accent-hover: #2ea043;
    --shadow: rgba(0,0,0,0.3);
    --code-bg: #161b22;
}

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

/* === Layout === */
.app {
    display: flex;
    height: 100vh;
}

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

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border);
}

.logo {
    font-size: 18px;
    margin-bottom: 12px;
}

.btn-new-chat {
    width: 100%;
    padding: 10px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: background 0.2s;
}
.btn-new-chat:hover { background: var(--accent-hover); }

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

.control-group {
    margin-bottom: 8px;
}
.control-group:last-child { margin-bottom: 0; }

.control-group label {
    display: block;
    font-size: 11px;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: 4px;
    font-weight: 600;
}

.control-group select {
    width: 100%;
    padding: 8px;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    cursor: pointer;
}

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

.conv-item {
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 2px;
    transition: background 0.15s;
}
.conv-item:hover { background: var(--bg-input); }
.conv-item.active { background: var(--accent); color: white; }

.conv-item .conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; }

.conv-item .conv-delete {
    opacity: 0;
    background: none;
    border: none;
    color: inherit;
    cursor: pointer;
    font-size: 14px;
    padding: 2px 6px;
    border-radius: 4px;
}
.conv-item:hover .conv-delete { opacity: 0.6; }
.conv-item .conv-delete:hover { opacity: 1; background: rgba(255,0,0,0.2); }

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

.btn-theme {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 12px;
    cursor: pointer;
    font-size: 16px;
}

.btn-sidebar-toggle {
    display: none;
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 100;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 18px;
    cursor: pointer;
    color: var(--text-primary);
}

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

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

.welcome {
    text-align: center;
    margin-top: 20vh;
    color: var(--text-secondary);
}
.welcome h2 { font-size: 24px; margin-bottom: 8px; color: var(--text-primary); }
.welcome p { margin-bottom: 4px; }

/* === Messages === */
.message {
    max-width: 780px;
    margin: 0 auto 16px;
    display: flex;
    gap: 12px;
}

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

.msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
    background: var(--bg-input);
}

.msg-bubble {
    padding: 12px 16px;
    border-radius: var(--radius);
    line-height: 1.6;
    font-size: 14px;
    max-width: 85%;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message.user .msg-bubble {
    background: var(--bg-user-msg);
    color: var(--text-user-msg);
    border-bottom-right-radius: 4px;
}

.message.assistant .msg-bubble {
    background: var(--bg-assistant-msg);
    color: var(--text-assistant-msg);
    border-bottom-left-radius: 4px;
}

/* Markdown in messages */
.msg-bubble h1, .msg-bubble h2, .msg-bubble h3 {
    margin: 12px 0 6px;
    font-size: 1.1em;
}
.msg-bubble h1:first-child, .msg-bubble h2:first-child, .msg-bubble h3:first-child {
    margin-top: 0;
}
.msg-bubble p { margin-bottom: 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble ul, .msg-bubble ol { padding-left: 20px; margin-bottom: 8px; }
.msg-bubble li { margin-bottom: 4px; }
.msg-bubble a { color: var(--accent); }

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

.msg-bubble pre {
    background: var(--code-bg);
    padding: 12px 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
    position: relative;
}
.msg-bubble pre code {
    background: none;
    padding: 0;
    font-size: 13px;
    line-height: 1.5;
}

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

/* Tool calls */
.tool-card {
    background: var(--bg-tool);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 14px;
    margin: 8px 0;
    font-size: 13px;
    max-width: 780px;
    margin-left: auto;
    margin-right: auto;
}

.tool-card-header {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-weight: 500;
}

.tool-card-header .tool-icon { font-size: 14px; }
.tool-card-header .tool-name { flex: 1; }
.tool-card-header .tool-duration { color: var(--text-secondary); font-size: 11px; }

.tool-card-body {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--border);
    white-space: pre-wrap;
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 12px;
    max-height: 200px;
    overflow-y: auto;
    display: none;
}
.tool-card.expanded .tool-card-body { display: block; }

/* Typing indicator */
.typing-indicator {
    display: inline-flex;
    gap: 4px;
    padding: 8px 12px;
}
.typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-secondary);
    animation: typing 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); }
    30% { opacity: 1; transform: scale(1); }
}

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

.file-chips {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin-bottom: 0;
}
.file-chips:not(:empty) { margin-bottom: 8px; }

.file-chip {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 4px 10px;
    font-size: 12px;
    display: flex;
    align-items: center;
    gap: 6px;
}
.file-chip .remove-file {
    cursor: pointer;
    opacity: 0.6;
    font-size: 14px;
}
.file-chip .remove-file:hover { opacity: 1; }

.input-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.btn-upload {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 16px;
    cursor: pointer;
    color: var(--text-primary);
    flex-shrink: 0;
}
.btn-upload:hover { background: var(--border); }

#messageInput {
    flex: 1;
    padding: 10px 14px;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 12px;
    font-size: 14px;
    font-family: inherit;
    resize: none;
    max-height: 150px;
    line-height: 1.5;
}
#messageInput:focus { outline: none; border-color: var(--accent); }

.btn-send {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 18px;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.2s;
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* === Responsive === */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 50;
        transform: translateX(-100%);
        box-shadow: 4px 0 20px var(--shadow);
    }
    .sidebar.open { transform: translateX(0); }
    .btn-sidebar-toggle { display: block; }
    .messages { padding: 16px; padding-top: 50px; }
    .input-area { padding: 12px 16px; }
}
