/* DOJ Document Viewer — Economist Dark Mode + Chat Sidebar */

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

:root {
  --bg: #121212;
  --surface: #1a1a1a;
  --surface-alt: #1f1f1f;
  --elevated: #242424;
  --text: #e0e0e0;
  --text-secondary: #9a9a9a;
  --text-muted: #666;
  --border: rgba(255,255,255,0.08);
  --border-hover: rgba(255,255,255,0.15);
  --red: #E3120B;
  --red-muted: rgba(227,18,11,0.15);
  --radius: 3px;
  --font: 'Inter', system-ui, -apple-system, sans-serif;
  --chat-width: 480px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  height: 100vh;
}

/* ---- Main Layout ---- */
.main-layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.archive-panel {
  flex: 1;
  overflow-y: auto;
  transition: margin-right 0.2s ease;
}

.archive-panel.chat-open {
  margin-right: 0;
}

/* ---- Header & Search ---- */
.header {
  background: var(--bg);
  padding: 12px 24px;
  position: sticky;
  top: 0;
  z-index: 100;
  border-bottom: 1px solid var(--border);
}

.search-box {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

.search-wrapper {
  position: relative;
  flex: 1;
  min-width: 200px;
}

.search-wrapper input[type="text"] {
  width: 100%;
  padding: 8px 12px;
  padding-right: 30px;
  font-size: 0.875rem;
  font-family: var(--font);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--elevated);
  color: var(--text);
  transition: border-color 0.15s;
}

.search-wrapper input[type="text"]::placeholder { color: var(--text-muted); }
.search-wrapper input[type="text"]:focus { border-color: var(--border-hover); outline: none; }

.search-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  display: none;
  line-height: 1;
}

.search-clear:hover { color: var(--text); }

.search-box select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  background: var(--elevated);
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 500;
  -webkit-appearance: none;
  appearance: none;
}

.search-box select:focus { border-color: var(--border-hover); outline: none; }

.search-box button[type="submit"] {
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  background: var(--red);
  color: #fff;
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 500;
}

.search-box button[type="submit"]:hover { background: #c50f09; }

.norway-link {
  display: inline-block;
  padding: 6px 14px;
  background: var(--red);
  color: #fff;
  text-decoration: none;
  border-radius: var(--radius);
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.norway-link:hover { background: #c50f09; }

/* Chat toggle button */
.chat-toggle-btn {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  background: var(--elevated);
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  transition: all 0.15s;
}

.chat-toggle-btn:hover { border-color: var(--border-hover); color: var(--text); }
.chat-toggle-btn.active { background: var(--red); color: #fff; border-color: var(--red); }

/* ---- Nav Bar ---- */
.nav-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 24px;
  background: var(--surface);
  font-size: 0.875rem;
  position: sticky;
  top: 49px;
  z-index: 99;
  border-bottom: 1px solid var(--border);
}

.nav-bar .index {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.875rem;
  font-weight: 500;
}

.nav-bar .controls { display: flex; gap: 8px; }

.nav-bar button {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 500;
}

.nav-bar button:hover { border-color: var(--border-hover); color: var(--text); }
.nav-bar button:disabled { opacity: 0.3; cursor: not-allowed; }
.nav-bar .help { color: var(--text-muted); font-size: 0.6875rem; }

/* ---- Gallery ---- */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
  padding: 16px 24px;
}

/* ---- Cards ---- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  position: relative;
  transition: border-color 0.15s;
}

.card:hover { border-color: var(--border-hover); }
.card.selected { border-color: var(--red); }

.card img {
  width: 100%;
  height: 160px;
  object-fit: cover;
  background: #000;
  display: block;
}

.card-info { padding: 10px; font-size: 0.6875rem; }

.card-info .dataset {
  color: var(--text-secondary);
  font-weight: 500;
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.card-info .type {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 2px;
  background: var(--elevated);
  border: 1px solid var(--border);
  margin-top: 4px;
  font-size: 0.625rem;
  font-weight: 500;
  color: var(--text-secondary);
  text-transform: uppercase;
}

.card-id {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 2;
  background: rgba(0,0,0,0.6);
  color: rgba(255,255,255,0.85);
  font-size: 0.625rem;
  padding: 2px 6px;
  border-radius: 2px;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

/* ---- Modal ---- */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.92);
  z-index: 200;
  overflow: auto;
}

.modal.active {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 60px;
}

.modal-nav {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  font-size: 48px;
  cursor: pointer;
  color: #fff;
  opacity: 0.4;
  z-index: 201;
  user-select: none;
}

.modal-nav:hover { opacity: 0.8; }
.modal-nav.prev { left: 20px; }
.modal-nav.next { right: 20px; }

.modal-content {
  max-width: 95vw;
  max-height: calc(100vh - 40px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-content img {
  max-width: 100%;
  display: block;
  margin: 0 auto;
}

.modal-pages {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 10px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.modal-pages img { max-width: 100%; }

.modal-info {
  padding: 16px 20px;
  flex-shrink: 0;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  border-top: 1px solid var(--border);
}

.modal-info .doc-id {
  font-size: 1rem;
  color: var(--text);
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-weight: 600;
  margin-bottom: 6px;
}

.modal-info b { color: var(--text); font-weight: 500; }

.close {
  position: fixed;
  top: 15px;
  right: 25px;
  font-size: 32px;
  cursor: pointer;
  color: #fff;
  opacity: 0.5;
  z-index: 202;
}

.close:hover { opacity: 0.9; }

/* ---- AI Filter Bar ---- */
.ai-filter-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 24px;
  background: var(--red-muted);
  border-bottom: 1px solid rgba(227,18,11,0.25);
  position: sticky;
  top: 98px;
  z-index: 98;
}

.ai-filter-label {
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--red);
}

.ai-filter-count {
  font-size: 0.75rem;
  color: var(--text-secondary);
  font-family: 'SF Mono', 'Fira Code', monospace;
}

.ai-filter-clear {
  margin-left: auto;
  padding: 4px 12px;
  border: 1px solid rgba(227,18,11,0.3);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: 0.6875rem;
  font-weight: 500;
  cursor: pointer;
}

.ai-filter-clear:hover { color: var(--text); border-color: rgba(227,18,11,0.5); }

.loading {
  text-align: center;
  padding: 32px;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 500;
  text-transform: uppercase;
}

.audio-player, .video-player { width: 100%; padding: 20px; }
.audio-player audio { width: 100%; }
.video-player video { width: 100%; max-height: 80vh; border-radius: var(--radius); }

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.18); }

/* ==== CHAT SIDEBAR ==== */

.chat-panel {
  width: var(--chat-width);
  min-width: var(--chat-width);
  max-width: var(--chat-width);
  background: var(--bg);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  transform: translateX(100%);
  position: fixed;
  right: 0;
  top: 0;
  z-index: 150;
  transition: transform 0.2s ease;
}

.chat-panel.open {
  transform: translateX(0);
}

.chat-panel.open ~ .archive-panel,
.archive-panel.chat-open {
  margin-right: var(--chat-width);
}

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

.chat-title {
  font-weight: 600;
  font-size: 0.8125rem;
  color: var(--text);
  letter-spacing: 0.01em;
}

.chat-header-actions { display: flex; gap: 4px; }

.chat-header-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  line-height: 1;
}

.chat-header-btn:hover { border-color: var(--border-hover); color: var(--text); }

/* Messages area */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-welcome {
  color: var(--text-muted);
  font-size: 0.8125rem;
  padding: 20px 0;
  text-align: center;
}

.chat-welcome p { margin-bottom: 16px; }

.chat-suggestions {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chat-suggestions button {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  padding: 8px 12px;
  font-family: var(--font);
  font-size: 0.75rem;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s, color 0.15s;
}

.chat-suggestions button:hover { border-color: var(--border-hover); color: var(--text); }

/* Message bubbles */
.chat-msg { display: flex; flex-direction: column; gap: 4px; }
.chat-msg.user { align-items: flex-end; }
.chat-msg.assistant { align-items: flex-start; }
.chat-msg.system { align-items: center; }

.chat-msg.user .chat-msg-content {
  background: var(--red);
  color: #fff;
  padding: 8px 14px;
  border-radius: 12px 12px 2px 12px;
  max-width: 90%;
  font-size: 0.8125rem;
  line-height: 1.5;
  word-break: break-word;
}

.chat-msg.assistant .chat-msg-content {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 2px 12px 12px 12px;
  max-width: 100%;
  font-size: 0.8125rem;
  line-height: 1.6;
  word-break: break-word;
}

.chat-msg.assistant .chat-msg-content:empty { display: none; }

.chat-msg.assistant .chat-msg-content p { margin: 0 0 8px 0; }
.chat-msg.assistant .chat-msg-content p:last-child { margin-bottom: 0; }
.chat-msg.assistant .chat-msg-content h3 { font-size: 0.875rem; margin: 12px 0 6px; color: var(--text); }
.chat-msg.assistant .chat-msg-content h4 { font-size: 0.8125rem; margin: 10px 0 4px; color: var(--text); }
.chat-msg.assistant .chat-msg-content ul { margin: 4px 0; padding-left: 18px; }
.chat-msg.assistant .chat-msg-content li { margin: 2px 0; }
.chat-msg.assistant .chat-msg-content strong { color: var(--text); }

/* Inline code & code blocks */
.chat-inline-code {
  background: var(--elevated);
  padding: 1px 5px;
  border-radius: 2px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.75rem;
}

.chat-code {
  background: var(--elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin: 8px 0;
  overflow-x: auto;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.75rem;
  line-height: 1.5;
}

.chat-code code { background: none; padding: 0; }

/* Tables in chat */
.chat-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 0.75rem;
}

.chat-table td, .chat-table th {
  padding: 4px 8px;
  border: 1px solid var(--border);
  text-align: left;
}

.chat-table th { background: var(--elevated); font-weight: 500; }

/* Document links */
.doc-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--red-muted);
  color: var(--text);
  padding: 2px 8px;
  border-radius: 3px;
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 500;
  border: 1px solid rgba(227,18,11,0.25);
  transition: background 0.15s;
  margin: 2px 0;
}

.doc-link:hover { background: rgba(227,18,11,0.25); }
.doc-link-icon { font-size: 0.875rem; }

/* Document sets / grids */
.docset {
  margin: 10px 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.docset-title {
  padding: 8px 12px;
  background: var(--elevated);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text);
  border-bottom: 1px solid var(--border);
}

.docset-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  padding: 2px;
}

.docset-grid img {
  width: 100%;
  height: 70px;
  object-fit: cover;
  cursor: pointer;
  transition: opacity 0.15s;
}

.docset-grid img:hover { opacity: 0.7; }
.docset-more { display: flex; align-items: center; justify-content: center; font-size: 0.6875rem; color: var(--text-muted); }

/* Tool calls */
.chat-tool {
  margin: 6px 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: 0.75rem;
}

.chat-tool-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--elevated);
  cursor: pointer;
  user-select: none;
}

.chat-tool-header:hover { background: var(--surface-alt); }

.chat-tool-icon { font-size: 0.875rem; }

.chat-tool-name {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-weight: 500;
  color: var(--text-secondary);
  flex: 1;
}

.chat-tool-status { color: var(--text-muted); font-size: 0.8125rem; }
.chat-tool-status.spinning { animation: spin 1s linear infinite; display: inline-block; }
.chat-tool-status.done { color: #4caf50; }

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

.chat-tool-expand {
  color: var(--text-muted);
  font-size: 0.625rem;
  transition: transform 0.15s;
}

.chat-tool.expanded .chat-tool-expand { transform: rotate(180deg); }

.chat-tool-body {
  display: none;
  border-top: 1px solid var(--border);
}

.chat-tool.expanded .chat-tool-body { display: block; }

/* Research file write notice */
.chat-tool.research-write { border-color: rgba(76,175,80,0.3); }
.chat-tool.research-write .chat-tool-header { background: rgba(76,175,80,0.08); }
.chat-tool.research-write .chat-tool-name { color: #4caf50; }
.chat-tool.research-write .chat-tool-body { display: block; }

.research-write-notice {
  padding: 8px 12px;
  font-size: 0.75rem;
  color: #4caf50;
  display: flex;
  align-items: center;
  gap: 6px;
}

.research-write-icon { font-size: 1rem; }

.chat-tool-input {
  border-bottom: 1px solid var(--border);
}

.chat-tool-input pre, .chat-tool-output pre {
  margin: 0;
  padding: 8px 10px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.6875rem;
  line-height: 1.5;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--text-secondary);
}

.chat-tool-input pre { color: var(--text-muted); }
.chat-tool-output pre.error { color: #ef5350; }
.chat-tool-empty { padding: 8px 10px; color: var(--text-muted); font-style: italic; }
.chat-tool-count { padding: 4px 10px; color: var(--text-muted); font-size: 0.6875rem; }

/* Result tables in tool output */
.chat-result-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.6875rem;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

.chat-result-table th, .chat-result-table td {
  padding: 3px 8px;
  border: 1px solid var(--border);
  text-align: left;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-result-table th {
  background: var(--elevated);
  font-weight: 500;
  color: var(--text-secondary);
  position: sticky;
  top: 0;
}

.chat-result-table td { color: var(--text-muted); }

.chat-tool-output { max-height: 300px; overflow: auto; }

.doc-link-inline {
  color: var(--red);
  text-decoration: none;
  font-weight: 500;
}

.doc-link-inline:hover { text-decoration: underline; }

.show-in-viewer-btn {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 8px;
  background: var(--red-muted);
  border: 1px solid rgba(227,18,11,0.25);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.625rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}

.show-in-viewer-btn:hover { background: rgba(227,18,11,0.25); }

/* Chat error */
.chat-error {
  background: rgba(239,83,80,0.1);
  border: 1px solid rgba(239,83,80,0.3);
  color: #ef5350;
  padding: 8px 12px;
  border-radius: var(--radius);
  margin-top: 8px;
  font-size: 0.75rem;
}

/* Session files bar */
.chat-files {
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.chat-files-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  cursor: pointer;
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.chat-files-header:hover { color: var(--text); }
.chat-files-toggle { font-size: 0.625rem; }

.chat-files-list {
  max-height: 120px;
  overflow-y: auto;
  padding: 0 8px 8px;
}

.chat-files-list.collapsed { display: none; }

.chat-file {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.chat-file:hover { background: var(--elevated); color: var(--text); }
.chat-file-icon { font-size: 0.875rem; }
.chat-file-name { flex: 1; }
.chat-file-size { color: var(--text-muted); font-size: 0.625rem; font-family: 'SF Mono', monospace; }

.chat-file-preview {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  max-width: 100%;
}

.chat-file-preview-header {
  padding: 6px 10px;
  background: var(--elevated);
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
}

.chat-file-preview pre {
  margin: 0;
  padding: 8px 10px;
  font-size: 0.6875rem;
  max-height: 200px;
  overflow: auto;
  color: var(--text-muted);
}

/* Input area */
.chat-input-area {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
  align-items: flex-end;
}

.chat-input-area textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--elevated);
  color: var(--text);
  padding: 8px 12px;
  font-family: var(--font);
  font-size: 0.8125rem;
  line-height: 1.5;
  max-height: 120px;
  outline: none;
}

.chat-input-area textarea:focus { border-color: var(--border-hover); }

.chat-input-area textarea::placeholder { color: var(--text-muted); }

#chatSendBtn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius);
  background: var(--red);
  color: #fff;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}

#chatSendBtn:hover { background: #c50f09; }
