:root {
  --brand: #3b6fd9;
  --brand-deep: #2f5eb8;
  --brand-light: #eef3fc;
  --bg-page: #e9edf3;
  --bg-chat: #f4f6f9;
  --surface: #ffffff;
  --text: #1a2332;
  --text-secondary: #6b7785;
  --border: #e3e8ef;
  --shadow-sm: 0 1px 3px rgba(20, 35, 60, 0.06);
  --shadow-md: 0 8px 32px rgba(20, 35, 60, 0.1);
  --radius: 10px;
}

html, body {
  height: 100%;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'PingFang SC', 'Hiragino Sans GB', 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg-page);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

*, *::before, *::after {
  box-sizing: border-box;
}

.chat-app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  background: var(--surface);
  box-shadow: var(--shadow-md);
  border: 1px solid rgba(255, 255, 255, 0.8);
}

/* ---- 顶栏 ---- */
.chat-header {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  min-height: 48px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-header-brand {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: linear-gradient(145deg, #4a7fe8 0%, #3b6fd9 100%);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: none;
}

.chat-header-info h1 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.25;
}

.chat-header-info p {
  display: none;
}

.chat-status {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  color: var(--text-secondary);
  padding: 2px 7px;
  background: transparent;
  border-radius: 20px;
}

.chat-status::before {
  content: '';
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #c5cdd6;
}

.chat-status.connected {
  color: #047857;
  background: transparent;
}

.chat-status.connected::before {
  background: #10b981;
  box-shadow: none;
}

/* ---- 消息区 ---- */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px 14px;
  background: var(--bg-chat);
  -webkit-overflow-scrolling: touch;
}

.chat-empty,
.session-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  font-size: 13px;
}

/* ---- 消息行 ---- */
.msg-row {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  max-width: 80%;
  animation: msg-in 0.2s ease;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg-row.continued {
  margin-top: -6px;
}

.msg-row.continued .msg-avatar {
  visibility: hidden;
}

.msg-row.continued .msg-time {
  display: none;
}

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

.msg-row.other {
  flex-direction: row;
  align-self: flex-start;
}

.msg-avatar {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  margin-top: 1px;
  box-shadow: var(--shadow-sm);
}

.msg-row.self .msg-avatar {
  background: linear-gradient(145deg, #eef3fc, #e3ecfa);
  color: var(--brand);
  border: 1px solid rgba(59, 111, 217, 0.12);
}

.msg-row.other .msg-avatar {
  background: linear-gradient(145deg, #fff, #f3f5f8);
  color: #5a6573;
  border: 1px solid var(--border);
}

.msg-body {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}

.msg-row.self .msg-body {
  align-items: flex-end;
}

.msg-row.other .msg-body {
  align-items: flex-start;
}

.msg-bubble {
  padding: 9px 12px;
  font-size: 14px;
  line-height: 1.55;
  border-radius: 10px;
  word-break: break-word;
  white-space: pre-wrap;
  max-width: 100%;
}

.msg-row.self .msg-bubble {
  background: linear-gradient(145deg, #4a7fe8 0%, #3b6fd9 100%);
  color: #fff;
  border-bottom-right-radius: 3px;
  box-shadow: 0 2px 6px rgba(59, 111, 217, 0.22);
}

.msg-row.other .msg-bubble {
  background: #fff;
  color: #2d3748;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  border-bottom-left-radius: 3px;
}

.msg-bubble.emoji-only {
  font-size: 19px;
  line-height: 1.35;
  padding: 7px 10px;
  letter-spacing: 0;
}

.msg-time {
  font-size: 10px;
  color: #a0aab4;
  padding: 0 3px;
  letter-spacing: 0.02em;
}

/* ---- 后台顶栏返回 ---- */
.chat-app .back-btn {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1;
  color: var(--brand);
  font-size: 14px;
  padding: 6px 8px;
  background: #f4f6f8;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s;
}

.chat-app .back-btn:hover {
  background: var(--brand-light);
}

.chat-header-info {
  flex: 1;
  min-width: 0;
}

.chat-app .chat-header.has-back {
  padding-left: 68px;
  min-height: 48px;
}

.chat-app .chat-header.has-back .chat-header-brand {
  display: none;
}

.chat-app .chat-header-info h1#room-token {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 520px) {
  .chat-app {
    max-width: 100%;
    box-shadow: none;
    border: none;
  }
}

@media (min-width: 521px) {
  body.chat-standalone {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: linear-gradient(160deg, #e4e9f0 0%, #eef1f6 100%);
  }

  .chat-app.chat-standalone {
    height: calc(100vh - 40px);
    height: calc(100dvh - 40px);
    border-radius: 14px;
    overflow: hidden;
  }
}

.admin-main .chat-app {
  max-width: none;
  margin: 0;
  box-shadow: none;
  height: 100%;
  border-radius: 0;
  border: none;
}
