:root {
  --bg: #0f1115;
  --bg-elev: #171a21;
  --bg-elev2: #1f232c;
  --bg-input: #232834;
  --border: #2a2f3a;
  --border-soft: #232733;
  --text: #e9ecf1;
  --text-dim: #9aa3b2;
  --text-faint: #6b7280;
  --accent: #5b8cff;
  --accent-soft: rgba(91, 140, 255, 0.15);
  --accent-2: #38d39f;
  --danger: #ff6b6b;
  --user-bubble: #2b3140;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f6f8;
    --bg-elev: #ffffff;
    --bg-elev2: #f0f2f5;
    --bg-input: #eef0f4;
    --border: #e2e5ea;
    --border-soft: #eaecf0;
    --text: #1c2230;
    --text-dim: #5b6472;
    --text-faint: #98a0ad;
    --accent: #3b6fff;
    --accent-soft: rgba(59, 111, 255, 0.12);
    --user-bubble: #e7ecff;
    --shadow: 0 8px 30px rgba(20, 30, 60, 0.12);
  }
}

* { box-sizing: border-box; }

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

button { font-family: inherit; cursor: pointer; }
.hidden { display: none !important; }

/* ---------------- 登录 ---------------- */
.login-gate {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(1200px 600px at 50% -10%, rgba(91,140,255,0.18), transparent), var(--bg);
  padding: 24px;
}
.login-card {
  width: 100%; max-width: 360px; text-align: center;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 22px; padding: 36px 26px; box-shadow: var(--shadow);
}
.login-logo {
  width: 56px; height: 56px; margin: 0 auto 18px;
  border-radius: 16px; display: grid; place-items: center;
  font-weight: 700; font-size: 20px; color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.login-card h1 { font-size: 20px; margin: 0 0 6px; }
.login-sub { color: var(--text-dim); margin: 0 0 22px; font-size: 14px; }
#login-form { display: flex; flex-direction: column; gap: 12px; }
#login-name {
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 12px; padding: 13px 14px; color: var(--text); font-size: 16px;
  outline: none; transition: border-color .15s;
}
#login-name:focus { border-color: var(--accent); }
.btn-primary {
  background: var(--accent); color: #fff; border: none;
  border-radius: 12px; padding: 13px; font-size: 16px; font-weight: 600;
  transition: filter .15s;
}
.btn-primary:active { filter: brightness(.92); }
.login-tip { color: var(--text-faint); font-size: 12px; margin: 16px 0 0; }

/* ---------------- 布局 ---------------- */
.app { height: 100%; position: relative; overflow: hidden; }
.main {
  height: 100%; display: flex; flex-direction: column;
  background: var(--bg);
}

/* ---------------- 顶栏 ---------------- */
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--border-soft);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  position: relative; z-index: 5;
}
.topbar-title { flex: 1; min-width: 0; text-align: center; }
.title-main {
  font-size: 15px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.title-sub { font-size: 11px; color: var(--text-faint); }

.icon-btn {
  width: 38px; height: 38px; flex: 0 0 auto;
  display: grid; place-items: center;
  background: transparent; border: none; color: var(--text);
  border-radius: 10px; font-size: 18px; transition: background .15s;
}
.icon-btn:active { background: var(--bg-elev2); }

.hamburger, .hamburger::before, .hamburger::after {
  content: ""; display: block; width: 18px; height: 2px;
  background: var(--text); border-radius: 2px; position: relative;
}
.hamburger { position: relative; }
.hamburger::before { position: absolute; top: -6px; }
.hamburger::after { position: absolute; top: 6px; }

/* ---------------- 抽屉 ---------------- */
.overlay {
  position: absolute; inset: 0; z-index: 19;
  background: rgba(0,0,0,0.5); opacity: 0; pointer-events: none;
  transition: opacity .25s;
}
.overlay.show { opacity: 1; pointer-events: auto; }

.drawer {
  position: absolute; top: 0; left: 0; bottom: 0; z-index: 20;
  width: 84%; max-width: 320px;
  background: var(--bg-elev); border-right: 1px solid var(--border);
  transform: translateX(-100%); transition: transform .25s cubic-bezier(.4,0,.2,1);
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top, 0px);
}
.drawer.open { transform: translateX(0); }

.drawer-head {
  display: flex; align-items: center; gap: 8px;
  padding: 14px 12px 10px;
}
.user-card {
  flex: 1; display: flex; align-items: center; gap: 10px;
  padding: 6px; border-radius: 12px; transition: background .15s;
}
.user-card:active { background: var(--bg-elev2); }
.user-avatar {
  width: 38px; height: 38px; border-radius: 11px; flex: 0 0 auto;
  display: grid; place-items: center; color: #fff; font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.user-meta { min-width: 0; }
.user-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 12px; color: var(--text-faint); }

.search-row { padding: 4px 12px 10px; }
#search-input {
  width: 100%; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px; color: var(--text); font-size: 14px; outline: none;
}
#search-input:focus { border-color: var(--accent); }

.drawer-actions { display: flex; gap: 8px; padding: 0 12px 10px; }
.action-chip {
  flex: 1; background: var(--bg-elev2); border: 1px solid var(--border);
  color: var(--text); border-radius: 10px; padding: 10px; font-size: 13px; font-weight: 500;
  transition: background .15s;
}
.action-chip:active { background: var(--bg-input); }

.drawer-scroll { flex: 1; overflow-y: auto; padding: 4px 8px 20px; }
.drawer-section { margin-bottom: 10px; }
.section-title {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 8px 4px; font-size: 12px; color: var(--text-faint); font-weight: 600;
  letter-spacing: .03em;
}

/* 项目项 */
.project-item, .session-item {
  display: flex; align-items: center; gap: 8px;
  padding: 10px; border-radius: 10px; cursor: pointer;
  transition: background .12s;
}
.project-item:active, .session-item:active { background: var(--bg-elev2); }
.project-item.active, .session-item.active { background: var(--accent-soft); }
.project-icon { flex: 0 0 auto; width: 26px; height: 26px; border-radius: 8px;
  display: grid; place-items: center; background: var(--bg-elev2); font-size: 14px; }
.item-body { flex: 1; min-width: 0; }
.item-title { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-sub { font-size: 11px; color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-menu { flex: 0 0 auto; width: 28px; height: 28px; border-radius: 8px; border: none;
  background: transparent; color: var(--text-dim); font-size: 16px; display: grid; place-items: center; }
.item-menu:active { background: var(--bg-input); }
.pin-flag { color: var(--accent); font-size: 11px; margin-left: 4px; }

.group-label {
  font-size: 11px; color: var(--text-faint); padding: 12px 8px 4px; font-weight: 600;
}
.empty-hint { color: var(--text-faint); font-size: 13px; padding: 10px; text-align: center; }

.all-sessions-item { font-weight: 600; }

/* ---------------- 消息区 ---------------- */
.message-area { flex: 1; overflow-y: auto; position: relative; scroll-behavior: smooth; }
.message-list { max-width: 860px; margin: 0 auto; padding: 16px 14px 24px; }

.empty-state {
  height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 24px; gap: 6px;
}
.empty-logo {
  width: 52px; height: 52px; border-radius: 15px; margin-bottom: 8px;
  display: grid; place-items: center; color: #fff; font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.empty-state h2 { margin: 0; font-size: 19px; }
.empty-state p { margin: 0 0 14px; color: var(--text-dim); }
.suggest-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; max-width: 420px; }
.suggest {
  background: var(--bg-elev); border: 1px solid var(--border); color: var(--text);
  border-radius: 12px; padding: 12px; font-size: 13px; text-align: left; transition: border-color .15s;
}
.suggest:active { border-color: var(--accent); }

/* 消息行 */
.msg { margin-bottom: 18px; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.msg.assistant { align-items: stretch; }

.bubble-user {
  background: var(--user-bubble); padding: 10px 14px; border-radius: 16px 16px 4px 16px;
  max-width: 86%; white-space: pre-wrap; word-break: break-word;
}
.bubble-assistant {
  width: 100%; word-break: break-word;
}
.assistant-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.assistant-avatar {
  width: 24px; height: 24px; border-radius: 8px; flex: 0 0 auto;
  display: grid; place-items: center; color: #fff; font-size: 12px; font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.assistant-name { font-size: 12px; color: var(--text-dim); }

.md { font-size: 15px; }
.md p { margin: 0 0 10px; }
.md p:last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 14px 0 8px; line-height: 1.35; }
.md h1 { font-size: 20px; } .md h2 { font-size: 18px; } .md h3 { font-size: 16px; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 22px; }
.md li { margin: 3px 0; }
.md blockquote { margin: 0 0 10px; padding: 6px 12px; border-left: 3px solid var(--accent);
  background: var(--bg-elev2); border-radius: 0 8px 8px 0; color: var(--text-dim); }
.md hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
.md a { color: var(--accent); text-decoration: none; }
.md code.inline {
  background: var(--bg-elev2); padding: 1px 6px; border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px;
}
.code-block { margin: 0 0 12px; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--bg-elev2); }
.code-bar { display: flex; align-items: center; justify-content: space-between;
  padding: 6px 10px; background: var(--bg-elev); border-bottom: 1px solid var(--border); }
.code-lang { font-size: 11px; color: var(--text-faint); text-transform: uppercase; }
.code-copy { margin-left: auto; background: transparent; border: none; color: var(--text-dim); font-size: 12px; padding: 2px 6px; border-radius: 6px; }
.code-copy:active { background: var(--bg-input); }
.code-block pre { margin: 0; padding: 12px; overflow-x: auto; }
.code-block code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px; line-height: 1.55; white-space: pre;
}

/* 流式光标 */
.cursor-blink::after {
  content: "▍"; color: var(--accent); animation: blink 1s steps(1) infinite; margin-left: 1px;
}
@keyframes blink { 50% { opacity: 0; } }

.typing-dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing-dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); animation: bounce 1.2s infinite; }
.typing-dots i:nth-child(2) { animation-delay: .15s; }
.typing-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity:.5 } 30% { transform: translateY(-4px); opacity:1 } }

/* 消息操作栏 */
.msg-actions { display: flex; gap: 2px; margin-top: 6px; opacity: .9; }
.msg .msg-actions { flex-wrap: wrap; }
.act-btn {
  background: transparent; border: none; color: var(--text-dim);
  font-size: 12px; padding: 5px 8px; border-radius: 8px; display: inline-flex; align-items: center; gap: 4px;
  transition: background .12s, color .12s;
}
.act-btn:active { background: var(--bg-elev2); }
.act-btn.danger:active { color: var(--danger); }
.msg-error { color: var(--danger); font-size: 13px; margin-top: 4px; }

/* ---------------- 底部输入 ---------------- */
.composer {
  border-top: 1px solid var(--border-soft);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  padding: 8px 10px calc(8px + var(--safe-bottom));
}
.composer-inner {
  max-width: 860px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 22px; padding: 6px 6px 6px 4px;
}
.composer-attach { width: 36px; height: 36px; font-size: 22px; color: var(--text-dim); }
.composer-mid { flex: 1; min-width: 0; }
.attach-preview { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 4px 2px; }
.attach-chip { display: inline-flex; align-items: center; gap: 6px; background: var(--bg-elev2);
  border: 1px solid var(--border); border-radius: 8px; padding: 4px 8px; font-size: 12px; max-width: 180px; }
.attach-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-chip button { background: none; border: none; color: var(--text-dim); font-size: 14px; }
#input {
  width: 100%; resize: none; border: none; outline: none; background: transparent;
  color: var(--text); font-size: 16px; line-height: 1.5; max-height: 160px; padding: 8px 4px;
}
.send-btn {
  width: 38px; height: 38px; flex: 0 0 auto; border: none; border-radius: 50%;
  background: var(--accent); color: #fff; display: grid; place-items: center;
  font-size: 18px; transition: filter .15s, opacity .15s; align-self: flex-end;
}
.send-btn:disabled { opacity: .4; }
.send-btn:active { filter: brightness(.9); }
.stop-btn { background: var(--text); }
.stop-icon { width: 12px; height: 12px; background: var(--bg); border-radius: 3px; }
.composer-hint { text-align: center; font-size: 11px; color: var(--text-faint); margin-top: 5px; }

/* ---------------- 弹窗 ---------------- */
.modal-mask {
  position: fixed; inset: 0; z-index: 60; display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0,0,0,.5); padding: 0; animation: fade .2s;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  width: 100%; max-width: 460px; background: var(--bg-elev);
  border-radius: 20px 20px 0 0; padding: 20px 18px calc(20px + var(--safe-bottom));
  box-shadow: var(--shadow); animation: slideup .25s cubic-bezier(.4,0,.2,1);
}
@keyframes slideup { from { transform: translateY(100%); } }
.modal h3 { margin: 0 0 14px; font-size: 17px; }
.modal label { display: block; font-size: 13px; color: var(--text-dim); margin: 12px 0 6px; }
.modal input, .modal textarea {
  width: 100%; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 10px; padding: 11px 12px; color: var(--text); font-size: 15px; outline: none;
  resize: vertical;
}
.modal input:focus, .modal textarea:focus { border-color: var(--accent); }
.modal textarea { min-height: 72px; line-height: 1.5; }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.modal-actions button { flex: 1; padding: 12px; border-radius: 11px; font-size: 15px; font-weight: 600; border: none; }
.btn-ghost { background: var(--bg-elev2); color: var(--text); border: 1px solid var(--border) !important; }
.btn-confirm { background: var(--accent); color: #fff; }
.btn-confirm.danger { background: var(--danger); }

/* 下拉操作菜单 */
.sheet-list { display: flex; flex-direction: column; }
.sheet-item { display: flex; align-items: center; gap: 10px; padding: 14px 8px; font-size: 15px;
  border: none; background: transparent; color: var(--text); text-align: left; border-radius: 10px; }
.sheet-item:active { background: var(--bg-elev2); }
.sheet-item.danger { color: var(--danger); }
.sheet-item .si-icon { width: 22px; text-align: center; }

/* ---------------- 提示 ---------------- */
.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%);
  background: var(--bg-elev2); color: var(--text); border: 1px solid var(--border);
  padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 80; box-shadow: var(--shadow);
  animation: fade .2s;
}

/* ---------------- 桌面端适配 ---------------- */
@media (min-width: 860px) {
  .drawer {
    transform: translateX(0); position: relative;
    width: 300px; box-shadow: none;
  }
  .drawer.collapsed { display: none; }
  .app { display: flex; }
  .overlay { display: none; }
  .main { flex: 1; min-width: 0; }
  .topbar-title { text-align: left; padding-left: 4px; }
  .modal-mask { align-items: center; }
  .modal { border-radius: 20px; padding-bottom: 20px; }
  .suggest:hover { border-color: var(--accent); }
  .icon-btn:hover { background: var(--bg-elev2); }
  .session-item:hover, .project-item:hover { background: var(--bg-elev2); }
}
