:root {
  color-scheme: dark;
  --bg: #101214;
  --panel: #181c20;
  --panel-strong: #20262b;
  --text: #eef2f3;
  --muted: #9aa8ad;
  --line: #2d363c;
  --accent: #37c6a5;
  --accent-strong: #61e7c5;
  --answer-bg: #fffaf0;
  --answer-line: #eadcc7;
  --answer-text: #1a2428;
  --answer-heading: #0f6b58;
  --answer-mark: #fff2b8;
  --danger: #f46d75;
}

* {
  box-sizing: border-box;
}

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

button,
select,
textarea,
pre {
  font: inherit;
}

button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-strong);
  color: var(--text);
  padding: 0 14px;
  cursor: pointer;
}

select {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-strong);
  color: var(--text);
  padding: 0 12px;
}

.toggle {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-strong);
  color: var(--text);
  padding: 0 12px;
  font-size: 13px;
}

.toggle input {
  accent-color: var(--accent);
}

button:hover:not(:disabled) {
  border-color: var(--accent);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

#startButton,
#generateAnswer {
  background: var(--accent);
  color: #06251f;
  border-color: var(--accent);
  font-weight: 700;
}

.shell {
  width: min(1680px, 100%);
  margin: 0 auto;
  padding: clamp(12px, 2vw, 28px);
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line);
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: clamp(24px, 2vw, 38px);
  line-height: 1.15;
}

.topbar p,
.panel-header span {
  color: var(--muted);
}

.actions,
.panel-actions,
.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.header-actions {
  align-items: center;
  justify-content: flex-end;
}

.header-actions button {
  min-height: 30px;
  padding: 0 10px;
  font-size: 13px;
}

.diagnostics {
  display: grid;
  grid-template-columns: 180px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  min-height: 42px;
  padding: 12px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.meter {
  height: 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  overflow: hidden;
  background: var(--panel);
}

.meter span {
  display: block;
  width: 0%;
  height: 100%;
  background: var(--accent);
  transition: width 80ms linear;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 0.92fr) minmax(0, 1.08fr);
  gap: clamp(12px, 1.5vw, 24px);
  padding-top: 18px;
}

.panel {
  min-height: calc(100dvh - 140px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

h2 {
  font-size: 18px;
}

textarea {
  flex: 1;
  width: 100%;
  min-height: clamp(280px, 52dvh, 680px);
  resize: none;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  padding: clamp(12px, 1.4vw, 18px);
  line-height: 1.55;
}

.panel-actions {
  padding: 14px 16px;
  border-top: 1px solid var(--line);
}

pre {
  margin: 0;
  padding: 16px;
  overflow: auto;
  white-space: pre-wrap;
  line-height: 1.55;
  color: var(--text);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.chat-log {
  flex: 1;
  min-height: clamp(320px, 58dvh, 760px);
  padding: clamp(12px, 1.4vw, 20px);
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background:
    linear-gradient(rgba(255, 255, 255, 0.015), rgba(255, 255, 255, 0.015)),
    var(--panel);
}

.chat-empty {
  color: var(--muted);
  line-height: 1.5;
}

.chat-row {
  display: flex;
  flex-direction: column;
  max-width: min(86%, 900px);
  gap: 6px;
}

.chat-row.user {
  align-self: flex-end;
  align-items: flex-end;
}

.chat-row.assistant,
.chat-row.system {
  align-self: flex-start;
  align-items: flex-start;
}

.bubble {
  padding: clamp(10px, 1vw, 14px) clamp(12px, 1.2vw, 18px);
  border: 1px solid var(--line);
  border-radius: 8px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.chat-row.user .bubble {
  background: #d8f5bd;
  color: #10220c;
  border-color: #d8f5bd;
  border-bottom-right-radius: 2px;
}

.chat-row.assistant .bubble {
  background: var(--answer-bg);
  color: var(--answer-text);
  border-color: var(--answer-line);
  border-bottom-left-radius: 2px;
}

.chat-row.system .bubble {
  background: var(--panel-strong);
  color: var(--muted);
}

.bubble-meta {
  color: var(--muted);
  font-size: 11px;
}

.bubble-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.bubble-actions button {
  min-height: 28px;
  padding: 0 9px;
  font-size: 12px;
}

.markdown {
  white-space: normal;
}

.markdown h3 {
  margin: 14px 0 8px;
  color: var(--answer-heading);
  font-size: clamp(16px, 1.15vw, 20px);
  font-weight: 800;
  text-decoration: underline;
  text-decoration-color: rgba(55, 198, 165, 0.5);
  text-decoration-thickness: 2px;
  text-underline-offset: 4px;
}

.markdown p {
  margin: 8px 0 10px;
}

.markdown ul,
.markdown ol {
  margin: 8px 0 8px 22px;
  padding: 0;
}

.markdown li {
  margin: 4px 0;
}

.markdown strong {
  color: #063f35;
  font-weight: 800;
}

.markdown em {
  color: #38544d;
}

.markdown u {
  text-decoration-color: var(--answer-heading);
  text-decoration-thickness: 2px;
  text-underline-offset: 3px;
}

.markdown mark {
  border-radius: 4px;
  background: var(--answer-mark);
  color: inherit;
  padding: 0 3px;
}

.markdown code {
  border-radius: 4px;
  background: #e9e1d4;
  color: #7b2d12;
  padding: 1px 4px;
  font-family: Consolas, "SFMono-Regular", Menlo, monospace;
  font-size: 0.94em;
}

.code-block {
  margin: 12px 0;
  border: 1px solid #28343b;
  border-radius: 8px;
  background: #0d1117;
  color: #d7e1e8;
  padding: clamp(10px, 1vw, 14px);
  overflow-x: auto;
  white-space: pre;
  font-family: Consolas, "SFMono-Regular", Menlo, monospace;
  font-size: clamp(12px, 0.95vw, 15px);
}

.code-block code {
  background: transparent;
  color: inherit;
  padding: 0;
}

.sources {
  border-top: 1px solid var(--line);
  padding: 14px 16px;
}

.tabs {
  display: flex;
  gap: 8px;
  padding: 12px 16px 0;
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.02);
}

.tab-button {
  min-height: 34px;
  padding: 0 12px;
}

.tab-button.active {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.tab-panel {
  display: none;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.02);
}

.tab-panel.active {
  display: grid;
}

.helper-text {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.tab-panel textarea {
  min-height: 72px;
  max-height: 120px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  padding: 10px;
}

.tab-panel button {
  align-self: start;
  width: max-content;
}

.tab-panel input[type="file"] {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--muted);
  padding: 8px;
}

.sources h3 {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}

.sources ul {
  margin: 0;
  padding-left: 18px;
  color: var(--muted);
  font-size: 13px;
}

@media (min-width: 1600px) {
  .workspace {
    grid-template-columns: minmax(420px, 0.82fr) minmax(720px, 1.18fr);
  }

  .chat-row {
    max-width: min(80%, 1120px);
  }
}

@media (max-width: 1100px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

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

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

  .panel {
    min-height: 560px;
  }
}

@media (max-width: 640px) {
  button,
  select,
  textarea,
  pre {
    font-size: 15px;
  }

  .topbar,
  .panel-header {
    align-items: stretch;
    flex-direction: column;
  }

  .actions,
  .header-actions,
  .panel-actions {
    width: 100%;
  }

  .actions > *,
  .panel-actions button,
  select {
    flex: 1 1 100%;
  }

  .toggle {
    justify-content: center;
  }

  .chat-row {
    max-width: 96%;
  }

  .chat-row.user,
  .chat-row.assistant,
  .chat-row.system {
    align-self: stretch;
    align-items: stretch;
  }

  .tabs {
    overflow-x: auto;
  }

  .sources {
    max-height: 140px;
    overflow: auto;
  }
}
