/* ============================================================
   CC 聊天 · 样式
   深色/浅色主题（CSS 变量）+ 移动端适配
   ============================================================ */

:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --border: #e3e6ea;
  --text: #1b1e23;
  --text-dim: #6d7480;
  --accent: #4c6ef5;
  --accent-hover: #4263e6;
  --accent-soft: rgba(76, 110, 245, 0.12);
  --user-bubble: #4c6ef5;
  --user-text: #ffffff;
  --danger: #e03131;
  --ok: #2f9e44;
  --code-bg: #f6f8fa;
  --shadow: 0 6px 24px rgba(20, 25, 40, 0.08);
  --radius: 14px;
}

[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --border: #2a2f3a;
  --text: #e7e9ee;
  --text-dim: #98a1b0;
  --accent: #6d8dff;
  --accent-hover: #7f9bff;
  --accent-soft: rgba(109, 141, 255, 0.16);
  --user-bubble: #3b5bdb;
  --user-text: #ffffff;
  --danger: #ff6b6b;
  --ok: #51cf66;
  --code-bg: #0d1117;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
}

/* 跟随系统（用户未手动切换时） */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --panel: #171a21;
    --panel-2: #1e222b;
    --border: #2a2f3a;
    --text: #e7e9ee;
    --text-dim: #98a1b0;
    --accent: #6d8dff;
    --accent-hover: #7f9bff;
    --accent-soft: rgba(109, 141, 255, 0.16);
    --user-bubble: #3b5bdb;
    --danger: #ff6b6b;
    --ok: #51cf66;
    --code-bg: #0d1117;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

button {
  font: inherit;
  color: inherit;
  border: none;
  background: none;
  cursor: pointer;
}

/* ============================ 登录页 ============================ */

.login-view {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 100;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 36px 28px 28px;
  text-align: center;
}

.login-logo { font-size: 34px; color: var(--accent); line-height: 1; }

.login-card h1 { margin: 10px 0 4px; font-size: 22px; }

.login-sub { margin: 0 0 22px; color: var(--text-dim); font-size: 13px; }

.login-card input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 15px;
  outline: none;
  margin-bottom: 14px;
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.primary-btn {
  width: 100%;
  padding: 12px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  transition: background .15s;
}
.primary-btn:hover { background: var(--accent-hover); }
.primary-btn:disabled { opacity: .6; cursor: default; }

.login-error { color: var(--danger); font-size: 13px; margin-top: 12px; min-height: 18px; }

/* ============================ 应用布局 ============================ */

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  min-height: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; font-size: 15px; color: var(--accent); }

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--text-dim);
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

.conv-list { flex: 1; overflow-y: auto; padding: 10px; }

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  margin-bottom: 2px;
  transition: background .12s;
}
.conv-item:hover { background: var(--panel-2); }
.conv-item.active { background: var(--accent-soft); }

.conv-item .conv-main { flex: 1; min-width: 0; }

.conv-item .conv-title {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-item.active .conv-title { color: var(--accent); font-weight: 600; }

.conv-item .conv-time { font-size: 12px; color: var(--text-dim); margin-top: 1px; }

.conv-item .conv-del {
  opacity: 0;
  color: var(--text-dim);
  font-size: 13px;
  padding: 4px 6px;
  border-radius: 6px;
  flex-shrink: 0;
}
.conv-item:hover .conv-del { opacity: 1; }
.conv-item .conv-del:hover { color: var(--danger); background: var(--panel); }

.conv-empty { color: var(--text-dim); font-size: 13px; text-align: center; padding: 24px 10px; }

.sidebar-foot {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
}

.foot-btn {
  flex: 1;
  padding: 8px;
  border-radius: 9px;
  font-size: 13px;
  color: var(--text-dim);
  background: var(--panel-2);
  transition: background .15s, color .15s;
}
.foot-btn:hover { color: var(--text); }

.sidebar-mask { display: none; }

/* ============================ 主区域 ============================ */

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  min-height: 56px;
}

.menu-btn { display: none; }

.chat-title {
  flex: 1;
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-right { display: flex; align-items: center; gap: 10px; }

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--panel-2);
  padding: 4px 10px;
  border-radius: 999px;
}

.status-pill .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: .35; transform: scale(.75); }
}

.conn-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--ok);
  transition: background .3s;
}
.conn-dot.off { background: var(--danger); }
.conn-dot.connecting { background: #f59f00; }

/* ============================ 消息区 ============================ */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 22px 20px 10px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

.msg { display: flex; flex-direction: column; max-width: min(860px, 100%); }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; }

.bubble {
  padding: 11px 16px;
  border-radius: 16px;
  overflow-wrap: break-word;
  word-break: break-word;
  max-width: 100%;
}

.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-radius: 16px 16px 4px 16px;
  white-space: pre-wrap;
}

.msg.assistant .bubble {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px 16px 16px 4px;
}

.msg-meta {
  font-size: 11.5px;
  color: var(--text-dim);
  margin-top: 5px;
  padding: 0 6px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.msg-meta .badge {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 11px;
}
.msg-meta .badge.warn { color: var(--danger); border-color: var(--danger); }

/* 流式光标 */
.msg.streaming .part-text:last-child .md > :last-child::after {
  content: "▍";
  color: var(--accent);
  animation: blink 1s steps(1) infinite;
  margin-left: 1px;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---------------- Markdown ---------------- */

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }

.md p { margin: .55em 0; }
.md h1, .md h2, .md h3, .md h4 { margin: .9em 0 .5em; line-height: 1.35; }
.md h1 { font-size: 1.35em; }
.md h2 { font-size: 1.2em; }
.md h3 { font-size: 1.08em; }

.md ul, .md ol { margin: .5em 0; padding-left: 1.5em; }
.md li { margin: .18em 0; }

.md blockquote {
  margin: .6em 0;
  padding: .1em 1em;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 8px 8px 0;
  color: var(--text-dim);
}

.md code {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  font-size: .9em;
  background: var(--panel-2);
  padding: .15em .4em;
  border-radius: 5px;
}

.md pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: .6em 0;
}

.md pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.55;
  display: block;
}

.code-wrap { position: relative; }
.code-wrap .copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 3px 10px;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 7px;
  opacity: 0;
  transition: opacity .15s;
}
.code-wrap:hover .copy-btn { opacity: 1; }
.code-wrap .copy-btn:hover { color: var(--accent); }

.md table {
  border-collapse: collapse;
  margin: .6em 0;
  display: block;
  max-width: 100%;
  overflow-x: auto;
}
.md th, .md td {
  border: 1px solid var(--border);
  padding: 6px 12px;
  font-size: .93em;
}
.md th { background: var(--panel-2); }

.md a { color: var(--accent); text-decoration: none; }
.md a:hover { text-decoration: underline; }

.md hr { border: none; border-top: 1px solid var(--border); margin: 1em 0; }

.md img { max-width: 100%; border-radius: 8px; }

/* ---------------- 思考过程 / 工具调用 ---------------- */

.thinking, .tool {
  margin: 8px 0;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  font-size: 13.5px;
}

.thinking > summary, .tool > summary {
  padding: 7px 12px;
  cursor: pointer;
  color: var(--text-dim);
  user-select: none;
  display: flex;
  align-items: center;
  gap: 8px;
}

.tool > summary .tool-name {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--text);
  font-size: 13px;
}

.tool > summary .tool-status {
  font-size: 11.5px;
  margin-left: auto;
  padding: 1px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
}
.tool > summary .tool-status.running { color: var(--accent); border-color: var(--accent); }
.tool > summary .tool-status.error { color: var(--danger); border-color: var(--danger); }
.tool > summary .tool-status.done { color: var(--ok); border-color: var(--ok); }

.thinking .md { padding: 4px 12px 10px; color: var(--text-dim); }

.tool .tool-body { padding: 0 12px 10px; }

.tool pre.tool-input, .tool pre.tool-result {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  overflow: auto;
  max-height: 260px;
  margin: 6px 0 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
}
.tool pre.tool-result { color: var(--text-dim); }

/* ---------------- 欢迎页 ---------------- */

.welcome {
  margin: auto;
  text-align: center;
  color: var(--text-dim);
  max-width: 460px;
  padding: 30px 20px;
}
.welcome .big { font-size: 44px; margin-bottom: 8px; }
.welcome h2 { color: var(--text); margin: 0 0 8px; font-size: 19px; }
.welcome p { margin: 6px 0; font-size: 14px; }
.welcome .tips {
  margin-top: 18px;
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 18px;
  font-size: 13.5px;
}
.welcome .tips div { margin: 4px 0; }
.welcome kbd {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
  font-size: 12px;
  font-family: inherit;
}

/* ============================ 输入区 ============================ */

.composer-wrap {
  padding: 10px 20px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px 10px;
  box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }

#input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: none;
  color: var(--text);
  font: inherit;
  line-height: 1.5;
  max-height: 200px;
  padding: 6px 4px;
}

.send-btn, .stop-btn {
  width: 38px;
  height: 38px;
  border-radius: 11px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  transition: background .15s, opacity .15s;
}

.send-btn { background: var(--accent); color: #fff; }
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: .45; cursor: default; }

.stop-btn { background: var(--danger); color: #fff; }
.stop-btn:hover { opacity: .85; }

.composer-hint {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-dim);
  margin-top: 7px;
}

/* ============================ Toast ============================ */

.toasts {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
  width: min(92vw, 460px);
  pointer-events: none;
}

.toast {
  background: var(--panel);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 10px 14px;
  font-size: 13.5px;
  animation: toast-in .18s ease-out;
  pointer-events: auto;
}
.toast.error { border-left-color: var(--danger); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: none; }
}

/* ============================ 移动端 ============================ */

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(82vw, 320px);
    transform: translateX(-105%);
    transition: transform .22s ease;
    z-index: 40;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }

  .sidebar-mask {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .45);
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s;
    z-index: 35;
  }
  .sidebar-mask.show { opacity: 1; pointer-events: auto; }

  .menu-btn { display: inline-flex; }

  .messages { padding: 16px 12px 8px; }
  .msg { max-width: 94%; }
  .composer-wrap { padding-left: 12px; padding-right: 12px; }
}

/* 滚动条 */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }
::-webkit-scrollbar-track { background: transparent; }
