/* ==================== CSS Variables ==================== */
:root {
    /* Light Theme */
    --bg-primary: #ffffff;
    --bg-secondary: #f7f7f8;
    --bg-tertiary: #ececec;
    --text-primary: #1a1a1a;
    --text-secondary: #6b6b6b;
    --text-tertiary: #999999;
    --border-color: #e5e5e5;
    --accent-color: #4f46e5;
    --accent-hover: #4338ca;
    --accent-light: #e0e7ff;
    --user-message-bg: #f0f4ff;
    --ai-message-bg: #ffffff;
    --code-bg: #f6f8fa;
    --scrollbar-track: #f1f1f1;
    --scrollbar-thumb: #c1c1c1;
    --shadow-light: 0 1px 3px rgba(0, 0, 0, 0.1);
    --shadow-medium: 0 4px 12px rgba(0, 0, 0, 0.15);
    --transition-fast: 0.15s ease;
    --transition-normal: 0.3s ease;
    
    /* Sidebar */
    --sidebar-width: 280px;
}

/* Dark Theme */
[data-theme="dark"] {
    --bg-primary: #1e1e1e;
    --bg-secondary: #2d2d2d;
    --bg-tertiary: #3d3d3d;
    --text-primary: #e5e5e5;
    --text-secondary: #9a9a9a;
    --text-tertiary: #666666;
    --border-color: #404040;
    --accent-color: #818cf8;
    --accent-hover: #a5b4fc;
    --accent-light: #312e47;
    --user-message-bg: #312e47;
    --ai-message-bg: #2d2d2d;
    --code-bg: #0d0d0d;
    --scrollbar-track: #2d2d2d;
    --scrollbar-thumb: #4a4a4a;
}

/* ==================== Reset & Base ==================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

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;
    transition: background 0.3s, color 0.3s;
}

/* ==================== App Container ==================== */
.app-container {
    display: flex;
    height: 100vh;
}

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

.sidebar.open {
    transform: translateX(0);
}

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

.sidebar-header h2 {
    font-size: 20px;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-primary);
}

.sidebar-header h2 i {
    color: var(--accent-color);
    font-size: 24px;
}

.new-chat-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--accent-color);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.3s;
    box-shadow: var(--shadow-light);
}

.new-chat-btn:hover {
    background: var(--accent-hover);
    transform: rotate(90deg);
}

/* Agent Selector */
.agent-selector {
    padding: 15px 20px;
    border-bottom: 1px solid var(--border-color);
}

.agent-selector label {
    font-size: 13px;
    color: var(--text-secondary);
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
}

.agent-selector select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s;
}

.agent-selector select:hover {
    border-color: var(--accent-color);
}

/* System Prompt */
.system-prompt-section {
    padding: 15px 20px;
    border-bottom: 1px solid var(--border-color);
    display: none;
}

.system-prompt-section label {
    font-size: 13px;
    color: var(--text-secondary);
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
}

.system-prompt-section textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 13px;
    font-family: inherit;
    resize: vertical;
    min-height: 80px;
}

/* Sessions List */
.sessions-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.session-item {
    padding: 12px 15px;
    border-radius: 10px;
    cursor: pointer;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: all 0.3s;
    background: transparent;
}

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

.session-item.active {
    background: var(--accent-light);
}

.session-info {
    flex: 1;
    overflow: hidden;
}

.session-title {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.session-time {
    font-size: 12px;
    color: var(--text-tertiary);
    margin-top: 4px;
}

.session-delete {
    background: none;
    border: none;
    color: var(--text-tertiary);
    cursor: pointer;
    padding: 8px;
    border-radius: 6px;
    transition: all 0.3s;
    font-size: 14px;
}

.session-delete:hover {
    color: #ef4444;
    background: rgba(239, 68, 68, 0.1);
}

/* Sidebar Footer */
.sidebar-footer {
    padding: 15px 20px;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 10px;
}

.clear-all-btn {
    flex: 1;
    padding: 10px;
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.clear-all-btn:hover {
    color: #ef4444;
    border-color: #ef4444;
    background: rgba(239, 68, 68, 0.1);
}

.theme-toggle-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--bg-tertiary);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: var(--text-primary);
    transition: all 0.3s;
}

.theme-toggle-btn:hover {
    background: var(--accent-color);
    color: white;
}

/* ==================== Main Content ==================== */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg-primary);
}

/* Mobile Header */
.mobile-header {
    display: none;
    padding: 15px 20px;
    border-bottom: 1px solid var(--border-color);
    align-items: center;
    justify-content: space-between;
    background: var(--bg-secondary);
}

.hamburger-btn,
.settings-toggle-btn {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: transparent;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: var(--text-primary);
    transition: all 0.3s;
}

.hamburger-btn:hover,
.settings-toggle-btn:hover {
    background: var(--bg-tertiary);
}

.mobile-header h1 {
    font-size: 18px;
    font-weight: 700;
}

/* ==================== Chat Container ==================== */
.chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    scroll-behavior: smooth;
}

.messages-wrapper {
    max-width: 900px;
    margin: 0 auto;
}

.messages {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* Welcome Message */
.welcome-message {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    text-align: center;
}

.welcome-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent-color) 0%, var(--accent-hover) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 36px;
    color: white;
    margin-bottom: 24px;
    box-shadow: var(--shadow-medium);
}

.welcome-message h2 {
    font-size: 28px;
    margin-bottom: 12px;
    color: var(--text-primary);
    font-weight: 700;
}

.welcome-message p {
    font-size: 16px;
    color: var(--text-secondary);
    margin-bottom: 32px;
}

.quick-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
    max-width: 600px;
}

.quick-action-btn {
    padding: 14px 20px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    color: var(--text-primary);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    gap: 8px;
}

.quick-action-btn i {
    color: var(--accent-color);
    font-size: 16px;
}

.quick-action-btn:hover {
    border-color: var(--accent-color);
    background: var(--accent-light);
    transform: translateY(-2px);
    box-shadow: var(--shadow-light);
}

/* ==================== Messages ==================== */
.message {
    display: flex;
    gap: 16px;
    animation: fadeIn 0.3s ease;
}

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

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

.message-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
    box-shadow: var(--shadow-light);
}

.user-avatar {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.assistant-avatar {
    background: linear-gradient(135deg, var(--accent-color) 0%, var(--accent-hover) 100%);
    color: white;
}

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

.message-bubble {
    padding: 16px 20px;
    border-radius: 16px;
    line-height: 1.6;
    word-wrap: break-word;
    position: relative;
}

.user-message .message-bubble {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-bottom-right-radius: 4px;
}

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

.message-actions {
    margin-top: 10px;
    display: flex;
    gap: 8px;
    opacity: 0;
    transition: opacity 0.3s;
}

.message:hover .message-actions {
    opacity: 1;
}

.action-btn {
    background: var(--bg-tertiary);
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 12px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    gap: 4px;
}

.action-btn:hover {
    background: var(--accent-color);
    color: white;
}

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

.input-container {
    max-width: 900px;
    margin: 0 auto;
}

.input-wrapper {
    display: flex;
    gap: 12px;
    align-items: flex-end;
    background: var(--bg-secondary);
    border: 2px solid var(--border-color);
    border-radius: 16px;
    padding: 8px;
    transition: all 0.3s;
}

.input-wrapper:focus-within {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px var(--accent-light);
}

.input-wrapper textarea {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 15px;
    font-family: inherit;
    resize: none;
    outline: none;
    padding: 8px;
    max-height: 200px;
}

.input-wrapper textarea::placeholder {
    color: var(--text-tertiary);
}

.input-actions {
    display: flex;
    gap: 8px;
}

.clear-input-btn,
.stop-btn,
.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    transition: all 0.3s;
}

.clear-input-btn {
    background: transparent;
    color: var(--text-tertiary);
}

.clear-input-btn:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.stop-btn {
    background: #ef4444;
    color: white;
}

.stop-btn:hover {
    background: #dc2626;
}

.send-btn {
    background: linear-gradient(135deg, var(--accent-color) 0%, var(--accent-hover) 100%);
    color: white;
}

.send-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.4);
}

.send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* ==================== Settings Panel ==================== */
.settings-panel {
    position: fixed;
    top: 0;
    right: -400px;
    width: 400px;
    height: 100vh;
    background: var(--bg-secondary);
    box-shadow: -4px 0 12px rgba(0, 0, 0, 0.1);
    z-index: 1000;
    transition: right 0.3s;
}

.settings-panel.open {
    right: 0;
}

.settings-content {
    height: 100%;
    display: flex;
    flex-direction: column;
}

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

.settings-header h3 {
    font-size: 18px;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 10px;
}

.settings-header h3 i {
    color: var(--accent-color);
}

.close-settings {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: transparent;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: var(--text-primary);
    transition: all 0.3s;
}

.close-settings:hover {
    background: var(--bg-tertiary);
}

.settings-body {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
}

.setting-item {
    margin-bottom: 24px;
}

.setting-item label {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    display: block;
    margin-bottom: 12px;
}

.setting-item input[type="range"] {
    width: calc(100% - 50px);
    height: 4px;
    border-radius: 2px;
    background: var(--border-color);
    outline: none;
    -webkit-appearance: none;
}

.setting-item input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent-color);
    cursor: pointer;
}

.setting-item span {
    display: inline-block;
    width: 40px;
    text-align: right;
    font-size: 14px;
    color: var(--text-primary);
    font-weight: 600;
}

.setting-item select {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 14px;
    cursor: pointer;
}

/* ==================== Typing Indicator ==================== */
.typing-indicator {
    display: flex;
    gap: 6px;
    padding: 8px;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent-color);
    animation: bounce 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

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

/* ==================== Toast ==================== */
.toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    padding: 14px 24px;
    background: #333;
    color: white;
    border-radius: 8px;
    font-size: 14px;
    z-index: 2000;
    opacity: 0;
    transition: opacity 0.3s;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.toast.show {
    opacity: 1;
}

.toast.error {
    background: #ef4444;
}

.toast.success {
    background: #10b981;
}

/* ==================== Scrollbar ==================== */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
}

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

::-webkit-scrollbar-thumb:hover {
    background: #999;
}

/* ==================== Markdown Styles ==================== */
.message-bubble h1,
.message-bubble h2,
.message-bubble h3 {
    margin: 16px 0 8px;
}

.message-bubble h1 { font-size: 1.5em; font-weight: 700; }
.message-bubble h2 { font-size: 1.3em; font-weight: 700; }
.message-bubble h3 { font-size: 1.1em; font-weight: 700; }

.message-bubble p {
    margin: 8px 0;
}

.message-bubble ul,
.message-bubble ol {
    margin: 8px 0;
    padding-left: 24px;
}

.message-bubble li {
    margin: 4px 0;
}

.message-bubble code {
    background: var(--code-bg);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.9em;
    font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
}

.message-bubble pre {
    background: var(--code-bg);
    padding: 16px;
    border-radius: 12px;
    overflow-x: auto;
    margin: 12px 0;
    position: relative;
}

.message-bubble pre code {
    background: transparent;
    padding: 0;
}

.copy-button {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.1);
    border: none;
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 12px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.3s;
}

.copy-button:hover {
    background: var(--accent-color);
    color: white;
}

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

.message-bubble table {
    border-collapse: collapse;
    width: 100%;
    margin: 12px 0;
}

.message-bubble th,
.message-bubble td {
    border: 1px solid var(--border-color);
    padding: 10px 14px;
    text-align: left;
}

.message-bubble th {
    background: var(--bg-tertiary);
    font-weight: 600;
}

/* ==================== Mobile Responsive ==================== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        height: 100%;
        z-index: 100;
        transform: translateX(-100%);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .mobile-header {
        display: flex;
    }

    .chat-container {
        padding: 15px;
    }

    .welcome-message {
        padding: 40px 15px;
    }

    .welcome-icon {
        width: 60px;
        height: 60px;
        font-size: 28px;
    }

    .welcome-message h2 {
        font-size: 24px;
    }

    .quick-actions {
        gap: 10px;
    }

    .quick-action-btn {
        padding: 12px 16px;
        font-size: 13px;
    }

    .message {
        gap: 12px;
    }

    .message-avatar {
        width: 36px;
        height: 36px;
        font-size: 16px;
    }

    .message-bubble {
        padding: 12px 16px;
    }

    .input-area {
        padding: 15px;
    }

    .settings-panel {
        width: 100%;
        right: -100%;
    }
}
