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

:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --primary-light: #a5b4fc;
    --bg: #f1f5f9;
    --surface: #ffffff;
    --sidebar-bg: #1e293b;
    --sidebar-text: #94a3b8;
    --sidebar-active: #e2e8f0;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --success: #22c55e;
    --success-bg: #dcfce7;
    --danger: #ef4444;
    --danger-bg: #fee2e2;
    --blue: #3b82f6;
    --blue-bg: #dbeafe;
    --green: #22c55e;
    --green-bg: #dcfce7;
    --purple: #8b5cf6;
    --purple-bg: #ede9fe;
    --orange: #f59e0b;
    --orange-bg: #fef3c7;
    --teal: #14b8a6;
    --teal-bg: #ccfbf1;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
    --transition: 0.2s ease;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    display: flex;
    min-height: 100vh;
    overflow: hidden;
}

/* === Sidebar === */
.sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    height: 100vh;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 100;
}

.sidebar-header {
    padding: 24px 20px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo-icon {
    width: 36px;
    height: 36px;
    background: var(--primary);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 18px;
    color: white;
}

.logo-text {
    font-size: 18px;
    font-weight: 700;
    color: #f1f5f9;
    letter-spacing: -0.3px;
}

.nav-links {
    list-style: none;
    padding: 16px 12px;
    flex: 1;
}

.nav-links li {
    margin-bottom: 4px;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    transition: all var(--transition);
}

.nav-link:hover {
    background: rgba(255,255,255,0.08);
    color: #e2e8f0;
}

.nav-link.active {
    background: var(--primary);
    color: white;
}

.nav-link svg {
    flex-shrink: 0;
}

.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid rgba(255,255,255,0.08);
}

.status-badge {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: var(--success);
}

.status-dot {
    width: 8px;
    height: 8px;
    background: var(--success);
    border-radius: 50%;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

/* === Main Content === */
.main-content {
    flex: 1;
    margin-left: 260px;
    height: 100vh;
    overflow-y: auto;
    padding: 32px 40px;
}

.page {
    display: none;
    animation: fadeIn 0.3s ease;
}

.page.active {
    display: block;
}

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

.page-header {
    margin-bottom: 28px;
}

.page-header h1 {
    font-size: 26px;
    font-weight: 700;
    color: var(--text);
    letter-spacing: -0.5px;
}

.page-subtitle {
    color: var(--text-muted);
    font-size: 14px;
    margin-top: 4px;
}

/* === Stats Row === */
.stats-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 32px;
}

.stat-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    box-shadow: var(--shadow);
    transition: transform var(--transition), box-shadow var(--transition);
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.stat-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.stat-icon.blue { background: var(--blue-bg); color: var(--blue); }
.stat-icon.blue svg { stroke: var(--blue); }
.stat-icon.green { background: var(--green-bg); color: var(--green); }
.stat-icon.green svg { stroke: var(--green); }
.stat-icon.purple { background: var(--purple-bg); color: var(--purple); }
.stat-icon.purple svg { stroke: var(--purple); }

.stat-info {
    display: flex;
    flex-direction: column;
}

.stat-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--text);
    line-height: 1;
}

.stat-label {
    font-size: 13px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* === Section Title === */
.section-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 16px;
}

/* === Department Grid === */
.dept-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 16px;
}

.dept-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: all var(--transition);
    border-left: 4px solid var(--primary);
    position: relative;
    overflow: hidden;
}

.dept-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.dept-card:nth-child(5n+1) { border-left-color: var(--blue); }
.dept-card:nth-child(5n+2) { border-left-color: var(--green); }
.dept-card:nth-child(5n+3) { border-left-color: var(--purple); }
.dept-card:nth-child(5n+4) { border-left-color: var(--orange); }
.dept-card:nth-child(5n+5) { border-left-color: var(--teal); }

.dept-card-name {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 8px;
}

.dept-card-count {
    font-size: 13px;
    color: var(--text-muted);
}

.dept-card-count strong {
    font-size: 20px;
    color: var(--text);
    display: block;
    margin-bottom: 2px;
}

/* === Empty State === */
.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--text-muted);
    grid-column: 1 / -1;
}

.empty-state svg {
    margin-bottom: 16px;
}

.empty-state p {
    margin-bottom: 16px;
    font-size: 14px;
}

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    border: none;
    cursor: pointer;
    transition: all var(--transition);
    text-decoration: none;
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-primary:disabled {
    background: var(--primary-light);
    cursor: not-allowed;
}

.btn-full {
    width: 100%;
    justify-content: center;
    padding: 12px 20px;
    font-size: 15px;
}

/* === Upload Form === */
.upload-container {
    max-width: 560px;
}

.upload-form {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 28px;
    box-shadow: var(--shadow);
}

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

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

select, input[type="text"] {
    width: 100%;
    padding: 10px 14px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    color: var(--text);
    background: var(--surface);
    transition: border-color var(--transition);
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2364748b' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10l-5 5z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
}

input[type="text"] {
    background-image: none;
}

select:focus, input[type="text"]:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99,102,241,0.15);
}

/* === Dropzone === */
.dropzone {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 40px 24px;
    text-align: center;
    cursor: pointer;
    transition: all var(--transition);
    background: #fafbfc;
}

.dropzone:hover, .dropzone.dragover {
    border-color: var(--primary);
    background: rgba(99,102,241,0.04);
}

.dropzone-text {
    margin-top: 12px;
    font-size: 14px;
    color: var(--text-muted);
}

.dropzone-link {
    color: var(--primary);
    font-weight: 600;
    cursor: pointer;
}

.dropzone-hint {
    margin-top: 6px;
    font-size: 12px;
    color: #94a3b8;
}

.file-preview {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 12px;
    padding: 10px 14px;
    background: var(--blue-bg);
    border-radius: var(--radius-sm);
    font-size: 14px;
    color: var(--blue);
    font-weight: 500;
}

.file-preview svg {
    stroke: var(--blue);
    flex-shrink: 0;
}

.file-preview.hidden {
    display: none;
}

.file-remove {
    margin-left: auto;
    background: none;
    border: none;
    font-size: 20px;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}

.file-remove:hover {
    color: var(--danger);
}

/* === Upload Status === */
.upload-status {
    margin-top: 16px;
    padding: 14px 18px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 10px;
}

.upload-status.hidden {
    display: none;
}

.upload-status.loading {
    background: var(--blue-bg);
    color: var(--blue);
}

.upload-status.success {
    background: var(--success-bg);
    color: #16a34a;
}

.upload-status.error {
    background: var(--danger-bg);
    color: var(--danger);
}

.spinner {
    width: 18px;
    height: 18px;
    border: 2.5px solid var(--blue-bg);
    border-top-color: var(--blue);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* === Filter Bar === */
.filter-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}

.filter-bar label {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-muted);
    white-space: nowrap;
}

.filter-bar select {
    width: auto;
    min-width: 180px;
}

/* === Documents List === */
.documents-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.doc-row {
    background: var(--surface);
    border-radius: var(--radius-sm);
    padding: 16px 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    box-shadow: var(--shadow);
    transition: all var(--transition);
}

.doc-row:hover {
    box-shadow: var(--shadow-md);
    transform: translateX(4px);
}

.doc-icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

.doc-icon.pdf { background: #fee2e2; color: #ef4444; }
.doc-icon.docx { background: var(--blue-bg); color: var(--blue); }
.doc-icon.txt { background: #f3f4f6; color: #6b7280; }
.doc-icon.html { background: var(--orange-bg); color: var(--orange); }
.doc-icon.img { background: var(--teal-bg); color: var(--teal); }

.doc-info {
    flex: 1;
    min-width: 0;
}

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

.doc-meta {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 3px;
    display: flex;
    gap: 12px;
}

.doc-badge {
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    background: var(--purple-bg);
    color: var(--purple);
    white-space: nowrap;
}

.doc-sections {
    font-size: 13px;
    color: var(--text-muted);
    white-space: nowrap;
}

.btn-delete {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all var(--transition);
}

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

.btn-delete:active {
    transform: scale(0.92);
}

/* === Chat === */
.chat-wrapper {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 64px);
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

.chat-header-left {
    display: flex;
    align-items: center;
    gap: 14px;
}

.chat-avatar {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: var(--primary);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
}

.chat-header h2 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
}

.chat-header-sub {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 2px;
}

.chat-header-right select {
    width: auto;
    min-width: 160px;
    padding: 8px 12px;
    font-size: 13px;
}

.chatbox {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: #f8fafc;
}

.chat-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 60px 24px;
    flex: 1;
}

.chat-welcome-icon {
    width: 72px;
    height: 72px;
    background: var(--purple-bg);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
}

.chat-welcome h3 {
    font-size: 20px;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 8px;
}

.chat-welcome p {
    font-size: 14px;
    color: var(--text-muted);
    max-width: 420px;
    line-height: 1.5;
}

.suggestion-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 20px;
    justify-content: center;
}

.chip {
    padding: 8px 16px;
    border: 1.5px solid var(--border);
    border-radius: 20px;
    background: var(--surface);
    font-size: 13px;
    color: var(--text-muted);
    cursor: pointer;
    transition: all var(--transition);
}

.chip:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: rgba(99,102,241,0.04);
}

/* Chat Messages */
.message-bubble {
    max-width: 75%;
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
    white-space: pre-wrap;
    animation: msgIn 0.25s ease;
}

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

.message-bubble.user {
    align-self: flex-end;
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.message-bubble.assistant {
    align-self: flex-start;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.message-bubble.thinking {
    align-self: flex-start;
    background: var(--surface);
    color: var(--text-muted);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Sources Card */
.sources-card {
    align-self: flex-start;
    max-width: 75%;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
    animation: msgIn 0.25s ease;
}

.sources-card-header {
    padding: 10px 16px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    background: #f8fafc;
    border-bottom: 1px solid var(--border);
}

.source-item {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
    line-height: 1.5;
}

.source-item:last-child {
    border-bottom: none;
}

.source-item-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}

.source-context {
    color: var(--text-muted);
    font-size: 12px;
}

/* Chat Input */
.chat-input-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 16px 24px;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

.chat-input-bar input[type="text"] {
    flex: 1;
    border: 1.5px solid var(--border);
    border-radius: 24px;
    padding: 12px 20px;
    font-size: 14px;
    background: #f8fafc;
    background-image: none;
}

.chat-input-bar input[type="text"]:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99,102,241,0.12);
    background: var(--surface);
}

.btn-send {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--primary);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all var(--transition);
}

.btn-send:hover {
    background: var(--primary-dark);
    transform: scale(1.05);
}

.btn-send:disabled {
    background: var(--primary-light);
    cursor: not-allowed;
    transform: none;
}

/* === Responsive === */
@media (max-width: 768px) {
    .sidebar {
        width: 64px;
        min-width: 64px;
    }
    .sidebar .logo-text,
    .sidebar .nav-link span,
    .sidebar .status-badge {
        display: none;
    }
    .sidebar .nav-link {
        justify-content: center;
        padding: 12px;
    }
    .sidebar-header {
        padding: 16px;
        display: flex;
        justify-content: center;
    }
    .main-content {
        margin-left: 64px;
        padding: 20px 16px;
    }
    .stats-row {
        grid-template-columns: 1fr;
    }
    .dept-grid {
        grid-template-columns: 1fr 1fr;
    }
    .message-bubble, .sources-card {
        max-width: 90%;
    }
    .chat-header {
        flex-direction: column;
        gap: 10px;
        align-items: flex-start;
    }
    .chat-header-right select {
        width: 100%;
    }
    .suggestion-chips {
        flex-direction: column;
    }
}

/* === Answer Truncation & Highlights === */
.answer-truncated {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.6;
}

.answer-full {
    line-height: 1.6;
}

.hidden {
    display: none !important;
}

.show-more-btn {
    background: none;
    border: none;
    color: var(--primary);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    padding: 4px 0 0 0;
    margin-top: 4px;
    display: block;
    transition: color var(--transition);
}

.show-more-btn:hover {
    color: var(--primary-dark);
    text-decoration: underline;
}

/* Highlighted department | doc tags inside answer text */
.hl-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: linear-gradient(135deg, #ede9fe, #dbeafe);
    border: 1px solid var(--primary-light);
    border-radius: 6px;
    padding: 2px 10px;
    margin: 2px 0;
    font-size: 12px;
    font-weight: 600;
    vertical-align: middle;
}

.hl-dept {
    color: #7c3aed;
    background: rgba(139, 92, 246, 0.12);
    padding: 1px 6px;
    border-radius: 4px;
}

.hl-sep {
    color: #94a3b8;
    font-weight: 400;
}

.hl-doc {
    color: #2563eb;
    background: rgba(59, 130, 246, 0.10);
    padding: 1px 6px;
    border-radius: 4px;
}

/* Source card highlights */
.source-dept-badge {
    padding: 3px 10px;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 700;
    background: linear-gradient(135deg, #8b5cf6, #7c3aed);
    color: white;
    letter-spacing: 0.3px;
    text-transform: uppercase;
}

.source-doc-name {
    font-weight: 700;
    color: var(--blue);
    font-size: 14px;
}

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

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}
