:root {
  --bg: #ffffff;
  --bg2: #f7f8fa;
  --panel: #f9fafb;
  --hover: #f2f3f5;
  --border: #e7e9ee;
  --text: #1a1a1a;
  --muted: #8b8e98;
  --accent: #4d6bfe;
  --accent-soft: #eef1ff;
  --user-bubble: #f2f3f5;
  --danger: #e5484d;
}
body.dark {
  --bg: #1a1d24;
  --bg2: #232733;
  --panel: #1f222b;
  --hover: #2a2e39;
  --border: #333844;
  --text: #e6e8ec;
  --muted: #8b8e98;
  --accent: #5c78ff;
  --accent-soft: #2a3350;
  --user-bubble: #2a2e39;
  --danger: #f87171;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: -apple-system, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', system-ui, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}
.app { display: flex; height: 100vh; }

.hamburger {
  display: none; background: transparent; border: 0; cursor: pointer;
  font-size: 20px; padding: 6px 8px; border-radius: 8px; color: var(--text);
}
.hamburger:hover { background: var(--hover); }

/* ---- sidebar ---- */
.sidebar {
  width: 260px; background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
}
.logo { display: flex; align-items: center; gap: 10px; padding: 18px 16px 12px; }
.logo-mark { font-size: 26px; line-height: 1; }
.logo-text { font-size: 19px; font-weight: 700; letter-spacing: -0.3px; }
.new-chat-btn {
  margin: 4px 12px 12px; background: var(--accent-soft); color: var(--accent);
  border: 0; border-radius: 10px; padding: 11px; font-weight: 600; cursor: pointer;
  font-size: 14px; display: flex; align-items: center; justify-content: center; gap: 6px;
}
.new-chat-btn:hover { filter: brightness(1.05); }
.plus { font-size: 16px; font-weight: 400; }

.search-box { padding: 0 12px 8px; }
#chatSearch {
  width: 100%; background: var(--bg2); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 13px; outline: none;
}
#chatSearch:focus { border-color: var(--accent); }

.chat-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.chat-item {
  position: relative;
  padding: 10px 12px; border-radius: 8px; cursor: pointer; margin-bottom: 2px;
  display: flex; flex-direction: column; gap: 2px;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--hover); }
.ci-title { font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 42px; }
.ci-meta { font-size: 11px; color: var(--muted); }
.ci-empty { padding: 16px; color: var(--muted); font-size: 13px; text-align: center; }
.pin-btn {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  background: transparent; border: 0; color: var(--muted); font-size: 11px; cursor: pointer;
  padding: 2px 4px; border-radius: 4px;
}
.pin-btn:hover { color: var(--accent); background: var(--bg2); }

.sidebar-footer { padding: 12px; border-top: 1px solid var(--border); }
.side-row { display: flex; align-items: center; gap: 4px; }
.side-tab {
  flex: 1; padding: 9px 12px; cursor: pointer; color: var(--text); border-radius: 8px; font-size: 13.5px;
}
.side-tab:hover { background: var(--hover); }
.del-all {
  width: 100%; background: transparent; color: var(--danger); border: 0;
  border-radius: 8px; padding: 9px; cursor: pointer; font-size: 13px; margin-bottom: 4px;
}
.del-all:hover { background: rgba(229,72,77,0.08); }
.dark-toggle { font-size: 15px; padding: 8px; }
.profile { display: flex; align-items: center; gap: 10px; padding: 10px 4px 4px; }
.avatar {
  width: 28px; height: 28px; border-radius: 50%; background: linear-gradient(135deg,#4d6bfe,#7c5cff);
  color: #fff; font-size: 13px; font-weight: 600; display: flex; align-items: center; justify-content: center;
}
.profile-name { font-size: 13.5px; font-weight: 500; }
.key-line { font-size: 11px; color: var(--muted); margin-top: 6px; }

/* ---- main ---- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.view { display: none; flex-direction: column; height: 100%; }
.view.active { display: flex; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--bg);
  max-width: 900px; margin: 0 auto; width: 100%;
}
.topbar-center { display: flex; align-items: center; gap: 8px; }
.model-pill {
  background: var(--bg2); color: var(--text); border: 1px solid var(--border);
  padding: 7px 14px; border-radius: 999px; font-size: 13px; cursor: pointer; outline: none;
}
.topbar-actions { display: flex; gap: 8px; }

/* ---- messages ---- */
.messages { flex: 1; overflow-y: auto; padding: 8px 4px; position: relative; }
.msg { display: flex; flex-direction: column; margin-bottom: 28px; }
.msg .role { font-size: 13px; font-weight: 600; color: var(--text); margin-bottom: 6px; padding: 0 8px; }
.msg .bubble {
  padding: 2px 8px; line-height: 1.7; word-wrap: break-word; white-space: pre-wrap;
  font-size: 15px; max-width: 100%;
}
.msg.user { align-items: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); border-radius: 14px; padding: 12px 16px;
  margin: 0 12px; max-width: 85%;
}
.msg.assistant { max-width: 100%; }
.msg.assistant .bubble { max-width: 860px; margin: 0 auto; width: 100%; white-space: normal; }
.msg.assistant .role { max-width: 860px; margin: 0 auto 6px; width: 100%; }
.msg.assistant .bubble code {
  background: var(--bg2); padding: 2px 6px; border-radius: 5px; font-family: 'SF Mono', Consolas, monospace; font-size: 13px;
}
.msg.assistant .bubble pre.code-block {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 10px;
  overflow: hidden; margin: 8px 0;
}
.code-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 12px; background: var(--panel); border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}
.code-head .copy-code { background: transparent; border: 0; color: var(--accent); cursor: pointer; font-size: 12px; }
.code-block code { display: block; padding: 12px; overflow-x: auto; font-size: 13px; background: transparent; }
.msg.assistant .bubble h1, .msg.assistant .bubble h2, .msg.assistant .bubble h3 { margin: 12px 0 6px; }
.msg.assistant .bubble h4, .msg.assistant .bubble h5, .msg.assistant .bubble h6 { margin: 10px 0 4px; }
.msg.assistant .bubble p { margin: 6px 0; }
.msg.assistant .bubble ul, .msg.assistant .bubble ol { padding-left: 22px; margin: 6px 0; }
.msg.assistant .bubble a { color: var(--accent); }
.msg-img {
  display: block; max-width: 240px; max-height: 240px; border-radius: 10px;
  margin: 6px 0; border: 1px solid var(--border);
}

.msg-actions { display: flex; gap: 6px; margin-top: 4px; padding: 0 8px; }
.msg.user .msg-actions { justify-content: flex-end; margin-right: 12px; }
.msg.assistant .msg-actions { max-width: 860px; margin: 4px auto 0; width: 100%; }
.msg-actions button {
  background: transparent; border: 0; color: var(--muted); cursor: pointer;
  font-size: 12px; padding: 3px 8px; border-radius: 6px;
}
.msg-actions button:hover { background: var(--hover); color: var(--text); }

.empty-hint { color: var(--muted); text-align: center; margin-top: 120px; font-size: 15px; }

.scroll-bottom {
  position: absolute; bottom: 16px; right: 24px; display: none;
  width: 36px; height: 36px; border-radius: 50%; background: var(--accent); color: #fff;
  border: 0; cursor: pointer; font-size: 18px; align-items: center; justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25); z-index: 10;
}

/* ---- composer ---- */
.composer { padding: 8px 16px 12px; background: var(--bg); }
.composer-inner { max-width: 800px; margin: 0 auto; }
.preview-row { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.prev-item { position: relative; }
.prev-item img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); }
.prev-text {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  width: 72px; height: 64px; background: var(--bg2); border: 1px solid var(--border);
  border-radius: 10px; gap: 2px;
}
.pt-icon { font-size: 20px; }
.pt-name { font-size: 10px; color: var(--muted); max-width: 66px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.prev-x {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%;
  background: #333; color: #fff; border: 0; cursor: pointer; font-size: 12px; line-height: 1;
}
.composer-box {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg); border: 1px solid #d7dae0; border-radius: 16px; padding: 8px 10px;
  transition: border-color 0.15s;
}
body.dark .composer-box { border-color: var(--border); }
.composer-box:focus-within { border-color: var(--accent); }
#chatInput {
  flex: 1; border: 0; outline: none; resize: none; background: transparent;
  font-family: inherit; font-size: 15px; line-height: 1.5; max-height: 200px; padding: 6px 4px;
  color: var(--text);
}
#chatInput::placeholder { color: #b0b3bd; }
.composer-actions { display: flex; align-items: center; gap: 6px; }
.tool-toggle {
  display: flex; align-items: center; gap: 6px; background: var(--accent-soft); color: var(--accent);
  border: 0; border-radius: 999px; padding: 6px 12px; font-size: 12.5px; font-weight: 500; cursor: pointer;
}
.tool-toggle:hover { filter: brightness(1.05); }
.tt-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.icon-btn {
  background: transparent; border: 0; cursor: pointer; font-size: 16px; padding: 6px; border-radius: 8px;
}
.icon-btn:hover { background: var(--hover); }
.send-btn {
  width: 32px; height: 32px; border-radius: 50%; background: var(--accent); color: #fff;
  border: 0; cursor: pointer; font-size: 17px; display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
}
.send-btn:disabled { background: #d7dae0; cursor: not-allowed; }
body.dark .send-btn:disabled { background: var(--border); }
.send-btn:not(:disabled):hover { filter: brightness(1.1); }
.disclaimer { text-align: center; font-size: 11.5px; color: #b0b3bd; margin-top: 8px; }

button.ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); border-radius: 8px; padding: 7px 12px; font-size: 13px; cursor: pointer; }
button.ghost:hover { background: var(--hover); color: var(--text); }
button.ghost.danger { color: var(--danger); }

/* ---- terminal ---- */
.term-head {
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  font-family: Consolas, monospace; font-size: 12px; color: var(--muted);
}
.term-body {
  flex: 1; overflow-y: auto; padding: 12px 16px;
  background: #0d1117; font-family: Consolas, monospace; font-size: 13px; color: #e6edf3;
}
.term-line { white-space: pre-wrap; word-break: break-all; line-height: 1.45; }
.term-line.cmd { color: #58a6ff; }
.term-line.out { color: #e6edf3; }
.term-line.err { color: #f85149; }
.term-input {
  display: flex; align-items: center; gap: 8px; padding: 10px 16px;
  border-top: 1px solid var(--border); background: #0d1117;
}
.prompt { color: #3fb950; font-family: Consolas, monospace; }
#termInput {
  flex: 1; background: transparent; border: 0; color: #e6edf3;
  font-family: Consolas, monospace; font-size: 13px; outline: none;
}

/* ---- gate ---- */
#gate {
  position: fixed; inset: 0; background: var(--bg); z-index: 100;
  display: flex; align-items: center; justify-content: center;
}
#gate.hidden { display: none; }
.gate-box { text-align: center; width: 340px; }
.gate-logo { font-size: 44px; margin-bottom: 8px; }
.gate-box h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.3px; margin-bottom: 8px; }
.gate-box p { color: var(--muted); font-size: 14px; margin-bottom: 18px; }
#gateKey {
  width: 100%; background: var(--bg); color: var(--text); border: 1px solid #d7dae0;
  border-radius: 10px; padding: 12px; margin-bottom: 14px; font-size: 15px; text-align: center; outline: none;
}
body.dark #gateKey { border-color: var(--border); }
#gateKey:focus { border-color: var(--accent); }
#gateBtn {
  width: 100%; background: var(--accent); color: #fff; border: 0; border-radius: 10px;
  padding: 12px; font-weight: 600; cursor: pointer; font-size: 15px;
}
#gateBtn:hover { filter: brightness(1.1); }
.gate-err { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 16px; }

/* ---- settings + modal + stop + syntax ---- */
.settings-body { padding: 20px; max-width: 600px; overflow-y: auto; flex: 1; }
.setting-group { margin-bottom: 18px; }
.setting-group label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.setting-group textarea, .setting-group input {
  width: 100%; background: var(--bg2); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px; font-size: 14px; font-family: inherit; outline: none;
}
.setting-group textarea:focus, .setting-group input:focus { border-color: var(--accent); }
.save-settings {
  background: var(--accent); color: #fff; border: 0; border-radius: 8px;
  padding: 10px 18px; font-weight: 600; cursor: pointer; font-size: 14px;
}
.save-settings:hover { filter: brightness(1.1); }

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 150;
  display: flex; align-items: center; justify-content: center;
}
.modal-box {
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  width: 90%; max-width: 420px; max-height: 70vh; display: flex; flex-direction: column;
}
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 16px; border-bottom: 1px solid var(--border); font-weight: 600;
}
.modal-body { padding: 12px 16px; overflow-y: auto; }
.modal-foot { padding: 12px 16px; border-top: 1px solid var(--border); }
.trash-item {
  display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border);
}
.trash-item .ci-title { flex: 1; }
.trash-item button {
  background: transparent; border: 0; color: var(--accent); cursor: pointer; font-size: 12px; padding: 4px 6px;
}
.trash-item button:hover { background: var(--hover); }

.stop-btn {
  position: absolute; bottom: 16px; right: 68px; display: none;
  width: 36px; height: 36px; border-radius: 50%; background: var(--danger); color: #fff;
  border: 0; cursor: pointer; font-size: 14px; align-items: center; justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25); z-index: 10;
}

.tk-kw { color: #c678dd; }
.tk-str { color: #98c379; }
.tk-com { color: #7f848e; font-style: italic; }
.tk-num { color: #d19a66; }

/* ---- responsive ---- */
@media (max-width: 768px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 50;
    transform: translateX(-100%); transition: transform 0.2s ease;
  }
  .sidebar.open { transform: translateX(0); }
  .hamburger { display: block; }
  .main { width: 100%; }
  .topbar { max-width: 100%; }
  .msg.assistant .bubble, .msg.assistant .role { max-width: 100%; }
  .msg.user .bubble { max-width: 92%; }
  .composer-actions .tool-toggle { display: none; }
}
