/* ==========================================================
   SECURE CHAT — Stylesheet
   WhatsApp-inspired look, mobile-first & responsive
   ========================================================== */
:root {
  --wa-green-dark: #075E54;
  --wa-green: #128C7E;
  --wa-green-light: #25D366;
  --wa-bg-chat: #E5DDD5;
  --wa-bubble-sent: #DCF8C6;
  --wa-bubble-received: #FFFFFF;
  --wa-text: #303030;
  --wa-gray: #8696a0;
  --wa-border: #e9edef;
  --wa-sidebar-bg: #FFFFFF;
  --tick-gray: #8696a0;
  --tick-blue: #34B7F1;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: "Segoe UI", Helvetica, Arial, sans-serif;
  color: var(--wa-text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none; /* prevents iOS/Android pull-to-refresh & rubber-band bounce chrome without blocking normal scrolling */
  overflow-x: hidden; /* belt-and-braces: stops the whole page from ever scrolling/shifting sideways on mobile */
  width: 100%;
  -webkit-text-size-adjust: 100%; /* stops iOS/Android auto-resizing text on rotation, which was contributing to layout jumps */
}

/* ---------------- AUTH PAGES ---------------- */
.auth-body {
  background: linear-gradient(135deg, var(--wa-green-dark), var(--wa-green));
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.auth-card {
  background: #fff; width: 100%; max-width: 400px;
  border-radius: 12px; padding: 32px 28px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.25);
}
.auth-logo {
  text-align: center; font-size: 24px; font-weight: 700;
  color: var(--wa-green-dark); margin-bottom: 20px;
}
.auth-card label { display: block; font-size: 13px; color: var(--wa-gray); margin: 14px 0 4px; }
.auth-card input {
  width: 100%; padding: 12px 14px; border: 1px solid #ddd; border-radius: 8px;
  font-size: 16px; outline: none; transition: border-color .15s;
}
.auth-card input:focus { border-color: var(--wa-green); }
.btn-primary {
  margin-top: 22px; width: 100%; padding: 13px; border: none; border-radius: 8px;
  background: var(--wa-green-dark); color: #fff; font-size: 15px; font-weight: 600;
  cursor: pointer; transition: background .15s;
}
.btn-primary:hover { background: var(--wa-green); }
.btn-secondary {
  padding: 9px 14px; border: 1px solid var(--wa-green); border-radius: 8px;
  background: #fff; color: var(--wa-green-dark); font-weight: 600; cursor: pointer; font-size: 13px;
}
.switch-link { text-align: center; margin-top: 16px; font-size: 14px; color: var(--wa-gray); }
.switch-link a { color: var(--wa-green-dark); font-weight: 600; text-decoration: none; }
.alert { padding: 12px 14px; border-radius: 8px; font-size: 14px; margin-bottom: 10px; }
.alert-error { background: #fde8e8; color: #c0392b; }
.alert-success { background: #e6f7ee; color: #1a7a4c; }
.generated-id {
  text-align: center; font-size: 26px; font-weight: 800; letter-spacing: 1px;
  color: var(--wa-green-dark); margin: 10px 0;
}
.small-note { font-size: 12px; color: var(--wa-gray); text-align: center; }

/* ---------------- APP SHELL ---------------- */
.app-shell {
  display: flex; width: 100%; overflow: hidden;
  background: var(--wa-bg-chat);
  /* Fallback for older browsers */
  height: 100vh;
  /* --vh is set by JS (app.js) to the real visible height, so the layout
     resizes correctly when the mobile keyboard opens/closes or the
     browser's address bar shows/hides — plain 100vh does NOT react to
     either of those on many mobile browsers, which is what was causing
     the chat box to not fit properly on phones. */
  height: calc(var(--vh, 1vh) * 100);
  /* Modern browsers: dynamic viewport height as an extra safety net */
  height: 100dvh;
}
/* ---------------- SIDEBAR ---------------- */
.sidebar {
  width: 380px; max-width: 100%; background: var(--wa-sidebar-bg);
  display: flex; flex-direction: column; border-right: 1px solid var(--wa-border);
  flex-shrink: 0;
}
.sidebar-header {
  background: var(--wa-green-dark); color: #fff; padding: 12px 16px;
  display: flex; align-items: center; gap: 12px;
}
.my-avatar, .avatar-circle {
  width: 40px; height: 40px; border-radius: 50%; background: #ccc;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: #fff; overflow: hidden; flex-shrink: 0; cursor: pointer;
  background: linear-gradient(135deg, #667eea, #764ba2); font-size: 15px;
}
.my-avatar img, .avatar-circle img { width: 100%; height: 100%; object-fit: cover; }
.sidebar-title { flex: 1; font-size: 18px; font-weight: 600; }
.sidebar-actions { display: flex; gap: 14px; font-size: 18px; }
.sidebar-actions button, .sidebar-actions a { background: none; border: none; color: #fff; cursor: pointer; text-decoration: none; }
#notifyBellBtn.notify-active { color: var(--wa-green-light); }

.notify-banner {
  display: flex; align-items: center; gap: 8px; background: #fff8e1;
  padding: 10px 14px; font-size: 13px; color: #6b5900; border-bottom: 1px solid #f0e0a0;
}
.notify-banner span { flex: 1; }
#notifyEnableBtn {
  background: var(--wa-green-dark); color: #fff; border: none; border-radius: 14px;
  padding: 5px 12px; font-size: 12.5px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
#notifyDismissBtn { background: none; border: none; color: #6b5900; cursor: pointer; font-size: 14px; }

.search-wrap { padding: 10px 12px; background: #f7f7f7; }
.search-wrap input {
  width: 100%; padding: 9px 14px; border-radius: 20px; border: none;
  background: #fff; font-size: 16px; outline: none; box-shadow: inset 0 0 0 1px #eee;
}

.user-list { flex: 1; overflow-y: auto; }
.list-loading { padding: 20px; text-align: center; color: var(--wa-gray); font-size: 14px; }

.user-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px;
  cursor: pointer; border-bottom: 1px solid #f2f2f2; position: relative;
  transition: background .1s;
}
.user-item:hover, .user-item.active { background: #f5f5f5; }
.avatar-wrap { position: relative; flex-shrink: 0; }
.online-dot {
  width: 11px; height: 11px; border-radius: 50%; background: var(--wa-green-light);
  border: 2px solid #fff; position: absolute; bottom: -1px; right: -1px;
}
.user-item-info { flex: 1; min-width: 0; }
.user-item-top { display: flex; justify-content: space-between; align-items: center; }
.user-item-name { font-size: 15.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display:flex; align-items:center; gap:4px; }
.verified-badge { color: #1a9cf0; font-size: 12px; font-weight: 600; }
.user-item-time { font-size: 11.5px; color: var(--wa-gray); flex-shrink: 0; }
.user-item-bottom { display: flex; justify-content: space-between; align-items: center; margin-top: 2px; }
.user-item-preview { font-size: 13px; color: var(--wa-gray); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread-badge {
  background: var(--wa-green-light); color: #fff; font-size: 11px; font-weight: 700;
  border-radius: 10px; padding: 1px 7px; min-width: 18px; text-align: center; flex-shrink: 0;
}

/* ---------------- CHAT AREA ---------------- */
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.chat-placeholder {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--wa-gray); text-align: center; padding: 20px; background: #f8f9fa;
}
.placeholder-icon { font-size: 60px; margin-bottom: 12px; }

.chat-open { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-header {
  background: var(--wa-green-dark); color: #fff; padding: 10px 16px;
  display: flex; align-items: center; gap: 12px; flex-shrink: 0;
}
.back-btn { display: none; background: none; border: none; color: #fff; font-size: 20px; cursor: pointer; }
.chat-header-avatar {
  width: 40px; height: 40px; border-radius: 50%; overflow: hidden;
  background: linear-gradient(135deg, #667eea, #764ba2); display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: #fff; flex-shrink: 0;
}
.chat-header-avatar img { width: 100%; height: 100%; object-fit: cover; }
.chat-header-info { flex: 1; min-width: 0; }
.chat-header-name {
  font-size: 16px; font-weight: 600; display: flex; align-items: center; gap: 5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-header-status { font-size: 12.5px; color: #d3ede8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages-container {
  flex: 1; overflow-y: auto; overflow-x: visible; padding: 16px 6%;
  background-color: var(--wa-bg-chat);
  background-image: repeating-linear-gradient(45deg, rgba(0,0,0,0.015) 0 2px, transparent 2px 40px);
  display: flex; flex-direction: column; gap: 3px;
}
.date-separator {
  align-self: center; background: #e1f2fb; color: #4a5568; font-size: 12px;
  padding: 4px 12px; border-radius: 8px; margin: 10px 0; box-shadow: 0 1px 1px rgba(0,0,0,.08);
}

.msg-row { display: flex; max-width: 78%; margin-bottom: 2px; position: relative; }
.msg-row.mine { align-self: flex-end; justify-content: flex-end; }
.msg-row.theirs { align-self: flex-start; }

.bubble {
  padding: 6px 9px 7px 9px; border-radius: 8px; position: relative;
  box-shadow: 0 1px 1px rgba(0,0,0,0.1); font-size: 14.5px; line-height: 1.4;
  word-wrap: break-word; word-break: break-word; min-width: 60px;
  touch-action: pan-y; will-change: transform;
}
.msg-row.mine .bubble { background: var(--wa-bubble-sent); border-top-right-radius: 0; }
.msg-row.theirs .bubble { background: var(--wa-bubble-received); border-top-left-radius: 0; }

.bubble-text { white-space: pre-wrap; padding-right: 4px; }
.bubble-meta {
  display: flex; align-items: center; justify-content: flex-end; gap: 4px;
  font-size: 11px; color: var(--wa-gray); margin-top: 2px; float: right; margin-left: 8px;
}
.edited-tag { font-style: italic; margin-right: 3px; }
.tick { font-size: 14px; }
.tick.read { color: var(--tick-blue); }
.tick.delivered, .tick.sent { color: var(--tick-gray); }

.bubble-media img, .bubble-media video { max-width: 260px; max-height: 320px; border-radius: 6px; display: block; cursor: pointer; }
.bubble-audio audio { max-width: 240px; }
.bubble-file {
  display: flex; align-items: center; gap: 10px; background: rgba(0,0,0,0.03);
  padding: 8px 10px; border-radius: 6px; text-decoration: none; color: var(--wa-text);
}
.file-icon { font-size: 26px; }
.file-name { font-size: 13.5px; word-break: break-all; }

.deleted-msg { font-style: italic; color: var(--wa-gray); font-size: 13.5px; }

.msg-actions {
  position: absolute; top: -16px; right: 4px; display: none; gap: 2px;
  background: #fff; border-radius: 8px; box-shadow: 0 1px 6px rgba(0,0,0,0.25); padding: 3px;
  z-index: 2;
}
/* Desktop: reveal on hover */
.msg-row.mine:hover .msg-actions { display: flex; }
.msg-row.theirs:hover .msg-actions { display: flex; right: auto; left: 4px; }
/* Mobile / touch: reveal on tap (JS toggles .actions-open on the row) */
.msg-row.actions-open .msg-actions { display: flex; }
.msg-row.theirs.actions-open .msg-actions { right: auto; left: 4px; }
.msg-actions button {
  border: none; background: none; cursor: pointer; font-size: 15px; padding: 6px 8px;
  min-width: 32px; min-height: 32px; display: flex; align-items: center; justify-content: center;
}

/* ---------------- WHATSAPP-STYLE QUOTE REPLY ---------------- */
.reply-preview-bar {
  display: flex; align-items: center; gap: 10px; background: #f0f0f0;
  padding: 8px 16px; border-left: 4px solid var(--wa-green); flex-shrink: 0;
}
.reply-preview-content { flex: 1; min-width: 0; }
.reply-preview-label { font-size: 12.5px; font-weight: 700; color: var(--wa-green-dark); }
.reply-preview-text { font-size: 13px; color: var(--wa-gray); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#cancelReplyBtn { background: none; border: none; font-size: 16px; color: var(--wa-gray); cursor: pointer; }

.quoted-reply {
  background: rgba(0,0,0,0.06); border-left: 3px solid var(--wa-green);
  border-radius: 5px; padding: 5px 8px; margin-bottom: 4px; cursor: pointer;
}
.quoted-reply-sender { font-size: 12.5px; font-weight: 700; color: var(--wa-green-dark); }
.quoted-reply-text { font-size: 12.5px; color: #555; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 250px; }

@keyframes highlightFlash {
  0%   { background-color: rgba(37, 211, 102, 0.35); }
  100% { background-color: transparent; }
}
.msg-row.highlight-flash .bubble { animation: highlightFlash 1.2s ease-out; }

/* ---------------- SWIPE-TO-REPLY (WhatsApp-style gesture) ---------------- */
.swipe-reply-icon {
  position: absolute; left: 6px; top: 50%; transform: translateY(-50%) scale(0.6);
  width: 26px; height: 26px; border-radius: 50%; background: rgba(0,0,0,0.12);
  display: flex; align-items: center; justify-content: center; color: var(--wa-green-dark);
  opacity: 0; transition: opacity 0.15s ease, transform 0.15s ease, background 0.15s ease, color 0.15s ease;
  pointer-events: none; font-size: 12px; z-index: 1;
}
.msg-row.swiping .swipe-reply-icon { opacity: 1; transform: translateY(-50%) scale(0.85); }
.msg-row.swipe-armed .swipe-reply-icon {
  opacity: 1; transform: translateY(-50%) scale(1.05);
  background: var(--wa-green-light); color: #fff;
}

/* ---------------- CHAT INPUT BAR ---------------- */
.chat-input-bar {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  background: #f0f0f0; flex-shrink: 0;
}
.chat-input-bar input[type=text] {
  flex: 1; padding: 11px 16px; border-radius: 22px; border: none; outline: none;
  font-size: 16px; background: #fff;
}
#attachBtn, #sendBtn {
  background: var(--wa-green-dark); color: #fff; border: none; width: 42px; height: 42px;
  border-radius: 50%; font-size: 17px; cursor: pointer; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
#attachBtn { background: transparent; color: var(--wa-green-dark); font-size: 22px; }

/* ---------------- MODALS ---------------- */
.modal-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  align-items: center; justify-content: center; z-index: 999; padding: 16px;
}
.modal-overlay.show { display: flex; }
.modal-box {
  background: #fff; width: 100%; max-width: 420px; border-radius: 10px;
  max-height: 90vh; max-height: calc(var(--vh, 1vh) * 90); max-height: 90dvh;
  overflow-y: auto;
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 20px; border-bottom: 1px solid var(--wa-border); background: var(--wa-green-dark); color: #fff;
  border-radius: 10px 10px 0 0;
}
.modal-close { background: none; border: none; color: inherit; font-size: 18px; cursor: pointer; }
.modal-body { padding: 20px; }
.profile-photo-wrap { text-align: center; margin-bottom: 16px; }
.profile-photo-preview {
  width: 100px; height: 100px; border-radius: 50%; margin: 0 auto 10px;
  background: linear-gradient(135deg, #667eea, #764ba2); display: flex; align-items: center; justify-content: center;
  font-size: 30px; font-weight: 700; color: #fff; overflow: hidden;
}
.profile-photo-preview img { width: 100%; height: 100%; object-fit: cover; }
.upload-photo-btn {
  display: inline-block; padding: 7px 14px; border: 1px solid var(--wa-green); border-radius: 20px;
  font-size: 13px; color: var(--wa-green-dark); cursor: pointer; font-weight: 600;
}
.profile-field { margin-bottom: 14px; }
.profile-field label { display: flex; gap:6px; align-items:center; font-size: 13px; color: var(--wa-gray); margin-bottom: 4px; }
.profile-field input {
  width: 100%; padding: 10px 12px; border: 1px solid #ddd; border-radius: 8px; font-size: 16px; outline: none;
}
.profile-field input:disabled { background: #f5f5f5; color: #888; }

.media-modal-box { position: relative; max-width: 90vw; max-height: 90vh; }
.media-modal-box img, .media-modal-box video { max-width: 90vw; max-height: 85vh; border-radius: 6px; }
.media-modal-box .modal-close {
  position: absolute; top: -36px; right: 0; background: rgba(255,255,255,0.9); border-radius: 50%;
  width: 30px; height: 30px;
}

/* ---------------- GROUP CHAT ---------------- */
.group-avatar { background: linear-gradient(135deg, #56ab2f, #a8e063) !important; }
.group-icon { font-size: 16px; color: #fff; }
.group-sender-name {
  font-size: 12.5px; font-weight: 700; color: var(--wa-green-dark); margin-bottom: 2px;
}
.admin-tag {
  font-size: 10.5px; font-weight: 700; color: #fff; background: var(--wa-green);
  border-radius: 8px; padding: 1px 7px; margin-left: 4px; vertical-align: middle;
}
#chatHeaderInfo { min-width: 0; flex: 1; }

.member-picker-list {
  max-height: 220px; overflow-y: auto; border: 1px solid #e5e5e5; border-radius: 8px; padding: 4px;
}
.member-picker-row {
  display: flex; align-items: center; gap: 10px; padding: 8px 6px; cursor: pointer;
  border-radius: 6px; font-size: 14px;
}
.member-picker-row:hover { background: #f7f7f7; }
.member-picker-row input[type=checkbox] { width: 16px; height: 16px; flex-shrink: 0; }

/* ---------------- EMOJI PICKER ---------------- */
#emojiBtn { background: transparent; color: var(--wa-green-dark); font-size: 22px; border: none; cursor: pointer; }
.emoji-picker {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px;
  background: #fff; padding: 10px; max-height: 180px; overflow-y: auto;
  border-top: 1px solid var(--wa-border); flex-shrink: 0;
}
.emoji-item {
  font-size: 22px; text-align: center; cursor: pointer; padding: 4px; border-radius: 6px;
  user-select: none;
}
.emoji-item:hover { background: #f0f0f0; }

/* ---------------- RESPONSIVE (MOBILE) ---------------- */
@media (max-width: 820px) {
  .sidebar { width: 100%; }
  .chat-area { position: absolute; inset: 0; width: 100%; z-index: 5; display: none; }
  .app-shell.chat-open-mobile .sidebar { display: none; }
  .app-shell.chat-open-mobile .chat-area { display: flex; }
  .back-btn { display: inline-block; }
  .msg-row { max-width: 88%; }
  .emoji-picker { grid-template-columns: repeat(6, 1fr); max-height: 220px; }
  .modal-box { max-width: 100%; }
  .bubble-media img, .bubble-media video { max-width: 200px; max-height: 260px; }
}

@media (max-width: 380px) {
  .emoji-picker { grid-template-columns: repeat(5, 1fr); }
  .sidebar-title { font-size: 16px; }
  .sidebar-actions { gap: 10px; font-size: 16px; }
}
