/* ==========================================================================
   Design System & Base Reset
   ========================================================================== */
:root {
    --font-primary: 'Plus Jakarta Sans', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'JetBrains Mono', monospace;

    /* Dark Mode Theme Palette (Harmonized HSL Colors) */
    --bg-app: radial-gradient(circle at top, #0f1626 0%, #080c14 100%);
    --bg-card: rgba(15, 23, 42, 0.65);
    --bg-card-hover: rgba(30, 41, 59, 0.8);
    --bg-input: rgba(10, 15, 30, 0.8);
    
    --primary: #4f46e5;         /* Indigo 600 */
    --primary-light: #6366f1;   /* Indigo 500 */
    --primary-glow: rgba(99, 102, 241, 0.25);
    --accent: #8b5cf6;          /* Violet 500 */
    --accent-glow: rgba(139, 92, 246, 0.2);
    
    --text-primary: #f8fafc;    /* Slate 50 */
    --text-secondary: #94a3b8;  /* Slate 400 */
    --text-muted: #64748b;      /* Slate 500 */
    
    --border-color: rgba(255, 255, 255, 0.07);
    --border-glow: rgba(99, 102, 241, 0.15);
    --border-success: rgba(16, 185, 129, 0.3);
    
    --success: #10b981;         /* Emerald 500 */
    --success-bg: rgba(16, 185, 129, 0.1);
    --danger: #ef4444;          /* Red 500 */
    --danger-bg: rgba(239, 68, 68, 0.1);
    --warning: #f59e0b;         /* Amber 500 */
    --warning-bg: rgba(245, 158, 11, 0.1);
    --info: #0ea5e9;            /* Sky 500 */
    --info-bg: rgba(14, 165, 233, 0.1);
    
    --shadow-sm: 0 2px 8px -2px rgba(0, 0, 0, 0.5);
    --shadow-md: 0 4px 20px -4px rgba(0, 0, 0, 0.6);
    --shadow-lg: 0 10px 30px -10px rgba(0, 0, 0, 0.8);
    
    --radius-sm: 8px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

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

body {
    font-family: var(--font-primary);
    background: var(--bg-app);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* Custom Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--primary-light);
}

/* ==========================================================================
   Layout Structures
   ========================================================================== */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    padding: 20px;
    gap: 20px;
    max-width: 1600px;
    margin: 0 auto;
}

/* Header Area */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--bg-card);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 15px 24px;
    box-shadow: var(--shadow-sm);
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 15px;
}

.brand-logo {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    border-radius: var(--radius-sm);
    color: white;
    font-size: 20px;
    box-shadow: 0 0 15px var(--primary-glow);
}

.brand-title h1 {
    font-size: 18px;
    font-weight: 700;
    letter-spacing: -0.5px;
    background: linear-gradient(120deg, #ffffff, #c7d2fe);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.brand-title .sub-title {
    font-size: 11px;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1.5px;
    display: block;
}

/* Main Dashboard Layout Grid */
.app-main {
    display: grid;
    grid-template-columns: 380px 1fr;
    gap: 20px;
    flex: 1;
    min-height: 0; /* Ensures proper scroll child behaviors */
}

/* Sidebar and Workspace Panels */
.sidebar-panel {
    display: flex;
    flex-direction: column;
    gap: 20px;
    min-height: 0;
}

.workspace-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

/* Card Structures */
.panel-card {
    background: var(--bg-card);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-md);
    transition: var(--transition);
    overflow: hidden;
}

.panel-card:hover {
    border-color: var(--border-glow);
    box-shadow: 0 10px 25px -8px rgba(99, 102, 241, 0.1);
}

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

.card-header h2 {
    font-size: 14px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-primary);
    letter-spacing: -0.2px;
}

.card-header h2 i {
    color: var(--primary-light);
}

.card-body {
    padding: 20px;
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

.card-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
    background: rgba(10, 15, 30, 0.4);
}

/* ==========================================================================
   UI Components: Badges & Statuses
   ========================================================================== */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 14px;
    border-radius: 30px;
    font-size: 12px;
    font-weight: 500;
    border: 1px solid var(--border-color);
}

.status-badge.status-ok {
    background: var(--success-bg);
    border-color: var(--border-success);
    color: var(--success);
}

.status-badge.status-loading {
    background: rgba(255, 255, 255, 0.03);
    color: var(--text-secondary);
}

.status-badge.status-error {
    background: var(--danger-bg);
    border-color: rgba(239, 68, 68, 0.3);
    color: var(--danger);
}

.pulse-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: currentColor;
    display: inline-block;
}

.status-ok .pulse-dot {
    box-shadow: 0 0 10px var(--success);
}

.status-loading .pulse-dot {
    animation: pulse 1.5s infinite ease-in-out;
}

/* ==========================================================================
   Upload Area (Part A)
   ========================================================================== */
.upload-card {
    flex-shrink: 0;
}

.drag-zone {
    border: 2px dashed rgba(255, 255, 255, 0.15);
    border-radius: var(--radius-sm);
    padding: 24px;
    text-align: center;
    cursor: pointer;
    background: rgba(255, 255, 255, 0.01);
    transition: var(--transition);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.drag-zone:hover, .drag-zone.dragover {
    border-color: var(--primary-light);
    background: rgba(99, 102, 241, 0.04);
    box-shadow: inset 0 0 15px rgba(99, 102, 241, 0.05);
}

.drag-icon {
    font-size: 36px;
    color: var(--text-muted);
    transition: var(--transition);
}

.drag-zone:hover .drag-icon {
    color: var(--primary-light);
    transform: translateY(-3px);
}

.drag-text {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 500;
}

.drag-text span {
    color: var(--primary-light);
    text-decoration: underline;
    font-weight: 600;
}

.drag-subtext {
    font-size: 11px;
    color: var(--text-muted);
}

/* Progress bar container */
.upload-progress-container {
    margin-top: 15px;
    padding: 12px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
}

.progress-info {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    margin-bottom: 6px;
}

#upload-filename {
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 200px;
}

#upload-percentage {
    color: var(--primary-light);
    font-weight: 700;
}

.progress-bar-bg {
    width: 100%;
    height: 6px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 4px;
    overflow: hidden;
}

.progress-bar-fg {
    height: 100%;
    background: linear-gradient(90deg, var(--primary), var(--accent));
    border-radius: 4px;
    transition: width 0.2s ease;
    box-shadow: 0 0 8px var(--primary-glow);
}

.upload-status-text {
    font-size: 10px;
    color: var(--text-secondary);
    margin-top: 6px;
    font-style: italic;
}

/* ==========================================================================
   Knowledge Base / Document List (Part A)
   ========================================================================== */
.documents-card {
    flex: 1;
}

.doc-select-container {
    margin-bottom: 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.doc-select-container label {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.custom-select {
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    padding: 10px 14px;
    font-family: var(--font-primary);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
}

.custom-select:focus {
    outline: none;
    border-color: var(--primary-light);
    box-shadow: 0 0 8px var(--primary-glow);
}

.doc-list-title {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 10px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 5px;
}

.documents-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 250px;
    overflow-y: auto;
}

.empty-docs-placeholder {
    padding: 30px 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.empty-docs-placeholder i {
    font-size: 24px;
    opacity: 0.5;
}

/* Ingested File Card */
.doc-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 12px;
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    transition: var(--transition);
}

.doc-item:hover {
    background: rgba(255, 255, 255, 0.04);
    border-color: var(--border-glow);
}

.doc-item.active-item {
    border-color: rgba(99, 102, 241, 0.4);
    background: rgba(99, 102, 241, 0.04);
}

.doc-info {
    display: flex;
    align-items: center;
    gap: 10px;
    overflow: hidden;
    flex: 1;
}

.doc-info-icon {
    font-size: 16px;
    color: var(--primary-light);
}

.doc-details {
    overflow: hidden;
}

.doc-name {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 200px;
}

.doc-meta {
    font-size: 10px;
    color: var(--text-muted);
}

/* ==========================================================================
   Chat workspace (Part A)
   ========================================================================== */
.chat-card {
    height: 100%;
}

.chat-workspace {
    display: flex;
    gap: 20px;
    position: relative;
    padding: 0;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* Message Bubbles */
.message-bubble {
    display: flex;
    gap: 14px;
    max-width: 85%;
    animation: fadeIn 0.3s ease;
}

.message-bubble.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-bubble.assistant {
    align-self: flex-start;
}

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

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

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

.message-content {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.message-bubble.user .message-content {
    align-items: flex-end;
}

.message-meta {
    font-size: 10px;
    font-weight: 600;
    color: var(--text-muted);
    display: flex;
    gap: 8px;
}

.message-text-wrapper {
    padding: 12px 16px;
    border-radius: var(--radius-md);
    font-size: 13px;
    line-height: 1.6;
    color: var(--text-primary);
    box-shadow: var(--shadow-sm);
    word-break: break-word;
}

.user .message-text-wrapper {
    background: var(--primary-light);
    border-bottom-right-radius: 2px;
}

.assistant .message-text-wrapper {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-color);
    border-bottom-left-radius: 2px;
}

/* System Message */
.system-message {
    align-self: center;
    background: var(--info-bg);
    border: 1px solid rgba(14, 165, 233, 0.2);
    padding: 12px 18px;
    border-radius: var(--radius-sm);
    display: flex;
    gap: 12px;
    max-width: 90%;
    font-size: 12px;
    color: #e0f2fe;
}

.system-message .message-icon {
    font-size: 16px;
    color: var(--info);
    flex-shrink: 0;
    margin-top: 2px;
}

/* Typing indicator spinner */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 12px;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    display: inline-block;
    animation: typing 1s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

/* Dynamic Source references badges under assistant chat bubble */
.source-citations {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 6px;
}

.source-ref-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 10px;
    color: var(--text-secondary);
    cursor: pointer;
    font-weight: 500;
    transition: var(--transition);
}

.source-ref-badge:hover {
    background: rgba(99, 102, 241, 0.1);
    border-color: rgba(99, 102, 241, 0.4);
    color: var(--primary-light);
    transform: translateY(-1px);
}

.source-ref-badge i {
    font-size: 10px;
}

.source-ref-badge .score-pill {
    background: rgba(16, 185, 129, 0.15);
    color: var(--success);
    padding: 1px 4px;
    border-radius: 4px;
    font-weight: 700;
}

/* ==========================================================================
   Chat controls (footer inputs)
   ========================================================================== */
.chat-controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-alert {
    background: var(--warning-bg);
    border: 1px solid rgba(245, 158, 11, 0.25);
    color: #fef3c7;
    font-size: 11px;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    gap: 10px;
    animation: slideInDown 0.2s ease-out;
}

.chat-alert i {
    color: var(--warning);
}

.chat-input-wrapper {
    display: flex;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 6px;
    transition: var(--transition);
}

.chat-input-wrapper:focus-within {
    border-color: var(--primary-light);
    box-shadow: 0 0 10px var(--primary-glow);
}

.chat-input-wrapper input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    padding: 10px 14px;
    font-family: var(--font-primary);
    font-size: 13px;
    outline: none;
}

.chat-input-wrapper input::placeholder {
    color: var(--text-muted);
}

.chat-input-wrapper input:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

.send-btn {
    background: linear-gradient(135deg, var(--primary), var(--accent));
    border: none;
    color: white;
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    box-shadow: 0 0 10px rgba(99, 102, 241, 0.2);
}

.send-btn:hover:not(:disabled) {
    transform: scale(1.04);
    box-shadow: 0 0 15px rgba(99, 102, 241, 0.4);
}

.send-btn:active:not(:disabled) {
    transform: scale(0.96);
}

.send-btn:disabled {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-muted);
    box-shadow: none;
    cursor: not-allowed;
}

/* ==========================================================================
   Source Modal Overlay
   ========================================================================== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    transition: opacity 0.3s ease;
}

.modal.hidden {
    display: none;
    pointer-events: none;
    opacity: 0;
}

.modal-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(3, 7, 18, 0.75);
    backdrop-filter: blur(8px);
}

.modal-content {
    position: relative;
    background: #0f172a;
    border: 1px solid var(--border-glow);
    border-radius: var(--radius-md);
    width: 650px;
    max-width: 90%;
    max-height: 85vh;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    animation: zoomIn 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: hidden;
    z-index: 1001;
}

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

.modal-title-group {
    display: flex;
    align-items: center;
    gap: 12px;
}

.modal-title-group h3 {
    font-size: 16px;
    font-weight: 700;
}

.modal-body {
    padding: 24px;
    overflow-y: auto;
}

.meta-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
    margin-bottom: 20px;
}

.meta-item {
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid var(--border-color);
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.meta-label {
    font-size: 10px;
    color: var(--text-secondary);
    text-transform: uppercase;
    font-weight: 600;
    letter-spacing: 0.5px;
}

.meta-value {
    font-size: 14px;
    font-weight: 700;
    color: var(--text-primary);
}

.meta-value.code-text {
    font-family: var(--font-mono);
    font-size: 11px;
    font-weight: 400;
    color: var(--primary-light);
}

.excerpt-container {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 16px;
    max-height: 350px;
    overflow-y: auto;
}

.excerpt-container pre {
    font-family: var(--font-mono);
    font-size: 12px;
    color: #e2e8f0;
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.7;
}

/* Source Badges */
.source-badge {
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
}

.source-badge.text-badge {
    background: rgba(99, 102, 241, 0.15);
    color: var(--primary-light);
}

.source-badge.table-badge {
    background: rgba(245, 158, 11, 0.15);
    color: var(--warning);
}

.source-badge.visual-badge {
    background: rgba(139, 92, 246, 0.15);
    color: var(--accent);
}

/* ==========================================================================
   Buttons and Controls
   ========================================================================== */
.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
}

.icon-btn.delete-btn:hover {
    background: var(--danger-bg);
    color: var(--danger);
}

.text-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-family: var(--font-primary);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 6px;
    transition: var(--transition);
}

.text-btn:hover {
    background: rgba(255, 255, 255, 0.04);
    color: var(--text-primary);
}

/* Helpers */
.hidden {
    display: none !important;
}

/* ==========================================================================
   Animations
   ========================================================================== */
@keyframes pulse {
    0% { opacity: 0.4; }
    50% { opacity: 1; }
    100% { opacity: 0.4; }
}

@keyframes typing {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-4px); }
}

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

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

@keyframes zoomIn {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
}

/* ==========================================================================
   Responsive Adaptations
   ========================================================================== */
@media (max-width: 1024px) {
    .app-main {
        grid-template-columns: 1fr;
    }
    .app-container {
        height: auto;
        overflow-y: auto;
    }
    .chat-messages {
        height: 450px;
    }
}
