/* 小动物聊天室样式：移动优先，浅色卡片风格 + 圆角漫画感。 */

:root {
  --bg: #fdf6f9;
  --bg-soft: #fff6fa;
  --card: #ffffff;
  --text: #3a3138;
  --text-soft: #8b7d86;
  --line: #f0e2ea;
  --accent: #ff8fb1;
  --accent-deep: #e5668f;
  --accent-soft: #ffe6ef;
  --mine: #ffe9f1;
  --system: #f4eef2;
  --danger: #d9534f;
  --radius: 16px;
  --shadow: 0 6px 24px rgba(160, 120, 140, 0.12);
  --header-h: 64px;
}

* {
  box-sizing: border-box;
}

/*
 * [hidden] 必须真的隐藏。
 *
 * 坑：HTML 的 hidden 只是 UA 样式表里的 `display: none`，任何作者样式里的
 * `display: flex/grid/...` 都会把它盖掉。本文件里 .welcome / .composer /
 * .chat-header 等都设了 display:flex，于是 hidden="true" 完全失效——
 * 症状是"填完昵称进了聊天室，欢迎卡片还盖在上面"。
 */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: radial-gradient(circle at 12% 8%, #ffe9f2 0%, transparent 42%),
    radial-gradient(circle at 88% 4%, #e8f5ff 0%, transparent 38%), var(--bg);
  color: var(--text);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB',
    'Microsoft YaHei', sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  /*
   * 关键：必须是 height 而不是 min-height。
   * 只写 min-height:100dvh 时，.app 没有高度上限，.messages{flex:1} 会被消息内容撑高，
   * 滚动就落到整个页面上——JS 里 el.scrollTop = el.scrollHeight 对 #messages 完全无效，
   * 表现就是"新消息不会自动下滑"。加上 overflow:hidden 彻底断掉页面级滚动。
   */
  height: 100dvh;
  overflow: hidden;
  max-width: 780px;
  margin: 0 auto;
  background: var(--bg-soft);
  position: relative;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- 顶栏 ---------- */

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}

.chat-header__me {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.chat-header__avatar .avatar__img {
  width: 42px;
  height: 42px;
}

.chat-header__meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-header__name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-header__actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-soft);
}

.status__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #c9c0c6;
}

.status[data-kind='online'] .status__dot {
  background: #57c98a;
}

.status[data-kind='connecting'] .status__dot,
.status[data-kind='reconnecting'] .status__dot {
  background: #f0b429;
  animation: pulse 1.2s infinite;
}

.status[data-kind='offline'] .status__dot,
.status[data-kind='rate-limited'] .status__dot {
  background: var(--danger);
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.3;
  }
}

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

/* ---------- 按钮 ---------- */

.ghost-button,
.icon-button,
.send-button,
.emoji-panel__tab,
.emoji-panel__item,
.emoji-panel__quick-item,
.emoji-panel__close,
.new-messages,
.msg__image-button,
.avatar-option {
  font: inherit;
  cursor: pointer;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  transition: transform 0.12s ease, background 0.12s ease, border-color 0.12s ease;
}

.ghost-button:hover,
.icon-button:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.ghost-button:active,
.icon-button:active,
.send-button:active {
  transform: scale(0.96);
}

.ghost-button {
  padding: 6px 12px;
  font-size: 13px;
}

.ghost-button--warn {
  border-color: var(--danger);
  color: var(--danger);
}

.badge {
  display: inline-block;
  min-width: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-deep);
  font-size: 12px;
  font-weight: 600;
  text-align: center;
}

.icon-button {
  width: 40px;
  height: 40px;
  font-size: 19px;
  line-height: 1;
  flex-shrink: 0;
  padding: 0;
}

.send-button {
  padding: 9px 18px;
  border: none;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-deep) 100%);
  color: #fff;
  font-weight: 600;
  flex-shrink: 0;
}

.send-button:disabled,
.icon-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ---------- 在线面板 ---------- */

.online-panel {
  padding: 8px 14px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}

.online-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.online__item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px 3px 3px;
  border-radius: 999px;
  background: var(--bg-soft);
  font-size: 13px;
}

.online__empty {
  font-size: 13px;
  color: var(--text-soft);
}

/* ---------- 消息流 ---------- */

.messages {
  flex: 1;
  /* flex 子项默认 min-height:auto，内容多时会被顶大导致内部不出现滚动条 */
  min-height: 0;
  overflow-y: auto;
  /* 用 overscroll-behavior 防止滚动到边界时把整页带走（移动端手感） */
  overscroll-behavior: contain;
  padding: 14px 14px 4px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  /*
   * 刻意不用 scroll-behavior:smooth。开了它，每次 scrollTo 都是一次动画，
   * 连续来消息时动画不断被打断重启，反而追不到底。贴底改成瞬时，
   * 需要动画的场合（点"有新消息"）在 JS 里显式指定 behavior:"smooth"。
   */
}

.avatar {
  display: inline-flex;
  flex-shrink: 0;
}

.avatar__img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 6px rgba(160, 120, 140, 0.18);
  display: block;
}

.avatar--tiny .avatar__img {
  width: 26px;
  height: 26px;
}

.msg {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  max-width: 100%;
}

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

.msg__main {
  min-width: 0;
  max-width: min(78%, 520px);
}

.msg--mine .msg__main {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.msg__head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 12px;
  color: var(--text-soft);
  padding: 0 4px 2px;
}

.msg--mine .msg__head {
  flex-direction: row-reverse;
}

.msg__name {
  font-weight: 600;
  color: #6b5c65;
  max-width: 12em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.msg__bubble {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 12px;
  box-shadow: var(--shadow);
  word-break: break-word;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg--mine .msg__bubble {
  background: var(--mine);
  border-color: #ffd3e2;
}

.msg--pending .msg__bubble {
  opacity: 0.6;
}

.msg__text--emoji {
  font-size: 34px;
  line-height: 1.25;
}

.msg__bubble--image {
  padding: 6px;
}

.msg__image-button {
  display: block;
  padding: 0;
  border: none;
  border-radius: 12px;
  overflow: hidden;
  background: transparent;
  max-width: 100%;
}

.msg__image {
  display: block;
  max-width: min(320px, 62vw);
  max-height: 320px;
  width: auto;
  height: auto;
  border-radius: 12px;
}

.msg--system {
  justify-content: center;
}

.msg__system-body {
  background: var(--system);
  color: var(--text-soft);
  font-size: 12.5px;
  padding: 4px 12px;
  border-radius: 999px;
  text-align: center;
}

.day-separator {
  display: flex;
  justify-content: center;
  margin: 6px 0;
}

.day-separator__label {
  background: rgba(255, 255, 255, 0.85);
  border: 1px solid var(--line);
  color: var(--text-soft);
  font-size: 12px;
  padding: 2px 12px;
  border-radius: 999px;
}

.messages__footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 0 14px 6px;
}

.typing {
  font-size: 12px;
  color: var(--text-soft);
  min-height: 18px;
}

.new-messages {
  padding: 6px 14px;
  font-size: 13px;
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-deep);
}

/* ---------- 输入区 ---------- */

.composer {
  position: sticky;
  bottom: 0;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.composer__row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.composer__input {
  flex: 1;
  min-height: 40px;
  max-height: 140px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  font: inherit;
  resize: none;
  background: #fff;
  color: var(--text);
  overflow-y: auto;
}

.composer__input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.composer__hints {
  display: flex;
  gap: 10px;
  align-items: baseline;
  min-height: 16px;
}

.composer__hint {
  font-size: 12px;
  color: var(--text-soft);
}

/* 临时提示（粘贴多图、未登录等）用强调色，和上传进度区分开 */
.composer__hint--notify {
  color: var(--accent-deep);
}

/* ---------- emoji 面板 ---------- */

.emoji-panel-host {
  position: relative;
}

.emoji-panel {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  right: 0;
  max-height: 320px;
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 12px 32px rgba(150, 110, 130, 0.22);
  padding: 10px;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.emoji-panel__quick {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding-bottom: 8px;
  border-bottom: 1px dashed var(--line);
}

.emoji-panel__tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.emoji-panel__tab {
  padding: 4px 12px;
  font-size: 13px;
  background: var(--bg-soft);
}

.emoji-panel__tab.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.emoji-panel__grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  gap: 2px;
}

.emoji-panel__item,
.emoji-panel__quick-item {
  border: none;
  background: transparent;
  font-size: 22px;
  padding: 4px 0;
  border-radius: 10px;
}

.emoji-panel__item:hover,
.emoji-panel__quick-item:hover {
  background: var(--accent-soft);
}

.emoji-panel__footer {
  display: flex;
  justify-content: flex-end;
}

.emoji-panel__close {
  padding: 4px 14px;
  font-size: 13px;
}

/* ---------- 欢迎 / 昵称阶段 ---------- */

.welcome {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(253, 246, 249, 0.96);
  z-index: 40;
  /* 外壳已经禁止页面滚动，短屏（横屏手机/小窗）时这里必须能自己滚 */
  overflow-y: auto;
}

.welcome__card {
  width: 100%;
  max-width: 420px;
  background: var(--card);
  border-radius: 24px;
  border: 1px solid var(--line);
  box-shadow: 0 18px 48px rgba(170, 120, 145, 0.2);
  padding: 24px 22px;
  text-align: center;
}

.welcome__eyebrow {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.08em;
  color: var(--accent-deep);
}

.welcome__title {
  margin: 6px 0 8px;
  font-size: 21px;
}

.welcome__desc {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--text-soft);
}

.welcome__avatars {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}

.avatar-options {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.avatar-option {
  padding: 4px;
  border-radius: 50%;
  border: 3px solid transparent;
  background: transparent;
  line-height: 0;
}

.avatar-option__img {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 4px 12px rgba(160, 120, 140, 0.2);
}

.avatar-option.is-active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.welcome__form {
  display: flex;
  gap: 8px;
}

.welcome__input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  font: inherit;
}

.welcome__input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.welcome__error {
  min-height: 18px;
  margin: 8px 0 0;
  color: var(--danger);
  font-size: 13px;
}

.welcome__tip {
  margin: 6px 0 0;
  font-size: 12px;
  color: var(--text-soft);
}

/* ---------- 大图查看 ---------- */

.viewer {
  position: fixed;
  inset: 0;
  background: rgba(30, 22, 28, 0.86);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 20px;
  z-index: 60;
}

.viewer__img {
  max-width: 92vw;
  max-height: 78vh;
  border-radius: 12px;
  background: #fff;
}

.viewer__close {
  padding: 8px 18px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font: inherit;
  cursor: pointer;
}

/* ---------- 窄屏微调 ---------- */

@media (max-width: 480px) {
  .msg__main {
    max-width: 82%;
  }

  .ghost-button {
    padding: 5px 9px;
    font-size: 12px;
  }

  .welcome__card {
    padding: 20px 16px;
  }
}
