:root {
  /* 明亮主题（默认） */
  --bg: #f8fafc;
  --panel: #ffffff;
  --panel-2: rgba(255, 255, 255, 0.85);
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #4f46e5;
  --primary2: #4338ca;
  --primary-glow: rgba(79, 70, 229, 0.4);
  --danger: #ef4444;
  --good: #10b981;
  --radius: 16px;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
  --shadow-strong: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
  --bubble-assistant: #ffffff;
  --bubble-user: #eef2ff;
  --bubble-user-border: #c7d2fe;
  --blockquote-bg: #f8fafc;
  --blockquote-border: #818cf8;
  --code-bg: #f1f5f9;
  --mesh-1: #e0e7ff;
  --mesh-2: #f3e8ff;
  --icon-color: #6366f1;
}

/* 暗黑主题 */
:root[data-theme="dark"] {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: rgba(30, 41, 59, 0.85);
  --text: #f8fafc;
  --muted: #94a3b8;
  --border: #334155;
  --primary: #6366f1;
  --primary2: #4f46e5;
  --primary-glow: rgba(99, 102, 241, 0.4);
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -2px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -4px rgba(0, 0, 0, 0.3);
  --shadow-strong: 0 20px 25px -5px rgba(0, 0, 0, 0.4), 0 8px 10px -6px rgba(0, 0, 0, 0.4);
  --bubble-assistant: #1e293b;
  --bubble-user: rgba(99, 102, 241, 0.15);
  --bubble-user-border: rgba(99, 102, 241, 0.3);
  --blockquote-bg: rgba(255, 255, 255, 0.03);
  --blockquote-border: #818cf8;
  --code-bg: #0f172a;
  --mesh-1: #1e1b4b;
  --mesh-2: #312e81;
  --icon-color: #818cf8;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 300ms ease, color 300ms ease;
  overflow: hidden; /* 防止背景滚动 */
}

/* 动态背景 */
.bg-mesh {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: -1;
  background-image: 
    radial-gradient(at 40% 20%, var(--mesh-1) 0px, transparent 50%),
    radial-gradient(at 80% 0%, var(--mesh-2) 0px, transparent 50%),
    radial-gradient(at 0% 50%, var(--mesh-2) 0px, transparent 50%);
  filter: blur(80px);
  opacity: 0.6;
  transition: background-image 500ms ease;
}

.app {
  height: 100%;
  display: block;
  position: relative;
}

.main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100%;
}

/* 顶部栏 */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: background 300ms ease, border-color 300ms ease;
  z-index: 10;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.topbar-brand {
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: -0.5px;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
}

.topbar-brand i {
  color: var(--primary);
  font-size: 1.3rem;
}

.status {
  color: var(--muted);
  font-size: 13px;
  text-align: right;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
}
.status::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  transition: background 300ms ease;
}
.status.error::before { background: var(--danger); }
.status.good::before { background: var(--good); }

.ghost {
  display: flex;
  align-items: center;
  gap: 6px;
  border-radius: 8px;
  padding: 6px 12px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
  transition: all 200ms ease;
}
.ghost:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: rgba(99, 102, 241, 0.05);
}

/* 欢迎页 */
.chat-wrap {
  overflow-y: auto;
  padding: 24px 16px 200px; /* 增加底部 padding，防止被输入框遮挡 */
  /* 移除 smooth，防止自动滚动时动画堆叠导致不跟手 */
  /* scroll-behavior: smooth; */
  /* 如果容器不是 100% 高度，滚动计算会失效。需要让它占满剩余空间 */
  height: 100%;
}

.welcome {
  max-width: 600px;
  margin: 10vh auto 24px;
  text-align: center;
  color: var(--muted);
  animation: slideUp 400ms cubic-bezier(0.16, 1, 0.3, 1) both;
}
.welcome-icon {
  font-size: 3rem;
  color: var(--icon-color);
  margin-bottom: 16px;
  display: inline-block;
  background: linear-gradient(135deg, var(--mesh-1), var(--mesh-2));
  padding: 16px;
  border-radius: 24px;
  box-shadow: var(--shadow);
}
.welcome-title {
  color: var(--text);
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.5px;
  margin-bottom: 12px;
}
.welcome-sub {
  font-size: 15px;
  line-height: 1.6;
}

.mode-center {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 24px;
}

.chip {
  display: flex;
  align-items: center;
  gap: 8px;
  border-radius: 99px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 500;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: all 200ms ease;
}
.chip i {
  font-size: 1.1rem;
}
.chip.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
  box-shadow: 0 4px 12px var(--primary-glow);
}
.chip:not(.active):hover {
  border-color: var(--primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

/* 文档流区域 */
.chat {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.user-request-block {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  padding: 16px 20px;
  background: var(--bubble-user);
  border: 1px solid var(--bubble-user-border);
  border-radius: 16px;
  color: var(--text);
  font-weight: 500;
  animation: slideUp 300ms ease-out both;
  font-size: 15px;
  line-height: 1.6;
}
.user-request-block i {
  font-size: 1.5rem;
  color: var(--primary);
  flex-shrink: 0;
}

.doc-block {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  transition: all 300ms ease;
  animation: slideUp 300ms ease-out both;
}
.doc-block.streaming {
  box-shadow: 0 0 0 2px var(--primary);
  animation: pulse-border 2s infinite;
}

.doc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  color: var(--text);
  font-weight: 600;
  transition: background 200ms ease;
}
.doc-header:hover {
  background: var(--border);
}
.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}
.header-title {
  font-size: 15px;
}
.header-title span {
  font-size: 13px;
  color: var(--muted);
  font-weight: 400;
  margin-left: 6px;
}
.doc-header .caret {
  transition: transform 200ms ease;
  color: var(--muted);
}
.doc-block.collapsed .doc-header .caret {
  transform: rotate(-90deg);
}
.doc-block.collapsed .doc-body {
  display: none;
}
.doc-block.collapsed .doc-header {
  border-bottom: none;
}
.edit-hint {
  color: var(--muted);
  font-size: 1.1rem;
}

.doc-body {
  padding: 24px 32px;
  line-height: 1.7;
  font-size: 15px;
  color: var(--text);
  outline: none;
}
@media (max-width: 600px) {
  .doc-body {
    padding: 16px 20px;
  }
}

.analysis-body[contenteditable="true"] {
  transition: background 200ms, box-shadow 200ms;
}
.analysis-body[contenteditable="true"]:focus {
  background: var(--bg);
  box-shadow: inset 0 4px 12px rgba(0,0,0,0.02);
}

.doc-actions {
  display: flex;
  gap: 12px;
  padding: 12px 24px;
  background: var(--bg);
  border-top: 1px solid var(--border);
}

/* Markdown 内容样式 */
.doc-body h1, .doc-body h2, .doc-body h3 {
  margin: 1.5em 0 0.5em;
  font-weight: 600;
  letter-spacing: -0.025em;
}
.doc-body h1 { font-size: 1.5rem; }
.doc-body h2 { font-size: 1.25rem; }
.doc-body h3 { font-size: 1.125rem; }
.doc-body p { margin: 0.75em 0; }
.doc-body p:first-child { margin-top: 0; }
.doc-body p:last-child { margin-bottom: 0; }
.doc-body ul, .doc-body ol { margin: 0.75em 0 0.75em 1.5em; padding: 0; }
.doc-body li { margin: 0.25em 0; }
.doc-body blockquote {
  margin: 1em 0;
  padding: 0.75em 1em;
  border-left: 4px solid var(--blockquote-border);
  background: var(--blockquote-bg);
  border-radius: 0 8px 8px 0;
  color: var(--muted);
}
.doc-body code {
  padding: 0.2em 0.4em;
  border-radius: 6px;
  background: var(--code-bg);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.9em;
}

/* 底部输入框 */
.composer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 0 16px 24px;
  background: linear-gradient(to top, var(--bg) 60%, transparent);
  pointer-events: none; /* 让渐变层不阻挡点击，子元素恢复 */
}

.composer-box {
  pointer-events: auto;
  max-width: 800px;
  margin: 0 auto;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--panel-2);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  padding: 16px;
  box-shadow: var(--shadow-lg);
  transition: all 300ms ease;
}
.composer-box:focus-within {
  border-color: var(--primary);
  box-shadow: 0 8px 30px var(--primary-glow);
}

.composer-input-wrap {
  position: relative;
}

textarea, input[type="text"], input[type="number"] {
  width: 100%;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  outline: none;
  font-size: 15px;
  font-family: inherit;
  transition: all 200ms ease;
}
textarea {
  resize: none;
  line-height: 1.6;
  border: none;
  background: transparent;
  padding: 4px;
}
textarea:focus {
  box-shadow: none;
}
input[type="text"]:focus, input[type="number"]:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-glow);
}

.composer-bottom {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-top: 12px;
  flex-wrap: wrap;
  gap: 12px;
}

/* 高级设置手风琴 */
details.advanced {
  position: relative;
}
details.advanced > summary {
  list-style: none;
  cursor: pointer;
  color: var(--muted);
  user-select: none;
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  transition: all 200ms ease;
}
details.advanced > summary::-webkit-details-marker {
  display: none;
}
details.advanced > summary:hover {
  background: var(--bg);
  color: var(--text);
}
.summary-content {
  display: flex;
  align-items: center;
  gap: 6px;
}
.caret {
  transition: transform 200ms ease;
}
details.advanced[open] .caret {
  transform: rotate(180deg);
}
details.advanced[open] > summary {
  color: var(--primary);
  background: var(--bubble-user);
}
.advanced-content {
  position: absolute;
  bottom: calc(100% + 12px);
  left: 0;
  width: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow-strong);
  animation: scaleIn 200ms cubic-bezier(0.16, 1, 0.3, 1);
  transform-origin: bottom left;
}
@media (max-width: 600px) {
  .advanced-content {
    width: 280px;
  }
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.field > span {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 8px;
}

.hint {
  margin-top: 16px;
  padding: 12px;
  border-radius: 10px;
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
  display: flex;
  gap: 8px;
  align-items: flex-start;
}
.hint i {
  color: var(--primary);
  font-size: 1.1rem;
  margin-top: 2px;
}

.actions {
  display: flex;
  gap: 12px;
  margin-left: auto;
}

button.primary, button.secondary {
  display: flex;
  align-items: center;
  gap: 6px;
  border-radius: 12px;
  padding: 10px 18px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: all 200ms ease;
  border: none;
}
button.primary {
  background: linear-gradient(135deg, var(--primary), var(--primary2));
  color: white;
  box-shadow: 0 4px 12px var(--primary-glow);
}
button.primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px var(--primary-glow);
}
button.secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}
button.secondary:hover:not(:disabled) {
  background: var(--border);
}
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}

.hidden {
  display: none !important;
}

/* 主题切换 */
.theme-toggle {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel-2);
  backdrop-filter: blur(8px);
  box-shadow: var(--shadow-lg);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  z-index: 100;
  transition: all 300ms ease;
}
.theme-toggle:hover {
  transform: translateY(-4px) rotate(15deg);
  border-color: var(--primary);
  color: var(--primary);
}
.theme-icon-dark { display: none; }
.theme-icon-light { display: block; }
:root[data-theme="dark"] .theme-icon-dark { display: block; }
:root[data-theme="dark"] .theme-icon-light { display: none; }

/* 动画 */
@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}
@keyframes pulse-border {
  0% { box-shadow: 0 0 0 0 var(--primary-glow); }
  70% { box-shadow: 0 0 0 6px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}
