/* ============ 主题变量 ============ */
:root {
  --brand: #4d6bfe;
  --brand-2: #7c5cff;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
[data-theme='dark'] {
  --bg: #0f1115;
  --panel: #14171d;
  --card: #1a1e26;
  --card-2: #20252f;
  --line: #262c38;
  --text: #e9edf5;
  --muted: #8d95a6;
  --user-bubble: linear-gradient(135deg, #3d59e8, #6a49e6);
  --user-text: #fff;
  --code-bg: #0b0d12;
  --shadow: 0 12px 40px rgba(0, 0, 0, .45);
}
[data-theme='light'] {
  --bg: #f5f6f9;
  --panel: #ffffff;
  --card: #ffffff;
  --card-2: #f0f2f7;
  --line: #e2e6ee;
  --text: #131722;
  --muted: #6b7280;
  --user-bubble: linear-gradient(135deg, #4d6bfe, #7c5cff);
  --user-text: #fff;
  --code-bg: #f6f8fb;
  --shadow: 0 10px 30px rgba(19, 23, 34, .12);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
        'Hiragino Sans GB', 'Microsoft YaHei', system-ui, sans-serif;
  overscroll-behavior: none;
}
button, textarea, input, select { font: inherit; color: inherit; }
a { color: var(--brand); }

#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  max-width: 780px;
  margin: 0 auto;
  position: relative;
  background: var(--bg);
}

/* ============ 顶栏 ============ */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-t) + 8px) 10px 8px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.icon-btn {
  border: 0; background: transparent; color: var(--text);
  width: 38px; height: 38px; border-radius: 50%;
  font-size: 18px; display: grid; place-items: center; cursor: pointer;
}
.icon-btn:active { background: var(--card-2); }
.model-pill {
  flex: 1; min-width: 0;
  display: flex; align-items: center; justify-content: center; gap: 7px;
  height: 38px; padding: 0 14px; border-radius: 19px;
  background: var(--card); border: 1px solid var(--line);
  cursor: pointer; overflow: hidden;
}
.model-pill #model-name {
  font-size: 14px; font-weight: 600; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.model-pill .caret { color: var(--muted); font-size: 11px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }

/* ============ 消息区 ============ */
.messages {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}
.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 20px;
}
.empty .big { font-size: 40px; margin-bottom: 8px; }
.empty h3 { color: var(--text); margin: 0 0 6px; font-size: 17px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 8px 13px; border-radius: 18px; font-size: 13px; cursor: pointer;
}
.chip:active { background: var(--card-2); }

.msg { display: flex; flex-direction: column; gap: 5px; }
.msg .who {
  font-size: 11px; color: var(--muted); padding: 0 6px;
  display: flex; align-items: center; gap: 6px;
}
.msg .who .tag {
  background: var(--card-2); border-radius: 6px; padding: 1px 6px; font-size: 10px;
}
.bubble {
  padding: 11px 14px;
  border-radius: var(--radius);
  max-width: 100%;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  font-size: 15px;
}
.msg.user { align-items: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); color: var(--user-text);
  border-bottom-right-radius: 6px;
  white-space: pre-wrap;
  max-width: 88%;
}
.msg.assistant .bubble {
  background: var(--card); border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
  width: 100%;
}
.msg.error .bubble { border-color: #7a2e2e; background: #2a1618; color: #ffb4b4; }
.msg-actions { display: flex; gap: 6px; padding: 0 4px; }
.mini {
  border: 0; background: transparent; color: var(--muted);
  font-size: 12px; padding: 3px 7px; border-radius: 8px; cursor: pointer;
}
.mini:active { background: var(--card-2); color: var(--text); }

/* 思考过程 */
.reasoning {
  border-left: 2px solid var(--line);
  padding: 4px 0 4px 10px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
  white-space: pre-wrap;
}
.reasoning summary { cursor: pointer; font-size: 12px; color: var(--brand); }

/* ============ Markdown ============ */
.bubble p { margin: 0 0 9px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h3, .bubble h4, .bubble h5 { margin: 14px 0 7px; line-height: 1.35; }
.bubble h3 { font-size: 17px; }
.bubble h4 { font-size: 15.5px; }
.bubble h5 { font-size: 14.5px; }
.bubble ul, .bubble ol { margin: 6px 0; padding-left: 20px; }
.bubble li { margin: 3px 0; }
.bubble blockquote {
  margin: 8px 0; padding: 2px 0 2px 10px;
  border-left: 3px solid var(--brand); color: var(--muted);
}
.bubble hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }
.bubble code.ic {
  background: var(--card-2); padding: 1px 5px; border-radius: 5px;
  font-size: .9em; font-family: ui-monospace, 'SF Mono', Consolas, monospace;
}
.code {
  border: 1px solid var(--line); border-radius: 10px; overflow: hidden;
  margin: 9px 0; background: var(--code-bg);
}
.code-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 9px; background: var(--card-2);
  font-size: 11px; color: var(--muted);
}
.code-lang { font-family: ui-monospace, monospace; }
.code-warn { color: #ffb86b; }
.code-copy {
  margin-left: auto; border: 0; background: transparent; color: var(--muted);
  font-size: 11px; cursor: pointer; padding: 3px 6px; border-radius: 6px;
}
.code-copy:active { background: var(--line); color: var(--text); }
.code pre { margin: 0; padding: 10px; overflow-x: auto; }
.code code {
  font-family: ui-monospace, 'SF Mono', 'Cascadia Code', Consolas, monospace;
  font-size: 12.5px; line-height: 1.5; white-space: pre;
}
.tablewrap { overflow-x: auto; margin: 9px 0; }
.tablewrap table { border-collapse: collapse; font-size: 13px; width: 100%; }
.tablewrap th, .tablewrap td { border: 1px solid var(--line); padding: 6px 9px; text-align: left; }
.tablewrap th { background: var(--card-2); }

/* 光标 */
.caret-blink {
  display: inline-block; width: 7px; height: 15px; margin-left: 2px;
  background: var(--brand); vertical-align: -2px; border-radius: 1px;
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ============ 输入区 ============ */
.composer {
  padding: 8px 10px calc(var(--safe-b) + 8px);
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.composer-inner {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--card); border: 1px solid var(--line);
  border-radius: 22px; padding: 6px 6px 6px 14px;
}
#input {
  flex: 1; border: 0; background: transparent; resize: none;
  outline: none; max-height: 132px; padding: 8px 0; line-height: 1.45;
}
#input::placeholder { color: var(--muted); }
.send-btn {
  flex: none; width: 38px; height: 38px; border-radius: 50%;
  border: 0; display: grid; place-items: center; cursor: pointer;
  background: var(--brand); color: #fff;
}
.send-btn[data-mode='stop'] { background: #e5484d; }
.send-btn[data-mode='stop'] svg { display: none; }
.send-btn[data-mode='stop']::after {
  content: ''; width: 12px; height: 12px; background: #fff; border-radius: 2px;
}
.send-btn:disabled { opacity: .45; }
.composer-tip {
  font-size: 11px; color: var(--muted); text-align: center;
  height: 14px; line-height: 14px; margin-top: 3px;
}

/* ============ 侧栏 ============ */
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; width: 274px; max-width: 82vw;
  background: var(--panel); border-right: 1px solid var(--line);
  transform: translateX(-102%); transition: transform .22s ease;
  z-index: 60; display: flex; flex-direction: column;
  padding-top: var(--safe-t);
}
.drawer.open { transform: none; box-shadow: var(--shadow); }
.drawer-head { padding: 14px 14px 10px; }
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.brand-logo { width: 34px; height: 34px; border-radius: 10px; }
.brand-name { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 11px; color: var(--muted); }
.btn-primary {
  border: 0; border-radius: 11px; padding: 10px 14px;
  background: var(--brand); color: #fff; font-weight: 600; cursor: pointer;
}
.btn-primary.wide { width: 100%; }
.btn-ghost {
  border: 1px solid var(--line); background: transparent; color: var(--text);
  border-radius: 10px; padding: 8px 12px; cursor: pointer; font-size: 13px;
}
.chat-list { flex: 1; overflow-y: auto; padding: 4px 8px 10px; }
.chat-item {
  display: flex; align-items: center; gap: 6px;
  padding: 9px 10px; border-radius: 10px; cursor: pointer; margin-bottom: 2px;
}
.chat-item:active { background: var(--card-2); }
.chat-item.active { background: var(--card); border: 1px solid var(--line); }
.chat-item .t { flex: 1; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .m { font-size: 10px; color: var(--muted); }
.chat-item .del { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 2px 4px; }
.drawer-foot {
  border-top: 1px solid var(--line); padding: 10px 14px calc(var(--safe-b) + 10px);
  display: flex; align-items: center; gap: 8px;
}
.stat { margin-left: auto; font-size: 11px; color: var(--muted); }
.scrim {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .5);
  z-index: 50; backdrop-filter: blur(1px);
}

/* ============ 弹层 ============ */
.sheet-wrap { position: fixed; inset: 0; z-index: 80; display: flex; align-items: flex-end; }
.sheet-scrim { position: absolute; inset: 0; background: rgba(0, 0, 0, .5); }
.sheet {
  position: relative; width: 100%; max-width: 780px; margin: 0 auto;
  background: var(--panel); border-radius: 18px 18px 0 0;
  border-top: 1px solid var(--line);
  max-height: 88dvh; display: flex; flex-direction: column;
  padding-bottom: var(--safe-b);
  animation: slideUp .22s ease;
}
@keyframes slideUp { from { transform: translateY(12%); opacity: .6; } }
.sheet-grip {
  width: 38px; height: 4px; border-radius: 2px; background: var(--line);
  margin: 8px auto 0;
}
.sheet-head {
  display: flex; align-items: center; padding: 8px 12px 8px 16px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 { margin: 0; font-size: 15.5px; flex: 1; }
.sheet-body { overflow-y: auto; padding: 12px 16px 22px; }

/* 设置项 */
.group { margin-bottom: 20px; }
.group > h3 {
  font-size: 12px; color: var(--muted); text-transform: uppercase;
  letter-spacing: .06em; margin: 0 0 8px;
}
.row {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 12px; background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; margin-bottom: 8px;
}
.row .label { flex: 1; font-size: 14px; }
.row .label small { display: block; color: var(--muted); font-size: 11.5px; font-weight: 400; }
.input, .select, textarea.input {
  width: 100%; background: var(--card-2); border: 1px solid var(--line);
  border-radius: 9px; padding: 9px 11px; outline: none; font-size: 13.5px;
}
textarea.input { resize: vertical; font-family: ui-monospace, monospace; font-size: 12.5px; }
.switch { position: relative; width: 44px; height: 26px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; }
.switch i {
  position: absolute; inset: 0; border-radius: 13px; background: var(--line);
  transition: background .18s; pointer-events: none;
}
.switch i::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px;
  border-radius: 50%; background: #fff; transition: transform .18s;
}
.switch input:checked + i { background: var(--brand); }
.switch input:checked + i::after { transform: translateX(18px); }
.slider { width: 100%; accent-color: var(--brand); }

.seg {
  display: flex; gap: 4px; background: var(--card-2); border-radius: 10px;
  padding: 3px; margin-bottom: 14px;
}
.seg button {
  flex: 1; border: 0; background: transparent; color: var(--muted);
  padding: 8px; border-radius: 8px; font-size: 13px; cursor: pointer;
}
.seg button.on { background: var(--card); color: var(--text); font-weight: 600; }

/* 服务商条目 */
.prov {
  border: 1px solid var(--line); border-radius: 12px; margin-bottom: 9px;
  background: var(--card); overflow: hidden;
}
.prov-head { display: flex; align-items: center; gap: 10px; padding: 11px 12px; cursor: pointer; }
.prov-badge {
  width: 26px; height: 26px; border-radius: 8px; flex: none;
  display: grid; place-items: center; color: #fff; font-size: 12px; font-weight: 700;
}
.prov-name { flex: 1; font-size: 14px; }
.prov-name small { display: block; color: var(--muted); font-size: 11px; }
.prov-state { font-size: 11px; color: var(--muted); }
.prov-state.ok { color: #34c759; }
.prov-body { padding: 0 12px 12px; display: none; gap: 8px; flex-direction: column; }
.prov.open .prov-body { display: flex; }
.prov-body label { font-size: 11.5px; color: var(--muted); }
.prov-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.mini-btn {
  border: 1px solid var(--line); background: var(--card-2); color: var(--text);
  border-radius: 8px; padding: 7px 11px; font-size: 12px; cursor: pointer;
}
.mini-btn.danger { color: #ff6b6b; }

/* 模型选择 */
.model-search { margin-bottom: 10px; }
.model-group-title {
  font-size: 11px; color: var(--muted); margin: 12px 4px 5px;
  text-transform: uppercase; letter-spacing: .05em;
}
.model-item {
  display: flex; align-items: center; gap: 10px; padding: 11px 12px;
  border-radius: 11px; cursor: pointer; border: 1px solid transparent;
}
.model-item:active { background: var(--card-2); }
.model-item.on { background: var(--card); border-color: var(--brand); }
.model-item .mm { flex: 1; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-item .pp { font-size: 11px; color: var(--muted); }
.model-item.locked { opacity: .55; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 84px);
  transform: translateX(-50%);
  background: #23262e; color: #fff; padding: 9px 15px; border-radius: 10px;
  font-size: 13px; z-index: 100; box-shadow: var(--shadow); max-width: 88vw;
  text-align: center;
}
.toast.err { background: #4a1d20; color: #ffc4c4; }

@media (min-width: 640px) {
  .messages { padding: 18px 18px 26px; }
  .bubble { font-size: 15px; }
}
