/* ============================================================
   WHATSAPP DARK MODE — Exact UI Replica (Fixed Layout)
   ============================================================ */

:root {
  --wa-bg-main: #111b21;
  --wa-bg-panel: #202c33;
  --wa-bg-header: #202c33;
  --wa-bg-search: #2a3942;
  --wa-bg-bubble-out: #005c4b;
  --wa-bg-bubble-in: #202c33;
  --wa-accent: #00a884;
  --wa-accent-light: #25d366;
  --wa-text-primary: #e9edef;
  --wa-text-secondary: #8696a0;
  --wa-text-icon: #8696a0;
  --wa-border: #2a3942;
  --wa-read-blue: #53bdeb;
}

/* --- CHAT APP SHELL --- */
#chat-app {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
  flex-direction: column;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--wa-bg-main);
  color: var(--wa-text-primary);
  direction: rtl;
  height: 100%;
  height: 100dvh; /* dynamic viewport height — يتكيف مع ظهور الكيبورد */
  width: 100%;
  overflow: hidden;
}

/* --- CONTAINER FIX --- */
#chat-root {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  width: 100%;
  position: relative;
  overflow: hidden;
}

/* --- HEADER --- */
.wa-header {
  display: flex;
  align-items: center;
  padding: 0 10px;
  height: 60px;
  min-height: 60px;
  background: var(--wa-bg-header);
  border-bottom: 1px solid var(--wa-border);
  flex-shrink: 0;
  /* ثابت دائماً في الأعلى حتى عند ظهور الكيبورد */
  position: sticky;
  top: 0;
  z-index: 100;
  /* مساحة آمنة لـ notch في أعلى الشاشة */
  padding-top: env(safe-area-inset-top, 0px);
  height: calc(60px + env(safe-area-inset-top, 0px));
}

.wa-header .back-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  color: var(--wa-text-icon);
  font-size: 1.2rem;
  cursor: pointer;
  margin-left: 5px;
}

.wa-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  background: var(--wa-bg-search);
  flex-shrink: 0;
  position: relative;
}

.wa-online-dot {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 12px;
  height: 12px;
  background: var(--wa-accent-light);
  border: 2px solid var(--wa-bg-header);
  border-radius: 50%;
}

.wa-contact-info {
  flex: 1;
  margin-right: 15px;
  overflow: hidden;
}

.wa-contact-name {
  font-size: 1rem;
  font-weight: 500;
  color: var(--wa-text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.wa-contact-status {
  font-size: 0.8rem;
  color: var(--wa-text-secondary);
  white-space: nowrap;
}

.wa-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.wa-icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: transparent;
  border: none;
  color: var(--wa-text-icon);
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
}

.wa-icon-btn:hover {
  background: rgba(255,255,255,0.05);
}

/* --- MESSAGES AREA --- */
.wa-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 10%;
  background-color: var(--wa-bg-main);
  background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23ffffff' fill-opacity='0.02' fill-rule='evenodd'/%3E%3C/svg%3E");
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
  overflow-x: hidden;
  /* منع التكبير بالأصابع وبالضغط المزدوج */
  touch-action: pan-y;
}

.wa-messages::-webkit-scrollbar {
  width: 6px;
}
.wa-messages::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.1);
  border-radius: 3px;
}

/* --- DATE CHIP --- */
.date-chip {
  align-self: center;
  background: rgba(0,0,0,0.3);
  color: rgba(255,255,255,0.6);
  font-size: 0.7rem;
  padding: 6px 12px;
  border-radius: 8px;
  margin: 10px 0;
}

.encrypted-notice {
  align-self: center;
  color: var(--wa-text-secondary);
  font-size: 0.75rem;
  margin: 10px 0 20px;
  text-align: center;
  opacity: 0.8;
}

/* --- MESSAGE ROWS --- */
.msg-row {
  display: flex;
  width: 100%;
  margin-bottom: 2px;
}

.msg-row.me {
  justify-content: flex-start;
}

.msg-row.them {
  justify-content: flex-end;
}

/* --- BUBBLE --- */
.bubble {
  position: relative;
  max-width: 65%;
  padding: 6px 7px 8px 9px;
  border-radius: 7.5px;
  font-size: 0.9rem;
  line-height: 1.4;
  word-wrap: break-word;
  box-shadow: 0 1px 0.5px rgba(0,0,0,0.13);
}

.msg-row.me .bubble {
  background: var(--wa-bg-bubble-out);
  border-top-right-radius: 0;
}

.msg-row.me .bubble::before {
  content: '';
  position: absolute;
  top: 0;
  right: -8px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 0 8px 8px;
  border-color: transparent transparent transparent var(--wa-bg-bubble-out);
}

.msg-row.them .bubble {
  background: var(--wa-bg-bubble-in);
  border-top-left-radius: 0;
}

.msg-row.them .bubble::before {
  content: '';
  position: absolute;
  top: 0;
  left: -8px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 8px 8px 0;
  border-color: transparent var(--wa-bg-bubble-in) transparent transparent;
}

.bubble.sticker {
  background: transparent !important;
  border-radius: 8px !important;
  box-shadow: none !important;
  padding: 0 !important;
}
.bubble.sticker::before { display: none; }

/* --- MESSAGE META --- */
.msg-meta {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  float: left;
  margin-top: 4px;
  margin-right: 8px;
  direction: ltr;
  white-space: nowrap;
}

.msg-time {
  font-size: 0.68rem;
  color: rgba(255,255,255,0.5);
}

.msg-ticks {
  font-size: 0.8rem;
  color: rgba(255,255,255,0.5);
  line-height: 1;
}
.msg-ticks.read {
  color: var(--wa-read-blue);
}

/* --- MEDIA --- */
.media-wrap {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  margin: -4px -9px 0 -9px;
}
.media-wrap img, .media-wrap video {
  display: block;
  max-width: 330px;
  width: 100%;
  border-radius: 8px;
}

/* --- INPUT AREA --- */
.wa-input-area {
  display: flex;
  align-items: flex-end;
  padding: 10px 16px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  background: var(--wa-bg-panel);
  border-top: 1px solid var(--wa-border);
  gap: 8px;
  flex-shrink: 0;
  /* منع الانكماش عند ظهور الكيبورد */
  position: sticky;
  bottom: 0;
  z-index: 10;
}

.wa-input-box {
  flex: 1;
  display: flex;
  align-items: flex-end;
  background: var(--wa-bg-search);
  border-radius: 25px;
  padding: 0 12px;
  min-height: 42px;
}

#msg-inp {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--wa-text-primary);
  font-family: inherit;
  /* 16px على الأقل لمنع iOS من التكبير التلقائي عند الفوكس */
  font-size: 16px;
  resize: none;
  max-height: 100px;
  padding: 10px 0;
  line-height: 1.4;
  /* منع التكبير */
  touch-action: manipulation;
}
#msg-inp::placeholder { color: var(--wa-text-secondary); }

.emoji-open-btn, .wa-attach-btn, .wa-send-btn, .wa-voice-btn {
  background: none;
  border: none;
  color: var(--wa-text-icon);
  font-size: 1.4rem;
  padding: 8px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  /* منع التكبير عند الضغط المزدوج */
  touch-action: manipulation;
}

.wa-send-btn, .wa-voice-btn {
  color: var(--wa-accent);
}

.wa-voice-btn.recording {
  color: #ef4444;
  animation: recPulse 1s infinite;
}

/* --- REPLY BAR --- */
.reply-bar {
  display: none;
  align-items: center;
  padding: 8px 16px;
  background: var(--wa-bg-panel);
  border-top: 1px solid var(--wa-border);
  gap: 10px;
  flex-shrink: 0;
}
.reply-bar.show { display: flex; }
.reply-bar-line { width: 4px; height: 36px; background: var(--wa-accent); border-radius: 2px; }
.reply-bar-content { flex: 1; overflow: hidden; }
.reply-bar-name { color: var(--wa-accent); font-size: 0.85rem; font-weight: 500; }
.reply-bar-text { color: var(--wa-text-secondary); font-size: 0.85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-bar-close { background: none; border: none; color: var(--wa-text-secondary); font-size: 1.2rem; }

/* --- MENUS --- */
.attach-menu, .ctx-menu, .emoji-picker {
  position: fixed;
  background: var(--wa-bg-panel);
  border: 1px solid var(--wa-border);
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.3);
  z-index: 1000;
  display: none;
  animation: fadeIn 0.2s;
}
.attach-menu.open, .ctx-menu.open, .emoji-picker.open { display: flex; }

.attach-menu {
  bottom: 70px;
  left: 16px;
  flex-direction: column;
  padding: 8px 0;
  min-width: 200px;
}
.att-item {
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  cursor: pointer;
  color: var(--wa-text-primary);
}
.att-item:hover { background: rgba(255,255,255,0.05); }

.ctx-menu {
  width: 180px;
  flex-direction: column;
  padding: 4px;
}
.ctx-item {
  padding: 10px 16px;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--wa-text-primary);
  border-radius: 4px;
}
.ctx-item:hover { background: var(--wa-bg-search); }
.ctx-item.danger { color: #ef4444; }

/* --- CALL OVERLAY --- */
.call-overlay {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, #111b21 0%, #0b141a 100%);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9998;
}
.call-overlay.open { display: flex; }

.call-avatar-big {
  width: 120px; height: 120px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 4rem;
  margin-bottom: 20px;
  box-shadow: 0 0 0 10px rgba(0,168,132,0.1);
  animation: callPulse 2s infinite;
}

@keyframes callPulse {
  0% { box-shadow: 0 0 0 0 rgba(0,168,132,0.2); }
  70% { box-shadow: 0 0 0 20px rgba(0,168,132,0); }
}

.call-name { font-size: 1.8rem; margin-bottom: 10px; font-weight: 500; }
.call-status { color: var(--wa-text-secondary); margin-bottom: 40px; }
.call-btns { display: flex; gap: 30px; }
.call-btn {
  width: 60px; height: 60px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  cursor: pointer;
  transition: transform 0.2s;
}
.call-btn:hover { transform: scale(1.1); }
.call-btn.end { background: #ef4444; color: white; }
.call-btn.accept { background: var(--wa-accent); color: white; }
.call-btn.mute { background: var(--wa-bg-search); color: var(--wa-text-primary); }

/* --- TOAST & SCROLL --- */
.wa-toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.8);
  color: white;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 0.85rem;
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 10000;
}
.wa-toast.show { opacity: 1; }

.scroll-down {
  position: absolute;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--wa-bg-panel);
  color: var(--wa-text-primary);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 0.8rem;
  border: 1px solid var(--wa-border);
  cursor: pointer;
  display: none;
}
.scroll-down.show { display: block; }

/* Preview Bar */
#preview-bar {
  display: none;
  flex-direction: row;
  gap: 8px;
  padding: 10px 16px;
  background: var(--wa-bg-panel);
  border-top: 1px solid var(--wa-border);
  overflow-x: auto;
  flex-shrink: 0;
}
.prev-item img, .prev-item video {
  width: 60px; height: 60px;
  object-fit: cover;
  border-radius: 8px;
  border: 2px solid var(--wa-accent);
}

/* Image Viewer */
.img-viewer {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.95);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 10000;
}
.img-viewer img { max-width: 95vw; max-height: 95vh; }
.viewer-close {
  position: absolute;
  top: 20px; right: 20px;
  background: rgba(255,255,255,0.1);
  border: none;
  color: white;
  font-size: 1.5rem;
  width: 40px; height: 40px;
  border-radius: 50%;
  cursor: pointer;
}

@keyframes recPulse { 0% { transform: scale(1); } 50% { transform: scale(1.1); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* Responsive */
@media (max-width: 768px) {
  .wa-messages { padding: 10px 16px; }
  .bubble { max-width: 85%; }
}

/* ── منع التكبير على الموبايل بشكل كامل ── */
#chat-app * {
  /* منع تحديد النص (يمنع الضغط المزدوج من التكبير في بعض المتصفحات) */
  -webkit-tap-highlight-color: transparent;
}

#chat-app button,
#chat-app .att-item,
#chat-app .ctx-item,
#chat-app .portfolio-item {
  touch-action: manipulation;
}

/* منع تكبير الصور في viewer بالأصابع — السماح فقط بالإغلاق */
.img-viewer {
  touch-action: pinch-zoom;
}

/* ── PINNED MESSAGE ─────────────────────────────────────── */
.msg-pinned .bubble {
  border: 1px solid rgba(245,159,0,0.4);
  box-shadow: 0 0 8px rgba(245,159,0,0.15);
}

/* ── EDIT BAR ───────────────────────────────────────────── */
#edit-bar {
  display: none;
  align-items: center;
  padding: 8px 16px;
  background: #1a2c36;
  border-top: 1px solid #2a3942;
  gap: 10px;
  flex-shrink: 0;
}
#edit-bar.show { display: flex; }

/* ── DELETE TIMER badge ─────────────────────────────────── */
.delete-badge {
  position: absolute;
  top: -6px;
  left: -6px;
  background: #ef4444;
  color: #fff;
  font-size: 0.6rem;
  padding: 2px 5px;
  border-radius: 8px;
  line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
}