/* ===== Energy Doc Analyzer - Interface ===== */
:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --success: #16a34a;
    --warning: #d97706;
    --danger: #dc2626;
    --bg: #f8fafc;
    --surface: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-light: #64748b;
    --sidebar-w: 420px;
}

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

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
}

/* --- Header --- */
.header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 24px;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 100;
}

.header h1 {
    font-size: 18px;
    font-weight: 700;
    color: var(--primary);
}

.header-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

/* --- Layout --- */
.app-layout {
    display: flex;
    height: calc(100vh - 56px);
}

/* --- Sidebar (Document List) --- */
.sidebar {
    width: var(--sidebar-w);
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border);
}

.sidebar-header h2 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 8px;
}

.search-box {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    outline: none;
}

.search-box:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

.filter-row {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.filter-row select {
    flex: 1;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 12px;
    background: white;
}

.doc-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.doc-item {
    padding: 12px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 4px;
    transition: background 0.15s;
    border: 1px solid transparent;
}

.doc-item:hover { background: #f1f5f9; }
.doc-item.active {
    background: #eff6ff;
    border-color: var(--primary);
}

.doc-item .doc-name {
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.doc-item .doc-meta {
    font-size: 11px;
    color: var(--text-light);
    display: flex;
    gap: 8px;
    align-items: center;
}

.status-badge {
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
}

.status-pending { background: #fef3c7; color: #92400e; }
.status-processing { background: #dbeafe; color: #1e40af; }
.status-extracted { background: #d1fae5; color: #065f46; }
.status-validated { background: #a7f3d0; color: #047857; }
.status-error { background: #fee2e2; color: #991b1b; }

/* --- Main Content --- */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* --- PDF Viewer --- */
.pdf-viewer-container {
    flex: 1;
    position: relative;
    overflow: auto;
    background: #4a5568;
    display: flex;
    justify-content: center;
    padding: 20px;
}

.pdf-canvas-wrapper {
    position: relative;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}

#pdf-canvas {
    display: block;
}

.highlight-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.highlight-overlay.annotating {
    pointer-events: auto;
    cursor: crosshair;
}

.highlight-rect {
    position: absolute;
    opacity: 0.35;
    border: 3px solid;
    border-radius: 4px;
    transition: opacity 0.3s, box-shadow 0.3s, transform 0.2s;
    box-shadow: 0 0 6px rgba(0,0,0,0.3);
    cursor: pointer;
    z-index: 10;
}

.highlight-rect:hover {
    opacity: 0.55;
    box-shadow: 0 0 12px rgba(0,0,0,0.6);
    transform: scale(1.02);
    z-index: 20;
}

.highlight-rect .hl-label {
    position: absolute;
    top: -20px;
    left: -1px;
    font-size: 10px;
    font-weight: 700;
    padding: 2px 6px;
    border-radius: 3px;
    color: white;
    text-shadow: 0 1px 2px rgba(0,0,0,0.5);
    white-space: nowrap;
    letter-spacing: 0.3px;
    white-space: nowrap;
}

.pdf-nav {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 16px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    justify-content: center;
}

.pdf-nav button {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: white;
    cursor: pointer;
    font-size: 13px;
}

.pdf-nav button:hover { background: #f1f5f9; }
.pdf-nav button:disabled { opacity: 0.4; cursor: default; }

/* --- Right Panel (Extraction & Validation) --- */
.right-panel {
    width: 420px;
    background: var(--surface);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.panel-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
}

.panel-tab {
    flex: 1;
    padding: 12px;
    text-align: center;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    color: var(--text-light);
    background: none;
    border-top: none;
    border-left: none;
    border-right: none;
}

.panel-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.panel-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

/* --- Extraction Fields --- */
.field-group {
    margin-bottom: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
}

.field-header {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    background: #f8fafc;
    gap: 8px;
}

.field-color-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
}

.field-label {
    font-size: 12px;
    font-weight: 600;
    flex: 1;
}

.field-status-select {
    font-size: 11px;
    padding: 2px 6px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: white;
}

.field-body {
    padding: 10px 12px;
}

.field-input {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    margin-bottom: 4px;
}

.field-input:focus {
    border-color: var(--primary);
    outline: none;
}

.field-info {
    font-size: 11px;
    color: var(--text-light);
    display: flex;
    justify-content: space-between;
}

.confidence-bar {
    height: 3px;
    background: #e2e8f0;
    border-radius: 2px;
    margin-top: 4px;
}

.confidence-fill {
    height: 100%;
    border-radius: 2px;
    transition: width 0.3s;
}

/* --- Annotation Mode --- */
.annotation-toolbar {
    padding: 12px;
    background: #fef3c7;
    border: 1px solid #f59e0b;
    border-radius: 8px;
    margin-bottom: 12px;
}

.annotation-toolbar p {
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 8px;
}

.annotation-field-select {
    width: 100%;
    padding: 8px;
    border: 1px solid #f59e0b;
    border-radius: 6px;
    font-size: 13px;
    margin-bottom: 8px;
}

/* --- Buttons --- */
.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: #15803d; }
.btn-warning { background: var(--warning); color: white; }
.btn-danger { background: var(--danger); color: white; }
.btn-outline {
    background: white;
    border: 1px solid var(--border);
    color: var(--text);
}
.btn-outline:hover { background: #f1f5f9; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-block { width: 100%; }

/* --- Stats cards --- */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    margin-bottom: 16px;
}

.stat-card {
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    text-align: center;
}

.stat-value {
    font-size: 24px;
    font-weight: 700;
    color: var(--primary);
}

.stat-label {
    font-size: 11px;
    color: var(--text-light);
    margin-top: 2px;
}

/* --- Empty state --- */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-light);
    padding: 40px;
    text-align: center;
}

.empty-state svg {
    width: 64px;
    height: 64px;
    margin-bottom: 16px;
    opacity: 0.4;
}

/* --- Modal --- */
.modal-backdrop {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal {
    background: white;
    border-radius: 12px;
    padding: 24px;
    max-width: 500px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.modal h3 {
    font-size: 16px;
    margin-bottom: 16px;
}

/* --- Loading --- */
.loading-spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid #e2e8f0;
    border-top: 2px solid var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* --- Toast notifications --- */
.toast-container{position:fixed!important;top:20px!important;right:20px!important;z-index:99999!important}

.toast {
    padding: 12px 20px;
    border-radius: 8px;
    color: white;
    font-size: 13px;
    margin-top: 8px;
    animation: slideIn 0.3s ease;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.toast-info { background: var(--primary); }

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}


/* --- Click-to-page: field card clickable + highlight flash --- */
.field-group[data-has-page="true"] {
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
}
.field-group[data-has-page="true"]:hover {
    background: #f0f7ff;
    transform: translateX(2px);
}
.field-group[data-has-page="true"] .field-header .page-badge {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    font-size: 10px;
    color: #666;
    background: #f0f0f0;
    padding: 1px 6px;
    border-radius: 8px;
    margin-left: 6px;
}
.highlight-rect.flash {
    animation: highlightFlash 2s ease-out;
    z-index: 30;
}
@keyframes highlightFlash {
    0% { opacity: 0.85; box-shadow: 0 0 25px rgba(255,80,0,0.9); border-width: 4px; }
    15% { opacity: 0.5; box-shadow: 0 0 15px rgba(255,80,0,0.5); }
    30% { opacity: 0.8; box-shadow: 0 0 20px rgba(255,80,0,0.7); border-width: 4px; }
    50% { opacity: 0.5; box-shadow: 0 0 10px rgba(255,80,0,0.4); }
    70% { opacity: 0.65; box-shadow: 0 0 12px rgba(255,80,0,0.3); }
    100% { opacity: 0.35; box-shadow: 0 0 6px rgba(0,0,0,0.3); border-width: 3px; }
}


/* Active field highlight in panel */
.field-group.active {
    border-left: 4px solid #3b82f6 !important;
    background: #eff6ff !important;
    box-shadow: 0 2px 8px rgba(59,130,246,0.15);
    transition: all 0.3s ease;
}

/* Repeat block styling improvements */
.repeat-block {
    transition: box-shadow 0.3s ease;
}

.repeat-block:hover {
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* Smooth field appearance */
.field-group {
    transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
}

/* --- Sidebar Tabs --- */
.sidebar-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.sidebar-tab {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 10px 4px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-light);
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}
.sidebar-tab:hover { color: var(--text); background: #f8fafc; }
.sidebar-tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.sidebar-tab svg { flex-shrink: 0; }
.tab-count {
    background: #e2e8f0;
    color: var(--text-light);
    font-size: 10px;
    font-weight: 700;
    padding: 1px 6px;
    border-radius: 10px;
    min-width: 18px;
    text-align: center;
}
.sidebar-tab.active .tab-count { background: var(--primary); color: white; }

/* --- Tree View --- */
.tree-container { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.tree-toolbar { padding: 12px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.export-options { margin-top: 8px; display: flex; flex-direction: column; gap: 4px; }
.export-option {
    display: flex; align-items: center; gap: 6px;
    font-size: 11px; color: var(--text-light); cursor: pointer; padding: 2px 0;
}
.export-option:hover { color: var(--text); }
.export-option input[type="checkbox"] { width: 14px; height: 14px; accent-color: var(--primary); }
.tree-content { flex: 1; overflow-y: auto; padding: 8px; }
.tree-node { user-select: none; }
.tree-node-header {
    display: flex; align-items: center; gap: 4px;
    padding: 5px 8px; border-radius: 6px; cursor: pointer;
    font-size: 12px; transition: background 0.1s;
}
.tree-node-header:hover { background: #f1f5f9; }
.tree-toggle {
    width: 16px; height: 16px; display: flex; align-items: center;
    justify-content: center; flex-shrink: 0; color: var(--text-light);
    transition: transform 0.15s;
}
.tree-toggle.expanded { transform: rotate(90deg); }
.tree-toggle.hidden { visibility: hidden; }
.tree-icon { width: 16px; height: 16px; flex-shrink: 0; }
.tree-icon-folder { color: #f59e0b; }
.tree-icon-pdf { color: #ef4444; }
.tree-icon-json { color: #8b5cf6; }
.tree-icon-file { color: var(--text-light); }
.tree-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.tree-meta { font-size: 10px; color: var(--text-light); flex-shrink: 0; }
.tree-children { padding-left: 16px; display: none; }
.tree-children.expanded { display: block; }
.tree-node-header .doc-status-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.dot-pending { background: #f59e0b; }
.dot-processing { background: #3b82f6; }
.dot-extracted { background: #22c55e; }
.dot-validated { background: #047857; }
.dot-error { background: #ef4444; }
