* {
  box-sizing: border-box;
}

:root {
  --bg: #f6f7f4;
  --surface: #ffffff;
  --surface-soft: #f9faf7;
  --text: #111827;
  --muted: #6b7280;
  --faint: #9ca3af;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --brand-soft: #eaf2ff;
  --suisent: #06a96b;
  --success: #059669;
  --danger: #b91c1c;
  --shadow: 0 18px 45px rgba(17, 24, 39, 0.12);
}

html,
body {
  width: 100%;
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  color: var(--text);
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}

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

button:not(:disabled):active {
  transform: translateY(1px);
}

p,
h1,
h2 {
  margin: 0;
}

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

.page-shell {
  width: 100%;
  height: 100svh;
  display: flex;
  justify-content: center;
  background: var(--bg);
}

#app-shell {
  width: 100%;
  height: 100%;
  max-width: 860px;
  min-width: 0;
  background: var(--surface);
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.auth-card {
  width: min(92vw, 430px);
  margin: max(8vh, 42px) auto 0;
  padding: 26px;
}

.auth-logo {
  display: block;
  width: 76px;
  height: 76px;
  object-fit: contain;
  margin-bottom: 14px;
}

.auth-header h1 {
  font-size: 1.45rem;
  line-height: 1.25;
  letter-spacing: 0;
}

.auth-header p {
  margin-top: 8px;
  color: var(--muted);
  line-height: 1.6;
}

#login-form {
  margin-top: 22px;
  display: grid;
  gap: 12px;
}

input,
textarea {
  width: 100%;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 14px;
  outline: none;
}

input:focus,
textarea:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

input {
  min-height: 48px;
  padding: 0 14px;
}

#login-btn,
#send-btn {
  min-height: 48px;
  color: #ffffff;
  background: var(--brand);
  border-radius: 14px;
  font-weight: 700;
}

#send-btn.stop {
  background: #111827;
}

#login-btn {
  width: 100%;
}

.status {
  font-size: 0.82rem;
  line-height: 1.45;
}

.status-muted {
  color: var(--muted);
}

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

#login-status,
#login-error {
  margin-top: 12px;
}

#chat-view {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--surface);
}

.chat-header {
  position: relative;
  z-index: 10;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  gap: 4px 8px;
  align-items: center;
  padding: max(11px, env(safe-area-inset-top)) 14px 8px;
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid var(--line);
}

.nav-icon-btn {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  color: var(--text);
  background: transparent;
  border-radius: 13px;
  font-size: 1.45rem;
  line-height: 1;
  font-weight: 700;
}

.nav-icon-btn:active {
  background: var(--surface-soft);
}

.title-area {
  min-width: 0;
  text-align: center;
}

.title-area h1 {
  font-size: 1.04rem;
  line-height: 1.25;
  letter-spacing: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#health-status {
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ghost-btn {
  color: var(--text);
  background: var(--surface-soft);
  border: 1px solid var(--line);
}

.current-topic-title {
  grid-column: 1 / -1;
  min-width: 0;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(17, 24, 39, 0.28);
  backdrop-filter: blur(1px);
}

.topics-drawer {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 40;
  width: min(82vw, 360px);
  padding: max(18px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 14px;
  overflow: hidden;
  background: var(--surface);
  border-right: 1px solid var(--line);
  box-shadow: 22px 0 45px rgba(17, 24, 39, 0.16);
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

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

.drawer-logo {
  width: 42px;
  height: 42px;
  object-fit: contain;
  border-radius: 12px;
}

.drawer-brand-name {
  font-size: 1rem;
  font-weight: 800;
  line-height: 1.25;
}

.drawer-brand-sub {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.78rem;
}

.drawer-close-btn {
  color: var(--muted);
  font-size: 1.65rem;
}

.topic-search {
  height: 44px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  color: var(--faint);
  background: #f3f4f6;
  border-radius: 14px;
}

.topic-search input {
  min-height: 0;
  height: 100%;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}

.topics-list {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.topic-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.topic-select-btn {
  min-width: 0;
  min-height: 54px;
  padding: 9px 10px;
  text-align: left;
  color: var(--text);
  background: transparent;
  border: 0;
  border-radius: 14px;
}

.topic-select-btn[aria-pressed="true"] {
  background: #f3f4f6;
}

.topic-title-text,
.topic-meta-text {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topic-title-text {
  font-size: 0.9rem;
  font-weight: 700;
}

.topic-meta-text {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.75rem;
}

.topic-delete-btn {
  width: 42px;
  height: 38px;
  color: var(--muted);
  border-radius: 12px;
  font-size: 0.72rem;
}

.topics-empty {
  padding: 18px 10px;
  color: var(--muted);
  font-size: 0.86rem;
  text-align: center;
}

.drawer-clear-btn {
  height: 42px;
  color: var(--muted);
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 14px;
  font-size: 0.84rem;
  font-weight: 650;
}

.conversation-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow-x: hidden;
  background: var(--surface);
}

.chat-transcript {
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 18px 14px 22px;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 32px;
  text-align: center;
  pointer-events: none;
}

.empty-logo {
  width: 86px;
  height: 86px;
  object-fit: contain;
}

.empty-state h2 {
  margin-top: 16px;
  font-size: 1.08rem;
  line-height: 1.35;
  letter-spacing: 0;
}

.empty-state p {
  max-width: 320px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.7;
}

.message {
  max-width: 100%;
  min-width: 0;
  word-break: break-word;
}

.message.user {
  align-self: flex-end;
  max-width: min(86%, 620px);
  padding: 10px 13px;
  color: #0f172a;
  background: var(--brand-soft);
  border: 1px solid rgba(37, 99, 235, 0.12);
  border-radius: 18px 18px 5px 18px;
}

.message.assistant,
.message.loading {
  align-self: stretch;
  padding: 0;
  background: transparent;
}

.message.error {
  padding: 12px;
  color: var(--danger);
  background: #fff1f2;
  border: 1px solid #fecdd3;
  border-radius: 14px;
}

.message-head {
  margin-bottom: 6px;
  color: var(--faint);
  font-size: 0.72rem;
  line-height: 1.25;
}

.message.user .message-head {
  text-align: right;
}

.message-answer {
  max-width: 100%;
  min-width: 0;
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  line-height: 1.72;
  font-size: 0.96rem;
}

.message-answer.markdown-content {
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
  line-height: 1.78;
  font-size: 0.98rem;
}

.markdown-content,
.markdown-content * {
  max-width: 100%;
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
}

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

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

.markdown-content p {
  margin: 0 0 0.88em;
}

.markdown-content h1,
.markdown-content h2,
.markdown-content h3,
.markdown-content h4 {
  margin: 1.05em 0 0.45em;
  color: #0f172a;
  line-height: 1.35;
  font-weight: 780;
  letter-spacing: 0;
}

.markdown-content h1 {
  font-size: 1.18rem;
}

.markdown-content h2 {
  font-size: 1.08rem;
}

.markdown-content h3,
.markdown-content h4 {
  font-size: 1rem;
}

.markdown-content strong {
  color: #0b1220;
  font-weight: 780;
}

.markdown-content ul,
.markdown-content ol {
  margin: 0.35em 0 0.95em;
  padding-left: 1.35em;
}

.markdown-content li {
  margin: 0.38em 0;
  padding-left: 0.08em;
}

.markdown-content li > p {
  margin: 0.25em 0;
}

.markdown-content table {
  display: block;
  width: 100%;
  max-width: 100%;
  margin: 0.65em 0 1em;
  overflow-x: auto;
  border-collapse: collapse;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 0.86rem;
  -webkit-overflow-scrolling: touch;
}

.markdown-content th,
.markdown-content td {
  min-width: 72px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  white-space: normal;
}

.markdown-content th {
  background: var(--surface-soft);
  font-weight: 760;
}

.markdown-content blockquote {
  margin: 0.85em 0;
  padding: 8px 12px;
  color: #374151;
  background: var(--surface-soft);
  border-left: 3px solid var(--brand);
  border-radius: 8px;
}

.markdown-content code {
  padding: 1px 5px;
  background: #f1f5f9;
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.9em;
}

.markdown-content pre {
  max-width: 100%;
  margin: 0.75em 0 1em;
  padding: 10px 12px;
  overflow-x: auto;
  background: #0f172a;
  border-radius: 8px;
  -webkit-overflow-scrolling: touch;
}

.markdown-content pre code {
  padding: 0;
  color: #e5e7eb;
  background: transparent;
  white-space: pre;
}

.message-progress-slot:not(:empty) + .message-answer {
  margin-top: 12px;
}

.message-answer:empty {
  display: none;
}

.message.assistant .message-answer,
.message.loading .message-answer {
  padding-right: 2px;
}

.message-sources,
.message-trace {
  margin-top: 12px;
  padding: 10px 12px;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.message.loading .message-trace {
  background: #f8fbff;
  border-color: #cfe0ff;
}

.message-sources-title,
.message-trace-title,
.message-progress-title {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 750;
}

.message-sources-title,
.message-trace-title {
  cursor: pointer;
  list-style: none;
}

.message-sources-title::-webkit-details-marker,
.message-trace-title::-webkit-details-marker {
  display: none;
}

.message-sources-title::after,
.message-trace-title::after {
  content: "展开";
  float: right;
  color: var(--brand);
  font-weight: 700;
}

.message-sources[open] .message-sources-title::after,
.message-trace[open] .message-trace-title::after {
  content: "收起";
}

.message-sources ul,
.message-trace ul,
.message-progress-list {
  margin: 7px 0 0;
  padding-left: 18px;
  color: #374151;
  font-size: 0.8rem;
  line-height: 1.6;
}

.message-sources li,
.message-trace li,
.message-progress-list li {
  overflow-wrap: anywhere;
}

.message-sources li + li,
.message-trace li + li {
  margin-top: 4px;
}

.message-progress {
  display: inline-block;
  max-width: 100%;
  padding: 10px 12px;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.message-progress-list {
  list-style: none;
  padding-left: 0;
}

.message-progress-list li {
  white-space: normal;
}

.message-actions {
  margin-top: 12px;
  display: flex;
  gap: 8px;
}

.copy-btn {
  width: 42px;
  height: 42px;
  padding: 0;
  color: var(--brand);
  background: #eef5ff;
  border: 1px solid #d9e8ff;
  border-radius: 14px;
  font-size: 0;
  font-weight: 650;
}

.copy-btn::before {
  content: "⧉";
  font-size: 1.2rem;
  line-height: 1;
}

.copy-btn.copied::before {
  content: "✓";
}

.composer {
  padding: 10px 12px max(12px, env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.98);
  border-top: 1px solid var(--line);
}

#message-input {
  display: block;
  min-height: 50px;
  max-height: 138px;
  padding: 13px 58px 13px 14px;
  resize: none;
  border-radius: 18px;
  line-height: 1.5;
}

.composer-footer {
  position: relative;
}

#chat-error {
  min-height: 0;
  margin-top: 7px;
  padding-right: 88px;
}

#send-btn {
  position: absolute;
  right: 7px;
  bottom: 7px;
  min-width: 48px;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 13px;
  font-size: 0.88rem;
}

@media (min-width: 768px) {
  .page-shell {
    padding: 18px;
    align-items: center;
  }

  #app-shell {
    height: min(880px, calc(100svh - 36px));
    border: 1px solid var(--line);
    border-radius: 22px;
    box-shadow: var(--shadow);
    overflow: hidden;
  }

  .chat-header {
    padding: 14px 18px 11px;
  }

  .chat-transcript {
    padding: 24px 22px 28px;
  }

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

@media (max-width: 380px) {
  .chat-header {
    grid-template-columns: 40px minmax(0, 1fr) 40px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .message.user {
    max-width: 92%;
  }
}
