:root {
  --chat-bg: #f6f7fb;
  --chat-surface: #ffffff;
  --chat-sidebar: #ffffff;
  --chat-border: #e5e7eb;
  --chat-user: #4f46e5;
  --chat-user-text: #ffffff;
  --chat-ai: #ffffff;
  --chat-ai-border: #e5e7eb;
  --chat-text: #1f2937;
  --chat-muted: #6b7280;
  --chat-accent: #4f46e5;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--chat-bg);
  color: var(--chat-text);
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ---------- Top bar ---------- */
.topbar {
  background: var(--chat-surface);
  border-bottom: 1px solid var(--chat-border);
  padding: 0.6rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 0 0 auto;
  z-index: 20;
}

.topbar .brand {
  font-weight: 700;
  letter-spacing: -0.01em;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.brand-icon {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem;
}

.brand-sub {
  font-size: 0.72rem;
  color: var(--chat-muted);
  font-weight: 500;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #22c55e;
  display: inline-block;
}

.status-dot.busy {
  background: #f59e0b;
}

.topbar .btn {
  border-radius: 8px;
}

/* ---------- Layout: sidebar + chat ---------- */
.app-body {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
}

.sidebar {
  width: 280px;
  flex: 0 0 280px;
  background: var(--chat-sidebar);
  border-right: 1px solid var(--chat-border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar .sidebar-head {
  padding: 0.9rem 1rem 0.6rem;
}

.sidebar .list-group {
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}

.sidebar .list-group-item {
  border: none;
  border-bottom: 1px solid var(--chat-border);
  border-radius: 0 !important;
  cursor: pointer;
  background: transparent;
  padding: 0.65rem 1rem;
}

.sidebar .list-group-item:hover {
  background: #f3f4f6;
}

.sidebar .list-group-item.active {
  background: #eef2ff;
  color: var(--chat-accent);
  font-weight: 600;
}

.sidebar .conv-title {
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.sidebar .conv-time {
  font-size: 0.72rem;
  color: var(--chat-muted);
}

/* ---------- Main chat column ---------- */
.chat-col {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.thread {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 1.5rem 1rem 0.5rem;
}

.thread-inner {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ---------- Bubbles ---------- */
.msg-row {
  display: flex;
  width: 100%;
}

.msg-stack {
  display: flex;
  flex-direction: column;
  max-width: min(78%, 640px);
}

.msg-row.user .msg-stack {
  align-items: flex-end;
}

.msg-row.ai .msg-stack {
  align-items: flex-start;
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-row.ai {
  justify-content: flex-start;
}

.bubble {
  max-width: 100%;
  padding: 0.7rem 0.95rem;
  border-radius: 16px;
  font-size: 0.95rem;
  line-height: 1.55;
  overflow-wrap: anywhere;
}

.msg-meta {
  display: flex;
  gap: 0.25rem;
  padding: 0.2rem 0.35rem 0;
}

.copy-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--chat-muted);
  cursor: pointer;
  font-size: 0.85rem;
  transition: color 0.15s, background 0.15s, opacity 0.15s;
}

.msg-meta .copy-btn {
  opacity: 0.4;
}

.msg-meta .copy-btn:hover {
  opacity: 1;
  background: #eef2ff;
  color: var(--chat-accent);
}

.msg-meta .copy-btn.copied {
  opacity: 1;
  color: #16a34a;
}

.msg-row.user .bubble {
  background: var(--chat-user);
  color: var(--chat-user-text);
  border-bottom-right-radius: 4px;
}

.msg-row.ai .bubble {
  background: var(--chat-ai);
  border: 1px solid var(--chat-ai-border);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.bubble p {
  margin: 0 0 0.5rem;
}

.bubble p:last-child {
  margin-bottom: 0;
}

.bubble pre {
  background: #0f172a;
  color: #e2e8f0;
  border-radius: 8px;
  padding: 0.65rem 0.85rem;
  font-size: 0.83rem;
  overflow-x: auto;
  margin: 0;
}

.code-block {
  position: relative;
  margin: 0.4rem 0;
}

.code-block .copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(255, 255, 255, 0.12);
  color: #cbd5e1;
}

.code-block .copy-btn:hover {
  background: rgba(255, 255, 255, 0.25);
  color: #fff;
}

.code-block .copy-btn.copied {
  color: #4ade80;
}

.bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.msg-row.user .bubble code.inline {
  background: rgba(255, 255, 255, 0.18);
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
}

.msg-row.ai .bubble code.inline {
  background: #f1f2f6;
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
  color: #b02a37;
}

.bubble pre code.inline {
  background: none;
  padding: 0;
  color: inherit;
}

/* ---------- Markdown rendering ---------- */
.bubble h1,
.bubble h2,
.bubble h3,
.bubble h4,
.bubble h5,
.bubble h6 {
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.01em;
  margin: 0.85rem 0 0.4rem;
}

.bubble h1:first-child,
.bubble h2:first-child,
.bubble h3:first-child,
.bubble h4:first-child,
.bubble h5:first-child,
.bubble h6:first-child {
  margin-top: 0;
}

.bubble .md-h1 { font-size: 1.4rem; }
.bubble .md-h2 { font-size: 1.25rem; }
.bubble .md-h3 { font-size: 1.1rem; }
.bubble .md-h4 { font-size: 1.02rem; }
.bubble .md-h5 { font-size: 0.98rem; }
.bubble .md-h6 { font-size: 0.95rem; }

.bubble ul,
.bubble ol {
  margin: 0.4rem 0 0.6rem;
  padding-left: 1.4rem;
}

.bubble ul li,
.bubble ol li {
  margin: 0.15rem 0;
}

.bubble blockquote {
  margin: 0.5rem 0;
  padding: 0.4rem 0.9rem;
  border-left: 3px solid var(--chat-accent);
  background: #f7f7fb;
  border-radius: 0 8px 8px 0;
}

.bubble blockquote p {
  margin: 0.25rem 0;
}

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

.bubble a {
  color: var(--chat-accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.msg-row.user .bubble a {
  color: #e0e7ff;
}

.bubble .md-table {
  font-size: 0.85rem;
  margin: 0.5rem 0;
  width: 100%;
}

.bubble .md-table th,
.bubble .md-table td {
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--chat-border);
  vertical-align: top;
}

.bubble .md-table th {
  background: #f1f2f6;
  font-weight: 600;
}

.bubble .table-responsive {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.msg-row.user .bubble .md-table th {
  background: rgba(255, 255, 255, 0.16);
}

/* ---------- Streaming cursor ---------- */
.stream-cursor {
  display: inline-block;
  width: 8px;
  height: 1.05em;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: var(--chat-accent);
  border-radius: 2px;
  animation: cursor-blink 0.9s steps(2, start) infinite;
}

.msg-row.user .stream-cursor {
  background: #e0e7ff;
}

@keyframes cursor-blink {
  to { visibility: hidden; }
}

/* ---------- Typing indicator ---------- */
.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 0.35rem 0.1rem;
}

.typing-dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--chat-muted);
  animation: blink 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* ---------- Welcome / empty state ---------- */
.welcome {
  text-align: center;
  max-width: 560px;
  margin: auto;
  padding: 1.5rem 1rem;
}

.welcome .welcome-icon {
  font-size: 3rem;
  color: var(--chat-accent);
  margin-bottom: 0.75rem;
}

.suggestion-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  margin-top: 1.25rem;
}

.chip {
  background: var(--chat-surface);
  border: 1px solid var(--chat-border);
  border-radius: 999px;
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
  color: var(--chat-text);
  cursor: pointer;
  transition: all 0.15s;
}

.chip:hover {
  border-color: var(--chat-accent);
  color: var(--chat-accent);
  background: #eef2ff;
}

/* ---------- Error banner ---------- */
.error-banner {
  max-width: 860px;
  margin: 0 auto 0.5rem;
  border-left: 4px solid #dc3545;
}

/* ---------- Composer ---------- */
.composer {
  flex: 0 0 auto;
  padding: 0.75rem 1rem 1rem;
}

.composer-inner {
  max-width: 860px;
  margin: 0 auto;
  background: var(--chat-surface);
  border: 1px solid var(--chat-border);
  border-radius: 14px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.5rem 0.5rem 0.5rem 0.9rem;
}

.composer textarea {
  flex: 1 1 auto;
  border: none;
  outline: none;
  resize: none;
  max-height: 200px;
  font-size: 0.95rem;
  line-height: 1.5;
  background: transparent;
  padding: 0.4rem 0;
  color: var(--chat-text);
}

.composer textarea::placeholder {
  color: var(--chat-muted);
}

.send-btn {
  border-radius: 10px;
  width: 42px;
  height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 42px;
  background: var(--chat-accent);
  border: none;
  color: #fff;
  font-size: 1.1rem;
}

.send-btn:hover:not(:disabled) {
  background: #4338ca;
}

.send-btn:disabled {
  opacity: 0.55;
}

.composer-hint {
  max-width: 860px;
  margin: 0.45rem auto 0;
  text-align: center;
  font-size: 0.72rem;
  color: var(--chat-muted);
}

/* ---------- Scrollbars ---------- */
.thread::-webkit-scrollbar,
.sidebar .list-group::-webkit-scrollbar {
  width: 8px;
}

.thread::-webkit-scrollbar-thumb,
.sidebar .list-group::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 8px;
}

.thread::-webkit-scrollbar-track,
.sidebar .list-group::-webkit-scrollbar-track {
  background: transparent;
}

/* ---------- Mobile ---------- */
@media (max-width: 768px) {
  .sidebar {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 260px;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 30;
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.15);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 25;
  }

  .msg-stack {
    max-width: 88%;
  }
}