* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #f5f6fa;
  --sidebar: #1e2a3a;
  --sidebar-text: #aab8c9;
  --accent: #2f6fed;
  --accent-hover: #1e56c9;
  --msg-user: #2f6fed;
  --msg-ai: #ffffff;
  --border: #e5e8ef;
  --text: #1f2937;
  --text-light: #6b7280;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

#app { display: flex; height: 100vh; }

/* ---------- 侧边栏 ---------- */
#sidebar {
  width: 240px;
  background: var(--sidebar);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.logo-icon { font-size: 32px; }
.logo-text h1 { font-size: 16px; color: #fff; font-weight: 600; }
.logo-text span { font-size: 11px; opacity: 0.6; }

.btn-new {
  margin: 16px;
  padding: 10px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s;
}
.btn-new:hover { background: var(--accent-hover); }

#session-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px;
}
.session-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  margin-bottom: 2px;
  transition: background 0.2s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session-item:hover { background: rgba(255,255,255,0.08); }
.session-item.active { background: var(--accent); color: #fff; }

.sidebar-footer { padding: 12px; border-top: 1px solid rgba(255,255,255,0.08); }
.btn-ghost {
  width: 100%;
  padding: 8px;
  background: transparent;
  color: var(--sidebar-text);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.btn-ghost:hover { background: rgba(255,255,255,0.08); }

/* ---------- 主区域 ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; } /* 【修复】min-height:0 让内部 #messages overflow 生效 */

#chat-header {
  height: 56px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}
#chat-title { font-size: 15px; font-weight: 600; }
.header-right { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-light); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #ccc; }
.dot.online { background: #22c55e; }
.dot.offline { background: #ef4444; }

/* ---------- 消息区 ---------- */
#messages {
  flex: 1;
  min-height: 0;   /* 【修复】flex 子项必须 min-height:0 否则 overflow 失效，消息区无法滚动 */
  overflow-y: auto;
  padding: 20px 24px;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
}
#messages > .tool-status-line, #messages > .thinking { width: 100%; max-width: 860px; }

.msg {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
  max-width: 860px;
  width: fit-content;
  min-width: 80px;
  align-items: flex-start;
}
.msg.user { max-width: min(860px, calc(100% - 40px)); }
.msg.ai { max-width: min(860px, calc(100% - 40px)); }
.msg.user { flex-direction: row-reverse; margin-left: auto; }
.msg .avatar {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}
.msg.user .avatar { background: var(--accent); }
.msg.ai .avatar { background: #eef2ff; }

.msg .bubble {
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.7;
  word-break: break-word;
  white-space: pre-wrap;
  max-width: 100%;
}
.msg.user .bubble { background: var(--msg-user); color: #fff; border-top-right-radius: 4px; }
.msg.ai .bubble { background: var(--msg-ai); border: 1px solid var(--border); border-top-left-radius: 4px; }

.msg.ai .bubble pre {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  font-size: 13px;
  margin: 8px 0;
}
.msg.ai .bubble code { font-family: "JetBrains Mono", Consolas, monospace; }
.msg.ai .bubble table {
  border-collapse: collapse;
  margin: 8px 0;
  max-width: 100%;
  display: block;
  overflow-x: auto;
  white-space: nowrap;
  font-size: 13px;
}
.msg.ai .bubble th, .msg.ai .bubble td { border: 1px solid var(--border); padding: 6px 12px; }
.msg.ai .bubble h1, .msg.ai .bubble h2, .msg.ai .bubble h3,
.msg.ai .bubble h4, .msg.ai .bubble h5 { margin: 10px 0 6px; line-height: 1.4; }
.msg.ai .bubble h1 { font-size: 18px; }
.msg.ai .bubble h2 { font-size: 17px; }
.msg.ai .bubble h3 { font-size: 16px; }
.msg.ai .bubble h4 { font-size: 15px; }
.msg.ai .bubble h5 { font-size: 14px; }
.msg.ai .bubble ul, .msg.ai .bubble ol { margin: 6px 0 6px 20px; }
.msg.ai .bubble li { margin: 3px 0; }
.msg.ai .bubble blockquote {
  margin: 8px 0;
  padding: 4px 12px;
  border-left: 3px solid var(--accent);
  background: #f8fafc;
  color: var(--text-light);
}
.msg.ai .bubble a { color: var(--accent); }
.msg.ai .bubble code { background: #f1f5f9; padding: 1px 5px; border-radius: 4px; font-size: 12px; }

.thinking {
  background: #f8fafc;
  border-left: 3px solid var(--accent);
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 12px;
  color: var(--text-light);
  margin-bottom: 8px;
  white-space: pre-wrap;
}

/* ---------- 输入区 ---------- */
#input-area {
  border-top: 1px solid var(--border);
  background: #fff;
  padding: 12px 24px 16px;
}
#model-selector { margin-bottom: 8px; }
#model-select {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
}
#input-row { display: flex; gap: 10px; align-items: flex-end; }
#attach-tools { display: flex; gap: 4px; flex-direction: column; flex-shrink: 0; }
.attach-btn { text-align: left; }
.attach-btn { padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; cursor: pointer; font-size: 12px; color: var(--text-light); white-space: nowrap; transition: all 0.2s; }
.attach-btn:hover { border-color: var(--accent); color: var(--accent); background: #f0f4ff; }
#pending-files { display: flex; gap: 8px; padding: 8px 0; flex-wrap: wrap; }
.pending-file { background: #f0f4ff; border: 1px solid #c7d6f5; border-radius: 8px; padding: 4px 10px; font-size: 12px; color: #2f6fed; display: flex; align-items: center; gap: 6px; }
.pending-file img { width: 36px; height: 36px; object-fit: cover; border-radius: 4px; }
.pending-file .remove { cursor: pointer; color: #ef4444; font-weight: bold; }
#input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 14px;
  resize: none;
  outline: none;
  font-family: inherit;
  max-height: 150px;
  line-height: 1.5;
}
#input:focus { border-color: var(--accent); }
.btn-send {
  padding: 12px 24px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s;
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { background: #9db8f0; cursor: not-allowed; }
.input-tip { text-align: center; font-size: 11px; color: var(--text-light); margin-top: 8px; }

/* ---------- 设置弹窗 ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }
.modal-content {
  background: #fff;
  border-radius: 16px;
  width: 520px;
  max-width: 90vw;
  max-height: 85vh;
  overflow-y: auto;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 18px; }
.modal-close { background: none; border: none; font-size: 18px; cursor: pointer; color: var(--text-light); }
.modal-body { padding: 20px 24px; }
.modal-footer {
  padding: 14px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
}
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { border-color: var(--accent); }
.form-group .hint { font-size: 11px; color: var(--text-light); margin-top: 4px; }
.btn-primary {
  padding: 10px 24px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}
.btn-primary:hover { background: var(--accent-hover); }
.hint { font-size: 12px; color: var(--text-light); }

/* 滚动条 */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
::-webkit-scrollbar-track { background: transparent; }


/* ---------- 侧边栏导航（P4d 版） ---------- */
.nav-group {
  font-size: 11px;
  color: var(--sidebar-text);
  padding: 12px 12px 4px;
  opacity: 0.7;
  letter-spacing: 0.5px;
}
.nav { padding: 0 8px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--sidebar-text);
  transition: all 0.15s;
  margin-bottom: 2px;
  text-decoration: none;
}
.nav-item:hover { background: rgba(255,255,255,0.08); color: #fff; }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-item .badge {
  margin-left: auto;
  background: rgba(255,255,255,0.15);
  border-radius: 10px;
  padding: 1px 8px;
  font-size: 11px;
}
.user-card {
  background: rgba(255,255,255,0.08);
  border-radius: 10px;
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.user-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
}
.user-info { flex: 1; min-width: 0; }
.user-info .name { font-size: 13px; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-info .balance { font-size: 11px; color: #4ade80; }

/* ===== 改进：新建会话+清空按钮行 ===== */
.btn-new-row { display: flex; gap: 8px; margin: 16px 16px 4px; }
.btn-new-row .btn-new { flex: 1; margin: 0; }
.btn-clear {
  padding: 10px 12px;
  background: transparent;
  color: var(--sidebar-text);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  transition: all 0.2s;
}
.btn-clear:hover { background: rgba(248,81,73,0.2); color: #f85149; border-color: rgba(248,81,73,0.5); }

/* ===== 改进：会话项 + 删除按钮 ===== */
.session-item {
  position: relative;
  padding-right: 30px;
}
.session-del {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  border-radius: 6px;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--sidebar-text);
  cursor: pointer;
  background: rgba(255,255,255,0.12);
  border: none;
}
.session-item:hover .session-del { display: flex; }
.session-del:hover { background: #f85149; color: #fff; }
.session-item.active .session-del { color: #fff; background: rgba(255,255,255,0.25); }
.session-item.active .session-del:hover { background: #f85149; }

/* ===== 改进：思考/工作过程状态条 ===== */
.thinking {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  background: #f8fafc;
  border-left: 3px solid var(--accent);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-light);
  margin-bottom: 8px;
  white-space: pre-wrap;
  cursor: pointer;
  user-select: none;
  transition: background 0.2s;
}
.thinking:hover { background: #f0f4ff; }
.thinking .think-label { font-weight: 600; color: var(--accent); white-space: nowrap; flex-shrink: 0; }
.thinking .think-dots { animation: thinkPulse 1.2s infinite; }
@keyframes thinkPulse { 0%,100% { opacity: 0.4; } 50% { opacity: 1; } }
.thinking.done { border-left-color: #22c55e; background: #f0fdf4; }
.thinking.done .think-label { color: #16a34a; }
.thinking .think-body { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thinking .think-toggle { color: #999; font-size: 11px; flex-shrink: 0; }
.thinking.expanded .think-body { white-space: pre-wrap; }

/* ===== 改进：工具步骤条 ===== */
.tool-step {
  width: 100%;
  max-width: 860px;
  background: #f0f4ff;
  border-left: 3px solid #2f6fed;
  padding: 7px 12px;
  border-radius: 6px;
  font-size: 12px;
  color: #4a5568;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.tool-step .step-num {
  background: #2f6fed;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  border-radius: 10px;
  padding: 1px 8px;
  white-space: nowrap;
  flex-shrink: 0;
}
.tool-step .step-name { font-weight: 600; white-space: nowrap; }
.tool-step .step-arg { color: #8899aa; font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.tool-step .step-status { margin-left: auto; font-size: 12px; flex-shrink: 0; }
.tool-step.running { border-left-color: #d97757; background: #fff8e6; }
.tool-step.running .step-status { animation: thinkPulse 1.2s infinite; }
.tool-step.done { border-left-color: #22c55e; background: #f0fdf4; }

/* ===== 改进：授权弹窗（内容超出可滚动，按钮始终可见） ===== */
.approval-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  padding: 20px;
}
.approval-modal .approval-card {
  background: #1e2a3a;
  border-radius: 14px;
  width: 520px;
  max-width: 100%;
  max-height: calc(100vh - 40px);
  display: flex;
  flex-direction: column;
  color: #e6edf3;
  font-family: system-ui, 'Microsoft YaHei', sans-serif;
  overflow: hidden;
}
.approval-modal .approval-header {
  padding: 16px 20px 0;
  font-size: 17px;
  font-weight: 700;
  flex-shrink: 0;
}
.approval-modal .approval-desc {
  padding: 10px 20px 0;
  font-size: 13px;
  color: #aab8c9;
  flex-shrink: 0;
}
.approval-modal .approval-body {
  padding: 10px 20px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}
.approval-modal .approval-body pre {
  background: #0d1117;
  border-radius: 8px;
  padding: 12px;
  font-family: Consolas, monospace;
  font-size: 12px;
  color: #7ee787;
  white-space: pre-wrap;
  word-break: break-all;
  margin: 0;
}
.approval-modal .approval-footer {
  padding: 12px 20px 16px;
  border-top: 1px solid rgba(255,255,255,0.08);
  flex-shrink: 0;
}
.approval-modal .approval-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
.approval-modal .approval-tip { font-size: 11px; color: #8b949e; margin-top: 8px; text-align: center; }
.approval-modal .btn-deny {
  padding: 9px 18px;
  border-radius: 8px;
  border: 1px solid #f85149;
  background: transparent;
  color: #f85149;
  cursor: pointer;
  font-size: 13px;
}
.approval-modal .btn-deny:hover { background: rgba(248,81,73,0.15); }
.approval-modal .btn-allow {
  padding: 9px 18px;
  border-radius: 8px;
  border: none;
  background: #2f6fed;
  color: #fff;
  cursor: pointer;
  font-size: 13px;
}
.approval-modal .btn-allow:hover { filter: brightness(1.15); }
.approval-modal .btn-allow-all {
  padding: 9px 18px;
  border-radius: 8px;
  border: none;
  background: #238636;
  color: #fff;
  cursor: pointer;
  font-size: 13px;
}
.approval-modal .btn-allow-all:hover { filter: brightness(1.15); }


/* EBAI 风格聊天底部控制栏 */
#input-controls{display:flex;align-items:center;gap:12px;min-height:32px;margin-bottom:8px;color:#6b7280;font-size:12px}
#input-controls select{height:28px;border:1px solid #e5e7eb;border-radius:6px;background:#fff;color:#4b5563;padding:0 8px;font-size:12px;outline:none}
.switch-item{display:inline-flex;align-items:center;gap:6px;cursor:pointer;user-select:none}
.switch-item input{display:none}
.switch-slider{position:relative;width:30px;height:16px;background:#d1d5db;border-radius:10px;transition:.2s}
.switch-slider:after{content:'';position:absolute;width:12px;height:12px;left:2px;top:2px;background:white;border-radius:50%;box-shadow:0 1px 2px #999;transition:.2s}
.switch-item input:checked+.switch-slider{background:#667eea}
.switch-item input:checked+.switch-slider:after{transform:translateX(14px)}
.switch-label{font-weight:500;color:#6b7280}
.clear-chat-btn{height:28px;padding:0 10px;border:1px solid #e5e7eb;border-radius:6px;background:#fff;color:#6b7280;font-size:12px;cursor:pointer}
.clear-chat-btn:hover{background:#fef2f2;color:#dc2626;border-color:#fecaca}
.gen-status{margin-left:auto;color:#667eea;font-size:12px}
.btn-stop{height:28px;padding:0 12px;border:1px solid #fca5a5;border-radius:6px;background:#fff1f2;color:#dc2626;font-size:12px;cursor:pointer}
.btn-stop:hover{background:#fee2e2}
.hidden{display:none!important}
#input-row{display:flex;align-items:flex-end;gap:8px}
#input-row #input{min-height:38px}
.btn-send{min-width:72px}
@media(max-width:700px){#input-controls{gap:6px;flex-wrap:wrap}.gen-status{margin-left:0}.clear-chat-btn{margin-left:auto}}
