:root {
  --bg: #0e1116;
  --bg-soft: #151a21;
  --bg-card: #1a2028;
  --line: #262e3a;
  --text: #e6edf3;
  --text-dim: #8b97a6;
  --text-faint: #5b6675;
  --accent: #4dd4a4;
  --accent-dim: #2b8f6e;
  --user-bubble: #1f3a33;
  --warn: #e0a458;
  --err: #e06c75;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.65 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  overscroll-behavior: none;
}

body { display: flex; overflow: hidden; max-width: 100vw; }

/* ---------------- 侧栏 ---------------- */
#sidebar {
  width: 268px; flex: 0 0 268px;
  background: var(--bg-soft);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  transition: transform .22s ease;
  z-index: 30;
}

.brand {
  padding: 18px 18px 12px;
  display: flex; align-items: center; gap: 10px;
}
.brand .dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--text-faint); flex: none;
  box-shadow: 0 0 0 3px rgba(255,255,255,.04);
}
.brand .dot.on { background: var(--accent); box-shadow: 0 0 10px rgba(77,212,164,.6); }
.brand .dot.err { background: var(--err); }
.brand h1 { font-size: 16px; margin: 0; font-weight: 600; letter-spacing: .5px; }
.brand small { color: var(--text-faint); font-size: 11px; display: block; margin-top: 1px; }

.side-actions { padding: 0 12px 12px; display: flex; gap: 8px; }
.btn {
  border: 1px solid var(--line); background: var(--bg-card); color: var(--text);
  border-radius: 10px; padding: 9px 12px; font-size: 13px; cursor: pointer;
  transition: .15s; font-family: inherit;
}
.btn:hover { border-color: var(--accent-dim); background: #1e2630; }
.btn.primary { background: var(--accent); color: #08211a; border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.wide { flex: 1; }
.btn.ghost { background: transparent; }

.nav-tabs {
  display: flex; gap: 4px; padding: 0 12px 10px;
}
.nav-tabs button {
  flex: 1; background: transparent; border: none; color: var(--text-dim);
  padding: 7px 4px; font-size: 12.5px; cursor: pointer; border-radius: 8px;
  font-family: inherit;
}
.nav-tabs button.active { background: var(--bg-card); color: var(--text); }

.panel { flex: 1; overflow-y: auto; padding: 0 10px 16px; display: none; }
.panel.active { display: block; }
.panel::-webkit-scrollbar { width: 6px; }
.panel::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }

.conv-item {
  padding: 10px 12px; border-radius: 10px; cursor: pointer;
  color: var(--text-dim); font-size: 13.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
}
.conv-item:hover { background: var(--bg-card); color: var(--text); }
.conv-item.active { background: var(--bg-card); color: var(--text); box-shadow: inset 2px 0 0 var(--accent); }
.conv-item .del { opacity: 0; font-size: 12px; color: var(--text-faint); padding: 0 4px; }
.conv-item:hover .del { opacity: 1; }
.conv-item .del:hover { color: var(--err); }

.fact-item {
  background: var(--bg-card); border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 11px; margin-bottom: 8px; font-size: 13px; color: var(--text-dim);
  position: relative;
}
.fact-item .kind {
  font-size: 10px; color: var(--accent); border: 1px solid var(--accent-dim);
  border-radius: 5px; padding: 0 5px; margin-right: 6px;
}
.fact-item .x { position: absolute; right: 8px; top: 7px; color: var(--text-faint); cursor: pointer; font-size: 12px; }
.fact-item .x:hover { color: var(--err); }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.stat {
  background: var(--bg-card); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px; text-align: center;
}
.stat b { display: block; font-size: 18px; color: var(--accent); font-weight: 600; }
.stat span { font-size: 11px; color: var(--text-faint); }

.kv { font-size: 12.5px; color: var(--text-dim); margin: 6px 0; }
.kv b { color: var(--text); font-weight: 500; }

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

#topbar {
  height: 54px; flex: none; display: flex; align-items: center; gap: 10px;
  padding: 0 16px; border-bottom: 1px solid var(--line);
  background: rgba(14,17,22,.82); backdrop-filter: blur(12px);
  position: relative; z-index: 20;
}
#topbar .title { font-size: 14.5px; font-weight: 500; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#topbar .model-tag {
  font-size: 11px; color: var(--text-faint); border: 1px solid var(--line);
  padding: 3px 8px; border-radius: 20px;
}
#menuBtn { display: none; }

#messages {
  flex: 1; overflow-y: auto; padding: 22px 16px 8px;
  scroll-behavior: smooth;
}
#messages::-webkit-scrollbar { width: 8px; }
#messages::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }

.wrap { max-width: 780px; margin: 0 auto; }

.msg { display: flex; gap: 10px; margin-bottom: 20px; animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.avatar {
  width: 28px; height: 28px; border-radius: 9px; flex: none;
  display: grid; place-items: center; font-size: 12px; font-weight: 700;
  background: var(--bg-card); color: var(--text-dim); border: 1px solid var(--line);
}
.msg.assistant .avatar { background: linear-gradient(135deg, #2b8f6e, #1e6b52); color: #d8fff0; border: none; }
.msg.user .avatar { background: #2a3340; color: #a9b6c6; }

.body { flex: 1; min-width: 0; }
.who { font-size: 11.5px; color: var(--text-faint); margin-bottom: 3px; }
.who .route {
  display: inline-block; margin-left: 6px; padding: 0 5px; border-radius: 4px;
  font-size: 10px; line-height: 15px; vertical-align: 1px;
}
.who .route.local { color: var(--accent); border: 1px solid var(--accent-dim); }
.who .route.cloud { color: var(--warn); border: 1px solid rgba(224, 164, 88, .45); }
.content { word-wrap: break-word; overflow-wrap: anywhere; }

.msg.user .content {
  background: var(--user-bubble); border-radius: var(--radius);
  padding: 10px 14px; display: inline-block; max-width: 100%;
}

.content p { margin: 0 0 10px; }
.content p:last-child { margin-bottom: 0; }
.content ul, .content ol { margin: 6px 0 10px; padding-left: 22px; }
.content li { margin: 3px 0; }
.content code {
  background: #0b0f14; border: 1px solid var(--line); border-radius: 5px;
  padding: 1px 5px; font-size: 13px; font-family: "Cascadia Code", Consolas, monospace;
  color: #9ad9bf;
}
.content pre {
  background: #0b0f14; border: 1px solid var(--line); border-radius: 10px;
  padding: 12px 14px; overflow-x: auto; margin: 8px 0 12px;
}
.content pre code { border: none; padding: 0; background: none; font-size: 12.5px; line-height: 1.55; }
.content a { color: var(--accent); text-decoration: none; border-bottom: 1px solid rgba(77,212,164,.3); }
.content a:hover { border-bottom-color: var(--accent); }
.content blockquote {
  margin: 8px 0; padding: 6px 12px; border-left: 3px solid var(--accent-dim);
  color: var(--text-dim); background: rgba(77,212,164,.04); border-radius: 0 8px 8px 0;
}
.content h1, .content h2, .content h3 { font-size: 15.5px; margin: 14px 0 8px; }
.content hr { border: none; border-top: 1px solid var(--line); margin: 14px 0; }
.content table { border-collapse: collapse; margin: 8px 0; font-size: 13.5px; }
.content th, .content td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.content th { background: var(--bg-card); }

.tool-card {
  margin: 8px 0; border: 1px dashed var(--line); border-radius: 10px;
  padding: 7px 11px; font-size: 12px; color: var(--text-faint);
  display: flex; align-items: center; gap: 8px;
}
.tool-card.running .spin {
  width: 10px; height: 10px; border: 2px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.tool-card.done { color: var(--text-dim); }
.tool-card .nm { color: var(--accent); font-family: Consolas, monospace; }

.typing::after {
  content: ""; display: inline-block; width: 7px; height: 15px;
  background: var(--accent); margin-left: 2px; vertical-align: -2px;
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---------------- 输入区 ---------------- */
#composer {
  flex: none; padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 60%, rgba(14,17,22,0));
}
.inputbox {
  max-width: 780px; margin: 0 auto;
  background: var(--bg-card); border: 1px solid var(--line); border-radius: 16px;
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 8px 8px 14px;
  transition: border-color .18s;
}
.inputbox:focus-within { border-color: var(--accent-dim); box-shadow: 0 0 0 3px rgba(77,212,164,.06); }
#input {
  flex: 1; background: none; border: none; outline: none; color: var(--text);
  font: inherit; resize: none; max-height: 180px; line-height: 1.55;
  padding: 7px 0; font-family: inherit;
}
#input::placeholder { color: var(--text-faint); }
#send {
  width: 34px; height: 34px; flex: none; border-radius: 10px; border: none;
  background: var(--accent); color: #08211a; cursor: pointer; font-size: 16px;
  display: grid; place-items: center; transition: .15s;
}
#send:disabled { background: var(--line); color: var(--text-faint); cursor: not-allowed; }
#send:not(:disabled):hover { filter: brightness(1.1); }

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

/* ---------------- 空态 ---------------- */
.empty { text-align: center; padding: 12vh 20px 0; color: var(--text-faint); }
.empty .big { font-size: 30px; margin-bottom: 10px; }
.empty h2 { color: var(--text); font-size: 19px; font-weight: 600; margin: 0 0 8px; }
.empty p { font-size: 13.5px; margin: 0 0 22px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; max-width: 560px; margin: 0 auto; padding: 0 4px; }
.chip {
  border: 1px solid var(--line); background: var(--bg-card); color: var(--text-dim);
  border-radius: 20px; padding: 8px 14px; font-size: 12.5px; cursor: pointer;
  font-family: inherit; transition: .15s; max-width: 100%;
  white-space: normal; word-break: break-word; text-align: center; line-height: 1.4;
}
.chip:hover { border-color: var(--accent-dim); color: var(--text); }

/* ---------------- 移动端 ---------------- */
@media (max-width: 760px) {
  #sidebar {
    position: fixed; inset: 0 auto 0 0; transform: translateX(-100%);
    box-shadow: var(--shadow);
  }
  #sidebar.open { transform: none; }
  #menuBtn { display: grid; }
  #topbar { padding: 0 10px; height: 50px; padding-top: env(safe-area-inset-top); }
  #messages { padding: 14px 10px 6px; }
  .msg { gap: 8px; margin-bottom: 16px; }
  .avatar { width: 25px; height: 25px; font-size: 11px; }
  #composer { padding: 8px 10px calc(10px + env(safe-area-inset-bottom)); }
  .hint { display: none; }
  .chips { flex-direction: column; align-items: stretch; gap: 7px; padding: 0; }
  .chip { width: auto; align-self: stretch; text-align: center; }
  .empty { padding: 10vh 16px 0; overflow-x: clip; }
}

#scrim {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5);
  z-index: 25; backdrop-filter: blur(2px);
}
#scrim.on { display: block; }

.iconbtn {
  width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--line);
  background: var(--bg-card); color: var(--text-dim); cursor: pointer;
  display: grid; place-items: center; font-size: 15px; flex: none;
}
.iconbtn:hover { color: var(--text); border-color: var(--accent-dim); }

.toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--bg-card); border: 1px solid var(--line); color: var(--text);
  padding: 9px 16px; border-radius: 10px; font-size: 13px; opacity: 0;
  transition: .25s; pointer-events: none; z-index: 100; box-shadow: var(--shadow);
  max-width: 80vw;
}
.toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------------- 口令闸门 ---------------- */
.gate {
  position: fixed; inset: 0; background: rgba(8, 10, 14, .9);
  backdrop-filter: blur(8px); z-index: 200;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.gate-box {
  background: var(--bg-card); border: 1px solid var(--line); border-radius: 16px;
  padding: 22px 20px; width: 100%; max-width: 340px; box-shadow: var(--shadow);
  box-sizing: border-box;
}
.gate-box h3 { margin: 0 0 10px; font-size: 16px; }
.gate-box p { margin: 0 0 16px; font-size: 12.5px; color: var(--text-dim); line-height: 1.7; }
.gate-box code {
  background: #0b0f14; border: 1px solid var(--line); border-radius: 5px;
  padding: 1px 5px; font-size: 11.5px; color: #9ad9bf; word-break: break-all;
}
.gate-box input {
  width: 100%; background: var(--bg-soft); border: 1px solid var(--line);
  border-radius: 10px; padding: 11px 13px; color: var(--text); font-family: inherit;
  font-size: 14px; outline: none;
}
.gate-box input:focus { border-color: var(--accent-dim); }
.gate-err { color: var(--err); font-size: 12px; margin-top: 8px; min-height: 16px; }
