:root {
  --bg: #0f1115;
  --panel: #161920;
  --panel-alt: #1d212b;
  --border: #272c38;
  --text: #e8ecf3;
  --muted: #8b95a7;
  --accent: #4c8bf5;
  --accent-hover: #3a77e0;
  --bubble-me: #274a87;
  --bubble-peer: #1f2430;
  --danger: #e05252;
  --ok: #38b87c;
  --radius: 12px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* 极简自动淡化滚动条 */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.25); }

.hidden { display: none !important; }

/* 主布局 */
#app {
  display: flex;
  height: 100%;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

/* 左侧好友栏 */
#sidebar {
  width: 280px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.me-info { min-width: 0; }
.me-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-email { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.header-actions { display: flex; gap: 6px; }

.icon-btn {
  background: var(--panel-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s;
}
.icon-btn:hover { background: #262c3a; }
.icon-btn.danger { color: #ff8080; border-color: rgba(224,82,82,0.4); }
.icon-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.contact-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.contact-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 12px;
  border-radius: 10px;
  cursor: pointer;
  margin-bottom: 4px;
  transition: background 0.12s;
}
.contact-item:hover { background: var(--panel-alt); }
.contact-item.active { background: #232b3c; }

.contact-meta { min-width: 0; flex: 1; }
.contact-name { font-size: 14px; font-weight: 500; display: flex; align-items: center; gap: 6px; }
.contact-preview { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 3px; }

.unread-badge {
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 99px;
  margin-left: 8px;
}

.e2ee-footer {
  padding: 10px 14px;
  font-size: 11px;
  color: var(--muted);
  border-top: 1px solid var(--border);
  text-align: center;
}

/* 右侧聊天区 */
#chat-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

#empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  gap: 10px;
  padding: 20px;
  text-align: center;
}

#active-chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-header {
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.chat-header-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
#back-btn { display: none; }
.peer-title { font-weight: 600; font-size: 15px; }
.peer-sub { font-size: 11px; color: var(--ok); }

.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg-row { display: flex; flex-direction: column; max-width: 78%; }
.msg-row.me { align-self: flex-end; align-items: flex-end; }
.msg-row.peer { align-self: flex-start; align-items: flex-start; }

.msg-bubble {
  padding: 9px 13px;
  border-radius: 14px;
  font-size: 14.5px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
  position: relative;
}
.msg-row.me .msg-bubble { background: var(--bubble-me); border-bottom-right-radius: 4px; }
.msg-row.peer .msg-bubble { background: var(--bubble-peer); border-bottom-left-radius: 4px; }
.msg-bubble.sticker-bubble { background: transparent !important; padding: 2px; }
.msg-bubble.system-warn { background: rgba(224,82,82,0.16); color: #ffb4b4; font-size: 13px; }

.msg-meta {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.msg-img {
  max-width: 240px;
  max-height: 280px;
  border-radius: 10px;
  cursor: pointer;
  display: block;
}

.msg-sticker {
  max-width: 150px;
  max-height: 150px;
  border-radius: 8px;
  display: block;
  cursor: pointer;
}

.msg-video {
  max-width: 280px;
  max-height: 320px;
  border-radius: 10px;
  background: #000;
  display: block;
}

.msg-audio {
  width: 230px;
  max-width: 100%;
  height: 38px;
}

.media-actions {
  margin-top: 6px;
  display: flex;
  gap: 8px;
  font-size: 12px;
}
.media-link {
  color: #9ec0ff;
  text-decoration: none;
  cursor: pointer;
  background: none;
  border: none;
  font-size: 12px;
  padding: 0;
}
.media-link:hover { text-decoration: underline; }

/* 表情包抽屉 */
#sticker-drawer {
  background: var(--panel);
  border-top: 1px solid var(--border);
  height: 220px;
  display: flex;
  flex-direction: column;
}

.drawer-tabs {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
}

.drawer-tab-group { display: flex; gap: 6px; }
.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(38px, 1fr));
  gap: 6px;
}
.emoji-btn {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
}
.emoji-btn:hover { background: var(--panel-alt); }

.sticker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  gap: 8px;
}
.sticker-card {
  position: relative;
  background: var(--panel-alt);
  border-radius: 8px;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 76px;
  cursor: pointer;
}
.sticker-card img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; }
.sticker-del {
  position: absolute;
  top: -5px;
  right: -5px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  border: none;
  font-size: 12px;
  cursor: pointer;
  line-height: 20px;
}

/* 输入栏 */
.composer {
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.composer-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.compress-label {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
  cursor: pointer;
}

.composer-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#msg-input {
  flex: 1;
  background: var(--panel-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  max-height: 120px;
  min-height: 40px;
  outline: none;
}
#msg-input:focus { border-color: var(--accent); }

.send-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  flex-shrink: 0;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

#voice-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(224,82,82,0.14);
  border: 1px solid rgba(224,82,82,0.4);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
}

/* 弹窗 */
.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}

.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  max-width: 460px;
  max-height: 88dvh;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-title { font-size: 17px; font-weight: 600; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; color: var(--muted); }
.field input[type="text"], .field input[type="password"] {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
}
.field input:focus { border-color: var(--accent); }

.section-box {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.section-box h4 { font-size: 14px; }
.hint { font-size: 12px; color: var(--muted); line-height: 1.5; }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.admin-user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.admin-user-row:last-child { border-bottom: none; }

/* 图片大图预览 */
#lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
  cursor: zoom-out;
}
#lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

/* Toast 提示 */
#toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #252b38;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 300;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  max-width: 90vw;
  text-align: center;
}

/* 移动端适配 */
@media (max-width: 768px) {
  #sidebar { width: 100%; }
  #app.mobile-chat-open #sidebar { display: none; }
  #app:not(.mobile-chat-open) #chat-pane { display: none; }
  #back-btn { display: inline-block; }
  .msg-row { max-width: 88%; }
}
