:root {
  --bg: #0f1117;
  --bg-soft: #161925;
  --bg-elevated: #1d2130;
  --border: rgba(255, 255, 255, 0.08);
  --text: #e8eaf2;
  --text-dim: #9aa3b8;
  --accent: #5b8cff;
  --accent-soft: rgba(91, 140, 255, 0.16);
  --danger: #ff6b6b;
  --radius: 14px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", "Hiragino Sans GB", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  background:
    radial-gradient(900px 500px at 85% -10%, rgba(91, 140, 255, 0.16), transparent 60%),
    radial-gradient(700px 420px at 5% 105%, rgba(168, 91, 255, 0.12), transparent 60%);
  pointer-events: none;
}

button, textarea, input { font: inherit; color: inherit; }
button, textarea, input, a { touch-action: manipulation; }
button { -webkit-tap-highlight-color: transparent; }

/* 触屏上别让 :hover 粘住 */
@media (hover: none) {
  .ghost-btn:hover { color: var(--text-dim); border-color: var(--border); background: var(--bg-soft); }
  .icon-btn:hover { background: var(--bg-soft); border-color: var(--border); }
  .panel-view button:hover, .panel-tab:hover { background: transparent; }
  .panel-tab.active:hover { background: rgba(91, 140, 255, 0.18); }
  .send-btn:hover { filter: none; }
}

.app {
  position: relative;
  display: flex;
  flex-direction: column;
  /* 手机上用 JS 写入的 visualViewport 高度，键盘弹出时输入框不会被挡住 */
  height: var(--app-height, 100dvh);
  max-width: 900px;
  margin: 0 auto;
}

/* ---------------------------------------------------------------- 顶栏 */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: rgba(15, 17, 23, 0.82);
  backdrop-filter: blur(12px);
  z-index: 5;
}

.topbar-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { font-size: 22px; }
.titles { min-width: 0; }
.titles h1 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: 0.2px; }

.subtitle {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}
.subtitle .sep { opacity: 0.5; }

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #f5b944;
  box-shadow: 0 0 0 3px rgba(245, 185, 68, 0.15);
  transition: background 0.3s, box-shadow 0.3s;
}
.dot.online { background: #34d399; box-shadow: 0 0 0 3px rgba(52, 211, 153, 0.15); }
.dot.offline { background: var(--danger); box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.15); }

.topbar-right { display: flex; align-items: center; gap: 8px; }

.ghost-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 200px;
  padding: 6px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-soft);
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.ghost-btn:hover { color: var(--text); border-color: rgba(255, 255, 255, 0.18); background: var(--bg-elevated); }
.ghost-icon { padding: 6px 9px; font-size: 14px; line-height: 1; }
#me-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------------------------------------------------------------- 断线横幅 */
.offline-banner {
  padding: 6px 12px;
  text-align: center;
  font-size: 12.5px;
  color: #ffb4b4;
  background: rgba(255, 107, 107, 0.14);
  border-bottom: 1px solid rgba(255, 107, 107, 0.22);
}

/* ---------------------------------------------------------------- 在线名单 */
.user-panel {
  position: absolute;
  top: 58px;
  right: 12px;
  width: 240px;
  max-height: 60vh;
  overflow: auto;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
  z-index: 20;
}
.user-panel-title { font-size: 12px; color: var(--text-dim); margin-bottom: 8px; letter-spacing: 0.4px; }
.user-panel ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.user-panel li { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.user-panel li .me-tag {
  font-size: 11px;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 0 4px;
}
.user-panel .hint { margin: 12px 0 0; font-size: 12px; color: var(--text-dim); line-height: 1.5; }

/* ---------------------------------------------------------------- 消息区 */
.messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.16) transparent;
}
.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.14); border-radius: 8px; }

/* ---------------------------------------------------------------- 空状态 */
.empty-state {
  margin: 9vh auto 14px;
  padding: 24px 18px;
  text-align: center;
  color: var(--text-dim);
  animation: pop 0.3s ease-out;
}
.empty-emoji { font-size: 42px; line-height: 1; }
.empty-title { margin: 10px 0 12px; font-size: 15px; color: var(--text); }
.empty-tips { margin: 0; font-size: 12.5px; line-height: 2; }

.day-divider {
  align-self: center;
  margin: 14px 0 10px;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.05);
  font-size: 12px;
  color: var(--text-dim);
}

.sys {
  align-self: center;
  margin: 6px 0;
  font-size: 12px;
  color: var(--text-dim);
  opacity: 0.7;
}

.msg {
  display: flex;
  gap: 10px;
  padding: 3px 0;
  max-width: 100%;
  animation: pop 0.16s ease-out;
}
@keyframes pop { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.msg.self { flex-direction: row-reverse; }

.avatar {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 15px;
  font-weight: 600;
  color: #0d1018;
  user-select: none;
}
.avatar-sm { width: 20px; height: 20px; font-size: 10px; }
.msg.spacer .avatar { visibility: hidden; height: 0; }
.msg.spacer { margin-top: -3px; }

.bubble-wrap { display: flex; flex-direction: column; min-width: 0; max-width: min(680px, 78%); }
.msg.self .bubble-wrap { align-items: flex-end; }

.msg-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 3px;
  font-size: 12px;
  color: var(--text-dim);
}
.msg-head .name { font-size: 13px; font-weight: 600; }
.msg.self .msg-head { flex-direction: row-reverse; }

.bubble {
  padding: 8px 12px;
  border-radius: 14px;
  border-top-left-radius: 4px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.msg.self .bubble {
  border-top-left-radius: 14px;
  border-top-right-radius: 4px;
  background: var(--accent-soft);
  border-color: rgba(91, 140, 255, 0.35);
}
.bubble a { color: #8ab4ff; text-decoration: none; border-bottom: 1px solid rgba(138, 180, 255, 0.4); }
.bubble a:hover { border-bottom-color: #8ab4ff; }
.bubble.emoji-only { font-size: 30px; line-height: 1.25; padding: 4px 10px; background: none; border: none; }

.msg-head .status { font-size: 11px; color: var(--text-dim); }
.msg.pending .bubble { opacity: 0.55; }
.msg.pending .status::after { content: "发送中…"; }
.msg.failed .bubble { background: rgba(255, 107, 107, 0.1); border-color: rgba(255, 107, 107, 0.45); }
.msg.failed .status { color: #ff9d9d; cursor: pointer; }

.bubble img {
  display: block;
  max-width: 100%;
  max-height: 340px;
  border-radius: 10px;
  cursor: zoom-in;
}
.bubble.has-image { padding: 5px; }
.bubble.has-image .caption { padding: 6px 7px 2px; }
.bubble.sticker { background: none; border: none; padding: 0; }
.bubble.sticker img { max-width: 150px; max-height: 150px; border-radius: 6px; }

/* ---------------------------------------------------------------- 输入区 */
.composer {
  position: relative;
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: rgba(15, 17, 23, 0.9);
  backdrop-filter: blur(12px);
}
.composer-row { display: flex; align-items: flex-end; gap: 8px; }

.icon-btn {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  cursor: pointer;
  font-size: 17px;
  transition: background 0.15s, border-color 0.15s;
}
.icon-btn:hover { background: var(--bg-elevated); border-color: rgba(255, 255, 255, 0.18); }

.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-right: 6px;
  vertical-align: -2px;
  border: 2px solid rgba(255, 255, 255, 0.22);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.emoji-panel {
  position: absolute;
  bottom: calc(100% - 6px);
  left: 12px;
  width: min(320px, calc(100% - 24px));
  max-height: min(42vh, 330px);
  overflow: auto;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
  z-index: 15;
  animation: pop 0.14s ease-out;
}

.panel-tabs {
  position: sticky;
  top: -8px;
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
  padding: 2px 0;
  background: var(--bg-elevated);
}
.panel-tab {
  flex: 1;
  padding: 5px 0;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
}
.panel-tab:hover { background: rgba(255, 255, 255, 0.06); }
.panel-tab.active { background: rgba(91, 140, 255, 0.18); color: var(--text); font-weight: 600; }

.panel-view {
  display: grid;
  /* minmax(0, 1fr)：不让里面的图片把格子撑大 */
  grid-template-columns: repeat(auto-fill, minmax(34px, 1fr));
  gap: 2px;
  min-width: 0;
}
.panel-view button {
  padding: 6px 0;
  border: none;
  border-radius: 8px;
  background: transparent;
  font-size: 20px;
  line-height: 1.2;
  cursor: pointer;
}
.panel-view button:hover { background: rgba(255, 255, 255, 0.09); }

.sticker-grid {
  grid-template-columns: repeat(auto-fill, minmax(46px, 1fr));
  gap: 6px;
}
.sticker-grid .sticker-btn {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  aspect-ratio: 1 / 1;
  padding: 4px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 10px;
  overflow: hidden;
}
.sticker-grid .sticker-btn img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}
.sticker-grid .sticker-add {
  border: 1px dashed rgba(255, 255, 255, 0.25);
  color: var(--text-dim);
  font-size: 11px;
  line-height: 1.35;
}
.sticker-empty {
  grid-column: 1 / -1;
  padding: 4px 2px 8px;
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.7;
}

#input {
  flex: 1;
  min-height: 40px;
  max-height: 180px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  resize: none;
  outline: none;
  line-height: 1.45;
  transition: border-color 0.15s;
}
#input:focus { border-color: rgba(91, 140, 255, 0.6); }
#input::placeholder { color: #6c7488; }

.send-btn {
  flex: 0 0 auto;
  height: 40px;
  padding: 0 18px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.15s, transform 0.1s;
}
.send-btn:hover { filter: brightness(1.1); }
.send-btn:active { transform: scale(0.97); }
.send-btn:disabled { opacity: 0.5; cursor: default; }

.typing {
  height: 18px;
  padding: 0 4px;
  font-size: 12px;
  color: var(--text-dim);
  opacity: 0;
  transition: opacity 0.2s;
}
.typing.show { opacity: 1; }

.drop-hint {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(91, 140, 255, 0.18);
  border: 2px dashed var(--accent);
  font-weight: 600;
  z-index: 10;
}

/* ---------------------------------------------------------------- 悬浮按钮 */
.jump-btn {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 92px;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  box-shadow: var(--shadow);
  z-index: 6;
}

/* ---------------------------------------------------------------- 图片大图 */
.lightbox {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.86);
  z-index: 50;
  cursor: zoom-out;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.lightbox-close {
  position: absolute;
  top: 14px;
  right: 18px;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 110px;
  transform: translateX(-50%);
  padding: 8px 16px;
  border-radius: 10px;
  background: rgba(255, 107, 107, 0.92);
  color: #fff;
  font-size: 13px;
  z-index: 60;
  box-shadow: var(--shadow);
}

/* ---------------------------------------------------------------- 窄屏 */
@media (max-width: 560px) {
  .topbar { padding: 10px 12px; gap: 8px; }
  .titles h1 { font-size: 14px; }
  #status-text, .subtitle .sep { display: none; }
  #users-btn { display: none; }
  #me-btn { max-width: 132px; }
  .ghost-btn { padding: 8px 10px; }          /* 手指点得到 */
  .bubble-wrap { max-width: 84%; }
  .msg-head .time { display: none; }
  .msg { gap: 8px; }
  .avatar { width: 32px; height: 32px; font-size: 14px; }
  .sys { font-size: 11.5px; margin: 4px 0; }
  .user-panel { left: 12px; right: 12px; width: auto; }
  .icon-btn { width: 40px; height: 40px; font-size: 17px; }
  /* 16px 是底线：小于它 iOS Safari 会在聚焦时自动放大页面 */
  #input { font-size: 16px; min-height: 40px; max-height: 140px; padding: 9px 12px; }
  .send-btn { min-height: 40px; height: 40px; padding: 0 15px; }
  .composer { padding: 6px 10px calc(8px + env(safe-area-inset-bottom)); }
  .composer-row { gap: 6px; }
  .emoji-panel { left: 10px; width: min(340px, calc(100% - 20px)); max-height: min(40vh, 300px); }
  .panel-view { grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); }
  .sticker-grid { grid-template-columns: repeat(auto-fill, minmax(46px, 1fr)); }
  .panel-view button { font-size: 22px; padding: 8px 0; }
  .bubble { padding: 8px 11px; }
  .bubble.emoji-only { font-size: 32px; }
  .jump-btn { bottom: 100px; }
}

/* 横屏、超窄屏（如 iPhone SE 320）再收一点 */
@media (max-width: 380px) {
  #me-btn { max-width: 116px; }
  .icon-btn { width: 38px; height: 38px; }
  .send-btn { padding: 0 12px; }
  .msg { gap: 6px; }
  .avatar { width: 30px; height: 30px; font-size: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
