/* ============================================
   GangDan - Styles
   ============================================ */

* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
    --primary: #4fc3f7;
    --primary-dark: #29b6f6;
    --success: #66bb6a;
    --danger: #ef5350;
    --warning: #ff9800;
    --bg-dark: #0f0f1a;
    --bg-card: rgba(255,255,255,0.04);
    --bg-card-hover: rgba(255,255,255,0.08);
    --text: #e0e0e0;
    --text-muted: #90a4ae;
    --border: rgba(255,255,255,0.1);
    --shadow: 0 4px 20px rgba(0,0,0,0.3);
}
body { 
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, #0f0f1a 0%, #1a1a2e 50%, #16213e 100%);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.5;
}
.container { max-width: 1400px; margin: 0 auto; padding: 15px 20px; }

/* Header - Compact */
.header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px 20px; background: var(--bg-card);
    border-radius: 12px; margin-bottom: 15px;
    backdrop-filter: blur(10px); border: 1px solid var(--border);
}
.header h1 { font-size: 1.4em; color: var(--primary); font-weight: 600; }
.header p { color: var(--text-muted); font-size: 0.85em; margin-top: 2px; }
.lang-select {
    padding: 6px 12px; border-radius: 6px; border: 1px solid var(--primary);
    background: transparent; color: var(--primary); cursor: pointer;
    font-size: 0.9em; transition: all 0.2s;
}
.lang-select:hover { background: rgba(79,195,247,0.15); }

/* Tabs - Compact */
.tabs { display: flex; gap: 8px; margin-bottom: 15px; flex-wrap: wrap; }
.tab {
    padding: 10px 20px; border-radius: 8px; border: none;
    background: var(--bg-card); color: var(--text);
    cursor: pointer; transition: all 0.2s; font-size: 0.9em;
    border: 1px solid transparent;
}
.tab:hover { background: var(--bg-card-hover); border-color: var(--border); }
.tab.active { background: var(--primary); color: var(--bg-dark); font-weight: 500; }

/* Panels */
.panel { display: none; animation: fadeIn 0.3s ease; }
.panel.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* Chat Container - Taller */
.chat-container {
    background: var(--bg-card); border-radius: 12px;
    padding: 15px; height: calc(100vh - 200px); min-height: 500px;
    display: flex; flex-direction: column;
    border: 1px solid var(--border);
}
.chat-messages {
    flex: 1; overflow-y: auto; padding: 10px;
    display: flex; flex-direction: column; gap: 12px;
}
.message { padding: 10px 14px; border-radius: 12px; max-width: 85%; }
.message.user { 
    background: var(--primary); color: var(--bg-dark); 
    align-self: flex-end; margin-left: auto;
}
.message.assistant { 
    background: var(--bg-card-hover); 
    align-self: flex-start;
}
.message pre { 
    background: rgba(0,0,0,0.4); padding: 10px; 
    border-radius: 6px; overflow-x: auto; margin: 8px 0;
    font-size: 0.9em;
}
.message code { font-family: 'Fira Code', 'Consolas', monospace; font-size: 0.9em; }

/* Code Blocks */
.code-block { position: relative; margin: 8px 0; }
.code-block-header {
    display: flex; justify-content: space-between; align-items: center;
    background: rgba(0,0,0,0.5); padding: 4px 10px;
    border-radius: 6px 6px 0 0; font-size: 0.8em;
}
.code-block-header .lang { color: var(--primary); font-weight: 500; }
.code-block-header .actions { display: flex; gap: 4px; }
.code-block-header button {
    padding: 2px 8px; font-size: 0.75em; border: none; border-radius: 3px; cursor: pointer;
}
.code-block-header .btn-run { background: var(--success); color: white; }
.code-block-header .btn-copy { background: rgba(255,255,255,0.2); color: var(--text); }
.code-block pre { margin: 0; border-radius: 0 0 6px 6px; }

.code-output {
    background: #0a0a0a; border: 1px solid #333; border-radius: 0 0 6px 6px;
    padding: 8px; margin-top: -1px; font-family: monospace; font-size: 0.85em;
    max-height: 200px; overflow: auto; white-space: pre-wrap;
}
.code-output.error { border-color: var(--danger); color: var(--danger); }
.code-output.success { border-color: var(--success); }

/* Markdown Tables */
.message table, .ai-message-content table {
    border-collapse: collapse; margin: 8px 0; width: 100%;
    background: rgba(0,0,0,0.2); border-radius: 6px; overflow: hidden;
    font-size: 0.9em;
}
.message th, .message td, .ai-message-content th, .ai-message-content td {
    border: 1px solid var(--border); padding: 6px 10px; text-align: left;
}
.message th, .ai-message-content th {
    background: rgba(79,195,247,0.15); color: var(--primary); font-weight: 500;
}

/* Math & Blockquotes */
.math-block { display: block; padding: 10px; margin: 8px 0; background: rgba(0,0,0,0.2); border-radius: 6px; text-align: center; }
.message blockquote, .ai-message-content blockquote {
    border-left: 3px solid var(--primary); padding-left: 12px;
    margin: 8px 0; color: var(--text-muted); font-style: italic;
}

/* Chat Input */
.chat-input-area { display: flex; gap: 8px; margin-top: 12px; align-items: flex-end; }
.chat-input {
    flex: 1; padding: 10px; border-radius: 8px; border: 1px solid var(--border);
    background: var(--bg-card); color: var(--text);
    resize: none; min-height: 44px; font-size: 0.95em;
    transition: border-color 0.2s;
}
.chat-input:focus { border-color: var(--primary); outline: none; }
.chat-options { display: flex; gap: 15px; margin-bottom: 8px; font-size: 0.9em; }
.chat-options label { display: flex; align-items: center; gap: 5px; cursor: pointer; color: var(--text-muted); }
.chat-options input:checked + span { color: var(--primary); }

/* Buttons - Compact */
.btn {
    padding: 8px 16px; border-radius: 6px; border: none;
    cursor: pointer; transition: all 0.2s; font-weight: 500; font-size: 0.9em;
}
.btn-primary { background: var(--primary); color: var(--bg-dark); }
.btn-primary:hover { background: var(--primary-dark); transform: translateY(-1px); }
.btn-danger { background: var(--danger); color: white; }
.btn-secondary { background: var(--bg-card-hover); color: var(--text); }
.btn-secondary:hover { background: rgba(255,255,255,0.15); }
.btn-sm { padding: 5px 10px; font-size: 0.8em; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Settings & Docs - Improved Grid */
.settings-grid {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 15px;
}
.settings-card {
    background: var(--bg-card); border-radius: 10px; padding: 16px;
    border: 1px solid var(--border); transition: all 0.2s;
}
.settings-card:hover { border-color: rgba(79,195,247,0.3); }
.settings-card h3 { color: var(--primary); margin-bottom: 12px; font-size: 1em; font-weight: 500; }
.settings-card.highlight { background: rgba(79,195,247,0.08); border-color: rgba(79,195,247,0.3); }

.form-group { margin-bottom: 12px; }
.form-group label { display: block; margin-bottom: 4px; color: var(--text-muted); font-size: 0.85em; }
.form-group select, .form-group input {
    width: 100%; padding: 8px 10px; border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--bg-card); color: var(--text); font-size: 0.9em;
    transition: border-color 0.2s;
}
.form-group select:focus, .form-group input:focus { border-color: var(--primary); outline: none; }

/* Docs List - Compact */
.docs-list { display: flex; flex-direction: column; gap: 6px; max-height: 200px; overflow-y: auto; }
.doc-item {
    display: flex; justify-content: space-between; align-items: center;
    padding: 8px 10px; background: rgba(0,0,0,0.2); border-radius: 6px; font-size: 0.9em;
}
.doc-item .name { font-weight: 500; }
.doc-item .status { color: var(--text-muted); font-size: 0.8em; }

/* Batch Sources - Compact Grid */
.batch-sources {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 4px; max-height: 180px; overflow-y: auto; padding: 5px;
    background: rgba(0,0,0,0.2); border-radius: 6px;
}
.batch-sources label {
    display: flex; align-items: center; gap: 5px; padding: 4px 6px;
    cursor: pointer; font-size: 0.85em; border-radius: 4px;
    transition: background 0.2s;
}
.batch-sources label:hover { background: var(--bg-card-hover); }

/* Status Indicator */
.status-dot { 
    width: 8px; height: 8px; border-radius: 50%; 
    display: inline-block; margin-right: 6px;
}
.status-dot.online { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-dot.offline { background: var(--danger); }

/* Loading */
.loading { display: inline-block; }
.loading::after {
    content: ''; display: inline-block; width: 14px; height: 14px;
    border: 2px solid var(--primary); border-radius: 50%;
    border-top-color: transparent; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Toast */
.toast {
    position: fixed; bottom: 20px; right: 20px; padding: 12px 20px;
    border-radius: 8px; background: #323232; color: white;
    transform: translateY(100px); opacity: 0; transition: all 0.3s;
    font-size: 0.9em; box-shadow: var(--shadow);
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* Terminal Panel - Better Proportions */
.terminal-panel-grid {
    display: grid; grid-template-columns: 1fr 1fr; gap: 15px;
    height: calc(100vh - 200px); min-height: 450px;
}
.terminal-container {
    background: #0d0d0d; border-radius: 10px;
    padding: 12px; display: flex; flex-direction: column;
    border: 1px solid var(--border);
}
.terminal-header {
    display: flex; justify-content: space-between; align-items: center;
    padding-bottom: 8px; border-bottom: 1px solid var(--border); margin-bottom: 8px;
}
.terminal-header h4 { font-size: 0.95em; font-weight: 500; }
.terminal-output {
    flex: 1; overflow-y: auto; font-family: 'Fira Code', monospace;
    font-size: 0.85em; white-space: pre-wrap; padding: 8px;
    background: #080808; border-radius: 6px; margin-bottom: 8px;
}
.terminal-output .cmd { color: var(--primary); display: block; }
.terminal-output .stdout { color: var(--text); }
.terminal-output .stderr { color: var(--danger); }
.terminal-input-area { display: flex; gap: 6px; align-items: center; }
.terminal-input {
    flex: 1; padding: 8px 10px; border-radius: 6px; border: 1px solid #333;
    background: #080808; color: var(--primary); font-family: 'Fira Code', monospace;
    font-size: 0.9em; transition: all 0.2s;
}
.terminal-input:focus { border-color: var(--primary); outline: none; }
.terminal-input.drag-over { border-color: var(--success); box-shadow: 0 0 10px rgba(102,187,106,0.4); }
.terminal-prompt { color: var(--success); font-size: 0.9em; }

/* AI Chat Messages - With Collapsible */
.ai-chat-messages {
    flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px;
    padding: 8px; background: #080808; border-radius: 6px; margin-bottom: 8px;
}
.ai-message {
    padding: 10px 12px; border-radius: 10px; max-width: 98%;
    animation: slideIn 0.2s ease;
}
@keyframes slideIn { from { opacity: 0; transform: translateY(5px); } }
.ai-message.user {
    background: var(--primary); color: var(--bg-dark);
    align-self: flex-end; margin-left: auto; font-size: 0.9em;
}
.ai-message.assistant {
    background: rgba(255,255,255,0.06); align-self: flex-start;
}
.ai-message.system {
    background: rgba(79,195,247,0.08); color: var(--text-muted);
    font-size: 0.85em; border: 1px dashed rgba(79,195,247,0.2);
}
.ai-message-content { line-height: 1.6; font-size: 0.9em; }
.ai-message-content p { margin: 6px 0; }
.ai-message-content ul, .ai-message-content ol { margin: 6px 0; padding-left: 20px; }
.ai-message-content li { margin: 3px 0; }
.ai-message-content h1, .ai-message-content h2, .ai-message-content h3, 
.ai-message-content h4, .ai-message-content h5, .ai-message-content h6 {
    color: var(--primary); margin: 12px 0 6px 0; font-weight: 600;
}
.ai-message-content h1 { font-size: 1.3em; }
.ai-message-content h2 { font-size: 1.2em; }
.ai-message-content h3 { font-size: 1.1em; }
.ai-message-content h4, .ai-message-content h5, .ai-message-content h6 { font-size: 1em; }
.ai-message-content strong { color: var(--primary); font-weight: 600; }
.ai-message-content em { font-style: italic; }
.ai-message-content code:not(pre code) { 
    background: rgba(79,195,247,0.15); padding: 2px 6px; 
    border-radius: 4px; font-family: 'Fira Code', monospace; font-size: 0.9em;
    color: var(--primary);
}
.ai-message-content a { color: var(--primary); text-decoration: underline; }
.ai-message-content hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }

/* Code blocks in AI message - same as chat */
.ai-message-content .code-block { position: relative; margin: 8px 0; }
.ai-message-content .code-block-header {
    display: flex; justify-content: space-between; align-items: center;
    background: rgba(0,0,0,0.5); padding: 4px 10px;
    border-radius: 6px 6px 0 0; font-size: 0.8em;
}
.ai-message-content .code-block-header .lang { color: var(--primary); font-weight: 500; }
.ai-message-content .code-block-header .actions { display: flex; gap: 4px; }
.ai-message-content .code-block-header button {
    padding: 2px 8px; font-size: 0.75em; border: none; border-radius: 3px; cursor: pointer;
}
.ai-message-content .code-block-header .btn-run { background: var(--success); color: white; }
.ai-message-content .code-block-header .btn-copy { background: rgba(255,255,255,0.2); color: var(--text); }
.ai-message-content .code-block pre {
    margin: 0; padding: 10px; border-radius: 0 0 6px 6px;
    background: rgba(0,0,0,0.4); overflow-x: auto;
}
.ai-message-content .code-block pre code {
    font-family: 'Fira Code', monospace; font-size: 0.9em;
    background: none; padding: 0; color: var(--text);
}
/* Standalone pre (not in code-block) */
.ai-message-content > pre, .ai-message-content > div > pre:not(.code-block pre) { 
    background: rgba(0,0,0,0.4); padding: 10px; 
    border-radius: 6px; overflow-x: auto; margin: 8px 0;
}
.ai-message-content .code-output {
    background: #0a0a0a; border: 1px solid #333; border-radius: 0 0 6px 6px;
    padding: 8px; margin-top: -1px; font-family: monospace; font-size: 0.85em;
    max-height: 150px; overflow: auto; white-space: pre-wrap;
}
.ai-message-content .code-output.error { border-color: var(--danger); color: var(--danger); }
.ai-message-content .code-output.success { border-color: var(--success); }

/* Math in AI message */
.ai-message-content .math-block {
    display: block; padding: 10px; margin: 8px 0;
    background: rgba(0,0,0,0.2); border-radius: 6px;
    overflow-x: auto; text-align: center;
}
.ai-message-content .math-inline { padding: 0 3px; }
.ai-message-content .katex { font-size: 1.1em; }

/* Collapsible Content */
.collapsible-content {
    position: relative; overflow: hidden;
    transition: max-height 0.3s ease;
}
.collapsible-content.collapsed { max-height: 120px; }
.collapsible-content.collapsed::after {
    content: ''; position: absolute; bottom: 0; left: 0; right: 0;
    height: 40px; background: linear-gradient(transparent, rgba(8,8,8,0.95));
    pointer-events: none;
}
.collapse-toggle {
    display: block; width: 100%; padding: 6px; margin-top: 4px;
    background: rgba(79,195,247,0.1); border: 1px dashed rgba(79,195,247,0.3);
    border-radius: 4px; color: var(--primary); cursor: pointer;
    font-size: 0.8em; text-align: center; transition: all 0.2s;
}
.collapse-toggle:hover { background: rgba(79,195,247,0.2); }

/* Command Execution Box */
.ai-exec-command {
    background: rgba(102,187,106,0.1); border: 1px solid rgba(102,187,106,0.3);
    border-radius: 8px; padding: 10px; margin: 8px 0;
}
.ai-exec-command .cmd-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 6px; color: var(--success); font-size: 0.8em;
}
.ai-exec-command .cmd-code {
    background: rgba(0,0,0,0.4); padding: 8px; border-radius: 5px;
    font-family: 'Fira Code', monospace; font-size: 0.9em; cursor: grab;
}
.ai-exec-command .cmd-code:active { cursor: grabbing; }
.ai-exec-command .cmd-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.ai-exec-command .cmd-actions button {
    padding: 5px 12px; border: none; border-radius: 4px;
    cursor: pointer; font-size: 0.8em; transition: all 0.2s;
}
.ai-exec-command .btn-run-cmd { background: var(--success); color: var(--bg-dark); }
.ai-exec-command .btn-copy-cmd { background: rgba(255,255,255,0.1); color: var(--text); }
.ai-exec-command .btn-auto-exec { background: var(--warning); color: var(--bg-dark); }

/* Execution Result */
.ai-exec-result {
    background: rgba(0,0,0,0.3); border-radius: 6px; padding: 10px;
    margin: 6px 0; border-left: 3px solid var(--success); font-size: 0.85em;
}
.ai-exec-result.error { border-left-color: var(--danger); }
.ai-exec-result .result-header { font-size: 0.8em; color: var(--text-muted); margin-bottom: 6px; }
.ai-exec-result pre {
    background: rgba(0,0,0,0.3); padding: 8px; border-radius: 4px;
    max-height: 150px; overflow: auto; white-space: pre-wrap; font-size: 0.85em;
}

/* Scrollbar Styling */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: rgba(0,0,0,0.2); border-radius: 3px; }
::-webkit-scrollbar-thumb { background: rgba(79,195,247,0.3); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(79,195,247,0.5); }

/* KB Scope Selector Dropdown */
.kb-selector-wrapper { position: relative; display: inline-block; vertical-align: middle; }
.kb-dropdown-toggle {
    background: var(--bg-card); border: 1px solid var(--border);
    color: var(--text); padding: 4px 10px; border-radius: 6px;
    cursor: pointer; font-size: 0.85em; white-space: nowrap;
}
.kb-dropdown-toggle:hover { border-color: var(--primary); }
.kb-dropdown-menu {
    display: none; position: absolute; top: calc(100% + 4px); left: 0; z-index: 100;
    background: var(--bg-dark); border: 1px solid var(--border);
    border-radius: 8px; padding: 8px; min-width: 280px; max-height: 340px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}
.kb-dropdown-menu.open { display: block; }
.kb-dropdown-header {
    display: flex; gap: 6px; margin-bottom: 6px; padding-bottom: 6px;
    border-bottom: 1px solid var(--border);
}
.kb-dropdown-list {
    overflow-y: auto; max-height: 270px; display: flex;
    flex-direction: column; gap: 2px;
}
.kb-dropdown-item {
    display: flex; align-items: center; gap: 6px; padding: 5px 6px;
    border-radius: 4px; cursor: pointer; font-size: 0.85em; color: var(--text);
}
.kb-dropdown-item:hover { background: var(--bg-card-hover); }
.kb-dropdown-item input[type="checkbox"] { accent-color: var(--primary); }
.kb-type-badge {
    font-size: 0.7em; padding: 1px 6px; border-radius: 3px; margin-left: auto;
    white-space: nowrap;
}
.kb-type-badge.builtin { background: rgba(79,195,247,0.15); color: var(--primary); }
.kb-type-badge.user { background: rgba(102,187,106,0.15); color: var(--success); }

/* File Upload Input */
.file-input {
    width: 100%; padding: 8px; border-radius: 6px;
    border: 1px solid var(--border); background: var(--bg-card);
    color: var(--text); font-size: 0.85em;
}
.file-input::file-selector-button {
    background: var(--primary); color: var(--bg-dark); border: none;
    padding: 4px 10px; border-radius: 4px; cursor: pointer;
    font-size: 0.85em; font-weight: 500; margin-right: 8px;
}
.file-input::file-selector-button:hover { background: var(--primary-dark); }

/* Tab Divider (between core and learning tabs) */
.tab-divider {
    width: 1px; height: 24px;
    background: var(--border); align-self: center;
    margin: 0 4px;
}

/* Learning Tabs - slightly smaller */
.tab.tab-learning {
    padding: 8px 14px; font-size: 0.84em;
}

/* ============================================
   Image Gallery Styles
   ============================================ */

.gallery-container {
    background: var(--bg-card);
    border-radius: 12px;
    padding: 20px;
    min-height: 400px;
}

.gallery-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    flex-wrap: wrap;
    gap: 10px;
}

.gallery-controls {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.gallery-stats {
    padding: 10px;
    background: var(--bg-secondary);
    border-radius: 6px;
    font-size: 0.9em;
}

.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 15px;
    margin-top: 15px;
}

.image-card {
    background: var(--bg-secondary);
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.image-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.image-card img {
    width: 100%;
    height: 150px;
    object-fit: cover;
}

.image-card p {
    margin: 0;
    padding: 10px;
}

.image-card p:first-child {
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.image-card p:last-child {
    font-size: 0.75em;
    color: var(--text-muted);
}

.gallery-empty {
    text-align: center;
    padding: 50px;
    color: var(--text-muted);
}

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.8);
}

.modal-content {
    position: relative;
    background: transparent;
    margin: 2% auto;
    padding: 0;
    width: 90%;
    max-width: 1200px;
    max-height: 90vh;
    overflow: auto;
}

.modal-content img {
    max-width: 100%;
    max-height: 70vh;
    object-fit: contain;
    border-radius: 8px;
}

.modal-info {
    background: var(--bg-secondary);
    padding: 15px;
    border-radius: 8px;
    margin-top: 15px;
    width: 100%;
    max-width: 800px;
}

.modal-info p {
    margin: 5px 0;
    color: var(--text-muted);
}

/* Chat Image References */
.chat-message.images {
    background: rgba(79, 195, 247, 0.1);
    border-left: 3px solid var(--primary);
}

.chat-image-ref {
    display: inline-block;
    margin: 5px;
    padding: 5px 10px;
    background: var(--bg-secondary);
    border-radius: 4px;
    font-size: 0.85em;
    cursor: pointer;
}

.chat-image-ref:hover {
    background: var(--bg-card-hover);
}
