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

:root, [data-theme="dark"] {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-tertiary: #0f3460;
    --bg-input: #1e2a4a;
    --text-primary: #e0e0e0;
    --text-secondary: #a0a0b8;
    --text-muted: #6a6a80;
    --accent: #e94560;
    --accent-hover: #ff6b81;
    --border: #2a2a4a;
    --msg-user: #1a3a5c;
    --msg-ai: #22223b;
    --scrollbar: #2a2a4a;
    --scrollbar-hover: #3a3a5a;
    --shadow: rgba(0,0,0,0.3);
    --code-bg: #0d1117;
    --code-inline-bg: rgba(255,255,255,0.08);
}

[data-theme="light"] {
    --bg-primary: #f5f5f5;
    --bg-secondary: #ffffff;
    --bg-tertiary: #e8edf2;
    --bg-input: #f0f2f5;
    --text-primary: #1a1a2e;
    --text-secondary: #555568;
    --text-muted: #8888a0;
    --accent: #e94560;
    --accent-hover: #d63050;
    --border: #dddde5;
    --msg-user: #e3ecf5;
    --msg-ai: #f0f0f5;
    --scrollbar: #ccccdd;
    --scrollbar-hover: #aaaabc;
    --shadow: rgba(0,0,0,0.1);
    --code-bg: #f0f2f5;
    --code-inline-bg: rgba(0,0,0,0.06);
}

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

/* Login Screen */
.login-screen {
    display: flex; align-items: center; justify-content: center;
    height: 100vh; background: var(--bg-primary);
}
.login-card {
    background: var(--bg-secondary); border: 1px solid var(--border); border-radius: 16px;
    padding: 40px 36px; width: 380px; max-width: 90vw; text-align: center;
    box-shadow: 0 20px 60px var(--shadow);
}
.login-card h1 { font-size: 32px; font-weight: 700; margin-bottom: 8px; }
.login-card h1 span { color: var(--accent); }
.login-subtitle { color: var(--text-muted); font-size: 14px; margin-bottom: 28px; }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.login-form input {
    background: var(--bg-input); color: var(--text-primary); border: 1px solid var(--border);
    border-radius: 10px; padding: 12px 16px; font-size: 15px; outline: none;
    transition: border-color 0.2s;
}
.login-form input:focus { border-color: var(--accent); }
.login-form input::placeholder { color: var(--text-muted); }
.login-error { color: #ff4d6a; font-size: 13px; min-height: 18px; }
.login-form button {
    background: var(--accent); color: white; border: none; border-radius: 10px;
    padding: 13px; font-size: 16px; font-weight: 600; cursor: pointer;
    transition: background 0.2s;
}
.login-form button:hover { background: var(--accent-hover); }
.login-form button:disabled { opacity: 0.5; cursor: not-allowed; }

/* Logout button */
.btn-logout {
    background: none; border: 1px solid var(--border); color: var(--text-secondary);
    padding: 8px 18px; border-radius: 8px; cursor: pointer; font-size: 13px;
    font-weight: 600; transition: all 0.2s;
}
.btn-logout:hover { border-color: var(--accent); color: var(--accent); }

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

/* Sidebar */
.sidebar {
    width: 280px; min-width: 280px; background: var(--bg-secondary);
    display: flex; flex-direction: column; border-right: 1px solid var(--border);
    transition: transform 0.3s;
}
.sidebar.hidden { transform: translateX(-100%); position: absolute; z-index: 100; height: 100%; }
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 99; }
.sidebar-overlay.active { display: block; }
.sidebar-header { padding: 16px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border); }
.sidebar-header h1 { font-size: 20px; font-weight: 700; }
.sidebar-header h1 span { color: var(--accent); }
.btn-new-chat {
    background: var(--accent); color: white; border: none; padding: 8px 14px;
    border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 600;
    transition: background 0.2s;
}
.btn-new-chat:hover { background: var(--accent-hover); }

.conversations-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
    padding: 12px 14px; border-radius: 8px; cursor: pointer; margin-bottom: 2px;
    display: flex; justify-content: space-between; align-items: center;
    transition: background 0.15s; font-size: 14px; color: var(--text-secondary);
}
.conv-item:hover { background: var(--bg-tertiary); }
.conv-item.active { background: var(--bg-tertiary); color: var(--text-primary); }
.conv-item .conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .conv-delete {
    opacity: 0; background: none; border: none; color: var(--text-muted);
    cursor: pointer; padding: 4px; font-size: 16px; transition: opacity 0.15s;
}
.conv-item:hover .conv-delete { opacity: 1; }
.conv-item .conv-delete:hover { color: var(--accent); }

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

/* Top Bar */
.topbar {
    padding: 12px 20px; display: flex; align-items: center; justify-content: space-between;
    border-bottom: 1px solid var(--border); background: var(--bg-secondary); min-height: 56px;
}
.topbar-left { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; overflow: hidden; }
.btn-toggle-sidebar {
    background: none; border: none; color: var(--text-secondary); cursor: pointer;
    font-size: 20px; padding: 4px; display: none;
}
.model-select {
    background: var(--bg-input); color: var(--text-primary); border: 1px solid var(--border);
    border-radius: 8px; padding: 6px 12px; font-size: 13px; cursor: pointer; outline: none;
    min-width: 0; max-width: 100%; text-overflow: ellipsis;
}
.model-select:focus { border-color: var(--accent); }
.topbar-right { display: flex; gap: 8px; flex-shrink: 0; position: relative; z-index: 50; }
.btn-icon {
    background: none; border: 1px solid var(--border); color: var(--text-secondary);
    width: 36px; height: 36px; border-radius: 8px; cursor: pointer; font-size: 18px;
    display: flex; align-items: center; justify-content: center; transition: all 0.15s;
}
.btn-icon:hover { background: var(--bg-tertiary); color: var(--text-primary); border-color: var(--accent); }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 20px; scroll-behavior: smooth; }
.messages::-webkit-scrollbar { width: 6px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 3px; }
.messages::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-hover); }

.message { max-width: 800px; margin: 0 auto 16px; display: flex; gap: 12px; padding: 16px 20px; border-radius: 12px; line-height: 1.65; }
.message.user { background: var(--msg-user); }
.message.assistant { background: var(--msg-ai); }
.message .avatar {
    width: 32px; height: 32px; border-radius: 8px; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    font-size: 16px; font-weight: 700;
}
.message.user .avatar { background: var(--accent); color: white; }
.message.assistant .avatar { background: #6c63ff; color: white; }
.message .content { flex: 1; min-width: 0; overflow-wrap: break-word; }
.message .content p { margin-bottom: 8px; }
.message .content p:last-child { margin-bottom: 0; }
.message .content pre { background: var(--code-bg); padding: 12px 16px; border-radius: 8px; overflow-x: auto; margin: 8px 0; font-size: 13px; }
.message .content code { font-family: 'SF Mono', 'Fira Code', monospace; font-size: 0.9em; }
.message .content code:not(pre code) { background: var(--code-inline-bg); padding: 2px 6px; border-radius: 4px; }
.message .content strong { color: var(--text-primary); }
.message .content a { color: var(--accent, #4a9eff); text-decoration: underline; text-underline-offset: 2px; }
.message .content a:hover { opacity: 0.8; }
.message .content ul, .message .content ol { padding-left: 20px; margin: 8px 0; }
.message .content li { margin-bottom: 4px; }

/* Welcome */
.welcome { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: var(--text-muted); text-align: center; padding: 40px; }
.welcome h2 { font-size: 28px; color: var(--text-primary); margin-bottom: 8px; }
.welcome h2 span { color: var(--accent); }
.welcome p { font-size: 15px; margin-top: 8px; }

/* Input Area */
.input-area { padding: 16px 20px 20px; border-top: 1px solid var(--border); background: var(--bg-secondary); }
.input-wrapper {
    max-width: 800px; margin: 0 auto; display: flex; gap: 10px;
    background: var(--bg-input); border: 1px solid var(--border); border-radius: 12px;
    padding: 10px 14px; align-items: flex-end; transition: border-color 0.2s;
}
.input-wrapper:focus-within { border-color: var(--accent); }
.input-wrapper textarea {
    flex: 1; background: none; border: none; color: var(--text-primary); resize: none;
    font-size: 15px; line-height: 1.5; outline: none; font-family: inherit;
    max-height: 150px; min-height: 24px;
}
.input-wrapper textarea::placeholder { color: var(--text-muted); }
.btn-send {
    background: var(--accent); color: white; border: none; width: 38px; height: 38px;
    border-radius: 8px; cursor: pointer; font-size: 18px; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center; transition: background 0.2s;
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* Modal */
.modal-overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.6);
    z-index: 1000; align-items: center; justify-content: center;
}
.modal-overlay.active { display: flex; }
.modal {
    background: var(--bg-secondary); border: 1px solid var(--border); border-radius: 16px;
    width: 500px; max-width: 90vw; max-height: 80vh; overflow-y: auto;
    box-shadow: 0 20px 60px var(--shadow);
}
.modal-header { padding: 20px 24px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-header h3 { font-size: 18px; }
.modal-close { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 22px; }
.modal-close:hover { color: var(--text-primary); }
.modal-body { padding: 20px 24px; }
.modal-body .setting-group { margin-bottom: 20px; }
.modal-body label { display: block; font-size: 13px; color: var(--text-secondary); margin-bottom: 6px; font-weight: 600; }
.modal-body select, .modal-body input[type="text"] {
    width: 100%; background: var(--bg-input); color: var(--text-primary); border: 1px solid var(--border);
    border-radius: 8px; padding: 10px 12px; font-size: 14px; outline: none;
}
.modal-body select:focus, .modal-body input:focus { border-color: var(--accent); }
.modal-body .hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* Image Generation Button (in topbar) */
.btn-image-topbar {
    font-size: 20px;
    min-width: 44px; min-height: 44px;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    position: relative;
    z-index: 50;
}
.btn-image-topbar:active { transform: scale(0.92); }

/* Generated Image in Chat */
.generated-image-wrap { margin-top: 8px; }
.generated-image-wrap img {
    max-width: 100%; max-height: 400px; border-radius: 10px; cursor: pointer;
    transition: transform 0.2s; border: 1px solid var(--border);
}
.generated-image-wrap img:hover { transform: scale(1.02); }
.generated-image-wrap .image-desc { font-size: 13px; color: var(--text-secondary); margin-top: 6px; }

/* Image Loading */
.image-loading {
    display: flex; align-items: center; gap: 10px; padding: 16px 0;
    color: var(--text-secondary); font-size: 14px;
}
.image-loading .spinner {
    width: 20px; height: 20px; border: 2px solid var(--border);
    border-top-color: var(--accent); border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Code Executing Loading */
.code-executing {
    display: flex; align-items: center; gap: 8px;
    padding: 12px 16px; background: var(--bg-secondary, #f0f0f0);
    border-radius: 8px; margin-top: 8px;
    color: var(--text-secondary); font-size: 14px;
}
.code-executing .spinner {
    width: 16px; height: 16px; border: 2px solid #ccc;
    border-top-color: #4472C4; border-radius: 50%;
    animation: spin 0.8s linear infinite; flex-shrink: 0;
}

/* Lightbox */
.lightbox-overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.85);
    z-index: 2000; align-items: center; justify-content: center; cursor: zoom-out;
}
.lightbox-overlay.active { display: flex; }
.lightbox-overlay img { max-width: 92vw; max-height: 92vh; border-radius: 8px; object-fit: contain; }
.lightbox-close {
    position: absolute; top: 16px; right: 24px; background: none; border: none;
    color: white; font-size: 36px; cursor: pointer; opacity: 0.7; transition: opacity 0.2s;
}
.lightbox-close:hover { opacity: 1; }

/* Attach / Upload Button */
.btn-attach {
    background: none; border: none; color: var(--text-secondary); cursor: pointer;
    font-size: 22px; flex-shrink: 0; display: flex;
    align-items: center; justify-content: center; transition: color 0.15s;
    min-width: 44px; min-height: 44px;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    position: relative;
    z-index: 50;
}
.btn-attach:hover { color: var(--accent); }
.btn-attach:active { transform: scale(0.92); }

/* Multi-file Preview Bar */
.multi-preview-bar {
    max-width: 800px; margin: 0 auto 8px; display: flex; align-items: center; gap: 8px;
    overflow-x: auto; padding: 4px 0;
    scrollbar-width: thin;
}
.multi-preview-bar::-webkit-scrollbar { height: 4px; }
.multi-preview-bar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.preview-item {
    position: relative; flex-shrink: 0;
}
.preview-item img {
    width: 72px; height: 72px; object-fit: cover; border-radius: 8px;
    border: 1px solid var(--border);
}
.preview-item .preview-file-chip {
    display: flex; align-items: center; gap: 6px;
    background: var(--bg-input); border: 1px solid var(--border);
    border-radius: 8px; padding: 6px 10px; white-space: nowrap; max-width: 160px;
}
.preview-item .preview-file-chip .pf-icon { font-size: 20px; }
.preview-item .preview-file-chip .pf-name {
    font-size: 12px; color: var(--text-primary);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.preview-item .preview-remove {
    position: absolute; top: -6px; right: -6px; width: 20px; height: 20px;
    border-radius: 50%; background: var(--accent); color: white; border: none;
    font-size: 12px; cursor: pointer; display: flex; align-items: center;
    justify-content: center; line-height: 1; z-index: 1;
}
.preview-item .preview-remove:hover { background: var(--accent-hover); }

/* Upload status */
.upload-status {
    max-width: 800px; margin: 0 auto 6px; padding: 6px 12px;
    font-size: 13px; color: var(--text-secondary);
    background: var(--bg-input); border-radius: 8px; text-align: center;
}

/* Legacy: keep for existing message rendering */
.image-preview-bar {
    max-width: 800px; margin: 0 auto 8px; display: flex; align-items: center; gap: 8px;
}
.image-preview-item {
    position: relative; display: inline-block;
}
.image-preview-item img {
    width: 80px; height: 80px; object-fit: cover; border-radius: 8px;
    border: 1px solid var(--border);
}
.image-preview-remove {
    position: absolute; top: -6px; right: -6px; width: 22px; height: 22px;
    border-radius: 50%; background: var(--accent); color: white; border: none;
    font-size: 14px; cursor: pointer; display: flex; align-items: center;
    justify-content: center; line-height: 1;
}
.image-preview-remove:hover { background: var(--accent-hover); }

/* File preview in input bar */
.file-preview-info {
    display: flex; align-items: center; gap: 8px;
    background: var(--bg-input); border: 1px solid var(--border);
    border-radius: 8px; padding: 8px 14px;
}
.file-preview-info .file-icon { font-size: 24px; }
.file-preview-info .file-name {
    font-size: 13px; color: var(--text-primary);
    max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* File attachment in chat messages */
.file-attachment {
    margin: 4px 0;
}
.file-link {
    display: inline-flex; align-items: center; gap: 10px;
    background: var(--bg-input); border: 1px solid var(--border);
    border-radius: 10px; padding: 10px 16px;
    text-decoration: none; color: var(--text-primary);
    transition: background 0.2s;
}
.file-link:hover { background: var(--bg-tertiary); }
.file-icon-large { font-size: 28px; }
.file-name-display {
    font-size: 14px; font-weight: 500;
    max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}


/* Download Link in Chat */
.download-link-wrap { margin: 8px 0; }
.download-link {
    display: inline-flex; align-items: center; gap: 10px;
    background: var(--bg-input); border: 1px solid var(--border);
    border-radius: 10px; padding: 12px 18px;
    text-decoration: none; color: var(--text-primary);
    font-size: 14px; font-weight: 500;
    transition: background 0.2s, border-color 0.2s;
}
.download-link:hover { background: var(--bg-tertiary); border-color: var(--accent); }
.download-hint { font-size: 12px; color: var(--text-muted); margin-left: 4px; }

/* Drag & Drop */
.input-area.drag-over { outline: 2px dashed var(--accent); outline-offset: -4px; border-radius: 12px; }

/* Code Block Wrapper with Copy & Download Buttons */
.code-block-wrapper {
    position: relative; margin: 8px 0;
}
.code-block-wrapper pre {
    margin: 0;
}
.code-block-actions {
    position: absolute; top: 6px; right: 6px; display: flex; gap: 4px; opacity: 0;
    transition: opacity 0.15s; z-index: 1;
}
.code-block-wrapper:hover .code-block-actions { opacity: 1; }
.code-copy-btn, .code-download-btn {
    background: var(--bg-tertiary);
    color: var(--text-secondary); border: 1px solid var(--border); border-radius: 6px;
    padding: 3px 10px; font-size: 12px; cursor: pointer;
    transition: background 0.15s;
}
.code-copy-btn:hover, .code-download-btn:hover { background: var(--accent); color: white; border-color: var(--accent); }
.code-download-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Markdown table styling */
.message .content table {
    border-collapse: collapse; margin: 8px 0; width: 100%; font-size: 14px;
}
.message .content th, .message .content td {
    border: 1px solid var(--border); padding: 8px 12px; text-align: left;
}
.message .content th {
    background: var(--bg-tertiary); font-weight: 600;
}
.message .content tr:nth-child(even) { background: rgba(255,255,255,0.03); }
[data-theme="light"] .message .content tr:nth-child(even) { background: rgba(0,0,0,0.03); }

/* Markdown blockquote */
.message .content blockquote {
    border-left: 3px solid var(--accent); margin: 8px 0; padding: 4px 16px;
    color: var(--text-secondary); background: rgba(255,255,255,0.03);
    border-radius: 0 6px 6px 0;
}
[data-theme="light"] .message .content blockquote { background: rgba(0,0,0,0.03); }

/* Markdown horizontal rule */
.message .content hr {
    border: none; border-top: 1px solid var(--border); margin: 16px 0;
}

/* highlight.js overrides */
.message .content pre code.hljs {
    background: transparent; padding: 0;
}

/* Streaming cursor */
.streaming-cursor::after { content: ''; display: inline-block; width: 8px; height: 18px; background: var(--accent); margin-left: 2px; animation: blink 0.8s infinite; vertical-align: text-bottom; }
@keyframes blink { 0%, 50% { opacity: 1; } 51%, 100% { opacity: 0; } }

/* Image Prompt Modal input focus */
#image-prompt-input:focus { border-color: var(--accent); }
#btn-image-generate:hover { background: var(--accent-hover); }

/* Responsive */
@media (max-width: 768px) {
    .sidebar { position: absolute; z-index: 100; height: 100%; transform: translateX(-100%); }
    .sidebar.visible { transform: translateX(0); }
    .btn-toggle-sidebar { display: block; }
    .btn-image-topbar { font-size: 24px; min-width: 44px; min-height: 44px; }
    .btn-icon { min-width: 44px; min-height: 44px; width: 44px; height: 44px; }
    .btn-attach { font-size: 24px; min-width: 48px; min-height: 48px; }
}

/* Scrollbar for sidebar */
.conversations-list::-webkit-scrollbar { width: 4px; }
.conversations-list::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 2px; }

/* Terminal Panel */
.terminal-panel {
    position: fixed; inset: 0; z-index: 500;
    background: var(--bg-primary);
    display: flex; flex-direction: column;
}
.terminal-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 8px 16px; background: var(--bg-secondary);
    border-bottom: 1px solid var(--border); flex-shrink: 0;
    gap: 12px; flex-wrap: wrap;
}
.terminal-header-left {
    display: flex; align-items: center; gap: 12px;
}
.terminal-back {
    background: none; border: 1px solid var(--border); color: var(--text-secondary);
    padding: 6px 14px; border-radius: 6px; cursor: pointer; font-size: 13px;
    font-weight: 600; transition: all 0.15s; white-space: nowrap;
}
.terminal-back:hover { border-color: var(--accent); color: var(--accent); }
.terminal-status {
    font-size: 13px; color: var(--text-secondary); white-space: nowrap;
}
.terminal-actions {
    display: flex; gap: 6px; flex-wrap: wrap;
}
.terminal-action-btn {
    background: var(--bg-tertiary); border: 1px solid var(--border); color: var(--text-secondary);
    padding: 5px 12px; border-radius: 6px; cursor: pointer; font-size: 12px;
    transition: all 0.15s; white-space: nowrap;
}
.terminal-action-btn:hover { background: var(--accent); color: white; border-color: var(--accent); }
#admin-output {
    max-height: 40%;
    overflow-y: auto;
    padding: 8px 12px;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 13px;
    line-height: 1.5;
    color: #e0e0e0;
    background: #0d0d1a;
    border-bottom: 1px solid var(--border);
    white-space: pre-wrap;
    word-break: break-all;
}
#admin-output .admin-output-item {
    padding: 8px 0;
    border-bottom: 1px solid rgba(255,255,255,0.06);
}
#admin-output .admin-output-item:last-child { border-bottom: none; }
#admin-output .admin-output-label {
    color: #e94560;
    font-weight: bold;
    margin-bottom: 4px;
}
#admin-output .admin-output-loading {
    color: #888;
    font-style: italic;
}
#terminal-container {
    flex: 1; padding: 4px; overflow: hidden;
}
#terminal-container .xterm { height: 100%; }
@media (max-width: 768px) {
    .terminal-header { padding: 6px 10px; }
    .terminal-action-btn { padding: 4px 8px; font-size: 11px; }
    .terminal-back { padding: 5px 10px; font-size: 12px; }
}
