:root {
  --bg-primary: #0f0f10;
  --bg-secondary: #1a1a1d;
  --bg-card: #232326;
  --bg-hover: #2a2a2e;
  --accent: #7c6af7;
  --accent-light: #9d8ff9;
  --accent-dim: rgba(124, 106, 247, 0.15);
  --text-primary: #f0eff4;
  --text-secondary: #a09db8;
  --text-muted: #6b6884;
  --border: #2e2e33;
  --border-light: #3a3a42;
  --success: #22c55e;
  --error: #ef4444;
  --warning: #f59e0b;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 24px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --transition: 0.2s ease;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  /* dvh 让高度跟随移动端可视区动态变化，兼容旧浏览器降级到 vh */
  height: -webkit-fill-available;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  height: 100dvh; /* 动态视口高度，iOS Safari 15.4+、Chrome 108+ 支持 */
  overflow: hidden;
  /* 防止 iOS 上双击缩放 */
  touch-action: manipulation;
}

.app-container {
  display: flex;
  height: 100%;
}

/* ===== 侧边栏 ===== */
.sidebar {
  width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  gap: 20px;
  flex-shrink: 0;
}

.sidebar-header {
  padding: 4px 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}

.logo-img {
  width: 26px;
  height: 26px;
  object-fit: contain;
}

/* Notebook 选择 */
.notebook-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.notebook-selector {
  display: flex;
  gap: 6px;
  align-items: center;
}

select {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 8px 10px;
  font-size: 13px;
  cursor: pointer;
  outline: none;
  transition: border-color var(--transition);
}

select:hover, select:focus {
  border-color: var(--accent);
}

select option {
  background: var(--bg-card);
}

.notebook-id-input {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.notebook-id-input label {
  font-size: 11px;
  color: var(--text-muted);
}

input[type="text"] {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 8px 10px;
  font-size: 13px;
  width: 100%;
  outline: none;
  transition: border-color var(--transition);
}

input[type="text"]:focus {
  border-color: var(--accent);
}

input[type="text"]::placeholder {
  color: var(--text-muted);
}

.icon-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--accent);
  border-color: var(--accent);
}

.icon-btn svg {
  width: 15px;
  height: 15px;
}

/* 新对话按钮 */
.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--accent-dim);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  color: var(--accent-light);
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  width: 100%;
  transition: all var(--transition);
}

.new-chat-btn:hover {
  background: var(--accent);
  color: white;
}

.new-chat-btn svg {
  width: 16px;
  height: 16px;
}

/* 状态指示器 */
.sidebar-footer {
  margin-top: auto;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bg-card);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
  transition: background var(--transition);
}

.status-dot.connected {
  background: var(--success);
  box-shadow: 0 0 6px var(--success);
}

.status-dot.error {
  background: var(--error);
}

.status-text {
  font-size: 12px;
  color: var(--text-muted);
}

/* ===== 主区域 ===== */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-primary);
  /* 确保自身不超出父容器 */
  min-height: 0;
}

.chat-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(15, 15, 16, 0.8);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-actions {
  display: flex;
  align-items: center;
}

.lang-select {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  padding: 5px 8px;
  font-size: 12px;
  cursor: pointer;
  outline: none;
  transition: border-color var(--transition);
}

.lang-select:hover, .lang-select:focus {
  border-color: var(--accent);
  color: var(--text-primary);
}

.lang-select option {
  background: var(--bg-card);
}

.chat-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-logo {
  width: 28px;
  height: 28px;
  object-fit: contain;
}

.chat-title h1 {
  font-size: 17px;
  font-weight: 600;
  color: var(--text-primary);
}

.notebook-badge {
  font-size: 12px;
  background: var(--accent-dim);
  color: var(--accent-light);
  border: 1px solid rgba(124, 106, 247, 0.3);
  border-radius: 100px;
  padding: 3px 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

/* ===== 消息区域 ===== */
.messages-container {
  flex: 1;
  overflow-y: auto;
  /* min-height: 0 是关键，防止 flex 子元素撑破容器 */
  min-height: 0;
  padding: 24px 0;
  scroll-behavior: smooth;
  /* iOS Safari 惯性滚动 */
  -webkit-overflow-scrolling: touch;
}

.messages-container::-webkit-scrollbar {
  width: 4px;
}

.messages-container::-webkit-scrollbar-track {
  background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* 欢迎屏幕 */
.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 40px 20px;
  text-align: center;
  gap: 16px;
}

.welcome-logo {
  width: 110px;
  height: 110px;
  object-fit: contain;
  opacity: 0.9;
}

.welcome-screen h2 {
  font-size: 22px;
  font-weight: 600;
  color: var(--text-primary);
}

.welcome-screen > p {
  color: var(--text-secondary);
  font-size: 15px;
  max-width: 380px;
}

.example-questions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
  width: 100%;
  max-width: 400px;
}

.examples-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 4px;
}

.example-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  padding: 10px 16px;
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  transition: all var(--transition);
}

.example-btn:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
  color: var(--text-primary);
}

/* 消息气泡 */
.message-group {
  max-width: 760px;
  margin: 0 auto;
  padding: 6px 28px;
  animation: fadeIn 0.25s ease;
}

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

.message {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.message.user {
  flex-direction: row-reverse;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  flex-shrink: 0;
}

.user .avatar {
  background: var(--accent);
  color: white;
}

.assistant .avatar {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--accent);
}

.assistant .avatar svg {
  width: 17px;
  height: 17px;
}

.bubble {
  max-width: calc(100% - 50px);
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  font-size: 14.5px;
  line-height: 1.65;
  color: var(--text-primary);
}

.user .bubble {
  background: var(--accent);
  color: white;
  border-bottom-right-radius: var(--radius-sm);
}

.assistant .bubble {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-bottom-left-radius: var(--radius-sm);
}

/* ===== Markdown 渲染样式 ===== */
.markdown-body {
  line-height: 1.7;
}

.markdown-body p {
  margin: 0 0 10px;
}

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

.markdown-body h1, .markdown-body h2, .markdown-body h3,
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
  font-weight: 600;
  margin: 16px 0 8px;
  line-height: 1.4;
  color: var(--text-primary);
}

.markdown-body h1 { font-size: 1.3em; }
.markdown-body h2 { font-size: 1.15em; }
.markdown-body h3 { font-size: 1.05em; }

.markdown-body ul, .markdown-body ol {
  margin: 6px 0 10px;
  padding-left: 20px;
}

.markdown-body li {
  margin: 4px 0;
}

.markdown-body li > ul, .markdown-body li > ol {
  margin: 2px 0 4px;
}

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

.markdown-body em {
  font-style: italic;
  color: var(--text-secondary);
}

.markdown-body code {
  background: rgba(124, 106, 247, 0.12);
  border: 1px solid rgba(124, 106, 247, 0.2);
  border-radius: 4px;
  padding: 1px 6px;
  font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  font-size: 0.88em;
  color: var(--accent-light);
}

.markdown-body pre {
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  overflow-x: auto;
  margin: 10px 0;
}

.markdown-body pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.85em;
  color: var(--text-primary);
}

.markdown-body blockquote {
  border-left: 3px solid var(--accent);
  padding: 4px 0 4px 14px;
  margin: 10px 0;
  color: var(--text-secondary);
  font-style: italic;
}

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

.markdown-body th, .markdown-body td {
  border: 1px solid var(--border);
  padding: 7px 12px;
  text-align: left;
}

.markdown-body th {
  background: var(--bg-hover);
  font-weight: 600;
}

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

.markdown-body a {
  color: var(--accent-light);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.markdown-body a:hover {
  color: white;
}

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

/* 引用来源 */
.citations {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.citation {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-secondary);
}

.citation-source {
  font-weight: 600;
  color: var(--accent-light);
  margin-bottom: 3px;
}

/* 加载状态 */
.loading-bubble {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 14px 18px;
}

.loading-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite;
}

.loading-dot:nth-child(2) { animation-delay: 0.2s; }
.loading-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-6px); }
}

/* ===== 输入区域 ===== */
.input-area {
  padding: 16px 28px 20px;
  /* 安全区适配：iPhone 底部 Home Bar */
  padding-bottom: max(20px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: rgba(15, 15, 16, 0.9);
  backdrop-filter: blur(12px);
  /* 防止被压缩 */
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 10px 12px 10px 16px;
  transition: border-color var(--transition);
}

.input-wrapper:has(textarea:focus) {
  border-color: var(--accent);
}

textarea {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 14.5px;
  line-height: 1.5;
  resize: none;
  outline: none;
  max-height: 140px;
  overflow-y: auto;
  font-family: inherit;
}

textarea::placeholder {
  color: var(--text-muted);
}

textarea::-webkit-scrollbar {
  width: 3px;
}

textarea::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.input-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.voice-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: transparent;
  border: 1px solid var(--border-light);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
}

.voice-btn:hover {
  background: var(--bg-hover);
  color: var(--accent);
  border-color: var(--accent);
}

.voice-btn.recording {
  background: var(--error);
  border-color: var(--error);
  color: white;
  animation: pulse 1.5s infinite;
}

.voice-btn.cancel-hint {
  background: var(--text-muted);
  border-color: var(--text-muted);
  animation: none;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
  50% { box-shadow: 0 0 0 8px rgba(239, 68, 68, 0); }
}

.voice-btn svg {
  width: 17px;
  height: 17px;
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
}

.send-btn:hover {
  background: var(--accent-light);
  transform: scale(1.05);
}

.send-btn:disabled {
  background: var(--bg-hover);
  color: var(--text-muted);
  cursor: not-allowed;
  transform: none;
}

.send-btn svg {
  width: 16px;
  height: 16px;
}

/* 语音状态条 */
.voice-status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  margin-top: 8px;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: #f87171;
}

.recording-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--error);
  animation: blink 1s infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.input-hint {
  font-size: 11.5px;
  color: var(--text-muted);
  margin-top: 8px;
  text-align: center;
}

/* 通用 */
.hidden {
  display: none !important;
}

/* 错误消息 */
.error-bubble {
  background: rgba(239, 68, 68, 0.1) !important;
  border-color: rgba(239, 68, 68, 0.3) !important;
  color: #f87171 !important;
}

/* ===== 语音播报按钮 ===== */
.speak-btn-row {
  margin-left: 44px;
  margin-top: 4px;
  margin-bottom: 2px;
}

.speak-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 0;
  transition: background 0.15s, color 0.15s;
}

.speak-btn svg {
  width: 15px;
  height: 15px;
}

.speak-btn:hover {
  background: var(--bg-card);
  color: var(--accent);
}

.speak-btn.speaking {
  color: var(--accent);
  animation: pulse-speak 1.4s ease-in-out infinite;
}

@keyframes pulse-speak {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* 响应式 */
@media (max-width: 640px) {
  .sidebar {
    display: none;
  }
  .message-group {
    padding: 6px 16px;
  }
  .input-area {
    padding: 12px 16px 16px;
  }
  .chat-header {
    padding: 14px 16px;
  }
}
