:root {
  --bg1: #667eea;
  --bg2: #764ba2;
  --panel: #ffffff;
  --muted: #666;
  --muted2: #999;
  --border: #e8e8e8;
  --bubble: #ffffff;
  --bubble-own: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 20px;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: linear-gradient(135deg, var(--bg1) 0%, var(--bg2) 100%);
}

.chat-container {
  width: 100%;
  max-width: 980px;
  height: 90vh;
  background: var(--panel);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.connection-status {
  padding: 10px 20px;
  color: white;
  background: #ff6b6b;
  text-align: center;
  font-size: 14px;
}
.connection-status.connected {
  background: #51cf66;
}
.connection-status.reconnecting {
  background: #ffa94d;
}

.chat-header {
  background: linear-gradient(135deg, var(--bg1) 0%, var(--bg2) 100%);
  color: white;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.chat-header h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 700;
}

.badges {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.badge {
  background: rgba(255, 255, 255, 0.18);
  border: 1px solid rgba(255, 255, 255, 0.25);
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 12px;
  white-space: nowrap;
}

.badge-btn {
  appearance: none;
  cursor: pointer;
}
.badge-btn:hover {
  background: rgba(255, 255, 255, 0.26);
}
.badge-btn:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.7);
  outline-offset: 2px;
}

.chat-body {
  flex: 1;
  min-height: 0;
  display: flex;
  background: #f5f5f5;
  position: relative;
}

.overlay {
  display: none;
}

.user-panel {
  width: 220px;
  background: #ffffff;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.user-panel-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  color: #333;
  font-weight: 700;
  font-size: 14px;
}

.user-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px 8px;
  list-style: none;
  margin: 0;
}

.user-item {
  padding: 10px 10px;
  margin: 6px 6px;
  border-radius: 12px;
  background: #fafafa;
  border: 1px solid #f0f0f0;
  color: #333;
  font-size: 13px;
}
.user-item.self {
  border-color: rgba(102, 126, 234, 0.6);
  background: rgba(102, 126, 234, 0.08);
}

.messages {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  padding: 18px 16px;
}

.message {
  margin-bottom: 14px;
  animation: fadeIn 0.15s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.message.system {
  text-align: center;
  color: var(--muted2);
  font-size: 13px;
}

.message.user {
  display: flex;
  flex-direction: column;
  max-width: 100%;
}
.message.user.own { align-items: flex-end; }
.message.user.other { align-items: flex-start; }

.message-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 6px 6px 6px;
}
.message-username {
  font-weight: 700;
  font-size: 13px;
  color: #667eea;
}
.message-time {
  font-size: 12px;
  color: var(--muted2);
}
.message-tag {
  font-size: 12px;
  color: var(--muted2);
}

.message-content {
  background: var(--bubble);
  padding: 10px 14px;
  border-radius: 16px;
  max-width: 78%;
  word-wrap: break-word;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
}
.message.user.own .message-content {
  background: var(--bubble-own);
  color: white;
}

.msg-image {
  max-width: 320px;
  max-height: 240px;
  display: block;
  border-radius: 12px;
}

.msg-video {
  max-width: 360px;
  max-height: 260px;
  display: block;
  border-radius: 12px;
  background: #000;
}

.msg-audio {
  width: 320px;
}

.media-placeholder {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 220px;
  max-width: 78%;
}

.media-label {
  font-size: 13px;
  color: var(--muted);
}

.media-meta {
  font-size: 12px;
  color: var(--muted2);
}

.media-progress {
  width: 100%;
  height: 10px;
}

.message-actions {
  display: flex;
  gap: 8px;
  margin: 6px 6px 0 6px;
}
.action-btn {
  border: 1px solid rgba(0,0,0,0.08);
  background: rgba(255,255,255,0.8);
  color: #333;
  padding: 6px 10px;
  border-radius: 10px;
  font-size: 12px;
  cursor: pointer;
}
.action-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.message.user.own .action-btn {
  background: rgba(255,255,255,0.92);
  color: #4c51bf;
  border-color: rgba(102,126,234,0.35);
}

.edit-input {
  width: 100%;
  min-width: 220px;
  max-width: 78%;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: 14px;
  border: 2px solid rgba(102, 126, 234, 0.45);
  font-size: 14px;
  resize: vertical;
}

.message.user.own .edit-input {
  border-color: rgba(255,255,255,0.75);
}

.composer {
  background: #ffffff;
  border-top: 1px solid var(--border);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.emoji-bar {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.emoji-btn {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  border: 1px solid rgba(0,0,0,0.08);
  background: #fff;
  cursor: pointer;
  font-size: 16px;
  line-height: 32px;
}

.tool-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.tool-btn {
  padding: 8px 12px;
  border-radius: 12px;
  border: 1px solid rgba(0,0,0,0.08);
  background: #fff;
  cursor: pointer;
  font-size: 13px;
}
.tool-btn.primary {
  background: linear-gradient(135deg, var(--bg1) 0%, var(--bg2) 100%);
  color: #fff;
  border: none;
}
.tool-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.input-row {
  display: flex;
  gap: 10px;
}
.message-input {
  flex: 1;
  padding: 12px 12px;
  border-radius: 12px;
  border: 2px solid #e0e0e0;
  font-size: 15px;
}
.message-input:focus {
  outline: none;
  border-color: #667eea;
}

.hint {
  font-size: 12px;
  color: var(--muted2);
}

/* Mobile responsive */
@media (max-width: 760px) {
  body {
    padding: 0;
  }

  .chat-container {
    height: 100vh;
    border-radius: 0;
    max-width: 100%;
  }

  .chat-header {
    padding: 14px 14px;
  }

  .badge-btn {
    display: inline-flex;
    align-items: center;
  }

  .overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.25);
    z-index: 15;
  }

  .chat-container.show-users .overlay {
    display: block;
  }

  .user-panel {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(84vw, 320px);
    z-index: 20;
    border-right: 1px solid var(--border);
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.18);
    transform: translateX(-110%);
    transition: transform 0.18s ease;
  }

  .chat-container.show-users .user-panel {
    transform: translateX(0);
  }

  .messages {
    padding: 14px 12px;
  }

  .message-content {
    max-width: 92%;
  }

  .msg-image,
  .msg-video,
  .msg-audio {
    width: 100%;
    max-width: 100%;
  }

  .msg-image {
    max-height: 360px;
  }

  .composer {
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }

  .emoji-bar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }
  .emoji-bar::-webkit-scrollbar {
    display: none;
  }
}
