:root {
  --bg: #fbfaf7;
  --pane-bg: #ffffff;
  --border: #e6e3dc;
  --text: #2a2a28;
  --muted: #6b6864;
  --accent: #1f4d3a;
  --accent-bg: #e8efe9;
  --accent-hover: #16382a;
  --cite-bg: #fff5d6;
  --cite-border: #d6c66b;
  --cite-text: #5a4500;
  --user-bg: #eef2f6;
  --code-bg: #f3f2ee;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  font-family: -apple-system, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; padding: 0; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-columns: minmax(380px, 1fr) minmax(420px, 1.2fr);
  height: 100vh;
  gap: 1px;
  background: var(--border);
  overflow: hidden;
}

/* ----- Chat pane ----- */
.chat-pane {
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: var(--pane-bg);
  min-width: 0;
  min-height: 0;       /* let the messages row actually shrink and scroll */
  overflow: hidden;
}

.chat-header {
  padding: 14px 18px 10px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
.chat-header .title-block { flex: 1 1 auto; min-width: 0; }
.chat-header h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.subtitle {
  font-size: 12px;
  color: var(--muted);
}
.icon-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  font-size: 17px;
  line-height: 1;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.messages {
  overflow-y: auto;
  min-height: 0;       /* same — force the 1fr row to be scroll-bounded */
  padding: 18px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

.hint {
  background: var(--accent-bg);
  border: 1px solid #d4ddd4;
  border-radius: 8px;
  padding: 14px 16px;
  font-size: 14px;
  color: var(--text);
}
.hint ul { margin: 6px 0 6px 18px; padding: 0; color: var(--muted); }
.hint li { margin: 2px 0; }
.cite-demo {
  display: inline-block;
  padding: 0 5px;
  background: var(--cite-bg);
  border: 1px solid var(--cite-border);
  border-radius: 3px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--cite-text);
}

.msg {
  border-radius: 10px;
  padding: 12px 14px;
  max-width: 100%;
}
.msg.user {
  background: var(--user-bg);
  align-self: flex-end;
  max-width: 85%;
  white-space: pre-wrap;
}
.msg.assistant {
  background: transparent;
  align-self: flex-start;
  padding: 0;
  border-radius: 0;
}
.msg.assistant .answer {
  /* markdown-rendered content */
}
.msg.assistant p { margin: 6px 0; }
.msg.assistant ul, .msg.assistant ol { margin: 6px 0 6px 22px; padding: 0; }
.msg.assistant li { margin: 2px 0; }
.msg.assistant code {
  background: var(--code-bg);
  border-radius: 3px;
  padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.92em;
}
.msg.assistant blockquote {
  border-left: 3px solid var(--border);
  margin: 8px 0;
  padding: 2px 0 2px 12px;
  color: var(--muted);
}

.cite {
  display: inline-block;
  padding: 0 5px;
  margin: 0 1px;
  background: var(--cite-bg);
  border: 1px solid var(--cite-border);
  border-radius: 3px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--cite-text);
  text-decoration: none;
  cursor: pointer;
  vertical-align: 1px;
}
.cite:hover {
  background: #ffea99;
  border-color: #b89e3a;
}
.cite.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}
/* A [?] chip: the model cited an index no source in the conversation has. */
.cite.dead {
  cursor: help;
  opacity: 0.55;
  background: transparent;
  border-style: dashed;
}

.status-line {
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
  padding-left: 4px;
}
.status-line::before {
  content: "↳ ";
}

.citations-panel {
  margin-top: 10px;
  border-top: 1px dashed var(--border);
  padding-top: 8px;
  font-size: 13px;
}
.citations-panel .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-bottom: 4px;
}
.citations-panel .cite-row {
  display: flex;
  gap: 8px;
  padding: 4px 6px;
  cursor: pointer;
  border-radius: 4px;
  align-items: baseline;
  border-left: 2px solid transparent;
}
.citations-panel .cite-row:hover {
  background: var(--accent-bg);
}
.citations-panel .cite-row.cited {
  border-left-color: var(--cite-border);
  background: #fffbe6;
}
.citations-panel .cite-row.cited:hover {
  background: #fff5cc;
}
.citations-panel .cite-row.uncited {
  opacity: 0.7;
}
.citations-panel .cite-row.uncited:hover {
  opacity: 1;
}

/* Sub-section headers inside the Sources panel ("Cited in answer", "Other retrieved") */
.citations-panel .cite-section-header {
  margin-top: 8px;
  padding: 4px 0 3px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  font-weight: 600;
}
.citations-panel .cite-section-header.collapsible {
  cursor: pointer;
  user-select: none;
}
.citations-panel .cite-section-header.collapsible:hover {
  color: var(--text);
}
.citations-panel .cite-section-header .arrow {
  display: inline-block;
  width: 12px;
  margin-right: 4px;
  color: var(--muted);
  font-size: 9px;
  transform: translateY(-1px);
}
.citations-panel .cite-num {
  flex: 0 0 28px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--cite-text);
}
.citations-panel .cite-body {
  flex: 1 1 auto;
  min-width: 0;
}
.citations-panel .cite-title {
  font-weight: 500;
  font-size: 13px;
}
.citations-panel .cite-meta {
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* ----- Composer ----- */
.composer {
  border-top: 1px solid var(--border);
  padding: 12px 24px 18px;
  background: var(--pane-bg);
}
textarea#input {
  width: 100%;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.45;
  background: var(--pane-bg);
  color: var(--text);
  min-height: 64px;
  outline: none;
  transition: border-color 0.15s;
}
textarea#input:focus { border-color: var(--accent); }

.composer-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  align-items: center;
}

button {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: white;
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  border-radius: 6px;
  padding: 7px 16px;
  cursor: pointer;
  transition: background 0.15s;
}
button:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
button:disabled { background: #888; border-color: #888; cursor: progress; }
button.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}
button.ghost:hover { background: var(--pane-bg); color: var(--text); border-color: var(--muted); }

.status {
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
  margin-left: auto;
}

/* ----- PDF pane ----- */
.pdf-pane {
  display: grid;
  grid-template-rows: auto 1fr;
  background: var(--pane-bg);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.pdf-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  color: var(--muted);
  background: var(--pane-bg);
  z-index: 2;
  flex-shrink: 0;
}
.pdf-title {
  flex: 1 1 auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.pdf-search-form {
  flex: 0 0 auto;
  margin: 0;
}
.pdf-search-form input {
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 9px;
  font-family: inherit;
  font-size: 12px;
  width: 200px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, width 0.15s;
}
.pdf-search-form input:focus { border-color: var(--accent); width: 240px; }
.ghost-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 12px;
}
.ghost-link:hover { text-decoration: underline; }

.pdf-frame-wrap {
  position: relative;
  background: #2a2a28;
}
.pdf-frame {
  width: 100%;
  height: 100%;
  border: 0;
  background: #2a2a28;
}
.pdf-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  color: var(--muted);
  pointer-events: none;
}
.placeholder-inner {
  max-width: 380px;
  padding: 20px;
  text-align: center;
}
.placeholder-inner h2 {
  margin: 0 0 8px;
  font-size: 15px;
  color: var(--text);
}
.placeholder-inner p {
  margin: 0;
  font-size: 13px;
}

/* ----- Settings modal ----- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 18, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  z-index: 100;
  padding: 5vh 20px;
  overflow-y: auto;
}
.modal[hidden] { display: none; }
.modal-content {
  background: var(--pane-bg);
  border-radius: 10px;
  width: 100%;
  max-width: 980px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.25);
  overflow: hidden;
}
.modal-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}
.modal-body {
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 22px;
}
.config-section h3 {
  margin: 0 0 8px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 10px;
}
.model-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}
.model-row select {
  flex: 1 1 auto;
  max-width: 380px;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 6px 8px;
  font-family: inherit;
  font-size: 13px;
  background: var(--pane-bg);
  color: var(--text);
}
.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 6px 0;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}
.checkbox-row input[type="checkbox"] { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
}
.small-btn {
  padding: 2px 8px;
  font-size: 11px;
  border-radius: 4px;
}
.prompt-display {
  margin: 0;
  padding: 12px 14px;
  background: var(--code-bg);
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.55;
  white-space: pre-wrap;
  max-height: 50vh;
  overflow-y: auto;
}
.table-scroll {
  max-height: 320px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.corpus-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.corpus-table thead th {
  position: sticky;
  top: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.corpus-table tbody td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.corpus-table tbody tr:last-child td { border-bottom: 0; }
.corpus-table .col-cat,
.corpus-table .col-tier,
.corpus-table .col-chunks,
.corpus-table .col-size { white-space: nowrap; color: var(--muted); }
.corpus-table .col-title { font-weight: 500; }
.corpus-table a.src-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 11px;
}
.corpus-table a.src-link:hover { text-decoration: underline; }
.refresh-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}
.refresh-log {
  margin: 0;
  padding: 12px 14px;
  background: #1c1c1a;
  color: #d6d3cc;
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  line-height: 1.5;
  max-height: 280px;
  overflow-y: auto;
  white-space: pre-wrap;
}
.refresh-log .step-line { color: #b8d9be; }
.refresh-log .err-line { color: #f0907a; }

/* ----- Discover ----- */
.discover-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0 8px;
}
.discover-list {
  max-height: 360px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fcfbf7;
}
.discover-cat-header {
  position: sticky;
  top: 0;
  background: var(--bg);
  padding: 6px 12px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  z-index: 1;
}
.discover-row {
  padding: 6px 12px 6px 14px;
  display: grid;
  grid-template-columns: 22px 1fr auto;
  gap: 8px;
  align-items: baseline;
  font-size: 13px;
  border-bottom: 1px solid #eee8d6;
}
.discover-row:last-child { border-bottom: 0; }
.discover-row.known { opacity: 0.5; }
.discover-row .doc-title { font-weight: 500; }
.discover-row .doc-meta {
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: nowrap;
}
.discover-row .doc-url {
  font-size: 11px;
  color: var(--accent);
  text-decoration: none;
  display: block;
  word-break: break-all;
}
.discover-row input[type="checkbox"] {
  margin: 4px 0 0;
}
.tag {
  display: inline-block;
  padding: 0 6px;
  border-radius: 3px;
  font-size: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-left: 6px;
  vertical-align: 1px;
}
.tag.new { background: #d1ebd6; color: #1e4a2b; }
.tag.known { background: #eee; color: #777; }
.tag.saved { background: #fff5cc; color: #5a4500; }

/* Responsive: stack on narrow screens */
@media (max-width: 920px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr 50vh; }
}
