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

:root {
  --bg: #f0f4f8;
  --surface: #ffffff;
  --primary: #4f6ef7;
  --primary-hover: #3d5ce0;
  --primary-light: #eef1fe;
  --text: #1a1f36;
  --text-secondary: #697386;
  --border: #e3e8ef;
  --success: #3ecf8e;
  --warn: #f5a623;
  --danger: #e25950;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --sent-bg: #4f6ef7;
  --received-bg: #f0f4f8;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

.hidden { display: none !important; }

/* Auth Screen */
#auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.auth-card {
  background: var(--surface);
  border-radius: 20px;
  padding: 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow-lg);
}

.logo {
  text-align: center;
  margin-bottom: 32px;
}

.logo-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 8px;
}

.logo h1 {
  font-size: 28px;
  font-weight: 700;
  color: var(--text);
}

.subtitle {
  color: var(--text-secondary);
  font-size: 14px;
  margin-top: 4px;
}

.tabs {
  display: flex;
  background: var(--bg);
  border-radius: var(--radius);
  padding: 4px;
  margin-bottom: 24px;
}

.tab {
  flex: 1;
  padding: 10px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  color: var(--text-secondary);
  transition: all 0.2s;
}

.tab.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow);
}

.field {
  margin-bottom: 16px;
}

.field label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.field input {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  transition: border-color 0.2s;
  outline: none;
}

.field input:focus {
  border-color: var(--primary);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-primary {
  background: var(--primary);
  color: white;
  width: 100%;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  padding: 8px 12px;
  font-size: 14px;
}

.btn-ghost:hover {
  background: var(--bg);
  color: var(--text);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
}

.btn-success {
  background: var(--success);
  color: white;
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}

.error {
  color: var(--danger);
  font-size: 14px;
  text-align: center;
  margin-top: 12px;
}

.info {
  color: var(--success);
  font-size: 14px;
  text-align: center;
  margin-top: 12px;
}

/* Main Layout */
#main-screen {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-small { font-size: 24px; }

.user-name {
  font-weight: 600;
  font-size: 16px;
}

.header-right {
  display: flex;
  gap: 8px;
}

.main-layout {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 280px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
}

.sidebar-section {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-section h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.badge {
  background: var(--primary-light);
  color: var(--primary);
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 10px;
  font-weight: 600;
}

.badge-warn {
  background: #fff3e0;
  color: var(--warn);
}

.badge-unread {
  background: #ffebee;
  color: #e53935;
}

.unread-badge {
  background: #e53935;
  color: #fff;
  font-size: 10px;
  min-width: 18px;
  height: 18px;
  line-height: 18px;
  text-align: center;
  padding: 0 5px;
  border-radius: 9px;
  font-weight: 700;
  flex-shrink: 0;
}

.friend-preview {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}

.friend-item.has-unread .name {
  font-weight: 700;
}

.friend-list, .request-list, .search-results {
  list-style: none;
}

.friend-item, .request-item, .search-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
}

.friend-item:hover, .friend-item.active {
  background: var(--primary-light);
}

.friend-item.active {
  border-left: 3px solid var(--primary);
}

.avatar-wrap {
  position: relative;
  flex-shrink: 0;
}

.presence-dot {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 2px solid var(--surface);
}

.presence-dot.online {
  background: #22c55e;
}

.presence-dot.offline {
  background: #9ca3af;
}

.presence-text {
  font-size: 11px;
  font-weight: 500;
}

.presence-text.online {
  color: #22c55e;
}

.presence-text.offline {
  color: var(--text-secondary);
}

.presence-label.online {
  color: #22c55e;
  font-weight: 600;
}

.presence-label.offline {
  color: var(--text-secondary);
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), #764ba2);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  flex-shrink: 0;
}

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

.friend-info .name {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.friend-info .username {
  font-size: 12px;
  color: var(--text-secondary);
}

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

.empty-hint {
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
  padding: 16px 8px;
}

/* Chat Area */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}

.chat-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.placeholder-content {
  text-align: center;
  color: var(--text-secondary);
}

.placeholder-icon {
  font-size: 64px;
  display: block;
  margin-bottom: 16px;
}

.chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 20px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.chat-header-info {
  min-width: 0;
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 0 8px;
}

.chat-friend-name {
  font-weight: 600;
  font-size: 16px;
}

.chat-friend-username {
  font-size: 13px;
  color: var(--text-secondary);
}

.btn-danger-text {
  color: #c0392b !important;
  flex-shrink: 0;
}

.btn-danger-text:hover {
  background: rgba(192, 57, 43, 0.08);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message {
  max-width: 70%;
  display: flex;
  flex-direction: column;
}

.message.sent {
  align-self: flex-end;
  align-items: flex-end;
}

.message.received {
  align-self: flex-start;
  align-items: flex-start;
}

.message-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.4;
  word-break: break-word;
}

.message.sent .message-bubble {
  background: var(--sent-bg);
  color: white;
  border-bottom-right-radius: 4px;
}

.message.received .message-bubble {
  background: var(--received-bg);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.message-time {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 4px;
  padding: 0 4px;
}

.message-read {
  margin-left: 4px;
  opacity: 0.85;
}

.message-form {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 14px 20px;
  display: flex;
  gap: 10px;
  flex-shrink: 0;
  position: relative;
}

.message-form input[type="text"] {
  flex: 1;
  padding: 12px 16px;
  border: 1.5px solid var(--border);
  border-radius: 24px;
  font-size: 15px;
  outline: none;
}

.message-form input[type="text"]:focus {
  border-color: var(--primary);
}

.message-bubble.emoji-only {
  font-size: 36px;
  line-height: 1.2;
  padding: 6px 10px;
  background: transparent !important;
}

.message.sent .message-bubble.emoji-only,
.message.received .message-bubble.emoji-only {
  background: transparent !important;
}

.emoji-picker {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  width: 320px;
  max-height: 220px;
  overflow-y: auto;
  z-index: 50;
}

.emoji-picker button {
  border: none;
  background: transparent;
  font-size: 24px;
  line-height: 1;
  padding: 6px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

.emoji-picker button:hover {
  background: var(--primary-light);
}

.btn-icon {
  font-size: 20px;
  padding: 8px 10px;
  flex-shrink: 0;
}

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

.message-image:hover {
  opacity: 0.9;
}

.message-bubble.image-bubble {
  padding: 4px;
  background: transparent !important;
}

.message.sent .message-bubble.image-bubble {
  background: transparent !important;
}

.image-loading {
  width: 120px;
  height: 80px;
  background: var(--border);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  color: var(--text-secondary);
}

/* Image lightbox */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}

.btn-voice-active {
  background: #ffe0e0 !important;
  color: #c0392b !important;
  animation: pulse 1s infinite;
}

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

.voice-bubble {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 88px;
  cursor: pointer;
  user-select: none;
}

.voice-bubble .voice-icon {
  font-size: 16px;
  line-height: 1;
}

.voice-bubble .voice-duration {
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.voice-bubble.playing .voice-icon {
  animation: pulse 0.8s infinite;
}

.voice-recording-hint {
  position: absolute;
  left: 50%;
  bottom: 72px;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.78);
  color: #fff;
  padding: 10px 16px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 20;
  pointer-events: none;
  white-space: nowrap;
}

.message-sender {
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 2px;
  padding: 0 4px;
}

.member-check {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.member-check:hover {
  background: var(--bg);
}

.member-check input {
  width: 18px;
  height: 18px;
}

.group-avatar {
  background: linear-gradient(135deg, #3ecf8e, #4f6ef7) !important;
}

.mention-picker {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 12px;
  right: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  max-height: 180px;
  overflow-y: auto;
  z-index: 60;
}

.mention-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border: none;
  background: transparent;
  cursor: pointer;
  text-align: left;
}

.mention-item:hover {
  background: var(--primary-light);
}

.mention-highlight {
  color: var(--primary);
  font-weight: 600;
  background: var(--primary-light);
  padding: 0 2px;
  border-radius: 4px;
}

#mention-badge {
  margin-left: 8px;
  cursor: pointer;
}

.btn-send {
  width: auto;
  border-radius: 24px;
  padding: 12px 24px;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
}

.modal-content {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  width: 90%;
  max-width: 440px;
  box-shadow: var(--shadow-lg);
  z-index: 1;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 0;
}

.modal-header h2 {
  font-size: 18px;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--text-secondary);
  line-height: 1;
}

.modal-body {
  padding: 16px 20px 20px;
}

.search-input {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  outline: none;
  margin-bottom: 12px;
}

.search-input:focus {
  border-color: var(--primary);
}

.search-item {
  cursor: default;
}

.search-item .btn {
  width: auto;
  flex-shrink: 0;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: white;
  padding: 12px 24px;
  border-radius: 24px;
  font-size: 14px;
  z-index: 200;
  box-shadow: var(--shadow-lg);
  transition: opacity 0.3s;
}

/* Responsive */
@media (max-width: 640px) {
  .sidebar {
    width: 100%;
    position: absolute;
    z-index: 10;
    height: calc(100% - 57px);
  }

  .chat-area {
    width: 100%;
  }

  .main-layout.chat-open .sidebar {
    display: none;
  }

  .auth-card {
    padding: 28px 24px;
  }
}
