/* ==============================================
   CSS VARIABLES - DESIGN TOKENS
   ============================================== */
:root {
  /* Premium Dark Mode Palette */
  --bg-body: #0b141a; /* Deep WhatsApp-like dark background */
  --bg-chat: #0b141a; /* Background behind messages */
  --bg-panel: #202c33; /* Input and header background */

  --primary: #00a884; /* Teal - Sent messages */
  --primary-hover: #008f6f;
  --bubble-other: #202c33; /* Received messages */

  --text-main: #e9edef; /* Primary text */
  --text-muted: #8696a0; /* Secondary text / timestamps */
  --error: #ef4444;
  --success: #00a884;

  /* Spacing & Sizing */
  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --spacing-sm: 8px;
  --spacing-md: 16px;
  --spacing-lg: 20px;

  /* Typography */
  --font-family: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
}

/* ==============================================
   RESET & GLOBAL STYLES
   ============================================== */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-family);
  background: var(--bg-body);
  color: var(--text-main);
  /* 100dvh prevents issues with mobile keyboards hiding the input */
  height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ==============================================
   UTILITY CLASSES
   ============================================== */
.hidden {
  display: none !important;
}

/* The container for each main screen */
.screen {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: opacity 0.3s ease;
}

/* ==============================================
   LOGIN SCREEN
   ============================================== */
.login-card {
  width: 90%;
  max-width: 360px;
  background: var(--bg-panel);
  padding: 40px;
  border-radius: var(--radius-lg);
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  animation: scaleUp 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.icon-container {
  width: 72px;
  height: 72px;
  background: var(--primary);
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto 24px;
  color: white;
}

.login-card h1 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.login-card p {
  color: var(--text-muted);
  margin-bottom: 32px;
  font-size: 14px;
}

form {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-md);
}

input {
  width: 100%;
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  background: #2a3942;
  border: 1px solid transparent;
  color: var(--text-main);
  font-size: 16px;
  font-family: inherit;
  transition: all 0.2s ease;
  outline: none;
}

input:focus {
  border-color: var(--primary);
  background: #202c33;
}

input::placeholder {
  color: var(--text-muted);
}

.btn-primary {
  background: var(--primary);
  color: white;
  border: none;
  padding: 14px;
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s ease;
}

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

.error-text {
  color: var(--error);
  font-size: 14px;
  margin-top: -8px;
  text-align: left;
}

/* ==============================================
   CHAT SCREEN
   ============================================== */
.chat-container {
  width: 100%;
  height: 100%;
  max-width: 800px;
  background: var(--bg-chat);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.glass-card {
  background: rgba(18, 28, 34, 0.92);
  backdrop-filter: blur(14px);
  border: 1px solid rgba(255, 255, 255, 0.06);
}

@media (max-width: 720px) {
  .chat-container {
    max-width: 100%;
    border-radius: 0;
    box-shadow: none;
    border: none;
  }

  .chat-header {
    padding: 10px 14px;
    gap: 10px;
    flex-wrap: wrap;
  }

  .chat-header h2 {
    font-size: 15px;
  }

  .chat-messages {
    padding: 12px;
    gap: 10px;
  }

  .chat-input-area {
    padding: 10px 12px;
    gap: 10px;
    flex-wrap: wrap;
  }

  .chat-input-area input {
    min-width: 0;
    flex: 1;
  }

  .btn-send {
    width: 44px;
    height: 44px;
  }

  .btn-icon {
    padding: 9px;
  }

  .message {
    max-width: 90%;
  }

  .reply-preview {
    padding: 10px 12px;
    font-size: 13px;
  }

  .reply-preview-text {
    max-width: 80%;
    white-space: normal;
  }
}

/* Deskto/Tablet styling borders */
@media (min-width: 801px) {
  .chat-container {
    height: 95vh;
    border-radius: var(--radius-lg);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    border: 1px solid #2a3942;
  }
}

.chat-header {
  padding: 12px var(--spacing-lg);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(16, 23, 30, 0.96);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  z-index: 10;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #6e7d88;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: 600;
  font-size: 16px;
  color: white;
}

.chat-header h2 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 2px;
}

.online-status {
  font-size: 13px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.online-status::before {
  content: "";
  display: block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted); /* Grey by default */
  transition: background 0.3s ease;
}

.online-status.is-online::before {
  background: var(--success); /* Green when online */
}

.btn-icon {
  background: rgba(255, 255, 255, 0.04);
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 10px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  transition:
    background 0.2s ease,
    color 0.2s ease;
}

.btn-icon:hover {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.12);
}

.btn-icon:active {
  transform: scale(0.97);
}

/* Messages Area */
.chat-messages {
  flex: 1;
  padding: var(--spacing-lg);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px; /* Tighter gap between messages */
  background: rgba(8, 17, 25, 0.72);
}

/* Scrollbar for webkit */
.chat-messages::-webkit-scrollbar {
  width: 6px;
}
.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.18);
  border-radius: 10px;
}

.message {
  max-width: 85%;
  display: flex;
  flex-direction: column;
  animation: fadeIn 0.2s ease-out;
  margin-bottom: 8px; /* Space between message groups */
}

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

.message.other {
  align-self: flex-start;
}

.message-bubble {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  line-height: 1.5;
  word-wrap: break-word;
  position: relative;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

/* Styling like modern chat apps where borders curve nicely */
.message.self .message-bubble {
  background: rgba(0, 193, 142, 0.16);
  color: #ffffff;
  border-top-right-radius: 0;
  border-color: rgba(0, 193, 142, 0.24);
}

.message.other .message-bubble {
  background: rgba(255, 255, 255, 0.08);
  color: #ffffff;
  border-top-left-radius: 0;
  border-color: rgba(255, 255, 255, 0.12);
}

.message-meta {
  font-size: 11px;
  margin-top: 8px;
  display: flex;
  gap: 8px;
  justify-content: space-between;
  flex-wrap: wrap;
  color: rgba(255, 255, 255, 0.65);
}

.message.self .message-meta {
  justify-content: space-between;
  color: rgba(235, 235, 236, 0.7);
}

/* Typing Indicator */
.typing-indicator {
  padding: 0 var(--spacing-lg) 10px;
  font-size: 13px;
  color: var(--primary);
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 4px;
}

.date-separator {
  margin: 8px auto;
  padding: 8px 12px;
  align-self: center;
  background: rgba(255, 255, 255, 0.08);
  color: rgba(255, 255, 255, 0.75);
  border-radius: 999px;
  font-size: 12px;
  letter-spacing: 0.03em;
  max-width: fit-content;
}

/* Input Area */
.chat-input-area {
  padding: 10px var(--spacing-lg);
  background: var(--bg-panel);
  display: flex;
  gap: 12px;
  align-items: center;
  flex-direction: row; /* Ensure row alignment */
}

.chat-input-area input {
  flex: 1;
  background: #2a3942;
  border-radius: 24px;
  padding: 12px 20px;
  margin: 0;
}

.btn-send {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: #111b21; /* Dark icon for contrast */
  border: none;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: transform 0.1s ease;
  flex-shrink: 0;
}

.btn-send:hover {
  transform: scale(1.05);
}

.recording-indicator {
  position: absolute;
  bottom: 72px;
  left: 0;
  right: 0;
  margin: 0 auto;
  max-width: 260px;
  background: rgba(17, 27, 33, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  z-index: 30;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.35);
}

.recording-status {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #f8fafc;
  font-weight: 700;
}

.recording-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #ef4444;
  animation: pulse 1.1s infinite ease-in-out;
  box-shadow: 0 0 0 rgba(239, 68, 68, 0.5);
}

.recording-hint {
  color: #a3b5c1;
  font-size: 13px;
}

.recording-indicator.recording-cancelled .recording-dot {
  background: #f59e0b;
  box-shadow: 0 0 0 rgba(245, 158, 11, 0.4);
}

.recording-indicator.recording-cancelled .recording-hint {
  color: #fbbf24;
}

.audio-message {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.audio-message audio {
  width: 100%;
  outline: none;
  border-radius: 14px;
  background: #0f172a;
}

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

/* Custom audio player */
.audio-player {
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(255, 255, 255, 0.08);
  padding: 12px 14px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  width: 100%;
  max-width: 100%;
  flex-wrap: wrap;
}

.audio-play-btn {
  width: 40px;
  height: 40px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.12);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--primary);
  cursor: pointer;
  flex-shrink: 0;
}

.audio-player.playing .audio-play-btn {
  background: linear-gradient(180deg, var(--primary), var(--primary-hover));
  color: #071014;
}

.audio-wave {
  flex: 1 1 120px;
  min-width: 100px;
  height: 18px;
  background: repeating-linear-gradient(
    90deg,
    rgba(255, 255, 255, 0.08) 0 2px,
    transparent 2px 6px
  );
  border-radius: 8px;
  opacity: 0.95;
}

.audio-progress {
  position: relative;
  height: 6px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  overflow: hidden;
  flex: 1 1 auto;
  min-width: 100px;
  cursor: pointer;
}

.audio-progress-fill {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), var(--primary-hover));
  transition: width 0.12s linear;
}

.audio-meta {
  width: auto;
  flex-shrink: 0;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.72);
  min-width: 36px;
}

.audio-label {
  margin-top: 6px;
  color: rgba(255, 255, 255, 0.78);
  font-size: 13px;
  line-height: 1.4;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message.self .audio-player {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.12);
}

.message.other .audio-player {
  background: rgba(255, 255, 255, 0.08);
}

.message.self .audio-meta {
  color: rgba(239, 244, 245, 0.65);
}

/* ==============================================
   ANIMATIONS
   ============================================== */
@keyframes scaleUp {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

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

/* ==============================================
   REPLY FEATURE STYLES
   ============================================== */
.reply-preview {
  padding: 8px var(--spacing-lg);
  background: var(--bg-panel);
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-left: 4px solid var(--primary);
  border-bottom: 1px solid #2a3942;
  font-size: 13px;
}

.reply-preview-content {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
}

.reply-preview-author {
  color: var(--primary);
  font-weight: 600;
}

.reply-preview-text {
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 90%;
}

.message-bubble-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
}

.message.self .message-bubble-wrapper {
  flex-direction: row-reverse;
}

.btn-reply-msg {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 50%;
  opacity: 0;
  transition:
    opacity 0.2s,
    background 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.message:hover .btn-reply-msg {
  opacity: 1;
}

.btn-reply-msg:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-main);
}

.quoted-message {
  background: rgba(0, 0, 0, 0.15);
  border-left: 4px solid var(--primary);
  padding: 6px 8px;
  border-radius: 4px;
  margin-bottom: 6px;
  font-size: 13px;
}

.message.self .quoted-message {
  background: rgba(0, 0, 0, 0.1);
  border-left-color: #0b141a;
}

.quoted-message-author {
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 2px;
}

.message.self .quoted-message-author {
  color: #111b21;
}

.quoted-message-text {
  color: rgba(255, 255, 255, 0.8);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.message.self .quoted-message-text {
  color: rgba(17, 27, 33, 0.8);
}

/* ==============================================
   SETTINGS MODAL
   ============================================== */
.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(11, 20, 26, 0.8);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex;
  justify-content: center;
  align-items: center;
  animation: fadeIn 0.2s ease;
}

.modal-content {
  background: var(--bg-panel);
  width: 90%;
  max-width: 360px;
  border-radius: var(--radius-lg);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  animation: scaleUp 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal-header {
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.modal-header h3 {
  font-size: 18px;
  font-weight: 600;
}

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

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 15px;
}

/* Toggle Switch */
.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
  margin: 0;
  padding: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #374045;
  transition: 0.4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: 0.4s;
}

input:checked + .slider {
  background-color: var(--primary);
}

input:checked + .slider:before {
  transform: translateX(20px);
}

.slider.round {
  border-radius: 24px;
}

.slider.round:before {
  border-radius: 50%;
}

.btn-secondary {
  background: transparent;
  color: var(--primary);
  border: 1px solid var(--primary);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 600;
  transition: all 0.2s;
  font-family: inherit;
}

.btn-secondary:hover {
  background: var(--primary);
  color: #111b21;
}

.btn-danger {
  background: rgba(239, 68, 68, 0.1);
  color: var(--error);
  border: none;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 600;
  transition: all 0.2s;
  font-family: inherit;
}

.btn-danger:hover {
  background: var(--error);
  color: white;
}

/* ==============================================
   SNAP FEATURE STYLES
   ============================================== */
.btn-view-snap {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #ffffff;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  transition: all 0.2s ease;
  font-family: inherit;
}

.message.self .btn-view-snap {
  background: rgba(255, 255, 255, 0.16);
  border-color: rgba(255, 255, 255, 0.24);
  color: #ffffff;
}

.btn-view-snap:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: translateY(-1px);
}

.message.self .btn-view-snap:hover {
  background: rgba(0, 0, 0, 0.15);
}

.image-modal-content {
  background: #111b21;
  padding: 20px;
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
  animation: scaleUp 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.snap-option-btn {
  background: transparent;
  border: none;
  color: var(--text-main);
  padding: 12px 16px;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  transition: background 0.2s;
}

.snap-option-btn:hover {
  background: rgba(255, 255, 255, 0.05);
}

.snap-option-btn:first-child {
  border-top-left-radius: var(--radius-sm);
  border-top-right-radius: var(--radius-sm);
  border-bottom: 1px solid #2a3942;
}

.snap-option-btn:last-child {
  border-bottom-left-radius: var(--radius-sm);
  border-bottom-right-radius: var(--radius-sm);
}

/* ==========================================
   CALL MODALS STYLING
   ========================================== */

#active-call-modal,
#incoming-call-modal {
  position: fixed !important;
  top: 50% !important;
  left: 50% !important;
  transform: translate(-50%, -50%) !important;
  width: 100% !important;
  height: 100% !important;
  max-width: 420px !important;
  max-height: 820px !important;
  border-radius: var(--radius-lg) !important;
  overflow: hidden !important;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.8) !important;
  background: #0b141a !important; /* Force deep dark theme for calling screens */
}

@media (max-width: 480px) {
  #active-call-modal,
  #incoming-call-modal {
    width: 100% !important;
    height: 100% !important;
    max-width: 100% !important;
    max-height: 100% !important;
    border-radius: 0 !important;
  }
}

#active-call-modal .in-call-header {
  position: relative;
  z-index: 10; /* Float user name/status above video */
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.9);
}

#active-call-modal .in-call-controls {
  position: relative;
  z-index: 10; /* Float action buttons above video */
}

#active-call-modal .avatar {
  animation: pulse-ring 2s infinite;
}

@keyframes pulse-ring {
  0% {
    box-shadow: 0 0 0 0 rgba(0, 168, 132, 0.7);
  }
  70% {
    box-shadow: 0 0 0 20px rgba(0, 168, 132, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(0, 168, 132, 0);
  }
}

#active-call-modal video#remote-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  border-radius: inherit;
}

#active-call-modal video#local-video {
  width: 100px;
  height: 140px;
  object-fit: cover;
  position: absolute;
  bottom: 120px; /* Elevated to sit nicely above the control panel buttons */
  right: 20px;
  z-index: 5;
  border-radius: var(--radius-md);
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.6);
  transform: scaleX(-1); /* Mirror local video feed */
}

/* Hide avatars when video is active */
.video-active #active-call-avatar {
  display: none !important;
}
