/* ===== CSS Variables ===== */
:root {
    --bg-primary: #0f0f0f;
    --bg-secondary: #1a1a1a;
    --bg-tertiary: #252525;
    --bg-hover: #2a2a2a;
    --bg-active: #333333;
    --text-primary: #e8e8e8;
    --text-secondary: #a0a0a0;
    --text-muted: #666666;
    --border-color: #2a2a2a;
    --accent-color: #667eea;
    --accent-hover: #5a6fd6;
    --accent-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --user-bubble: #2d5a8e;
    --ai-bubble: #1e1e1e;
    --danger: #e74c3c;
    --success: #2ecc71;
    --sidebar-width: 280px;
    --header-height: 56px;
    --input-area-height: auto;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --shadow-sm: 0 2px 8px rgba(0,0,0,0.2);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.3);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.4);
    --transition: all 0.2s ease;
}

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

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', 'PingFang SC', 'Microsoft YaHei', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
    width: 6px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: var(--bg-active);
    border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

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

/* ===== Sidebar ===== */
.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: var(--transition);
    z-index: 100;
}

.sidebar.collapsed {
    margin-left: calc(-1 * var(--sidebar-width));
}

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

.new-chat-btn {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
}

.new-chat-btn:hover {
    background: var(--bg-hover);
    border-color: var(--accent-color);
}

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

/* Chat History */
.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.chat-history-item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    margin-bottom: 2px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition);
    position: relative;
    group: true;
}

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

.chat-history-item.active {
    background: var(--bg-active);
}

.chat-history-item .chat-title {
    flex: 1;
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-history-item.active .chat-title {
    color: var(--text-primary);
}

.chat-history-item .chat-actions {
    display: none;
    gap: 4px;
    margin-left: 8px;
}

.chat-history-item:hover .chat-actions {
    display: flex;
}

.chat-history-item .chat-actions button {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-history-item .chat-actions button:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

.chat-history-item .chat-actions .delete-btn:hover {
    color: var(--danger);
}

/* Sidebar Footer */
.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid var(--border-color);
}

.settings-btn {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: none;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 14px;
    cursor: pointer;
    transition: var(--transition);
}

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

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

/* Header */
.main-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    padding: 0 16px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-primary);
    gap: 12px;
}

.toggle-sidebar-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.model-selector {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition);
}

.model-selector:hover {
    background: var(--bg-hover);
}

.model-name {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
}

/* ===== Chat Container ===== */
.chat-container {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

/* Welcome Screen */
.welcome-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    text-align: center;
}

.welcome-screen.hidden {
    display: none;
}

.welcome-logo {
    margin-bottom: 24px;
}

.welcome-screen h1 {
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 8px;
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.welcome-subtitle {
    font-size: 15px;
    color: var(--text-secondary);
    margin-bottom: 40px;
}

.suggestion-cards {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    max-width: 600px;
    width: 100%;
}

.suggestion-card {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 18px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
    text-align: left;
}

.suggestion-card:hover {
    background: var(--bg-hover);
    border-color: var(--accent-color);
    transform: translateY(-1px);
}

.suggestion-card svg {
    flex-shrink: 0;
    color: var(--accent-color);
}

.suggestion-card span {
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.4;
}

/* Messages */
.messages {
    padding: 20px 0;
    display: none;
}

.messages.active {
    display: block;
}

.message {
    display: flex;
    gap: 16px;
    padding: 16px 24px;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
    animation: fadeIn 0.3s ease;
}

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

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

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

.message.assistant .message-avatar {
    background: var(--accent-gradient);
    color: white;
}

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

.message-role {
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 6px;
    color: var(--text-primary);
}

.message-text {
    font-size: 14px;
    line-height: 1.7;
    color: var(--text-primary);
    word-wrap: break-word;
    overflow-wrap: break-word;
}

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

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

.message-text code {
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    font-family: 'SF Mono', 'Fira Code', monospace;
}

.message-text pre {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 16px;
    overflow-x: auto;
    margin: 12px 0;
    position: relative;
}

.message-text pre code {
    background: none;
    padding: 0;
    font-size: 13px;
    line-height: 1.5;
}

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

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

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

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

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

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

.message-text h1, .message-text h2, .message-text h3,
.message-text h4, .message-text h5, .message-text h6 {
    margin: 16px 0 8px;
    font-weight: 600;
}

.message-text h1 { font-size: 20px; }
.message-text h2 { font-size: 18px; }
.message-text h3 { font-size: 16px; }

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

.message-text a:hover {
    text-decoration: underline;
}

.message-text img {
    max-width: 100%;
    border-radius: var(--radius-sm);
    margin: 8px 0;
}

/* File attachments in messages */
.message-files {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 8px;
}

.message-file-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    font-size: 12px;
    color: var(--text-secondary);
}

.message-file-item img {
    width: 40px;
    height: 40px;
    object-fit: cover;
    border-radius: 4px;
}

/* Message actions */
.message-actions {
    display: flex;
    gap: 4px;
    margin-top: 8px;
    opacity: 0;
    transition: var(--transition);
}

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

.message-action-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    display: flex;
    align-items: center;
    gap: 4px;
    transition: var(--transition);
}

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

/* Typing indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 8px 0;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: typing 1.4s 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% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* ===== Input Area ===== */
.input-area {
    padding: 12px 24px 20px;
    background: var(--bg-primary);
}

.input-container {
    max-width: 900px;
    margin: 0 auto;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 12px 16px;
    transition: var(--transition);
    position: relative;
}

.input-container:focus-within {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.15);
}

.file-preview {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 8px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border-color);
}

.file-preview-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    font-size: 12px;
    color: var(--text-secondary);
    position: relative;
}

.file-preview-item img {
    width: 40px;
    height: 40px;
    object-fit: cover;
    border-radius: 4px;
}

.file-preview-item .remove-file {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 18px;
    height: 18px;
    background: var(--danger);
    border: none;
    border-radius: 50%;
    color: white;
    font-size: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

#messageInput {
    width: 100%;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 14px;
    line-height: 1.5;
    resize: none;
    outline: none;
    max-height: 200px;
    font-family: inherit;
}

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

.input-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 8px;
}

.action-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.send-btn {
    background: var(--accent-gradient);
    border: none;
    color: white;
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.send-btn:hover:not(:disabled) {
    opacity: 0.9;
    transform: scale(1.05);
}

.send-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* ===== Modal ===== */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-xl);
    width: 90%;
    max-width: 520px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
    animation: modalIn 0.2s ease;
}

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

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

.modal-header h2 {
    font-size: 18px;
    font-weight: 600;
}

.close-modal-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.modal-body {
    padding: 24px;
}

.setting-group {
    margin-bottom: 20px;
}

.setting-group:last-child {
    margin-bottom: 0;
}

.setting-group label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.setting-group input[type="text"],
.setting-group input[type="password"],
.setting-group select,
.setting-group textarea {
    width: 100%;
    padding: 10px 14px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    outline: none;
    transition: var(--transition);
}

.setting-group input:focus,
.setting-group select:focus,
.setting-group textarea:focus {
    border-color: var(--accent-color);
}

.setting-group textarea {
    resize: vertical;
    min-height: 80px;
}

.setting-hint {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 6px;
}

.setting-hint a {
    color: var(--accent-color);
    text-decoration: none;
}

.setting-hint a:hover {
    text-decoration: underline;
}

.setting-group label input[type="checkbox"] {
    margin-right: 8px;
    accent-color: var(--accent-color);
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    padding: 16px 24px;
    border-top: 1px solid var(--border-color);
}

.btn-primary {
    padding: 10px 20px;
    background: var(--accent-gradient);
    border: none;
    border-radius: var(--radius-sm);
    color: white;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
}

.btn-primary:hover {
    opacity: 0.9;
}

.btn-secondary {
    padding: 10px 20px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
}

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

/* ===== Toast Notification ===== */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    padding: 12px 24px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 14px;
    z-index: 2000;
    transition: transform 0.3s ease;
    box-shadow: var(--shadow-md);
}

.toast.show {
    transform: translateX(-50%) translateY(0);
}

.toast.error {
    border-color: var(--danger);
}

.toast.success {
    border-color: var(--success);
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        height: 100%;
        z-index: 200;
        box-shadow: var(--shadow-lg);
    }
    
    .sidebar.collapsed {
        transform: translateX(-100%);
        margin-left: 0;
    }
    
    .suggestion-cards {
        grid-template-columns: 1fr;
    }
    
    .message {
        padding: 12px 16px;
    }
    
    .input-area {
        padding: 8px 12px 16px;
    }
}

/* ===== Copy Code Button ===== */
.code-block-wrapper {
    position: relative;
}

.copy-code-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 4px 10px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    color: var(--text-secondary);
    font-size: 12px;
    cursor: pointer;
    transition: var(--transition);
    opacity: 0;
}

.code-block-wrapper:hover .copy-code-btn {
    opacity: 1;
}

.copy-code-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

/* ===== Streaming cursor ===== */
.streaming-cursor::after {
    content: '▊';
    animation: blink 1s infinite;
    color: var(--accent-color);
}

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