/* ==========================================================================
   AI 聊天助手 - 样式表
   ========================================================================== */

/* ---------- 主题变量 ---------- */
:root{
  --font: system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;
  --mono: ui-monospace,SFMono-Regular,Menlo,Consolas,"Courier New",monospace;

  --primary:#4D6BFE;
  --primary-soft:#EEF2FF;
  --on-primary:#fff;

  --bg:#FFFFFF;
  --bg-alt:#F7F8FA;
  --surface:#FFFFFF;
  --surface-2:#F3F4F6;
  --text:#111827;
  --text-sub:#6B7280;
  --border:#E5E7EB;
  --bubble-user:#EFF2FE;
  --bubble-ai:#FFFFFF;
  --code-bg:#F6F8FA;
  --code-head:#EEF1F5;
  --danger:#EF4444;
  --success:#10B981;
  --shadow:0 8px 30px rgba(17,24,39,.12);

  --safe-top: env(safe-area-inset-top,0px);
  --safe-bottom: env(safe-area-inset-bottom,0px);
  --header-h: 56px;
}

[data-theme="dark"]{
  --primary:#5A78FF;
  --primary-soft:#1E293B;
  --bg:#0F1420;
  --bg-alt:#0B0F18;
  --surface:#161C2A;
  --surface-2:#1F2739;
  --text:#E9EDF5;
  --text-sub:#98A2B3;
  --border:#273145;
  --bubble-user:#233052;
  --bubble-ai:transparent;
  --code-bg:#0C111B;
  --code-head:#151C2B;
  --danger:#F87171;
  --success:#34D399;
  --shadow:0 8px 30px rgba(0,0,0,.5);
}

/* ---------- 基础 ---------- */
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
html,body{height:100%}
body{
  margin:0;
  font-family:var(--font);
  font-size:15px;
  line-height:1.7;
  color:var(--text);
  background:var(--bg);
  overflow:hidden;
  -webkit-font-smoothing:antialiased;
  overscroll-behavior:none;
}
button{font-family:inherit;font-size:inherit;color:inherit;cursor:pointer}
textarea{font-family:inherit}
svg{display:block;flex-shrink:0}
img{max-width:100%}
::-webkit-scrollbar{width:5px;height:5px}
::-webkit-scrollbar-thumb{background:var(--border);border-radius:4px}

/* ---------- 顶部栏 ---------- */
.topbar{
  height:calc(var(--header-h) + var(--safe-top));
  padding-top:var(--safe-top);
  display:flex;align-items:center;gap:8px;
  padding-left:8px;padding-right:8px;
  background:var(--bg);
  border-bottom:1px solid var(--border);
  flex-shrink:0;z-index:20;
}
.icon-btn{
  width:38px;height:38px;border:none;background:transparent;border-radius:12px;
  display:flex;align-items:center;justify-content:center;color:var(--text);
  transition:background .15s;
}
.icon-btn:active{background:var(--surface-2)}
.icon-btn[disabled]{opacity:.35}
.topbar-main{flex:1;min-width:0;display:flex;flex-direction:column;align-items:center;gap:1px}
.topbar-title{
  font-size:15px;font-weight:600;max-width:100%;
  white-space:nowrap;overflow:hidden;text-overflow:ellipsis;
}
.model-chip{
  display:inline-flex;align-items:center;gap:4px;max-width:100%;
  padding:1px 8px;border:none;border-radius:20px;
  background:var(--surface-2);color:var(--text-sub);
  font-size:11px;font-weight:500;line-height:18px;
}
.model-chip span{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.model-chip #provider-name{max-width:86px}
.model-chip #model-name{max-width:132px}
.chip-sep{opacity:.6}
.model-chip:active{background:var(--primary-soft);color:var(--primary)}

/* ---------- 聊天区 ---------- */
.chat{
  flex:1;min-height:0;overflow-y:auto;-webkit-overflow-scrolling:touch;
  background:var(--bg);
}
.msg-list{
  display:flex;flex-direction:column;gap:22px;
  padding:18px 14px calc(18px + 140px + var(--safe-bottom));
  max-width:820px;margin:0 auto;width:100%;
}

/* 欢迎页 */
.welcome{padding:34px 10px 0;text-align:center}
.welcome.hidden{display:none}
.welcome-logo{
  width:70px;height:70px;margin:0 auto 16px;border-radius:20px;
  background:linear-gradient(135deg,var(--primary),#8AA0FF);
  display:flex;align-items:center;justify-content:center;color:#fff;
  font-size:30px;font-weight:800;box-shadow:0 10px 26px rgba(77,107,254,.35);
}
.welcome h1{font-size:21px;margin:0 0 6px;font-weight:700}
.welcome p{margin:0 0 26px;color:var(--text-sub);font-size:13px}
.suggestions{display:grid;gap:10px;max-width:520px;margin:0 auto}
.suggestion{
  text-align:left;padding:13px 15px;border-radius:14px;
  background:var(--surface-2);border:1px solid transparent;
  font-size:14px;color:var(--text);line-height:1.5;
  transition:border-color .15s,transform .1s;
}
.suggestion:active{transform:scale(.98);border-color:var(--primary)}
.welcome-tip{margin-top:26px;font-size:12px;color:var(--text-sub);opacity:.8}

/* 消息 */
.msg{display:flex;gap:10px;align-items:flex-start;animation:fadeUp .25s ease}
@keyframes fadeUp{from{opacity:0;transform:translateY(6px)}to{opacity:1;transform:none}}
.msg.user{flex-direction:row-reverse}
.avatar{
  width:32px;height:32px;border-radius:10px;flex-shrink:0;
  display:flex;align-items:center;justify-content:center;
  font-size:12px;font-weight:700;color:#fff;background:var(--primary);
  overflow:hidden;
}
.msg.user .avatar{background:var(--surface-2);color:var(--text-sub)}
.msg-body{min-width:0;max-width:calc(100% - 42px);display:flex;flex-direction:column}
.msg.user .msg-body{align-items:flex-end}
.bubble{
  padding:11px 15px;border-radius:16px;word-break:break-word;overflow-wrap:anywhere;
  user-select:text;
}
.msg.user .bubble{
  background:var(--bubble-user);border-top-right-radius:5px;
  white-space:pre-wrap;
}
.msg.ai .bubble{
  background:var(--bubble-ai);padding:0;border-radius:0;width:100%;
}
.msg.ai .bubble.error{background:rgba(239,68,68,.08);border:1px solid rgba(239,68,68,.3);color:var(--danger);padding:10px 14px;border-radius:12px;font-size:14px}
.msg-time{font-size:11px;color:var(--text-sub);margin-top:5px;opacity:.75}

/* 消息操作条 */
.msg-actions{display:flex;gap:2px;margin-top:6px;opacity:.65}
.msg-actions button{
  border:none;background:transparent;padding:5px 7px;border-radius:8px;
  color:var(--text-sub);display:flex;align-items:center;gap:4px;font-size:11px;
}
.msg-actions button:active{background:var(--surface-2);color:var(--primary)}

/* 思考过程 */
.think{border-left:2px solid var(--border);padding:2px 0 2px 10px;margin-bottom:12px}
.think-head{
  display:inline-flex;align-items:center;gap:5px;font-size:12px;color:var(--text-sub);
  font-weight:500;user-select:none;background:none;border:none;padding:2px 0;
}
.think-head svg{transition:transform .2s}
.think.open .think-head svg{transform:rotate(90deg)}
.think-body{
  display:none;font-size:12.5px;line-height:1.85;color:var(--text-sub);
  white-space:pre-wrap;margin-top:6px;max-height:260px;overflow-y:auto;
}
.think.open .think-body{display:block}

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

/* 加载三个点 */
.dots{display:inline-flex;gap:4px;padding:6px 0}
.dots i{width:7px;height:7px;border-radius:50%;background:var(--text-sub);opacity:.4;animation:bounce 1.2s infinite}
.dots i:nth-child(2){animation-delay:.15s}
.dots i:nth-child(3){animation-delay:.3s}
@keyframes bounce{0%,60%,100%{transform:translateY(0);opacity:.35}30%{transform:translateY(-5px);opacity:.9}}

/* ---------- Markdown 正文 ---------- */
.md{line-height:1.85;word-break:break-word}
.md>*:first-child{margin-top:0}
.md>*:last-child{margin-bottom:0}
.md p{margin:0 0 12px}
.md h1,.md h2,.md h3,.md h4,.md h5,.md h6{
  margin:20px 0 10px;line-height:1.4;font-weight:700;
}
.md h1{font-size:20px}.md h2{font-size:18px}.md h3{font-size:16px}
.md h4,.md h5,.md h6{font-size:15px}
.md h2{padding-bottom:6px;border-bottom:1px solid var(--border)}
.md ul,.md ol{margin:0 0 12px;padding-left:22px}
.md li{margin:4px 0}
.md li>ul,.md li>ol{margin:4px 0 0}
.md a{color:var(--primary);text-decoration:none;border-bottom:1px solid rgba(77,107,254,.35)}
.md blockquote{
  margin:0 0 12px;padding:2px 0 2px 12px;border-left:3px solid var(--border);
  color:var(--text-sub);
}
.md hr{border:none;border-top:1px solid var(--border);margin:18px 0}
.md strong{font-weight:700}
.md table{
  width:100%;border-collapse:collapse;margin:0 0 12px;font-size:13.5px;
  display:block;overflow-x:auto;
}
.md th,.md td{border:1px solid var(--border);padding:7px 10px;text-align:left}
.md th{background:var(--surface-2);font-weight:600;white-space:nowrap}
.md img{border-radius:10px;margin:6px 0}

.inline-code{
  font-family:var(--mono);font-size:.88em;padding:1.5px 5px;border-radius:5px;
  background:var(--surface-2);color:#D6336C;
}
[data-theme="dark"] .inline-code{color:#FF8FB1}

.code-block{
  margin:0 0 14px;border:1px solid var(--border);border-radius:12px;
  overflow:hidden;background:var(--code-bg);
}
.code-head{
  display:flex;align-items:center;justify-content:space-between;
  padding:5px 8px 5px 12px;background:var(--code-head);
  font-size:11px;color:var(--text-sub);
}
.code-head .code-lang{font-family:var(--mono);text-transform:lowercase}
.code-head button{
  border:none;background:transparent;display:flex;align-items:center;gap:4px;
  padding:4px 7px;border-radius:7px;color:var(--text-sub);font-size:11px;
}
.code-head button:active{background:var(--surface-2);color:var(--primary)}
.code-block pre{
  margin:0;padding:12px 14px;overflow-x:auto;font-family:var(--mono);
  font-size:12.8px;line-height:1.7;
}
.code-block code{font-family:inherit;white-space:pre}

/* ---------- 输入区 ---------- */
.composer{
  position:fixed;left:0;right:0;bottom:0;z-index:30;
  padding:8px 12px calc(8px + var(--safe-bottom));
  background:linear-gradient(to bottom,transparent,var(--bg) 22%);
}
.composer-inner{max-width:820px;margin:0 auto}
.composer-box{
  display:flex;align-items:flex-end;gap:6px;
  background:var(--surface-2);border:1px solid var(--border);
  border-radius:22px;padding:6px;
  transition:border-color .15s,background .15s;
}
.attach-btn{
  width:38px;height:38px;flex-shrink:0;border:none;border-radius:50%;
  background:transparent;color:var(--text-sub);
  display:flex;align-items:center;justify-content:center;
}
.attach-btn:active{background:var(--surface);color:var(--primary)}
.composer-box:focus-within{border-color:var(--primary);background:var(--bg)}
#input{
  flex:1;min-height:26px;max-height:150px;resize:none;border:none;outline:none;
  background:transparent;color:var(--text);font-size:15px;line-height:1.6;
  padding:8px 0;
}
#input::placeholder{color:var(--text-sub);opacity:.8}
.send-btn{
  width:38px;height:38px;border:none;border-radius:50%;flex-shrink:0;
  background:var(--primary);color:var(--on-primary);
  display:flex;align-items:center;justify-content:center;
  transition:opacity .15s,transform .1s;
}
.send-btn:active{transform:scale(.92)}
.send-btn[disabled]{opacity:.35}
.send-btn.stop{background:var(--surface);border:1.5px solid var(--border);color:var(--text)}
.composer-tip{
  text-align:center;font-size:11px;color:var(--text-sub);margin-top:6px;opacity:.75;
}

/* ---------- 侧边栏 ---------- */
.overlay{
  position:fixed;inset:0;background:rgba(0,0,0,.45);z-index:80;
  opacity:0;pointer-events:none;transition:opacity .25s;
}
.overlay.show{opacity:1;pointer-events:auto}

.sidebar{
  position:fixed;top:0;bottom:0;left:0;width:282px;max-width:84vw;z-index:90;
  background:var(--surface);border-right:1px solid var(--border);
  display:flex;flex-direction:column;
  transform:translateX(-102%);transition:transform .28s cubic-bezier(.2,.9,.3,1);
  padding-top:var(--safe-top);padding-bottom:var(--safe-bottom);
}
.sidebar.show{transform:none}
.sidebar-head{display:flex;align-items:center;gap:8px;padding:12px 12px 8px}
.sidebar-head .brand{flex:1;font-size:15px;font-weight:700;display:flex;align-items:center;gap:7px}
.brand-dot{width:22px;height:22px;border-radius:7px;background:linear-gradient(135deg,var(--primary),#8AA0FF)}
.new-chat{
  margin:4px 12px 12px;padding:11px;border-radius:13px;border:none;
  background:var(--primary-soft);color:var(--primary);font-weight:600;font-size:14px;
  display:flex;align-items:center;justify-content:center;gap:7px;
}
.new-chat:active{transform:scale(.98)}
.sidebar-search{
  margin:0 12px 10px;display:flex;align-items:center;gap:7px;
  background:var(--surface-2);border-radius:11px;padding:8px 11px;
}
.sidebar-search input{
  flex:1;border:none;outline:none;background:transparent;color:var(--text);font-size:13.5px;
}
.conv-list{flex:1;overflow-y:auto;padding:0 8px 8px}
.conv-group{font-size:11px;color:var(--text-sub);padding:10px 8px 6px;font-weight:600}
.conv-item{
  display:flex;align-items:center;gap:8px;padding:10px 10px;border-radius:11px;
  margin-bottom:2px;border:1px solid transparent;
}
.conv-item.active{background:var(--primary-soft);border-color:rgba(77,107,254,.25)}
.conv-item.active .conv-title{color:var(--primary);font-weight:600}
.conv-title{
  flex:1;min-width:0;font-size:13.5px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;
}
.conv-more{
  border:none;background:transparent;color:var(--text-sub);padding:4px;border-radius:7px;
  opacity:.6;
}
.conv-more:active{background:var(--surface-2);opacity:1}
.conv-empty{padding:26px 14px;text-align:center;color:var(--text-sub);font-size:13px}
.sidebar-foot{padding:10px 12px;border-top:1px solid var(--border);display:flex;flex-direction:column;gap:8px}
.side-row{
  display:flex;align-items:center;gap:9px;padding:9px 10px;border-radius:11px;
  border:none;background:transparent;width:100%;font-size:13.5px;color:var(--text);text-align:left;
}
.side-row:active{background:var(--surface-2)}
.side-row span{flex:1}

/* ---------- 底部弹层 ---------- */
.sheet{
  position:fixed;inset:0;z-index:100;display:none;
}
.sheet.show{display:block}
.sheet-mask{position:absolute;inset:0;background:rgba(0,0,0,.45);opacity:0;transition:opacity .25s}
.sheet.show .sheet-mask{opacity:1}
.sheet-panel{
  position:absolute;left:0;right:0;bottom:0;max-height:88vh;
  background:var(--surface);border-radius:20px 20px 0 0;
  display:flex;flex-direction:column;padding-bottom:var(--safe-bottom);
  transform:translateY(100%);transition:transform .28s cubic-bezier(.2,.9,.3,1);
  box-shadow:var(--shadow);
}
.sheet.show .sheet-panel{transform:none}
.sheet-head{
  display:flex;align-items:center;padding:14px 16px 10px;flex-shrink:0;
}
.sheet-head h3{flex:1;margin:0;font-size:16px;font-weight:700}
.sheet-body{overflow-y:auto;padding:0 16px 20px}

.field{margin-bottom:16px}
.field label{display:block;font-size:12.5px;font-weight:600;color:var(--text-sub);margin-bottom:6px}
.field input[type=text],.field input[type=password],.field input[type=number],.field textarea,.field select{
  width:100%;padding:11px 13px;border-radius:12px;font-size:14px;
  border:1px solid var(--border);background:var(--surface-2);color:var(--text);
  outline:none;font-family:inherit;
}
.field input:focus,.field textarea:focus,.field select:focus{border-color:var(--primary)}
.field textarea{min-height:74px;resize:vertical;line-height:1.6}
.field .hint{font-size:11.5px;color:var(--text-sub);margin-top:5px;line-height:1.5}
.field-row{display:flex;gap:10px}
.field-row .field{flex:1}
.range-row{display:flex;align-items:center;gap:10px}
.range-row input[type=range]{flex:1;accent-color:var(--primary)}
.range-val{font-size:13px;font-weight:600;min-width:34px;text-align:right;color:var(--primary);font-family:var(--mono)}

.switch-row{display:flex;align-items:center;gap:10px;margin-bottom:14px}
.switch-row .sr-text{flex:1}
.switch-row .sr-text b{display:block;font-size:13.5px;font-weight:600}
.switch-row .sr-text small{color:var(--text-sub);font-size:11.5px}
.switch{position:relative;width:46px;height:27px;flex-shrink:0}
.switch input{opacity:0;width:0;height:0;position:absolute}
.switch .track{
  position:absolute;inset:0;background:var(--surface-2);border:1px solid var(--border);
  border-radius:20px;transition:background .2s;
}
.switch .track::after{
  content:'';position:absolute;top:2.5px;left:3px;width:20px;height:20px;border-radius:50%;
  background:#fff;box-shadow:0 1px 4px rgba(0,0,0,.25);transition:transform .2s;
}
.switch input:checked+.track{background:var(--primary);border-color:var(--primary)}
.switch input:checked+.track::after{transform:translateX(19px)}

.btn{
  padding:12px 16px;border-radius:13px;border:1px solid var(--border);
  background:var(--surface-2);font-size:14px;font-weight:600;
  display:inline-flex;align-items:center;justify-content:center;gap:7px;
}
.btn:active{transform:scale(.98)}
.btn-primary{background:var(--primary);border-color:var(--primary);color:var(--on-primary)}
.btn-danger{color:var(--danger);border-color:rgba(239,68,68,.4);background:transparent}
.btn-block{width:100%}
.btn-row{display:flex;gap:10px;margin-top:6px}

.model-item{
  display:flex;align-items:center;gap:10px;padding:12px;border-radius:12px;
  border:1px solid var(--border);margin-bottom:8px;background:var(--surface-2);
}
.model-item.active{border-color:var(--primary);background:var(--primary-soft)}
.model-item .mi-name{flex:1;min-width:0;font-size:13.5px;font-weight:600;word-break:break-all}
.model-item .mi-meta{font-size:11px;color:var(--text-sub);font-weight:400}
.model-item .mi-check{color:var(--primary);display:none}
.model-item.active .mi-check{display:block}
.list-state{padding:24px 0;text-align:center;color:var(--text-sub);font-size:13px}

/* ---------- Toast ---------- */
.toast{
  position:fixed;left:50%;bottom:calc(110px + var(--safe-bottom));
  transform:translate(-50%,14px);z-index:200;
  background:rgba(17,24,39,.92);color:#fff;font-size:13px;
  padding:9px 16px;border-radius:20px;max-width:80vw;
  opacity:0;pointer-events:none;transition:opacity .22s,transform .22s;
  backdrop-filter:blur(6px);
}
.toast.show{opacity:1;transform:translate(-50%,0)}

/* ---------- 确认弹窗 ---------- */
.dialog{
  position:fixed;inset:0;z-index:150;display:none;align-items:center;justify-content:center;
  padding:26px;
}
.dialog.show{display:flex}
.dialog-mask{position:absolute;inset:0;background:rgba(0,0,0,.45)}
.dialog-panel{
  position:relative;width:100%;max-width:330px;background:var(--surface);
  border-radius:18px;padding:20px;box-shadow:var(--shadow);
}
.dialog-panel h4{margin:0 0 8px;font-size:16px}
.dialog-panel p{margin:0 0 18px;font-size:13.5px;color:var(--text-sub);line-height:1.6}

@media (min-width:700px){
  .msg-body{max-width:calc(100% - 42px)}
  .welcome{padding-top:60px}
}

/* ==========================================================================
   图片上传 / 图片消息
   ========================================================================== */
.attach-bar{
  display:none;gap:8px;padding:8px 4px;overflow-x:auto;align-items:center;
}
.attach-item{
  position:relative;width:62px;height:62px;flex-shrink:0;border-radius:12px;
  overflow:hidden;border:1px solid var(--border);background:var(--surface-2);
}
.attach-item img{width:100%;height:100%;object-fit:cover;display:block}
.attach-del{
  position:absolute;top:2px;right:2px;width:19px;height:19px;border:none;
  border-radius:50%;background:rgba(17,24,39,.72);color:#fff;
  display:flex;align-items:center;justify-content:center;padding:0;
}
.attach-del:active{background:rgba(239,68,68,.9)}

.img-grid{display:flex;flex-wrap:wrap;gap:6px;margin-bottom:2px}
.img-grid img{
  width:118px;height:118px;object-fit:cover;border-radius:12px;
  border:1px solid var(--border);background:var(--surface-2);cursor:zoom-in;
}
.img-grid.single img{width:170px;height:170px}
.user-text{white-space:pre-wrap;word-break:break-word}
.user-text + .img-grid,.img-grid + .user-text{margin-top:8px}

/* 图片查看器 */
.viewer{
  position:fixed;inset:0;z-index:300;display:none;
  align-items:center;justify-content:center;background:rgba(0,0,0,.92);
  padding:20px;
}
.viewer.show{display:flex}
.viewer img{max-width:100%;max-height:100%;object-fit:contain;border-radius:8px}
.viewer-close{
  position:absolute;top:16px;right:16px;width:38px;height:38px;border:none;
  border-radius:50%;background:rgba(255,255,255,.16);color:#fff;
  display:flex;align-items:center;justify-content:center;
}

/* ==========================================================================
   设置面板：分组标题 / 选项列表
   ========================================================================== */
.sec-title{
  font-size:12px;font-weight:700;color:var(--text-sub);letter-spacing:.04em;
  margin:22px 0 10px;padding-bottom:7px;border-bottom:1px solid var(--border);
  text-transform:uppercase;
}
.sec-title:first-child{margin-top:4px}

.opt-list{display:flex;flex-direction:column;gap:8px}
.opt-row{
  display:flex;align-items:center;gap:12px;width:100%;text-align:left;
  padding:13px 14px;border-radius:14px;border:1px solid var(--border);
  background:var(--surface-2);color:var(--text);
}
.opt-row:active{border-color:var(--primary);background:var(--primary-soft)}
.opt-row svg{color:var(--text-sub);flex-shrink:0}
.opt-text{flex:1;min-width:0}
.opt-text b{display:block;font-size:13.5px;font-weight:600;line-height:1.4}
.opt-text small{display:block;font-size:11.5px;color:var(--text-sub);margin-top:2px}

#provider-list .model-item{cursor:pointer}
#provider-list .mi-name{font-size:13px}
#model-provider{
  width:100%;padding:11px 13px;border-radius:12px;font-size:14px;
  border:1px solid var(--border);background:var(--surface-2);color:var(--text);
  outline:none;font-family:inherit;
}
