:root {
  --bg: #0e1424;
  --bg-2: #141c30;
  --panel: #1a2338;
  --panel-2: #222d47;
  --line: rgba(255, 255, 255, 0.08);
  --text: #eef1f8;
  --muted: #8d97ad;
  --accent: #ff8a3d;
  --accent-2: #4fd1c5;
  --user: #2c3b5e;
  --danger: #ff6b6b;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

#app { position: fixed; inset: 0; display: flex; overflow: hidden; }

#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background:
    radial-gradient(120% 60% at 100% 0%, rgba(79, 209, 197, 0.08), transparent 60%),
    radial-gradient(100% 50% at 0% 0%, rgba(255, 138, 61, 0.07), transparent 60%),
    var(--bg);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(var(--safe-top) + 6px) 10px 8px;
  border-bottom: 1px solid var(--line);
  background: rgba(14, 20, 36, 0.78);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  z-index: 2;
}

.icon-btn {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  flex: none;
}
.icon-btn:active { background: var(--panel); }

.model-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  max-width: 62vw;
  padding: 7px 12px 7px 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  font-weight: 600;
  font-size: 15px;
}
.model-pill span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-pill svg { width: 18px; height: 18px; color: var(--muted); flex: none; }

#messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 14px 24px;
  scroll-behavior: smooth;
}

.empty {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  padding: 20px;
}
.empty img { width: 86px; height: 86px; border-radius: 22px; box-shadow: 0 12px 40px rgba(79, 209, 197, 0.18); }
.empty h2 { color: var(--text); margin: 18px 0 4px; font-size: 22px; }
.empty p { margin: 0 0 18px; }
.suggestions { display: grid; gap: 10px; width: min(100%, 420px); }
.suggestion { line-height: 1.45;
  text-align: left;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--bg-2);
  color: var(--text);
  font-size: 15px;
}
.suggestion:active { background: var(--panel); }

.msg { display: flex; flex-direction: column; margin: 0 auto 18px; max-width: 760px; animation: rise 0.25s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.msg.user { align-items: flex-end; }
.msg.user .bubble {
  background: var(--user);
  border-radius: var(--radius) var(--radius) 6px var(--radius);
  padding: 10px 14px;
  max-width: 85%;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.assistant .bubble { width: 100%; word-break: break-word; }
.msg .meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; display: flex; gap: 6px; align-items: center; }
.msg .meta .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent-2); }

.msg-images { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; }
.msg-images img { width: 96px; height: 96px; object-fit: cover; border-radius: 12px; border: 1px solid var(--line); }

.gen-images { display: grid; gap: 8px; margin: 2px 0 10px; }
.gen-images figure { margin: 0; }
.gen-images img { width: 100%; max-width: 520px; border-radius: 16px; display: block; border: 1px solid var(--line); background: var(--bg-2); }

.thinking {
  margin: 0 0 8px;
  border-left: 2px solid var(--panel-2);
  padding: 2px 0 2px 12px;
  color: var(--muted);
  font-size: 14px;
}
.thinking summary { cursor: pointer; list-style: none; font-size: 13px; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking-body { white-space: pre-wrap; margin-top: 6px; max-height: 40vh; overflow: auto; }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 10px; }
.md h1, .md h2, .md h3 { margin: 16px 0 8px; line-height: 1.35; }
.md h1 { font-size: 1.3em; } .md h2 { font-size: 1.18em; } .md h3 { font-size: 1.06em; }
.md ul, .md ol { padding-left: 1.4em; margin: 0 0 10px; }
.md li { margin: 3px 0; }
.md a { color: var(--accent-2); }
.md blockquote { margin: 0 0 10px; padding: 2px 12px; border-left: 3px solid var(--panel-2); color: var(--muted); }
.md code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 0.88em; background: var(--panel); padding: 2px 6px; border-radius: 6px; }
.md pre { position: relative; background: #0a0f1c; border: 1px solid var(--line); border-radius: 12px; padding: 12px; overflow-x: auto; margin: 0 0 10px; }
.md pre code { background: none; padding: 0; font-size: 13px; line-height: 1.55; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; margin: 0 0 10px; font-size: 14px; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; }
.md th { background: var(--panel); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }
.md img { max-width: 100%; border-radius: 12px; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }
.code-copy {
  position: absolute; top: 6px; right: 6px;
  font-size: 12px; padding: 3px 8px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel); color: var(--muted);
}

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

.pending { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; padding: 6px 0; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--panel-2); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.img-skeleton {
  width: min(100%, 360px); aspect-ratio: 1; border-radius: 16px; margin-top: 8px;
  background: linear-gradient(110deg, var(--bg-2) 30%, var(--panel) 50%, var(--bg-2) 70%);
  background-size: 200% 100%; animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }

.error-box { background: rgba(255, 107, 107, 0.1); border: 1px solid rgba(255, 107, 107, 0.35); color: #ffb3b3; border-radius: 12px; padding: 10px 12px; font-size: 14px; }

.md a.cite {
  display: inline-block; min-width: 18px; height: 18px; line-height: 18px; padding: 0 4px; margin: 0 1px;
  font-size: 11px; text-align: center; vertical-align: 2px; text-decoration: none;
  color: var(--accent-2); background: rgba(79, 209, 197, 0.14); border-radius: 6px;
}
.chip-toggle {
  flex: none; border: 1px solid var(--line); background: var(--panel); color: var(--muted);
  border-radius: 10px; padding: 5px 10px; font-size: 13px;
}
.chip-toggle.on { color: var(--accent-2); border-color: rgba(79, 209, 197, 0.45); background: rgba(79, 209, 197, 0.1); }
.search-steps { display: grid; gap: 4px; margin: 0 0 8px; }
.search-step { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.search-step b { color: var(--text); font-weight: 500; }
.search-step.running b { color: var(--accent-2); }
.search-count { margin-left: auto; font-size: 12px; }
.spinner.small { width: 13px; height: 13px; border-width: 2px; }
.sources { margin-top: 10px; border: 1px solid var(--line); border-radius: 14px; background: var(--bg-2); }
.sources summary { cursor: pointer; padding: 8px 12px; font-size: 13px; color: var(--muted); list-style: none; display: flex; align-items: center; gap: 8px; }
.sources-label { flex: none; color: var(--text); }
.sources-hosts { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.sources-toggle { flex: none; color: var(--accent-2); font-size: 12px; }
.sources-toggle::after { content: '展开 ▾'; }
.sources[open] .sources-toggle::after { content: '收起 ▴'; }
.sources summary::-webkit-details-marker { display: none; }
.sources[open] summary { border-bottom: 1px solid var(--line); }
.source-list { display: grid; padding: 4px; max-height: 50vh; overflow-y: auto; }
.source { display: flex; gap: 10px; align-items: flex-start; padding: 8px; border-radius: 10px; text-decoration: none; color: var(--text); }
.source:active { background: var(--panel); }
.source-num { flex: none; width: 20px; height: 20px; border-radius: 6px; background: var(--panel-2); color: var(--accent-2); font-size: 11px; display: grid; place-items: center; margin-top: 2px; }
.source-main { min-width: 0; display: grid; }
.source-title { font-size: 14px; line-height: 1.4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.source-host { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.actions { display: flex; gap: 2px; margin-top: 6px; opacity: 0.85; }
.actions button { border: 0; background: transparent; color: var(--muted); font-size: 13px; padding: 4px 8px; border-radius: 8px; }
.actions button:active { background: var(--panel); }

.composer-wrap {
  padding: 6px 10px calc(var(--safe-bottom) + 8px);
  background: rgba(14, 20, 36, 0.86);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-top: 1px solid var(--line);
}
.modes { display: flex; gap: 6px; margin: 2px auto 8px; max-width: 760px; overflow-x: auto; scrollbar-width: none; }
.modes::-webkit-scrollbar { display: none; }
.mode {
  flex: none; display: inline-flex; align-items: center; gap: 4px;
  border: 1px solid var(--line); background: var(--bg-2); color: var(--muted);
  padding: 6px 14px; border-radius: 999px; font-size: 14px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.mode.active { background: rgba(255, 138, 61, 0.14); border-color: rgba(255, 138, 61, 0.55); color: #ffc49b; }

.mode-options { display: flex; gap: 6px; max-width: 760px; margin: 0 auto; overflow-x: auto; scrollbar-width: none; }
.mode-options:not(:empty) { margin-bottom: 8px; }
.mode-options::-webkit-scrollbar { display: none; }
.chip-select {
  flex: none; appearance: none; -webkit-appearance: none;
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
  border-radius: 10px; padding: 5px 10px; font-size: 13px;
}
.chip-label { flex: none; font-size: 12px; color: var(--muted); align-self: center; }

.attachments { display: flex; gap: 8px; max-width: 760px; margin: 0 auto; overflow-x: auto; }
.attachments:not(:empty) { margin-bottom: 8px; }
.thumb { position: relative; flex: none; }
.thumb img { width: 62px; height: 62px; object-fit: cover; border-radius: 12px; border: 1px solid var(--line); display: block; }
.thumb button {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px;
  border-radius: 50%; border: 0; background: #000c; color: #fff; font-size: 13px; line-height: 22px; padding: 0;
}

.composer {
  display: flex; align-items: flex-end; gap: 6px;
  max-width: 760px; margin: 0 auto;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 24px; padding: 4px;
}
.composer:focus-within { border-color: rgba(79, 209, 197, 0.45); }
.composer .attach { color: var(--muted); width: 40px; height: 40px; }
#input {
  flex: 1; min-height: 40px; max-height: 38vh; resize: none;
  border: 0; outline: 0; background: transparent; color: var(--text);
  font: inherit; font-size: 16px; line-height: 1.5; padding: 8px 2px;
}
#input::placeholder { color: #66708a; }
.send {
  width: 40px; height: 40px; flex: none; border: 0; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent); color: #1b0f05;
  transition: transform 0.1s, opacity 0.15s;
}
.send:disabled { opacity: 0.35; }
.send:active { transform: scale(0.92); }
.send .i-stop { display: none; fill: currentColor; stroke: none; }
.busy .send { background: var(--text); }
.busy .send .i-send { display: none; }
.busy .send .i-stop { display: block; }

#drawer {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 20;
  width: min(84vw, 320px);
  display: flex; flex-direction: column;
  background: var(--bg-2); border-right: 1px solid var(--line);
  padding: calc(var(--safe-top) + 14px) 12px calc(var(--safe-bottom) + 10px);
  transform: translateX(-102%); transition: transform 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.drawer-open #drawer { transform: none; }
#scrim { position: fixed; inset: 0; z-index: 19; background: #0009; opacity: 0; pointer-events: none; transition: opacity 0.28s; }
.drawer-open #scrim { opacity: 1; pointer-events: auto; }
.drawer-head { display: flex; gap: 10px; align-items: center; padding: 0 4px 14px; }
.drawer-logo { width: 42px; height: 42px; border-radius: 11px; }
.drawer-title { font-weight: 700; }
.drawer-sub { font-size: 12px; color: var(--muted); }
.new-chat { border: 1px dashed rgba(255, 255, 255, 0.18); background: transparent; border-radius: 12px; padding: 10px; margin-bottom: 10px; }
.new-chat:active { background: var(--panel); }
.conv-list { flex: 1; overflow-y: auto; margin: 0 -4px; }
.conv { display: flex; align-items: center; gap: 8px; padding: 10px; border-radius: 12px; margin: 2px 4px; }
.conv.active { background: var(--panel); }
.conv-main { flex: 1; min-width: 0; }
.conv-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15px; }
.conv-sub { font-size: 12px; color: var(--muted); }
.conv-del { border: 0; background: transparent; color: var(--muted); padding: 4px 6px; font-size: 16px; }
.conv-empty { color: var(--muted); font-size: 14px; text-align: center; padding: 24px 0; }
.drawer-foot { border-top: 1px solid var(--line); padding-top: 8px; }
.link-btn { border: 0; background: transparent; color: var(--muted); padding: 8px; }

dialog { color: var(--text); }
dialog::backdrop { background: #000a; backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }

.sheet {
  margin: auto auto 0; width: 100%; max-width: 560px; max-height: 82vh;
  border: 0; border-radius: 22px 22px 0 0; padding: 0;
  background: var(--bg-2);
  padding-bottom: var(--safe-bottom);
}
.sheet[open] { animation: up 0.28s cubic-bezier(0.2, 0.8, 0.2, 1); display: flex; flex-direction: column; }
@keyframes up { from { transform: translateY(100%); } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 8px; font-weight: 700; }
.sheet-head button { border: 0; background: var(--panel); border-radius: 999px; padding: 4px 12px; font-size: 14px; }
.sheet-body { overflow-y: auto; padding: 0 12px 16px; }
.sheet-group { font-size: 12px; color: var(--muted); padding: 12px 6px 4px; }
.opt {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
  border: 0; background: transparent; padding: 11px 10px; border-radius: 12px;
}
.opt:active { background: var(--panel); }
.opt.active { background: var(--panel); }
.opt-name { flex: 1; }
.opt-id { display: block; font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: rgba(79, 209, 197, 0.14); color: var(--accent-2); }
.tag.eye { background: rgba(255, 138, 61, 0.14); color: #ffc49b; }
.check { color: var(--accent); font-weight: 700; }
.field { display: grid; gap: 6px; padding: 10px 6px; }
.field label { font-size: 13px; color: var(--muted); }
.field input, .field textarea {
  width: 100%; font: inherit; font-size: 16px; color: var(--text);
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; outline: 0;
}
.field textarea { min-height: 90px; resize: vertical; }
.row-btns { display: flex; gap: 8px; padding: 10px 6px; flex-wrap: wrap; }
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 12px; padding: 9px 14px; }
.btn.danger { color: var(--danger); }
.primary { border: 0; background: var(--accent); color: #1b0f05; font-weight: 700; border-radius: 14px; padding: 12px; width: 100%; }

.lock {
  border: 0; padding: 0; background: transparent; width: 100%; max-width: 360px;
}
.lock form {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 24px; padding: 28px 22px 22px;
}
.lock-logo { width: 76px; height: 76px; border-radius: 20px; }
.lock h1 { margin: 6px 0 0; font-size: 22px; }
.lock p { margin: 0 0 6px; color: var(--muted); font-size: 14px; }
.lock input {
  width: 100%; font: inherit; font-size: 16px; color: var(--text); text-align: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px; outline: 0;
}
.lock-error { color: var(--danger); font-size: 13px; min-height: 18px; }

#viewer {
  position: fixed; inset: 0; z-index: 50; background: #000;
  display: flex; flex-direction: column; justify-content: center;
}
#viewer[hidden] { display: none; }
#viewer img { max-width: 100%; max-height: calc(100% - 90px); object-fit: contain; margin: auto; touch-action: pinch-zoom; }
.viewer-bar { display: flex; gap: 10px; justify-content: center; padding: 10px 10px calc(var(--safe-bottom) + 14px); }
.viewer-bar button { border: 1px solid #fff3; background: #ffffff14; color: #fff; border-radius: 999px; padding: 9px 18px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 120px); z-index: 60;
  transform: translate(-50%, 10px); opacity: 0;
  background: #000d; color: #fff; padding: 8px 16px; border-radius: 999px; font-size: 14px;
  transition: opacity 0.2s, transform 0.2s; pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 900px) {
  #drawer { position: static; transform: none; }
  #scrim, #menuBtn { display: none; }
  #messages { padding: 24px 24px 32px; }
}