:root {
  color-scheme: light;
  --bg: #f3f4f6;
  --panel: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --brand: #000000;
  --border: #e5e7eb;
  --active-bg: #f9fafb;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent; /* 消除移动端点击蓝色高亮 */
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh; /* 动态视口高度，防止移动端键盘/浏览器工具栏撑破布局 */
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.site-header {
  padding: 16px;
  text-align: center;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.site-header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 20px;
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  height: calc(100% - 53px); /* 减去 header 后的高度 */
  overflow: hidden;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 100%;
}

.panel-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: #f9fafb;
  flex-shrink: 0;
}

.panel-header h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.chat-title-area {
  display: flex;
  align-items: center;
  gap: 12px;
  overflow: hidden;
  flex: 1;
}

/* 默认在电脑端隐藏返回按钮 */
.back-btn {
  display: none;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  padding: 6px 8px 6px 4px;
  font-size: 14px;
  font-weight: 500;
  color: var(--brand);
  cursor: pointer;
  border-radius: 6px;
}

.back-btn:hover {
  background: var(--bg);
}

.chat-meta {
  overflow: hidden;
  flex: 1;
}

.chat-meta h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-meta p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.persona-list {
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.persona-card {
  text-align: left;
  background: #fff;
  border: 1px solid var(--border);
  padding: 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.persona-card:hover {
  background: var(--active-bg);
  border-color: #d1d5db;
}

.persona-card.active {
  background: #000;
  border-color: #000;
  color: #fff;
}

.persona-card.active p,
.persona-card.active .tag {
  color: rgba(255, 255, 255, 0.8);
}

.persona-card h3 {
  margin: 0 0 4px;
  font-size: 14px;
  font-weight: 600;
}

.persona-card p {
  margin: 0 0 8px;
  font-size: 12px;
  color: var(--muted);
}

.tags {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.tag {
  font-size: 10px;
  background: #f3f4f6;
  padding: 2px 6px;
  border-radius: 4px;
  color: #4b5563;
}

.persona-card.active .tag {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: #fff;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* 消息气泡基础样式 —— 修复 fadeInBubble 拼写（原为 "fadeIn Bubble"） */
.message {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 85%;
  animation: fadeInBubble 0.2s ease-out;
}

@keyframes fadeInBubble {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* 对方消息靠左 */
.message.bot {
  align-self: flex-start;
  align-items: flex-start;
}

/* 我方消息靠右 */
.message.user {
  align-self: flex-end;
  align-items: flex-end;
}

.message .meta {
  font-size: 11px;
  color: #a1a1aa;
  margin: 0 4px;
}

.bubble {
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.5;
  background: #f3f4f6;
  color: #1f2937;
  word-break: break-all;
}

/* 对方气泡圆角修饰 */
.message.bot .bubble {
  border-top-left-radius: 4px;
}

/* 我方气泡圆角修饰及配色 */
.message.user .bubble {
  background: var(--brand);
  color: #fff;
  border-top-right-radius: 4px;
}

.quick-replies {
  padding: 8px 16px 0;
  overflow-x: auto;
  flex-shrink: 0;
  -webkit-overflow-scrolling: touch;
}

.quick-buttons {
  display: flex;
  gap: 6px;
  padding-bottom: 4px;
  width: max-content;
}

.quick-buttons button {
  background: #fff;
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 100px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s ease;
}

.quick-buttons button:hover {
  border-color: #000;
  color: #000;
}

.chat-input {
  padding: 12px 16px 16px;
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.chat-input input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px; /* 防止 iOS 输入框聚焦时页面自动放大 */
  outline: none;
  transition: border-color 0.15s ease;
}

.chat-input input:focus {
  border-color: #000;
}

.chat-input button {
  background: #000;
  color: #fff;
  border: none;
  padding: 0 18px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: opacity 0.15s ease;
}

.chat-input button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  font-weight: 500;
}

.ghost-btn:hover {
  border-color: #000;
}

/* ==========================================
   3. 移动端适配核心规则 (平板及手机)
   ========================================== */
@media (max-width: 768px) {
  /* 隐藏桌面端大标题，省出高度给聊天区 */
  .site-header {
    display: none;
  }

  .layout {
    grid-template-columns: 1fr;
    padding: 0;
    gap: 0;
    height: 100%;
  }

  .panel {
    border: none;
    border-radius: 0;
    height: 100%;
    width: 100%;
  }

  /* 状态一：未激活聊天时，展示角色列表，隐藏聊天面板 */
  body:not(.chat-active) .panel.personas {
    display: flex;
  }
  body:not(.chat-active) .panel.chat {
    display: none;
  }

  /* 状态二：激活聊天时，显示聊天面板，隐藏角色面板 */
  body.chat-active .panel.personas {
    display: none;
  }
  body.chat-active .panel.chat {
    display: flex;
  }

  /* 显示返回按钮 */
  .back-btn {
    display: flex;
  }

  .chat-messages {
    padding: 12px;
  }

  .message {
    max-width: 90%;
  }

  .chat-input {
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); /* 适配刘海屏底部安全距离 */
  }
}