@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500&display=swap');

:root {
  --bg-primary: #f5f5f7;
  --bg-secondary: #ffffff;
  --bg-tertiary: #eef0f4;
  --bg-card: #ffffff;
  --bg-glass: rgba(0, 0, 0, 0.03);
  --bg-glass-hover: rgba(0, 0, 0, 0.06);
  --border-subtle: #e0e2e8;
  --border-active: rgba(99, 60, 200, 0.5);
  --text-primary: #1a1a2e;
  --text-secondary: #555570;
  --text-muted: #8888a8;
  --accent-purple: #633cc8;
  --accent-purple-dim: rgba(99, 60, 200, 0.1);
  --accent-blue: #3b82f6;
  --accent-green: #10b981;
  --accent-amber: #f59e0b;
  --accent-red: #ef4444;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 10px 30px rgba(0,0,0,0.1);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  --sidebar-width: 40%;
}

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

html, body {
  height: 100%; width: 100%;
  font-family: 'Inter', -apple-system, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  touch-action: pan-x pan-y; /* prevent browser pinch zoom */
}

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 7px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: rgba(0,0,0,0.25); }

/* ===== LAYOUT ===== */
.app-container { display: flex; flex-direction: column; height: 100vh; }

/* ===== TOP BAR ===== */
.top-bar {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 100; flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.logo {
  display: flex; align-items: center; gap: 8px;
  font-weight: 700; font-size: 15px;
  color: var(--accent-purple);
  letter-spacing: -0.3px; flex-shrink: 0;
}
.logo svg { width: 20px; height: 20px; }

.file-selector { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.file-selector select {
  background: var(--bg-tertiary); color: var(--text-primary);
  border: 1px solid var(--border-subtle); border-radius: var(--radius-sm);
  padding: 6px 28px 6px 10px; font-size: 13px; font-family: inherit;
  cursor: pointer; outline: none; max-width: 280px; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23555570' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 8px center;
  transition: var(--transition);
}
.file-selector select:hover { border-color: var(--accent-purple); }

.nav-controls { display: flex; align-items: center; gap: 4px; margin-left: auto; flex-shrink: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 5px; padding: 5px 10px;
  background: var(--bg-tertiary); color: var(--text-secondary);
  border: 1px solid var(--border-subtle); border-radius: var(--radius-sm);
  font-size: 13px; font-family: inherit; cursor: pointer;
  transition: var(--transition); white-space: nowrap;
}
.btn:hover { background: var(--bg-glass-hover); color: var(--text-primary); border-color: var(--accent-purple); }
.btn:active { transform: scale(0.97); }
.btn-icon { padding: 5px 7px; }
.btn svg { width: 15px; height: 15px; flex-shrink: 0; }

.page-info {
  font-size: 13px; color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  padding: 0 6px; white-space: nowrap;
}
.page-info input {
  width: 38px; text-align: center;
  background: var(--bg-tertiary); color: var(--text-primary);
  border: 1px solid var(--border-subtle); border-radius: 4px;
  padding: 3px; font-size: 13px; font-family: inherit;
}

.zoom-controls {
  display: flex; align-items: center; gap: 3px;
  padding-left: 8px; margin-left: 8px;
  border-left: 1px solid var(--border-subtle);
}
.zoom-label { font-size: 12px; color: var(--text-muted); min-width: 38px; text-align: center; }

/* ===== MAIN AREA — 60:40 layout ===== */
.main-area { display: flex; flex: 1; overflow: hidden; }

/* ===== SIDEBAR (40% default, resizable) ===== */
.sidebar {
  width: var(--sidebar-width); flex-shrink: 0;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-subtle);
  display: flex; flex-direction: column;
  transition: width 0.3s ease, opacity 0.3s ease;
  overflow: hidden;
  min-width: 250px;
  max-width: 800px;
}
.sidebar.collapsed { width: 0 !important; min-width: 0; opacity: 0; pointer-events: none; }

/* ===== PANE RESIZER ===== */
.pane-resizer {
  width: 5px; flex-shrink: 0;
  background: var(--border-subtle);
  cursor: col-resize;
  transition: background 0.2s;
  position: relative;
}
.pane-resizer:hover,
.pane-resizer.active {
  background: var(--accent-purple);
}
.pane-resizer::after {
  content: '';
  position: absolute; left: -3px; right: -3px; top: 0; bottom: 0;
}

/* ===== SIDEBAR TABS ===== */
.sidebar-tabs {
  display: flex; border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.sidebar-tab {
  flex: 1; padding: 10px 12px;
  font-size: 13px; font-weight: 500;
  color: var(--text-muted); text-align: center;
  cursor: pointer; border: none; background: none;
  border-bottom: 2px solid transparent;
  transition: var(--transition);
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.sidebar-tab:hover { color: var(--text-primary); background: var(--bg-glass); }
.sidebar-tab.active {
  color: var(--accent-purple);
  border-bottom-color: var(--accent-purple);
  background: var(--accent-purple-dim);
}
.sidebar-tab .tab-badge {
  font-size: 10px; font-weight: 700;
  padding: 1px 6px; border-radius: 10px;
  background: var(--bg-tertiary); color: var(--text-muted);
}
.sidebar-tab.active .tab-badge {
  background: var(--accent-purple); color: #fff;
}

/* Tab panels */
.tab-panel { display: none; flex-direction: column; flex: 1; overflow: hidden; }
.tab-panel.active { display: flex; }

/* ===== SEARCH TAB ===== */
.sidebar-header {
  padding: 12px 14px 10px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.search-box { position: relative; display: flex; align-items: center; }
.search-box svg {
  position: absolute; left: 10px;
  width: 15px; height: 15px; color: var(--text-muted);
  pointer-events: none; z-index: 1;
}
.search-box input {
  width: 100%; padding: 9px 34px 9px 32px;
  background: var(--bg-tertiary); color: var(--text-primary);
  border: 1px solid var(--border-subtle); border-radius: var(--radius-md);
  font-size: 13px; font-family: inherit; outline: none;
  transition: var(--transition);
}
.search-box input:focus { border-color: var(--accent-purple); box-shadow: 0 0 0 3px rgba(99,60,200,0.1); }
.search-box input::placeholder { color: var(--text-muted); }

.search-clear {
  position: absolute; right: 8px;
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; padding: 4px; display: none;
  transition: var(--transition);
}
.search-clear:hover { color: var(--text-primary); }
.search-clear svg { width: 14px; height: 14px; }

.search-options { display: flex; align-items: center; gap: 12px; padding: 8px 0 0; }
.search-option {
  display: flex; align-items: center; gap: 5px;
  font-size: 12px; color: var(--text-muted); cursor: pointer; user-select: none;
}
.search-option input[type="checkbox"] {
  appearance: none; width: 14px; height: 14px;
  border: 1px solid var(--border-subtle); border-radius: 3px;
  background: var(--bg-tertiary); cursor: pointer;
  display: grid; place-content: center;
}
.search-option input[type="checkbox"]::before {
  content: ''; width: 8px; height: 8px;
  transform: scale(0); transition: 0.15s ease;
  background: var(--accent-purple); border-radius: 2px;
}
.search-option input[type="checkbox"]:checked::before { transform: scale(1); }
.search-option input[type="checkbox"]:checked { border-color: var(--accent-purple); }

.search-stats {
  padding: 7px 14px; font-size: 12px; color: var(--text-muted);
  border-bottom: 1px solid var(--border-subtle);
  display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0;
}
.search-stats .count { color: var(--accent-purple); font-weight: 600; }
.search-nav-btns { display: flex; gap: 3px; }
.search-nav-btns .btn { padding: 2px 6px; font-size: 11px; }

/* ===== RESULTS LIST ===== */
.results-list { flex: 1; overflow-y: auto; padding: 4px 6px; }

/* ===== PAGE GROUPS ===== */
.page-group {
  margin-bottom: 10px;
  border-radius: 8px;
  background: #ffffff;
  border: 1px solid var(--border-subtle);
  overflow: hidden;
}
.page-group.page-group-alt {
  background: #f9f7fe;
}
.page-group-header {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px;
  background: linear-gradient(135deg, var(--accent-purple-dim), rgba(59,130,246,0.06));
  border-bottom: 1px solid var(--border-subtle);
  position: sticky; top: 0; z-index: 2;
}
.page-group-label {
  font-size: 11px; font-weight: 700;
  color: var(--accent-purple);
  white-space: nowrap;
  letter-spacing: 0.3px;
  text-transform: uppercase;
}
.page-group-line {
  flex: 1; height: 1px;
  background: linear-gradient(to right, var(--border-subtle), transparent);
}

/* ===== RESULT CARDS (inside groups) ===== */
.result-card {
  padding: 5px 8px;
  cursor: pointer; transition: var(--transition);
  position: relative;
  border-bottom: 1px solid rgba(0,0,0,0.04);
}
.result-card:last-child { border-bottom: none; }
.result-card:hover {
  background: var(--accent-purple-dim) !important;
}
.result-card.active {
  background: rgba(99, 60, 200, 0.12) !important;
  border-left: 3px solid var(--accent-purple);
}

/* ===== PDF SNIPPET IMAGE IN RESULTS ===== */
.result-snippet-wrap {
  margin: 3px 0 4px;
  border-radius: 4px;
  overflow: hidden;
  background: #fdfdfd;
  border: 1px solid var(--border-subtle);
  min-height: 0;
}
.result-snippet-wrap:empty { display: none; }
.result-snippet {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 3px;
  image-rendering: -webkit-optimize-contrast;
}

.result-context { font-size: 12px; line-height: 1.45; word-break: break-word; }
.result-context .prefix,
.result-context .suffix { color: var(--text-muted); }
.result-context .match-text {
  color: #1a1a2e; font-weight: 600;
  background: rgba(99, 60, 200, 0.18);
  padding: 1px 3px; border-radius: 3px;
}

/* ===== INDEX STATUS TAB ===== */
.index-status-panel { padding: 16px; overflow-y: auto; }

.index-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 16px; margin-bottom: 12px;
  box-shadow: var(--shadow-sm);
}
.index-card h4 {
  font-size: 13px; font-weight: 600; color: var(--text-primary);
  margin-bottom: 10px; display: flex; align-items: center; gap: 8px;
}
.index-card h4 svg { width: 16px; height: 16px; color: var(--accent-purple); }

.index-progress-track {
  width: 100%; height: 8px; background: var(--bg-tertiary);
  border-radius: 4px; overflow: hidden; margin-bottom: 6px;
}
.index-progress-fill {
  height: 100%; border-radius: 4px;
  background: linear-gradient(90deg, var(--accent-purple), var(--accent-blue));
  transition: width 0.4s ease; width: 0%;
}
.index-progress-fill.complete {
  background: linear-gradient(90deg, var(--accent-green), #34d399);
}

.index-pct {
  font-size: 24px; font-weight: 700; color: var(--accent-purple);
  text-align: center; margin: 8px 0;
}
.index-pct.complete { color: var(--accent-green); }

.index-detail {
  font-size: 12px; color: var(--text-muted); margin-top: 4px;
  display: flex; justify-content: space-between;
}

.index-status-badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 10px; border-radius: 12px;
  font-size: 11px; font-weight: 600;
}
.index-status-badge.pending { background: #fef3c7; color: #92400e; }
.index-status-badge.indexing { background: #ede9fe; color: #5b21b6; }
.index-status-badge.ready { background: #d1fae5; color: #065f46; }
.index-status-badge.none { background: var(--bg-tertiary); color: var(--text-muted); }

.index-stat-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
  margin-top: 12px;
}
.index-stat {
  background: var(--bg-tertiary); border-radius: var(--radius-sm);
  padding: 10px; text-align: center;
}
.index-stat .label { font-size: 10px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.index-stat .value { font-size: 18px; font-weight: 700; color: var(--text-primary); margin-top: 2px; }

/* ===== LOADING / EMPTY STATES ===== */
.empty-state {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 40px 20px; text-align: center;
  color: var(--text-muted);
}
.empty-state svg { width: 40px; height: 40px; margin-bottom: 10px; opacity: 0.3; }
.empty-state h3 { font-size: 14px; font-weight: 500; margin-bottom: 4px; color: var(--text-secondary); }
.empty-state p { font-size: 12px; }

.loading-spinner {
  width: 22px; height: 22px;
  border: 2px solid var(--border-subtle);
  border-top-color: var(--accent-purple);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== PDF VIEWER (60% default) ===== */
.viewer-area {
  flex: 1; overflow: auto;
  background: var(--bg-primary);
  padding: 20px;
  min-width: 300px;
}

.pdf-page-wrapper {
  position: relative;
  box-shadow: var(--shadow-lg);
  border-radius: 3px;
  overflow: hidden;
  background: white;
  margin: 0 auto 16px;
}
.pdf-page-wrapper canvas { display: block; }

/* Placeholder for lazy-loaded pages — VISIBLE styling */
.pdf-page-wrapper.placeholder {
  background: #e8e8ec;
  display: flex; align-items: center; justify-content: center;
  border: 1px dashed #ccc;
}
.pdf-page-wrapper.placeholder span {
  color: #999;
  font-size: 14px;
  font-weight: 500;
}

/* Text layer */
.text-layer {
  position: absolute; top: 0; left: 0;
  width: 100%; height: 100%;
  overflow: hidden; opacity: 0.25; line-height: 1;
}
.text-layer span {
  position: absolute; white-space: pre;
  color: transparent; cursor: text;
}

/* Search highlight overlay — bright, visible on top of canvas */
.search-highlight-overlay {
  position: absolute;
  background: rgba(255, 200, 0, 0.45);
  outline: 3px solid #f59e0b;
  border-radius: 3px;
  z-index: 10;
  pointer-events: none;
  animation: highlightPulse 1.5s ease-in-out infinite;
}
@keyframes highlightPulse {
  0%, 100% { outline-color: #f59e0b; background: rgba(255, 200, 0, 0.45); }
  50% { outline-color: #d97706; background: rgba(255, 200, 0, 0.6); }
}

/* ===== WELCOME ===== */
.welcome-screen {
  display: flex; align-items: center; justify-content: center;
  flex: 1; width: 100%;
}
.welcome-card {
  text-align: center; padding: 50px 36px;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  max-width: 460px;
}
.welcome-card h2 {
  font-size: 20px; font-weight: 700; margin-bottom: 8px;
  color: var(--accent-purple);
}
.welcome-card p { font-size: 13px; color: var(--text-muted); margin-bottom: 16px; line-height: 1.5; }

.upload-area {
  border: 2px dashed var(--border-subtle);
  border-radius: var(--radius-md); padding: 24px;
  text-align: center; cursor: pointer;
  transition: var(--transition);
}
.upload-area:hover { border-color: var(--accent-purple); background: var(--accent-purple-dim); }
.upload-area input { display: none; }

/* ===== TOAST ===== */
.toast {
  position: fixed; bottom: 20px; right: 20px;
  background: var(--bg-card); color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 10px 18px; font-size: 13px;
  box-shadow: var(--shadow-lg);
  transform: translateY(80px); opacity: 0;
  transition: 0.3s ease; z-index: 1000;
}
.toast.show { transform: translateY(0); opacity: 1; }

/* ===== ANIMATIONS ===== */
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.result-card { animation: fadeIn 0.15s ease both; }
