:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface-2: #243044;
  --border: #334155;
  --text: #e8eef7;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --secondary: #10b981;
  --secondary-hover: #059669;
  --error-bg: #3f1d1d;
  --error-text: #fecaca;
  --radius: 12px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: radial-gradient(ellipse at top, #1e293b 0%, var(--bg) 55%);
  color: var(--text);
  min-height: 100vh;
}

.app {
  max-width: 960px;
  margin: 0 auto;
  padding: 2rem 1.25rem 3rem;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.header h1 {
  margin: 0 0 0.25rem;
  font-size: 1.75rem;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.25rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

.card h2 {
  margin: 0 0 1rem;
  font-size: 1.15rem;
}

.card h3 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
  color: var(--muted);
  font-weight: 600;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}

@media (max-width: 700px) {
  .form-grid { grid-template-columns: 1fr; }
  .header { flex-direction: column; }
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.9rem;
  color: var(--muted);
}

input[type="file"],
input[type="text"],
input[type="password"],
input[type="search"],
select {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
  font: inherit;
}

.toggles {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  margin: 0 0 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.toggles legend {
  padding: 0 0.35rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.toggles label {
  flex-direction: row;
  align-items: center;
  gap: 0.4rem;
  color: var(--text);
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1.1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  color: white;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary { background: var(--primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn-secondary { background: var(--secondary); }
.btn-secondary:hover:not(:disabled) { background: var(--secondary-hover); }

.btn-danger { background: var(--danger); }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }

.progress-panel {
  margin-top: 1rem;
  padding: 0.75rem;
  background: var(--surface-2);
  border-radius: 8px;
}

.progress-bar {
  height: 10px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 0.5rem;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), #60a5fa);
  transition: width 0.3s ease;
}

.session-status {
  margin: 0 0 1.1rem;
  padding: 0.75rem 0.9rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--secondary);
  border-radius: 8px;
  font-size: 0.9rem;
  line-height: 1.5;
}

.status-done {
  color: #4ade80;
  font-weight: 600;
}

.status-done::before {
  content: "\2713\00a0";
}

.status-next {
  color: var(--muted);
  margin-top: 0.4rem;
}

/* ============ Analysis page tiles + streamed report ============ */
.page-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.75rem;
  margin-top: 1rem;
}

@media (max-width: 720px) {
  .page-grid { grid-template-columns: repeat(3, 1fr); }
}

.page-tile {
  margin: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.4rem;
  text-align: center;
}

.page-tile img {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 4px;
  background: #fff;
}

.page-tile figcaption {
  margin-top: 0.35rem;
  font-size: 0.72rem;
  line-height: 1.35;
}

.page-name { display: block; color: var(--text); font-weight: 600; }

.page-status { display: block; color: var(--muted); }
.page-status.busy { color: #fbbf24; }
.page-status.step { color: #7dd3fc; }
.page-status.ok { color: #4ade80; }
.page-status.warn { color: #fb923c; }

.report-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.report-head h2 { margin: 0; }

.report-stream {
  margin-top: 0.75rem;
  max-height: 32rem;
  overflow-y: auto;
  padding: 1rem 1.25rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  line-height: 1.6;
}

.report-stream h2,
.report-stream h3,
.report-stream h4,
.report-stream h5,
.report-stream h6 {
  margin: 1.1rem 0 0.4rem;
  line-height: 1.3;
}

.report-stream h2 { font-size: 1.2rem; }
.report-stream h3 { font-size: 1.05rem; }
.report-stream h4,
.report-stream h5,
.report-stream h6 { font-size: 0.95rem; color: var(--muted); }

.report-stream p { margin: 0 0 0.7rem; }
.report-stream ul,
.report-stream ol { margin: 0 0 0.7rem 1.2rem; padding: 0; }
.report-stream li { margin-bottom: 0.3rem; }

.report-stream table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.5rem 0 1rem;
  font-size: 0.88rem;
}

.report-stream th,
.report-stream td {
  border: 1px solid var(--border);
  padding: 0.4rem 0.55rem;
  text-align: left;
  vertical-align: top;
}

.report-stream th { background: var(--surface); font-weight: 600; }

.report-stream code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85em;
}

.file-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.file-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
}

.file-list a {
  color: #93c5fd;
  text-decoration: none;
  font-weight: 600;
}

.file-list a:hover { text-decoration: underline; }

.checkbox-list {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  max-height: 180px;
  overflow: auto;
  padding: 0.5rem;
  background: var(--surface-2);
  border-radius: 8px;
  border: 1px solid var(--border);
}

.checkbox-list label {
  flex-direction: row;
  color: var(--text);
}

.meta-row {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.muted { color: var(--muted); font-size: 0.9rem; }

.error {
  margin-top: 0.75rem;
  background: var(--error-bg);
  color: var(--error-text);
  padding: 0.75rem 1rem;
  border-radius: 8px;
  border: 1px solid #7f1d1d;
}

.hidden { display: none !important; }

.gate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.gate-card {
  max-width: 28rem;
  width: 100%;
  margin: 0;
}

.gate-card h1 {
  margin: 0 0 0.35rem;
  font-size: 1.5rem;
  letter-spacing: -0.02em;
}

.gate-card .subtitle {
  margin: 0 0 1.25rem;
}

.gate-card .btn {
  margin-top: 1rem;
  width: 100%;
}

.footer {
  margin-top: 1.5rem;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--muted);
}

.badge-warn {
  border-color: #b45309;
  color: #fbbf24;
}

.badge-ok {
  border-color: #15803d;
  color: #4ade80;
}

/* Admin console tabs — segmented control */
.tabs {
  display: flex;
  gap: 0.4rem;
  margin: 1.25rem 0;
  padding: 0.4rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  flex-wrap: wrap;
}

.tab {
  flex: 1 1 auto;
  padding: 0.7rem 1.1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.tab:hover {
  background: var(--border);
  color: var(--text);
}

.tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.code-area {
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.5;
}

/* ================= Knowledge base console ================= */
.app-wide { max-width: 1240px; }

.statusbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 1rem;
  margin-bottom: 1rem;
}

.badge-row { display: flex; gap: 0.4rem; flex-wrap: wrap; }

.statusbar-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.statusbar-actions .btn { padding: 0.45rem 0.85rem; font-size: 0.9rem; }

.inline-check {
  flex-direction: row;
  align-items: center;
  gap: 0.35rem;
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
}

.kb-layout {
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
  gap: 1.25rem;
  align-items: start;
}

@media (max-width: 900px) {
  .kb-layout { grid-template-columns: 1fr; }
}

.kb-sidebar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  position: sticky;
  top: 1rem;
}

.kb-sidebar h3 {
  margin: 0 0 0.6rem;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.scope-list { list-style: none; padding: 0; margin: 0 0 0.85rem; }

.scope-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.55rem 0.6rem;
  margin-bottom: 0.3rem;
  border: 1px solid transparent;
  border-radius: 8px;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.scope-item:hover { background: var(--surface-2); }

.scope-item.selected {
  background: var(--surface-2);
  border-color: var(--primary);
}

.scope-item-body { flex: 1; min-width: 0; }

.scope-item-name {
  display: block;
  font-weight: 600;
  font-size: 0.92rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.scope-item-meta { display: block; color: var(--muted); font-size: 0.75rem; }

.scope-item.inactive .scope-item-name { color: var(--muted); }

.icon-btn {
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 0.15rem 0.3rem;
  border-radius: 6px;
  line-height: 1;
}

.icon-btn:hover { background: var(--danger); color: #fff; }

.scope-create { display: flex; gap: 0.4rem; }
.scope-create input { flex: 1; min-width: 0; }
.scope-create .btn { padding: 0.5rem 0.7rem; font-size: 0.85rem; }

.kb-root {
  margin-top: 0.85rem;
  font-size: 0.72rem;
  word-break: break-all;
  border-top: 1px solid var(--border);
  padding-top: 0.6rem;
}

.kb-main { min-width: 0; }

.dropzone {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  flex-wrap: wrap;
  padding: 1rem 1.1rem;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  transition: border-color 0.15s, background 0.15s;
}

.dropzone.dragover {
  border-color: var(--primary);
  background: rgba(59, 130, 246, 0.12);
}

/* A label styled as a button opens the file picker without any JS. */
.btn-label {
  display: inline-block;
  white-space: nowrap;
}

.dropzone-text { min-width: 0; font-size: 0.9rem; }
.dropzone-hint { color: var(--muted); font-size: 0.8rem; margin-top: 0.15rem; }

.table-wrap { overflow-x: auto; }

.kb-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.kb-table th {
  text-align: left;
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.kb-table td {
  padding: 0.6rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.kb-table tbody tr:hover { background: var(--surface-2); }

.kb-table .col-name { font-weight: 600; color: #93c5fd; word-break: break-word; }
.kb-table .col-num { text-align: right; white-space: nowrap; }
.kb-table .row-actions { display: flex; gap: 0.35rem; justify-content: flex-end; }
.kb-table .row-actions .btn { padding: 0.3rem 0.6rem; font-size: 0.8rem; }
.kb-table select { padding: 0.3rem 0.4rem; font-size: 0.8rem; }

.search-row {
  display: flex;
  gap: 0.5rem;
  margin: 0.85rem 0 0.6rem;
  flex-wrap: wrap;
}

.search-row input { flex: 1; min-width: 200px; }

#retrieval-console summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 1.05rem;
}

.hit {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  margin-bottom: 0.5rem;
  background: var(--surface-2);
}

.hit-head {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: baseline;
  margin-bottom: 0.35rem;
  flex-wrap: wrap;
}

.hit-score {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-weight: 700;
  color: #4ade80;
}

.hit-text {
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre-wrap;
  max-height: 7.5rem;
  overflow: auto;
  margin: 0;
}
