:root {
  --bg-primary: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #1c2333;
  --bg-card: #1a1f2e;
  --bg-hover: #252d3a;
  --bg-input: #0d1117;
  --border-color: #30363d;
  --border-active: #58a6ff;
  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --text-muted: #484f58;
  --accent-blue: #58a6ff;
  --accent-green: #3fb950;
  --accent-purple: #a371f7;
  --accent-orange: #f0883e;
  --accent-red: #f85149;
  --accent-yellow: #d29922;
  --accent-cyan: #39d2c0;
  --sidebar-width: 320px;
  --toolbar-height: 44px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.5;
  overflow: hidden;
  height: 100vh;
}

#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ───────────────────────────────────────────*/

.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

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

.sidebar-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--accent-blue);
}

.sidebar-stats {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}

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

.sidebar-section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

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

.sidebar-actions { margin-top: 8px; }

.skill-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 8px;
}

.skill-toggle {
  font-size: 11px;
  padding: 3px 8px;
  background: var(--bg-tertiary);
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-secondary);
  border: 1px solid transparent;
  transition: all 0.15s;
}

.skill-toggle:hover { border-color: var(--border-color); color: var(--text-primary); }
.skill-toggle input { display: none; }
.skill-toggle input:checked + span,
.skill-toggle:has(input:checked) {
  background: var(--accent-blue);
  color: #fff;
  border-color: var(--accent-blue);
}

textarea, input[type="text"], input[type="number"], select {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}

textarea:focus, input:focus, select:focus { border-color: var(--border-active); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 6px 14px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  transition: all 0.15s;
  font-family: inherit;
}

.btn:hover { background: var(--bg-hover); border-color: var(--text-muted); }
.btn-primary { background: var(--accent-blue); color: #fff; border-color: var(--accent-blue); }
.btn-primary:hover { opacity: 0.85; }
.btn-secondary { background: var(--bg-tertiary); }
.btn-small { padding: 4px 10px; font-size: 11px; }

.upload-label .btn { font-size: 11px; }

.doc-list { margin-top: 6px; }

.doc-item, .session-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: background 0.1s;
  border-left: 2px solid transparent;
}

.doc-item:hover, .session-item:hover { background: var(--bg-hover); }
.doc-item.active, .session-item.active { background: var(--bg-tertiary); border-left-color: var(--accent-blue); }

.doc-item .file-icon { font-size: 14px; flex-shrink: 0; }
.doc-item .file-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.doc-item .file-status { font-size: 10px; padding: 1px 5px; border-radius: 4px; flex-shrink: 0; }
.doc-item .file-status.indexed { background: rgba(63,185,80,0.15); color: var(--accent-green); }
.doc-item .file-status.processing { background: rgba(240,136,62,0.15); color: var(--accent-orange); }

.doc-item .file-remove {
  width: 16px; height: 16px;
  background: none; border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  flex-shrink: 0;
}
.doc-item .file-remove:hover { color: var(--accent-red); }

.sidebar-footer { padding: 12px; margin-top: auto; }
.health-indicator {
  display: flex; align-items: center; gap: 6px;
  font-size: 11px; color: var(--text-secondary);
}
.health-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent-red);
}
.health-dot.healthy { background: var(--accent-green); animation: pulse 2s infinite; }

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

/* ── Main Content ───────────────────────────────────────*/

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-primary);
}

.toolbar {
  height: var(--toolbar-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  padding: 0 16px;
  flex-shrink: 0;
}

.toolbar-tabs { display: flex; gap: 2px; }

.tab {
  padding: 8px 16px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
  font-family: inherit;
}

.tab:hover { color: var(--text-primary); }
.tab.active { color: var(--accent-blue); border-bottom-color: var(--accent-blue); }

.tab-panes { flex: 1; overflow: hidden; position: relative; }
.tab-pane { display: none; height: calc(100vh - var(--toolbar-height)); overflow-y: auto; padding: 16px; }
.tab-pane.active { display: block; }

/* ── Analysis ─────────────────────────────────────────────────*/

.analysis-container { height: 100%; }

.research-output {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 20px;
  height: 100%;
  overflow-y: auto;
  font-size: 13px;
  line-height: 1.6;
}

.research-section { margin-bottom: 20px; }
.research-section h3 { color: var(--accent-blue); font-size: 14px; margin-bottom: 8px; }
.research-section h4 { color: var(--accent-purple); font-size: 13px; margin: 12px 0 6px; }
.research-section p { margin-bottom: 8px; color: var(--text-primary); }

.research-section .citation {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 11px;
  color: var(--accent-cyan);
  cursor: pointer;
  margin: 2px;
}
.research-section .citation:hover { border-color: var(--accent-blue); }

.research-section ul { padding-left: 18px; margin: 6px 0; }
.research-section li { margin: 3px 0; }
.research-section li::marker { color: var(--accent-blue); }

.research-section table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
}
.research-section th { text-align: left; padding: 6px 10px; background: var(--bg-tertiary); color: var(--text-secondary); border: 1px solid var(--border-color); font-weight: 600; font-size: 11px; }
.research-section td { padding: 5px 10px; border: 1px solid var(--border-color); color: var(--text-primary); }

.research-section .block {
  background: var(--bg-tertiary);
  border-left: 3px solid var(--accent-blue);
  padding: 10px 14px;
  border-radius: 0 6px 6px 0;
  margin: 8px 0;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
}
.empty-icon { font-size: 40px; margin-bottom: 12px; opacity: 0.5; }
.empty-text { font-size: 14px; }

/* ── Viewer ─────────────────────────────────────────────────*/

.viewer-container { height: 100%; display: flex; flex-direction: column; }

#viewer-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

#viewer-doc-name { font-weight: 600; color: var(--accent-blue); font-size: 14px; }
#viewer-page-info { color: var(--text-secondary); font-size: 12px; }

.viewer-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.page-input {
  width: 60px;
  text-align: center;
  padding: 4px 6px;
  font-size: 12px;
}

.viewer-toggle {
  display: flex;
  gap: 12px;
  margin-left: auto;
}
.viewer-toggle label {
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--text-secondary);
}
.viewer-toggle input { cursor: pointer; }

.polygon-viewer {
  flex: 1;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  background: #1a1a2e;
}

.polygon-svg, .polygon-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

/* ── Search ────────────────────────────────────────────*/

.search-container { max-width: 800px; }

.search-input-row {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.search-input { flex: 1; }
.search-type-select { width: 120px; }

.search-results-list { }

.search-result-item {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: all 0.15s;
}
.search-result-item:hover {
  border-color: var(--accent-blue);
  background: var(--bg-hover);
}
.search-result-item .result-score { font-size: 11px; color: var(--accent-blue); margin-bottom: 4px; }
.search-result-item .result-content { font-size: 13px; color: var(--text-primary); }
.search-result-item .result-content mark { background: var(--accent-orange); color: #000; }
.search-result-item .result-meta { font-size: 11px; color: var(--text-muted); margin-top: 4px; }

/* ── Memories ─────────────────────────────────────────*/

.memories-container { max-width: 800px; }
.memories-container h3 { color: var(--accent-purple); margin-bottom: 12px; }

.memories-list { }

.memory-item {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-left: 3px solid var(--accent-purple);
  border-radius: 0 6px 6px 0;
  padding: 10px 14px;
  margin-bottom: 8px;
  font-size: 13px;
}
.memory-item .memory-type { font-size: 10px; color: var(--accent-orange); font-weight: 600; }
.memory-item .memory-content { margin-top: 4px; color: var(--text-primary); }

/* ── Findings ────────────────────────────────────────────*/

.findings-container { max-width: 800px; }
.findings-container h3 { color: var(--accent-cyan); margin-bottom: 12px; }

.findings-list { }
.finding-item {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 8px;
}
.finding-item .finding-agent { font-size: 11px; color: var(--accent-purple); font-weight: 600; }
.finding-item .finding-query { font-size: 12px; color: var(--text-secondary); margin: 4px 0; }
.finding-item .finding-answer { font-size: 13px; color: var(--text-primary); margin-top: 6px; }

/* ── Chat Overlay ───────────────────────────────────────

.chat-overlay {
  position: fixed;
  top: 0; right: 0;
  width: 420px;
  height: 100vh;
  background: var(--bg-secondary);
  border-left: 1px solid var(--border-color);
  z-index: 100;
  display: flex;
  flex-direction: column;
}

.chat-window {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.chat-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  font-size: 14px;
  font-weight: 600;
  color: var(--accent-blue);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.chat-header .btn-close {
  background: none; border: none;
  color: var(--text-secondary);
  font-size: 20px;
  cursor: pointer;
  padding: 0 4px;
}
.chat-header .btn-close:hover { color: var(--text-primary); }

.chat-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.chat-message {
  margin-bottom: 12px;
  font-size: 13px;
}
.chat-message.sender { margin-left: 32px; }
.chat-message.received { margin-right: 32px; }
.chat-message .msg-label { font-size: 10px; font-weight: 600; margin-bottom: 2px; }
.chat-message.sender .msg-label { color: var(--accent-purple); }
.chat-message.received .msg-label { color: var(--accent-cyan); }

.chat-input-row {
  padding: 12px;
  border-top: 1px solid var(--border-color);
  display: flex;
  gap: 8px;
}

.chat-input {
  flex: 1;
  padding: 8px 10px;
}

/* ── Loading ──────────────────────────────────────

.spinner {
  display: inline-block;
  width: 16px; height: 16px;
  border: 2px solid var(--border-color);
  border-top-color: var(--accent-blue);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.typing { color: var(--text-muted); font-style: italic; font-size: 12px; }

/* ── Pipeline ─────────────────────────────────────*/

.mode-section {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.mode-toggle {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
}
.mode-toggle input { cursor: pointer; margin: 0; }

.mode-select {
  font-size: 11px;
  padding: 3px 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  color: var(--text-primary);
}

.pipeline-container { max-width: 960px; }

.pipeline-header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.pipeline-header-row h3 { color: var(--accent-green); margin: 0; }
.pipeline-controls { display: flex; align-items: center; gap: 10px; }

.pipeline-output { }

.pipeline-stage-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  margin-bottom: 16px;
  overflow: hidden;
}

.pipeline-stage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border-color);
  cursor: pointer;
  user-select: none;
}
.pipeline-stage-header:hover { background: var(--bg-hover); }
.pipeline-stage-header .stage-title { font-size: 13px; font-weight: 600; }
.pipeline-stage-header .stage-status { font-size: 11px; padding: 2px 8px; border-radius: 4px; }
.pipeline-stage-header .stage-status.complete { background: rgba(63,185,80,0.15); color: var(--accent-green); }
.pipeline-stage-header .stage-body { padding: 14px 16px; font-size: 13px; color: var(--text-primary); max-height: 800px; overflow-y: auto; }
.pipeline-stage-header .stage-body.collapsed { display: none; }

.pipeline-section-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--accent-blue);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin: 10px 0 4px;
}

.pipeline-section-content {
  padding: 6px 0;
  color: var(--text-primary);
}

.evidence-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
}
.evidence-table th {
  text-align: left;
  padding: 6px 10px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  font-weight: 600;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.evidence-table td {
  padding: 6px 10px;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  vertical-align: top;
}
.evidence-table td.confidence-high { color: var(--accent-green); }
.evidence-table td.confidence-medium { color: var(--accent-yellow); }
.evidence-table td.confidence-low { color: var(--accent-red); }

.synthesis-card {
  background: var(--bg-tertiary);
  border-left: 3px solid var(--accent-green);
  padding: 12px 16px;
  border-radius: 0 6px 6px 0;
  margin: 8px 0;
}
.synthesis-card h4 { color: var(--accent-green); font-size: 14px; margin: 0 0 8px; }

.gap-card {
  background: var(--bg-tertiary);
  border-left: 3px solid var(--accent-orange);
  padding: 10px 14px;
  border-radius: 0 6px 6px 0;
  margin: 6px 0;
}
.gap-card .gap-topic { font-weight: 600; color: var(--accent-orange); font-size: 12px; }
.gap-card .gap-detail { font-size: 12px; color: var(--text-primary); margin-top: 4px; }

.matrix-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
}
.matrix-table th {
  text-align: left;
  padding: 6px 10px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  font-weight: 600;
  font-size: 10px;
}
.matrix-table td {
  padding: 5px 10px;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
}
