/* ============================================================
   创意熔炉 —— ChatGPT 风格暗色主题
   ============================================================ */
:root {
  --bg: #212121;
  --bg-elev: #2f2f2f;
  --bg-hover: #3a3a3a;
  --bg-active: #454545;
  --sidebar: #171717;
  --sidebar-hover: #2f2f2f;
  --border: #3d3d3d;
  --border-soft: #2c2c2c;
  --text: #ececec;
  --text-dim: #b4b4b4;
  --text-faint: #8f8f8f;
  --accent: #10a37f;
  --accent-hover: #0d8a6c;
  --user-bubble: #303030;
  --danger: #ef4444;
  --radius: 12px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

/* hidden 属性必须能压过组件自身的 display，否则弹层会一直显示 */
[hidden] { display: none !important; }

* { 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",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  overscroll-behavior: none;
}

body { display: flex; overflow: hidden; }

svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ============ 侧边栏 ============ */
.sidebar {
  width: 260px; flex-shrink: 0;
  background: var(--sidebar);
  display: flex; flex-direction: column;
  height: 100%;
  padding-top: var(--safe-t);
  z-index: 40;
}

.sidebar-head { display: flex; align-items: center; gap: 6px; padding: 12px 10px; }

.icon-btn {
  width: 36px; height: 36px; flex-shrink: 0;
  border: none; background: transparent; color: var(--text-dim);
  border-radius: 8px; cursor: pointer; padding: 8px;
  transition: background .15s;
}
.icon-btn:hover { background: var(--sidebar-hover); color: var(--text); }

.new-chat-btn {
  flex: 1; display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border: none; background: transparent;
  color: var(--text); font-size: 14px; font-family: inherit;
  border-radius: 10px; cursor: pointer; text-align: left;
  transition: background .15s;
}
.new-chat-btn:hover { background: var(--sidebar-hover); }
.pencil { width: 17px; height: 17px; color: var(--text-dim); flex-shrink: 0; }

.sidebar-scroll { flex: 1; overflow-y: auto; padding: 4px 8px; }
.history-group { margin-bottom: 18px; }
.history-title { font-size: 12px; color: var(--text-faint); padding: 6px 10px; }

.hist-item {
  display: flex; align-items: center; gap: 4px;
  padding: 9px 8px; border-radius: 9px; cursor: pointer;
  color: var(--text-dim); font-size: 13.5px;
  transition: background .12s;
  position: relative;
}
.hist-item:hover { background: var(--sidebar-hover); color: var(--text); }
.hist-item.active { background: var(--bg-active); color: var(--text); }

/* 标题：可点进入重命名 */
.hist-title-text {
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 1px 2px; border-radius: 4px;
}
.hist-title-text:hover { background: rgba(255,255,255,.06); }

/* 重命名输入框 */
.hist-input {
  flex: 1; min-width: 0;
  background: var(--bg-elev);
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: var(--text);
  font-size: 13.5px; font-family: inherit;
  padding: 4px 7px; outline: none;
}
.hist-item.editing { background: var(--bg-active); }
.hist-item.editing .hist-act { display: none; }

/* 悬停显示的操作按钮 */
.hist-act {
  width: 22px; height: 22px; border: none; background: transparent;
  color: var(--text-faint); border-radius: 5px; cursor: pointer;
  padding: 4px; flex-shrink: 0;
  opacity: 0; transition: opacity .12s;
}
.hist-item:hover .hist-act,
.hist-item.active .hist-act { opacity: 1; }
.hist-act:hover { background: var(--bg-hover); color: var(--text); }
.hist-act.hist-del:hover { background: var(--bg-hover); color: var(--danger); }
/* 触摸设备没有 hover，常驻显示 */
@media (hover: none) {
  .hist-act { opacity: 0.55; }
}

.sidebar-foot { padding: 10px; border-top: 1px solid var(--border-soft); padding-bottom: calc(10px + var(--safe-b)); }
.user-row { display: flex; align-items: center; gap: 10px; padding: 6px; border-radius: 9px; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, var(--accent), #0b7a5f);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600; color: #fff;
}
.user-name { font-size: 13.5px; }
.user-sub {
  font-size: 11px; color: var(--text-faint);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 170px;
}

/* 深思档按钮激活态 */
.tool-btn.active { color: var(--accent); background: rgba(16,163,127,.13); }

.scrim { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 35; }

/* ============ 主区 ============ */
.main { flex: 1; display: flex; flex-direction: column; height: 100%; min-width: 0; }

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + var(--safe-t)) 14px 10px;
  flex-shrink: 0;
}
.model-picker { display: flex; align-items: center; gap: 6px; cursor: pointer; padding: 4px 8px; border-radius: 8px; }
.model-picker:hover { background: var(--bg-hover); }
.model-name { font-size: 15.5px; font-weight: 500; }
.chev { width: 14px; height: 14px; color: var(--text-faint); }
.topbar-right { margin-left: auto; }
.ghost-btn {
  padding: 7px 13px; border-radius: 9px; cursor: pointer;
  background: transparent; border: 1px solid var(--border);
  color: var(--text-dim); font-size: 13.5px; font-family: inherit;
  transition: all .15s;
}
.ghost-btn:hover { background: var(--bg-hover); color: var(--text); }

/* ============ 对话流 ============ */
.scroll { flex: 1; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; }
.thread { max-width: 768px; margin: 0 auto; padding: 8px 18px 30px; }

/* --- 空态 --- */
.welcome { padding: 6vh 0 24px; text-align: center; animation: fadeUp .5s ease; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

.welcome-mark { width: 54px; height: 54px; margin: 0 auto 20px; }
.flame { stroke: none; fill: url(#g); fill: var(--accent); opacity: .92; }

.welcome-title { font-size: 30px; font-weight: 600; margin: 0 0 12px; letter-spacing: -.5px; }
.welcome-sub { font-size: 15px; color: var(--text-dim); margin: 0 auto 30px; max-width: 540px; line-height: 1.75; }
.welcome-sub strong { color: var(--text); font-weight: 600; }

.chips { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 12px; }
.chip {
  display: flex; align-items: center; gap: 11px; text-align: left;
  padding: 13px 15px; border-radius: var(--radius);
  background: var(--bg-elev); border: 1px solid var(--border-soft);
  color: var(--text-dim); font-size: 13.5px; font-family: inherit;
  cursor: pointer; transition: all .16s;
}
.chip:hover { background: var(--bg-hover); color: var(--text); border-color: var(--border); transform: translateY(-1px); }
.chip-ico { font-size: 17px; flex-shrink: 0; }

.capabilities { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.cap {
  display: flex; align-items: center; gap: 12px; text-align: left;
  padding: 14px 16px; border-radius: var(--radius);
  background: transparent; border: 1px dashed var(--border);
  color: var(--text); font-family: inherit; cursor: pointer;
  transition: all .16s;
}
.cap:hover { border-color: var(--accent); background: rgba(16,163,127,.06); }
.cap-ico { font-size: 19px; }
.cap span { display: flex; flex-direction: column; }
.cap b { font-size: 13.5px; font-weight: 500; }
.cap i { font-size: 12px; color: var(--text-faint); font-style: normal; margin-top: 2px; }

/* --- 消息 --- */
.msg { display: flex; gap: 14px; margin: 26px 0; animation: fadeUp .35s ease; }
.msg-user { justify-content: flex-end; }
.msg-user .bubble {
  background: var(--user-bubble); border-radius: var(--radius);
  padding: 11px 16px; max-width: 82%; white-space: pre-wrap;
  word-break: break-word; font-size: 15px;
}
.msg-avatar {
  width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0; margin-top: 2px;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, var(--accent), #0b7a5f);
}
.msg-avatar svg { width: 16px; height: 16px; fill: #fff; stroke: #fff; stroke-width: 1.6; }
.msg-body { flex: 1; min-width: 0; }
.msg-actions { display: flex; gap: 4px; margin-top: 10px; opacity: 0; transition: opacity .2s; }
.msg:hover .msg-actions { opacity: 1; }
.act-btn {
  padding: 5px 9px; border-radius: 7px; cursor: pointer;
  background: transparent; border: none; color: var(--text-faint);
  font-size: 12px; font-family: inherit; display: flex; align-items: center; gap: 5px;
}
.act-btn:hover { background: var(--bg-hover); color: var(--text); }
.act-btn svg { width: 14px; height: 14px; }

/* --- Markdown 排版 --- */
.md { font-size: 15px; word-break: break-word; }
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md h3 { font-size: 17px; font-weight: 650; margin: 26px 0 10px; color: #fff; }
.md h4 { font-size: 15.5px; font-weight: 600; margin: 20px 0 8px; color: #f0f0f0; }
.md p { margin: 11px 0; }
.md ul, .md ol { margin: 11px 0; padding-left: 24px; }
.md li { margin: 6px 0; }
.md strong { color: #fff; font-weight: 600; }
.md em { color: var(--text-dim); }
.md hr { border: none; border-top: 1px solid var(--border-soft); margin: 22px 0; }
.md blockquote {
  margin: 12px 0; padding: 8px 14px;
  border-left: 3px solid var(--accent); background: rgba(255,255,255,.03);
  border-radius: 0 8px 8px 0; color: var(--text-dim);
}
.md code {
  background: rgba(255,255,255,.09); padding: 2px 6px;
  border-radius: 5px; font-size: 13px; font-family: "SF Mono", Consolas, monospace;
}
.md pre {
  background: #171717; border: 1px solid var(--border-soft);
  padding: 14px; border-radius: 10px; overflow-x: auto; margin: 14px 0;
}
.md pre code { background: none; padding: 0; font-size: 12.5px; line-height: 1.6; }
.md table { width: 100%; border-collapse: collapse; margin: 14px 0; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 9px 11px; text-align: left; }
.md th { background: var(--bg-elev); font-weight: 600; }

/* --- 图片消息 --- */
.gen-image { margin: 16px 0; }
.gen-image img {
  width: 100%; max-width: 420px; border-radius: var(--radius);
  display: block; cursor: zoom-in; background: var(--bg-elev);
}
.img-bar { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.img-btn {
  padding: 8px 14px; border-radius: 9px; cursor: pointer;
  background: var(--bg-elev); border: 1px solid var(--border-soft);
  color: var(--text-dim); font-size: 13px; font-family: inherit;
  display: flex; align-items: center; gap: 6px; transition: all .15s;
}
.img-btn:hover { background: var(--bg-hover); color: var(--text); }
.img-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.img-btn.primary:hover { background: var(--accent-hover); }
.img-btn svg { width: 15px; height: 15px; }
.img-note { font-size: 13px; color: var(--text-faint); margin-top: 8px; line-height: 1.6; }

/* 用户上传的图 */
.attached-in-msg { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; justify-content: flex-end; }
.attached-in-msg img { width: 78px; height: 78px; object-fit: cover; border-radius: 9px; }

.thinking { display: flex; align-items: center; gap: 9px; color: var(--text-faint); font-size: 14px; padding: 6px 0; }
.dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); animation: bounce 1.3s infinite; }
.dot:nth-child(2) { animation-delay: .18s; }
.dot:nth-child(3) { animation-delay: .36s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity: .4; } 30% { transform: translateY(-5px); opacity: 1; } }

.err-box {
  background: rgba(239,68,68,.09); border: 1px solid rgba(239,68,68,.35);
  border-radius: 10px; padding: 12px 14px; font-size: 13.5px;
  color: #fca5a5; word-break: break-word;
}

/* ============ 输入区 ============ */
.composer-wrap {
  flex-shrink: 0; padding: 0 18px calc(10px + var(--safe-b));
  max-width: 768px; width: 100%; margin: 0 auto;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.composer {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 22px; padding: 12px 14px 10px;
  display: flex; flex-direction: column; gap: 8px;
  transition: border-color .18s;
}
.composer:focus-within { border-color: #565656; }
.composer textarea {
  background: transparent; border: none; resize: none; outline: none;
  color: var(--text); font-size: 15.5px; font-family: inherit;
  line-height: 1.6; max-height: 200px; padding: 0;
}
.composer textarea::placeholder { color: var(--text-faint); }

.composer-bar {
  display: flex; align-items: center; gap: 8px;
}
.tool-group {
  display: flex; align-items: center; gap: 4px;
  flex: 1; min-width: 0;
  overflow-x: auto; overflow-y: hidden;
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.tool-group::-webkit-scrollbar { display: none; }
.tool-btn {
  width: 34px; height: 34px; border-radius: 9px; cursor: pointer;
  background: transparent; border: none; color: var(--text-dim);
  padding: 8px; display: flex; align-items: center; justify-content: center;
  transition: all .15s;
}
.tool-btn:hover { background: var(--bg-hover); color: var(--text); }
.tool-btn.active { color: var(--accent); background: rgba(16,163,127,.13); }
.tool-btn.labeled {
  width: auto; padding: 7px 10px; gap: 5px;
  font-size: 12.5px; font-family: inherit;
  white-space: nowrap; flex-shrink: 0;
}
.tool-btn.labeled svg { width: 16px; height: 16px; flex-shrink: 0; }

.spacer { display: none; }

.stop-btn {
  padding: 6px 13px; border-radius: 8px; cursor: pointer;
  background: transparent; border: 1px solid var(--border);
  color: var(--text-dim); font-size: 13px; font-family: inherit;
  white-space: nowrap; flex-shrink: 0;
}
.stop-btn:hover { background: var(--bg-hover); color: var(--text); }

.send-btn {
  width: 34px; height: 34px; border-radius: 50%; cursor: pointer;
  background: var(--accent); border: none; color: #fff;
  display: flex; align-items: center; justify-content: center;
  transition: all .15s; flex-shrink: 0;
}
.send-btn svg { width: 17px; height: 17px; stroke-width: 2.2; }
.send-btn:disabled { background: var(--bg-active); color: var(--text-faint); cursor: not-allowed; }
.send-btn:not(:disabled):hover { background: var(--accent-hover); }

.disclaimer { font-size: 11.5px; color: var(--text-faint); text-align: center; margin-top: 9px; }

/* 参考图预览 */
.image-preview {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px; margin-bottom: 8px;
}
.preview-head {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 12.5px; color: var(--text-faint); margin-bottom: 8px;
}
.preview-head button {
  background: none; border: none; color: var(--text-faint);
  font-size: 12.5px; cursor: pointer; font-family: inherit; text-decoration: underline;
}
.preview-strip { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; }
.preview-item { position: relative; flex-shrink: 0; }
.preview-item img { width: 58px; height: 58px; object-fit: cover; border-radius: 8px; display: block; }
.preview-item button {
  position: absolute; top: -5px; right: -5px;
  width: 19px; height: 19px; border-radius: 50%;
  background: #3a3a3a; border: 1px solid var(--border);
  color: #fff; cursor: pointer; padding: 3px; display: flex;
}
.preview-item button svg { width: 11px; height: 11px; stroke-width: 2.5; }

/* ============ Lightbox ============ */
.lightbox {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(0,0,0,.94);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 20px; gap: 16px; animation: fadeIn .2s;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.lightbox img { max-width: 100%; max-height: 72vh; border-radius: 10px; object-fit: contain; }
.lb-close {
  position: absolute; top: calc(14px + var(--safe-t)); right: 14px;
  width: 40px; height: 40px; border-radius: 50%;
  background: rgba(255,255,255,.1); border: none; color: #fff;
  cursor: pointer; padding: 10px;
}
.lb-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; padding-bottom: var(--safe-b); }
.lb-btn {
  padding: 11px 20px; border-radius: 10px; cursor: pointer;
  background: rgba(255,255,255,.1); border: 1px solid rgba(255,255,255,.2);
  color: #fff; font-size: 14px; font-family: inherit;
}
.lb-btn.primary { background: var(--accent); border-color: var(--accent); font-weight: 500; }

/* ============ Toast ============ */
.toast {
  position: fixed; left: 50%; bottom: calc(28px + var(--safe-b));
  transform: translateX(-50%) translateY(20px);
  background: #3a3a3a; color: #fff; padding: 11px 20px;
  border-radius: 10px; font-size: 13.5px; z-index: 100;
  opacity: 0; pointer-events: none; transition: all .28s;
  max-width: 88vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ============ 移动端 ============ */
@media (max-width: 820px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    transition: transform .26s cubic-bezier(.4,0,.2,1);
    box-shadow: 4px 0 24px rgba(0,0,0,.4);
  }
  .sidebar.open { transform: none; }
  .scrim.show { display: block; }
  .chips, .capabilities { grid-template-columns: 1fr; }
  .welcome-title { font-size: 25px; }
  .welcome { padding-top: 4vh; }
  .thread { padding: 8px 14px 24px; }
  .composer-wrap { padding-left: 12px; padding-right: 12px; }
  .msg { gap: 10px; }
  .msg-user .bubble { max-width: 88%; }
  .gen-image img { max-width: 100%; }
}

@media (min-width: 821px) {
  .menu-btn { display: none; }
  #closeSidebar { display: none; }
  .ghost-btn { display: none; }
}
