/* ═══════════════════════════════════════════════════════════════════════════
   RESET & BASE
   ═══════════════════════════════════════════════════════════════════════════ */

*,
*::before,
*::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent; /* remove grey flash on tap (Android) */
}

html {
  /* Lock the root — nothing above body should ever scroll */
  overflow: hidden;
  height: 100%;
}

body {
  font-family: 'Tajawal', sans-serif;
  background-color: #0b141a;
  margin: 0;

  /*
    position:fixed + inset:0 is the most reliable way to lock the viewport
    on Android Chrome. It prevents the URL bar resize from shifting layout
    and stops the page itself from scrolling when a child overscrolls.
  */
  position: fixed;
  inset: 0;

  /* Use the fixed dimensions as the flex container for login/chat */
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
}

/* Font helpers */
.header-font { font-family: 'Tajawal', sans-serif; }
.msg-text     { font-family: 'Noto Naskh Arabic', serif; }

/* Hide scrollbars while keeping scroll functionality */
.no-scrollbar {
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.no-scrollbar::-webkit-scrollbar { display: none; }

/* Prevent text selection on interactive elements */
button,
label,
.scroll-btn {
  user-select: none;
  -webkit-user-select: none;
}

/* Eliminate 300ms tap delay on buttons and labels */
button,
label {
  touch-action: manipulation;
}

/* Shared placeholder color */
input::placeholder { color: #8696a0; }


/* ═══════════════════════════════════════════════════════════════════════════
   LOGIN SCREEN
   ═══════════════════════════════════════════════════════════════════════════ */

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #0b141a;
  padding: 16px;
}

.login-card {
  background-color: #202c33;
  padding: 32px;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  width: 100%;
  max-width: 384px;
  border-top: 4px solid #00a884;
}

.login-title {
  font-size: 22px;
  font-weight: 700;
  color: #fff;
  text-align: center;
  margin: 0 0 32px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.login-input {
  width: 100%;
  padding: 16px;
  background-color: #2a3942;
  color: #fff;
  border: none;
  border-radius: 12px;
  outline: none;
  font-size: 16px;       /* 16px minimum prevents iOS/Android auto-zoom */
  font-family: 'Tajawal', sans-serif;
  transition: box-shadow 0.2s ease;
}

.login-input:focus {
  box-shadow: 0 0 0 2px #00a884;
}

.login-btn {
  width: 100%;
  background-color: #00a884;
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  padding: 16px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  transition: background-color 0.2s ease;
}

.login-btn:active { background-color: #06cf9c; }


/* ═══════════════════════════════════════════════════════════════════════════
   CHAT SCREEN SHELL
   The shell is a fixed-position flex column that fills the viewport.
   The keyboard on Android resizes window.innerHeight but NOT the fixed
   element — so the layout stays pinned and only the messages area shrinks
   via the visualViewport resize handler in script.js.
   ═══════════════════════════════════════════════════════════════════════════ */

.chat-screen {
  /*
    Don't use dvh/svh here. body is position:fixed and already fills the
    viewport via inset:0. height:100% inherits that exact pixel size.
    dvh would recalculate on keyboard open and cause a layout jump.
  */
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.hidden { display: none !important; }


/* ═══════════════════════════════════════════════════════════════════════════
   HEADER
   ═══════════════════════════════════════════════════════════════════════════ */

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #202c33;
  color: #fff;
  padding: 12px 16px;
  flex-shrink: 0;        /* never compress — always visible above keyboard */
  z-index: 10;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

.header-start {
  display: flex;
  align-items: center;
  gap: 12px;
}

.room-name {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
}

.presence-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.05);
  padding: 4px 12px;
  border-radius: 999px;
}

.presence-dot {
  width: 8px;
  height: 8px;
  background-color: #00a884;
  border-radius: 50%;
  animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.4; }
}

.presence-text {
  font-size: 12px;
  color: #d1d5db;
  margin: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Generic icon button used in header */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: #fff;
  padding: 8px;
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.1s ease;
}

.icon-btn:active { transform: scale(0.9); }

/* Ping button gets a yellow background */
.ping-btn {
  background-color: #f59e0b;
}

.ping-btn:active { background-color: #d97706; }

.exit-btn {
  font-size: 12px;
  font-weight: 700;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #d1d5db;
  padding: 6px 16px;
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.exit-btn:active { background-color: rgba(255, 255, 255, 0.08); }


/* ═══════════════════════════════════════════════════════════════════════════
   CONNECTION BANNER
   ═══════════════════════════════════════════════════════════════════════════ */

.connection-banner {
  font-size: 12px;
  text-align: center;
  max-height: 0;
  overflow: hidden;
  padding: 0;
  flex-shrink: 0;
  transition: max-height 0.3s ease, padding 0.3s ease;
}

.connection-banner.reconnecting {
  max-height: 32px;
  padding: 6px;
  background-color: #7c5c00;
  color: #ffd97d;
}

.connection-banner.restored {
  max-height: 32px;
  padding: 6px;
  background-color: #0a3d2b;
  color: #00a884;
}


/* ═══════════════════════════════════════════════════════════════════════════
   MAIN / MESSAGES AREA
   ═══════════════════════════════════════════════════════════════════════════ */

.chat-main {
  flex: 1;              /* takes all remaining space between header and footer */
  position: relative;
  overflow: hidden;     /* clips the chat-bg and contains the scroll region */
  min-height: 0;        /* critical: flex children can shrink below content size */
}

/* Tiled wallpaper background */
.chat-bg {
  background-image: url('https://user-images.githubusercontent.com/15075759/28719144-86dc0f70-73b1-11e7-911d-60d70fcded21.png');
  background-repeat: repeat;
  background-size: 400px;
  opacity: 0.05;
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}

/* The scrollable message list */
.messages {
  position: absolute;   /* fills chat-main exactly, independent of content height */
  inset: 0;
  z-index: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; /* momentum scrolling on iOS/Android */
  overscroll-behavior-y: contain;    /* stop scroll chaining to the page */
  padding: 16px;
  padding-bottom: 32px; /* breathing room so last message clears the typing indicator */
}


/* ═══════════════════════════════════════════════════════════════════════════
   SCROLL-TO-BOTTOM BUTTON
   ═══════════════════════════════════════════════════════════════════════════ */

.scroll-btn {
  position: absolute;
  bottom: 16px;
  left: 16px;           /* RTL: left side = visually on the right in Arabic layout */
  z-index: 10;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #202c33;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.scroll-btn.visible {
  opacity: 1;
  pointer-events: auto;
}

.scroll-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: #00a884;
  color: #fff;
  font-size: 10px;
  font-family: 'Tajawal', sans-serif;
  font-weight: 700;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  padding: 0 3px;
  display: none;
  align-items: center;
  justify-content: center;
}

.scroll-badge.visible { display: flex; }


/* ═══════════════════════════════════════════════════════════════════════════
   FOOTER / INPUT BAR
   ═══════════════════════════════════════════════════════════════════════════ */

.chat-footer {
  flex-shrink: 0;       /* never compress */
  position: relative;   /* anchor for the absolute typing indicator */
  background-color: #202c33;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  padding: 8px;
  z-index: 10;
}

/* Safe area padding for phones with a home indicator */
.safe-area-bottom {
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
}

/* ─── Typing Indicator ──────────────────────────────────────────────────── */

/*
  position:absolute + bottom:100% floats it just above the footer.
  It animates via height so the slide-up effect is preserved.
  Because it's out of flow it never pushes messages up.
*/
.typing-indicator {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  font-size: 12px;
  color: #8696a0;
  background-color: #202c33; /* match footer so it looks seamless */
  height: 0;
  overflow: hidden;
  opacity: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px;
  transition: height 0.2s ease, opacity 0.2s ease, padding 0.2s ease;
  pointer-events: none;
}

.typing-indicator.visible {
  height: 24px;
  opacity: 1;
  padding: 0 12px 4px;
  pointer-events: auto;
}

/* Bouncing dots */
.typing-dots {
  display: flex;
  gap: 3px;
  align-items: center;
}

.typing-dots span {
  width: 4px;
  height: 4px;
  background-color: #00a884;
  border-radius: 50%;
  animation: typing-bounce 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0);    opacity: 0.4; }
  30%           { transform: translateY(-4px); opacity: 1;   }
}

/* ─── Message Form ──────────────────────────────────────────────────────── */

.message-form {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-direction: row-reverse; /* RTL: send button on the left, input on the right */
  width: 100%;
}

.message-input {
  flex: 1;
  min-width: 0;
  padding: 12px 20px;
  border-radius: 999px;
  outline: none;
  background-color: #2a3942;
  color: #fff;
  border: none;
  font-size: 16px;     /* must be >=16px on mobile to prevent browser auto-zoom */
  font-family: 'Noto Naskh Arabic', serif;
  transition: box-shadow 0.2s ease;
}

.message-input:focus {
  box-shadow: 0 0 0 2px rgba(0, 168, 132, 0.5);
}

/* Shared style for image-picker label and send button */
.input-icon-btn,
.send-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  color: #fff;
  transition: transform 0.1s ease;
}

.input-icon-btn:active,
.send-btn:active { transform: scale(0.9); }

.input-icon-btn { background-color: #2a3942; }
.send-btn       { background-color: #00a884; }


/* ═══════════════════════════════════════════════════════════════════════════
   MESSAGE BUBBLES  (classes applied by script.js / images.js)
   ═══════════════════════════════════════════════════════════════════════════ */

/* ─── Delivery tick ─────────────────────────────────────────────────────── */

.msg-tick {
  display: inline-flex;
  align-items: center;
  margin-right: 2px;
  margin-left: 4px;
  letter-spacing: -3px;
  font-size: 10px;
  color: #53bdeb;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.msg-tick.visible { opacity: 1; }

/* ─── Clickable links ───────────────────────────────────────────────────── */

.msg-link {
  color: #53bdeb;
  text-decoration: underline;
  text-underline-offset: 2px;
  word-break: break-all;
}


/* ═══════════════════════════════════════════════════════════════════════════
   IMAGE MESSAGE GRID
   ═══════════════════════════════════════════════════════════════════════════ */

.msg-img-grid {
  display: grid;
  gap: 3px;
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 4px;
  max-width: 260px;
}

.msg-img-grid.count-1    { grid-template-columns: 1fr; }
.msg-img-grid.count-2    { grid-template-columns: 1fr 1fr; }
.msg-img-grid.count-3    { grid-template-columns: 1fr 1fr 1fr; }
.msg-img-grid.count-4,
.msg-img-grid.count-more { grid-template-columns: 1fr 1fr; }

.msg-img-thumb {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  cursor: pointer;
  display: block;
  transition: opacity 0.2s ease;
}

.msg-img-thumb:active { opacity: 0.8; }

.msg-img-more { position: relative; }

.msg-img-more-label {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 22px;
  font-weight: 700;
  font-family: 'Tajawal', sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}


/* ═══════════════════════════════════════════════════════════════════════════
   FULL-SCREEN IMAGE OVERLAY
   ═══════════════════════════════════════════════════════════════════════════ */

.image-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  /* dir="ltr" is set in HTML to prevent RTL from mirroring the arrow characters */
}

.image-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.overlay-img {
  max-width: 95vw;
  max-height: 90vh;
  object-fit: contain;
  border-radius: 8px;
}

.overlay-close {
  position: absolute;
  top: 16px;
  right: 20px;
  color: #fff;
  font-size: 24px;
  line-height: 1;
  padding: 8px;
  cursor: pointer;
  z-index: 101;
}

.overlay-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: #fff;
  font-size: 40px;
  padding: 8px 14px;
  border-radius: 8px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.2s ease;
}

.overlay-nav:active { background: rgba(255, 255, 255, 0.25); }
.overlay-prev { left: 8px; }
.overlay-next { right: 8px; }


/* ═══════════════════════════════════════════════════════════════════════════
   VOICE CALL OVERLAY
   Floats below the header as a compact card.
   The chat remains fully usable underneath.
   ═══════════════════════════════════════════════════════════════════════════ */

.call-overlay {
  position: fixed;
  top: 68px;             /* clear the header */
  left: 12px;
  right: 12px;
  z-index: 80;
  background-color: #1a2530;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
  overflow: hidden;

  /* Hidden by default */
  opacity: 0;
  pointer-events: none;
  transform: translateY(-6px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.call-overlay.visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

/* ─── Generic panel ─────────────────────────────────────────────────────── */

.call-panel {
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 20px 16px;
  width: 100%;
}

/* ─── Label above user list ─────────────────────────────────────────────── */

.call-panel-label {
  font-size: 12px;
  color: #8696a0;
  margin: 0;
  align-self: flex-start;
}

/* ─── User picker list ──────────────────────────────────────────────────── */

.call-user-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}

.call-user-item {
  width: 100%;
  background-color: #2a3942;
  border: none;
  color: #fff;
  font-size: 16px;
  padding: 12px 16px;
  border-radius: 12px;
  cursor: pointer;
  text-align: right;
  transition: background-color 0.15s ease;
}

.call-user-item:active { background-color: #3a4f5c; }

/* ─── Avatar ring ───────────────────────────────────────────────────────── */

.call-avatar-ring {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background-color: #2a3942;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
}

/* Pulsing ring animation for incoming call */
.call-avatar-ring.ringing {
  animation: call-ring-pulse 1.4s ease-out infinite;
  box-shadow: 0 0 0 0 rgba(0, 168, 132, 0.6);
}

@keyframes call-ring-pulse {
  0%   { box-shadow: 0 0 0 0   rgba(0, 168, 132, 0.6); }
  70%  { box-shadow: 0 0 0 14px rgba(0, 168, 132, 0);   }
  100% { box-shadow: 0 0 0 0   rgba(0, 168, 132, 0);   }
}

.call-avatar-icon {
  width: 24px;
  height: 24px;
  fill: #00a884;
}

/* ─── Name and status text ──────────────────────────────────────────────── */

.call-name {
  font-size: 18px;
  font-weight: 700;
  color: #fff;
  margin: 0;
}

.call-status-text {
  font-size: 13px;
  color: #8696a0;
  margin: 0;
}

/* ─── Call timer ────────────────────────────────────────────────────────── */

.call-timer {
  font-size: 22px;
  font-weight: 700;
  color: #00a884;
  letter-spacing: 2px;
  margin: 0;
}

/* ─── Action row ────────────────────────────────────────────────────────── */

.call-actions {
  display: flex;
  gap: 24px;
  align-items: center;
  justify-content: center;
  margin-top: 4px;
}

/* End / Decline — red circle */
.call-end-btn {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background-color: #e53e3e;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease;
  /* rotate icon so the phone points down = hang up signal */
  transform: rotate(135deg);
}

.call-end-btn:active {
  background-color: #c53030;
  transform: rotate(135deg) scale(0.92);
}

/* Accept — green circle */
.call-accept-btn {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background-color: #00a884;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease;
}

.call-accept-btn:active {
  background-color: #06cf9c;
  transform: scale(0.92);
}

/* Mute — neutral circle, red when muted */
.call-mute-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background-color: #2a3942;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease;
}

.call-mute-btn.muted { background-color: #e53e3e; }
.call-mute-btn:active { transform: scale(0.92); }

/* Secondary / cancel text button */
.call-secondary-btn {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: #8696a0;
  font-size: 14px;
  padding: 8px 24px;
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.call-secondary-btn:active { background-color: rgba(255, 255, 255, 0.06); }