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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f1f5f9;
    color: #1e293b;
    height: 100vh;
    overflow: hidden;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* ============ TOP BAR ============ */
.topbar {
    background: #1e293b;
    color: white;
    padding: 12px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-shrink: 0;
}

.topbar-brand { display: flex; align-items: center; gap: 12px; }
.topbar-brand h1 { font-size: 18px; font-weight: 600; }
.topbar-logo {
    width: 32px; height: 32px; border-radius: 8px;
    background: #2563eb; display: flex; align-items: center;
    justify-content: center; font-weight: 700; font-size: 16px;
}

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

/* ============ BUTTONS ============ */
.btn {
    padding: 8px 16px; border: none; border-radius: 8px;
    font-size: 13px; font-weight: 500; cursor: pointer;
    transition: background 0.15s; text-decoration: none;
}
.btn-primary { background: #2563eb; color: white; }
.btn-primary:hover { background: #1d4ed8; }
.btn-primary:disabled { background: #94a3b8; cursor: not-allowed; }
.btn-ghost { background: transparent; color: white; border: 1px solid rgba(255,255,255,0.2); }
.btn-ghost:hover { background: rgba(255,255,255,0.1); }
.btn-danger { background: #dc2626; color: white; }
.btn-danger:hover { background: #b91c1c; }
.btn-sm { padding: 4px 10px; font-size: 12px; border-radius: 6px; }
.btn-icon {
    background: none; border: 1px solid rgba(255,255,255,0.2);
    color: inherit; width: 28px; height: 28px; border-radius: 6px;
    cursor: pointer; font-size: 14px; display: flex;
    align-items: center; justify-content: center;
}
.btn-icon:hover { background: rgba(255,255,255,0.1); }

/* ============ MAIN LAYOUT ============ */
.main-layout {
    display: grid;
    grid-template-columns: 240px 1fr;
    flex: 1;
    overflow: hidden;
}

/* ============ SIDEBAR ============ */
.panel { background: white; border-right: 1px solid #e2e8f0; overflow-y: auto; }
.panel-header {
    padding: 14px 16px; border-bottom: 1px solid #e2e8f0;
    display: flex; align-items: center; justify-content: space-between;
    position: sticky; top: 0; background: white; z-index: 1;
}
.panel-header h2 { font-size: 14px; font-weight: 600; }

.panel-sidebar { display: flex; flex-direction: column; }

.source-list { flex: 1; overflow-y: auto; }
.source-item {
    padding: 10px 16px; cursor: pointer; font-size: 13px;
    border-bottom: 1px solid #f1f5f9; display: flex;
    justify-content: space-between; align-items: center;
    transition: background 0.1s;
}
.source-item:hover { background: #f8fafc; }
.source-item.active { background: #eff6ff; border-left: 3px solid #2563eb; }
.source-item .source-name {
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1;
}
.source-item .source-count {
    background: #e2e8f0; color: #475569; padding: 2px 8px;
    border-radius: 10px; font-size: 11px; font-weight: 600; margin-left: 8px;
}
.source-item .source-actions { display: none; margin-left: 8px; }
.source-item:hover .source-actions { display: flex; gap: 4px; }
.source-item:hover .source-count { display: none; }

.btn-source-delete {
    background: none; border: none; color: #dc2626; cursor: pointer;
    font-size: 14px; padding: 2px 4px; border-radius: 4px;
}
.btn-source-delete:hover { background: #fee2e2; }

.sidebar-stats {
    padding: 12px 16px; border-top: 1px solid #e2e8f0;
    font-size: 12px; color: #64748b; background: #f8fafc;
}

/* ============ MAIN PANEL ============ */
.panel-main { border-right: none; display: flex; flex-direction: column; }

.panel-header-actions {
    display: flex; align-items: center; gap: 10px;
}
.panel-header-actions .btn-ghost {
    color: #475569;
    border-color: #d1d5db;
}
.panel-header-actions .btn-ghost:hover {
    background: #f1f5f9;
}

.search-input {
    padding: 6px 12px; border: 1px solid #d1d5db; border-radius: 6px;
    font-size: 13px; width: 250px; outline: none;
}
.search-input:focus { border-color: #2563eb; box-shadow: 0 0 0 2px rgba(37,99,235,0.15); }

/* ============ CHUNK TABLE ============ */
.chunk-table-container { flex: 1; overflow-y: auto; padding: 0; }

.chunk-table {
    width: 100%; border-collapse: collapse; font-size: 13px;
}
.chunk-table thead { position: sticky; top: 0; z-index: 1; }
.chunk-table th {
    background: #f8fafc; padding: 10px 12px; text-align: left;
    font-weight: 600; font-size: 12px; color: #64748b;
    text-transform: uppercase; letter-spacing: 0.5px;
    border-bottom: 2px solid #e2e8f0;
}
.chunk-table td {
    padding: 10px 12px; border-bottom: 1px solid #f1f5f9;
    vertical-align: top;
}
.chunk-table tr:hover { background: #fafbfd; }

.col-source { width: 150px; }
.col-content { }
.col-metadata { width: 160px; }
.col-date { width: 100px; }
.col-actions { width: 90px; }

.chunk-content {
    max-height: 80px; overflow: hidden; line-height: 1.5;
    white-space: pre-wrap; word-break: break-word;
}
.chunk-content.expanded { max-height: none; }

.chunk-source {
    font-family: 'Cascadia Code', 'Consolas', monospace;
    font-size: 11px; color: #475569; word-break: break-all;
}

.chunk-meta-tags { display: flex; flex-wrap: wrap; gap: 4px; }
.meta-tag {
    background: #e0e7ff; color: #3730a3; padding: 2px 6px;
    border-radius: 4px; font-size: 11px;
}

.chunk-date { font-size: 12px; color: #64748b; white-space: nowrap; }

.chunk-actions { display: flex; gap: 4px; }
.btn-edit, .btn-delete {
    background: none; border: 1px solid #d1d5db; cursor: pointer;
    padding: 4px 8px; border-radius: 4px; font-size: 12px;
    transition: all 0.1s;
}
.btn-edit { color: #2563eb; }
.btn-edit:hover { background: #eff6ff; border-color: #2563eb; }
.btn-delete { color: #dc2626; }
.btn-delete:hover { background: #fef2f2; border-color: #dc2626; }
.btn-expand {
    background: none; border: none; color: #2563eb; cursor: pointer;
    font-size: 11px; padding: 2px 0; margin-top: 4px;
}

.empty-state {
    display: flex; align-items: center; justify-content: center;
    padding: 60px 20px; color: #94a3b8; font-size: 14px;
}

/* ============ PAGINATION ============ */
.pagination {
    padding: 12px 16px; border-top: 1px solid #e2e8f0;
    display: flex; align-items: center; justify-content: space-between;
    font-size: 13px; color: #64748b; background: #f8fafc;
}
.pagination-buttons { display: flex; gap: 6px; }
.pagination .btn {
    background: white; color: #1e293b; border: 1px solid #d1d5db;
}
.pagination .btn:hover { background: #f1f5f9; }
.pagination .btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ============ MODALS ============ */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.5);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
}
.modal {
    background: white; border-radius: 12px; width: 600px;
    max-height: 85vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,0.2);
    padding: 24px;
}
.modal-sm { width: 420px; }
.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 20px;
}
.modal-header h3 { font-size: 16px; font-weight: 600; }
.btn-close {
    background: none; border: none; font-size: 22px; cursor: pointer;
    color: #94a3b8; padding: 0 4px;
}
.btn-close:hover { color: #1e293b; }
.modal-body { margin-bottom: 20px; color: #475569; line-height: 1.6; }

.form-row { display: flex; gap: 12px; }
.form-row .form-group { flex: 1; }
.form-group { margin-bottom: 16px; }
.form-group label {
    display: block; font-size: 13px; font-weight: 500;
    margin-bottom: 6px; color: #374151;
}
.form-group input,
.form-group select,
.form-group textarea {
    width: 100%; padding: 8px 12px; border: 1px solid #d1d5db;
    border-radius: 8px; font-size: 13px; font-family: inherit; outline: none;
}
.form-group textarea {
    font-family: 'Cascadia Code', 'Consolas', monospace;
    font-size: 12px; line-height: 1.6; resize: vertical;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    border-color: #2563eb; box-shadow: 0 0 0 2px rgba(37,99,235,0.15);
}

.form-actions {
    display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px;
}
.form-actions .btn-ghost { color: #475569; border-color: #d1d5db; }

/* ============ TOAST ============ */
.toast-container {
    position: fixed; bottom: 20px; right: 20px; z-index: 200;
    display: flex; flex-direction: column; gap: 8px;
}
.toast {
    padding: 12px 20px; border-radius: 8px; font-size: 13px;
    color: white; box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    animation: toast-in 0.3s ease;
}
.toast-success { background: #16a34a; }
.toast-error { background: #dc2626; }
.toast-info { background: #2563eb; }

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

/* ============ LOADING ============ */
.loading {
    display: inline-block; width: 16px; height: 16px;
    border: 2px solid rgba(255,255,255,0.3); border-top-color: white;
    border-radius: 50%; animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
