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

:root {
    --bg-primary: #ffffff;
    --bg-secondary: #f7f7f8;
    --bg-sidebar: #202123;
    --bg-sidebar-hover: #2f3034;
    --bg-input: #ffffff;
    --border-color: #e5e5e6;
    --text-primary: #353740;
    --text-secondary: #6e6e80;
    --text-sidebar: #ececf1;
    --accent-color: #10a37f;
    --accent-hover: #0d8f6e;
    --user-msg-bg: #10a37f;
    --assistant-msg-bg: #f7f7f8;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.1);
    --code-bg: rgba(0, 0, 0, 0.05);
    --code-border: rgba(0, 0, 0, 0.1);
}

[data-theme="dark"] {
    --bg-primary: #343541;
    --bg-secondary: #40414f;
    --bg-sidebar: #202123;
    --bg-sidebar-hover: #2f3034;
    --bg-input: #40414f;
    --border-color: #565869;
    --text-primary: #ececf1;
    --text-secondary: #acacbe;
    --text-sidebar: #ececf1;
    --accent-color: #10a37f;
    --accent-hover: #0d8f6e;
    --user-msg-bg: #10a37f;
    --assistant-msg-bg: #40414f;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.3);
    --code-bg: rgba(0, 0, 0, 0.3);
    --code-border: rgba(255, 255, 255, 0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    font-size: 15px;
    line-height: 1.5;
    transition: background-color 0.3s, color 0.3s;
}

.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--bg-sidebar);
    color: var(--text-sidebar);
    display: flex;
    flex-direction: column;
    border-right: 1px solid rgba(255, 255, 255, 0.1);
    transition: width 0.2s ease, min-width 0.2s ease;
    overflow: hidden;
}

.sidebar.sidebar-collapsed {
    width: 0;
    min-width: 0;
    border-right: none;
}

.sidebar-header {
    padding: 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.new-chat-btn {
    width: 100%;
    padding: 12px;
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    color: var(--text-sidebar);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s;
}

.new-chat-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.3);
}

.new-chat-btn svg {
    flex-shrink: 0;
}

/* Conversation history list */
.conversations-section {
    flex: 1;
    overflow-y: auto;
    padding: 8px 8px 0;
}

.conversations-section::-webkit-scrollbar { width: 6px; }
.conversations-section::-webkit-scrollbar-track { background: transparent; }
.conversations-section::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 3px; }

.conversations-empty {
    font-size: 12px;
    color: rgba(255,255,255,0.35);
    text-align: center;
    padding: 24px 12px;
}

.conversations-group-label {
    font-size: 11px;
    font-weight: 600;
    color: rgba(255,255,255,0.35);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    padding: 10px 12px 4px;
    user-select: none;
}

.conversation-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.15s;
    margin: 1px 0;
    position: relative;
}

.conversation-item:hover {
    background: rgba(255,255,255,0.08);
}

.conversation-item.active {
    background: rgba(255,255,255,0.12);
}

.conversation-title {
    flex: 1;
    font-size: 13px;
    color: var(--text-sidebar);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
}

.conversation-delete {
    opacity: 0;
    flex-shrink: 0;
    background: transparent;
    border: none;
    color: rgba(255,255,255,0.4);
    cursor: pointer;
    padding: 2px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    transition: all 0.15s;
}

.conversation-item:hover .conversation-delete {
    opacity: 1;
}

.conversation-delete:hover {
    color: #ff6b6b;
    background: rgba(255,107,107,0.1);
}

/* Sidebar settings pinned to bottom */
.sidebar-settings {
    padding: 12px;
    border-top: 1px solid rgba(255,255,255,0.08);
    flex-shrink: 0;
}

.setting-item {
    padding: 10px 12px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
    margin-bottom: 8px;
}

.setting-label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 500;
}

.setting-description {
    font-size: 12px;
    color: var(--text-secondary);
    opacity: 0.8;
}

.theme-toggle-btn {
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 6px;
    color: var(--text-sidebar);
    cursor: pointer;
    padding: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    width: 32px;
    height: 32px;
}

.theme-toggle-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.3);
}

.theme-toggle-btn svg {
    width: 16px;
    height: 16px;
}

/* Toggle Switch */
.switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.2);
    transition: 0.3s;
    border-radius: 24px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: 0.3s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: var(--accent-color);
}

input:checked + .slider:before {
    transform: translateX(20px);
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    overflow: hidden;
    transition: background-color 0.3s;
}

.chat-header {
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-primary);
    display: flex;
    align-items: center;
    gap: 12px;
    transition: background-color 0.3s, border-color 0.3s;
}

.sidebar-toggle {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    flex-shrink: 0;
    transition: background 0.15s, color 0.15s;
}

.sidebar-toggle:hover {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.header-title {
    flex: 1;
    min-width: 0;
}

.header-title h1 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: color 0.3s;
}

/* Messages Container */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    background: var(--bg-primary);
    scroll-behavior: smooth;
    transition: background-color 0.3s;
}

.messages-container::-webkit-scrollbar {
    width: 8px;
}

.messages-container::-webkit-scrollbar-track {
    background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Welcome Message */
.welcome-message {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 40px;
    max-width: 600px;
    margin: 0 auto;
}

.welcome-icon {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(--bg-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 24px;
    color: var(--text-secondary);
    transition: background-color 0.3s, color 0.3s;
}

.welcome-message h2 {
    font-size: 32px;
    font-weight: 600;
    margin-bottom: 12px;
    color: var(--text-primary);
    transition: color 0.3s;
}

.welcome-message p {
    font-size: 16px;
    color: var(--text-secondary);
    transition: color 0.3s;
}

/* Messages */
.message {
    display: flex;
    gap: 24px;
    margin-bottom: 32px;
    animation: fadeIn 0.3s ease-out;
    max-width: 768px;
    margin-left: auto;
    margin-right: auto;
}

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

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

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 4px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 14px;
}

.message.user .message-avatar {
    background: var(--user-msg-bg);
    color: white;
}

.message.assistant .message-avatar {
    background: var(--bg-secondary);
    color: var(--text-primary);
    transition: background-color 0.3s, color 0.3s;
}

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

.message-text {
    padding: 12px 16px;
    border-radius: 12px;
    line-height: 1.6;
    word-wrap: break-word;
    white-space: pre-wrap;
    transition: background-color 0.3s, color 0.3s;
}

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

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

/* Markdown Styles */
.message-text :global(h1),
.message-text :global(h2),
.message-text :global(h3),
.message-text :global(h4),
.message-text :global(h5),
.message-text :global(h6) {
    margin-top: 16px;
    margin-bottom: 8px;
    font-weight: 600;
    color: var(--text-primary);
    transition: color 0.3s;
}

.message-text :global(h1) { font-size: 1.5em; }
.message-text :global(h2) { font-size: 1.3em; }
.message-text :global(h3) { font-size: 1.1em; }

.message-text :global(p) {
    margin-bottom: 12px;
}

.message-text :global(p:last-child) {
    margin-bottom: 0;
}

.message-text :global(ul),
.message-text :global(ol) {
    margin-left: 24px;
    margin-bottom: 12px;
    padding-left: 0;
}

.message-text :global(li) {
    margin-bottom: 4px;
}

.message-text :global(a) {
    color: var(--accent-color);
    text-decoration: underline;
}

.message-text :global(blockquote) {
    border-left: 4px solid var(--accent-color);
    padding-left: 16px;
    margin: 16px 0;
    color: var(--text-secondary);
    font-style: italic;
}

.message-text :global(table) {
    border-collapse: collapse;
    width: 100%;
    margin: 16px 0;
}

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

.message-text :global(th) {
    background: var(--bg-secondary);
    font-weight: 600;
}

/* Code Blocks */
.message-text :global(pre) {
    background: var(--code-bg);
    border: 1px solid var(--code-border);
    border-radius: 8px;
    padding: 16px;
    margin: 16px 0;
    overflow-x: auto;
    font-family: 'Courier New', 'Monaco', 'Menlo', monospace;
    font-size: 14px;
    line-height: 1.5;
    transition: background-color 0.3s, border-color 0.3s;
}

.message-text :global(pre code) {
    background: transparent;
    border: none;
    padding: 0;
    font-size: inherit;
    color: inherit;
}

.message-text :global(code) {
    background: var(--code-bg);
    border: 1px solid var(--code-border);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', 'Monaco', 'Menlo', monospace;
    font-size: 0.9em;
    transition: background-color 0.3s, border-color 0.3s;
}

.message.user .message-text :global(code) {
    background: rgba(255, 255, 255, 0.2);
    border-color: rgba(255, 255, 255, 0.3);
    color: white;
}

.message.user .message-text :global(pre) {
    background: rgba(255, 255, 255, 0.15);
    border-color: rgba(255, 255, 255, 0.3);
}

/* Highlight.js overrides for dark mode */
[data-theme="dark"] .message-text :global(pre code.hljs) {
    background: var(--code-bg) !important;
}

/* Thinking Indicator */
.thinking-container {
    padding: 16px 24px;
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
    transition: background-color 0.3s, border-color 0.3s;
}

.thinking-bubble {
    max-width: 768px;
    margin: 0 auto;
    padding: 12px 16px;
    background: var(--assistant-msg-bg);
    border-radius: 12px;
    border-top-left-radius: 4px;
    display: inline-block;
    transition: background-color 0.3s;
}

.thinking-dots {
    display: flex;
    gap: 6px;
    align-items: center;
}

.thinking-dots span {
    width: 8px;
    height: 8px;
    background: var(--text-secondary);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
    transition: background-color 0.3s;
}

.thinking-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.thinking-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce {
    0%, 80%, 100% {
        transform: scale(0);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Input Container */
.input-container {
    padding: 16px 24px 24px;
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
    transition: background-color 0.3s, border-color 0.3s;
}

.input-wrapper {
    max-width: 768px;
    margin: 0 auto;
}

.input-inner {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: 24px;
    padding: 12px 16px;
    box-shadow: var(--shadow-sm);
    transition: all 0.2s;
}

.input-inner:focus-within {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(16, 163, 127, 0.1);
}

#messageInput {
    flex: 1;
    border: none;
    outline: none;
    resize: none;
    font-family: inherit;
    font-size: 15px;
    line-height: 1.5;
    background: transparent;
    color: var(--text-primary);
    max-height: 200px;
    overflow-y: auto;
    min-height: 24px;
    transition: color 0.3s;
}

#messageInput::placeholder {
    color: var(--text-secondary);
    transition: color 0.3s;
}

#messageInput::-webkit-scrollbar {
    width: 6px;
}

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

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

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

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

.send-button:active:not(:disabled) {
    transform: scale(0.95);
}

.send-button:disabled {
    background: var(--text-secondary);
    cursor: not-allowed;
    opacity: 0.5;
}

.input-footer {
    margin-top: 12px;
    text-align: center;
}

.footer-text {
    font-size: 12px;
    color: var(--text-secondary);
    transition: color 0.3s;
}

/* Typing Indicator */
.typing-indicator {
    display: inline-block;
}

.typing-indicator::after {
    content: '▊';
    animation: blink 1s infinite;
    margin-left: 2px;
    color: var(--text-primary);
    transition: color 0.3s;
}

@keyframes blink {
    0%, 50% {
        opacity: 1;
    }
    51%, 100% {
        opacity: 0;
    }
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        top: 0; left: 0; bottom: 0;
        z-index: 100;
        transform: translateX(0);
        transition: transform 0.2s ease;
    }

    .sidebar.sidebar-collapsed {
        transform: translateX(-100%);
        width: 260px;
        min-width: 260px;
    }

    .messages-container { padding: 16px; }
    .message { gap: 12px; margin-bottom: 24px; }
    .input-container { padding: 12px 16px 16px; }
    .welcome-message { padding: 24px; }
    .welcome-message h2 { font-size: 24px; }
}

/* Hide welcome message when messages exist */
.messages-container.has-messages .welcome-message {
    display: none;
}

/* ─── Citations Panel ───────────────────────────────────────────────────── */
.citations-panel {
    margin-top: 10px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    font-size: 13px;
    transition: border-color 0.3s;
}

.citations-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 7px 12px;
    background: var(--bg-secondary);
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 1px solid var(--border-color);
    transition: background-color 0.3s, color 0.3s, border-color 0.3s;
}

.citations-header svg {
    flex-shrink: 0;
    opacity: 0.7;
}

.citations-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.citation-item {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 6px 12px;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-primary);
    transition: background 0.15s, border-color 0.3s;
}

.citation-item:last-child {
    border-bottom: none;
}

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

.citation-item svg {
    flex-shrink: 0;
    color: var(--accent-color);
    opacity: 0.8;
}

.citation-filename {
    flex: 1;
    font-family: 'Courier New', 'Monaco', 'Menlo', monospace;
    font-size: 12px;
    color: var(--text-primary);
    word-break: break-all;
}

.citation-page {
    font-size: 11px;
    color: var(--text-secondary);
    white-space: nowrap;
    flex-shrink: 0;
}
