@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600&family=IBM+Plex+Serif:wght@500&family=IBM+Plex+Mono:wght@400;500&display=swap');

:root {
  --ink: #2e2620;
  --ink-soft: #6b5c4f;
  --ink-faint: #a0917f;
  --paper: #f8f1e6;
  --panel: #fffcf6;
  --line: #e9ddc8;
  --accent: #3f7d5f;
  --accent-soft: #e8f0e9;
  --warm: #d98a4a;
  --warm-soft: #fbe9d3;
  --warn: #b5502a;
  --warn-soft: #f8e6d8;
  --radius: 12px;
  font-size: 16px;
}

[data-theme="dark"] {
  --ink: #e8ddd0;
  --ink-soft: #a89880;
  --ink-faint: #6a5a48;
  --paper: #18140f;
  --panel: #211c16;
  --line: #38302a;
  --accent: #5faa7a;
  --accent-soft: #1c3226;
  --warm: #e09a5a;
  --warm-soft: #38200e;
  --warn: #e06848;
  --warn-soft: #3c1810;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }

body {
  font-family: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--paper);
  color: var(--ink);
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

.app-shell {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  max-width: 780px;
  margin: 0 auto;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  background: var(--panel);
}

/* ---- Topbar ---- */

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
}

.topbar__brand {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-weight: 600;
}
.topbar__mark { color: var(--accent); }
.topbar__product { color: var(--ink-soft); font-weight: 500; }

.topbar__session {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  font-size: 0.85rem;
  color: var(--ink-soft);
}
.topbar__session input {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.85rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 5px 8px;
  width: 96px;
  background: var(--paper);
  color: var(--ink);
}
.topbar__session input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.topbar__spacer { flex: 1; }

.topbar__status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--ink-soft);
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-faint); }
.status-dot.ok { background: var(--accent); }
.status-dot.mock { background: var(--warm); }
.status-dot.degraded { background: var(--warn); }

/* ---- Top nav links ---- */

.topbar__nav {
  display: flex;
  gap: 2px;
  margin-left: 8px;
}

.nav-link {
  padding: 6px 12px;
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--ink-soft);
  text-decoration: none;
  border-radius: var(--radius);
  transition: color 0.15s, background 0.15s;
}
.nav-link:hover { color: var(--ink); background: var(--paper); }
.nav-link--active { color: var(--accent); background: var(--accent-soft); }
.nav-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- Documents panel ---- */

.docs-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex: 1;
}

.docs-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 22px 10px;
  border-bottom: 1px solid var(--line);
}

.docs-search {
  flex: 1;
  font-family: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 12px;
  background: var(--paper);
  color: var(--ink);
}
.docs-search:focus { outline: none; border-color: var(--accent); }

.docs-count {
  font-size: 0.8rem;
  color: var(--ink-faint);
  white-space: nowrap;
}

.docs-list {
  overflow-y: auto;
  padding: 14px 22px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.docs-loading {
  color: var(--ink-faint);
  font-size: 0.9rem;
  padding: 20px 0;
  text-align: center;
}

.doc-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  transition: border-color 0.15s;
}
.doc-card:hover { border-color: var(--accent); }

.doc-format-badge {
  flex-shrink: 0;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.7rem;
  font-weight: 500;
  text-transform: uppercase;
  padding: 3px 7px;
  border-radius: 5px;
  min-width: 40px;
  text-align: center;
}
.doc-format-badge--pdf  { background: #fde8e0; color: #b53a1a; }
.doc-format-badge--html { background: #ddeeff; color: #1a55a0; }
.doc-format-badge--md   { background: var(--accent-soft); color: var(--accent); }
.doc-format-badge--txt  { background: var(--paper); color: var(--ink-faint); border: 1px solid var(--line); }

[data-theme="dark"] .doc-format-badge--pdf  { background: #3a1008; color: #f0866a; }
[data-theme="dark"] .doc-format-badge--html { background: #0a1e38; color: #6aa8f0; }

.doc-info { flex: 1; min-width: 0; }
.doc-title {
  font-weight: 500;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.doc-meta {
  display: flex;
  gap: 10px;
  margin-top: 2px;
  font-size: 0.78rem;
  color: var(--ink-faint);
}
.doc-id { font-family: 'IBM Plex Mono', monospace; }

.doc-actions { display: flex; gap: 6px; flex-shrink: 0; }

.doc-btn {
  font-size: 0.8rem;
  font-weight: 500;
  padding: 5px 11px;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink-soft);
  cursor: pointer;
  text-decoration: none;
  transition: border-color 0.15s, color 0.15s;
  white-space: nowrap;
}
.doc-btn:hover { border-color: var(--accent); color: var(--accent); }
.doc-btn--primary {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.doc-btn--primary:hover { background: var(--accent); color: #fff; }

@media (max-width: 640px) {
  .doc-card { flex-wrap: wrap; }
  .doc-actions { width: 100%; justify-content: flex-end; }
}

/* ---- Document preview modal ---- */

.doc-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.doc-modal[hidden] { display: none; }

.doc-modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
}

.doc-modal__dialog {
  position: relative;
  z-index: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  width: min(92vw, 920px);
  height: min(88vh, 820px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}

.doc-modal__header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.doc-modal__title {
  flex: 1;
  font-weight: 500;
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--ink);
}

.doc-modal__download {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--ink-soft);
  text-decoration: none;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--paper);
  white-space: nowrap;
  transition: border-color 0.15s, color 0.15s;
}
.doc-modal__download:hover { border-color: var(--accent); color: var(--accent); }

.doc-modal__close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--paper);
  color: var(--ink-soft);
  cursor: pointer;
  flex-shrink: 0;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.doc-modal__close:hover { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }
.doc-modal__close:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.doc-modal__body {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

.modal-frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

.modal-text {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 22px 26px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.82rem;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-y: auto;
  color: var(--ink);
  background: var(--paper);
}

.modal-loading {
  padding: 40px;
  text-align: center;
  color: var(--ink-faint);
  font-size: 0.9rem;
}

/* ---- Thread & empty state ---- */

.thread {
  overflow-y: auto;
  padding: 28px 22px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.thread__intro {
  max-width: 520px;
  margin: 24px auto 8px;
  text-align: center;
}

.intro__avatar {
  width: 52px;
  height: 52px;
  margin: 0 auto 18px;
  border-radius: 50%;
  background: var(--warm-soft);
  color: var(--warm);
  display: flex;
  align-items: center;
  justify-content: center;
}
.intro__avatar svg { width: 26px; height: 26px; }

.thread__intro h1 {
  font-family: 'IBM Plex Serif', serif;
  font-weight: 500;
  font-size: 1.5rem;
  line-height: 1.4;
  margin: 0 0 10px;
}

.thread__intro p {
  color: var(--ink-soft);
  line-height: 1.55;
  margin: 0 0 26px;
  font-size: 0.95rem;
}

.openers__label {
  font-size: 0.82rem;
  color: var(--ink-faint);
  margin: 0 0 10px;
  text-align: left;
}

.openers__row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.opener {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 13px 15px;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.opener:hover { border-color: var(--accent); transform: translateY(-1px); }
.opener:focus-visible { border-color: var(--accent); }
.opener:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.opener__title { font-weight: 500; font-size: 0.88rem; color: var(--ink); }
.opener__sub { font-size: 0.78rem; color: var(--ink-soft); }

/* ---- Messages ---- */

.message { display: flex; gap: 10px; }
.message--user { justify-content: flex-end; }
.message--agent { justify-content: flex-start; align-items: flex-start; }

.message__avatar {
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--warm-soft);
  color: var(--warm);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 2px;
}
.message__avatar svg { width: 16px; height: 16px; }

.message__bubble {
  max-width: 82%;
  border-radius: var(--radius);
  padding: 12px 14px;
  line-height: 1.55;
  font-size: 0.95rem;
}
.message--user .message__bubble { background: var(--ink); color: #fff; }
.message--agent .message__bubble {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 1px 3px rgba(46, 38, 32, 0.06);
}

.message__answer { margin: 0 0 8px; white-space: pre-wrap; }
.message__answer:last-child { margin-bottom: 0; }

.message__escalated {
  display: inline-block;
  font-size: 0.78rem;
  color: var(--warn);
  background: var(--warn-soft);
  border-radius: var(--radius);
  padding: 4px 9px;
  margin-bottom: 8px;
}

.message__section { margin-top: 6px; }
.section-toggle {
  background: none;
  border: none;
  padding: 4px 0;
  font-size: 0.78rem;
  color: var(--accent);
  font-weight: 500;
  cursor: pointer;
}
.section-toggle .count { color: var(--ink-faint); font-weight: 400; }
.section-toggle:focus-visible { outline: 2px solid var(--accent); border-radius: 3px; }

.section-body { display: none; margin-top: 6px; border-top: 1px solid var(--line); padding-top: 8px; }
.section-body.open { display: block; }

.citation-item, .trace-item { font-size: 0.8rem; padding: 6px 0; border-bottom: 1px dashed var(--line); }
.citation-item:last-child, .trace-item:last-child { border-bottom: none; }
.citation-item__title { font-weight: 500; }
.citation-item__section { color: var(--ink-faint); }
.citation-item__snippet { margin-top: 3px; color: var(--ink-soft); font-style: italic; }

.trace-item__tool {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 500;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 0.75rem;
}
.trace-item__args, .trace-item__result {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.72rem;
  color: var(--ink-faint);
  margin-top: 3px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* thinking indicator -- appears only while waiting on a real response */
.thinking-dots { display: inline-flex; gap: 4px; padding: 3px 0; }
.thinking-dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--ink-faint);
  animation: thinking-pulse 1.1s ease-in-out infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: 0.15s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes thinking-pulse {
  0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-2px); }
}

/* ---- Composer ---- */

.composer {
  display: flex;
  gap: 8px;
  padding: 14px 22px;
  border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1;
  resize: none;
  font-family: inherit;
  font-size: 0.95rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  background: var(--paper);
  color: var(--ink);
}
.composer textarea:focus { outline: none; border-color: var(--accent); }

.composer button {
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: var(--ink);
  color: #fff;
  border-radius: var(--radius);
  width: 44px;
  cursor: pointer;
}
.composer button:disabled { background: var(--ink-faint); cursor: default; }
.composer button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (max-width: 640px) {
  .app-shell { border: none; }
  .openers__row { grid-template-columns: 1fr; }
  .topbar__session input { width: 76px; }
}

/* Confirmation button, shown when the agent asks the user to confirm a side-effecting action */
.confirm-btn {
  display: inline-block;
  margin-top: 8px;
  border: 1px solid var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: var(--radius);
  padding: 7px 14px;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
}
.confirm-btn:hover { background: var(--accent); color: #fff; }
.confirm-btn:disabled { opacity: 0.5; cursor: default; }

/* ---- Theme toggle button ---- */
.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--ink-soft);
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
  flex-shrink: 0;
}
.theme-toggle:hover { border-color: var(--accent); color: var(--accent); }
.theme-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* show sun in dark mode, moon in light mode */
[data-theme="light"] .icon-moon { display: block; }
[data-theme="light"] .icon-sun  { display: none; }
[data-theme="dark"]  .icon-sun  { display: block; }
[data-theme="dark"]  .icon-moon { display: none; }

/* dark mode — user bubble uses accent instead of near-black */
[data-theme="dark"] .message--user .message__bubble {
  background: var(--accent);
  color: #fff;
}

/* dark mode — agent bubble shadow tweak */
[data-theme="dark"] .message--agent .message__bubble {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

/* dark mode — opener titles match h1 (var(--ink) via higher specificity) */
[data-theme="dark"] .opener__title { color: var(--ink); }

/* dark mode — invert iframe/embed previews so white-bg documents become dark */
[data-theme="dark"] .modal-frame {
  filter: invert(1) hue-rotate(180deg);
}
