:root {
  --bg: #ffffff;
  --bg-subtle: #f7f8fa;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --accent: #f6821f;
  --accent-ink: #ffffff;
  --line: #e5e7eb;
  --danger: #dc2626;
  --ok: #15803d;
  --font: "IBM Plex Sans", "Segoe UI", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}
* { box-sizing: border-box; }
/* hidden must always win over explicit display (e.g. .modal-backdrop is flex) */
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--ink); text-decoration-color: #cbd5e1; text-underline-offset: 2px; }
a:hover { color: var(--accent); text-decoration-color: var(--accent); }
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem 1.5rem;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand {
  font-weight: 650;
  letter-spacing: -0.01em;
  font-size: 1.05rem;
  color: var(--ink);
  text-decoration: none;
}
.brand span { color: var(--accent); font-weight: 500; }
nav { display: flex; gap: 1.1rem; align-items: center; font-size: 0.88rem; }
nav a { color: var(--muted); text-decoration: none; }
nav a:hover { color: var(--ink); }
nav a.active { color: var(--ink); font-weight: 550; }
main {
  max-width: 720px;
  margin: 0 auto;
  padding: 2.5rem 1.25rem 4rem;
}
h1 { font-size: 1.5rem; font-weight: 650; letter-spacing: -0.02em; margin: 0 0 0.35rem; }
.lead { color: var(--muted); margin: 0 0 1.5rem; font-size: 0.95rem; }
label {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
input, textarea, select, button {
  font: inherit;
  color: var(--ink);
}
input, textarea, select {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.65rem 0.8rem;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(246, 130, 31, 0.15);
}
textarea { min-height: 140px; resize: vertical; }
.row { display: flex; gap: 0.75rem; align-items: end; margin-bottom: 1rem; }
.row > * { flex: 1; }
.row .grow { flex: 3; }
button, .btn {
  appearance: none;
  border: 1px solid var(--ink);
  background: var(--ink);
  color: #ffffff;
  font-weight: 550;
  font-size: 0.9rem;
  padding: 0.65rem 1.1rem;
  border-radius: 6px;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  transition: background 0.15s;
}
button:hover, .btn:hover { background: #1e293b; }
button.secondary {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink);
}
button.secondary:hover { border-color: var(--ink); background: transparent; }
button:disabled { opacity: 0.5; cursor: wait; }
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1.25rem 1.4rem;
  margin-top: 1.25rem;
}
.card h2 { margin: 0 0 0.75rem; font-size: 0.95rem; font-weight: 650; letter-spacing: -0.01em; }
.answer { white-space: pre-wrap; }
.meta { color: var(--muted); font-size: 0.82rem; margin-top: 0.75rem; }
.citations { list-style: none; padding: 0; margin: 0.75rem 0 0; }
.citations li {
  padding: 0.45rem 0;
  border-top: 1px solid var(--line);
  font-size: 0.9rem;
}
.err { color: var(--danger); margin-top: 0.75rem; font-size: 0.9rem; }
.experts { margin: 0; padding-left: 1.1rem; }
.experts li { margin: 0.35rem 0; }
.user-pill { color: var(--muted); font-size: 0.82rem; }

/* Docs + Dashboard */
main.wide { max-width: 880px; }
code {
  font-family: var(--mono);
  font-size: 0.85em;
  background: var(--bg-subtle);
  border: 1px solid var(--line);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}
pre {
  background: var(--bg-subtle);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.9rem 1rem;
  overflow-x: auto;
}
pre code { background: none; border: none; padding: 0; font-size: 0.82rem; }
.card h3 {
  margin: 1.25rem 0 0.4rem;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 0.55rem 0.6rem; border-top: 1px solid var(--line); vertical-align: top; }
thead th {
  border-top: none;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.rules { padding-left: 1.2rem; }
.rules li { margin: 0.4rem 0; }
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem;
  margin-top: 1.25rem;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem 1.1rem;
}
.stat .num { font-size: 1.45rem; font-weight: 650; letter-spacing: -0.02em; }
.stat .lbl {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 0.2rem;
}
.badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.15em 0.55em;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.badge.ok { color: var(--ok); border-color: #bbf7d0; background: #f0fdf4; }
.badge.off { color: var(--danger); border-color: #fecaca; background: #fef2f2; }
.token-reveal {
  font-family: var(--mono);
  font-size: 0.85rem;
  word-break: break-all;
  background: var(--bg-subtle);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 0.9rem 1rem;
  margin-top: 0.75rem;
}
button.danger {
  background: transparent;
  border: 1px solid #fecaca;
  color: var(--danger);
  padding: 0.35rem 0.7rem;
  font-size: 0.8rem;
}
button.danger:hover { background: #fef2f2; }

/* Admin console */
main.admin { max-width: 1080px; }
.tabs {
  display: flex;
  gap: 0.15rem;
  border-bottom: 1px solid var(--line);
  margin: 1.5rem 0 0;
  overflow-x: auto;
}
.tabs button {
  appearance: none;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 550;
  padding: 0.6rem 0.85rem;
  cursor: pointer;
  border-radius: 0;
  white-space: nowrap;
}
.tabs button:hover { color: var(--ink); background: none; }
.tabs button.active { color: var(--ink); border-bottom-color: var(--accent); }
.tabs .tab-count {
  display: inline-block;
  min-width: 1.2em;
  font-size: 0.72rem;
  font-weight: 650;
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  padding: 0 0.35em;
  margin-left: 0.3em;
}
section.tab-panel { display: none; padding-top: 1.25rem; }
section.tab-panel.active { display: block; }
.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 0.75rem;
  margin-top: 1.25rem;
}
.grid-2 .card { margin-top: 0; }
.filters {
  display: flex;
  gap: 0.6rem;
  align-items: end;
  flex-wrap: wrap;
  margin-top: 1.25rem;
}
.filters > * { flex: 1; min-width: 140px; }
.filters > .shrink { flex: 0 0 auto; }
.filters label { margin-bottom: 0.25rem; }
.filters input, .filters select { padding: 0.5rem 0.7rem; font-size: 0.88rem; }
button.small, .btn.small {
  padding: 0.4rem 0.75rem;
  font-size: 0.82rem;
}
.pager { display: flex; gap: 0.75rem; align-items: center; justify-content: flex-end; margin-top: 0.75rem; }
.pager .meta { margin: 0; }

/* Bubbles */
.bubble-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-subtle);
  margin-top: 1.25rem;
  overflow: hidden;
}
.bubble-wrap svg { display: block; width: 100%; height: auto; }
.bubble { cursor: pointer; transition: opacity 0.15s; }
.bubble:hover { opacity: 0.85; }
.bubble text { pointer-events: none; }
.bubble-legend { display: flex; gap: 1rem; flex-wrap: wrap; padding: 0.6rem 0.9rem; border-top: 1px solid var(--line); background: var(--panel); }
.bubble-legend span { display: inline-flex; align-items: center; gap: 0.35rem; font-size: 0.78rem; color: var(--muted); }
.src-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  z-index: 50;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 4vh 1rem;
}
.modal {
  background: var(--panel);
  border-radius: 10px;
  border: 1px solid var(--line);
  max-width: 760px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  padding: 1.5rem 1.75rem;
}
.modal h2 { margin: 0 0 0.25rem; font-size: 1.1rem; }
.modal .modal-meta { color: var(--muted); font-size: 0.8rem; margin-bottom: 1rem; }
.modal pre.item-content { white-space: pre-wrap; font-family: var(--font); font-size: 0.88rem; max-height: 50vh; overflow-y: auto; }
.modal .modal-close { float: right; }

/* Toast */
.toast {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  background: var(--ink);
  color: #fff;
  font-size: 0.88rem;
  padding: 0.7rem 1.1rem;
  border-radius: 8px;
  z-index: 100;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  max-width: 420px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.err-toast { background: var(--danger); }

/* Activity mini bar chart */
.bars { display: flex; align-items: flex-end; gap: 4px; height: 64px; margin-top: 0.75rem; }
.bars .bar { flex: 1; background: var(--accent); opacity: 0.75; border-radius: 3px 3px 0 0; min-height: 2px; }
.bars .bar:hover { opacity: 1; }
.bar-labels { display: flex; gap: 4px; margin-top: 0.3rem; }
.bar-labels span { flex: 1; text-align: center; font-size: 0.66rem; color: var(--muted); }

/* Connections */
.conn-group { margin-top: 1.25rem; }
.conn-head { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; }
.conn-head h2 { margin: 0; }
.setting-row {
  display: grid;
  grid-template-columns: 180px 1fr auto;
  gap: 0.6rem;
  align-items: center;
  padding: 0.55rem 0;
  border-top: 1px solid var(--line);
}
.setting-row:first-of-type { border-top: none; }
.setting-row .setting-label { font-size: 0.88rem; }
.setting-row .setting-label .src-badge { margin-left: 0.4rem; }
.setting-row input { padding: 0.5rem 0.7rem; font-size: 0.85rem; font-family: var(--mono); }
.setting-row textarea { min-height: 90px; font-family: var(--mono); font-size: 0.8rem; }
.setting-row .setting-actions { display: flex; gap: 0.4rem; }
.test-result { font-size: 0.82rem; margin-left: 0.5rem; }
.test-result.ok { color: var(--ok); }
.test-result.fail { color: var(--danger); }
.banner {
  border: 1px solid #fde68a;
  background: #fffbeb;
  color: #92400e;
  border-radius: 8px;
  padding: 0.8rem 1rem;
  font-size: 0.88rem;
  margin-top: 1.25rem;
}
.badge.warn { color: #92400e; border-color: #fde68a; background: #fffbeb; }
.badge.info { color: #1d4ed8; border-color: #bfdbfe; background: #eff6ff; }

/* Inbox cards */
.inbox-card { display: flex; gap: 1rem; align-items: flex-start; }
.inbox-card .inbox-body { flex: 1; min-width: 0; }
.inbox-card .inbox-actions { display: flex; gap: 0.4rem; flex-shrink: 0; align-items: center; }
.inbox-card select { width: auto; padding: 0.4rem 0.6rem; font-size: 0.82rem; }

@media (max-width: 600px) {
  .row { flex-direction: column; align-items: stretch; }
  .setting-row { grid-template-columns: 1fr; }
  .inbox-card { flex-direction: column; }
}
