:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --text: #1f2329;
  --muted: #8a8f99;
  --line: #e8e8ec;
  --brand: #10a37f;
  --brand-dark: #0d8a6c;
  --user-bubble: #10a37f;
  --user-text: #ffffff;
  --ai-bubble: #ffffff;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1d;
    --panel: #242528;
    --text: #ececf1;
    --muted: #9a9ba2;
    --line: #34353a;
    --ai-bubble: #2c2d31;
    --shadow: 0 1px 3px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg);
}

/* 顶部栏 */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(var(--safe-top) + 10px) 14px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-title { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 17px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #c4c7cd; }
.dot.ok { background: var(--brand); box-shadow: 0 0 0 3px rgba(16,163,127,.18); }
.dot.err { background: #e5484d; }
.topbar-actions { display: flex; gap: 4px; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border: none; background: transparent;
  color: var(--muted); border-radius: 10px; cursor: pointer; transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--bg); color: var(--text); }

/* 消息区 */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-overflow-scrolling: touch;
}

.welcome { margin: auto; text-align: center; padding: 30px 10px; color: var(--text); }
.welcome-icon { font-size: 44px; }
.welcome h1 { font-size: 22px; margin: 12px 0 6px; }
.welcome p { color: var(--muted); margin: 0 0 22px; }
.suggestions { display: flex; flex-direction: column; gap: 10px; max-width: 360px; margin: 0 auto; }
.chip {
  background: var(--panel); border: 1px solid var(--line); color: var(--text);
  padding: 12px 14px; border-radius: 12px; font-size: 14px; text-align: left;
  cursor: pointer; transition: border-color .15s, transform .05s; box-shadow: var(--shadow);
}
.chip:active { transform: scale(.98); }
.chip:hover { border-color: var(--brand); }

/* 气泡 */
.msg { display: flex; gap: 10px; max-width: 100%; animation: pop .18s ease; }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.msg .avatar {
  width: 30px; height: 30px; border-radius: 8px; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center; font-size: 16px;
}
.msg.user { flex-direction: row-reverse; }
.msg.user .avatar { background: var(--user-bubble); color: #fff; }
.msg.ai .avatar { background: var(--line); }

.bubble {
  padding: 11px 14px; border-radius: 16px; line-height: 1.6; font-size: 15.5px;
  word-break: break-word; white-space: pre-wrap; max-width: calc(100% - 50px);
  box-shadow: var(--shadow);
}
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); border-top-right-radius: 5px; }
.msg.ai .bubble { background: var(--ai-bubble); color: var(--text); border-top-left-radius: 5px; }
.bubble.error { background: #fdecec; color: #c0353a; border: 1px solid #f6c5c7; box-shadow: none; }

.bubble p { margin: 0 0 8px; } .bubble p:last-child { margin: 0; }
.bubble pre {
  background: #0d1117; color: #e6edf3; padding: 12px; border-radius: 10px;
  overflow-x: auto; font-size: 13.5px; margin: 8px 0;
}
.bubble code { font-family: "SF Mono", Consolas, Menlo, monospace; font-size: 13.5px; }
.bubble :not(pre) > code { background: rgba(135,131,120,.18); padding: 1px 5px; border-radius: 5px; }
.bubble a { color: var(--brand); }

/* 打字光标 */
.cursor::after { content: "▍"; animation: blink 1s steps(1) infinite; color: var(--brand); }
@keyframes blink { 50% { opacity: 0; } }

.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing span { width: 7px; height: 7px; background: var(--muted); border-radius: 50%; animation: bounce 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity: .4; } 30% { transform: translateY(-5px); opacity: 1; } }

/* 输入区 */
.composer {
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding: 10px 12px calc(var(--safe-bottom) + 10px);
}
.composer-inner {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 22px; padding: 6px 6px 6px 14px;
}
#input {
  flex: 1; border: none; outline: none; background: transparent; resize: none;
  font: inherit; color: var(--text); max-height: 140px; line-height: 1.5; padding: 6px 0;
}
.send-btn {
  flex: 0 0 auto; width: 38px; height: 38px; border-radius: 50%; border: none;
  background: var(--brand); color: #fff; display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: background .15s, transform .05s;
}
.send-btn:disabled { background: #c4c7cd; cursor: not-allowed; }
.send-btn:active:not(:disabled) { transform: scale(.92); }
.hint { text-align: center; color: var(--muted); font-size: 11.5px; margin-top: 6px; }

/* 弹窗 */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center; z-index: 50;
}
.modal.hidden { display: none; }
.modal-card {
  background: var(--panel); width: 100%; max-width: 480px; border-radius: 18px 18px 0 0;
  max-height: 90dvh; display: flex; flex-direction: column; animation: slideup .25s ease;
  padding-bottom: var(--safe-bottom);
}
@media (min-width: 520px) { .modal { align-items: center; } .modal-card { border-radius: 18px; } }
@keyframes slideup { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 16px 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.modal-body label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.modal-body label.row { flex-direction: row; justify-content: space-between; align-items: center; }
.modal-body label.inline { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.modal-body input[type=text], .modal-body input[type=password], .modal-body textarea {
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
  border-radius: 10px; padding: 11px 12px; font: inherit; font-size: 15px; outline: none; width: 100%;
}
.modal-body input:focus, .modal-body textarea:focus { border-color: var(--brand); }
.modal-body textarea { resize: vertical; }
.modal-body input[type=range] { width: 100%; accent-color: var(--brand); }
.temp-val { color: var(--brand); font-weight: 700; }
.modal-foot { display: flex; gap: 10px; padding: 14px 18px; border-top: 1px solid var(--line); }
.btn { flex: 1; padding: 12px; border-radius: 12px; font-size: 15px; font-weight: 600; cursor: pointer; border: 1px solid var(--line); }
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn.primary:active { background: var(--brand-dark); }
.btn.ghost { background: transparent; color: var(--text); }
