:root {
  --bg: #f4f5fb;
  --card: #ffffff;
  --text: #1c1d22;
  --muted: #8a8d9b;
  --brand: #6d5efc;
  --brand-2: #9a8dff;
  --user-bubble: linear-gradient(135deg, #6d5efc, #8b7bff);
  --ai-bubble: #ffffff;
  --border: #ececf3;
  --shadow: 0 6px 24px rgba(40, 38, 80, 0.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1014;
    --card: #1a1b21;
    --text: #eceef5;
    --muted: #8b8f9e;
    --ai-bubble: #1f2128;
    --border: #2a2c34;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
/* 滚动容器：iOS 惯性滚动 + 防止聊天区下拉刷新/滚动穿透 */
.messages, .session-list, .panel-body, .model-card, .request-body, .think-body {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
#app {
  display: flex; flex-direction: column;
  height: 100dvh;
  max-width: 720px; margin: 0 auto;
  background: var(--bg);
}

/* 顶栏 */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff; font-weight: 700; font-size: 18px;
  display: grid; place-items: center;
}
.title { flex: 1; }
.title .name { font-weight: 650; font-size: 16px; }
.title .status { font-size: 12px; color: var(--muted); margin-top: 2px; }
.logout {
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  padding: 6px 12px; border-radius: 999px; font-size: 13px; cursor: pointer;
}
.expiry {
  font-size: 12px; color: var(--muted); white-space: nowrap;
  padding: 4px 8px; border-radius: 999px; background: var(--bg);
}

/* 登录/注册 */
.auth {
  position: absolute; inset: 0; z-index: 20;
  display: grid; place-items: center;
  background: var(--bg);
  padding: 20px;
}
.renew {
  position: absolute; inset: 0; z-index: 22;
  display: grid; place-items: center;
  background: var(--bg);
  padding: 20px;
}
.renew .auth-card h2 { font-size: 20px; margin: 0 0 4px; }
.renew input {
  width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 15px; outline: none; margin-bottom: 6px;
}
.renew input:focus { border-color: var(--brand); }
.buy {
  position: absolute; inset: 0; z-index: 22;
  display: grid; place-items: center;
  background: var(--bg);
  padding: 20px;
}
.buy .auth-card h2 { font-size: 20px; margin: 0 0 4px; }
.plans { display: flex; flex-direction: column; gap: 10px; margin: 6px 0; }
.plan {
  padding: 13px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--card); color: var(--text); font-size: 15px; cursor: pointer; text-align: center;
}
.plan:active { border-color: var(--brand); }
.qrcode { margin: 8px 0; }
.qrcode canvas { border: 1px solid var(--border); border-radius: 12px; padding: 8px; background: #fff; }
.auth-card {
  width: 100%; max-width: 340px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: 20px; padding: 28px 22px; text-align: center;
  box-shadow: var(--shadow);
}
.auth-logo {
  width: 56px; height: 56px; border-radius: 16px; margin: 0 auto 14px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff; font-weight: 800; font-size: 26px; display: grid; place-items: center;
}
.auth-card h1 { font-size: 20px; margin: 0 0 4px; }
.auth-sub { color: var(--muted); font-size: 13px; margin: 0 0 18px; }
.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tab {
  flex: 1; padding: 9px; border-radius: 12px; border: 1px solid var(--border);
  background: transparent; color: var(--muted); font-size: 14px; cursor: pointer;
}
.tab.active { background: var(--brand); color: #fff; border-color: var(--brand); }
#authForm { display: flex; flex-direction: column; gap: 10px; }
#authForm input {
  padding: 13px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 15px; outline: none;
}
#authForm input:focus { border-color: var(--brand); }
.primary {
  margin-top: 6px; padding: 13px; border: none; border-radius: 12px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff; font-size: 15px; font-weight: 650; cursor: pointer;
}
.ghost {
  margin-top: 8px; padding: 12px; border: 1px solid var(--border); border-radius: 12px;
  background: transparent; color: var(--muted); font-size: 15px; cursor: pointer; width: 100%;
}
.auth-msg { min-height: 18px; margin-top: 12px; font-size: 13px; color: #e5484d; }

/* 聊天 */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.messages {
  flex: 1; overflow-y: auto; padding: 16px 14px calc(env(safe-area-inset-bottom) + 8px);
  display: flex; flex-direction: column; gap: 10px;
}
.row { display: flex; gap: 8px; align-items: flex-end; }
.row.user { flex-direction: row-reverse; }
.bubble {
  max-width: 78%; padding: 10px 14px; border-radius: 16px; font-size: 15px;
  line-height: 1.5; word-wrap: break-word; white-space: pre-wrap;
}
.row.ai .bubble { background: var(--ai-bubble); border: 1px solid var(--border); border-bottom-left-radius: 5px; }
.row.user .bubble { background: var(--user-bubble); color: #fff; border-bottom-right-radius: 5px; }
/* 新消息入场动画（轻微上浮淡入） */
.row { animation: msg-in 0.22s ease both; }
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
/* 气泡按压反馈 */
.row.user .bubble:active, .row.ai .bubble:active { transform: scale(0.98); }
.bubble.thinking { color: var(--muted); font-style: italic; }
.tool {
  align-self: flex-start; font-size: 12px; color: var(--muted);
  background: var(--card); border: 1px solid var(--border);
  padding: 4px 10px; border-radius: 999px; margin: 2px 0;
}

/* 输入条 */
.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 12px calc(env(safe-area-inset-bottom) + 10px);
  background: var(--card); border-top: 1px solid var(--border);
}
#input {
  flex: 1; resize: none; max-height: 120px; padding: 11px 14px;
  border-radius: 18px; border: 1px solid var(--border); background: var(--bg);
  color: var(--text); font-size: 15px; outline: none; line-height: 1.4;
  font-family: inherit;
}
#input:focus { border-color: var(--brand); }
/* iOS Safari：输入框字号 <16px 会在聚焦时自动放大页面，统一提到 16px */
@media (max-width: 768px) {
  #input, #authForm input, .renew input, .req-input, .rename-input {
    font-size: 16px;
  }
}
#sendBtn {
  width: 42px; height: 42px; flex: 0 0 auto; border: none; border-radius: 50%;
  background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff;
  font-size: 20px; cursor: pointer; display: grid; place-items: center;
}
#sendBtn:disabled { opacity: 0.5; }

.toast {
  position: absolute; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 80px);
  transform: translateX(-50%); background: rgba(20,20,28,0.92); color: #fff;
  padding: 10px 16px; border-radius: 12px; font-size: 13px; z-index: 30;
  max-width: 80%; text-align: center;
}
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 5px; background: #c7c9d4; vertical-align: middle; }
.dot.on { background: #2ec27e; }
.dot.err { background: #e5484d; }

/* 顶栏菜单按钮 */
.menu {
  border: none; background: transparent; color: var(--text);
  font-size: 20px; line-height: 1; cursor: pointer; padding: 4px 6px;
  border-radius: 8px;
}
.menu:active { background: var(--bg); }

/* 会话抽屉 */
.overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35);
  z-index: 30; border: none;
}
.session-panel {
  position: fixed; left: 0; top: 0; bottom: 0; width: min(82vw, 320px);
  background: var(--card); z-index: 31;
  display: flex; flex-direction: column;
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.12);
  animation: slide-in 0.22s ease;
}
@keyframes slide-in {
  from { transform: translateX(-100%); }
  to { transform: translateX(0); }
}
.session-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 12px;
  border-bottom: 1px solid var(--border);
}
.session-title { font-weight: 650; font-size: 16px; }
.new-session {
  border: none; border-radius: 999px; padding: 6px 12px; font-size: 13px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff; cursor: pointer; font-weight: 600;
}
.session-list {
  flex: 1; overflow-y: auto; padding: 8px;
  padding-bottom: calc(env(safe-area-inset-bottom) + 8px);
}
.session-item {
  display: flex; align-items: center; gap: 10px;
  padding: 12px; border-radius: 12px; cursor: pointer;
}
.session-item:active { background: var(--bg); }
.session-item.active { background: var(--bg); }
.session-info { flex: 1; min-width: 0; }
.session-name {
  font-size: 14px; font-weight: 600; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.session-meta {
  font-size: 12px; color: var(--muted); margin-top: 3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.session-time { font-size: 12px; color: var(--muted); flex: 0 0 auto; }
.session-del {
  border: none; background: transparent; color: #e5484d;
  font-size: 12px; cursor: pointer; padding: 4px 6px; flex: 0 0 auto;
}
.session-empty {
  text-align: center; color: var(--muted); font-size: 13px;
  padding: 40px 0;
}

/* 顶栏模型标签 */
.model-chip {
  border: 1px solid var(--border); background: var(--bg); color: var(--muted);
  padding: 4px 10px; border-radius: 999px; font-size: 12px; cursor: pointer;
  max-width: 120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* 模型选择弹窗 */
.model {
  position: absolute; inset: 0; z-index: 22;
  display: grid; place-items: center;
  background: var(--bg); padding: 20px;
}
.model .auth-card h2 { font-size: 20px; margin: 0 0 4px; }
.model-card { max-height: 80vh; overflow-y: auto; }
.model-list { display: flex; flex-direction: column; gap: 10px; margin: 6px 0; text-align: left; }
.model-group { display: flex; flex-direction: column; gap: 6px; }
.model-provider { font-size: 12px; color: var(--muted); font-weight: 600; }
.model-item {
  padding: 11px 13px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--card); color: var(--text); font-size: 14px; cursor: pointer; text-align: left;
}
.model-item.current { border-color: var(--brand); color: var(--brand); font-weight: 600; }

/* 输入条工具按钮（附件/麦克风） */
.tool-btn {
  width: 40px; height: 40px; flex: 0 0 auto; border: none; border-radius: 50%;
  background: transparent; font-size: 18px; cursor: pointer; line-height: 1;
  display: grid; place-items: center; color: var(--muted);
}
.tool-btn:active { background: var(--bg); }
.tool-btn.recording {
  background: #e5484d; color: #fff; animation: pulse 1s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* 附件标签 */
.attachments {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 8px 12px 0; background: var(--card);
}
.attachment-chip {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 999px; padding: 4px 8px;
}
.attachment-x {
  border: none; background: transparent; color: var(--muted);
  font-size: 14px; cursor: pointer; padding: 0 2px; line-height: 1;
}

/* 审批/澄清/密钥 请求卡片 */
.request-card {
  max-width: 88%; background: var(--card); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 14px;
  border-left: 3px solid var(--brand);
  box-shadow: var(--shadow);
}
.request-title { font-size: 14px; font-weight: 650; color: var(--text); margin-bottom: 4px; }
.request-body {
  font-size: 13px; color: var(--muted); line-height: 1.5;
  white-space: pre-wrap; word-break: break-all; margin-bottom: 8px;
  max-height: 140px; overflow-y: auto;
}
.request-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.req-btn {
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  padding: 7px 14px; border-radius: 999px; font-size: 13px; cursor: pointer;
}
.req-btn.deny { color: #e5484d; border-color: #e5484d; }
.req-btn:disabled { opacity: 0.5; }
.req-input {
  flex: 1; min-width: 120px; padding: 8px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  font-size: 14px; outline: none;
}
.req-input:focus { border-color: var(--brand); }

/* 发送按钮停止态 */
#sendBtn.stop {
  background: #e5484d;
}

/* 消息包裹 + 操作按钮 */
.msg-wrap { max-width: 82%; display: flex; flex-direction: column; gap: 3px; }
.msg-wrap .bubble { max-width: 100%; }
.msg-meta {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  font-size: 11px; color: var(--muted);
}
.meta-btn {
  border: none; background: transparent; color: var(--muted);
  font-size: 11px; cursor: pointer; padding: 2px 4px;
}
.meta-btn:active { color: var(--brand); }
.meta-usage { opacity: 0.8; }

/* 思考过程折叠区 */
.think {
  font-size: 12px; color: var(--muted);
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 6px 10px;
}
.think summary { cursor: pointer; user-select: none; }
.think-body {
  margin-top: 6px; white-space: pre-wrap; line-height: 1.5;
  max-height: 200px; overflow-y: auto; color: var(--muted);
}

/* Markdown 渲染 */
.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; overflow-x: auto; margin: 8px 0;
}
.bubble code {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
}
.bubble pre code { background: none; border: none; padding: 0; }
.bubble ul, .bubble ol { padding-left: 20px; margin: 4px 0; }
.bubble table { border-collapse: collapse; margin: 8px 0; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 4px 8px; font-size: 13px; }
.bubble blockquote {
  border-left: 3px solid var(--border); margin: 6px 0; padding-left: 10px; color: var(--muted);
}

/* 会话抽屉操作按钮 */
.session-head-actions { display: flex; align-items: center; gap: 6px; }
.new-session.ghost2 {
  background: transparent; color: var(--muted);
  border: 1px solid var(--border);
}
.session-actions { display: flex; gap: 8px; }
.session-op {
  border: none; background: transparent; color: var(--muted);
  font-size: 12px; cursor: pointer; padding: 2px 4px;
}
.session-op.danger { color: #e5484d; }
.rename-input {
  width: 100%; padding: 4px 8px; border-radius: 6px;
  border: 1px solid var(--brand); background: var(--bg); color: var(--text);
  font-size: 14px; font-weight: 600; outline: none;
}

/* 多 tab 导航 */
.panel-tabs {
  display: flex; gap: 4px; padding: calc(env(safe-area-inset-top) + 10px) 12px 0;
  border-bottom: 1px solid var(--border);
}
.panel-tab {
  flex: 1; padding: 9px 0; border: none; background: transparent;
  color: var(--muted); font-size: 14px; cursor: pointer;
  border-bottom: 2px solid transparent;
}
.panel-tab.active {
  color: var(--brand); border-bottom-color: var(--brand); font-weight: 600;
}
.panel-body { flex: 1; overflow-y: auto; min-height: 0; }
.panel-page { display: flex; flex-direction: column; min-height: 0; }

/* 创建框（Agent/任务） */
.create-box {
  display: flex; gap: 8px; align-items: center; padding: 12px;
  border-bottom: 1px solid var(--border);
}
.create-box.tall {
  flex-direction: column; align-items: stretch; gap: 8px;
}
.create-box .req-input { flex: 1; }
.create-box textarea.req-input { resize: vertical; min-height: 60px; border-radius: 10px; padding: 10px 12px; }

/* 设置区 */
.settings-body { padding: 12px; display: flex; flex-direction: column; gap: 16px; }
.settings-section {
  display: flex; flex-direction: column; gap: 8px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 14px; padding: 14px;
}
.settings-title { font-size: 14px; font-weight: 650; color: var(--text); }
.settings-section .req-input { width: 100%; }
.settings-hint { font-size: 12px; color: var(--muted); line-height: 1.5; }

/* ================= 移动端美化（窄屏适配） ================= */
/* 窄屏顶栏：头像与长标题让位，按钮压缩为图标 */
@media (max-width: 520px) {
  .topbar { gap: 8px; padding-left: 12px; padding-right: 12px; }
  .topbar .avatar { display: none; }
  .title .name { font-size: 15px; max-width: 34vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .model-chip { max-width: 88px; padding: 4px 8px; font-size: 11px; }
  .logout { padding: 5px 10px; font-size: 12px; }
  .expiry { font-size: 11px; padding: 3px 6px; }
}
@media (max-width: 380px) {
  .title .name { max-width: 26vw; }
  .model-chip { max-width: 72px; }
  .logout { padding: 5px 8px; }
}

/* 超窄屏：模型标签降级为纯图标符号 */
@media (max-width: 340px) {
  .model-chip { font-size: 0; }
  .model-chip::before { content: '🧠'; font-size: 15px; }
}

/* 输入条在超窄屏下压缩工具按钮间距 */
@media (max-width: 380px) {
  .composer { gap: 6px; padding-left: 8px; padding-right: 8px; }
  .tool-btn { width: 38px; height: 38px; }
}

/* 抽屉宽度下限，避免文字换行异常 */
.session-panel { width: min(86vw, 320px); }

/* 用户消息行增加与左侧消息的视觉区分（行距） */
.messages { scroll-behavior: smooth; }

/* 尊重系统"减弱动态效果"偏好 */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
