:root {
  --bg: #070b12;
  --bg-elevated: #0e1420;
  --bg-panel: #121a28;
  --border: rgba(148, 163, 184, 0.12);
  --text: #e8edf5;
  --text-muted: #8b9bb8;
  --accent: #3dd9c4;
  --accent-dim: rgba(61, 217, 196, 0.15);
  --user-bubble: #1a2740;
  --assistant-bubble: #111827;
  --danger: #f87171;
  --success: #4ade80;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 24px 48px rgba(0, 0, 0, 0.45);
  --font: "DM Sans", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  background-image:
    radial-gradient(ellipse 80% 50% at 20% -10%, rgba(61, 217, 196, 0.08), transparent),
    radial-gradient(ellipse 60% 40% at 100% 0%, rgba(99, 102, 241, 0.06), transparent);
}

.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: 100vh;
  max-height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.25rem;
  border-right: 1px solid var(--border);
  background: var(--bg-elevated);
  overflow-y: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-icon {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  font-size: 1.25rem;
  color: var(--accent);
  background: var(--accent-dim);
  border-radius: 12px;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-tag {
  margin: 0.15rem 0 0;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 0;
}

input,
select,
textarea {
  font: inherit;
  color: var(--text);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.7rem;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus,
select:focus,
textarea:focus {
  border-color: rgba(61, 217, 196, 0.45);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.btn {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.9rem;
  transition: transform 0.1s, opacity 0.15s;
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(135deg, #3dd9c4, #2bb8a6);
  color: #042018;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 12px;
  flex-shrink: 0;
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn-ghost:hover {
  color: var(--text);
  border-color: rgba(148, 163, 184, 0.25);
}

.btn-sm {
  font-size: 0.8rem;
  padding: 0.4rem 0.65rem;
}

.status-line {
  font-size: 0.75rem;
  margin: 0;
  color: var(--text-muted);
}

.status-line.ok {
  color: var(--success);
}

.status-line.err {
  color: var(--danger);
}

.hint {
  font-size: 0.7rem;
  line-height: 1.4;
  color: var(--text-muted);
  margin: 0;
}

.hint strong {
  color: var(--accent);
  font-weight: 600;
}

.chips {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.chip {
  text-align: left;
  font-size: 0.78rem;
  line-height: 1.35;
  padding: 0.45rem 0.6rem;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.chip:hover {
  color: var(--text);
  border-color: rgba(61, 217, 196, 0.35);
  background: var(--accent-dim);
}

.chip--followup {
  border-style: dashed;
  font-size: 0.74rem;
}

body:not(.has-thread) .chip--followup {
  opacity: 0.65;
}

body.has-thread .chip--followup {
  border-color: rgba(129, 140, 248, 0.35);
}

body.has-thread .chip--followup:hover {
  border-color: rgba(129, 140, 248, 0.55);
  background: rgba(129, 140, 248, 0.12);
}

.label--spaced {
  margin-top: 0.65rem;
}

.conv-id {
  font-family: var(--mono);
  font-size: 0.65rem;
  color: var(--text-muted);
  word-break: break-all;
  margin: auto 0 0;
}

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
}

.chat-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem 1.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.thread-badge {
  flex-shrink: 0;
  margin: 0;
  font-size: 0.68rem;
  font-weight: 600;
  padding: 0.35rem 0.55rem;
  border-radius: 999px;
  background: rgba(129, 140, 248, 0.15);
  border: 1px solid rgba(129, 140, 248, 0.35);
  color: #a5b4fc;
  white-space: nowrap;
}

.chat-header h2 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
}

.chat-header p {
  margin: 0.25rem 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.chat-header code {
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--accent);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.welcome {
  max-width: 520px;
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.95rem;
  line-height: 1.55;
}

.welcome strong {
  color: var(--text);
  display: block;
  font-size: 1.1rem;
  margin-bottom: 0.5rem;
}

.welcome p {
  margin: 0.35rem 0 0;
}

.welcome-hint {
  margin-top: 0.75rem !important;
  font-size: 0.85rem;
  padding: 0.55rem 0.65rem;
  border-radius: 8px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 85%;
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.msg.user {
  align-self: flex-end;
}

.msg.assistant {
  align-self: flex-start;
  max-width: min(920px, 96%);
}

.msg-role {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.msg.user .msg-role {
  text-align: right;
}

.bubble {
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  line-height: 1.55;
  font-size: 0.92rem;
  word-break: break-word;
}

.msg.user .bubble {
  white-space: pre-wrap;
}

.bubble.markdown-body {
  white-space: normal;
}

.bubble.markdown-body > *:first-child {
  margin-top: 0;
}

.bubble.markdown-body > *:last-child {
  margin-bottom: 0;
}

.bubble.markdown-body h1,
.bubble.markdown-body h2,
.bubble.markdown-body h3,
.bubble.markdown-body h4 {
  margin: 1rem 0 0.5rem;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
}

.bubble.markdown-body h3 {
  font-size: 0.95rem;
}

.bubble.markdown-body p {
  margin: 0.5rem 0;
}

.bubble.markdown-body ul,
.bubble.markdown-body ol {
  margin: 0.5rem 0;
  padding-left: 1.25rem;
}

.bubble.markdown-body li {
  margin: 0.25rem 0;
}

.bubble.markdown-body strong {
  color: var(--text);
  font-weight: 600;
}

.bubble.markdown-body code {
  font-family: var(--mono);
  font-size: 0.8em;
  background: var(--bg);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

.bubble.markdown-body .table-wrap {
  overflow-x: auto;
  margin: 0.75rem 0;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.bubble.markdown-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  min-width: 320px;
}

.bubble.markdown-body th,
.bubble.markdown-body td {
  border: 1px solid var(--border);
  padding: 0.45rem 0.55rem;
  text-align: left;
  vertical-align: top;
}

.bubble.markdown-body th {
  background: var(--bg-panel);
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
}

.bubble.markdown-body tbody tr:nth-child(even) {
  background: rgba(255, 255, 255, 0.02);
}

.bubble.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1rem 0;
}

.msg.user .bubble {
  background: var(--user-bubble);
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-bottom-right-radius: 4px;
}

.msg.assistant .bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow);
}

.bubble.loading {
  color: var(--text-muted);
  font-style: italic;
}

.bubble.error {
  border-color: rgba(248, 113, 113, 0.35);
  color: #fecaca;
}

.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
}

.meta-pill {
  font-size: 0.65rem;
  font-family: var(--mono);
  padding: 0.2rem 0.45rem;
  border-radius: 6px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.meta-pill.news {
  border-color: rgba(61, 217, 196, 0.25);
  color: var(--accent);
}

.meta-pill.whale {
  border-color: rgba(251, 191, 36, 0.25);
  color: #fbbf24;
}

.meta-pill.market {
  border-color: rgba(129, 140, 248, 0.25);
  color: #a5b4fc;
}

.meta-pill.follow-up {
  border-color: rgba(129, 140, 248, 0.35);
  color: #a5b4fc;
}

.meta-pill.brief {
  border-color: rgba(61, 217, 196, 0.35);
  color: var(--accent);
}

.meta-pill.scope-warn {
  border-color: rgba(248, 113, 113, 0.35);
  color: #fecaca;
}

.bubble.scope-reject {
  border-color: rgba(248, 113, 113, 0.35);
}

.sources-toggle {
  font-size: 0.72rem;
  color: var(--text-muted);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}

.sources-list {
  font-size: 0.72rem;
  color: var(--text-muted);
  max-height: 280px;
  overflow-y: auto;
  padding: 0.65rem 0.75rem;
  background: var(--bg);
  border-radius: 8px;
  border: 1px solid var(--border);
  display: none;
}

.sources-list.open {
  display: block;
}

.source-group {
  margin-bottom: 0.65rem;
}

.source-group:last-child {
  margin-bottom: 0;
}

.source-group-title {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.source-group ul {
  margin: 0;
  padding-left: 1.1rem;
  list-style: disc;
}

.source-item {
  margin-bottom: 0.45rem;
  line-height: 1.4;
  word-break: break-word;
}

.source-item a {
  color: #93c5fd;
  text-decoration: none;
}

.source-item a:hover {
  text-decoration: underline;
}

.source-chain {
  display: inline-block;
  font-size: 0.62rem;
  font-family: var(--mono);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  background: rgba(129, 140, 248, 0.15);
  color: #a5b4fc;
  margin-right: 0.25rem;
  vertical-align: middle;
}

.source-url {
  font-family: var(--mono);
  font-size: 0.62rem;
  margin-top: 0.15rem;
  opacity: 0.85;
}

.source-url a {
  color: var(--text-muted);
}

.source-more {
  list-style: none;
  margin-left: -1.1rem;
  font-style: italic;
  opacity: 0.75;
}

.chat-footer-note {
  margin: 0;
  padding: 0.5rem 1.5rem 0;
  font-size: 0.7rem;
  color: var(--text-muted);
  text-align: center;
}

.composer {
  display: flex;
  gap: 0.65rem;
  align-items: flex-end;
  padding: 1rem 1.5rem 1.25rem;
  border-top: 1px solid var(--border);
  background: rgba(14, 20, 32, 0.85);
  backdrop-filter: blur(12px);
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 44px;
  max-height: 160px;
  line-height: 1.45;
}

.send-icon {
  font-size: 1.1rem;
  font-weight: 700;
}

@media (max-width: 768px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 40vh;
  }

  .msg {
    max-width: 95%;
  }
}
