:root {
  --grad: linear-gradient(to right, rgb(198, 255, 221), rgb(251, 215, 134), rgb(247, 121, 125));
  --grad-accent: linear-gradient(135deg, rgb(251, 215, 134), rgb(247, 121, 125));
  --glass: rgba(255, 255, 255, 0.62);
  --glass-strong: rgba(255, 255, 255, 0.85);
  --glass-light: rgba(255, 255, 255, 0.42);
  --line: rgba(120, 80, 70, 0.18);
  --text: #2a1f1c;
  --muted: rgba(42, 31, 28, 0.55);
  --accent: rgb(232, 95, 100);
  --accent-text: #ffffff;
  --bubble-user: rgba(255, 255, 255, 0.78);
  --bubble-assistant: rgba(255, 255, 255, 0.45);
  --bubble-error: rgba(200, 60, 70, 0.9);
  --radius: 14px;
  --sidebar-w: 280px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  height: 100%;
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--text);
  overscroll-behavior: none;
}
body {
  display: flex;
  height: 100dvh; height: 100vh; height: 100svh;
  overflow: hidden;
  background: var(--grad) fixed;
}

button { font: inherit; }
input, textarea { font: inherit; -webkit-appearance: none; appearance: none; }

/* ---------------- 移动优先：抽屉式侧栏 ---------------- */
#sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: min(var(--sidebar-w), 85vw);
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  background: var(--glass);
  -webkit-backdrop-filter: blur(20px) saturate(140%);
  backdrop-filter: blur(20px) saturate(140%);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  z-index: 30;
  transform: translateX(-100%);
  transition: transform 0.22s ease;
}
body.sidebar-open #sidebar {
  transform: translateX(0);
  box-shadow: 4px 0 18px rgba(80, 30, 30, 0.18);
}

.side-head {
  display: flex; align-items: center; gap: 8px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
#new-thread {
  flex: 1;
  min-height: 44px;
  padding: 10px;
  background: var(--grad-accent); color: #fff;
  border: none; border-radius: 12px;
  font-size: 15px; font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(247, 121, 125, 0.28);
}
#new-thread:active { filter: brightness(0.92); transform: translateY(1px); }
#close-sidebar {
  width: 44px; height: 44px;
  background: transparent; color: var(--muted);
  border: none; border-radius: 10px;
  font-size: 24px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
#close-sidebar:active { background: var(--glass-strong); }

#thread-list {
  flex: 1; overflow-y: auto;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
}
.thread-item {
  position: relative;
  padding: 12px 40px 12px 12px;
  margin-bottom: 4px;
  border-radius: 10px;
  font-size: 15px; color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  cursor: pointer;
  min-height: 44px;
  display: flex; align-items: center;
  background: transparent;
  transition: background 0.15s;
}
.thread-item:active { background: var(--glass-strong); }
.thread-item.active {
  background: var(--glass-strong);
  font-weight: 600;
}
.thread-item .del-btn {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 32px; height: 32px;
  background: transparent; color: var(--muted);
  border: none; border-radius: 6px;
  font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.thread-item .del-btn:active { background: rgba(200, 60, 70, 0.85); color: #fff; }

/* ---------------- 聊天主区 ---------------- */
#chat {
  flex: 1;
  display: flex; flex-direction: column;
  min-width: 0;
  height: 100%;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  position: relative;
}

#topbar {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px;
  padding-top: calc(8px + var(--safe-top));
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  min-height: calc(52px + var(--safe-top));
  background: var(--glass);
  -webkit-backdrop-filter: blur(20px) saturate(140%);
  backdrop-filter: blur(20px) saturate(140%);
}
.icon-btn {
  width: 44px; height: 44px;
  background: transparent; color: var(--text);
  border: none; border-radius: 10px;
  font-size: 22px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.icon-btn:active { background: var(--glass-strong); }

.title-wrap {
  flex: 1; min-width: 0;
  display: flex; align-items: center;
  position: relative;
  border-radius: 8px;
  transition: background 0.15s;
}
.title-wrap:hover { background: var(--glass-strong); }
.title-wrap:focus-within { background: var(--glass-strong); }
#thread-title {
  flex: 1; min-width: 0;
  background: transparent; border: none; outline: none;
  color: var(--text);
  font-size: 16px; font-weight: 600;
  padding: 8px 4px 8px 10px;
  height: 44px;
  cursor: text;
}
#thread-title::placeholder { color: var(--muted); font-weight: 500; }
.title-edit-hint {
  color: var(--muted);
  font-size: 14px;
  padding: 0 10px;
  pointer-events: none;
  opacity: 0.7;
}
.title-wrap:hover .title-edit-hint,
.title-wrap:focus-within .title-edit-hint { opacity: 1; }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}
.msg {
  margin-bottom: 14px;
  line-height: 1.7;
  font-size: 16px;
}
.msg.user {
  margin-left: auto;
  max-width: 85%;
  background: var(--bubble-user);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  padding: 10px 14px;
  border-radius: var(--radius);
  box-shadow: 0 2px 8px rgba(80, 40, 40, 0.08);
  color: var(--text);
}
.msg.assistant {
  margin-right: auto;
  max-width: 100%;
  padding: 10px 14px;
  background: var(--bubble-assistant);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  border-radius: var(--radius);
  color: var(--text);
}
.msg.error {
  max-width: 90%;
  background: var(--bubble-error);
  padding: 10px 14px;
  border-radius: var(--radius);
  color: #fff;
  font-size: 14px;
}
.msg-content { white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }

.typing {
  display: inline-block;
  color: var(--muted); font-style: italic;
}
.typing::after {
  content: '…';
  animation: dots 1.4s steps(4, end) infinite;
  display: inline-block; width: 1em; text-align: left;
}
@keyframes dots {
  0%, 20% { content: ''; }
  40%     { content: '·'; }
  60%     { content: '··'; }
  80%, 100% { content: '···'; }
}

/* ---------------- 输入区 ---------------- */
#composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px;
  padding-bottom: calc(8px + var(--safe-bottom));
  border-top: 1px solid var(--line);
  flex-shrink: 0;
  background: var(--glass);
  -webkit-backdrop-filter: blur(20px) saturate(140%);
  backdrop-filter: blur(20px) saturate(140%);
}
#prompt {
  flex: 1; min-width: 0;
  resize: none;
  min-height: 44px;
  max-height: 40dvh;
  background: var(--glass-strong);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 14px;
  font-size: 16px;
  line-height: 1.4;
  outline: none;
}
#prompt::placeholder { color: var(--muted); }
#prompt:focus { border-color: var(--accent); background: #fff; }
#send {
  flex-shrink: 0;
  width: 44px; height: 44px;
  background: var(--grad-accent); color: #fff;
  border: none; border-radius: 12px;
  font-size: 22px; font-weight: 700;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 12px rgba(247, 121, 125, 0.32);
}
#send:active { filter: brightness(0.92); transform: translateY(1px); }
#send:disabled { opacity: 0.45; filter: grayscale(0.4); box-shadow: none; }

/* ---------------- 抽屉遮罩 ---------------- */
#sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(80, 40, 40, 0.32);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  z-index: 20;
}
body.sidebar-open #sidebar-backdrop { display: block; }

/* ---------------- 桌面 ---------------- */
@media (min-width: 721px) {
  #sidebar {
    position: relative;
    transform: translateX(0);
    box-shadow: none;
    width: var(--sidebar-w);
    flex-shrink: 0;
  }
  #close-sidebar { display: none; }
  body:not(.sidebar-closed) #sidebar-backdrop { display: none !important; }
  body.sidebar-closed #sidebar {
    margin-left: calc(min(var(--sidebar-w), 85vw) * -1);
    transition: margin-left 0.22s ease;
  }
  .msg.user { max-width: 70%; }
  .msg.assistant { max-width: 75%; }
  #messages { padding: 24px 28px; }
}

/* ─── 模式切换 tab ─── */
.mode-tabs { display: flex; gap: 4px; flex: 1; }
.mode-tab {
  flex: 1; padding: 8px 0; border: none; border-radius: 8px;
  font-size: 13px; font-weight: 600; cursor: pointer;
  background: transparent; color: var(--muted);
  transition: all 0.15s;
}
.mode-tab.active { background: var(--glass-strong); color: var(--text); }
.mode-tab:active { transform: scale(0.96); }
.panel-action { padding: 8px 8px 4px; }
#new-thread { width: 100%; }

/* ─── 房间列表 ─── */
#rooms-panel { flex: 1; overflow-y: auto; padding: 8px; -webkit-overflow-scrolling: touch; }
.room-item {
  padding: 12px;
  margin-bottom: 4px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s;
  display: flex; align-items: center; gap: 10px;
}
.room-item:active, .room-item.active { background: var(--glass-strong); }
.room-item .room-emoji { font-size: 20px; flex-shrink: 0; }
.room-item .room-info { flex: 1; min-width: 0; }
.room-item .room-name { font-size: 14px; font-weight: 600; color: var(--text); }
.room-item .room-last { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ─── 群聊主区 ─── */
#room-view { flex: 1; display: none; flex-direction: column; min-width: 0; height: 100%; position: relative; }
#room-view.show { display: flex; }
#room-topbar {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; padding-top: calc(8px + var(--safe-top));
  border-bottom: 1px solid var(--line);
  flex-shrink: 0; min-height: calc(52px + var(--safe-top));
  background: var(--glass);
  -webkit-backdrop-filter: blur(20px) saturate(140%);
  backdrop-filter: blur(20px) saturate(140%);
}
#room-title { flex: 1; font-size: 16px; font-weight: 600; color: var(--text); }
#room-messages {
  flex: 1; overflow-y: auto; padding: 16px; min-height: 0;
  -webkit-overflow-scrolling: touch;
}
.room-msg {
  margin-bottom: 10px;
  max-width: 85%;
  padding: 8px 12px;
  border-radius: var(--radius);
  line-height: 1.6; font-size: 15px;
  word-break: break-word;
}
.room-msg .rm-sender { font-size: 11px; font-weight: 600; margin-bottom: 2px; opacity: 0.7; }
.room-msg .rm-time { font-size: 10px; opacity: 0.4; margin-top: 3px; text-align: right; }
.room-msg .rm-text { white-space: pre-wrap; }
.room-msg.heihei { background: rgba(255,255,255,0.6); border: 1px solid rgba(120,80,70,0.12); }
.room-msg.heihei .rm-sender { color: #333; }
.room-msg.baibai { background: rgba(200,230,255,0.5); border: 1px solid rgba(100,150,200,0.15); }
.room-msg.baibai .rm-sender { color: #0277bd; }
.room-msg.rain { margin-left: auto; background: var(--bubble-user); border: 1px solid var(--line); }
.room-msg.rain .rm-sender { color: var(--accent); }
.room-msg.cc { background: rgba(200,255,220,0.5); border: 1px solid rgba(80,160,100,0.15); }
.room-msg.cc .rm-sender { color: #2e7d32; }
.room-msg.system { margin: 0 auto; max-width: 90%; text-align: center; background: var(--glass-light); font-size: 13px; color: var(--muted); }

#room-composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px; padding-bottom: calc(8px + var(--safe-bottom));
  border-top: 1px solid var(--line); flex-shrink: 0;
  background: var(--glass);
  -webkit-backdrop-filter: blur(20px) saturate(140%);
  backdrop-filter: blur(20px) saturate(140%);
}
#room-prompt {
  flex: 1; min-width: 0; resize: none;
  min-height: 44px; max-height: 40dvh;
  background: var(--glass-strong); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px;
  padding: 11px 14px; font-size: 16px; line-height: 1.4; outline: none;
}
#room-prompt:focus { border-color: var(--accent); background: #fff; }
#room-send {
  flex-shrink: 0; width: 44px; height: 44px;
  background: var(--grad-accent); color: #fff;
  border: none; border-radius: 12px;
  font-size: 22px; font-weight: 700; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 12px rgba(247,121,125,0.32);
}
#room-send:active { filter: brightness(0.92); transform: translateY(1px); }

/* ─── 传图浮层 ─── */
#upload-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(80,40,40,0.4);
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
  display: none; align-items: center; justify-content: center;
}
#upload-overlay.show { display: flex; }
#upload-panel {
  background: #fff; border-radius: 20px; width: 90%; max-width: 400px;
  padding: 20px; box-shadow: 0 8px 32px rgba(0,0,0,0.15);
}
.upload-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; font-weight: 600; }
.upload-drop {
  min-height: 120px; border: 2px dashed var(--line); border-radius: 14px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  cursor: pointer; transition: all 0.2s; padding: 16px;
}
.upload-drop:hover, .upload-drop.dragover { border-color: var(--accent); background: rgba(247,121,125,0.05); }
.upload-drop .ud-icon { font-size: 2em; margin-bottom: 6px; }
.upload-drop p { color: var(--muted); font-size: 13px; }
.upload-preview { margin-top: 12px; }
.upload-preview img { width: 100%; border-radius: 12px; max-height: 240px; object-fit: contain; }
.up-info { font-size: 11px; color: var(--muted); margin-top: 6px; text-align: center; }
.upload-send {
  margin-top: 12px; padding: 10px 0; width: 100%;
  background: var(--grad-accent); color: #fff; border: none; border-radius: 12px;
  font-size: 15px; font-weight: 600; cursor: pointer;
}
.upload-send:disabled { opacity: 0.4; cursor: not-allowed; }
.upload-status { margin-top: 8px; font-size: 13px; text-align: center; }
.upload-status.ok { color: #2e7d32; }
.upload-status.err { color: var(--accent); }

@media (min-width: 721px) {
  #room-messages { padding: 24px 28px; }
  .room-msg { max-width: 70%; }
}

/* ─── @CC 按钮 ─── */
.call-cc-btn {
  flex-shrink: 0; height: 44px; padding: 0 14px;
  background: rgba(46,125,50,0.12); color: #2e7d32;
  border: 1.5px solid rgba(46,125,50,0.25); border-radius: 12px;
  font-size: 13px; font-weight: 700; cursor: pointer;
  transition: all 0.15s;
}
.call-cc-btn:hover { background: rgba(46,125,50,0.2); }
.call-cc-btn:active { transform: scale(0.95); }
.call-cc-btn:disabled { opacity: 0.4; cursor: not-allowed; }
