/* ═══════════════════════════════════════
   问答圣贤 — WeChat 风格样式
   ═══════════════════════════════════════ */

:root {
  --green: #07C160;
  --green-dark: #06ad56;
  --green-bubble: #95ec69;
  --bg: #ededed;
  --bg-white: #ffffff;
  --bg-header: #2e2e2e;
  --text: #333333;
  --text-secondary: #999999;
  --text-light: #666666;
  --border: #e5e5e5;
  --bubble-user: #95ec69;
  --bubble-sage: #ffffff;
  --radius: 8px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  width: 100%;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* ── 页面容器 ── */

.page {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: absolute;
  inset: 0;
  transition: transform .25s ease;
}

.page.hide { display: none; }

/* ── 顶栏 ── */

.header {
  background: var(--bg-header);
  color: #fff;
  height: 50px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}

.header-title {
  font-size: 17px;
  font-weight: 600;
  flex: 1;
  text-align: center;
}

.header-back {
  font-size: 28px;
  width: 40px;
  cursor: pointer;
  line-height: 1;
  user-select: none;
}

.header-avatar { font-size: 22px; margin-right: 8px; }

.header-actions {
  width: 40px;
  text-align: right;
  cursor: pointer;
  font-size: 20px;
  font-weight: 700;
  user-select: none;
}

.header-about {
  font-size: 14px;
  color: #fff;
  cursor: pointer;
  opacity: .8;
}

/* ── 圣贤列表 ── */

.list-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg-white);
}

.section-title {
  padding: 10px 16px 6px;
  font-size: 13px;
  color: var(--text-secondary);
  background: var(--bg);
  font-weight: 500;
}

.sage-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background .15s;
  -webkit-tap-highlight-color: transparent;
}

.sage-item:active { background: #f5f5f5; }

.sage-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  flex-shrink: 0;
  margin-right: 12px;
}

.sage-info { flex: 1; min-width: 0; }

.sage-name {
  font-size: 16px;
  font-weight: 500;
  color: var(--text);
}

.sage-desc {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sage-tag {
  font-size: 11px;
  color: var(--green);
  background: #ebfaf0;
  padding: 2px 8px;
  border-radius: 10px;
  margin-left: 8px;
  flex-shrink: 0;
}

/* ── 对话区域 ── */

.chat-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 12px calc(70px + var(--safe-bottom));
  background: var(--bg);
}

.chat-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-secondary);
  font-size: 14px;
}

/* 消息气泡 */

.msg {
  display: flex;
  margin-bottom: 16px;
  align-items: flex-start;
  gap: 8px;
}

.msg-user { flex-direction: row-reverse; }
.msg-sage { flex-direction: row; }

.msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  background: #f0f0f0;
}

.msg-user .msg-avatar { background: var(--green); }

.msg-body {
  max-width: 78%;
  display: flex;
  flex-direction: column;
}

.msg-bubble {
  padding: 10px 14px;
  font-size: 15px;
  line-height: 1.6;
  word-break: break-word;
  position: relative;
}

.msg-user .msg-bubble {
  background: var(--bubble-user);
  border-radius: 8px 2px 8px 8px;
}

.msg-sage .msg-bubble {
  background: var(--bubble-sage);
  border-radius: 2px 8px 8px 8px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}

.msg-bubble pre {
  background: #f5f5f5;
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 13px;
  overflow-x: auto;
  margin: 6px 0;
  font-family: "SF Mono", "Consolas", monospace;
}

.msg-bubble code {
  background: #f5f5f5;
  padding: 2px 4px;
  border-radius: 3px;
  font-size: 13px;
  font-family: "SF Mono", "Consolas", monospace;
}

.msg-bubble pre code { background: none; padding: 0; }

.msg-bubble p { margin: 4px 0; }
.msg-bubble p:first-child { margin-top: 0; }
.msg-bubble p:last-child { margin-bottom: 0; }

.msg-bubble ul, .msg-bubble ol { padding-left: 20px; margin: 4px 0; }
.msg-bubble blockquote {
  border-left: 3px solid var(--green);
  padding-left: 10px;
  color: var(--text-light);
  margin: 6px 0;
}

.msg-time {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 4px;
  padding: 0 4px;
}

.msg-user .msg-time { text-align: right; }
.msg-sage .msg-time { text-align: left; }

/* 思考中指示器 */
.typing-indicator {
  display: inline-flex;
  gap: 3px;
  padding: 4px 8px;
}

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

.typing-dot:nth-child(2) { animation-delay: .2s; }
.typing-dot:nth-child(3) { animation-delay: .4s; }

@keyframes typing {
  0%, 60%, 100% { opacity: .3; transform: scale(.8); }
  30% { opacity: 1; transform: scale(1); }
}

/* ── 底部输入 ── */

.chat-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg-white);
  border-top: 1px solid var(--border);
  padding: 8px 12px calc(8px + var(--safe-bottom));
  display: flex;
  align-items: flex-end;
  gap: 8px;
  z-index: 10;
}

.input-wrap {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  padding: 4px 8px;
}

.chat-input {
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  font-size: 16px;
  line-height: 1.5;
  max-height: 96px;
  resize: none;
  font-family: inherit;
  color: var(--text);
}

.chat-input::placeholder { color: var(--text-secondary); }
.chat-input:disabled { opacity: .5; }

.send-btn, .stop-btn {
  height: 38px;
  padding: 0 16px;
  border: none;
  border-radius: 6px;
  font-size: 15px;
  cursor: pointer;
  font-weight: 500;
  flex-shrink: 0;
  white-space: nowrap;
}

.send-btn {
  background: var(--green);
  color: #fff;
}

.send-btn:disabled { opacity: .4; cursor: not-allowed; }
.send-btn:active:not(:disabled) { background: var(--green-dark); }

.stop-btn {
  background: #ff4d4f;
  color: #fff;
}

.stop-btn:active { background: #d9363e; }

.hide { display: none !important; }

/* ── 操作菜单 ── */

.action-menu {
  position: fixed;
  top: 50px;
  right: 8px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,.15);
  z-index: 20;
  min-width: 140px;
  overflow: hidden;
}

.action-item {
  padding: 14px 18px;
  font-size: 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .15s;
}

.action-item:last-child { border-bottom: none; }
.action-item:active { background: #f5f5f5; }
.action-item.danger { color: #ff4d4f; }

/* ── 弹窗 ── */

.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
}

.modal {
  background: #fff;
  border-radius: 12px;
  max-width: 320px;
  width: 100%;
  overflow: hidden;
}

.modal-title {
  text-align: center;
  padding: 18px 16px 4px;
  font-size: 17px;
  font-weight: 600;
}

.modal-body { padding: 12px 20px 20px; font-size: 14px; line-height: 1.7; color: var(--text-light); }
.modal-footer { border-top: 1px solid var(--border); }

.modal-btn {
  width: 100%;
  padding: 14px;
  border: none;
  background: none;
  font-size: 16px;
  color: var(--green);
  cursor: pointer;
  font-weight: 500;
}

.modal-btn:active { background: #f5f5f5; }

/* ── Toast 提示 ── */

.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,.75);
  color: #fff;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 200;
  max-width: 80%;
  text-align: center;
  pointer-events: none;
}

/* ── 加载遮罩 ── */

.loading-mask {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 300;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: 14px;
  color: var(--text-secondary);
}

.loading-spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

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

/* ── 错误消息 ── */

.error-bubble .msg-bubble {
  background: #fff2f0 !important;
  color: #cf1322;
}

/* ── 桌面端适配 ── */

@media (min-width: 768px) {
  .page { max-width: 480px; margin: 0 auto; box-shadow: 0 0 20px rgba(0,0,0,.06); }
  .chat-footer { max-width: 480px; left: 50%; transform: translateX(-50%); }
  .action-menu { right: calc(50% - 232px); }
}

/* ── Markdown 表格等额外样式 ── */

.msg-bubble table {
  border-collapse: collapse;
  margin: 6px 0;
  font-size: 13px;
  width: 100%;
}

.msg-bubble th, .msg-bubble td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.msg-bubble th { background: #f5f5f5; font-weight: 600; }
.msg-bubble hr { border: none; border-top: 1px solid var(--border); margin: 8px 0; }
.msg-bubble a { color: var(--green); text-decoration: underline; }
.msg-bubble img { max-width: 100%; border-radius: 4px; }
