:root {
  --bg: #f4f7f8;
  --panel: #ffffff;
  --ink: #16202a;
  --muted: #637181;
  --line: #dbe3e8;
  --brand: #0f766e;
  --brand-dark: #0b5e58;
  --soft: #eaf4f3;
  --warn: #b45309;
  --danger: #b42318;
  --shadow: 0 18px 45px rgba(26, 44, 61, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
}

button, input {
  font: inherit;
}

.shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(0, 920px);
  gap: 18px;
  padding: 18px;
  justify-content: center;
}

body.debug-mode .shell {
  width: min(1360px, 100%);
  margin: 0 auto;
  grid-template-columns: minmax(0, 800px) minmax(360px, 500px);
}

.chat, .side section {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.chat {
  height: calc(100vh - 36px);
  min-height: 560px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
}

.chat-header {
  min-height: 72px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.chat-header h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0;
}

.chat-header p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.signal {
  width: 12px;
  height: 12px;
  border-radius: 999px;
  background: #16a34a;
  box-shadow: 0 0 0 6px rgba(22, 163, 74, 0.12);
}

.messages {
  padding: 18px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: #fbfcfd;
}

.bubble {
  max-width: min(640px, 78%);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 0;
}

.bubble.user {
  align-self: flex-end;
  background: var(--brand);
  color: #ffffff;
  border-color: var(--brand);
  border-bottom-right-radius: 3px;
}

.bubble.bot {
  align-self: flex-start;
  background: #ffffff;
  border-bottom-left-radius: 3px;
}

.bubble.system {
  align-self: center;
  max-width: 760px;
  color: var(--muted);
  background: #f8fafc;
  font-size: 13px;
}

.meta {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  min-height: 22px;
  border-radius: 999px;
  padding: 4px 8px;
  display: inline-flex;
  align-items: center;
  background: var(--soft);
  color: var(--brand-dark);
  font-size: 12px;
  line-height: 1;
}

.chip.warn {
  background: #fff4e5;
  color: var(--warn);
}

.composer {
  border-top: 1px solid var(--line);
  padding: 14px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 92px;
  gap: 10px;
}

.composer input {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 12px;
  outline: none;
}

.composer input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12);
}

.composer button, .suggestions button {
  min-height: 40px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
}

.composer button {
  background: var(--brand);
  color: #ffffff;
}

.composer button:disabled {
  opacity: 0.55;
  cursor: wait;
}

.side {
  display: grid;
  align-content: start;
  gap: 18px;
  max-height: calc(100vh - 36px);
  overflow: auto;
  padding-right: 2px;
}

.side[hidden] {
  display: none;
}

.side section {
  padding: 14px;
}

.side h2 {
  margin: 0 0 12px;
  font-size: 15px;
  letter-spacing: 0;
}

.side-title {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.side-title h2 {
  margin: 0;
}

.side-title a,
.side-title span {
  color: var(--muted);
  font-size: 12px;
  text-align: right;
}

.side-title a {
  text-decoration: none;
}

.side-title a:hover {
  color: var(--brand);
}

.suggestions {
  display: grid;
  gap: 8px;
}

.suggestions button {
  text-align: left;
  background: #ffffff;
  border-color: var(--line);
  color: var(--ink);
  padding: 8px 10px;
}

.suggestions button:hover {
  background: var(--soft);
  border-color: #bddbd7;
}

.stats {
  display: grid;
  gap: 10px;
  margin: 0;
}

.stats div {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 8px;
}

.stats div:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.stats dt {
  color: var(--muted);
}

.stats dd {
  margin: 0;
  font-weight: 650;
}

.matches {
  display: grid;
  gap: 10px;
}

.match {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  display: grid;
  gap: 6px;
}

.match strong {
  font-size: 13px;
}

.match-score {
  color: var(--muted);
  font-size: 12px;
}

.match p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
  word-break: break-word;
}

.match small {
  border-top: 1px solid var(--line);
  padding-top: 8px;
  color: var(--ink);
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.debug-pipeline {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.debug-pipeline article {
  min-height: 86px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px;
  background: #fbfcfd;
  display: grid;
  align-content: start;
  gap: 5px;
}

.debug-pipeline strong {
  font-size: 12px;
}

.debug-pipeline p {
  margin: 0;
  font-size: 12px;
  line-height: 1.35;
  word-break: break-word;
}

.debug-pipeline span {
  color: var(--muted);
  font-size: 11px;
}

.debug-tokens {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.debug-tokens span {
  border-radius: 999px;
  padding: 4px 8px;
  background: #eef5ff;
  color: #1d4ed8;
  font-size: 12px;
  line-height: 1.1;
}

.debug-tokens p {
  margin: 0;
  color: var(--muted);
}

.debug-prompt {
  max-height: 420px;
  overflow: auto;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #101828;
  color: #e6edf3;
  font-size: 11px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

@media (max-width: 980px) {
  .shell,
  body.debug-mode .shell {
    grid-template-columns: 1fr;
  }

  .side {
    max-height: none;
    overflow: visible;
  }

  .chat {
    height: calc(100vh - 20px);
    min-height: 560px;
  }
}

@media (max-width: 560px) {
  .shell {
    padding: 10px;
  }

  .composer {
    grid-template-columns: 1fr;
  }

  .bubble {
    max-width: 100%;
  }
}

.rag-page {
  background: #eef3f5;
}

.rag-shell {
  width: min(1280px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 18px 0 28px;
}

.rag-header {
  min-height: 74px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.rag-header p {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 13px;
}

.rag-header h1 {
  margin: 0;
  font-size: 28px;
  letter-spacing: 0;
}

.plain-link {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 12px;
  display: inline-flex;
  align-items: center;
  color: var(--ink);
  background: #ffffff;
  text-decoration: none;
}

.rag-console,
.rag-panel,
.rag-summary {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 28px rgba(26, 44, 61, 0.08);
}

.rag-console {
  padding: 16px;
}

.rag-form {
  display: grid;
  gap: 8px;
}

.rag-form label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.rag-compose {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 130px;
  gap: 10px;
}

.rag-compose textarea {
  width: 100%;
  min-height: 76px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 11px 12px;
  color: var(--ink);
  resize: vertical;
  outline: none;
  font: inherit;
  line-height: 1.45;
}

.rag-compose textarea:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12);
}

.rag-compose button,
.rag-samples button {
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
}

.rag-compose button {
  min-height: 44px;
  background: var(--brand);
  color: #ffffff;
  font-weight: 650;
}

.rag-compose button:disabled {
  opacity: 0.55;
  cursor: wait;
}

.rag-samples {
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.rag-samples button {
  min-height: 34px;
  padding: 7px 10px;
  background: #f8fafc;
  border-color: var(--line);
  color: var(--ink);
}

.rag-samples button:hover {
  background: #edf7f6;
  border-color: #b9d9d6;
}

.rag-summary {
  margin-top: 14px;
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.rag-summary div {
  min-height: 58px;
  border-left: 3px solid #2563eb;
  background: #f8fafc;
  border-radius: 6px;
  padding: 9px 10px;
}

.rag-summary div:nth-child(2) {
  border-left-color: #0f766e;
}

.rag-summary div:nth-child(3) {
  border-left-color: #b45309;
}

.rag-summary div:nth-child(4) {
  border-left-color: #64748b;
}

.rag-summary span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.rag-summary strong {
  display: block;
  margin-top: 4px;
  font-size: 18px;
  line-height: 1.2;
  word-break: break-word;
}

.rag-layout {
  margin-top: 14px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  gap: 14px;
  align-items: start;
}

.rag-main,
.rag-side {
  display: grid;
  gap: 14px;
}

.rag-panel {
  padding: 14px;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.panel-title h2 {
  margin: 0;
  font-size: 16px;
  letter-spacing: 0;
}

.panel-title span {
  color: var(--muted);
  font-size: 12px;
  text-align: right;
}

.pipeline {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.pipeline-step {
  min-height: 122px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px;
  display: grid;
  align-content: start;
  gap: 8px;
  background: #fbfcfd;
}

.pipeline-step strong {
  font-size: 13px;
}

.pipeline-step p {
  margin: 0;
  color: var(--ink);
  font-size: 13px;
  line-height: 1.4;
  word-break: break-word;
}

.pipeline-step span {
  color: var(--muted);
  font-size: 12px;
}

.final-reply {
  margin: 0;
  min-height: 70px;
  border: 1px solid #bddbd7;
  border-radius: 8px;
  padding: 14px;
  background: #f7fcfb;
  line-height: 1.55;
  white-space: pre-wrap;
}

.rag-matches {
  display: grid;
  gap: 12px;
}

.rag-match {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  display: grid;
  gap: 10px;
  background: #ffffff;
}

.match-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.match-head strong {
  font-size: 14px;
}

.match-head span {
  color: var(--muted);
  font-size: 12px;
  text-align: right;
}

.score-line {
  height: 7px;
  overflow: hidden;
  border-radius: 999px;
  background: #e8eef2;
}

.score-line span {
  height: 100%;
  display: block;
  border-radius: inherit;
  background: linear-gradient(90deg, #2563eb, #0f766e);
}

.rag-match dl {
  margin: 0;
  display: grid;
  gap: 8px;
}

.rag-match dl div {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr);
  gap: 8px;
}

.rag-match dt {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.rag-match dd {
  margin: 0;
  line-height: 1.45;
  word-break: break-word;
}

.match-tags,
.token-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.match-tags span,
.token-list span {
  border-radius: 999px;
  padding: 4px 8px;
  background: #eef5ff;
  color: #1d4ed8;
  font-size: 12px;
  line-height: 1.1;
}

.match-tags span:nth-child(2n) {
  background: var(--soft);
  color: var(--brand-dark);
}

.match-tags .warn {
  background: #fff4e5;
  color: var(--warn);
}

.match-tags .danger {
  background: #fff1f0;
  color: var(--danger);
}

.token-list p,
.empty {
  margin: 0;
  color: var(--muted);
}

.prompt-preview {
  max-height: 560px;
  overflow: auto;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #101828;
  color: #e6edf3;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

@media (max-width: 1040px) {
  .rag-layout,
  .pipeline {
    grid-template-columns: 1fr;
  }

  .rag-summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {
  .rag-shell {
    width: min(100vw - 20px, 1280px);
    padding-top: 10px;
  }

  .rag-compose,
  .rag-summary {
    grid-template-columns: 1fr;
  }

  .rag-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .match-head {
    align-items: flex-start;
    flex-direction: column;
  }
}
