:root {
  --bg: #0b0f14;
  --fg: #e6edf3;
  --mute: #7d8692;
  --accent: #8fd1ff;
  --card: #141a22;
  --border: #1f2a37;
  --warn: #f0b72f;
  --bubble-border: rgba(255, 255, 255, 0.1);
  --pulse: #7fffb0;
}

* {
  box-sizing: border-box;
}
body,
html {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font:
    14px/1.4 -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    system-ui,
    sans-serif;
}

.top {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}
.top .title {
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}
.top .ns,
.top .deployment {
  color: var(--mute);
  font-weight: 400;
}
.top .ns-picker {
  color: var(--mute);
  font-weight: 400;
  font-size: 0.9em;
}
.top .ns-picker select {
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.1rem 0.4rem;
  font: inherit;
}
.top .budget-badge {
  color: var(--mute);
  font-variant-numeric: tabular-nums;
  font-size: 0.85em;
  padding: 0.15rem 0.5rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 4px;
}
.top .budget-badge.near-empty {
  color: #b85c00;
  border-color: #b85c00;
}
.top .state {
  color: var(--mute);
  margin-left: auto;
}
.top .user {
  margin-left: 1rem;
}
.top button {
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  padding: 0.3rem 0.7rem;
  border-radius: 6px;
  cursor: pointer;
}

.intent {
  padding: 0.5rem 1rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  max-height: 3rem;
  overflow: hidden;
  position: relative;
}
.intent.expanded {
  max-height: none;
}
.intent h2 {
  margin: 0 0 0.2rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--mute);
  display: inline-block;
  margin-right: 0.6rem;
}
.intent #intent-md {
  display: inline;
  font-size: 12px;
  color: var(--fg);
}
.intent #intent-md p {
  display: inline;
  margin: 0;
}
.intent.expanded #intent-md,
.intent.expanded #intent-md p {
  display: block;
}
.intent.expanded h2 {
  display: block;
  margin-bottom: 0.4rem;
}
#intent-toggle {
  position: absolute;
  top: 0.4rem;
  right: 0.8rem;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--accent);
  padding: 0.1rem 0.5rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.75rem;
}

.columns {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 1px;
  background: var(--border);
  height: calc(100vh - 5rem);
}
.columns .side {
  background: var(--bg);
  padding: 0.8rem;
  overflow: auto;
}
.columns .center {
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.side h3 {
  margin: 0 0 0.4rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--mute);
}
.side table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
.side table th,
.side table td {
  padding: 4px 6px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
#library td:first-child {
  max-width: 130px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#library td:nth-child(2),
#library td:nth-child(3) {
  color: var(--mute);
  white-space: nowrap;
  font-size: 11px;
}
.side table a {
  color: var(--accent);
}

#output td:first-child {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#output td:nth-child(2),
#output td:nth-child(3) {
  color: var(--mute);
  white-space: nowrap;
  font-size: 11px;
}
.side .empty {
  color: var(--mute);
  font-size: 11px;
  font-style: italic;
}

#articles {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 0.8rem;
}

#graph {
  width: 100%;
  height: 380px;
  margin-bottom: 0.8rem;
  position: relative;
  overflow: hidden;
}
#graph svg { display: block; }
#graph circle:hover { stroke: var(--accent); stroke-width: 1.5px; }
.graph-clear-focus {
  position: absolute;
  top: 4px;
  right: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--accent);
  padding: 2px 6px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 10px;
}
.article-row {
  text-align: left;
  background: transparent;
  border: 1px solid var(--border);
  padding: 6px 8px;
  font: inherit;
  color: inherit;
  cursor: pointer;
  border-radius: 3px;
}
.article-row:hover {
  border-color: var(--accent);
}
.article-meta {
  color: var(--mute);
  font-size: 11px;
  margin-top: 2px;
}
.article-summary {
  font-size: 12px;
  margin-top: 4px;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
}
.toolbar .ticker {
  margin-left: auto;
  color: var(--mute);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
}
.toolbar #refresh {
  font-size: 12px;
}
.toolbar button {
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.6rem;
  cursor: pointer;
}
.toolbar button.on {
  border-color: var(--accent);
  color: var(--accent);
}
.toolbar select {
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  padding: 0.25rem;
  border-radius: 6px;
}
.toolbar .lookback {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 12px;
  color: var(--mute);
}

/* Chat-style stacked feed: time | sender | body. Persistent (no auto-remove);
   auto-scrolls to bottom only if the user is already at the bottom. */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 0.4rem 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.chat-header {
  display: grid;
  grid-template-columns: 80px 90px 90px 70px 1fr;
  gap: 0.7rem;
  padding: 0.4rem 1rem;
  border-bottom: 1px solid var(--border);
  font-size: 10px;
  color: var(--mute);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.msg {
  display: grid;
  grid-template-columns: 80px 90px 90px 70px 1fr;
  gap: 0.7rem;
  padding: 0.35rem 1rem;
  align-items: baseline;
  cursor: pointer;
  border-left: 3px solid transparent;
  font-size: 13px;
  line-height: 1.45;
}
/* Fade + slide-in for newly-arriving messages only (.is-fresh is added
   by timeline.mjs::render in live mode; replay-load omits it so the
   bulk backlog doesn't all flash at once). */
@keyframes msg-fade-in {
  from {
    opacity: 0;
    transform: translateY(-4px);
    border-left-color: var(--accent);
  }
  60% {
    border-left-color: var(--accent);
  }
  to {
    opacity: 1;
    transform: translateY(0);
    border-left-color: transparent;
  }
}
.msg.is-fresh {
  animation: msg-fade-in 360ms ease-out;
}
.msg:hover {
  background: var(--card);
}
.msg .when {
  color: var(--mute);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  white-space: nowrap;
}
.msg .from,
.msg .to {
  font-weight: 600;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg .to::before {
  content: "→ ";
  color: var(--mute);
  font-weight: 400;
  margin-right: 0.15rem;
}
.msg .activity {
  display: inline-block;
  font-weight: 400;
  font-size: 10px;
  color: var(--mute);
  padding: 0 0.4rem;
  border: 1px solid var(--border);
  border-radius: 3px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  align-self: center;
  text-align: center;
  white-space: nowrap;
}
.msg .activity.activity-document-fetch {
  color: var(--warn);
  border-color: var(--warn);
}
.msg .activity.activity-system-note {
  color: var(--mute);
  border-color: var(--mute);
  border-style: dashed;
}
.msg.kind-system-note {
  border-left-color: var(--mute);
}
.msg.kind-system-note .body {
  color: var(--mute);
  font-style: italic;
}
.msg .body {
  color: var(--fg);
  word-break: break-word;
}
.msg.kind-document-fetch {
  border-left-color: var(--warn);
}
.msg.kind-document-fetch .body {
  color: #c8d0db;
}

.ask {
  padding: 0.6rem 1rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.ask form {
  display: flex;
  gap: 0.5rem;
}
.ask input {
  flex: 1;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
}
.ask button {
  background: var(--accent);
  color: #0b0f14;
  border: 0;
  padding: 0.4rem 0.9rem;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
}
#ask-output {
  margin-top: 0.6rem;
  padding: 0.6rem 0.8rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  max-height: 40vh;
  overflow: auto;
}
#ask-output article {
  white-space: pre-wrap;
  margin: 0.5rem 0;
}
.ask-meta {
  color: var(--mute);
  font-size: 12px;
}
.ask-citations {
  margin: 0.4rem 0;
  padding-left: 1.2rem;
}
.ask-citations li {
  font-size: 12px;
}

#tagcloud {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}
#tagcloud span {
  padding: 0.15rem 0.4rem;
  border-radius: 999px;
  background: var(--card);
  border: 1px solid var(--border);
  cursor: pointer;
}

#ontology .onto-counts {
  font-size: 12px;
  color: var(--mute);
  margin-bottom: 0.4rem;
}
#ontology details {
  font-size: 12px;
}
#ontology details summary {
  cursor: pointer;
  color: var(--accent);
  font-size: 11px;
  padding: 0.2rem 0;
}
#ontology h4 {
  margin: 0.6rem 0 0.2rem;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--mute);
}
#ontology ul {
  list-style: none;
  padding-left: 0.8rem;
  margin: 0.2rem 0;
}
#ontology li {
  font-size: 12px;
  color: var(--fg);
  line-height: 1.5;
}
#ontology li.pulse {
  animation: pulse 4s ease-in-out;
}
@keyframes pulse {
  0%,
  100% {
    background: transparent;
  }
  40% {
    background: rgba(127, 255, 176, 0.2);
  }
}

dialog {
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  max-width: 80vw;
  width: 80vw;
  max-height: 80vh;
  padding: 1rem 1.2rem;
}
dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
}
dialog h3 {
  margin: 0 0 0.6rem;
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
}
.modal-meta {
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--mute);
}
.modal-meta-row {
  display: grid;
  grid-template-columns: 70px 1fr;
  gap: 0.6rem;
  padding: 1px 0;
}
.modal-meta-row .k {
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 10px;
  padding-top: 1px;
}
.modal-meta-row .v {
  word-break: break-all;
  color: var(--fg);
}
.modal-meta-row a {
  color: var(--accent);
  word-break: break-all;
}
dialog hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 0.7rem 0;
}
.modal-body-text {
  white-space: pre-wrap;
  word-break: break-word;
  font: 13px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--fg);
  max-height: 55vh;
  overflow-y: auto;
  margin: 0;
  padding: 0.4rem 0;
}
dialog form button {
  margin-top: 0.8rem;
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.8rem;
  cursor: pointer;
}

/* Memory graph — inline panel "open fullscreen" button + lightbox dialog. */
.graph-expand {
  position: absolute;
  top: 4px;
  right: 4px;
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.15rem 0.45rem;
  font-size: 0.78em;
  cursor: pointer;
  opacity: 0.6;
}
.graph-expand:hover {
  opacity: 1;
}
.graph-label {
  pointer-events: none;
  user-select: none;
}

/* Zoom + pan toolbar — bottom-left of the graph canvas (inline panel)
 * and bottom-left of the lightbox body. Buttons are deliberately small;
 * d3.zoom on the SVG handles wheel + pinch + drag-to-pan natively, so
 * these are a discoverable supplement, not the primary input. */
.graph-controls {
  position: absolute;
  bottom: 6px;
  left: 6px;
  display: grid;
  grid-template-columns: repeat(3, 26px);
  grid-template-rows: repeat(3, 26px);
  gap: 2px;
  z-index: 2;
  background: rgba(8, 10, 14, 0.55);
  padding: 3px;
  border-radius: 4px;
  border: 1px solid var(--border);
}
.graph-controls .gc-btn {
  width: 26px;
  height: 26px;
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0;
  font-size: 0.85em;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.graph-controls .gc-btn:hover {
  background: var(--border);
}
.graph-controls .gc-btn[data-act="in"]    { grid-column: 1; grid-row: 1; }
.graph-controls .gc-btn[data-act="up"]    { grid-column: 2; grid-row: 1; }
.graph-controls .gc-btn[data-act="fit"]   { grid-column: 3; grid-row: 1; }
.graph-controls .gc-btn[data-act="left"]  { grid-column: 1; grid-row: 2; }
.graph-controls .gc-btn[data-act="right"] { grid-column: 3; grid-row: 2; }
.graph-controls .gc-btn[data-act="out"]   { grid-column: 1; grid-row: 3; }
.graph-controls .gc-btn[data-act="down"]  { grid-column: 2; grid-row: 3; }

#graph-lightbox {
  width: 92vw;
  height: 88vh;
  max-width: none;
  max-height: none;
  padding: 0;
  background: rgba(8, 10, 14, 0.97);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
#graph-lightbox::backdrop {
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(2px);
}
.graph-lightbox-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.9rem;
  border-bottom: 1px solid var(--border);
  background: var(--card);
}
.graph-lightbox-title {
  font-weight: 600;
  color: var(--mute);
}
.graph-lightbox-close {
  background: transparent;
  color: var(--fg);
  border: none;
  font-size: 1.4em;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.4rem;
}
.graph-lightbox-close:hover {
  color: #ff8a65;
}
.graph-lightbox-body {
  position: relative;
  width: 100%;
  height: calc(88vh - 44px);
  overflow: hidden;
}
