:root {
  --accent: #4f46e5;
  --accent-dark: #4338ca;
  --bg: #eef1f9;
  --surface: #ffffff;
  --text: #1b1f2a;
  --text-muted: #6b7280;
  --border: #e4e7ef;
  --bubble-assistant: #f2f4fa;
  --danger: #b42318;
  --radius-lg: 20px;
  --radius-md: 14px;
  --shadow: 0 18px 50px rgba(23, 32, 64, 0.12);
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  padding: 24px;
  background:
    radial-gradient(circle at 12% 8%, rgba(79, 70, 229, 0.16), transparent 42%),
    radial-gradient(circle at 88% 92%, rgba(14, 165, 233, 0.14), transparent 45%),
    var(--bg);
  color: var(--text);
}

[hidden] {
  display: none !important;
}

.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;
}

.app {
  display: flex;
  flex-direction: column;
  width: min(860px, 100%);
  height: calc(100vh - 48px);
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid rgba(228, 231, 239, 0.9);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 13px;
  background: linear-gradient(135deg, #6366f1, #0ea5e9);
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  flex: none;
}

.brand-text h1 {
  margin: 0;
  font-size: 17px;
  letter-spacing: 0.02em;
}

.brand-text p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.conversation {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.ghost-button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}

.ghost-button:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: #f7f7ff;
}

.messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 24px;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.empty-state {
  margin: auto;
  max-width: 340px;
  text-align: center;
  color: var(--text-muted);
}

.empty-state h2 {
  margin: 0 0 8px;
  font-size: 18px;
  color: var(--text);
}

.empty-state p {
  margin: 0;
  font-size: 13px;
  line-height: 1.6;
}

.message {
  display: flex;
  max-width: 82%;
  animation: rise 0.18s ease-out;
}

.message--user {
  align-self: flex-end;
  justify-content: flex-end;
}

.message--assistant,
.message--error {
  align-self: flex-start;
}

.bubble {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  font-size: 14px;
  line-height: 1.65;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message--user .bubble {
  background: linear-gradient(135deg, var(--accent), #6366f1);
  color: #fff;
  border-bottom-right-radius: 5px;
}

.message--assistant .bubble {
  background: var(--bubble-assistant);
  border-bottom-left-radius: 5px;
}

.message--error .bubble {
  background: #fef3f2;
  color: var(--danger);
  border: 1px solid #fecdca;
  border-bottom-left-radius: 5px;
}

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

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

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

.markdown-body p {
  margin: 0.55em 0;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  margin: 1em 0 0.45em;
  line-height: 1.35;
}

.markdown-body h1 {
  font-size: 1.35em;
}

.markdown-body h2 {
  font-size: 1.22em;
}

.markdown-body h3,
.markdown-body h4 {
  font-size: 1.1em;
}

.markdown-body ul,
.markdown-body ol {
  margin: 0.55em 0;
  padding-left: 1.5em;
}

.markdown-body li + li {
  margin-top: 0.25em;
}

.markdown-body a {
  color: var(--accent-dark);
}

.markdown-body code {
  padding: 1px 5px;
  border-radius: 5px;
  background: rgba(15, 23, 42, 0.08);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.9em;
}

.markdown-body pre {
  margin: 0.7em 0;
  padding: 12px;
  border-radius: 10px;
  background: #172033;
  color: #e5e7eb;
  overflow-x: auto;
  white-space: pre;
}

.markdown-body pre code {
  padding: 0;
  background: transparent;
}

.markdown-body table {
  width: 100%;
  margin: 0.7em 0;
  border-collapse: collapse;
  font-size: 13px;
}

.markdown-body th,
.markdown-body td {
  padding: 6px 8px;
  border: 1px solid #dbe1ec;
  text-align: left;
  vertical-align: top;
}

.markdown-body th {
  background: #e7ebf5;
}

.markdown-body img {
  max-width: 100%;
  height: auto;
}

.markdown-body blockquote {
  margin: 0.7em 0;
  padding: 4px 12px;
  border-left: 3px solid #a5b4fc;
  color: var(--text-muted);
}

.sources {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid #dfe4ef;
  font-size: 12px;
  line-height: 1.45;
}

.sources-label {
  color: var(--text-muted);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}

.sources-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-top: 8px;
}

.sources a {
  color: var(--accent-dark);
  text-decoration: none;
  overflow-wrap: anywhere;
}

.sources a:hover {
  text-decoration: underline;
}

.typing .bubble {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 12px 16px;
  color: var(--text-muted);
}

.progress-spinner {
  width: 12px;
  height: 12px;
  flex: none;
  border: 2px solid #c7d2fe;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 16px 24px 8px;
  border-top: 1px solid var(--border);
}

.composer textarea {
  flex: 1;
  min-height: 46px;
  max-height: 160px;
  padding: 13px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: #fafbfe;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  line-height: 1.5;
  resize: none;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.composer textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.14);
  background: #fff;
}

.send-button {
  padding: 0 22px;
  height: 46px;
  border: none;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.send-button:hover:not(:disabled) {
  background: var(--accent-dark);
}

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

.status {
  min-height: 20px;
  margin: 0;
  padding: 0 24px 14px;
  font-size: 12px;
  color: var(--text-muted);
}

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

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

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 640px) {
  body {
    padding: 0;
  }

  .app {
    width: 100%;
    height: 100vh;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  .app-header {
    padding: 14px 16px;
  }

  .conversation {
    display: none;
  }

  .messages {
    padding: 18px 16px;
  }

  .message {
    max-width: 90%;
  }

  .composer {
    padding: 12px 16px 6px;
  }

  .status {
    padding: 0 16px 10px;
  }
}