:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
    monospace;
}

:root,
:root[data-theme="dark"] {
  --bg-main: #212121;
  --bg-sidebar: #171717;
  --bg-elev: #2f2f2f;
  --bg-input: #2f2f2f;
  --bg-hover: rgba(255, 255, 255, 0.07);
  --bg-active: rgba(255, 255, 255, 0.1);
  --border: rgba(255, 255, 255, 0.12);
  --border-soft: rgba(255, 255, 255, 0.08);
  --text: #ececec;
  --text-secondary: #a3a3a3;
  --text-faint: #7f7f7f;
  --accent: #10a37f;
  --accent-hover: #0e8f70;
  --brand: #f59e0b;
  --brand2: #f97316;
  --danger: #ef4444;
  --ok: #4ade80;
  --code-bg: #171717;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg-main: #ffffff;
  --bg-sidebar: #f7f7f8;
  --bg-elev: #ffffff;
  --bg-input: #ffffff;
  --bg-hover: rgba(0, 0, 0, 0.05);
  --bg-active: rgba(0, 0, 0, 0.08);
  --border: rgba(0, 0, 0, 0.18);
  --border-soft: rgba(0, 0, 0, 0.08);
  --text: #1f1f1f;
  --text-secondary: #4d4d4d;
  --text-faint: #8e8e8e;
  --accent: #10a37f;
  --accent-hover: #0e8f70;
  --code-bg: #0d1117;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  background: var(--bg-main);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  border: 0;
  background: none;
  color: inherit;
  cursor: pointer;
}

input,
textarea {
  font-family: inherit;
  color: inherit;
}

.hidden {
  display: none !important;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  color: var(--text-secondary);
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.icon-btn svg {
  width: 19px;
  height: 19px;
}

.icon-sun {
  display: none;
}

.icon-moon {
  display: block;
}

:root[data-theme="light"] .icon-sun {
  display: block;
}

:root[data-theme="light"] .icon-moon {
  display: none;
}

/* ---------- 登录 ---------- */
.login-view {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(1100px 500px at 80% -10%, rgba(16, 163, 127, 0.14), transparent 60%),
    radial-gradient(900px 500px at 10% 110%, rgba(245, 158, 11, 0.12), transparent 60%),
    var(--bg-main);
}

.login-card {
  width: min(380px, 100%);
  background: var(--bg-elev);
  border: 1px solid var(--border-soft);
  border-radius: 20px;
  padding: 36px 32px;
  box-shadow: var(--shadow);
  text-align: center;
}

.login-logo {
  display: flex;
  justify-content: center;
  margin-bottom: 18px;
}

.login-logo .spark {
  width: 52px;
  height: 52px;
  color: var(--brand);
  filter: drop-shadow(0 0 14px rgba(245, 158, 11, 0.35));
}

.login-card h1 {
  margin: 0 0 8px;
  font-size: 24px;
  font-weight: 650;
}

.login-hint {
  margin: 0 0 24px;
  color: var(--text-secondary);
  font-size: 14px;
}

.login-input {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.login-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(16, 163, 127, 0.22);
}

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin: 10px 0 0;
}

.login-submit {
  width: 100%;
  height: 44px;
  margin-top: 20px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  border-radius: 12px;
  transition: background 0.15s, transform 0.1s;
}

.login-submit:hover {
  background: var(--accent-hover);
}

.login-submit:active {
  transform: scale(0.98);
}

.login-submit:disabled {
  opacity: 0.6;
  cursor: default;
}

/* ---------- 布局 ---------- */
.app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  width: 260px;
  flex: 0 0 260px;
  display: flex;
  flex-direction: column;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-soft);
  transition: transform 0.22s ease;
}

.sidebar-head {
  padding: 12px;
}

.new-chat-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 11px;
  font-size: 14px;
  color: var(--text);
  transition: background 0.15s;
}

.new-chat-btn:hover {
  background: var(--bg-hover);
}

.new-chat-btn svg {
  width: 17px;
  height: 17px;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 2px 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.conv-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 9px 32px 9px 12px;
  border-radius: 9px;
  color: var(--text-secondary);
  font-size: 13.5px;
  text-align: left;
  line-height: 1.35;
  transition: background 0.15s, color 0.15s;
}

.conv-item:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.conv-item.active {
  background: var(--bg-active);
  color: var(--text);
}

.conv-item .conv-icon {
  width: 15px;
  height: 15px;
  flex: 0 0 15px;
  opacity: 0.7;
}

.conv-item .conv-title {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.conv-del {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  width: 26px;
  height: 26px;
  display: none;
  align-items: center;
  justify-content: center;
  border-radius: 7px;
  color: var(--text-faint);
}

.conv-del:hover {
  background: var(--bg-hover);
  color: var(--danger);
}

.conv-del svg {
  width: 14px;
  height: 14px;
}

.conv-item:hover .conv-del {
  display: inline-flex;
}

.sidebar-empty {
  color: var(--text-faint);
  font-size: 13px;
  text-align: center;
  padding: 32px 10px;
}

.sidebar-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-top: 1px solid var(--border-soft);
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.user-avatar {
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand2));
}

.user-name {
  font-size: 13.5px;
  color: var(--text-secondary);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 39;
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-main);
  position: relative;
}

.topbar {
  height: 52px;
  flex: 0 0 52px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 12px;
}

.topbar-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.topbar-actions {
  margin-left: auto;
}

#sidebar-toggle {
  display: none;
}

/* ---------- 消息区 ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.messages-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 10px 18px 30px;
}

.msg {
  display: flex;
  gap: 14px;
  padding: 18px 0;
}

.msg.user {
  justify-content: flex-end;
}

.msg-user-body {
  max-width: min(82%, 600px);
  background: var(--bg-elev);
  border: 1px solid var(--border-soft);
  padding: 11px 15px;
  border-radius: 18px 18px 5px 18px;
  font-size: 15px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.avatar {
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  margin-top: 2px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-elev);
  border: 1px solid var(--border-soft);
}

.avatar .spark {
  width: 18px;
  height: 18px;
  color: var(--brand);
}

.msg-main {
  min-width: 0;
  max-width: 100%;
  flex: 1;
}

.msg.user .msg-main {
  flex: 0 1 auto;
  max-width: 82%;
}

.msg-body {
  font-size: 15px;
  line-height: 1.75;
  overflow-wrap: anywhere;
}

.msg-body > :first-child {
  margin-top: 0;
}

.msg-body > :last-child {
  margin-bottom: 0;
}

.msg-body p {
  margin: 0.65em 0;
}

.msg-body a {
  color: var(--accent);
  text-decoration: none;
}

.msg-body a:hover {
  text-decoration: underline;
}

.msg-body h1,
.msg-body h2,
.msg-body h3,
.msg-body h4 {
  margin: 1em 0 0.5em;
  line-height: 1.35;
  font-weight: 650;
}

.msg-body h1 { font-size: 1.35em; }
.msg-body h2 { font-size: 1.22em; }
.msg-body h3 { font-size: 1.1em; }
.msg-body h4 { font-size: 1em; }

.msg-body ul,
.msg-body ol {
  padding-left: 1.5em;
  margin: 0.5em 0;
}

.msg-body li {
  margin: 0.22em 0;
}

.msg-body blockquote {
  margin: 0.7em 0;
  padding: 0.1em 0 0.1em 0.9em;
  border-left: 3px solid var(--border);
  color: var(--text-secondary);
}

.msg-body hr {
  border: 0;
  border-top: 1px solid var(--border-soft);
  margin: 1.2em 0;
}

.msg-body table {
  border-collapse: collapse;
  margin: 0.7em 0;
  width: 100%;
  font-size: 0.92em;
}

.msg-body th,
.msg-body td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.msg-body th {
  background: var(--bg-elev);
}

.msg-body code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: var(--bg-elev);
  border: 1px solid var(--border-soft);
  border-radius: 5px;
  padding: 0.15em 0.4em;
}

.msg-body pre {
  position: relative;
  margin: 0.8em 0;
  border-radius: 10px;
  overflow: hidden;
  background: var(--code-bg);
  border: 1px solid var(--border-soft);
}

.code-block {
  margin: 0.8em 0;
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  overflow: hidden;
  background: var(--code-bg);
}

.code-block pre {
  margin: 0;
  border: 0;
  border-radius: 0;
}

.msg-body pre code {
  display: block;
  padding: 13px 15px;
  background: transparent;
  border: 0;
  border-radius: 0;
  font-size: 13px;
  line-height: 1.55;
  overflow-x: auto;
  color: #e6edf3;
}

.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.04);
  border-bottom: 1px solid var(--border-soft);
}

.code-lang {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
  text-transform: lowercase;
}

.code-copy {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11.5px;
  padding: 3px 8px;
  border-radius: 6px;
  color: var(--text-faint);
}

.code-copy:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.code-copy svg {
  width: 12px;
  height: 12px;
}

/* 工具事件 */
.tools {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 8px 0;
}

.tool-item {
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  background: var(--bg-elev);
  overflow: hidden;
}

.tool-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 11px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-secondary);
  cursor: pointer;
  user-select: none;
}

.tool-head:hover {
  background: var(--bg-hover);
}

.tool-icon {
  width: 14px;
  height: 14px;
  flex: 0 0 14px;
  color: var(--text-faint);
}

.tool-cmd {
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  direction: ltr;
  text-align: left;
}

.tool-state {
  width: 16px;
  height: 16px;
  flex: 0 0 16px;
  border-radius: 50%;
  border: 2px solid var(--text-faint);
  border-top-color: transparent;
  animation: spin 0.8s linear infinite;
}

.tool-state.ok {
  border: 0;
  background: var(--ok);
  animation: none;
}

.tool-state.err {
  border: 0;
  background: var(--danger);
  animation: none;
}

.tool-state.wait {
  border: 0;
  background: var(--text-faint);
  animation: none;
}

.tool-output {
  margin: 0;
  padding: 8px 12px;
  border-top: 1px solid var(--border-soft);
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
  color: #b8c2cc;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 220px;
  overflow-y: auto;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 正在输入 */
.typing {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 2px;
}

.typing i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-faint);
  animation: bounce 1.2s infinite ease-in-out;
}

.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-5px); opacity: 1; }
}

.stop-notice {
  margin: 0.6em 0;
  padding: 8px 12px;
  border-radius: 9px;
  background: var(--bg-elev);
  border: 1px solid var(--border-soft);
  color: var(--text-secondary);
  font-size: 13px;
}

.type-caret::after {
  content: "▍";
  color: var(--text-faint);
  animation: blink 0.9s step-end infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ---------- 空状态 ---------- */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 24px 40px;
  text-align: center;
}

.empty-state.leave {
  display: none;
}

.empty-logo {
  margin-bottom: 12px;
}

.empty-logo .spark {
  width: 58px;
  height: 58px;
  color: var(--brand);
  filter: drop-shadow(0 0 16px rgba(245, 158, 11, 0.3));
}

.empty-title {
  margin: 0 0 6px;
  font-size: 27px;
  font-weight: 650;
  letter-spacing: 0.5px;
}

.empty-sub {
  margin: 0 0 26px;
  color: var(--text-secondary);
  font-size: 14.5px;
}

.suggestions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  width: min(620px, 100%);
}

.suggestion {
  padding: 13px 16px;
  background: var(--bg-elev);
  border: 1px solid var(--border-soft);
  border-radius: 13px;
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--text-secondary);
  text-align: left;
  transition: border-color 0.15s, background 0.15s, color 0.15s;
}

.suggestion:hover {
  border-color: var(--accent);
  color: var(--text);
}

.empty-tip {
  margin: 24px 0 0;
  color: var(--text-faint);
  font-size: 12px;
}

/* ---------- 输入区 ---------- */
.composer-wrap {
  flex: 0 0 auto;
  padding: 0 16px 10px;
}

.composer {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 9px 9px 7px 16px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.composer:focus-within {
  border-color: rgba(255, 255, 255, 0.28);
}

:root[data-theme="light"] .composer:focus-within {
  border-color: var(--accent);
}

#chat-input {
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  font-size: 15px;
  line-height: 1.55;
  padding: 4px 0;
  max-height: 190px;
  overflow-y: auto;
}

#chat-input::placeholder {
  color: var(--text-faint);
}

.composer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 4px;
}

.composer-tip {
  font-size: 11.5px;
  color: var(--text-faint);
}

.send-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--text);
  color: var(--bg-main);
  transition: opacity 0.15s;
}

.send-btn:hover {
  opacity: 0.85;
}

.send-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.send-btn svg {
  width: 17px;
  height: 17px;
}

.stop-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--bg-main);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
}

.stop-btn:hover {
  background: var(--bg-hover);
}

.stop-btn svg {
  width: 13px;
  height: 13px;
  fill: currentColor;
}

.composer-note {
  max-width: 760px;
  margin: 7px auto 0;
  text-align: center;
  font-size: 11.5px;
  color: var(--text-faint);
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  z-index: 100;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 16px;
  font-size: 13.5px;
  box-shadow: var(--shadow);
  max-width: min(90vw, 420px);
}

/* ---------- 响应式 ---------- */
@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 40;
    transform: translateX(-100%);
    box-shadow: none;
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: 8px 0 28px rgba(0, 0, 0, 0.3);
  }

  #sidebar-toggle {
    display: inline-flex;
  }

  .suggestions {
    grid-template-columns: 1fr;
  }

  .msg {
    gap: 10px;
  }

  .composer-wrap {
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }
}

::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-thumb {
  background: var(--bg-active);
  border-radius: 4px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

/* highlight.js 配色（github-dark 风格） */
.hljs {
  color: #e6edf3;
}
.hljs-doctag,
.hljs-keyword,
.hljs-meta .hljs-keyword,
.hljs-template-tag,
.hljs-template-variable,
.hljs-type,
.hljs-variable.language_ {
  color: #ff7b72;
}
.hljs-title,
.hljs-title.class_,
.hljs-title.class_.inherited__,
.hljs-title.function_ {
  color: #d2a8ff;
}
.hljs-attr,
.hljs-attribute,
.hljs-literal,
.hljs-meta,
.hljs-number,
.hljs-operator,
.hljs-variable,
.hljs-selector-attr,
.hljs-selector-class,
.hljs-selector-id {
  color: #79c0ff;
}
.hljs-regexp,
.hljs-string,
.hljs-meta .hljs-string {
  color: #a5d6ff;
}
.hljs-built_in,
.hljs-symbol {
  color: #ffa657;
}
.hljs-comment,
.hljs-code,
.hljs-formula {
  color: #8b949e;
}
.hljs-name,
.hljs-quote,
.hljs-selector-pseudo,
.hljs-selector-tag {
  color: #7ee787;
}
.hljs-subst {
  color: #c9d1d9;
}
.hljs-section {
  color: #1f6feb;
  font-weight: 700;
}
.hljs-bullet {
  color: #f2cc60;
}
.hljs-emphasis {
  color: #c9d1d9;
  font-style: italic;
}
.hljs-strong {
  color: #c9d1d9;
  font-weight: 700;
}
.hljs-addition {
  color: #aff5b4;
  background: #033a16;
}
.hljs-deletion {
  color: #ffdcd7;
  background: #67060c;
}
.hljs-char.escape_,
.hljs-link,
.hljs-params,
.hljs-property,
.hljs-punctuation,
.hljs-tag {
  color: #c9d1d9;
}

/* ---------- 应用导航 ---------- */
.topbar { position: relative; z-index: 30; }
.topbar-actions { position: relative; display: flex; align-items: center; }
.portal-menu {
  position: absolute; top: 46px; right: 0; z-index: 60;
  width: 280px; max-width: calc(100vw - 28px);
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow); overflow: hidden;
}
.portal-btn.on { background: var(--bg-hover); color: var(--text); }

.portal-head {
  padding: 11px 14px 9px; font-size: 13px; font-weight: 650;
  border-bottom: 1px solid var(--border-soft); color: var(--text-faint);
}
.portal-list { padding: 6px; display: flex; flex-direction: column; gap: 3px; }
.portal-item {
  display: flex; align-items: center; gap: 11px; width: 100%;
  padding: 9px 10px; border-radius: 10px; text-decoration: none;
  color: var(--text); transition: background .15s;
}
.portal-item:hover { background: var(--bg-hover); }
.portal-item.current { background: var(--bg-active); }
.portal-badge {
  width: 34px; height: 34px; flex: 0 0 34px; border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  font-size: 17px; background: var(--bg-input); border: 1px solid var(--border-soft);
}
.portal-info { min-width: 0; }
.portal-name { font-size: 13.5px; font-weight: 600; display: flex; align-items: center; gap: 7px; }
.portal-tag {
  font-size: 10px; color: var(--accent); border: 1px solid var(--accent);
  padding: 1px 6px; border-radius: 20px; white-space: nowrap;
}
.portal-desc { font-size: 11px; color: var(--text-faint); margin-top: 2px; }
