:root {
  --bg: #f5f4ef;
  --panel: #ffffff;
  --sidebar: #ece9e2;
  --text: #2b2a27;
  --muted: #6f6c64;
  --border: #e0ddd4;
  --accent: #c2603f;
  --accent-soft: #f3e6df;
  --user-bubble: #eceae3;
  --code-bg: #2b2a27;
  --code-text: #f5f4ef;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC",
    "PingFang SC", "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 15px;
  line-height: 1.6;
}

#app { display: flex; height: 100vh; overflow: hidden; }

/* ---------- 侧边栏 ---------- */
#sidebar {
  width: 264px;
  flex-shrink: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left .2s ease;
}
#sidebar.collapsed { margin-left: -264px; }

.sidebar-head { padding: 12px; }
#new-chat {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: background .15s;
}
#new-chat:hover { background: #fbfaf7; }
#new-agent {
  width: 100%;
  margin-top: 8px;
  padding: 9px 14px;
  border: 1px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  cursor: pointer;
}
#new-agent:hover { filter: brightness(0.98); }
#new-agent[hidden] { display: none; }

#conversations { flex: 1; overflow-y: auto; padding: 4px 8px; }

.conv {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 10px;
  margin: 2px 0;
  border-radius: 9px;
  cursor: pointer;
  color: var(--text);
  font-size: 14px;
}
.conv:hover { background: #e2dfd6; }
.conv.active { background: #d9d5ca; }
.conv .title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv .del {
  opacity: 0;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 6px;
}
.conv:hover .del { opacity: 1; }
.conv .del:hover { background: #cfcabd; color: var(--text); }

.sidebar-foot {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 7px;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); display: inline-block; }
.foot-spacer { flex: 1; }
#user-name { color: var(--text); font-weight: 600; }
#logout-btn {
  border: 1px solid var(--border); background: transparent;
  color: var(--muted); border-radius: 7px; padding: 3px 8px;
  font-size: 12px; cursor: pointer;
}
#logout-btn:hover { background: var(--panel); color: var(--text); }
#logout-btn[hidden] { display: none; }

/* ---------- 主区域 ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#topbar {
  height: 52px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
#toggle-sidebar {
  border: none;
  background: transparent;
  font-size: 18px;
  cursor: pointer;
  color: var(--muted);
  padding: 4px 8px;
  border-radius: 8px;
}
#toggle-sidebar:hover { background: var(--sidebar); }
#chat-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#mode-badge {
  flex-shrink: 0;
  font-size: 12px; color: var(--accent);
  background: var(--accent-soft); border: 1px solid var(--accent);
  border-radius: 6px; padding: 2px 8px;
  max-width: 50%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#mode-badge[hidden] { display: none; }

/* ---------- 工具步骤（Agent 模式）---------- */
.steps { margin-bottom: 10px; display: flex; flex-direction: column; gap: 6px; }
.step {
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
  font-family: "SFMono-Regular", Consolas, Menlo, monospace; font-size: 12.5px;
}
.step-head {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; background: #efece5; color: var(--text); font-weight: 600;
}
.step-head .tag { color: var(--accent); }
.step pre {
  margin: 0; padding: 8px 10px; white-space: pre-wrap; word-break: break-word;
  background: var(--code-bg); color: var(--code-text); overflow-x: auto;
}
.step-res { border-color: #d8d3c8; }
.step-res > summary {
  cursor: pointer; padding: 6px 10px; color: var(--muted); list-style: none;
  background: #f1eee7;
}
.step-res > summary::-webkit-details-marker { display: none; }
.step-res[data-error="1"] > summary { color: #b3402a; }
.step-res pre { background: #2b2a27; color: #e8e6df; max-height: 320px; }

#messages { flex: 1; overflow-y: auto; scroll-behavior: smooth; }

#empty-state {
  max-width: 720px;
  margin: 18vh auto 0;
  padding: 0 24px;
  text-align: center;
  color: var(--muted);
}
#empty-state h1 { font-size: 30px; color: var(--text); margin-bottom: 8px; font-weight: 600; }

.turn { max-width: 760px; margin: 0 auto; padding: 18px 24px; display: flex; gap: 14px; }
.turn .avatar {
  flex-shrink: 0;
  width: 28px; height: 28px;
  border-radius: 7px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; color: #fff;
}
.turn.user .avatar { background: #8a8madd; background: #7a766c; }
.turn.assistant .avatar { background: var(--accent); }
.turn .body { flex: 1; min-width: 0; padding-top: 2px; }
.turn .role { font-weight: 600; font-size: 13px; margin-bottom: 2px; color: var(--muted); }

/* 消息里的图片 */
.msg-images { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.msg-images img {
  max-width: 260px; max-height: 260px;
  border-radius: 10px; border: 1px solid var(--border);
  object-fit: cover; cursor: pointer;
}

.content { word-wrap: break-word; overflow-wrap: break-word; }
.content > *:first-child { margin-top: 0; }
.content > *:last-child { margin-bottom: 0; }
.content p { margin: 0 0 12px; }
.content.streaming { white-space: pre-wrap; }

/* 思考摘要 */
.thinking-box {
  margin: 0 0 12px;
  border-left: 3px solid var(--border);
  background: #efece5;
  border-radius: 8px;
  overflow: hidden;
}
.thinking-box summary {
  cursor: pointer;
  padding: 8px 12px;
  font-size: 13px;
  color: var(--muted);
  user-select: none;
  list-style: none;
}
.thinking-box summary::-webkit-details-marker { display: none; }
.thinking-box .thinking-content {
  padding: 0 14px 10px;
  font-size: 13.5px;
  color: var(--muted);
  white-space: pre-wrap;
}

/* Markdown 元素 */
.content h1, .content h2, .content h3 { margin: 18px 0 8px; line-height: 1.3; }
.content h1 { font-size: 22px; }
.content h2 { font-size: 19px; }
.content h3 { font-size: 16px; }
.content ul, .content ol { margin: 0 0 12px; padding-left: 22px; }
.content li { margin: 3px 0; }
.content a { color: var(--accent); }
.content blockquote {
  margin: 0 0 12px; padding: 2px 14px;
  border-left: 3px solid var(--border); color: var(--muted);
}
.content code {
  background: #e7e4dc; padding: 1.5px 5px; border-radius: 5px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 13px;
}
.content pre {
  background: var(--code-bg); color: var(--code-text);
  padding: 14px 16px; border-radius: 10px; overflow-x: auto; margin: 0 0 12px;
}
.content pre code { background: transparent; padding: 0; color: inherit; font-size: 13px; }
.content hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.content table { border-collapse: collapse; margin: 0 0 12px; }
.content th, .content td { border: 1px solid var(--border); padding: 6px 10px; }

.cursor::after {
  content: "▍"; color: var(--accent);
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.error-msg { color: #b3402a; background: #f7e3de; padding: 10px 14px; border-radius: 10px; }

/* ---------- 输入框 ---------- */
#composer { flex-shrink: 0; padding: 10px 24px 16px; background: var(--bg); }
#composer-box {
  max-width: 760px;
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
}
#composer-box.dragover { border-color: var(--accent); background: var(--accent-soft); }
#composer-box:focus-within { border-color: #c9c4b8; }
#composer-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding-left: 8px;
}
#attach {
  flex-shrink: 0;
  width: 32px; height: 32px;
  border: 1px solid var(--border); border-radius: 8px;
  background: transparent; color: var(--muted);
  font-size: 18px; line-height: 1; cursor: pointer;
  transition: background .15s;
}
#attach:hover { background: var(--sidebar); }

/* 待发送图片缩略图 */
#attachments { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 4px 0; }
#attachments:empty { display: none; }
.thumb {
  position: relative; width: 64px; height: 64px;
  border-radius: 8px; overflow: hidden; border: 1px solid var(--border);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .rm {
  position: absolute; top: 2px; right: 2px;
  width: 18px; height: 18px; border: none; border-radius: 50%;
  background: rgba(0,0,0,.6); color: #fff; font-size: 12px; line-height: 18px;
  cursor: pointer; padding: 0; text-align: center;
}
#input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  font: inherit;
  color: var(--text);
  max-height: 220px;
  line-height: 1.5;
  padding: 6px 0;
}
#send {
  flex-shrink: 0;
  width: 36px; height: 36px;
  border: none; border-radius: 9px;
  background: var(--accent); color: #fff;
  font-size: 18px; cursor: pointer;
  transition: opacity .15s, background .15s;
}
#send:disabled { background: #d8cfc6; cursor: default; }
#send.stop { background: #7a766c; }
#hint { max-width: 760px; margin: 6px auto 0; text-align: center; font-size: 12px; color: var(--muted); }

/* ---------- 登录遮罩 ---------- */
#login-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(43, 42, 39, .45);
  display: flex; align-items: center; justify-content: center;
}
#login-overlay[hidden] { display: none; }
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 28px 24px;
  width: 360px; max-width: calc(100vw - 40px);
  box-shadow: 0 10px 40px rgba(0,0,0,.18);
}
.login-card h2 { margin: 0 0 6px; font-size: 19px; }
.login-card p { margin: 0 0 16px; color: var(--muted); font-size: 14px; }
.login-card input {
  width: 100%; box-sizing: border-box;
  padding: 10px 12px; font: inherit;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg); color: var(--text); outline: none;
}
.login-card input { margin-bottom: 10px; }
.login-card input:focus { border-color: var(--accent); }
#login-err { color: #b3402a; font-size: 13px; min-height: 18px; margin: 8px 2px 0; }
#login-btn {
  width: 100%; margin-top: 12px; padding: 10px;
  border: none; border-radius: 10px;
  background: var(--accent); color: #fff;
  font-size: 15px; font-weight: 600; cursor: pointer;
}
#login-btn:hover { filter: brightness(1.05); }

/* ---------- 目录浏览器 ---------- */
#fb-overlay {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(43, 42, 39, .45);
  display: flex; align-items: center; justify-content: center;
}
#fb-overlay[hidden] { display: none; }
.fb-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
  padding: 22px; width: 520px; max-width: calc(100vw - 40px);
  box-shadow: 0 10px 40px rgba(0,0,0,.18); display: flex; flex-direction: column;
}
.fb-card h3 { margin: 0 0 4px; font-size: 17px; }
.fb-hint { margin: 0 0 12px; font-size: 13px; color: var(--muted); }
.fb-path {
  font-family: Consolas, Menlo, monospace; font-size: 12.5px; color: var(--muted);
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 10px; margin-bottom: 8px; word-break: break-all;
}
.fb-list {
  border: 1px solid var(--border); border-radius: 8px;
  height: 280px; overflow-y: auto; background: var(--bg);
}
.fb-item {
  padding: 8px 12px; cursor: pointer; font-size: 14px;
  border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 8px;
}
.fb-item:last-child { border-bottom: none; }
.fb-item:hover { background: var(--sidebar); }
.fb-empty { padding: 16px; color: var(--muted); font-size: 13px; text-align: center; }
.fb-actions { display: flex; gap: 8px; margin-top: 14px; }
.fb-actions button {
  padding: 9px 12px; border-radius: 9px; font: inherit; font-size: 14px; cursor: pointer;
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
}
#fb-up { margin-right: auto; }
#fb-pick { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }
#fb-pick:hover { filter: brightness(1.05); }

/* ---------- 响应式 ---------- */
#sidebar-backdrop { display: none; }

@media (max-width: 720px) {
  /* 侧边栏 → 覆盖式抽屉：默认收起，点 ☰ 滑出，点遮罩 / 选对话自动收起 */
  #sidebar {
    position: fixed; top: 0; left: 0; z-index: 30;
    height: 100%;
    width: min(280px, 82vw);
    margin-left: 0;
    box-shadow: 2px 0 16px rgba(0,0,0,.18);
    transform: translateX(0);
    transition: transform .22s ease;
  }
  #sidebar.collapsed { transform: translateX(-100%); margin-left: 0; }

  #sidebar-backdrop.show {
    display: block; position: fixed; inset: 0; z-index: 20;
    background: rgba(43, 42, 39, .42);
    -webkit-tap-highlight-color: transparent;
  }

  /* 窄屏让聊天区更舒展 */
  #topbar { padding: 0 8px; gap: 8px; }
  #toggle-sidebar { font-size: 22px; padding: 6px 10px; }
  .turn { max-width: 100%; padding: 14px; gap: 10px; }
  #composer { padding: 8px 12px 14px; }
  #composer-box, #hint { max-width: 100%; }
  #empty-state { margin-top: 14vh; padding: 0 18px; }
  #empty-state h1 { font-size: 24px; }
  .msg-images img { max-width: 70vw; }
}
