:root {
  --bg: #0b1220;
  --panel: #141c2e;
  --panel-2: #1b2540;
  --line: #26314d;
  --text: #e8eefc;
  --muted: #8b9ab8;
  --accent: #3ddc97;
  --accent-ink: #06281a;
  --danger: #ff6b6b;
  --radius: 16px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body { overflow: hidden; }

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 56px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(11, 18, 32, 0.92);
  backdrop-filter: blur(10px);
}

header h1 {
  font-size: 17px;
  font-weight: 600;
  margin: 0;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

header .sub {
  display: block;
  font-size: 11px;
  font-weight: 400;
  color: var(--muted);
  margin-top: 2px;
}

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

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  flex: 0 0 auto;
}

.icon-btn:active { transform: scale(0.94); }

.pill {
  font-size: 11px;
  padding: 4px 9px;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--line);
}

.pill.live {
  color: var(--accent);
  border-color: rgba(61, 220, 151, 0.4);
  background: rgba(61, 220, 151, 0.1);
}

main { flex: 1 1 auto; min-height: 0; position: relative; }

.view { height: 100%; overflow-y: auto; -webkit-overflow-scrolling: touch; }

.view[hidden] { display: none; }

/* ---------- pairing ---------- */
#pair {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 16px;
  padding: 28px 22px 40px;
  text-align: center;
}

#pair .logo {
  width: 76px;
  height: 76px;
  margin: 0 auto 4px;
  border-radius: 22px;
  display: grid;
  place-items: center;
  font-size: 34px;
  background: linear-gradient(160deg, var(--accent), #2ea6ff);
  color: #05210f;
}

#pair h2 { margin: 0; font-size: 20px; }

#pair p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

#pair .field { display: flex; gap: 8px; margin-top: 6px; }

#pair input {
  flex: 1;
  min-width: 0;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font-size: 16px;
  letter-spacing: 2px;
  text-transform: uppercase;
}

#pair input:focus { outline: 2px solid rgba(61, 220, 151, 0.5); outline-offset: 1px; }

.primary {
  padding: 14px 18px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  font-size: 15px;
}

.primary:active { transform: scale(0.98); }

.hint { font-size: 12px; color: var(--muted); }

/* ---------- session list ---------- */
#list { padding: 12px 12px 24px; }

.list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 4px 10px;
}

.list-head span { font-size: 12px; color: var(--muted); }

.new-chat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 13px;
  border-radius: 12px;
  background: rgba(61, 220, 151, 0.12);
  color: var(--accent);
  border: 1px solid rgba(61, 220, 151, 0.35);
  font-size: 13px;
  font-weight: 600;
}

.session {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  width: 100%;
  text-align: left;
  padding: 14px;
  margin-bottom: 9px;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid var(--line);
}

.session:active { background: var(--panel-2); }

.session .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-top: 7px;
  background: #3b4767;
  flex: 0 0 auto;
}

.session .dot.on { background: var(--accent); box-shadow: 0 0 0 4px rgba(61, 220, 151, 0.15); }

.session .body { flex: 1; min-width: 0; }

.session .title {
  font-size: 15px;
  line-height: 1.35;
  margin-bottom: 4px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.session .meta {
  font-size: 11px;
  color: var(--muted);
  display: flex;
  gap: 8px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.empty {
  margin: 40px auto;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.8;
}

/* ---------- chat ---------- */
#chat { display: none; flex-direction: column; height: 100%; }

#chat.active { display: flex; }

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.bubble {
  max-width: 88%;
  padding: 11px 14px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.bubble.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-ink);
  border-bottom-right-radius: 6px;
}

.bubble.assistant {
  align-self: flex-start;
  background: var(--panel);
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
}

.bubble.assistant.pending { color: var(--muted); }

.bubble .tool {
  display: block;
  margin-top: 8px;
  font-size: 12px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.md-img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 8px 0 2px;
  border-radius: 12px;
  background: #0a1526;
}

.bubble a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.file-card {
  align-self: flex-start;
  width: 90%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
}

.file-row {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 9px 11px;
  border-radius: 10px;
  background: var(--panel-2);
  text-decoration: none;
  color: var(--text);
}

.file-row:active { background: #223052; }

.file-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  word-break: break-all;
}

.file-desc {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.4;
}

.tool-line {
  align-self: flex-start;
  font-size: 12px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 2px 6px;
}

.bubble pre {
  background: #0a1526;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.45;
  margin: 8px 0 2px;
}

.bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
}

.bubble :not(pre) > code {
  background: rgba(61, 220, 151, 0.12);
  color: var(--accent);
  padding: 1px 5px;
  border-radius: 6px;
}

.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.35;
  animation: blink 1.2s infinite;
}

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

@keyframes blink {
  0%, 100% { opacity: 0.25; transform: translateY(0); }
  50% { opacity: 1; transform: translateY(-2px); }
}

.composer {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 8px 12px 12px;
  border-top: 1px solid var(--line);
  background: rgba(11, 18, 32, 0.96);
}

.composer textarea {
  flex: 1;
  min-height: 46px;
  max-height: 132px;
  resize: none;
  padding: 13px 15px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  line-height: 1.4;
}

.composer textarea:focus { outline: none; border-color: rgba(61, 220, 151, 0.5); }

.send {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 19px;
  flex: 0 0 auto;
}

.send:disabled { opacity: 0.35; }

.send.stop { background: var(--danger); color: #2a0505; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: rgba(20, 28, 46, 0.98);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 13px;
  max-width: 88vw;
  z-index: 20;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.toast.show { opacity: 1; }

/* ---------- attachments ---------- */
.composer-wrap {
  flex: 0 0 auto;
  border-top: 1px solid var(--line);
  background: rgba(11, 18, 32, 0.96);
}

.composer {
  border-top: 0;
  background: transparent;
}

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  max-width: 100%;
  padding: 6px 9px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  font-size: 12px;
}

.chip img {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  object-fit: cover;
}

.chip-name {
  max-width: 42vw;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.chip button {
  color: var(--muted);
  font-size: 15px;
  line-height: 1;
  padding: 0 2px;
}

.attach {
  font-size: 20px;
  line-height: 1;
  color: var(--accent);
}
.md-img-wrap {
  display: block;
}

.md-img-wrap.md-img-failed .md-img {
  display: none;
}

.md-img-wrap.md-img-failed::after {
  content: "图片暂时显示不了：桌面端重启后即可查看";
  display: block;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 12px;
}

.banner {
  flex: 0 0 auto;
  padding: 8px 14px;
  font-size: 12px;
  line-height: 1.5;
  color: #ffd58a;
  background: rgba(255, 180, 60, 0.12);
  border-bottom: 1px solid rgba(255, 180, 60, 0.28);
}
.stop-btn {
  background: var(--danger);
  color: #2a0505;
  font-size: 14px;
}

.send.steer {
  background: #ffb43c;
  color: #33210a;
}
.stop-btn {
  width: 40px;
  height: 40px;
  align-self: center;
}
.md-h {
  font-weight: 700;
  font-size: 15px;
  margin: 10px 0 4px;
  color: var(--text);
}
.attach {
  align-self: center;
  margin-bottom: 4px;
  cursor: pointer;
  user-select: none;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: 0;
  padding: 0;
  opacity: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
.older-wrap {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  padding: 8px 0 0;
}

.older {
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}

.older:disabled {
  opacity: 0.5;
}