/* ========================================
   WhatsApp PWA – Pixel-Perfect Dark Theme
   ======================================== */

:root {
  --bg-app:      #0b141a;
  --bg-panel:    #111b21;
  --bg-header:   #1f2c34;
  --bg-search:   #111b21;
  --bg-input:    #2a3942;
  --bg-outgoing: #005c4b;
  --bg-incoming: #1f2c34;
  --bg-hover:    #182229;
  --bg-active:   #2a3942;
  --text-main:   #e9edef;
  --text-sec:    #8696a0;
  --text-time:   #ffffff99;
  --text-time-in:#8696a0;
  --green:       #00a884;
  --green-dark:  #017561;
  --divider:     #222d34;
  --bubble-tail: 6px;
  --radius:      7.5px;
  --status-bar:  env(safe-area-inset-top, 0px);
  --bottom-safe: env(safe-area-inset-bottom, 0px);
  --font:        -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  font-family: var(--font);
  font-size: 15px;
  color: var(--text-main);
  background: var(--bg-app);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Screens */
.s { display: none; position: fixed; inset: 0; z-index: 1; flex-direction: column; background: var(--bg-app); }
.s.active { display: flex; }

/* Center layout (loading/QR/dc) */
.s-center {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 20px;
}
.wa-logo {
  width: 60px; height: 60px; margin-bottom: 20px;
  border-radius: 50%; background: var(--green);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z'/%3E%3C/svg%3E");
  background-size: 34px; background-repeat: no-repeat; background-position: center;
}
.s-hint { font-size: 16px; color: var(--text-sec); margin-bottom: 6px; text-align: center; }
.s-sub  { font-size: 13px; color: var(--text-sec); margin-bottom: 20px; text-align: center; opacity: .7; }

.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--divider); border-top-color: var(--green);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* QR */
.qr-box {
  background: #fff; padding: 12px; border-radius: 8px;
  width: min(260px, 80vw); aspect-ratio: 1;
  display: flex; align-items: center; justify-content: center;
}
.qr-box img { width: 100%; height: 100%; object-fit: contain; }

/* Shared Header */
.hdr, .chat-hdr {
  background: var(--bg-header);
  padding: 8px 12px;
  padding-top: calc(8px + var(--status-bar));
  display: flex; align-items: center;
  min-height: 56px;
  flex-shrink: 0;
}
.hdr-title {
  font-size: 20px; font-weight: 600; flex: 1;
  color: var(--text-main);
}
.hdr-actions { display: flex; align-items: center; gap: 8px; }
.hdr-btn {
  background: none; border: none; color: var(--text-sec);
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.hdr-btn:active { background: var(--bg-active); }
.ops-badge {
  background: var(--green);
  color: #fff; font-size: 11px; font-weight: 600;
  padding: 2px 6px; border-radius: 10px;
  min-width: 20px; text-align: center;
}

/* Search */
.search-wrap { padding: 6px 10px; background: var(--bg-panel); flex-shrink: 0; }
.search-box {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-input); border-radius: 8px;
  padding: 6px 12px;
}
.search-box input {
  flex: 1; background: none; border: none; outline: none;
  color: var(--text-main); font-size: 15px;
  font-family: var(--font);
}
.search-box input::placeholder { color: var(--text-sec); }

/* === CHAT LIST === */
.chat-list {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.cl-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px;
  cursor: pointer;
  transition: background .15s;
  border-bottom: 1px solid var(--divider);
}
.cl-item:active { background: var(--bg-active); }

/* Avatar */
.ava {
  width: 49px; height: 49px; min-width: 49px;
  border-radius: 50%; background: var(--bg-active);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-sec); font-size: 20px; font-weight: 600;
  overflow: hidden;
}
.ava-sm { width: 40px; height: 40px; min-width: 40px; font-size: 16px; }
.ava img { width: 100%; height: 100%; object-fit: cover; }

.cl-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.cl-top  { display: flex; align-items: baseline; gap: 4px; }
.cl-name {
  flex: 1; font-size: 16px; font-weight: 400;
  color: var(--text-main); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.cl-time { font-size: 12px; color: var(--text-sec); flex-shrink: 0; }
.cl-last {
  font-size: 14px; color: var(--text-sec);
  white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis;
  display: flex; align-items: center; gap: 4px;
}
.cl-unread {
  background: var(--green); color: #fff;
  font-size: 11px; font-weight: 600;
  min-width: 20px; height: 20px;
  border-radius: 50%; display: flex;
  align-items: center; justify-content: center;
  padding: 0 5px; flex-shrink: 0;
}

/* === CHAT HEADER === */
.chat-hdr { gap: 8px; }
.chat-hdr-info { flex: 1; min-width: 0; }
.chat-hdr-name {
  font-size: 16px; font-weight: 500;
  color: var(--text-main);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-hdr-status { font-size: 12px; color: var(--text-sec); }

/* === MESSAGES === */
.msgs-wrap {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  background: var(--bg-app);
  /* WhatsApp doodle-like subtle background */
  background-image: url("data:image/svg+xml,%3Csvg width='200' height='200' xmlns='http://www.w3.org/2000/svg'%3E%3Cdefs%3E%3Cpattern id='p' width='40' height='40' patternUnits='userSpaceOnUse'%3E%3Ccircle cx='20' cy='20' r='1.2' fill='%23ffffff07'/%3E%3C/pattern%3E%3C/defs%3E%3Crect width='200' height='200' fill='url(%23p)'/%3E%3C/svg%3E");
}
.msgs {
  display: flex; flex-direction: column;
  padding: 4px 10px 4px;
  min-height: 100%;
  justify-content: flex-end;
  gap: 1px;
}

/* Date separator */
.date-sep {
  align-self: center;
  background: #182229cc;
  color: var(--text-sec);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 7px;
  margin: 8px 0;
}

/* Single message bubble */
.msg {
  max-width: 75%;
  padding: 4px 7px 2px 9px;
  border-radius: var(--radius);
  margin: 1px 0;
  position: relative;
  word-wrap: break-word;
  overflow-wrap: break-word;
  line-height: 1.35;
  font-size: 14.2px;
}
.msg.has-img {
  padding: 3px 3px 2px 3px;
  overflow: hidden;
}
.msg.has-img .msg-text {
  padding: 1px 6px 0;
}
.msg.has-img .msg-meta {
  margin-right: 4px;
}
.msg.out {
  align-self: flex-end;
  background: var(--bg-outgoing);
  border-top-right-radius: 0;
}
.msg.inc {
  align-self: flex-start;
  background: var(--bg-incoming);
  border-top-left-radius: 0;
}

/* First message in group gets a tail */
.msg.out.tail { border-top-right-radius: 0; }
.msg.inc.tail { border-top-left-radius: 0; }
.msg.out.tail::after {
  content: ''; position: absolute; top: 0; right: -6px;
  width: 0; height: 0;
  border-left: 6px solid var(--bg-outgoing);
  border-top: 6px solid transparent;
}
.msg.inc.tail::after {
  content: ''; position: absolute; top: 0; left: -6px;
  width: 0; height: 0;
  border-right: 6px solid var(--bg-incoming);
  border-top: 6px solid transparent;
}

/* Send name in groups */
.msg-sender {
  font-size: 12.5px; font-weight: 500;
  color: #ffb742;
  margin-bottom: 1px;
}

/* Text */
.msg-text { white-space: pre-wrap; }
.msg-text a { color: #53bdeb; text-decoration: none; }

/* Image */
.msg-img {
  border-radius: 4px;
  width: 100%;
  max-width: 250px;
  max-height: 200px;
  cursor: pointer; display: block;
  background: var(--bg-active);
  object-fit: cover;
}

/* Video */
.msg-video {
  border-radius: 6px; max-width: 100%;
  max-height: 300px;
}

/* Voice/PTT player */
.msg-voice {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 0; min-width: 200px;
}
.mv-play {
  width: 32px; height: 32px; min-width: 32px;
  border-radius: 50%; border: none;
  background: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-main);
}
.mv-play svg { width: 28px; height: 28px; }
.mv-body { flex: 1; display: flex; flex-direction: column; gap: 3px; }
.mv-wave {
  height: 24px;
  display: flex; align-items: center; gap: 1.5px;
}
.mv-wave-bar {
  width: 3px; border-radius: 2px;
  background: var(--text-sec);
  transition: background .15s;
}
.mv-wave-bar.played { background: #34b7f1; }
.mv-dur { font-size: 11px; color: var(--text-time-in); }

/* Meta: time + ticks */
.msg-meta {
  float: right;
  display: flex; align-items: center; gap: 3px;
  margin: 4px 0 -3px 8px;
  font-size: 11px; color: var(--text-time);
}
.msg.inc .msg-meta { color: var(--text-time-in); }

.msg-tick { display: inline-block; }
.tick-sent { color: var(--text-time); }
.tick-delivered { color: var(--text-time); }
.tick-read { color: #53bdeb; }

/* Doc / sticker / location */
.msg-doc {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px;
  background: #00000015; border-radius: 6px;
  cursor: pointer; margin-bottom: 3px;
}
.msg-doc-icon {
  width: 32px; height: 32px; min-width: 32px;
  background: var(--green-dark); border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
}
.msg-doc-icon svg { width: 18px; height: 18px; }
.msg-doc-name { font-size: 13px; color: var(--text-main); }

.msg-sticker { width: 140px; height: 140px; object-fit: contain; }

.msg-location {
  display: flex; align-items: center; gap: 6px;
  color: #53bdeb; text-decoration: none;
}

/* Loading placeholder for media */
.msg-placeholder {
  width: 180px; height: 120px;
  border-radius: 4px;
  background: var(--bg-active);
  display: flex; align-items: center; justify-content: center;
}
.msg-placeholder .mini-spin {
  width: 24px; height: 24px;
  border: 2px solid var(--divider); border-top-color: var(--green);
  border-radius: 50%; animation: spin .8s linear infinite;
}

/* Image overlay loading */
.msg-img-loading {
  position: relative;
}
.msg-img-loading::after {
  content: '';
  position: absolute; inset: 0;
  background: rgba(0,0,0,.3);
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
}

/* === INPUT BAR === */
.input-bar {
  display: flex; align-items: flex-end; gap: 4px;
  padding: 4px 6px;
  padding-bottom: calc(4px + var(--bottom-safe));
  background: var(--bg-panel);
  flex-shrink: 0;
}
.ib-btn {
  width: 42px; height: 42px; min-width: 42px;
  border: none; background: none; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--text-sec);
}
.ib-btn:active { background: var(--bg-active); }
.ib-send { background: var(--green); color: #fff; }
.ib-send:active { background: var(--green-dark); }
.ib-mic { color: var(--text-sec); }
.ib-field-wrap {
  flex: 1;
  background: var(--bg-input);
  border-radius: 21px;
  padding: 6px 14px;
  display: flex; align-items: center;
}
.ib-field-wrap textarea {
  flex: 1; background: none; border: none; outline: none;
  color: var(--text-main); font-size: 15px;
  font-family: var(--font);
  line-height: 1.35;
  resize: none; max-height: 120px;
  min-height: 24px;
}
.ib-field-wrap textarea::placeholder { color: var(--text-sec); }

/* Voice recording bar */
.voice-bar {
  display: none; align-items: center; gap: 8px;
  padding: 4px 8px;
  padding-bottom: calc(4px + var(--bottom-safe));
  background: var(--bg-panel);
  flex-shrink: 0;
}
.voice-bar.active { display: flex; }
.vc-indicator {
  display: flex; align-items: center; gap: 6px;
}
.vc-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: #ea4335; animation: pulse-dot 1s ease-in-out infinite;
}
@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50% { opacity: .3; }
}
.vc-time { font-size: 15px; color: var(--text-sec); flex: 1; }

/* === IMAGE LIGHTBOX === */
.lightbox {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.92);
  display: flex; align-items: center; justify-content: center;
  padding: 40px 10px;
}
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; }
.lb-close {
  position: absolute; top: 12px; right: 12px;
  width: 40px; height: 40px; border-radius: 50%;
  background: rgba(255,255,255,.1); border: none;
  color: #fff; font-size: 22px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

/* Scrollbar */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-thumb { background: var(--divider); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

/* === PWA INSTALL BANNER === */
.pwa-banner {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 200;
  background: var(--bg-header);
  border-top: 1px solid var(--divider);
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--bottom-safe));
  flex-direction: column;
  gap: 10px;
  animation: slideUp .3s ease-out;
}
.pwa-banner.show { display: flex; }
@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
.pwa-banner-inner {
  display: flex; align-items: center; gap: 12px;
}
.pwa-icon {
  width: 48px; height: 48px; min-width: 48px;
  border-radius: 12px; overflow: hidden;
  background: var(--bg-active);
}
.pwa-icon img { width: 100%; height: 100%; object-fit: cover; }
.pwa-info { flex: 1; min-width: 0; }
.pwa-title {
  font-size: 15px; font-weight: 600;
  color: var(--text-main);
}
.pwa-desc {
  font-size: 13px; color: var(--text-sec);
  margin-top: 2px;
}
.pwa-close {
  background: none; border: none; color: var(--text-sec);
  font-size: 24px; line-height: 1;
  width: 32px; height: 32px; min-width: 32px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; border-radius: 50%;
}
.pwa-close:active { background: var(--bg-active); }
.pwa-install-btn {
  width: 100%;
  padding: 10px;
  border: none; border-radius: 8px;
  background: var(--green);
  color: #fff;
  font-size: 15px; font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: background .15s;
}
.pwa-install-btn:active { background: var(--green-dark); }
.pwa-ios {
  background: var(--bg-input);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--text-sec);
  line-height: 1.5;
}
.pwa-ios b { color: var(--text-main); }

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