/* emall web — 信息密度优先。双主题切换：data-theme="light"|"dark" 写到 <html>。 */

/* 默认 = 深色 */
:root {
  --bg: #1a1a1a;
  --bg-2: #242424;
  --bg-3: #2e2e2e;
  --fg: #e0e0e0;
  --fg-dim: #888;
  --fg-mute: #555;
  --accent: #5fa8ff;
  --user: #ffd470;
  --assistant: #5fa8ff;
  --ok: #5fc97f;
  --err: #ff6b6b;
  --warn: #ffb454;
  --thinking: #888;
  --bd: #333;
  --bd-strong: #444;
  --strong: #fff;
  --table-stripe: rgba(255, 255, 255, 0.02);
  --shadow: rgba(0, 0, 0, 0.6);
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* 浅色主题：覆盖颜色变量；其它（字体）继承。 */
html[data-theme="light"] {
  --bg: #ffffff;
  --bg-2: #f5f5f7;
  --bg-3: #e8e8ec;
  --fg: #1a1a1a;
  --fg-dim: #666;
  --fg-mute: #999;
  --accent: #2563eb;
  --user: #9a4900;
  --assistant: #1d4ed8;
  --ok: #15803d;
  --err: #b91c1c;
  --warn: #b45309;
  --thinking: #777;
  --bd: #d8d8dc;
  --bd-strong: #b8b8be;
  --strong: #000;
  --table-stripe: rgba(0, 0, 0, 0.025);
  --shadow: rgba(0, 0, 0, 0.2);
}

* { box-sizing: border-box; }

html, body, #app { height: 100%; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
}

button {
  font: inherit;
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--bd);
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
}

button:hover { background: var(--bd-strong); }
button.primary { background: var(--accent); color: #000; border-color: var(--accent); }
button.primary:hover { filter: brightness(1.1); }
button.danger { color: var(--err); border-color: var(--err); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

input, textarea {
  font: inherit;
  background: var(--bg-2);
  color: var(--fg);
  border: 1px solid var(--bd);
  padding: 8px 10px;
  border-radius: 4px;
  outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }

a { color: var(--accent); }

/* === 登录页 === */

.login-shell {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.login-card {
  background: var(--bg-2);
  border: 1px solid var(--bd);
  border-radius: 8px;
  padding: 32px;
  width: 320px;
}
.login-card h1 {
  margin: 0 0 20px;
  font-size: 18px;
  font-weight: 500;
}
.login-card label {
  display: block;
  font-size: 12px;
  color: var(--fg-dim);
  margin: 12px 0 4px;
}
.login-card input { width: 100%; }
.login-card .submit-row { margin-top: 20px; display: flex; gap: 8px; }
.login-card .err { color: var(--err); margin-top: 12px; font-size: 12px; }

/* === 主聊天页 === */

.chat-shell {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.statusbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--bd);
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--fg-dim);
  flex: 0 0 auto;
}
.statusbar .brand { font-weight: 600; color: var(--fg); }
.statusbar .gap { flex: 1; }
.statusbar .mode { padding: 2px 8px; border-radius: 3px; cursor: pointer; }
.statusbar .mode.default { background: var(--bd-strong); color: var(--fg); }
.statusbar .mode.acceptEdits { background: #d4a017; color: #000; }
.statusbar .mode.bypassPermissions { background: #a83232; color: #fff; }
.statusbar .mode.plan { background: #2c5fb8; color: #fff; }
.statusbar .logout { color: var(--fg-dim); cursor: pointer; }
.statusbar .theme-toggle {
  cursor: pointer;
  font-size: 14px;
  user-select: none;
  padding: 0 4px;
}
.statusbar .theme-toggle:hover { color: var(--fg); }

.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 0 24px;
}

/* 消息间分隔：上边框 + 16px 内距 */
.msg {
  padding: 16px 0;
  border-top: 1px solid var(--bd);
  font-size: 14px;
}
.msg:first-child { border-top: none; }

.msg .who {
  display: block;
  font-weight: 600;
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 8px;
}
.msg.user .who { color: var(--user); }
.msg.assistant .who { color: var(--assistant); }

.msg .body {
  display: block;
}

/* === Markdown 完整样式 === */
.msg .body p { margin: 8px 0; line-height: 1.6; }
.msg .body p:first-child { margin-top: 0; }
.msg .body p:last-child { margin-bottom: 0; }

.msg .body h1, .msg .body h2, .msg .body h3,
.msg .body h4, .msg .body h5, .msg .body h6 {
  margin: 18px 0 8px;
  font-weight: 600;
  line-height: 1.3;
}
.msg .body h1:first-child, .msg .body h2:first-child, .msg .body h3:first-child {
  margin-top: 0;
}
.msg .body h1 { font-size: 20px; }
.msg .body h2 { font-size: 17px; }
.msg .body h3 { font-size: 15px; }
.msg .body h4, .msg .body h5, .msg .body h6 { font-size: 14px; color: var(--fg-dim); }

.msg .body strong { font-weight: 600; color: var(--strong); }
.msg .body em { font-style: italic; }
.msg .body a { color: var(--accent); text-decoration: underline; }
.msg .body a:hover { text-decoration: none; }

.msg .body ul, .msg .body ol {
  margin: 8px 0;
  padding-left: 24px;
  line-height: 1.6;
}
.msg .body li { margin: 3px 0; }
.msg .body li > p { margin: 2px 0; }

.msg .body blockquote {
  margin: 8px 0;
  padding: 4px 14px;
  border-left: 3px solid var(--accent);
  background: var(--bg-2);
  color: var(--fg-dim);
  border-radius: 0 4px 4px 0;
}
.msg .body blockquote p { margin: 4px 0; }

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

.msg .body pre {
  background: var(--bg-2);
  padding: 10px 14px;
  border-radius: 4px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.5;
  margin: 10px 0;
  border: 1px solid var(--bd);
}
.msg .body pre code { padding: 0; background: none; border: none; font-size: inherit; }
.msg .body code:not(pre code) {
  background: var(--bg-2);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: var(--font-mono);
  font-size: 12.5px;
  border: 1px solid var(--bd);
}

.msg .body table {
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13px;
}
.msg .body th, .msg .body td {
  border: 1px solid var(--bd);
  padding: 5px 10px;
  text-align: left;
}
.msg .body th { background: var(--bg-2); font-weight: 600; }
.msg .body tbody tr:nth-child(odd) { background: var(--table-stripe); }

/* GFM 任务列表 */
.msg .body input[type="checkbox"] {
  margin-right: 6px;
  vertical-align: middle;
}

/* 多模态附件在 user 消息里 */
.msg.user img { display: block; margin-top: 6px; }

.thinking {
  margin-bottom: 8px;
  border-left: 2px solid var(--fg-mute);
  padding: 4px 12px;
  font-style: italic;
  color: var(--thinking);
  font-size: 13px;
  white-space: pre-wrap;
  cursor: pointer;
}
.thinking .head {
  font-weight: 500;
  user-select: none;
}
.thinking.collapsed .body { display: none; }

.tool-line {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--fg-dim);
  margin: 4px 0;
  padding: 4px 8px;
  background: var(--bg-2);
  border-radius: 3px;
}
.tool-line.error { color: var(--err); border-left: 2px solid var(--err); }
.tool-line.running { color: var(--accent); }
.tool-line .summary {
  margin-left: 8px;
  color: var(--fg-mute);
}
.tool-line .progress {
  display: block;
  margin-top: 4px;
  white-space: pre-wrap;
  color: var(--fg-mute);
  max-height: 200px;
  overflow-y: auto;
}
.tool-line .progress .stderr { color: var(--err); }

.tool-line .result {
  display: block;
  margin-top: 4px;
  white-space: pre-wrap;
  color: var(--fg);
  background: var(--bg);
  padding: 4px 8px;
  border-radius: 3px;
  max-height: 240px;
  overflow-y: auto;
}

/* === 输入区 === */

.composer {
  flex: 0 0 auto;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--bd);
  background: var(--bg);
}

.attachments {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.attachments .chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-3);
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
}
.attachments .chip .x {
  color: var(--err);
  cursor: pointer;
  font-weight: 700;
}

.composer .row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.composer textarea {
  flex: 1;
  min-height: 32px;
  max-height: 200px;
  resize: none;
  font-family: var(--font-sans);
}
.composer .send-btn { align-self: stretch; }
.composer .help {
  margin-top: 6px;
  font-size: 11px;
  color: var(--fg-mute);
  font-family: var(--font-mono);
}

/* === modal === */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal {
  background: var(--bg-2);
  border: 1px solid var(--bd);
  border-radius: 6px;
  padding: 20px 24px;
  max-width: 600px;
  width: 90%;
}
.modal h2 { margin: 0 0 12px; font-size: 16px; font-weight: 500; }
.modal .body { margin-bottom: 16px; word-break: break-word; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.modal pre {
  background: var(--bg);
  padding: 8px 12px;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 12px;
  overflow-x: auto;
  max-height: 200px;
  overflow-y: auto;
}

.modal.danger { border-color: var(--err); }

.q-options { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; }
.q-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg-3);
  border-radius: 4px;
  cursor: pointer;
}
.q-option:hover { background: #3a3a3a; }
.q-option .desc { color: var(--fg-dim); font-size: 12px; margin-left: 4px; }

/* === drag/drop indicator === */

body.dragging::after {
  content: "Drop image / PDF to attach";
  position: fixed;
  inset: 16px;
  border: 2px dashed var(--accent);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--accent);
  background: rgba(95, 168, 255, 0.05);
  pointer-events: none;
  z-index: 999;
}

/* === scrollbar：跟随主题 === */

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--bd-strong); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--fg-mute); }

/* === 流式输出光标 === */

@keyframes emall-caret-blink {
  0%, 50%   { opacity: 1; }
  51%, 100% { opacity: 0; }
}

.caret {
  display: inline-block;
  width: 6px;
  height: 1em;
  margin-left: 2px;
  background: var(--assistant);
  vertical-align: text-bottom;
  animation: emall-caret-blink 1s steps(1) infinite;
}

/* 流式中的消息：与最终消息视觉一致，光标提示"还在写" */
.msg.assistant.streaming .body { opacity: 0.95; }
.thinking.streaming { opacity: 0.85; font-style: italic; }
