/* ══════════════════════════════════════════
   RESET & BASE
══════════════════════════════════════════ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  /* WhatsApp Dark palette */
  --bg-primary:    #111b21;
  --bg-secondary:  #1f2c34;
  --bg-tertiary:   #2a3942;
  --bg-chat:       #0b141a;
  --bubble-out:    #005c4b;
  --bubble-in:     #1f2c34;
  --accent:        #00a884;
  --accent-dark:   #008069;
  --text-primary:  #e9edef;
  --text-secondary:#8696a0;
  --text-muted:    #667781;
  --divider:       #2a3942;
  --unread-badge:  #00a884;
  --topbar-height: 56px;
  --bottomnav-h:   58px;
  --inputbar-h:    62px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: -apple-system, 'Segoe UI', Helvetica, Arial, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  /* Prevent overscroll/bounce in fullscreen PWA */
  overscroll-behavior: none;
  touch-action: pan-x pan-y;
}

button { border: none; background: none; cursor: pointer; color: inherit; font: inherit; }
textarea { border: none; outline: none; resize: none; font: inherit; background: none; color: inherit; }
input { border: none; outline: none; font: inherit; background: none; color: inherit; }

/* ══════════════════════════════════════════
   SCREENS
══════════════════════════════════════════ */
/* ── The ONE fixed element. Tracks visual viewport height so the
   topbar never disappears behind the keyboard. ─────────────────── */
#app {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--app-height, 100dvh);
  overflow: hidden;
}

/* Screens are absolute inside #app — safe from transform issues */
.screen {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  will-change: transform;
  overflow: hidden;
}
.screen.active {
  transform: translateX(0);
}
.screen.slide-out {
  transform: translateX(-30%);
}

/* ══════════════════════════════════════════
   TOPBAR
══════════════════════════════════════════ */
.topbar {
  display: flex;
  align-items: center;
  padding: calc(var(--safe-top) + 10px) 8px 10px 16px;
  background: var(--bg-secondary);
  min-height: calc(var(--topbar-height) + var(--safe-top));
  gap: 4px;
  flex-shrink: 0;
  z-index: 10;
}

.topbar-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  flex: 1;
}

.topbar-actions {
  display: flex;
  gap: 4px;
  margin-left: auto;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  transition: background 0.15s;
  flex-shrink: 0;
}
.icon-btn:active { background: rgba(255,255,255,0.08); }
.icon-btn svg { width: 22px; height: 22px; }

/* Chat Topbar */
/* Chat screen uses CSS Grid so every row sizes itself —
   no hardcoded heights, no absolute positioning. */
#screen-chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.chat-topbar {
  /* Grid row 1: sizes to its own content, never scrolls */
  grid-row: 1;
  padding-left: 4px;
  gap: 0;
  z-index: 5;
  position: relative;
}

.back-btn { color: var(--text-primary); }
.back-btn svg { width: 24px; height: 24px; }

.chat-header-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  padding: 4px 0;
  cursor: pointer;
  border-radius: 8px;
  padding-left: 4px;
}
.chat-header-info:active { background: rgba(255,255,255,0.05); }

.chat-header-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

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

.chat-header-status {
  font-size: 12px;
  color: var(--text-secondary);
}

/* ══════════════════════════════════════════
   SEARCH BAR
══════════════════════════════════════════ */
.search-bar {
  display: flex;
  align-items: center;
  background: var(--bg-tertiary);
  margin: 8px 12px;
  border-radius: 8px;
  padding: 6px 12px;
  gap: 10px;
  flex-shrink: 0;
}
.search-bar .search-icon { width: 18px; height: 18px; color: var(--text-muted); flex-shrink: 0; }
.search-bar input { flex: 1; font-size: 15px; color: var(--text-primary); }
.search-bar input::placeholder { color: var(--text-muted); }
.search-bar .icon-btn { width: 32px; height: 32px; }

/* ══════════════════════════════════════════
   FILTER TABS
══════════════════════════════════════════ */
.filter-tabs {
  display: flex;
  gap: 8px;
  padding: 0 12px 8px;
  overflow-x: auto;
  scrollbar-width: none;
  flex-shrink: 0;
}
.filter-tabs::-webkit-scrollbar { display: none; }

.filter-tab {
  padding: 5px 14px;
  border-radius: 16px;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  transition: all 0.15s;
  flex-shrink: 0;
}
.filter-tab.active {
  background: #0d2d25;
  color: var(--accent);
}
.filter-tab:active { opacity: 0.7; }

/* ══════════════════════════════════════════
   CHAT LIST
══════════════════════════════════════════ */
.chat-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.chat-list::-webkit-scrollbar { display: none; }

.chat-item {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  gap: 12px;
  cursor: pointer;
  position: relative;
  transition: background 0.1s;
  -webkit-tap-highlight-color: transparent;
}
.chat-item:active { background: var(--bg-tertiary); }
.chat-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 72px;
  right: 0;
  height: 1px;
  background: var(--divider);
}

/* Avatar */
.chat-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 600;
  color: white;
  overflow: hidden;
  position: relative;
  user-select: none;
}
.chat-avatar.online::after {
  content: '';
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 13px;
  height: 13px;
  background: var(--accent);
  border-radius: 50%;
  border: 2px solid var(--bg-primary);
}

/* Chat row content */
.chat-content {
  flex: 1;
  min-width: 0;
}

.chat-row-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 3px;
}

.chat-name {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  margin-right: 8px;
}

.chat-time {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}
.chat-time.unread { color: var(--accent); }

.chat-row-bottom {
  display: flex;
  align-items: center;
  gap: 4px;
}

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

.chat-ticks {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: var(--text-muted);
}
.chat-ticks.read { color: #53bdeb; }

.unread-badge {
  background: var(--unread-badge);
  color: white;
  border-radius: 50%;
  min-width: 20px;
  height: 20px;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0 4px;
}

.mute-icon {
  color: var(--text-muted);
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

/* ══════════════════════════════════════════
   FAB
══════════════════════════════════════════ */
.fab {
  position: absolute;
  bottom: calc(var(--bottomnav-h) + var(--safe-bottom) + 16px);
  right: 16px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  z-index: 5;
  transition: transform 0.15s, box-shadow 0.15s;
}
.fab:active { transform: scale(0.93); box-shadow: 0 2px 6px rgba(0,0,0,0.4); }
#screen-chats .fab:first-of-type { display: none !important; }

/* ══════════════════════════════════════════
   BOTTOM NAV
══════════════════════════════════════════ */
.bottom-nav {
  display: flex;
  background: var(--bg-secondary);
  padding-bottom: var(--safe-bottom);
  height: calc(var(--bottomnav-h) + var(--safe-bottom));
  flex-shrink: 0;
  border-top: 1px solid var(--divider);
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  padding: 8px 0 6px;
  transition: color 0.15s;
}
.nav-btn svg { width: 22px; height: 22px; }
.nav-btn.active { color: var(--accent); }
.nav-btn:active { opacity: 0.7; }

/* ══════════════════════════════════════════
   MESSAGES
══════════════════════════════════════════ */
.messages-container {
  /* Grid row 2: stretches to fill all remaining space */
  grid-row: 2;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  min-height: 0; /* critical: lets grid child shrink below content size */
  background: var(--bg-chat);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='400'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='400' height='400' filter='url(%23n)' opacity='0.025'/%3E%3C/svg%3E");
}
.messages-container::-webkit-scrollbar { display: none; }

.messages {
  padding: 10px 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 100%;
}

/* Date separator */
.date-sep {
  text-align: center;
  margin: 8px 0;
}
.date-sep span {
  background: #1d2d35;
  color: var(--text-secondary);
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 6px;
  display: inline-block;
}

/* Bubble */
.bubble-wrap {
  display: flex;
  flex-direction: column;
  max-width: 85%;
  margin-bottom: 2px;
}
.bubble-wrap.out {
  align-self: flex-end;
  align-items: flex-end;
}
.bubble-wrap.in {
  align-self: flex-start;
  align-items: flex-start;
}
.bubble-wrap.out + .bubble-wrap.in,
.bubble-wrap.in  + .bubble-wrap.out {
  margin-top: 6px;
}

.bubble {
  padding: 6px 9px 4px 9px;
  border-radius: 7.5px;
  position: relative;
  word-break: break-word;
  font-size: 14.5px;
  line-height: 1.45;
  box-shadow: 0 1px 2px rgba(0,0,0,0.25);
}

.bubble-wrap.out .bubble {
  background: var(--bubble-out);
  border-bottom-right-radius: 2px;
}
.bubble-wrap.in .bubble {
  background: var(--bubble-in);
  border-bottom-left-radius: 2px;
}

/* tail */
.bubble-wrap.out .bubble::after {
  content: '';
  position: absolute;
  bottom: 0;
  right: -7px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 0 8px 8px;
  border-color: transparent transparent var(--bubble-out) transparent;
}
.bubble-wrap.in .bubble::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: -7px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 8px 8px 0;
  border-color: transparent var(--bubble-in) transparent transparent;
}

.bubble-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: flex-end;
  margin-top: 2px;
  float: right;
  margin-left: 6px;
  margin-bottom: -2px;
}
.bubble-time {
  font-size: 11px;
  color: rgba(233,237,239,0.6);
  white-space: nowrap;
}
.bubble-ticks svg { width: 16px; height: 16px; color: rgba(233,237,239,0.6); display: block; }
.bubble-ticks.read svg { color: #53bdeb; }

/* Reactions */
.bubble-reactions {
  display: flex;
  gap: 2px;
  margin-top: 2px;
}
.reaction-pill {
  background: var(--bg-tertiary);
  border-radius: 10px;
  padding: 2px 5px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 2px;
}

/* ══════════════════════════════════════════
   INPUT BAR
══════════════════════════════════════════ */
.input-bar {
  /* Grid row 3: sizes to its own content, always visible at bottom */
  grid-row: 3;
  display: flex;
  align-items: flex-end;
  padding: 8px 8px calc(8px + var(--safe-bottom));
  gap: 8px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--divider);
  position: relative;
  z-index: 4;
}

.emoji-btn svg { color: var(--text-secondary); }

.input-wrapper {
  flex: 1;
  background: var(--bg-tertiary);
  border-radius: 22px;
  display: flex;
  align-items: flex-end;
  padding: 8px 8px 8px 14px;
  gap: 4px;
  min-height: 44px;
  max-height: 120px;
}

.input-wrapper textarea {
  flex: 1;
  font-size: 15px;
  max-height: 100px;
  overflow-y: auto;
  scrollbar-width: none;
  line-height: 1.4;
}
.input-wrapper textarea::-webkit-scrollbar { display: none; }

.attach-btn svg { color: var(--text-secondary); }

.send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.15s, background 0.15s;
  box-shadow: 0 2px 6px rgba(0,168,132,0.3);
}
.send-btn:active { transform: scale(0.9); }

/* ══════════════════════════════════════════
   EMOJI PICKER
══════════════════════════════════════════ */
.emoji-picker {
  position: absolute; /* inside #app, so it hugs the bottom of the visible area */
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-secondary);
  border-top: 1px solid var(--divider);
  z-index: 50;
  padding-bottom: var(--safe-bottom);
  max-height: 280px;
  transition: transform 0.25s ease;
  transform: translateY(0);
  border-radius: 16px 16px 0 0;
  overflow: hidden;
}
.emoji-picker.hidden {
  transform: translateY(100%);
  pointer-events: none;
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 0;
  overflow-y: auto;
  max-height: 260px;
  padding: 8px 4px;
  scrollbar-width: none;
}
.emoji-grid::-webkit-scrollbar { display: none; }

.emoji-btn-item {
  font-size: 24px;
  padding: 6px;
  text-align: center;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.1s;
  line-height: 1;
}
.emoji-btn-item:active { background: var(--bg-tertiary); }

/* ══════════════════════════════════════════
   CONTEXT MENU
══════════════════════════════════════════ */
.context-menu {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 100;
  background: rgba(0,0,0,0.3);
}
.context-menu.hidden { display: none; }

.ctx-item {
  display: block;
  width: 200px;
  padding: 14px 18px;
  font-size: 15px;
  color: var(--text-primary);
  background: var(--bg-secondary);
  text-align: left;
  border-bottom: 1px solid var(--divider);
  cursor: pointer;
}
.ctx-item:first-child { border-radius: 12px 12px 0 0; }
.ctx-item:last-child { border-radius: 0 0 12px 12px; border-bottom: none; }
.ctx-item:active { background: var(--bg-tertiary); }
.ctx-item.danger { color: #ff4f4f; }

/* ══════════════════════════════════════════
   TOAST
══════════════════════════════════════════ */
.toast {
  position: fixed;
  bottom: calc(var(--bottomnav-h) + var(--safe-bottom) + 12px);
  left: 50%;
  transform: translateX(-50%);
  background: #3b4a54;
  color: var(--text-primary);
  font-size: 14px;
  padding: 10px 18px;
  border-radius: 8px;
  z-index: 200;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  transition: opacity 0.3s;
}
.toast.hidden { display: none; }

/* ══════════════════════════════════════════
   INSTALL OVERLAY (Android)
══════════════════════════════════════════ */
.install-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.88);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  backdrop-filter: blur(6px);
}
.install-overlay.hidden { display: none; }

.install-overlay-card {
  background: var(--bg-secondary);
  border-radius: 20px;
  padding: 32px 24px 24px;
  text-align: center;
  max-width: 320px;
  width: 100%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  border: 1px solid var(--divider);
}
.install-overlay-icon {
  width: 80px;
  height: 80px;
  border-radius: 18px;
  margin: 0 auto 16px;
  display: block;
  box-shadow: 0 8px 24px rgba(0,168,132,0.4);
}
.install-overlay-card h2 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 10px;
  color: var(--text-primary);
}
.install-overlay-card p {
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.6;
  margin-bottom: 24px;
}
.install-overlay-card p strong { color: var(--accent); }
.install-overlay-btn {
  width: 100%;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 700;
  padding: 14px;
  border-radius: 12px;
  margin-bottom: 10px;
  letter-spacing: 0.3px;
  transition: opacity 0.15s;
}
.install-overlay-btn:active { opacity: 0.85; }
.install-overlay-skip {
  width: 100%;
  color: var(--text-muted);
  font-size: 14px;
  padding: 10px;
}

/* ══════════════════════════════════════════
   FULLSCREEN NUDGE BAR
══════════════════════════════════════════ */
.fullscreen-nudge {
  position: fixed;
  bottom: calc(var(--bottomnav-h) + var(--safe-bottom) + 8px);
  left: 12px;
  right: 12px;
  background: #1a2e38;
  border: 1px solid var(--accent);
  border-radius: 12px;
  display: flex;
  align-items: center;
  padding: 10px 14px;
  gap: 10px;
  z-index: 300;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}
.fullscreen-nudge.hidden { display: none; }
.fullscreen-nudge span { flex: 1; font-size: 13px; color: var(--text-secondary); }
#btn-fullscreen {
  background: var(--accent);
  color: white;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 20px;
  white-space: nowrap;
}
#btn-nudge-dismiss { color: var(--text-muted); font-size: 16px; padding: 4px; }

/* ══════════════════════════════════════════
   AVATAR COLORS
══════════════════════════════════════════ */
.av-0  { background: #e57368; }
.av-1  { background: #f5a623; }
.av-2  { background: #4caf50; }
.av-3  { background: #2196f3; }
.av-4  { background: #9c27b0; }
.av-5  { background: #00bcd4; }
.av-6  { background: #ff7043; }
.av-7  { background: #8bc34a; }
.av-8  { background: #607d8b; }
.av-9  { background: #e91e63; }

/* ══════════════════════════════════════════
   TYPING INDICATOR
══════════════════════════════════════════ */
.typing-bubble { display: flex; align-items: center; gap: 4px; padding: 10px 14px; }
.dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-secondary);
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%           { transform: translateY(-5px); }
}

/* ══════════════════════════════════════════
   SCROLLBAR (desktop)
══════════════════════════════════════════ */
@media (pointer: fine) {
  .messages-container::-webkit-scrollbar,
  .chat-list::-webkit-scrollbar { width: 5px; display: block; }
  .messages-container::-webkit-scrollbar-thumb,
  .chat-list::-webkit-scrollbar-thumb { background: var(--bg-tertiary); border-radius: 3px; }
}

/* ══════════════════════════════════════════
   IOS INSTALL GUIDE
══════════════════════════════════════════ */
.ios-guide {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 400;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  backdrop-filter: blur(4px);
}
.ios-guide.hidden { display: none; }
.ios-guide-card {
  background: var(--bg-secondary);
  border-radius: 20px 20px 0 0;
  padding: 24px 24px 16px;
  width: 100%;
  max-width: 480px;
  position: relative;
  animation: slideUp 0.3s ease;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
.ios-guide-close {
  position: absolute; top: 14px; right: 16px;
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--bg-tertiary); color: var(--text-secondary);
  font-size: 16px; display: flex; align-items: center; justify-content: center;
}
.ios-guide-icon {
  width: 60px; height: 60px; border-radius: 14px;
  display: block; margin: 0 auto 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.ios-guide-card h3 { text-align: center; font-size: 18px; margin-bottom: 8px; }
.ios-guide-card p {
  text-align: center; font-size: 14px; color: var(--text-secondary);
  margin-bottom: 16px; line-height: 1.5;
}
.ios-guide-card ol {
  padding-left: 20px; display: flex; flex-direction: column;
  gap: 10px; margin-bottom: 20px;
}
.ios-guide-card ol li { font-size: 15px; color: var(--text-primary); line-height: 1.5; }
.ios-guide-card ol li strong { color: var(--accent); }
.ios-guide-arrow {
  text-align: center; font-size: 28px; color: var(--accent);
  animation: bob 1.2s ease-in-out infinite;
  padding-bottom: calc(var(--safe-bottom) + 4px);
}
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(6px); }
}

/* ══════════════════════════════════════════
   UTILITIES
══════════════════════════════════════════ */
.hidden { display: none !important; }
