/* ═══════════════════════════════════════════════════════
   Tenord Messenger — Design System & Styles
   ═══════════════════════════════════════════════════════ */

/* ─── CSS Variables (Light Theme) ──────────────────── */
:root {
  /* Primary */
  --primary: #6c5ce7;
  --primary-light: #a29bfe;
  --primary-dark: #5341d6;
  --primary-bg: rgba(108, 92, 231, 0.08);
  --primary-bg-hover: rgba(108, 92, 231, 0.14);

  /* Background */
  --bg-main: #f0f2f5;
  --bg-sidebar: #ffffff;
  --bg-chat: #e8ecf1;
  --bg-input: #ffffff;
  --bg-hover: rgba(0, 0, 0, 0.04);
  --bg-active: rgba(108, 92, 231, 0.1);
  --bg-modal: #ffffff;
  --bg-overlay: rgba(0, 0, 0, 0.45);
  --bg-tooltip: #333;

  /* Messages */
  --msg-out: #e3d8ff;
  --msg-out-hover: #d9ccff;
  --msg-in: #ffffff;
  --msg-in-hover: #f7f7f7;

  /* Text */
  --text-primary: #1a1a2e;
  --text-secondary: #65676b;
  --text-tertiary: #8e8e93;
  --text-inverse: #ffffff;
  --text-link: #6c5ce7;
  --text-danger: #e74c3c;

  /* Borders */
  --border: rgba(0, 0, 0, 0.08);
  --border-light: rgba(0, 0, 0, 0.04);
  --border-input: rgba(0, 0, 0, 0.15);
  --border-focus: var(--primary);

  /* Shadows */
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.12);
  --shadow-xl: 0 20px 60px rgba(0, 0, 0, 0.15);

  /* Sizing */
  --sidebar-width: clamp(280px, 25vw, 420px);
  --header-height: 60px;
  --input-height: 56px;
  --avatar-sm: 36px;
  --avatar-md: 48px;
  --avatar-lg: 80px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-full: 9999px;

  /* Transitions */
  --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-normal: 250ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 400ms cubic-bezier(0.4, 0, 0.2, 1);

  /* Z-index */
  --z-dropdown: 100;
  --z-modal: 200;
  --z-context: 300;
  --z-toast: 400;
}

/* ─── Dark Theme ───────────────────────────────────── */
[data-theme="dark"] {
  --primary: #8774e1;
  --primary-light: #9e8df0;
  --primary-dark: #6e58d1;
  --primary-bg: rgba(135, 116, 225, 0.1);
  --primary-bg-hover: rgba(135, 116, 225, 0.18);

  --bg-main: #0e1621;
  --bg-sidebar: #17212b;
  --bg-chat: #0e1621;
  --bg-input: #17212b;
  --bg-hover: rgba(255, 255, 255, 0.04);
  --bg-active: rgba(135, 116, 225, 0.12);
  --bg-modal: #17212b;
  --bg-overlay: rgba(0, 0, 0, 0.6);

  --msg-out: #2b5278;
  --msg-out-hover: #2f5a83;
  --msg-in: #18222d;
  --msg-in-hover: #1c2734;

  --text-primary: #f5f5f5;
  --text-secondary: #aaaaaa;
  --text-tertiary: #6d7883;
  --text-inverse: #ffffff;
  --text-link: #8774e1;

  --border: #232e3a;
  --border-light: #1f2936;
  --border-input: rgba(255, 255, 255, 0.12);

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.4);
  --shadow-xl: 0 20px 60px rgba(0, 0, 0, 0.5);
}

/* ─── Reset & Base ─────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

@media (max-width: 1024px) {
  html { font-size: 15px; }
}

@media (max-width: 768px) {
  html { font-size: 14px; }
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg-main);
  color: var(--text-primary);
  line-height: 1.5;
  overflow: hidden;
  height: 100%;
  height: 100dvh;
  width: 100vw;
  transition: background var(--transition-normal), color var(--transition-normal);
  position: fixed; /* Prevents bounce and scroll on mobile */
}

input, textarea, button {
  font-family: inherit;
  font-size: inherit;
}

a {
  color: var(--text-link);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.hidden {
  display: none !important;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--text-tertiary);
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
}

/* ═══════════════════════════════════════════════════════
   AUTH SCREEN
   ═══════════════════════════════════════════════════════ */

.auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  width: 100vw;
  background: linear-gradient(135deg, #0e0e10 0%, #1a1a2e 30%, #2d1b69 60%, #6c5ce7 100%);
  background-size: 400% 400%;
  animation: authBgShift 12s ease infinite;
  padding: 20px;
}

@keyframes authBgShift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

.auth-container {
  width: 100%;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.06);
  backdrop-filter: blur(40px);
  -webkit-backdrop-filter: blur(40px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 24px;
  padding: 48px 36px;
  box-shadow: 0 32px 64px rgba(0, 0, 0, 0.3);
  animation: authSlideUp 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes authSlideUp {
  from { opacity: 0; transform: translateY(30px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

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

.auth-logo-icon {
  width: 72px;
  height: 72px;
  margin: 0 auto 16px;
  animation: logoPulse 3s ease-in-out infinite;
}

@keyframes logoPulse {
  0%, 100% { transform: scale(1); filter: drop-shadow(0 0 8px rgba(108, 92, 231, 0.3)); }
  50% { transform: scale(1.05); filter: drop-shadow(0 0 20px rgba(108, 92, 231, 0.5)); }
}

.auth-logo-icon svg {
  width: 100%;
  height: 100%;
}

.auth-title {
  font-size: 2rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.5px;
}

.auth-subtitle {
  color: rgba(255, 255, 255, 0.55);
  font-size: 0.9rem;
  margin-top: 6px;
}

/* ─── Auth Forms ───────────────────────────────────── */
.auth-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.form-group {
  position: relative;
}

.form-input {
  width: 100%;
  padding: 24px 16px 8px;
  background: rgba(255, 255, 255, 0.07);
  border: 1.5px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius-md);
  color: #fff;
  font-size: 0.95rem;
  outline: none;
  transition: all var(--transition-fast);
}

.form-input:focus {
  border-color: var(--primary-light);
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.15);
}

.form-input::placeholder {
  color: transparent;
}

.form-label {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: rgba(255, 255, 255, 0.45);
  font-size: 0.95rem;
  pointer-events: none;
  transition: all var(--transition-fast);
}

.form-input:focus ~ .form-label,
.form-input:not(:placeholder-shown) ~ .form-label {
  top: 8px;
  transform: translateY(0);
  font-size: 0.75rem;
  color: var(--primary-light);
}

.form-textarea {
  resize: none;
  min-height: 80px;
  padding-top: 24px;
}

.form-textarea ~ .form-label {
  top: 16px;
  transform: none;
}

.form-textarea:focus ~ .form-label,
.form-textarea:not(:placeholder-shown) ~ .form-label {
  top: 6px;
  font-size: 0.7rem;
}

.form-error {
  color: #ff6b6b;
  font-size: 0.82rem;
  min-height: 20px;
  animation: shakeError 0.4s ease;
}

@keyframes shakeError {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* In messenger context, form inputs use messenger theme colors */
.messenger .form-input,
.modal .form-input {
  background: var(--bg-input);
  border-color: var(--border-input);
  color: var(--text-primary);
}

.messenger .form-input:focus,
.modal .form-input:focus {
  border-color: var(--primary);
  background: var(--bg-input);
  box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.1);
}

.messenger .form-label,
.modal .form-label {
  color: var(--text-tertiary);
}

.messenger .form-input:focus ~ .form-label,
.messenger .form-input:not(:placeholder-shown) ~ .form-label,
.modal .form-input:focus ~ .form-label,
.modal .form-input:not(:placeholder-shown) ~ .form-label {
  color: var(--primary);
}

/* ─── Buttons ──────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 24px;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
  position: relative;
  overflow: hidden;
}

.btn-primary {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff;
  box-shadow: 0 4px 15px rgba(108, 92, 231, 0.3);
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(108, 92, 231, 0.4);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.btn.loading .btn-text { opacity: 0; }
.btn.loading .btn-loader { display: block; }

.btn-loader {
  display: none;
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  position: absolute;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

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

.icon-btn svg {
  width: 20px;
  height: 20px;
}

.auth-switch {
  text-align: center;
  color: rgba(255, 255, 255, 0.5);
  font-size: 0.85rem;
}

.auth-switch a {
  color: var(--primary-light);
  font-weight: 500;
}

/* ═══════════════════════════════════════════════════════
   MESSENGER LAYOUT
   ═══════════════════════════════════════════════════════ */

.messenger {
  display: flex;
  height: 100%;
  width: 100%;
  animation: fadeIn 0.4s ease;
  overflow: hidden;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ─── Sidebar ──────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100%;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: relative;
  transition: background var(--transition-normal);
  z-index: 10;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px 8px 12px;
  height: var(--header-height);
  border-bottom: 1px solid var(--border-light);
}

.sidebar-search {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 12px;
  width: 18px;
  height: 18px;
  color: var(--text-tertiary);
  pointer-events: none;
}

.search-input {
  width: 100%;
  padding: 10px 12px 10px 40px;
  background: var(--bg-hover);
  border: 1.5px solid transparent;
  border-radius: var(--radius-full);
  color: var(--text-primary);
  font-size: 0.88rem;
  outline: none;
  transition: all var(--transition-fast);
}

.search-input:focus {
  background: var(--bg-input);
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-bg);
}

.search-input::placeholder {
  color: var(--text-tertiary);
}

/* ─── Chat List ────────────────────────────────────── */
.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  position: relative;
}

.chat-item:hover {
  background: var(--bg-hover);
}

.chat-item.active {
  background: var(--bg-active);
}

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

.avatar {
  width: var(--avatar-md);
  height: var(--avatar-md);
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 1.1rem;
  color: #fff;
  text-transform: uppercase;
  flex-shrink: 0;
  user-select: none;
}

.avatar-sm {
  width: var(--avatar-sm);
  height: var(--avatar-sm);
  font-size: 0.8rem;
}

.avatar-lg {
  width: var(--avatar-lg);
  height: var(--avatar-lg);
  font-size: 2rem;
}

.online-indicator {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 12px;
  height: 12px;
  background: #2ecc71;
  border-radius: 50%;
  border: 2.5px solid var(--bg-sidebar);
}

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

.chat-item-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.chat-item-name {
  font-weight: 600;
  font-size: 0.93rem;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-time {
  font-size: 0.72rem;
  color: var(--text-tertiary);
  flex-shrink: 0;
}

.chat-item-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
}

.chat-item-preview {
  font-size: 0.84rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.chat-item-preview .sender-name {
  color: var(--primary);
  font-weight: 500;
}

.unread-badge {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  background: var(--primary);
  color: #fff;
  border-radius: var(--radius-full);
  font-size: 0.72rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  animation: badgePop 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

@keyframes badgePop {
  from { transform: scale(0); }
  to { transform: scale(1); }
}

/* ─── Sidebar Menu ─────────────────────────────────── */
.sidebar-menu {
  position: absolute;
  top: var(--header-height);
  left: 12px;
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 6px;
  min-width: 240px;
  z-index: var(--z-dropdown);
  animation: menuSlide 0.2s ease;
}

.profile-trigger-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px;
  border-radius: 50%;
  transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.profile-trigger-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 0 0 3px var(--bg-hover);
}

.sidebar-menu-profile {
  padding: 12px 14px;
}

.sidebar-profile-info {
  font-size: 0.9rem;
  line-height: 1.3;
}


@keyframes menuSlide {
  from { opacity: 0; transform: translateY(-8px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 14px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-primary);
  font-size: 0.88rem;
  cursor: pointer;
  transition: background var(--transition-fast);
}

.menu-item:hover {
  background: var(--bg-hover);
}

.menu-item svg {
  width: 20px;
  height: 20px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.menu-item-danger {
  color: var(--text-danger);
}

.menu-item-danger svg {
  color: var(--text-danger);
}

.menu-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 8px;
}

/* ═══════════════════════════════════════════════════════
   CHAT AREA
   ═══════════════════════════════════════════════════════ */

.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--bg-chat);
  position: relative;
  transition: background var(--transition-normal);
}

/* Chat background pattern */
.chat-area::before {
  content: '';
  position: absolute;
  inset: 0;
  opacity: 0.05;
  background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M10 10h10v10H10zM30 30h10v10H30zM50 50h10v10H50zM70 70h10v10H70zM90 90h10v10H90z' fill='%23000000' fill-opacity='1' fill-rule='evenodd'/%3E%3C/svg%3E");
  background-size: 80px 80px;
  pointer-events: none;
  z-index: 0;
}

[data-theme="dark"] .chat-area::before {
  opacity: 0.03;
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='1'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}


/* ─── Empty State ──────────────────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  text-align: center;
  z-index: 1;
  animation: fadeIn 0.5s ease;
}

.empty-state-icon {
  width: 100px;
  height: 100px;
  margin-bottom: 24px;
  opacity: 0.6;
  animation: floatIcon 4s ease-in-out infinite;
}

@keyframes floatIcon {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

.empty-state-icon svg {
  width: 100%;
  height: 100%;
}

.empty-state-title {
  font-size: 1.3rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.empty-state-text {
  color: var(--text-tertiary);
  font-size: 0.9rem;
}

/* ─── Chat Header ──────────────────────────────────── */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  height: var(--header-height);
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  z-index: 2;
  transition: background var(--transition-normal);
  flex-shrink: 0;
}

.chat-header-avatar {
  cursor: pointer;
  transition: transform 0.2s;
}

.chat-header-avatar:hover {
  transform: scale(1.05);
}

.back-btn {
  display: none;
}

.chat-header-avatar .avatar {
  width: 42px;
  height: 42px;
  font-size: 1rem;
}

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

.chat-header-name {
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-header-status {
  font-size: 0.78rem;
  color: var(--text-tertiary);
}

.chat-header-status.online {
  color: #2ecc71;
}

.chat-header-status.typing {
  color: var(--primary);
  animation: typingPulse 1.5s ease infinite;
}

@keyframes typingPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.chat-header-actions {
  display: flex;
  gap: 4px;
}

/* ─── Messages ─────────────────────────────────────── */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px 0;
  z-index: 1;
  scroll-behavior: smooth;
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 4px; /* Default gap */
  padding: 0 60px;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

/* Date separator */
.date-separator {
  display: flex;
  justify-content: center;
  padding: 12px 0;
}

.date-separator-label {
  background: rgba(0, 0, 0, 0.12);
  color: #fff;
  padding: 4px 12px;
  border-radius: var(--radius-full);
  font-size: 0.76rem;
  font-weight: 500;
  backdrop-filter: blur(8px);
}

[data-theme="dark"] .date-separator-label {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-secondary);
}

/* Messages */
.message {
  display: flex;
  flex-direction: column;
  max-width: 65%;
  animation: msgAppear 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes msgAppear {
  from { opacity: 0; transform: translateY(12px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.message.outgoing {
  align-self: flex-end;
  align-items: flex-end;
}
 
.message.incoming {
  align-self: flex-start;
  align-items: flex-start;
}

/* Grouping classes */
.message.group-first { margin-bottom: -2px; }
.message.group-middle { margin-top: -2px; margin-bottom: -2px; }
.message.group-last { margin-top: -2px; }

/* Grouping radii logic */
.message.incoming.group-first .message-bubble { border-bottom-left-radius: 4px; }
.message.incoming.group-middle .message-bubble { border-top-left-radius: 4px; border-bottom-left-radius: 4px; }
.message.incoming.group-last .message-bubble { border-top-left-radius: 4px; }

.message.outgoing.group-first .message-bubble { border-bottom-right-radius: 4px; }
.message.outgoing.group-middle .message-bubble { border-top-right-radius: 4px; border-bottom-right-radius: 4px; }
.message.outgoing.group-last .message-bubble { border-top-right-radius: 4px; }

.message-bubble {
  padding: 6px 12px 6px 12px;
  border-radius: 12px;
  position: relative;
  cursor: default;
  word-wrap: break-word;
  overflow-wrap: break-word;
  transition: all var(--transition-fast);
  line-height: 1.5;
  font-size: 0.95rem;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.message.outgoing .message-bubble {
  background: var(--msg-out);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message.incoming .message-bubble {
  background: var(--msg-in);
  border-bottom-left-radius: 4px;
  border: none;
}

[data-theme="dark"] .message.incoming .message-bubble {
  background: var(--msg-in);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  border: none;
}

[data-theme="dark"] .message.outgoing .message-bubble {
  background: var(--msg-out);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}

/* Bubble Tails (Telegram style) */
.message.group-first .message-bubble::before,
.message.group-single .message-bubble::before {
  content: '';
  position: absolute;
  bottom: 0;
  width: 14px;
  height: 14px;
  z-index: -1;
  background: inherit;
}

.message.incoming.group-first .message-bubble::before,
.message.incoming.group-single .message-bubble::before {
  left: -6px;
  background: var(--msg-in);
  mask-image: radial-gradient(circle at 0 0, transparent 14px, black 15px);
  -webkit-mask-image: radial-gradient(circle at 0 0, transparent 14px, black 15px);
  clip-path: none;
}

.message.outgoing.group-first .message-bubble::before,
.message.outgoing.group-single .message-bubble::before {
  right: -6px;
  background: var(--msg-out);
  mask-image: radial-gradient(circle at 100% 0, transparent 14px, black 15px);
  -webkit-mask-image: radial-gradient(circle at 100% 0, transparent 14px, black 15px);
  clip-path: none;
}

.message-bubble:hover {
  filter: brightness(0.97);
}

.message-bubble {
  position: relative;
  display: block; /* For inline stacking */
  max-width: 100%;
}

.message-sender {
  font-size: 0.78rem;
  font-weight: 600;
  margin-bottom: 2px;
}

.message-reply {
  display: flex;
  padding: 6px 10px;
  margin-bottom: 4px;
  border-left: 3px solid var(--primary);
  border-radius: 4px;
  background: rgba(108, 92, 231, 0.08);
  cursor: pointer;
}

.message-reply-content {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.message-reply-name {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--primary);
}

.message-reply-text {
  font-size: 0.78rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.message-text {
  font-size: 0.92rem;
  white-space: pre-wrap;
}

.message-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  margin-top: 2px;
}

.message-time {
  font-size: 0.7rem;
  opacity: 0.7;
  font-weight: 500;
}

.message.outgoing .message-time {
  color: rgba(255,255,255,0.8);
}

.message-edited {
  font-size: 0.65rem;
  opacity: 0.6;
  font-style: italic;
}

.message-status {
  font-size: 0.85rem;
  line-height: 1;
}

.message-status.sent { color: rgba(255,255,255,0.5); }
.message.incoming .message-status.sent { color: var(--text-tertiary); }
.message-status.read { color: #fff; }
.message.incoming .message-status.read { color: var(--primary); }
 
/* ─── Scroll Bottom Button ────────────────────────── */
.scroll-bottom-btn {
  position: absolute;
  bottom: 80px;
  right: 20px;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: var(--bg-sidebar);
  color: var(--text-primary);
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  transition: all var(--transition-normal);
  border: 1px solid var(--border);
}

.scroll-bottom-btn:hover {
  transform: translateY(-2px);
  background: var(--bg-hover);
}

.scroll-bottom-btn svg {
  width: 20px;
  height: 20px;
}

.scroll-bottom-btn .unread-badge {
  position: absolute;
  top: -8px;
  left: -8px;
  background: var(--text-danger);
}

/* ─── Reply Preview ────────────────────────────────── */
.reply-preview {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background: var(--bg-sidebar);
  border-top: 1px solid var(--border);
  border-left: 3px solid var(--primary);
  margin: 0 60px;
  max-width: 900px;
  margin-left: auto;
  margin-right: auto;
  z-index: 2;
  animation: slideUp 0.2s ease;
}

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

.reply-preview-content {
  flex: 1;
  min-width: 0;
}

.reply-preview-name {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--primary);
}

.reply-preview-text {
  display: block;
  font-size: 0.82rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reply-close-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-tertiary);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.reply-close-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.reply-close-btn svg {
  width: 16px;
  height: 16px;
}

/* ─── Message Input ────────────────────────────────── */
.message-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 16px;
  background: var(--bg-sidebar);
  border-top: 1px solid var(--border);
  z-index: 2;
  transition: background var(--transition-normal);
}

.input-wrapper {
  flex: 1;
  position: relative;
}

.message-input {
  width: 100%;
  max-height: 150px;
  padding: 12px 16px;
  background: var(--bg-hover);
  border: 1.5px solid transparent;
  border-radius: 22px;
  color: var(--text-primary);
  font-size: 0.92rem;
  outline: none;
  resize: none;
  line-height: 1.4;
  transition: all var(--transition-fast);
}

.message-input:focus {
  background: var(--bg-input);
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-bg);
}

.message-input::placeholder {
  color: var(--text-tertiary);
}

.send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff;
  cursor: pointer;
  flex-shrink: 0;
  transition: all var(--transition-fast);
  box-shadow: 0 2px 8px rgba(108, 92, 231, 0.3);
}

.send-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(108, 92, 231, 0.4);
}

.send-btn:active {
  transform: scale(0.95);
}

.send-btn svg {
  width: 20px;
  height: 20px;
}

/* ─── Emoji Picker ─────────────────────────────────── */
.emoji-picker {
  position: absolute;
  bottom: var(--input-height);
  left: 16px;
  width: 340px;
  height: 360px;
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  z-index: var(--z-dropdown);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: menuSlide 0.2s ease;
}

.emoji-picker-header {
  border-bottom: 1px solid var(--border);
  padding: 8px;
}

.emoji-tabs {
  display: flex;
  gap: 2px;
}

.emoji-tab {
  flex: 1;
  padding: 6px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  font-size: 1.1rem;
  cursor: pointer;
  transition: background var(--transition-fast);
}

.emoji-tab:hover {
  background: var(--bg-hover);
}

.emoji-tab.active {
  background: var(--primary-bg);
}

.emoji-grid {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  align-content: start;
}

.emoji-item {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  font-size: 1.3rem;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.emoji-item:hover {
  background: var(--bg-hover);
  transform: scale(1.15);
}

/* ═══════════════════════════════════════════════════════
   MODALS
   ═══════════════════════════════════════════════════════ */

.modal-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-overlay);
  z-index: var(--z-modal);
  animation: fadeIn 0.2s ease;
  padding: 20px;
}

.modal {
  width: 100%;
  max-width: 440px;
  max-height: 80vh;
  background: var(--bg-modal);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  animation: modalAppear 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalAppear {
  from { opacity: 0; transform: scale(0.95) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-title {
  font-size: 1.15rem;
  font-weight: 600;
}

.modal-close svg {
  width: 18px;
  height: 18px;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ─── User Search Results ──────────────────────────── */
.user-search-results {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 300px;
  overflow-y: auto;
}

.user-result {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background var(--transition-fast);
}

.user-result:hover {
  background: var(--bg-hover);
}

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

.user-result-name {
  font-weight: 500;
  font-size: 0.92rem;
}

.user-result-username {
  font-size: 0.8rem;
  color: var(--text-tertiary);
}

/* ─── Settings ─────────────────────────────────────── */
.settings-header {
  justify-content: space-between;
}

.settings-header .save-btn {
  color: #2ecc71;
}

.settings-body {
  padding: 0;
  background: var(--bg-body);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.settings-profile-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--bg-modal);
  padding: 32px 20px 24px;
}

.settings-avatar-wrapper {
  position: relative;
  width: 96px;
  height: 96px;
  margin-bottom: 16px;
  cursor: pointer;
  border-radius: 50%;
  box-shadow: var(--shadow-md);
  transition: transform 0.3s ease;
}

.settings-avatar-wrapper:hover {
  transform: scale(1.05);
}

.settings-avatar-large {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
  color: #fff;
  font-weight: 600;
  overflow: hidden;
}

.settings-avatar-large img, .avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.settings-avatar-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  opacity: 0;
  transition: opacity 0.2s;
}

.settings-avatar-wrapper:hover .settings-avatar-overlay {
  opacity: 1;
}

.settings-avatar-overlay svg {
  width: 32px;
  height: 32px;
}

.settings-main-name {
  font-size: 1.3rem;
  font-weight: 600;
  margin-bottom: 4px;
}

.settings-status {
  font-size: 0.9rem;
  color: #2ecc71;
}

.settings-section {
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  background: var(--bg-modal);
}

.form-label-static {
  display: block;
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.avatar-color-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.avatar-color-option {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.avatar-color-option:hover {
  transform: scale(1.15);
}

.avatar-color-option.selected {
  border-color: var(--text-primary);
  transform: scale(1.1);
}

/* ─── Selected Members (Group) ─────────────────────── */
.selected-members {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.selected-member {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 4px 6px;
  background: var(--primary-bg);
  border-radius: var(--radius-full);
  font-size: 0.82rem;
  color: var(--primary);
  cursor: pointer;
  transition: all var(--transition-fast);
}

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

.selected-member .avatar {
  width: 22px;
  height: 22px;
  font-size: 0.55rem;
}

/* ═══════════════════════════════════════════════════════
   CONTEXT MENU
   ═══════════════════════════════════════════════════════ */

.context-menu {
  position: fixed;
  min-width: 180px;
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 6px;
  z-index: var(--z-context);
  animation: menuSlide 0.15s ease;
}

.context-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 9px 14px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-primary);
  font-size: 0.86rem;
  cursor: pointer;
  transition: background var(--transition-fast);
}

.context-item:hover {
  background: var(--bg-hover);
}

.context-item svg {
  width: 18px;
  height: 18px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.context-item-danger {
  color: var(--text-danger);
}

.context-item-danger svg {
  color: var(--text-danger);
}

/* ─── Typing indicator ─────────────────────────────── */
.typing-dots {
  display: inline-flex;
  gap: 3px;
  padding: 0 2px;
}

.typing-dots span {
  width: 5px;
  height: 5px;
  background: var(--primary);
  border-radius: 50%;
  animation: typingDot 1.4s ease-in-out infinite;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingDot {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* No results */
.no-results {
  text-align: center;
  padding: 24px;
  color: var(--text-tertiary);
  font-size: 0.88rem;
}

/* ═══════════════════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════════════════ */

@media (max-width: 768px) {
  :root {
    --header-height: calc(56px + env(safe-area-inset-top, 0px));
    --input-height: 52px;
  }

  body, .messenger, .sidebar, .chat-area {
    height: 100%;
    height: 100dvh;
  }

  .sidebar {
    position: absolute;
    width: 100%;
    min-width: 100%;
    z-index: 20;
    border-right: none;
  }

  .chat-area {
    width: 100%;
  }

  .messenger.chat-open .sidebar {
    display: none;
  }

  .chat-header {
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
    padding-bottom: 10px;
  }

  .message-input-area {
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }

  .sidebar-header {
    padding-top: calc(16px + env(safe-area-inset-top, 0px));
  }

  .back-btn {
    display: flex;
  }

  .messages-list {
    padding: 0 16px;
  }

  .reply-preview {
    margin: 0 16px;
  }

  .message {
    max-width: 85%;
  }

  .emoji-picker {
    left: 8px;
    right: 8px;
    width: auto;
    bottom: calc(var(--input-height) + env(safe-area-inset-bottom, 0px) + 8px);
  }
}

@media (max-width: 480px) {
  .auth-container {
    padding: 28px 20px;
    border-radius: 16px;
    max-width: 90%;
  }

  .auth-title {
    font-size: 1.6rem;
  }
  
  .message-bubble {
    padding: 6px 10px;
  }
}
/* ═══════════════════════════════════════════════════════
   USER PROFILE MODAL
   ═══════════════════════════════════════════════════════ */

.profile-modal-content {
  max-width: 420px;
  padding: 0;
  overflow: hidden;
  border-radius: 24px;
  background: var(--bg-modal);
  animation: profileModalOpen 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes profileModalOpen {
  from { opacity: 0; transform: translateY(40px) scale(0.95); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.profile-modal-header {
  position: relative;
  padding: 40px 24px 24px;
  background: linear-gradient(to bottom, var(--primary-bg), transparent);
  text-align: center;
}

#profile-modal-close {
  position: absolute;
  top: 16px;
  left: 16px; /* Moved to left like a back button */
  right: auto;
  font-size: 1.8rem;
  line-height: 1;
  background: rgba(0,0,0,0.2);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.profile-header-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.profile-header-info h2 {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-top: 8px;
}

.profile-header-info p {
  font-size: 0.95rem;
  color: var(--text-tertiary);
}

.profile-status {
  padding: 4px 12px;
  background: var(--bg-hover);
  border-radius: var(--radius-full);
  font-size: 0.8rem;
  font-weight: 600;
  margin-top: 4px;
}

.profile-status.online {
  background: rgba(46, 204, 113, 0.15);
  color: #2ecc71;
}

.profile-modal-body {
  padding: 24px;
}

.profile-section {
  margin-bottom: 24px;
}

.profile-section h3 {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-tertiary);
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.profile-bio {
  font-size: 0.95rem;
  color: var(--text-secondary);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-header-info {
  cursor: pointer;
  transition: opacity 0.2s;
}

.chat-header-info:hover {
  opacity: 0.8;
}

@media (max-width: 768px) {
  .profile-modal-content {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    max-width: 100%;
    height: 100dvh;
    border-radius: 0;
    display: flex;
    flex-direction: column;
    animation: profileSlideIn 0.35s cubic-bezier(0.2, 1, 0.3, 1);
  }

  .profile-modal-body {
    flex: 1;
    overflow-y: auto;
  }

  @keyframes profileSlideIn {
    from { transform: translateX(100%); }
    to { transform: translateX(0); }
  }
}
