:root {
  --bg:        #0a0a0a;
  --bg-1:      #101010;
  --bg-2:      #141414;
  --bg-3:      #1a1a1a;
  --bg-4:      #1f1f1f;
  --border:    #222;
  --border-2:  #2a2a2a;
  --border-3:  #333;
  --text:      #f5f5f5;
  --text-2:    #b5b5b5;
  --text-3:    #7a7a7a;
  --text-4:    #555;
  --red:       #E11D2E;
  --red-2:     #B8141F;
  --red-soft:  rgba(225, 29, 46, 0.10);
  --red-soft-2:rgba(225, 29, 46, 0.18);
  --red-glow:  rgba(225, 29, 46, 0.30);
  --green:     #22c55e;
  --green-soft:rgba(34,197,94,0.12);
  --amber:     #f59e0b;
  --blue:      #6b8cff;
  --purple:    #a78bfa;

  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: 'Figtree', system-ui, sans-serif;

  --topbar-h: 56px;
  --composer-h: 132px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  font-feature-settings: 'ss01' on, 'cv11' on;
  min-height: 100vh;
}
a { color: inherit; }
button, input, select, textarea { font-family: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: transparent; }

/* =================================================================
   TOPBAR
   ================================================================= */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 30;
  background: rgba(10,10,10,0.85);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1180px; margin: 0 auto;
  padding: 0 32px;
  display: flex; align-items: center; justify-content: space-between;
  height: var(--topbar-h);
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-logo {
  display: inline-flex;
  text-decoration: none;
  color: var(--text);
  line-height: 0;
}
.brand-logo svg {
  height: 20px;
  width: auto;
  display: block;
  color: var(--text);
  filter: drop-shadow(0 0 18px rgba(225,29,46,0.08));
}
.brand-logo:hover svg {
  filter: drop-shadow(0 0 22px rgba(225,29,46,0.18));
}
.brand-text {
  font-size: 14px; font-weight: 700; letter-spacing: -0.01em;
  display: flex; align-items: baseline; gap: 8px;
}
.brand-text .dim { color: var(--text-3); font-weight: 500; }
.brand-text .slash { color: var(--text-4); font-weight: 400; }
.brand-text .surface {
  color: var(--text-2); font-weight: 500;
  font-family: var(--mono); font-size: 12px;
}

.top-right { display: flex; align-items: center; gap: 12px; }
.env-pill {
  display: inline-flex; align-items: center; gap: 8px;
  height: 28px; padding: 0 10px;
  font-family: var(--mono); font-size: 11px; font-weight: 500;
  color: var(--text-2);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 4px;
}
.env-pill .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--amber); }
.env-pill .key { color: var(--text-4); }

.status-pill {
  display: inline-flex; align-items: center; gap: 8px;
  height: 28px; padding: 0 12px;
  font-family: var(--mono); font-size: 11px; font-weight: 600;
  color: var(--green);
  background: var(--green-soft);
  border: 1px solid rgba(34,197,94,0.30);
  border-radius: 4px;
  letter-spacing: 0.02em;
}
.status-pill .dot {
  width: 6px; height: 6px; border-radius: 50%; background: var(--green);
  box-shadow: 0 0 0 3px rgba(34,197,94,0.18);
}
.status-pill.busy { color: var(--amber); background: rgba(245,158,11,0.10); border-color: rgba(245,158,11,0.30); }
.status-pill.busy .dot { background: var(--amber); box-shadow: 0 0 0 3px rgba(245,158,11,0.18); animation: pulse 1.2s ease-in-out infinite; }
.status-pill.bad { color: var(--red); background: var(--red-soft); border-color: rgba(225,29,46,0.35); }
.status-pill.bad .dot { background: var(--red); box-shadow: 0 0 0 3px rgba(225,29,46,0.20); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

.topbar-btn {
  display: inline-flex; align-items: center; gap: 8px;
  height: 30px; padding: 0 12px;
  font-family: var(--sans); font-size: 12.5px; font-weight: 500;
  color: var(--text-2);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  transition: background .12s, color .12s, border-color .12s;
}
.topbar-btn:hover { background: var(--bg-3); color: var(--text); border-color: var(--border-2); }
.topbar-btn:active { background: var(--bg-4); }
.topbar-btn svg { color: var(--text-3); }
.topbar-btn:hover svg { color: var(--red); }

/* =================================================================
   CHAT LAYOUT
   ================================================================= */
.chat {
  max-width: 920px;
  margin: 0 auto;
  padding: calc(var(--topbar-h) + 36px) 32px calc(var(--composer-h) + 48px);
  min-height: 100vh;
}

.session-divider {
  display: flex; align-items: center; gap: 14px;
  margin: 0 0 28px;
}
.session-divider .rule { flex: 1; height: 1px; background: var(--border); }
.session-divider .label {
  font-family: var(--mono); font-size: 10px; font-weight: 600;
  color: var(--text-4); letter-spacing: 0.14em;
  white-space: nowrap;
}

.messages {
  display: flex; flex-direction: column; gap: 22px;
}

/* =================================================================
   MESSAGE BUBBLES
   ================================================================= */
.message { display: flex; width: 100%; }
.message.user { justify-content: flex-end; }
.message.assistant { justify-content: stretch; }

.bubble {
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-1);
}

.bubble-user {
  max-width: 78%;
  padding: 14px 18px;
  background: var(--red-soft);
  border-color: rgba(225, 29, 46, 0.30);
}
.bubble-user .bubble-body {
  font-size: 15px; line-height: 1.55;
  color: var(--text);
}
.bubble-user .bubble-meta {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed rgba(225, 29, 46, 0.25);
  font-family: var(--mono); font-size: 10.5px;
  color: var(--text-3);
  display: flex; flex-wrap: wrap; gap: 4px 12px;
}
.bubble-user .meta-k { color: var(--text-4); }
.bubble-user .meta-k b { color: var(--text-2); font-weight: 500; padding-left: 4px; }

.bubble-assistant {
  width: 100%;
  padding: 22px 26px 18px;
  background: var(--bg-1);
}
.bubble-assistant.bubble-error {
  border-color: rgba(225, 29, 46, 0.35);
  background: var(--red-soft);
}
.bubble-error .err-body {
  font-family: var(--mono); font-size: 12px;
  color: var(--red);
}

.bubble-foot {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.metrics-inline {
  display: flex; flex-wrap: wrap; gap: 4px 18px;
  font-family: var(--mono); font-size: 11px;
}
.metrics-inline .m { display: inline-flex; align-items: baseline; gap: 6px; }
.metrics-inline .k { color: var(--text-4); }
.metrics-inline .v { color: var(--text); font-weight: 600; }
.metrics-inline .m-total .v { color: var(--red); }

.chunks-toggle {
  display: inline-flex; align-items: center; gap: 8px;
  height: 28px; padding: 0 12px;
  font-family: var(--mono); font-size: 11px; font-weight: 500;
  color: var(--text-2);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  transition: background .12s, color .12s;
}
.chunks-toggle:hover { background: var(--bg-3); color: var(--text); }
.chunks-toggle svg { color: var(--text-3); }
.chunks-toggle .chunk-count { color: var(--text); font-weight: 600; }
.chunks-toggle .chev { transition: transform .15s ease; }
.chunks-toggle[aria-expanded="true"] .chev { transform: rotate(180deg); }
.chunks-toggle[aria-expanded="true"] { background: var(--bg-3); color: var(--text); }

.chunks-panel {
  margin-top: 14px;
  display: flex; flex-direction: column; gap: 10px;
}
.chunks-panel[hidden] { display: none; }

/* =================================================================
   THINKING BUBBLE (loading state while /answer is in flight)
   ================================================================= */
.bubble-thinking {
  display: flex; flex-direction: column; gap: 12px;
  padding: 18px 22px 18px;
}
.thinking-line {
  display: flex; align-items: center; gap: 12px;
  font-family: var(--mono); font-size: 12px;
  color: var(--text-3);
}
.thinking-label { letter-spacing: 0.02em; }
.thinking-stage {
  margin-left: 4px;
  padding-left: 10px;
  border-left: 1px solid var(--border-2);
  color: var(--text-4);
  font-size: 11px;
  opacity: 0;
  transition: opacity .2s;
}
.thinking-stage.active { opacity: 1; }

.thinking-dots {
  display: inline-flex; gap: 4px;
}
.thinking-dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--red);
  opacity: 0.4;
  animation: dot-bounce 1s ease-in-out infinite;
}
.thinking-dots span:nth-child(1) { animation-delay: 0s; }
.thinking-dots span:nth-child(2) { animation-delay: 0.18s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.36s; }

@keyframes dot-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-4px); opacity: 1; }
}

.thinking-bar {
  height: 2px; background: var(--bg-3);
  border-radius: 2px; overflow: hidden;
  position: relative;
}
.thinking-bar-fill {
  position: absolute; top: 0; left: 0;
  height: 100%; width: 35%;
  background: linear-gradient(90deg, transparent 0%, var(--red) 50%, transparent 100%);
  animation: bar-sweep 1.4s ease-in-out infinite;
}

@keyframes bar-sweep {
  0%   { left: -35%; }
  100% { left: 100%; }
}

/* =================================================================
   ANSWER MARKDOWN
   ================================================================= */
.answer-body {
  font-family: var(--sans);
  font-size: 15px; line-height: 1.7;
  color: var(--text);
  text-wrap: pretty;
}
.answer-body p { margin: 0 0 14px; }
.answer-body p:last-child { margin-bottom: 0; }
.answer-body ul, .answer-body ol { margin: 0 0 14px; padding-left: 22px; }
.answer-body li { margin: 4px 0; }
.answer-body strong { color: var(--text); font-weight: 700; }
.answer-body em { color: var(--text-2); }
.answer-body code {
  background: var(--bg-3); border: 1px solid var(--border);
  padding: 1px 6px; border-radius: 3px;
  font-family: var(--mono); font-size: 12.5px; color: var(--text);
}
.answer-body a {
  color: var(--text);
  text-decoration: none;
  background-image: linear-gradient(var(--red), var(--red));
  background-size: 100% 1px;
  background-position: 0 100%;
  background-repeat: no-repeat;
  padding-bottom: 1px;
  transition: color .12s, background-size .12s;
}
.answer-body a:hover { color: var(--red); }

.cite {
  display: inline-flex; align-items: center;
  font-family: var(--mono); font-size: 10px; font-weight: 600;
  height: 16px; padding: 0 5px;
  margin: 0 1px 0 2px;
  border-radius: 3px;
  color: var(--red);
  background: var(--red-soft);
  border: 1px solid rgba(225,29,46,0.25);
  vertical-align: 1px;
  letter-spacing: 0.02em;
  cursor: pointer;
  text-decoration: none;
  transition: background .12s, color .12s;
}
.cite:hover { background: var(--red); color: #fff; }

/* =================================================================
   CHUNKS (inside the chunks-panel)
   ================================================================= */
.chunk-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}
.chunk-card.cited { border-color: rgba(225,29,46,0.30); }
.chunk-card.cited .chunk-card-head { background: var(--red-soft); }

.chunk-card-head {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-3);
  font-family: var(--mono); font-size: 11px;
  flex-wrap: wrap;
}
.chunk-card-head .n { color: var(--red); font-weight: 700; }
.chunk-card-head .scope { color: var(--text-3); }
.chunk-card-head .path { color: var(--text); font-weight: 500; }
.chunk-card-head .label {
  color: var(--text-3);
  padding-left: 10px; margin-left: 4px;
  border-left: 1px solid var(--border-2);
}
.chunk-card-head .tag-audience {
  margin-left: auto;
  font-size: 10px; font-weight: 600;
  height: 18px; padding: 0 7px;
  display: inline-flex; align-items: center;
  border-radius: 3px;
  color: var(--green); background: var(--green-soft);
  border: 1px solid rgba(34,197,94,0.25);
  letter-spacing: 0.04em;
}
.chunk-card-head .tag-audience.internal {
  color: var(--amber); background: rgba(245,158,11,0.10);
  border-color: rgba(245,158,11,0.30);
}
.chunk-card-head .score {
  color: var(--text-2);
  display: inline-flex; align-items: center; gap: 6px;
}
.chunk-card-head .score .v { color: var(--text); font-weight: 600; }
.chunk-card-head .score-bar {
  width: 48px; height: 3px; background: var(--bg-4);
  border-radius: 2px; overflow: hidden;
}
.chunk-card-head .score-bar span {
  display: block; height: 100%; background: var(--red);
}

.chunk-card-body {
  padding: 12px 14px;
  font-family: var(--mono); font-size: 11.5px; line-height: 1.6;
  color: var(--text-2);
  white-space: pre-wrap; word-break: break-word;
}

/* =================================================================
   COMPOSER (input at bottom)
   ================================================================= */
.composer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
  background: linear-gradient(to top, var(--bg) 60%, rgba(10,10,10,0.0));
  padding: 24px 0 18px;
}
.composer-inner {
  max-width: 920px; margin: 0 auto;
  padding: 0 32px;
}
.composer-form { margin: 0; }
.composer-textarea-wrap {
  position: relative;
  background: var(--bg-1);
  border: 1px solid var(--border-2);
  border-radius: 10px;
  transition: border-color .12s, background .12s;
}
.composer-textarea-wrap:focus-within {
  border-color: rgba(225, 29, 46, 0.45);
  background: var(--bg-2);
}
.composer-textarea {
  display: block; width: 100%;
  background: transparent; border: 0; outline: 0; resize: none;
  padding: 16px 110px 16px 18px;
  font-family: var(--sans); font-size: 15px; line-height: 1.55;
  color: var(--text);
  max-height: 180px;
  min-height: 52px;
  overflow-y: auto;
}
.composer-textarea::placeholder { color: var(--text-4); }

.btn-primary {
  position: absolute; right: 10px; bottom: 10px;
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 16px;
  font-size: 13px; font-weight: 600;
  color: #fff; background: var(--red);
  border-radius: 6px;
  transition: background .12s;
}
.btn-primary:hover { background: var(--red-2); }
.btn-primary:disabled {
  background: var(--bg-3); color: var(--text-4); cursor: not-allowed;
}
.btn-primary svg { color: currentColor; }

.composer-footer {
  display: flex; align-items: center; justify-content: space-between;
  gap: 18px; flex-wrap: wrap;
  margin-top: 10px;
  font-family: var(--mono); font-size: 11px;
}
.composer-fields {
  display: flex; align-items: center; gap: 4px;
  flex-wrap: wrap;
  color: var(--text-3);
}
.composer-fields .dot-sep { color: var(--text-4); padding: 0 4px; }
.field-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background .12s;
  cursor: pointer;
}
.field-chip:hover { background: var(--bg-2); }
.field-chip-key { color: var(--text-4); }
.field-chip-control {
  font-family: var(--mono); font-size: 11px; font-weight: 600;
  color: var(--text-2);
  background: transparent; border: 0; outline: 0;
  padding: 0;
  cursor: pointer;
  appearance: none; -webkit-appearance: none;
  padding-right: 14px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='9' height='6' viewBox='0 0 9 6'><path d='M0.75 1l3.5 3.5 3.5-3.5' stroke='%237a7a7a' stroke-width='1.3' fill='none' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  background-repeat: no-repeat; background-position: right center;
}
.field-chip:hover .field-chip-control { color: var(--text); }
.field-chip-control:focus { color: var(--red); }
.field-chip-control.field-chip-num {
  appearance: textfield; -webkit-appearance: textfield;
  background-image: none; padding-right: 0;
  width: 28px; text-align: left;
}
.field-chip-control.field-chip-text {
  appearance: textfield; -webkit-appearance: textfield;
  background-image: none; padding-right: 0;
  width: 130px; text-align: left;
  cursor: text;
}
.field-chip-control.field-chip-text::placeholder {
  color: var(--text-4);
  font-weight: 500;
}
.field-chip-control option { background: var(--bg-2); color: var(--text); }
.field-chip-control::-webkit-outer-spin-button,
.field-chip-control::-webkit-inner-spin-button {
  -webkit-appearance: none; margin: 0;
}

.composer-hint {
  color: var(--text-4);
  display: inline-flex; align-items: center; gap: 6px;
  white-space: nowrap;
}
.composer-hint kbd {
  font-family: var(--mono); font-size: 10px;
  padding: 1px 5px; border-radius: 3px;
  background: var(--bg-2); border: 1px solid var(--border-2); color: var(--text-3);
}

/* =================================================================
   EMPTY STATE (first load, no messages)
   ================================================================= */
.empty-state {
  margin-top: 36px;
  display: flex; flex-direction: column; align-items: center;
  text-align: center;
  color: var(--text-2);
}
.empty-state[hidden] { display: none; }
.empty-mark {
  width: 44px; height: 44px; border-radius: 10px;
  background: linear-gradient(135deg, var(--red) 0%, var(--red-2) 100%);
  position: relative;
  box-shadow: 0 0 0 1px rgba(225,29,46,0.35), 0 0 38px var(--red-soft-2);
  margin-bottom: 18px;
}
.empty-mark::after {
  content: ''; position: absolute; inset: 10px;
  border: 2px solid #fff; border-radius: 4px; opacity: 0.96;
}
.empty-state h2 {
  margin: 0 0 6px;
  font-size: 22px; font-weight: 700; letter-spacing: -0.015em;
  color: var(--text);
}
.empty-state p {
  margin: 0 0 24px;
  font-size: 14px; color: var(--text-3);
}
.empty-examples {
  display: flex; flex-wrap: wrap; gap: 8px; justify-content: center;
  max-width: 640px;
}
.example-chip {
  font-family: var(--sans); font-size: 12.5px; font-weight: 500;
  color: var(--text-2);
  background: var(--bg-1);
  border: 1px solid var(--border-2);
  padding: 8px 12px; border-radius: 6px;
  transition: background .12s, border-color .12s, color .12s;
}
.example-chip:hover {
  background: var(--bg-2);
  border-color: rgba(225,29,46,0.40);
  color: var(--text);
}

/* =================================================================
   RESPONSIVE
   ================================================================= */
@media (max-width: 700px) {
  .topbar-inner { padding: 0 16px; }
  .top-right { gap: 6px; }
  .topbar-btn span { display: none; } /* icon-only on mobile */
  .env-pill .key { display: none; }
  .chat { padding-left: 16px; padding-right: 16px; }
  .composer-inner { padding: 0 16px; }
  .bubble-user { max-width: 90%; }
  .bubble-assistant { padding: 18px 18px 14px; }
  .metrics-inline { gap: 4px 12px; font-size: 10.5px; }
  .composer-textarea { padding-right: 96px; }
}
