/* ── 重置样式 ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

/* ── CSS 变量 ── */
:root {
  --bg:            #ffffff;
  --sb-bg:         #f5f4ef;
  --sb-hover:      #eceae3;
  --sb-active:     #e4e1d9;
  --border:        #e5e3dc;
  --text:          #1a1a1a;
  --muted:         #8a8a7a;
  --accent:        #c96442;
  --user-bg:       #f0ede6;
  --code-bg:       #f6f6f4;
  --radius:        12px;
  --input-radius:  16px;
  --sidebar-w:     260px;
  --header-h:      52px;
  --transition:    .15s ease;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:      #1e1e1e;
    --sb-bg:   #161616;
    --sb-hover:#252525;
    --sb-active:#2e2e2e;
    --border:  #333;
    --text:    #e8e6df;
    --muted:   #888;
    --user-bg: #2a2a2a;
    --code-bg: #252525;
  }
  #hljs-theme { display: none; }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans SC', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

.hidden { display: none !important; }

/* ── 登录页 ── */
#login-overlay {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  z-index: 100;
}
.login-card {
  width: 340px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  padding: 40px 32px;
  border: 1px solid var(--border);
  border-radius: 20px;
}
.login-logo { color: var(--accent); }
.login-card h1 { font-size: 22px; font-weight: 600; }
.login-sub { font-size: 13px; color: var(--muted); }
.login-card input {
  width: 100%; padding: 10px 14px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--sb-bg); color: var(--text);
  font-size: 14px; outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button {
  width: 100%; padding: 10px;
  background: var(--text); color: var(--bg);
  border: none; border-radius: var(--radius);
  font-size: 14px; font-weight: 500; cursor: pointer;
}
.login-card button:hover { opacity: .85; }
.login-err { font-size: 12px; color: #e55; min-height: 16px; }

/* ── 应用布局 ── */
#app {
  display: flex;
  height: 100vh;
}

/* ── 侧边栏 ── */
#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--sb-bg);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
  transition: width var(--transition), transform var(--transition);
}
#sidebar.collapsed { width: 0; border-right: none; }

.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand { font-size: 16px; font-weight: 700; color: var(--text); }

#conv-list {
  flex: 1; overflow-y: auto; padding: 8px;
}
#conv-list::-webkit-scrollbar { width: 4px; }
#conv-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.conv-group-label {
  font-size: 11px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .05em;
  padding: 8px 8px 4px;
}
.conv-item {
  display: flex; align-items: center;
  padding: 8px 10px; border-radius: 8px;
  cursor: pointer; user-select: none;
  transition: background var(--transition);
  gap: 8px;
}
.conv-item:hover { background: var(--sb-hover); }
.conv-item.active { background: var(--sb-active); }
.conv-title {
  flex: 1; font-size: 13px; color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-del {
  opacity: 0; background: none; border: none;
  color: var(--muted); cursor: pointer; padding: 2px 4px;
  border-radius: 4px; font-size: 14px; line-height: 1;
}
.conv-item:hover .conv-del { opacity: 1; }
.conv-del:hover { color: #e55; }

/* ── 主内容区 ── */
#main {
  flex: 1; display: flex; flex-direction: column;
  overflow: hidden; min-width: 0;
}

/* ── 顶部工具栏 ── */
.top-bar {
  height: var(--header-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.model-wrap { display: flex; align-items: center; }
#model-select {
  background: var(--sb-bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 5px 10px; font-size: 13px; cursor: pointer; outline: none;
}
#model-select:hover { border-color: var(--muted); }

/* ── 消息列表 ── */
.messages {
  flex: 1; overflow-y: auto;
  padding: 24px 0;
  display: flex; flex-direction: column; gap: 0;
}
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.welcome {
  display: flex; flex-direction: column; align-items: center;
  gap: 12px; margin: auto;
  color: var(--muted);
}
.welcome-icon { color: var(--accent); opacity: .6; }
.welcome h2 { font-size: 18px; font-weight: 500; color: var(--text); }

/* 消息行 */
.msg-row {
  display: flex;
  padding: 6px 24px;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
}
.msg-row.user { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }

.msg-bubble {
  max-width: 78%;
  font-size: 14.5px; line-height: 1.65;
}
.msg-row.user .msg-bubble {
  background: var(--user-bg);
  border-radius: 18px 18px 4px 18px;
  padding: 10px 16px;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-row.assistant .msg-bubble {
  max-width: 92%;
}

/* 消息内附件 */
.msg-attachments {
  display: flex; flex-wrap: wrap; gap: 8px;
  margin-bottom: 8px;
}
.att-thumb {
  width: 80px; height: 80px; object-fit: cover;
  border-radius: 8px; border: 1px solid var(--border);
}
.att-file {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px;
  background: var(--code-bg); border-radius: 8px;
  font-size: 12px; color: var(--muted);
  border: 1px solid var(--border);
}

/* Markdown 内容 */
.md-content { word-break: break-word; }
.md-content p { margin: 0 0 .75em; }
.md-content p:last-child { margin-bottom: 0; }
.md-content h1,.md-content h2,.md-content h3 { margin: 1em 0 .4em; font-weight: 600; }
.md-content ul,.md-content ol { margin: .4em 0; padding-left: 1.4em; }
.md-content li { margin: .2em 0; }
.md-content blockquote {
  border-left: 3px solid var(--border); margin: .6em 0;
  padding: .2em .8em; color: var(--muted);
}
.md-content table { border-collapse: collapse; width: 100%; margin: .6em 0; font-size: 13px; }
.md-content th,.md-content td {
  border: 1px solid var(--border); padding: 6px 10px; text-align: left;
}
.md-content th { background: var(--sb-bg); font-weight: 600; }
.md-content a { color: var(--accent); text-decoration: none; }
.md-content a:hover { text-decoration: underline; }
.md-content hr { border: none; border-top: 1px solid var(--border); margin: 1em 0; }

/* 代码块 */
.code-block-wrap { position: relative; margin: .6em 0; }
.code-block-wrap pre {
  background: var(--code-bg); border-radius: 10px;
  padding: 14px 16px; overflow-x: auto;
  font-size: 13px; line-height: 1.5;
  border: 1px solid var(--border);
}
.copy-btn {
  position: absolute; top: 8px; right: 8px;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 6px; padding: 3px 8px;
  font-size: 11px; color: var(--muted); cursor: pointer;
  opacity: 0; transition: opacity var(--transition);
}
.code-block-wrap:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--text); }

.md-content code:not(pre code) {
  background: var(--code-bg); padding: 1px 5px;
  border-radius: 4px; font-size: .9em;
  border: 1px solid var(--border);
}

/* 思考中 / 流式响应 */
.thinking-dots span {
  display: inline-block; width: 6px; height: 6px;
  background: var(--muted); border-radius: 50%; margin: 0 2px;
  animation: bounce .9s infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: .15s; }
.thinking-dots span:nth-child(3) { animation-delay: .30s; }
@keyframes bounce {
  0%,60%,100% { transform: translateY(0); }
  30%          { transform: translateY(-6px); }
}

.streaming-cursor::after {
  content: '▋'; animation: blink .7s infinite; color: var(--accent);
}
@keyframes blink { 50% { opacity: 0; } }

/* ── 输入区域 ── */
.input-wrap {
  padding: 12px 24px 16px;
  flex-shrink: 0;
  max-width: 860px; width: 100%; margin: 0 auto;
}

.attach-preview {
  display: flex; flex-wrap: wrap; gap: 8px;
  margin-bottom: 8px;
}
.prev-item {
  position: relative; display: flex;
  border-radius: 8px; overflow: hidden;
  border: 1px solid var(--border);
}
.prev-img {
  width: 64px; height: 64px; object-fit: cover;
}
.prev-file {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; background: var(--sb-bg);
  font-size: 12px; max-width: 180px;
}
.prev-file-name {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--text);
}
.prev-remove {
  position: absolute; top: 2px; right: 2px;
  width: 18px; height: 18px;
  background: rgba(0,0,0,.55); color: #fff;
  border: none; border-radius: 50%;
  font-size: 11px; line-height: 18px; text-align: center;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
}

.input-box {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--sb-bg);
  border: 1px solid var(--border); border-radius: var(--input-radius);
  padding: 10px 12px;
  transition: border-color var(--transition);
}
.input-box:focus-within { border-color: var(--muted); }
.input-box.drag-over { border-color: var(--accent); background: var(--sb-hover); }

.attach-icon { color: var(--muted); flex-shrink: 0; }
.attach-icon:hover { color: var(--text); }

#msg-input {
  flex: 1; background: none; border: none; outline: none;
  color: var(--text); font-size: 14.5px; line-height: 1.5;
  resize: none; max-height: 200px; overflow-y: auto;
  font-family: inherit;
}
#msg-input::placeholder { color: var(--muted); }

.send-btn {
  flex-shrink: 0;
  width: 34px; height: 34px;
  background: var(--text); color: var(--bg);
  border: none; border-radius: 10px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: opacity var(--transition);
}
.send-btn:disabled { opacity: .3; cursor: default; }
.send-btn:not(:disabled):hover { opacity: .8; }

.input-hint { font-size: 11px; color: var(--muted); margin-top: 6px; text-align: center; }

/* ── 图标按钮 ── */
.icon-btn {
  display: flex; align-items: center; justify-content: center;
  background: none; border: none; cursor: pointer;
  color: var(--muted); padding: 6px; border-radius: 8px;
  transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--sb-hover); color: var(--text); }

/* ── 响应式 ── */
@media (max-width: 640px) {
  #sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 50; width: var(--sidebar-w); }
  #sidebar.collapsed { transform: translateX(-100%); width: var(--sidebar-w); }
  .msg-bubble { max-width: 90% !important; }
  .input-wrap { padding: 8px 12px 12px; }
}
