/* ============================================================================
   WhatsApp module — dedicated design layer (frontend/js/whatsapp.js)

   A self-contained WhatsApp Web / WhatsApp Mobile styled surface. Kept in its
   own file rather than appended to the 4500-line style.css so the base design
   system and its documented bug-fix rules stay undisturbed. Built on the
   existing design-system tokens (--surface, --border, --text, --radius-*, …)
   with WhatsApp-authentic chat surfaces (wallpaper, bubble greens, tick blue)
   layered on top via --wa-* tokens so the chat area reads as a real WhatsApp
   client while the rest of the CRM chrome stays CRM-branded.

   Dark mode follows this project's convention: light values on the base
   selector, overridden under [data-theme="dark"] (explicit toggle, persisted
   in localStorage — NOT prefers-color-scheme).
   ========================================================================= */

:root {
  --wa-bg:            #efeae2;   /* chat wallpaper (light) */
  --wa-bg-pattern:    rgba(0,0,0,0.02);
  --wa-panel:         var(--surface);
  --wa-panel-2:       #f0f2f5;   /* header / search bar strip (light) */
  --wa-hover:         #f5f6f6;
  --wa-active:        #f0f2f5;
  --wa-divider:       #e9edef;
  --wa-out-bubble:    #d9fdd3;   /* outgoing (mine) */
  --wa-in-bubble:     #ffffff;   /* incoming (theirs) */
  --wa-bubble-text:   #111b21;
  --wa-meta:          #667781;   /* time / tick grey */
  --wa-tick-read:     #53bdeb;   /* WhatsApp read blue */
  --wa-accent:        #00a884;   /* WhatsApp Web teal-green */
  --wa-accent-strong: #017561;
  --wa-text:          #111b21;
  --wa-text-2:        #3b4a54;
  --wa-text-muted:    #667781;
  --wa-danger:        #ea0038;
  --wa-badge:         #25d366;   /* unread pill */
  --wa-header-h:      59px;
  --wa-shadow-bubble: 0 1px 0.5px rgba(11,20,26,0.13);
  --wa-shadow-pop:    0 2px 10px rgba(11,20,26,0.16);
}

[data-theme="dark"] {
  --wa-bg:            #0b141a;
  --wa-bg-pattern:    rgba(255,255,255,0.02);
  --wa-panel:         #111b21;
  --wa-panel-2:       #202c33;
  --wa-hover:         #202c33;
  --wa-active:        #2a3942;
  --wa-divider:       #222d34;
  --wa-out-bubble:    #005c4b;
  --wa-in-bubble:     #202c33;
  --wa-bubble-text:   #e9edef;
  --wa-meta:          #8696a0;
  --wa-tick-read:     #53bdeb;
  --wa-accent:        #00a884;
  --wa-accent-strong: #06cf9c;
  --wa-text:          #e9edef;
  --wa-text-2:        #d1d7db;
  --wa-text-muted:    #8696a0;
  --wa-danger:        #f15c6d;
  --wa-badge:         #00a884;
  --wa-shadow-bubble: 0 1px 0.5px rgba(0,0,0,0.3);
  --wa-shadow-pop:    0 2px 12px rgba(0,0,0,0.5);
}

/* ---------- Full-bleed takeover of the view area ---------- */
/* showView() in app.js toggles body.wa-fullbleed. The view becomes a fixed
   region between the topbar and (on mobile) the bottom tab bar, so the module
   uses the entire screen like WhatsApp Web, with zero dead space. */
body.wa-fullbleed main { padding: 0 !important; overflow: hidden; }

body.wa-fullbleed #view-whatsapp:not(.hidden) {
  position: fixed;
  top: var(--topbar-height, 56px);
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;                 /* below the mobile topbar(86) + tab bar(80) */
  background: var(--wa-panel);
  overflow: hidden;
}
body.wa-fullbleed.internal-call-bar-active #view-whatsapp:not(.hidden) {
  top: calc(var(--topbar-height, 56px) + 60px);
}
/* Below 640px the app shows a fixed bottom tab bar (56px + safe-area) —
   stop the WhatsApp *list* view above it so its content isn't hidden. Inside
   a thread the tab bar is removed entirely (body.wa-thread-open, below) and
   the view runs to the screen bottom so the composer sits on the keyboard. */
@media (max-width: 640px) {
  body.wa-fullbleed #view-whatsapp:not(.hidden) {
    bottom: calc(56px + env(safe-area-inset-bottom, 0));
  }
  body.wa-thread-open #view-whatsapp:not(.hidden) { bottom: 0; }
}
/* Native chat apps show no app tab bar inside a conversation. Hiding it also
   kills the iOS "fixed bottom bar rides up with the keyboard" bug. */
@media (max-width: 900px) {
  body.wa-thread-open .mobile-tab-bar { display: none !important; }
}
#whatsapp-content { height: 100%; }

/* ---------- Shell: left list pane + right conversation pane ---------- */
.wa-shell {
  display: flex;
  height: 100%;
  background: var(--wa-panel);
  overflow: hidden;
  font-size: var(--fs-14);
  color: var(--wa-text);
}
.wa-left {
  width: 32%;
  min-width: 320px;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  background: var(--wa-panel);
  border-right: 1px solid var(--wa-divider);
}
.wa-right {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--wa-bg);
}
/* Subtle doodle-style wallpaper texture without an external asset (CSP-safe). */
.wa-right::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(var(--wa-bg-pattern) 1px, transparent 1px),
    radial-gradient(var(--wa-bg-pattern) 1px, transparent 1px);
  background-size: 42px 42px, 42px 42px;
  background-position: 0 0, 21px 21px;
  opacity: 0.9;
}

/* ---------- Left pane: header ---------- */
.wa-left-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  height: var(--wa-header-h);
  padding: 0 16px;
  background: var(--wa-panel-2);
  flex-shrink: 0;
}
.wa-left-title { font-weight: var(--fw-bold); font-size: var(--fs-17); color: var(--wa-text); }
.wa-left-actions { display: flex; align-items: center; gap: 2px; }
.wa-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--wa-text-muted);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
  flex-shrink: 0;
}
.wa-icon-btn:hover { background: var(--wa-hover); color: var(--wa-text-2); }
.wa-icon-btn svg { width: 20px; height: 20px; }

/* ---------- Left pane: tabs ---------- */
.wa-tabs {
  display: flex;
  background: var(--wa-panel);
  border-bottom: 1px solid var(--wa-divider);
  flex-shrink: 0;
}
.wa-tab {
  flex: 1;
  padding: 12px 8px;
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  cursor: pointer;
  font-size: var(--fs-13);
  font-weight: var(--fw-semibold);
  color: var(--wa-text-muted);
  letter-spacing: 0.02em;
  transition: color 0.12s ease, border-color 0.12s ease;
}
.wa-tab:hover { color: var(--wa-text-2); }
.wa-tab.active { color: var(--wa-accent); border-bottom-color: var(--wa-accent); }
.wa-tab-count {
  display: inline-block;
  min-width: 16px;
  padding: 0 4px;
  margin-left: 4px;
  border-radius: var(--radius-pill);
  background: var(--wa-badge);
  color: #fff;
  font-size: 10px;
  line-height: 16px;
  font-weight: var(--fw-bold);
  vertical-align: middle;
}

/* ---------- Left pane: search + filters ---------- */
.wa-search-row {
  padding: 8px 12px;
  background: var(--wa-panel);
  flex-shrink: 0;
}
.wa-search {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--wa-panel-2);
  border-radius: var(--radius-pill);
  padding: 6px 14px;
}
.wa-search svg { width: 16px; height: 16px; color: var(--wa-text-muted); flex-shrink: 0; }
.wa-search input {
  flex: 1;
  border: none;
  background: none;
  outline: none;
  font-size: var(--fs-14);
  color: var(--wa-text);
  min-width: 0;
}
.wa-search input::placeholder { color: var(--wa-text-muted); }

.wa-filters {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px 8px;
  flex-wrap: wrap;
  flex-shrink: 0;
}
.wa-filter-chip {
  border: 1px solid var(--wa-divider);
  background: var(--wa-panel);
  color: var(--wa-text-muted);
  border-radius: var(--radius-pill);
  padding: 4px 10px;
  font-size: var(--fs-12);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}
.wa-filter-chip:hover { border-color: var(--border-strong); }
.wa-filter-chip.active {
  background: var(--tone-success-bg);            /* fallback */
  background: color-mix(in srgb, var(--wa-accent) 16%, transparent);
  color: var(--wa-accent-strong);
  border-color: var(--wa-accent);               /* fallback */
  border-color: color-mix(in srgb, var(--wa-accent) 40%, transparent);
}
[data-theme="dark"] .wa-filter-chip.active { color: var(--wa-accent-strong); }

/* ---------- Conversation / contact list ---------- */
.wa-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior-y: contain; /* see style.css's html/body overscroll-behavior rule */
  background: var(--wa-panel);
}
.wa-list-empty {
  padding: 40px 24px;
  text-align: center;
  color: var(--wa-text-muted);
  font-size: var(--fs-13);
}
.wa-row {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 10px 13px 10px 15px;
  cursor: pointer;
  border-bottom: 1px solid var(--wa-divider);
  transition: background 0.1s ease;
}
.wa-row:hover { background: var(--wa-hover); }
.wa-row.active { background: var(--wa-active); }

.wa-avatar {
  position: relative;
  width: 49px;
  height: 49px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: var(--fw-semibold);
  font-size: var(--fs-15);
  letter-spacing: 0.02em;
  user-select: none;
  overflow: hidden;
}
.wa-avatar svg { width: 55%; height: 55%; opacity: 0.9; }
.wa-avatar--sm { width: 40px; height: 40px; font-size: var(--fs-13); }

.wa-row-main { flex: 1; min-width: 0; }
.wa-row-line1 {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}
.wa-row-name {
  font-size: var(--fs-15);
  color: var(--wa-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.wa-row.unread .wa-row-name { font-weight: var(--fw-semibold); }
.wa-row-time {
  font-size: var(--fs-12);
  color: var(--wa-text-muted);
  flex-shrink: 0;
}
.wa-row.unread .wa-row-time { color: var(--wa-accent-strong); }
.wa-row-line2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 3px;
}
.wa-row-preview {
  font-size: var(--fs-13);
  color: var(--wa-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 4px;
}
.wa-row.unread .wa-row-preview { color: var(--wa-text-2); }
.wa-row-preview .wa-mini-tick { flex-shrink: 0; }
.wa-row-badges { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.wa-unread-pill {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--radius-pill);
  background: var(--wa-badge);
  color: #fff;
  font-size: var(--fs-12);
  font-weight: var(--fw-bold);
  display: flex;
  align-items: center;
  justify-content: center;
}
.wa-row-line3 {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
}
.wa-crm-chip {
  font-size: 10.5px;
  padding: 1px 7px;
  border-radius: var(--radius-pill);
  background: var(--tone-info-bg);
  color: var(--tone-info-fg);
  font-weight: var(--fw-medium);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60%;
}
.wa-crm-chip--none { background: var(--tone-neutral-bg); color: var(--tone-neutral-fg); }
.wa-crm-chip--wa { background: var(--tone-success-bg); color: var(--tone-success-fg); }
.wa-assignee-tag { font-size: 10.5px; color: var(--wa-text-muted); white-space: nowrap; }
.wa-status-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.wa-status-dot--pending { background: var(--tone-warning-fg); }
.wa-status-dot--resolved { background: var(--tone-success-fg); }

/* ---------- Right pane: empty placeholder (WhatsApp Web style) ---------- */
.wa-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px;
  position: relative;
  z-index: 1;
  border-bottom: 6px solid var(--wa-accent);
}
.wa-placeholder svg { width: 240px; max-width: 60%; opacity: 0.55; margin-bottom: 28px; }
.wa-placeholder h3 { margin: 0 0 10px; font-weight: 300; font-size: 26px; color: var(--wa-text-2); }
.wa-placeholder p { margin: 0; color: var(--wa-text-muted); font-size: var(--fs-14); max-width: 460px; line-height: var(--lh-normal); }

/* ---------- Thread: header ---------- */
.wa-thread { display: flex; flex-direction: column; height: 100%; position: relative; z-index: 1; min-width: 0; }
.wa-thread-header {
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--wa-header-h);
  padding: 0 16px;
  background: var(--wa-panel-2);
  flex-shrink: 0;
}
.wa-back-btn { display: none; }
.wa-thread-id { flex: 1; min-width: 0; cursor: pointer; }
.wa-thread-name {
  font-size: var(--fs-15);
  font-weight: var(--fw-semibold);
  color: var(--wa-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.wa-thread-sub {
  font-size: var(--fs-12);
  color: var(--wa-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 5px;
}
.wa-thread-sub .wa-link { color: var(--wa-accent-strong); cursor: pointer; }
.wa-thread-sub .wa-link:hover { text-decoration: underline; }
.wa-thread-sub .wa-typing { color: var(--wa-accent-strong); font-style: italic; }
.wa-sub-dot { margin: 0 5px; opacity: 0.55; }
.wa-assignee-chip { color: var(--wa-accent-strong); font-weight: var(--fw-medium); white-space: nowrap; }
.wa-assignee-chip--none { color: var(--wa-text-muted); font-weight: 400; }
.wa-status-chip {
  text-transform: capitalize;
  padding: 0 6px;
  border-radius: var(--radius-sm);
  font-size: 10.5px;
  font-weight: var(--fw-semibold);
}
.wa-status-chip--pending { background: var(--tone-warning-bg); color: var(--tone-warning-fg); }
.wa-status-chip--resolved { background: var(--tone-success-bg); color: var(--tone-success-fg); }

/* Header action buttons (call + ⋯ menu) */
.wa-thread-actions { display: flex; align-items: center; gap: 2px; flex-shrink: 0; }
.wa-hdr-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--wa-text-muted);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.wa-hdr-btn:hover { background: var(--wa-hover); color: var(--wa-text-2); }
.wa-hdr-btn svg { width: 21px; height: 21px; }
.wa-hdr-btn--disabled { opacity: 0.4; }
.wa-hdr-btn--disabled:hover { background: transparent; color: var(--wa-text-muted); }

/* Header overflow menu */
.wa-menu {
  position: absolute;
  top: calc(var(--wa-header-h) - 6px);
  right: 10px;
  width: 232px;
  max-height: 70vh;
  overflow-y: auto;
  padding: 6px 0;
}
.wa-menu-label {
  padding: 6px 14px 2px;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--wa-text-muted);
}
.wa-menu-item, .wa-menu-sub-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  cursor: pointer;
  font-size: var(--fs-13);
  color: var(--wa-text);
  padding: 9px 14px;
}
.wa-menu-sub-item { padding: 7px 14px 7px 22px; font-size: var(--fs-13); }
.wa-menu-item:hover, .wa-menu-sub-item:hover { background: var(--wa-hover); }
.wa-menu-sep { height: 1px; background: var(--wa-divider); margin: 5px 0; }

/* Contacts segmented toggle */
.wa-contact-scope {
  display: flex;
  gap: 4px;
  padding: 6px 12px 8px;
  background: var(--wa-panel);
  flex-shrink: 0;
}
.wa-seg {
  flex: 1;
  border: 1px solid var(--wa-divider);
  background: var(--wa-panel);
  color: var(--wa-text-muted);
  border-radius: var(--radius-pill);
  padding: 5px 8px;
  font-size: var(--fs-12);
  font-weight: var(--fw-medium);
  cursor: pointer;
}
.wa-seg.active {
  background: var(--wa-accent);
  border-color: var(--wa-accent);
  color: #fff;
}
/* Call marker in the thread timeline */
.wa-call-marker {
  align-self: center;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin: 8px 0;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  background: var(--wa-panel-2);
  color: var(--wa-text-muted);
  font-size: var(--fs-12);
  box-shadow: var(--wa-shadow-bubble);
}
.wa-call-marker svg { width: 14px; height: 14px; }
.wa-call-marker-time { opacity: 0.7; }

/* ===================== WhatsApp-style call screen ===================== */
/* One global overlay for every call state (incoming ring, outgoing, connected,
   ended). Fixed + high z-index so it floats over the whole CRM on any view. */
.wa-call-screen {
  position: fixed;
  inset: 0;
  z-index: 5000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #0b141a 0%, #111b21 100%);
}
.wa-call-panel {
  width: min(420px, 100%);
  min-height: 100%;
  padding: clamp(32px, 9vh, 80px) 28px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  color: #e9edef;
  text-align: center;
}
.wa-call-who { display: flex; flex-direction: column; align-items: center; gap: 6px; margin-top: 6vh; }
.wa-call-avatar-xl .wa-avatar {
  width: 116px; height: 116px; font-size: 42px; margin: 0 auto 12px;
  box-shadow: 0 0 0 4px rgba(255,255,255,0.08);
}
.wa-call-screen .wa-call-name { font-size: var(--fs-22, 22px); font-weight: var(--fw-semibold); color: #fff; }
.wa-call-screen .wa-call-sub { font-size: var(--fs-13); color: rgba(233,237,239,0.6); }
.wa-call-status {
  margin-top: 10px;
  font-size: var(--fs-14);
  color: rgba(233,237,239,0.75);
  letter-spacing: 0.02em;
  min-height: 20px;
}
.wa-call-timer {
  margin-top: 10px;
  font-size: var(--fs-17);
  color: #7fe3c0;
  font-variant-numeric: tabular-nums;
}
.wa-call-screen--incoming_ringing .wa-call-status,
.wa-call-screen--outgoing_ringing .wa-call-status { animation: wa-call-blink 1.4s ease-in-out infinite; }
@keyframes wa-call-blink { 50% { opacity: 0.45; } }

.wa-call-hint {
  font-size: var(--fs-12);
  color: rgba(233,237,239,0.65);
  line-height: var(--lh-normal);
  max-width: 300px;
  margin: 0 auto 8px;
}

/* Control rows */
.wa-call-controls {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 22px;
  margin-top: 24px;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.wa-call-controls--incoming { gap: 56px; }

.wa-call-ctl {
  border: none;
  background: none;
  color: rgba(233,237,239,0.85);
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  font-size: var(--fs-11);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.wa-call-ctl svg {
  width: 54px; height: 54px;
  padding: 15px;
  border-radius: 50%;
  background: rgba(255,255,255,0.14);
  box-sizing: border-box;
}
.wa-call-ctl.active svg { background: #e9edef; color: #111b21; }
.wa-call-ctl--end svg { background: #ea0038; color: #fff; }

/* Big Accept / Decline buttons (incoming ring, permission prompt) */
.wa-call-btn-lg {
  border: none;
  background: none;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 9px;
  font-size: var(--fs-12);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.wa-call-btn-lg svg {
  width: 66px; height: 66px;
  padding: 19px;
  border-radius: 50%;
  color: #fff;
  box-sizing: border-box;
}
.wa-call-btn-accept svg { background: #00a884; animation: wa-call-pulse 1.5s ease-in-out infinite; }
.wa-call-btn-decline svg { background: #ea0038; }
.wa-call-btn-lg span { color: rgba(233,237,239,0.8); }
@keyframes wa-call-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(0,168,132,0.55); }
  50% { box-shadow: 0 0 0 16px rgba(0,168,132,0); }
}

/* Desktop: a centered card rather than a full black screen */
@media (min-width: 720px) {
  .wa-call-screen { background: rgba(11,20,26,0.75); backdrop-filter: blur(4px); }
  .wa-call-panel {
    min-height: 0;
    background: linear-gradient(180deg, #111b21, #0b141a);
    border-radius: 16px;
    box-shadow: 0 24px 60px rgba(0,0,0,0.5);
    padding: 40px 40px 32px;
  }
  .wa-call-who { margin-top: 8px; }
}

.wa-assigned-banner {
  padding: 7px 16px;
  background: var(--tone-warning-bg);
  color: var(--tone-warning-fg);
  font-size: var(--fs-12);
  text-align: center;
  flex-shrink: 0;
}

/* ---------- Thread: messages ---------- */
.wa-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior-y: contain; /* see style.css's html/body overscroll-behavior rule */
  padding: 12px 6% 18px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scroll-behavior: auto;
}
.wa-load-older {
  align-self: center;
  margin: 6px 0 10px;
  border: none;
  background: var(--wa-panel-2);
  color: var(--wa-text-2);
  border-radius: var(--radius-pill);
  padding: 5px 14px;
  font-size: var(--fs-12);
  cursor: pointer;
  box-shadow: var(--wa-shadow-bubble);
}
.wa-load-older[disabled] { opacity: 0.5; cursor: default; }

.wa-day-sep {
  align-self: center;
  margin: 12px 0;
  background: var(--wa-panel-2);
  color: var(--wa-text-muted);
  font-size: 11.5px;
  font-weight: var(--fw-medium);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 5px 12px;
  border-radius: var(--radius-sm);
  box-shadow: var(--wa-shadow-bubble);
}

.wa-msg {
  display: flex;
  max-width: 65%;
  margin-top: 2px;
}
.wa-msg.out { align-self: flex-end; }
.wa-msg.in  { align-self: flex-start; }
.wa-msg.group-start { margin-top: 10px; }

.wa-bubble {
  position: relative;
  border-radius: 8px;
  padding: 6px 9px 8px;
  background: var(--wa-in-bubble);
  color: var(--wa-bubble-text);
  box-shadow: var(--wa-shadow-bubble);
  max-width: 100%;
  min-width: 92px;
}
.wa-msg.out .wa-bubble { background: var(--wa-out-bubble); }
/* Tail — only on the first bubble of a run. */
.wa-msg.group-start .wa-bubble::before {
  content: "";
  position: absolute;
  top: 0;
  width: 8px;
  height: 13px;
  background: inherit;
}
.wa-msg.in.group-start .wa-bubble::before  { left: -7px;  border-bottom-right-radius: 12px; clip-path: polygon(100% 0, 100% 100%, 0 0); }
.wa-msg.out.group-start .wa-bubble::before { right: -7px; border-bottom-left-radius: 12px;  clip-path: polygon(0 0, 100% 0, 0 100%); }

.wa-bubble-text {
  font-size: var(--fs-14);
  line-height: 1.35;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.wa-bubble-text a { color: var(--wa-accent-strong); text-decoration: underline; }
.wa-bubble-meta {
  float: right;
  margin: 4px 0 -2px 10px;
  font-size: 11px;
  color: var(--wa-meta);
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  column-gap: 4px;
  row-gap: 0;
  max-width: 100%;
  user-select: none;
  white-space: nowrap;
}
.wa-msg.out .wa-bubble-meta { color: var(--wa-meta); }   /* fallback */
.wa-msg.out .wa-bubble-meta { color: color-mix(in srgb, var(--wa-bubble-text) 55%, transparent); }
[data-theme="dark"] .wa-msg.out .wa-bubble-meta { color: rgba(233,237,239,0.6); }
.wa-meta-time { flex: 0 0 auto; }
/* "Sent by <employee>" - lives inside the meta line so it never affects the
   bubble width or the conversation alignment. Truncates on a long name. */
.wa-bubble-sender {
  display: inline-block;
  max-width: min(150px, 42vw);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
  font-weight: var(--fw-medium, 500);
  opacity: 0.92;
}
.wa-meta-sep { flex: 0 0 auto; opacity: 0.55; }
/* legacy - the old standalone sender line is gone; keep a no-op so any cached
   markup mid-deploy doesn't reserve space. */
.wa-msg-sender { display: none; }
.wa-bubble-agent { display: none; }

.wa-tick { display: inline-flex; }
.wa-tick svg { width: 16px; height: 11px; }
.wa-tick--read svg { color: var(--wa-tick-read); }
.wa-tick--failed { color: var(--wa-danger); }

.wa-bubble-err {
  margin-top: 4px;
  font-size: var(--fs-12);
  color: var(--wa-danger);
  display: flex;
  align-items: center;
  gap: 8px;
}
.wa-retry-btn {
  border: 1px solid currentColor;
  background: none;
  color: inherit;
  border-radius: var(--radius-sm);
  padding: 1px 8px;
  font-size: 11px;
  cursor: pointer;
}

.wa-quote {
  border-left: 3px solid var(--wa-accent);
  background: rgba(0,0,0,0.04);
  border-radius: 4px;
  padding: 4px 8px;
  margin-bottom: 4px;
  font-size: var(--fs-12);
  color: var(--wa-text-muted);
}
[data-theme="dark"] .wa-quote { background: rgba(255,255,255,0.06); }
.wa-quote-name { display: block; color: var(--wa-accent-strong); font-weight: var(--fw-semibold); }

.wa-tpl-badge {
  display: inline-block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: var(--tone-accent-bg);
  color: var(--tone-accent-fg);
  border-radius: 4px;
  padding: 1px 5px;
  margin-bottom: 3px;
}

/* Media inside bubbles */
.wa-media-img {
  display: block;
  max-width: 330px;
  width: 100%;
  max-height: 400px;
  object-fit: cover;
  border-radius: 6px;
  cursor: pointer;
  background: var(--wa-panel-2);
}
.wa-media-video { display: block; max-width: 330px; width: 100%; max-height: 420px; border-radius: 6px; background: #000; }
.wa-media-audio { width: 260px; max-width: 100%; margin: 2px 0; display: block; }

/* Media load wrapper: skeleton shown until the element loads, error card on failure */
.wa-media-wrap { position: relative; min-width: 140px; }
.wa-media-wrap .wa-media-img,
.wa-media-wrap .wa-media-video { display: block; }
.wa-media-skel {
  display: none;
  align-items: center;
  justify-content: center;
  width: 220px;
  max-width: 60vw;
  height: 160px;
  border-radius: 6px;
  background: var(--wa-panel-2);
}
.wa-media-wrap.wa-media-loading .wa-media-skel { display: flex; }
.wa-media-wrap.wa-media-loading [data-media-el] { position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px; }
.wa-media-err {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 6px;
  background: var(--tone-danger-bg, rgba(234,0,56,0.08));
  color: var(--tone-danger-fg, #c0002e);
  font-size: var(--fs-12);
  max-width: 300px;
}
.wa-media-err svg { width: 16px; height: 16px; flex-shrink: 0; }
.wa-media-retry {
  border: 1px solid currentColor;
  background: transparent;
  color: inherit;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: var(--fs-12);
  cursor: pointer;
}

/* Sticker: no bubble chrome, just the image */
.wa-media-sticker { max-width: 130px; max-height: 130px; object-fit: contain; background: transparent; }
.wa-bubble--sticker { background: transparent !important; box-shadow: none !important; padding: 0 !important; }
.wa-bubble--sticker .wa-bubble-meta { color: var(--wa-text-muted); }

/* Document card (WhatsApp-style) */
.wa-media-doc {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: rgba(0,0,0,0.04);
  border-radius: 6px;
  text-decoration: none;
  color: var(--wa-text);
  min-width: 230px;
  max-width: 300px;
}
[data-theme="dark"] .wa-media-doc { background: rgba(255,255,255,0.05); }
.wa-media-doc svg { width: 26px; height: 26px; flex-shrink: 0; color: var(--wa-accent-strong); }
.wa-media-doc-ico { display: flex; }
.wa-media-doc-meta { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.wa-media-doc-name { font-size: var(--fs-13); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wa-media-doc-sub { font-size: var(--fs-11); color: var(--wa-text-muted); }
.wa-media-doc-dl { color: var(--wa-text-muted); flex-shrink: 0; }
.wa-media-loading {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--fs-12);
  color: var(--wa-text-muted);
  padding: 8px 4px;
}
.wa-media-caption { margin-top: 4px; }
.wa-bubble-metarow { display: flow-root; text-align: right; min-height: 15px; }

/* Forwarded label + click-to-WhatsApp referral chip */
.wa-forwarded {
  font-size: var(--fs-11);
  color: var(--wa-text-muted);
  font-style: italic;
  margin-bottom: 2px;
}
.wa-referral-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: var(--fs-11);
  color: var(--wa-accent-strong);
  text-decoration: none;
  background: rgba(0,0,0,0.04);
  border-radius: 6px;
  padding: 4px 8px;
  margin-bottom: 4px;
  max-width: 100%;
}
[data-theme="dark"] .wa-referral-chip { background: rgba(255,255,255,0.06); }
.wa-quote--stub { opacity: 0.75; }
.wa-sys-note--unsupported { font-style: italic; }

/* ---------- Structured message types (location / contacts / reaction / system) ---------- */
.wa-loc-card {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  background: rgba(0,0,0,0.04);
  border-radius: 8px;
  text-decoration: none;
  color: var(--wa-text);
  min-width: 220px;
  max-width: 300px;
}
[data-theme="dark"] .wa-loc-card { background: rgba(255,255,255,0.05); }
.wa-loc-pin svg { width: 22px; height: 22px; color: var(--wa-accent-strong); flex-shrink: 0; }
.wa-loc-txt { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.wa-loc-txt b { font-size: var(--fs-13); }
.wa-loc-txt span { font-size: var(--fs-12); color: var(--wa-text-muted); overflow: hidden; text-overflow: ellipsis; }

.wa-contact-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(0,0,0,0.04);
  border-radius: 8px;
  min-width: 220px;
  max-width: 300px;
  margin-bottom: 2px;
}
[data-theme="dark"] .wa-contact-card { background: rgba(255,255,255,0.05); }
.wa-contact-txt { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.wa-contact-txt b { font-size: var(--fs-13); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wa-contact-txt span { font-size: var(--fs-12); color: var(--wa-text-muted); }

.wa-reaction-line {
  text-align: center;
  font-size: var(--fs-12);
  color: var(--wa-text-muted);
  margin: 2px 0 6px;
}
.wa-unsupported-line {
  font-size: var(--fs-11);
  opacity: 0.6;
  font-style: italic;
}

.wa-sys-note {
  font-size: var(--fs-13);
  color: var(--wa-text);
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ---------- Blocked contact ---------- */
.wa-blocked-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  font-size: var(--fs-13);
  font-weight: 600;
  color: #b3261e;
  background: rgba(179,38,30,0.08);
  border-bottom: 1px solid rgba(179,38,30,0.2);
}
.wa-blocked-banner svg { width: 16px; height: 16px; flex-shrink: 0; }
[data-theme="dark"] .wa-blocked-banner { color: #f2b8b5; background: rgba(242,184,181,0.1); border-bottom-color: rgba(242,184,181,0.22); }

.wa-composer--blocked {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  font-size: var(--fs-13);
  color: var(--wa-text-muted);
  background: var(--wa-panel-2);
}
.wa-composer--blocked .wa-composer-blocked-icon svg { width: 18px; height: 18px; color: #b3261e; }

.wa-row--blocked .wa-avatar { filter: grayscale(1); opacity: 0.7; }
.wa-row--blocked .wa-row-name { color: var(--wa-text-muted); }
.wa-chip--blocked {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  color: #b3261e;
  background: rgba(179,38,30,0.1);
  border-radius: 4px;
  padding: 0 5px;
  margin-right: 4px;
}
[data-theme="dark"] .wa-chip--blocked { color: #f2b8b5; background: rgba(242,184,181,0.14); }

.wa-menu-item--danger { color: #b3261e; }
[data-theme="dark"] .wa-menu-item--danger { color: #f2b8b5; }

/* ---------- Thread: composer ---------- */
.wa-composer {
  flex-shrink: 0;
  background: var(--wa-panel-2);
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom, 0));
}
.wa-composer-closed {
  background: var(--tone-warning-bg);
  color: var(--tone-warning-fg);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: var(--fs-13);
  margin-bottom: 8px;
  text-align: center;
}
.wa-attach-preview {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--wa-panel);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 8px;
}
.wa-attach-preview img { width: 46px; height: 46px; object-fit: cover; border-radius: 6px; flex-shrink: 0; }
.wa-attach-preview .wa-attach-info { flex: 1; min-width: 0; }
.wa-attach-preview .wa-attach-name { font-size: var(--fs-13); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wa-attach-preview .wa-attach-size { font-size: var(--fs-11); color: var(--wa-text-muted); }
.wa-attach-caption {
  flex: 2;
  border: 1px solid var(--wa-divider);
  border-radius: var(--radius-pill);
  padding: 6px 12px;
  font-size: var(--fs-13);
  background: var(--wa-panel-2);
  color: var(--wa-text);
  min-width: 0;
}

.wa-composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.wa-composer-input-wrap {
  flex: 1;
  display: flex;
  align-items: flex-end;
  background: var(--wa-panel);
  border-radius: 22px;
  padding: 4px 6px 4px 8px;
  min-width: 0;
}
.wa-composer-textarea {
  flex: 1;
  border: none;
  background: none;
  outline: none;
  resize: none;
  font-family: inherit;
  font-size: var(--fs-15);
  line-height: 1.4;
  color: var(--wa-text);
  padding: 8px 6px;
  max-height: 120px;
  min-height: 24px;
  overflow-y: auto;
}
.wa-composer-textarea::placeholder { color: var(--wa-text-muted); }
.wa-send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--wa-accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.12s ease, transform 0.08s ease;
}
.wa-send-btn:hover { background: var(--wa-accent-strong); }
.wa-send-btn:active { transform: scale(0.92); }
.wa-send-btn:disabled { opacity: 0.55; cursor: default; }
.wa-send-btn svg { width: 22px; height: 22px; }

/* Attach + emoji popovers */
.wa-pop {
  position: absolute;
  bottom: 60px;
  background: var(--wa-panel);
  border: 1px solid var(--wa-divider);
  border-radius: var(--radius);
  box-shadow: var(--wa-shadow-pop);
  z-index: 20;
  overflow: hidden;
}
.wa-pop--attach { left: 16px; width: 210px; padding: 6px; }
.wa-attach-opt {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 12px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: var(--fs-14);
  color: var(--wa-text);
  border-radius: var(--radius-sm);
  text-align: left;
}
.wa-attach-opt:hover { background: var(--wa-hover); }
.wa-attach-opt svg { width: 20px; height: 20px; color: var(--wa-accent-strong); }

.wa-pop--emoji { left: 16px; width: 300px; height: 240px; display: flex; flex-direction: column; }
.wa-emoji-grid {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  padding: 8px;
}
.wa-emoji-grid button {
  border: none;
  background: none;
  font-size: 21px;
  line-height: 1;
  padding: 5px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.wa-emoji-grid button:hover { background: var(--wa-hover); }

/* ---------- Template picker (kept, restyled) ---------- */
.wa-tpl-composer { background: var(--wa-panel-2); padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0)); }
.wa-tpl-pick { display: flex; gap: 8px; margin-top: 8px; }
.wa-tpl-pick select {
  flex: 1;
  border: 1px solid var(--wa-divider);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: var(--fs-13);
  background: var(--wa-panel);
  color: var(--wa-text);
}
.wa-tpl-vars { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.wa-tpl-vars input {
  border: 1px solid var(--wa-divider);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: var(--fs-13);
  background: var(--wa-panel);
  color: var(--wa-text);
}
/* Media-header attach control shown for an IMAGE/VIDEO/DOCUMENT template. */
.wa-tpl-hdr-media {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 12px;
  border: 1px dashed var(--wa-divider);
  border-radius: var(--radius-sm);
  background: var(--wa-panel);
}
.wa-tpl-hdr-label { font-size: var(--fs-12); font-weight: 600; color: var(--wa-text-2); }
.wa-tpl-hdr-preview {
  max-width: 160px;
  max-height: 110px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 1px solid var(--wa-divider);
}
.wa-tpl-hdr-file { font-size: var(--fs-12); color: var(--wa-text-2); }
.wa-tpl-hdr-status { font-size: var(--fs-12); color: var(--wa-text-2); }
.wa-tpl-preview {
  background: var(--wa-panel);
  border: 1px dashed var(--wa-divider);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: var(--fs-13);
  color: var(--wa-text-2);
  white-space: pre-wrap;
  margin-top: 8px;
}
.wa-btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 8px 16px;
  font-size: var(--fs-13);
  font-weight: var(--fw-semibold);
  cursor: pointer;
  background: var(--wa-accent);
  color: #fff;
  flex-shrink: 0;
}
.wa-btn:hover { background: var(--wa-accent-strong); }
.wa-btn:disabled { opacity: 0.55; cursor: default; }
.wa-btn--ghost { background: none; color: var(--wa-accent-strong); border: 1px solid var(--wa-divider); }

/* ---------- Templates manager panel ---------- */
.wa-panel-scroll { flex: 1; overflow-y: auto; padding: 16px; position: relative; z-index: 1; }
.wa-tpl-card {
  background: var(--wa-panel);
  border: 1px solid var(--wa-divider);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
}
.wa-tpl-card-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
.wa-tpl-card-top strong { font-size: var(--fs-14); }
.wa-tpl-card-body { font-size: var(--fs-13); color: var(--wa-text-2); white-space: pre-wrap; overflow-wrap: anywhere; }

/* ============================================================================
   Template Approval module — list (left pane) + editor/preview (right pane)
   ========================================================================= */
.wa-tpl-list-actions {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--wa-divider);
}
.wa-tpl-list-actions .wa-btn { flex: 1; text-align: center; }
.wa-tpl-rows { padding: 6px 0; }
.wa-tpl-group-label {
  font-size: var(--fs-11);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--wa-text-muted);
  padding: 12px 16px 4px;
}
.wa-tpl-row {
  padding: 10px 16px;
  border-bottom: 1px solid var(--wa-divider);
  cursor: pointer;
}
.wa-tpl-row:hover { background: var(--wa-hover); }
.wa-tpl-row.active { background: var(--wa-active); }
.wa-tpl-row-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.wa-tpl-row-name { font-weight: var(--fw-semibold); font-size: var(--fs-14); color: var(--wa-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wa-tpl-row-meta { font-size: var(--fs-12); color: var(--wa-text-muted); margin-top: 2px; }
.wa-tpl-row-preview { font-size: var(--fs-13); color: var(--wa-text-2); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wa-tpl-row-reason { font-size: var(--fs-12); color: #b3261e; margin-top: 4px; }
[data-theme="dark"] .wa-tpl-row-reason { color: #f2b8b5; }

.wa-tpl-status-chip {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-radius: 4px;
  padding: 2px 6px;
  flex-shrink: 0;
}
.wa-tpl-status-chip--draft { background: rgba(102,119,129,0.16); color: var(--wa-text-2); }
.wa-tpl-status-chip--pending { background: rgba(255,167,38,0.18); color: #b26a00; }
.wa-tpl-status-chip--approved { background: rgba(37,211,102,0.18); color: var(--wa-accent-strong); }
.wa-tpl-status-chip--rejected { background: rgba(179,38,30,0.14); color: #b3261e; }
.wa-tpl-status-chip--disabled { background: rgba(102,119,129,0.16); color: var(--wa-text-muted); }
[data-theme="dark"] .wa-tpl-status-chip--pending { color: #ffb74d; }
[data-theme="dark"] .wa-tpl-status-chip--rejected { color: #f2b8b5; }

/* ---- Editor shell: form + live preview, side by side on desktop ---- */
.wa-tpl-editor-shell { background: var(--wa-panel); }
.wa-tpl-editor-body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr minmax(300px, 380px);
  overflow: hidden;
}
.wa-tpl-editor {
  overflow-y: auto;
  padding: 18px 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.wa-tpl-preview-pane {
  border-left: 1px solid var(--wa-divider);
  background: var(--wa-bg);
  overflow-y: auto;
  padding: 18px 20px;
}
.wa-tpl-preview-label {
  font-size: var(--fs-11);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--wa-text-muted);
  margin-bottom: 12px;
}

.wa-tpl-field { display: flex; flex-direction: column; gap: 5px; }
.wa-tpl-field > span { font-size: var(--fs-13); font-weight: var(--fw-semibold); color: var(--wa-text-2); }
.wa-tpl-field > span small { font-weight: 400; color: var(--wa-text-muted); }
.wa-tpl-field small { font-size: var(--fs-11); color: var(--wa-text-muted); }
.wa-tpl-field input[type="text"],
.wa-tpl-field select,
.wa-tpl-field textarea {
  width: 100%;
  border: 1px solid var(--wa-divider);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  font: inherit;
  font-size: var(--fs-14);
  background: var(--wa-panel);
  color: var(--wa-text);
}
.wa-tpl-field textarea { resize: vertical; min-height: 96px; line-height: var(--lh-normal); }
.wa-tpl-field input:focus, .wa-tpl-field select:focus, .wa-tpl-field textarea:focus {
  outline: none; border-color: var(--wa-accent);
}
.wa-tpl-field input[readonly], .wa-tpl-field select:disabled { opacity: 0.6; cursor: not-allowed; }
.wa-tpl-row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.wa-tpl-palette { display: flex; flex-wrap: wrap; gap: 6px; }
.wa-tpl-chip {
  border: 1px dashed var(--wa-divider);
  background: var(--wa-panel-2);
  color: var(--wa-accent-strong);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: var(--fs-12);
  cursor: pointer;
}
.wa-tpl-chip:hover { background: var(--wa-hover); border-style: solid; }
.wa-tpl-chip--custom { color: var(--wa-text-muted); }

.wa-tpl-media-row { display: flex; align-items: center; gap: 10px; font-size: var(--fs-13); color: var(--wa-text-muted); flex-wrap: wrap; }

.wa-tpl-btn-builder { display: flex; flex-direction: column; gap: 8px; margin-bottom: 6px; }
.wa-tpl-btn-row { display: flex; gap: 6px; align-items: center; }
.wa-tpl-btn-row .wa-tpl-btn-type { flex: 0 0 128px; }
.wa-tpl-btn-row .wa-tpl-btn-text { flex: 1; }
.wa-tpl-btn-row .wa-tpl-btn-extra { flex: 1; }
.wa-tpl-btn-del { width: 30px; height: 30px; flex-shrink: 0; }

.wa-tpl-sample-row { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.wa-tpl-sample-row > span { flex: 0 0 130px; font-size: var(--fs-12); color: var(--wa-text-muted); font-family: monospace; }
.wa-tpl-sample-row > input { flex: 1; }

.wa-tpl-errors {
  border: 1px solid rgba(179,38,30,0.3);
  background: rgba(179,38,30,0.06);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: var(--fs-12);
  color: #b3261e;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
[data-theme="dark"] .wa-tpl-errors { color: #f2b8b5; }
.wa-tpl-errors .wa-tpl-ok { color: var(--wa-accent-strong); }

.wa-tpl-ro { display: flex; flex-direction: column; gap: 10px; font-size: var(--fs-14); }
.wa-tpl-ro-row { display: flex; gap: 12px; }
.wa-tpl-ro-row b { flex: 0 0 90px; color: var(--wa-text-muted); font-weight: var(--fw-semibold); }
.wa-tpl-ro-hint { font-size: var(--fs-13); color: var(--wa-text-muted); margin: 6px 0 0; }

.wa-tpl-editor-foot {
  flex-shrink: 0;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom, 0));
  border-top: 1px solid var(--wa-divider);
  background: var(--wa-panel-2);
  flex-wrap: wrap;
}
.wa-btn--danger { background: #f5334f; }
.wa-btn--danger:hover { background: #d92840; }

/* ---- The WhatsApp-style preview bubble ---- */
.wa-tpl-pv-bubble {
  background: var(--wa-in-bubble);
  border-radius: 8px;
  box-shadow: var(--wa-shadow-bubble);
  padding: 8px 10px 6px;
  max-width: 320px;
  font-size: var(--fs-14);
  color: var(--wa-bubble-text);
}
.wa-tpl-pv-header { font-weight: 700; margin-bottom: 5px; white-space: pre-wrap; }
.wa-tpl-pv-media {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  height: 120px; margin: -8px -10px 6px; border-radius: 8px 8px 0 0;
  background: var(--wa-panel-2); color: var(--wa-text-muted); font-size: var(--fs-13);
}
.wa-tpl-pv-media svg { width: 30px; height: 30px; }
.wa-tpl-pv-body { white-space: pre-wrap; overflow-wrap: anywhere; line-height: var(--lh-normal); }
.wa-tpl-pv-footer { color: var(--wa-meta); font-size: var(--fs-12); margin-top: 6px; }
.wa-tpl-pv-time { text-align: right; color: var(--wa-meta); font-size: var(--fs-11); margin-top: 3px; }
.wa-tpl-pv-btns { max-width: 320px; margin-top: 2px; }
.wa-tpl-pv-btn {
  background: var(--wa-in-bubble);
  box-shadow: var(--wa-shadow-bubble);
  color: var(--wa-tick-read);
  text-align: center;
  padding: 8px;
  border-radius: 8px;
  margin-top: 2px;
  font-size: var(--fs-14);
}

@media (max-width: 900px) {
  .wa-tpl-editor-body { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .wa-tpl-preview-pane {
    border-left: none;
    border-top: 1px solid var(--wa-divider);
    max-height: 40vh;
  }
}
@media (max-width: 640px) {
  .wa-tpl-field input[type="text"], .wa-tpl-field select, .wa-tpl-field textarea { font-size: 16px; }
  .wa-tpl-row2 { grid-template-columns: 1fr; }
  .wa-tpl-btn-row { flex-wrap: wrap; }
  .wa-tpl-btn-row .wa-tpl-btn-type { flex: 1 1 100%; }
}

/* ---------- Start-conversation pane (from a contact) ---------- */
.wa-start {
  flex: 1;
  overflow-y: auto;
  padding: 28px 24px;
  position: relative;
  z-index: 1;
  max-width: 560px;
  margin: 0 auto;
  width: 100%;
}
.wa-start h3 { margin: 0 0 4px; font-size: var(--fs-17); color: var(--wa-text); }
.wa-start p { margin: 0 0 16px; color: var(--wa-text-muted); font-size: var(--fs-13); }
.wa-start .wa-start-id {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--wa-panel);
  border: 1px solid var(--wa-divider);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 18px;
}

/* ---------- Lightbox ---------- */
.wa-lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}
.wa-lightbox img, .wa-lightbox video { max-width: 100%; max-height: 100%; border-radius: 4px; }
.wa-lightbox-close {
  position: absolute;
  top: 20px;
  right: 24px;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.14);
  color: #fff;
  font-size: 22px;
  cursor: pointer;
}

/* ---------- Toast (in-module, replaces alert()) ---------- */
.wa-toast {
  position: absolute;
  left: 50%;
  bottom: 84px;
  transform: translateX(-50%);
  background: #333d45;
  color: #fff;
  padding: 9px 16px;
  border-radius: var(--radius-pill);
  font-size: var(--fs-13);
  z-index: 30;
  box-shadow: var(--wa-shadow-pop);
  max-width: 80%;
  text-align: center;
}

/* ---------- Spinner ---------- */
.wa-spin {
  width: 15px;
  height: 15px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  display: inline-block;
  animation: wa-spin 0.7s linear infinite;
}
@keyframes wa-spin { to { transform: rotate(360deg); } }

/* ============================================================================
   Mobile — single pane with slide navigation (≤ 900px).
   Belt-and-suspenders: data-mobile-pane here + inline style.display set by
   setWaMobilePane() in JS (same proven technique as setMailMobilePane —
   see its three-bugfix history in style.css).
   ========================================================================= */
@media (max-width: 900px) {
  .wa-left {
    width: 100%;
    max-width: none;
    min-width: 0;
    border-right: none;
  }
  .wa-right { width: 100%; }
  .wa-shell[data-mobile-pane="list"] .wa-right { display: none; }
  .wa-shell[data-mobile-pane="thread"] .wa-left { display: none; }

  .wa-back-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 38px;
    border: none;
    background: none;
    color: var(--wa-text-2);
    cursor: pointer;
    flex-shrink: 0;
    margin-left: -6px;
  }
  .wa-back-btn svg { width: 22px; height: 22px; }

  .wa-messages { padding-left: 4%; padding-right: 4%; }
  .wa-msg { max-width: 82%; }
  .wa-media-img, .wa-media-video { max-width: 100%; }
  .wa-thread-controls select { max-width: 108px; }
  .wa-placeholder { display: none; }
}

@media (max-width: 640px) {
  /* iOS focus-zoom guard (matches the app-wide rule for every other input). */
  .wa-composer-textarea, .wa-search input, .wa-attach-caption,
  .wa-tpl-pick select, .wa-tpl-vars input, .wa-thread-controls select { font-size: 16px; }
}

/* Respect reduced-motion. */
@media (prefers-reduced-motion: reduce) {
  .wa-send-btn, .wa-row, .wa-icon-btn, .wa-tab { transition: none; }
  .wa-spin { animation-duration: 1.4s; }
}

/* ============================================================================
   INTERNAL CHAT — WhatsApp visual language
   Pure restyle of the existing .chat-* markup emitted by frontend/js/chat.js
   to match the WhatsApp module above. No JS/markup/permission changes — this
   file loads after style.css so these rules win. Reuses the --wa-* tokens.
   ========================================================================= */

body.chat-fullbleed main { padding: 0 !important; overflow: hidden; }
body.chat-fullbleed #view-chat:not(.hidden) {
  position: fixed;
  top: var(--topbar-height, 56px);
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  background: var(--wa-panel);
  overflow: hidden;
}
body.chat-fullbleed.internal-call-bar-active #view-chat:not(.hidden) {
  top: calc(var(--topbar-height, 56px) + 60px);
}
@media (max-width: 640px) {
  body.chat-fullbleed #view-chat:not(.hidden) { bottom: calc(56px + env(safe-area-inset-bottom, 0)); }
  body.chat-thread-open #view-chat:not(.hidden) { bottom: 0; }
}
@media (max-width: 900px) {
  body.chat-thread-open .mobile-tab-bar { display: none !important; }
}
#chat-content { height: 100%; }

.chat-layout {
  display: flex !important;
  grid-template-columns: none !important;
  height: 100% !important;
  min-height: 0 !important;
  gap: 0 !important;
  background: var(--wa-panel);
  overflow: hidden;
  font-size: var(--fs-14);
  color: var(--wa-text);
}
.chat-list-pane {
  width: 32%; min-width: 320px; max-width: 440px;
  display: flex; flex-direction: column;
  background: var(--wa-panel);
  border: none; border-right: 1px solid var(--wa-divider);
  border-radius: 0;
}
.chat-thread-pane {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column;
  background: var(--wa-bg);
  border: none; border-radius: 0;
  position: relative;
}
.chat-thread-pane::before {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background-image:
    radial-gradient(var(--wa-bg-pattern) 1px, transparent 1px),
    radial-gradient(var(--wa-bg-pattern) 1px, transparent 1px);
  background-size: 42px 42px, 42px 42px;
  background-position: 0 0, 21px 21px;
}
#chat-thread-content { position: relative; z-index: 1; }

.chat-list-search-row { padding: 8px 12px; border: none; background: var(--wa-panel); flex-shrink: 0; }
.chat-list-search-row .filter-search {
  width: 100%; background: var(--wa-panel-2); border: none;
  border-radius: var(--radius-pill); padding: 8px 14px;
  font-size: var(--fs-14); color: var(--wa-text);
}
.chat-list-search-row .filter-search::placeholder { color: var(--wa-text-muted); }

.chat-list-tabs {
  display: flex; align-items: center;
  background: var(--wa-panel); border-bottom: 1px solid var(--wa-divider); flex-shrink: 0;
}
.chat-list-tab {
  flex: 1; padding: 12px 8px; border: none; background: none;
  border-bottom: 3px solid transparent;
  font-size: var(--fs-13); font-weight: var(--fw-semibold);
  color: var(--wa-text-muted); letter-spacing: 0.02em; cursor: pointer;
}
.chat-list-tab:hover { color: var(--wa-text-2); }
.chat-list-tab.active { color: var(--wa-accent); border-bottom-color: var(--wa-accent); }
.chat-new-group-btn {
  flex-shrink: 0; margin-right: 8px;
  border: 1px solid var(--wa-divider); background: var(--wa-panel);
  color: var(--wa-accent-strong); border-radius: var(--radius-pill);
  padding: 4px 12px; font-size: var(--fs-12); cursor: pointer;
}

.chat-list-body { flex: 1; overflow-y: auto; background: var(--wa-panel); }
.chat-empty-hint { padding: 40px 24px; text-align: center; color: var(--wa-text-muted); font-size: var(--fs-13); }
.chat-list-item {
  display: flex; align-items: center; gap: 13px;
  padding: 10px 13px 10px 15px;
  border-bottom: 1px solid var(--wa-divider); cursor: pointer;
}
.chat-list-item:hover { background: var(--wa-hover); }
.chat-list-item.active { background: var(--wa-active); }
.chat-avatar {
  position: relative;
  width: 49px; height: 49px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--wa-accent); color: #fff;
  font-weight: var(--fw-semibold); font-size: var(--fs-15);
}
.chat-list-item-main { flex: 1; min-width: 0; }
.chat-list-item-name {
  font-size: var(--fs-15); font-weight: 400; color: var(--wa-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-list-item-sub {
  font-size: var(--fs-13); color: var(--wa-text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px;
}
.chat-list-item-presence { font-size: 11px; color: var(--wa-text-muted); margin-top: 1px; }
.chat-unread-badge {
  min-width: 20px; height: 20px; padding: 0 6px;
  border-radius: var(--radius-pill); background: var(--wa-badge); color: #fff;
  font-size: var(--fs-12); font-weight: var(--fw-bold);
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.chat-presence-dot {
  position: absolute; right: 0; bottom: 0;
  width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--wa-panel);
}
.chat-presence-dot.online { background: #25d366; }
.chat-presence-dot.away { background: var(--tone-warning-fg); }
.chat-presence-dot.offline { background: #8696a0; }

.chat-thread-header {
  display: flex; align-items: center; gap: 12px;
  height: var(--wa-header-h); padding: 0 16px;
  background: var(--wa-panel-2); border-bottom: none; flex-shrink: 0;
}
.chat-back-btn {
  display: none; border: none; background: none; cursor: pointer;
  font-size: 26px; line-height: 1; color: var(--wa-text-2);
  padding: 0 4px; margin-left: -6px;
}
.chat-thread-header .chat-avatar { width: 40px; height: 40px; font-size: var(--fs-13); }
.chat-thread-header-info { flex: 1; min-width: 0; }
.chat-thread-name {
  font-size: var(--fs-15); font-weight: var(--fw-semibold); color: var(--wa-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-thread-status { font-size: var(--fs-12); color: var(--wa-text-muted); }
.chat-merchant-link {
  border: none; background: none; cursor: pointer;
  font-size: var(--fs-12); color: var(--wa-accent-strong); padding: 2px 0;
}
.chat-thread-header-actions { display: flex; align-items: center; gap: 2px; flex-shrink: 0; }
.chat-icon-btn {
  width: 38px; height: 38px; border-radius: 50%;
  border: none; background: transparent;
  color: var(--wa-text-muted); font-size: 17px; cursor: pointer;
}
.chat-icon-btn:hover { background: var(--wa-hover); color: var(--wa-text-2); }
.chat-icon-btn:disabled { opacity: 0.4; cursor: default; }

.chat-messages {
  flex: 1; overflow-y: auto;
  padding: 12px 6% 18px;
  display: flex; flex-direction: column; gap: 2px;
  position: relative; z-index: 1; background: transparent;
}
.chat-system-row {
  align-self: center; margin: 12px 0;
  background: var(--wa-panel-2); color: var(--wa-text-muted);
  font-size: 11.5px; font-weight: var(--fw-medium);
  padding: 5px 12px; border-radius: var(--radius-sm);
  box-shadow: var(--wa-shadow-bubble); text-align: center;
}
.chat-message-row {
  display: flex; align-items: flex-end; gap: 6px;
  max-width: 65%; margin-top: 2px;
}
.chat-message-row.mine { align-self: flex-end; flex-direction: row-reverse; }
.chat-message-row.theirs { align-self: flex-start; }
.chat-bubble {
  position: relative;
  background: var(--wa-in-bubble); color: var(--wa-bubble-text);
  border-radius: 8px; padding: 6px 9px 8px;
  box-shadow: var(--wa-shadow-bubble);
  overflow-wrap: anywhere; min-width: 92px;
}
.chat-message-row.mine .chat-bubble { background: var(--wa-out-bubble); }
.chat-bubble-sender {
  display: block; font-size: 11.5px; font-weight: var(--fw-semibold);
  color: var(--wa-accent-strong); margin-bottom: 2px;
}
.chat-bubble-text { font-size: var(--fs-14); line-height: 1.35; white-space: pre-wrap; }
.chat-bubble-text a { color: var(--wa-accent-strong); text-decoration: underline; }
.chat-deleted-text { color: var(--wa-text-muted); }
.chat-bubble-meta {
  float: right; margin: 6px 0 -3px 10px;
  font-size: 11px; color: var(--wa-meta); user-select: none;
  display: inline-flex; align-items: center; gap: 3px;
}
.chat-message-row.mine .chat-bubble-meta { color: rgba(0,0,0,0.45); }
[data-theme="dark"] .chat-message-row.mine .chat-bubble-meta { color: rgba(233,237,239,0.6); }
.chat-edited-tag { font-style: italic; }
.chat-read-tick { letter-spacing: -2px; }
.chat-read-tick.read { color: var(--wa-tick-read); }
.chat-reply-preview {
  border-left: 3px solid var(--wa-accent);
  background: rgba(0,0,0,0.04); border-radius: 4px;
  padding: 4px 8px; margin-bottom: 4px;
  font-size: var(--fs-12); color: var(--wa-text-muted);
}
[data-theme="dark"] .chat-reply-preview { background: rgba(255,255,255,0.06); }
.chat-reply-preview strong { display: block; color: var(--wa-accent-strong); }
.chat-attachment-image {
  max-width: 300px; width: 100%; border-radius: 6px; display: block;
  margin-bottom: 4px; cursor: pointer;
}
.chat-attachment-file {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 12px; margin-bottom: 4px;
  background: rgba(0,0,0,0.04); border-radius: 6px;
  font-size: var(--fs-13); color: var(--wa-text); text-decoration: none;
}
[data-theme="dark"] .chat-attachment-file { background: rgba(255,255,255,0.05); }
.chat-reactions-row { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.chat-reaction-pill {
  font-size: 11px; padding: 1px 6px; border-radius: var(--radius-pill);
  background: var(--wa-panel-2); border: 1px solid var(--wa-divider); cursor: pointer;
}
.chat-reaction-pill.mine { border-color: var(--wa-accent); }
.chat-message-actions { display: flex; gap: 2px; opacity: 0; transition: opacity 0.1s; }
.chat-message-row:hover .chat-message-actions { opacity: 1; }
.chat-message-actions button {
  border: none; background: var(--wa-panel); box-shadow: var(--wa-shadow-bubble);
  border-radius: 50%; width: 26px; height: 26px; font-size: 12px; cursor: pointer;
}
.chat-typing-indicator {
  padding: 2px 6% 6px; font-size: var(--fs-12); color: var(--wa-text-muted);
  font-style: italic; position: relative; z-index: 1;
}

.chat-composer {
  flex-shrink: 0; background: var(--wa-panel-2); border-top: none;
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom, 0));
  position: relative; z-index: 1;
}
.chat-reply-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 14px; margin: 0 0 6px;
  background: var(--wa-panel); border-radius: var(--radius);
  font-size: var(--fs-12); color: var(--wa-text-2); border-top: none;
}
.chat-reply-bar button { border: none; background: none; cursor: pointer; font-size: 16px; color: var(--wa-text-muted); }
.chat-pending-files { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.chat-pending-file-chip {
  background: var(--wa-panel); border: 1px solid var(--wa-divider);
  border-radius: var(--radius-pill); padding: 3px 10px; font-size: var(--fs-12);
  display: inline-flex; align-items: center; gap: 6px;
}
.chat-pending-file-chip button { border: none; background: none; cursor: pointer; color: var(--wa-text-muted); }
.chat-composer-row { display: flex; align-items: flex-end; gap: 8px; }
.chat-composer-row .chat-icon-btn { flex-shrink: 0; align-self: center; }
.chat-input {
  flex: 1; min-width: 0;
  min-height: 24px; max-height: 120px; overflow-y: auto;
  background: var(--wa-panel); border: none; border-radius: 22px;
  padding: 11px 16px; font-size: var(--fs-15); line-height: 1.4; color: var(--wa-text);
}
.chat-input:empty:before { content: attr(data-placeholder); color: var(--wa-text-muted); }
.chat-input:focus { outline: none; }
.chat-send-btn {
  flex-shrink: 0; width: 42px; height: 42px; border-radius: 50%;
  border: none; background: var(--wa-accent); color: #fff;
  font-size: 0; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.chat-send-btn::after { content: "\27A4"; font-size: 17px; line-height: 1; }
.chat-send-btn:hover { background: var(--wa-accent-strong); }
.chat-send-btn:disabled { opacity: 0.55; cursor: default; }

@media (max-width: 900px) {
  .chat-list-pane { width: 100%; max-width: none; min-width: 0; border-right: none; }
  .chat-thread-pane { width: 100%; }
  .chat-layout[data-mobile-pane="list"] .chat-thread-pane { display: none; }
  .chat-layout[data-mobile-pane="detail"] .chat-list-pane { display: none; }
  .chat-back-btn { display: inline-flex; align-items: center; justify-content: center; }
  .chat-messages { padding-left: 4%; padding-right: 4%; }
  .chat-message-row { max-width: 82%; }
  .chat-attachment-image { max-width: 100%; }
}
@media (max-width: 640px) {
  .chat-input, .chat-list-search-row .filter-search { font-size: 16px; }
}

/* ============================================================================
   EMAIL — WhatsApp visual language
   Pure restyle of the existing .mail-* markup (frontend/js/app.js). No change
   to any Zoho call, threading, compose/dock behaviour, swipe or permissions.
   This file loads after style.css so these rules win. Reuses the --wa-* tokens
   so dark mode comes from the token overrides at the top of this file.
   showView() toggles body.mail-fullbleed; setMailMobilePane() toggles
   body.mail-thread-open (mobile, in a thread) — same pattern as WhatsApp/Chat.
   ========================================================================= */

/* ---------- Full-bleed takeover ---------- */
body.mail-fullbleed main { padding: 0 !important; overflow: hidden; }
body.mail-fullbleed #view-mail:not(.hidden) {
  position: fixed;
  top: var(--topbar-height, 56px);
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  background: var(--wa-panel);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
body.mail-fullbleed.internal-call-bar-active #view-mail:not(.hidden) {
  top: calc(var(--topbar-height, 56px) + 60px);
}
body.mail-fullbleed #view-mail > .view-header { display: none; }
body.mail-fullbleed #mail-content {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  height: auto;
}
body.mail-fullbleed .mail-refresh-error { margin: 0 12px; }
body.mail-fullbleed .mail-layout {
  flex: 1;
  min-height: 0;
  height: auto;
  gap: 0;
  margin: 0;
  border-top: 1px solid var(--wa-divider);
}
@media (max-width: 640px) {
  body.mail-fullbleed #view-mail:not(.hidden) { bottom: calc(56px + env(safe-area-inset-bottom, 0)); }
  body.mail-thread-open #view-mail:not(.hidden) { bottom: 0; }
}
@media (max-width: 900px) {
  body.mail-thread-open .mobile-tab-bar { display: none !important; }
}

/* ---------- Toolbar strip ---------- */
body.mail-fullbleed .mail-toolbar {
  margin: 0;
  padding: 8px 14px;
  background: var(--wa-panel-2);
  gap: 8px;
  flex-shrink: 0;
}
.mail-fullbleed .mail-search-wrap {
  background: var(--wa-panel);
  border: none;
  border-radius: 8px;
}
.mail-fullbleed #mail-search { color: var(--wa-text); }
.mail-fullbleed .mail-filter-chip {
  border-radius: 999px;
  border-color: var(--wa-divider);
  color: var(--wa-text-muted);
}
.mail-fullbleed .mail-filter-chip.active {
  background: var(--wa-accent);
  border-color: var(--wa-accent);
  color: #fff;
}

/* ---------- Panes: colours + hairline dividers, no cards ---------- */
.mail-fullbleed .mail-folders,
.mail-fullbleed .mail-messages,
.mail-fullbleed .mail-detail {
  background: var(--wa-panel);
  border: none;
  border-radius: 0;
}
.mail-fullbleed .mail-folders {
  background: var(--wa-panel-2);
  border-right: 1px solid var(--wa-divider);
}
.mail-fullbleed .mail-messages { border-right: 1px solid var(--wa-divider); }
.mail-fullbleed .mail-detail { background: var(--wa-bg); padding: 18px 22px; }
.mail-fullbleed .mail-folder-item { color: var(--wa-text-2); border-radius: 8px; }
.mail-fullbleed .mail-folder-item:hover { background: var(--wa-hover); }
.mail-fullbleed .mail-folder-item.active { background: var(--wa-accent); color: #fff; }
.mail-fullbleed .mail-folder-item.active .mail-folder-icon { color: #fff; }

/* ---------- Avatars (colour-hashed initials, seeded per sender) ---------- */
.mail-avatar { color: #fff; }
.mail-avatar-lg { width: 44px; height: 44px; font-size: 15px; }

/* ---------- List rows — WhatsApp list rhythm ---------- */
.mail-fullbleed .mail-message-row { border-bottom: 1px solid var(--wa-divider); }
.mail-fullbleed .mail-row-content {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 16px;
  background: var(--wa-panel);
}
.mail-fullbleed .mail-message-row:hover .mail-row-content { background: var(--wa-hover); }
.mail-fullbleed .mail-message-row.unread .mail-row-content { background: var(--wa-panel); }
.mail-row-body { flex: 1; min-width: 0; }
.mail-fullbleed .mail-row-top { color: var(--wa-text-muted); align-items: baseline; }
.mail-fullbleed .mail-row-sender { color: var(--wa-text); font-weight: 500; }
.mail-fullbleed .mail-message-row.unread .mail-row-sender { font-weight: 700; }
.mail-fullbleed .mail-row-sender-email { color: var(--wa-text-muted); font-weight: 400; }
.mail-fullbleed .mail-row-subject { color: var(--wa-text-muted); margin-top: 2px; }
.mail-fullbleed .mail-message-row.unread .mail-row-subject { color: var(--wa-text-2); }
.mail-fullbleed .mail-row-date { color: var(--wa-text-muted); }
.mail-fullbleed .mail-message-row.unread .mail-row-date { color: var(--wa-accent-strong); font-weight: 600; }
.mail-row-unread-dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--wa-badge);
  margin-right: 6px;
  vertical-align: middle;
}
.mail-fullbleed .mail-thread-badge {
  background: var(--wa-accent);
  color: #fff;
}
.mail-fullbleed .mail-row-actions { background: var(--wa-hover); }
.mail-fullbleed .mail-row-action:hover { background: var(--wa-active); color: var(--wa-text); }
.mail-fullbleed .mail-load-more {
  background: var(--wa-panel);
  border-top: 1px solid var(--wa-divider);
  color: var(--wa-accent-strong);
}
.mail-fullbleed .mail-load-more:hover { background: var(--wa-hover); }

/* ---------- Thread cards — clean message cards ---------- */
.mail-fullbleed .mail-thread-card {
  border: 1px solid var(--wa-divider);
  border-radius: 10px;
  background: var(--wa-panel);
}
.mail-fullbleed .mail-thread-card.collapsed:hover { background: var(--wa-hover); border-color: var(--wa-divider); }
.mail-fullbleed .mail-thread-card.collapsed.unread { background: var(--wa-hover); }
.mail-fullbleed .mail-thread-card.collapsed.unread .mail-thread-card-sender { color: var(--wa-text); }
.mail-fullbleed .mail-thread-card-sender { color: var(--wa-text-2); }
.mail-fullbleed .mail-thread-card-snippet { color: var(--wa-text-muted); }
.mail-fullbleed .mail-thread-card-date { color: var(--wa-text-muted); }
.mail-fullbleed .mail-thread-card.expanded.unread { border-left: 3px solid var(--wa-accent); }
.mail-fullbleed .mail-thread-card-header { border-bottom-color: var(--wa-divider); }
.mail-fullbleed .mail-meta-sender { color: var(--wa-text); }
.mail-fullbleed .mail-meta-from-email,
.mail-fullbleed .mail-meta-label { color: var(--wa-text-muted); }
.mail-fullbleed .mail-meta-value { color: var(--wa-text-2); }
.mail-fullbleed .mail-meta-date { color: var(--wa-text-muted); }
.mail-fullbleed .mail-thread-latest-badge,
.mail-fullbleed .mail-thread-card .mail-thread-latest-badge {
  background: var(--wa-accent);
  color: #fff;
}
.mail-fullbleed .mail-body { color: var(--wa-text); }

/* Reply / Reply All / Forward — WhatsApp accent pills */
.mail-fullbleed .mail-detail-actions { border-top-color: var(--wa-divider); }
.mail-fullbleed .mail-detail-actions button {
  background: var(--wa-accent);
  border: none;
  color: #fff;
  border-radius: 999px;
  padding: 8px 18px;
  font-weight: 600;
}
.mail-fullbleed .mail-detail-actions button:hover { background: var(--wa-accent-strong); color: #fff; }
.mail-fullbleed .mail-icon-btn-sm:hover { background: var(--wa-hover); color: var(--wa-text); }

/* Attachments as WhatsApp doc chips */
.mail-fullbleed .mail-attachment-chip {
  background: var(--wa-hover);
  border: 1px solid var(--wa-divider);
  color: var(--wa-text);
  border-radius: 8px;
}
.mail-fullbleed .mail-attachment-label { color: var(--wa-text-muted); }

/* ---------- Compose window ---------- */
.mail-compose-window {
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--wa-divider);
  box-shadow: var(--wa-shadow-pop);
}
.mail-compose-header {
  background: var(--wa-accent);
  color: #fff;
}
.mail-compose-header .mail-compose-icon-btn { color: #fff; }
.mail-compose-send-btn,
.mail-compose-window .mail-compose-send-btn {
  background: var(--wa-accent);
  border-color: var(--wa-accent);
}
.mail-compose-send-btn:hover { background: var(--wa-accent-strong); border-color: var(--wa-accent-strong); }
.recipient-chip-avatar { background: var(--wa-accent); color: #fff; }

/* Snippet / preview line (Zoho `summary`) - third line of a list row, muted. */
.mail-fullbleed .mail-row-snippet {
  font-size: var(--fs-12);
  color: var(--wa-text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 1px;
}

/* ---------- Mobile ---------- */
@media (max-width: 900px) {
  .mail-fullbleed .mail-detail { padding: 12px 14px; }
  body.mail-fullbleed .mail-layout { margin: 0 -16px; }
}

/* Native mobile-email list row: 44px avatar, one clean vertical rhythm,
   no per-row action column (swipe + the open-thread actions cover those) -
   the stacked star/archive/delete column was the main source of the
   "mismatched, too much spacing, misaligned" feel on a phone. */
@media (max-width: 640px) {
  .mail-fullbleed #mail-search,
  .mail-fullbleed .mail-body,
  .mail-fullbleed .mail-detail-actions button { font-size: 16px; }

  .mail-fullbleed .mail-row-content {
    align-items: center;
    gap: 12px;
    padding: 11px 16px;
  }
  /* Beat the global `@media(max-width:640px) .mail-avatar{32px}` for the
     LIST avatar only - the reading-view avatar keeps its own size. */
  .mail-fullbleed .mail-message-row .mail-avatar {
    width: 44px;
    height: 44px;
    font-size: 15px;
  }
  .mail-fullbleed .mail-row-body { min-width: 0; }
  .mail-fullbleed .mail-row-top { align-items: baseline; gap: 8px; flex-wrap: nowrap; }
  .mail-fullbleed .mail-row-sender {
    font-size: var(--fs-15);
    font-weight: 500;
    flex: 1 1 auto;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .mail-fullbleed .mail-message-row.unread .mail-row-sender { font-weight: 700; }
  /* A phone list row shows the sender NAME, not the raw address - the reading
     view still has the full "Name <email>" (that uses .mail-meta-from-email). */
  .mail-fullbleed .mail-message-row .mail-row-sender-email { display: none; }
  .mail-fullbleed .mail-row-side { flex-direction: row; align-items: baseline; flex-shrink: 0; }
  .mail-fullbleed .mail-row-date { font-size: var(--fs-12); }
  .mail-fullbleed .mail-row-subject {
    font-size: var(--fs-13);
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .mail-fullbleed .mail-message-row.unread .mail-row-subject { color: var(--wa-text-2); font-weight: 500; }
  .mail-fullbleed .mail-row-snippet { font-size: var(--fs-12); }
  /* Per-row hover actions never made sense on touch; the always-visible
     stacked column here was a desktop pattern squeezed into mobile. Swipe
     (archive / delete) and the actions inside an open thread replace it. */
  .mail-fullbleed .mail-row-actions { display: none; }
  .mail-fullbleed .mail-message-row:hover .mail-row-date { visibility: visible; }

  /* Reading view: tighten the header rhythm, slightly bigger avatar. */
  .mail-fullbleed .mail-thread-card.expanded { padding: 14px 14px 16px; }
  .mail-fullbleed .mail-thread-card-header { gap: 10px; padding-bottom: 10px; margin-bottom: 10px; }
  .mail-fullbleed .mail-thread-card .mail-avatar { width: 40px; height: 40px; font-size: 14px; }
  .mail-fullbleed .mail-thread-card.expanded .mail-body,
  .mail-fullbleed .mail-thread-card.expanded .mail-attachments:not(:empty),
  .mail-fullbleed .mail-thread-card.expanded .mail-quick-actions,
  .mail-fullbleed .mail-thread-card.expanded .mail-detail-actions { margin-left: 0; }
}

/* ============================================================================
   TELNYX CALLING — mobile full-screen (WhatsApp-call style)
   frontend/js/calls.js renders #call-widget-body with .call-active-panel /
   .call-incoming-panel > .call-screen-id + .call-screen-controls. Desktop
   keeps the small floating card (style.css). On a phone the whole widget
   becomes a full-screen call surface. No JS/behaviour change - every button
   id and handler is exactly as before.
   ========================================================================= */
.call-avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--wa-accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 20px;
  margin: 0 auto 10px;
}
.call-avatar svg { width: 26px; height: 26px; }
.call-screen-controls { margin-top: 14px; }

@media (max-width: 640px) {
  /* Full-screen takeover (but not while collapsed to the minimized bar). */
  .call-widget:not(.call-widget-minimized) {
    inset: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: none;
    border: none;
    border-radius: 0;
    z-index: 1002;
    box-shadow: none;
    display: flex;
    flex-direction: column;
    background: linear-gradient(180deg, #075e54 0%, #0b312c 100%);
  }
  .call-widget:not(.call-widget-minimized) .call-widget-header {
    background: transparent;
    padding: calc(10px + env(safe-area-inset-top, 0)) 16px 4px;
    font-size: 12px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    opacity: 0.85;
    justify-content: flex-end;
  }
  .call-widget:not(.call-widget-minimized) #call-widget-status {
    position: absolute;
    left: 0; right: 0;
    top: calc(14px + env(safe-area-inset-top, 0));
    text-align: center;
    pointer-events: none;
  }
  .call-widget:not(.call-widget-minimized) .call-widget-close {
    font-size: 22px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.14);
  }
  .call-widget:not(.call-widget-minimized) .call-widget-body {
    flex: 1;
    display: flex;
    padding: 8px 22px calc(26px + env(safe-area-inset-bottom, 0));
    color: #fff;
    overflow-y: auto;
  }
  .call-active-panel,
  .call-incoming-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
  }
  /* Identity block pushed to the visual centre-top, controls to the bottom. */
  .call-screen-id {
    margin-top: 14vh;
    text-align: center;
  }
  .call-screen-controls {
    margin-top: auto;
  }
  .call-avatar {
    width: 108px;
    height: 108px;
    font-size: 40px;
    margin-bottom: 18px;
    background: rgba(255, 255, 255, 0.18);
  }
  .call-avatar svg { width: 46px; height: 46px; }
  .call-active-merchant { color: #fff; font-size: 26px; font-weight: 700; }
  .call-active-number { color: rgba(255, 255, 255, 0.75); font-size: 15px; margin-top: 4px; }
  .call-active-state { color: rgba(255, 255, 255, 0.75); font-size: 15px; margin-top: 10px; }
  #call-view-merchant-btn,
  .call-screen-id .link-btn {
    color: #fff;
    text-decoration: underline;
    margin-top: 12px;
    display: inline-block;
  }

  /* In-call controls: round translucent icon buttons, WhatsApp-call style. */
  .call-active-controls-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 18px 12px;
    max-width: 320px;
    margin: 0 auto 22px;
  }
  .call-active-controls-grid .call-control-btn {
    background: transparent;
    border: none;
    color: #fff;
    gap: 7px;
    font-size: 12px;
    padding: 0;
  }
  .call-active-controls-grid .call-control-btn svg {
    width: 24px;
    height: 24px;
    box-sizing: content-box;
    padding: 16px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.16);
  }
  .call-active-controls-grid .call-control-btn.active svg {
    background: #fff;
    color: #075e54;
  }
  .call-active-controls-grid .call-control-btn:hover { background: transparent; color: #fff; }

  /* Hang up: a single big round red button, centred. */
  .call-hangup-btn-full {
    width: 68px;
    height: 68px;
    border-radius: 50%;
    padding: 0;
    margin: 0 auto;
    font-size: 0;
    background: #f5334f;
  }
  .call-hangup-btn-full svg { width: 26px; height: 26px; }
  .call-hangup-btn-full:hover { background: #d92840; }

  /* Incoming: two big round actions, decline (red) left, answer (green) right. */
  .call-incoming-actions {
    justify-content: space-around;
    max-width: 320px;
    margin: 0 auto;
    gap: 0;
  }
  .call-incoming-actions button {
    flex: 0 0 auto;
    width: 66px;
    height: 66px;
    border-radius: 50%;
    padding: 0;
    font-size: 0;
    border: none !important;
    color: #fff !important;
  }
  .call-incoming-actions button svg { width: 26px; height: 26px; }
  .call-incoming-actions #call-answer-btn { background: #25d366 !important; }
  .call-incoming-actions #call-decline-btn { background: #f5334f !important; }
  .call-incoming-actions #call-decline-btn:hover { background: #d92840 !important; }

  /* Keypad / transfer slide-ups keep working, just themed for the dark screen. */
  .call-widget:not(.call-widget-minimized) .call-dtmf-panel,
  .call-widget:not(.call-widget-minimized) .call-transfer-panel {
    border-top-color: rgba(255, 255, 255, 0.2);
    max-width: 320px;
    margin-left: auto;
    margin-right: auto;
  }
  .call-widget:not(.call-widget-minimized) .call-dtmf-btn {
    background: rgba(255, 255, 255, 0.14);
    border: none;
    color: #fff;
  }
  .call-widget:not(.call-widget-minimized) .call-transfer-input {
    background: rgba(255, 255, 255, 0.14);
    border: none;
    color: #fff;
  }
}

[data-theme="dark"] .call-avatar { color: #fff; }

/* ============================================================================
   Bulk WhatsApp Campaign wizard — reuses the Template editor's shell
   (.wa-thread / .wa-tpl-field / .wa-tpl-status-chip / .wa-tpl-preview-card /
   .wa-tpl-editor-foot) so it looks and behaves like one consistent module.
   ========================================================================= */
.wa-chip--wa {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  color: var(--wa-accent-strong);
  background: rgba(37,211,102,0.14);
  border-radius: 4px;
  padding: 1px 5px;
  margin-left: 4px;
  vertical-align: middle;
}

.wa-bulk-shell { background: var(--wa-panel); }
.wa-bulk-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-width: 640px;
}
.wa-bulk-step { transition: opacity 0.15s ease; }
.wa-bulk-step--disabled { opacity: 0.45; pointer-events: none; }
.wa-tpl-field-label {
  display: block;
  font-size: var(--fs-13);
  font-weight: var(--fw-semibold);
  color: var(--wa-text-2);
  margin-bottom: 8px;
}

.wa-bulk-search-row { margin-bottom: 10px; }
.wa-bulk-search { width: 100%; }
.wa-bulk-search input { width: 100%; }

.wa-bulk-select-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.wa-bulk-count { font-size: var(--fs-13); color: var(--wa-text-2); margin-right: auto; }
.wa-bulk-count b { color: var(--wa-accent-strong); }

.wa-bulk-results {
  max-height: 320px;
  overflow-y: auto;
  border: 1px solid var(--wa-divider);
  border-radius: var(--radius-sm);
}
.wa-bulk-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-bottom: 1px solid var(--wa-divider);
  cursor: pointer;
}
.wa-bulk-row:last-child { border-bottom: none; }
.wa-bulk-row:hover { background: var(--wa-hover); }
.wa-bulk-row.checked { background: var(--wa-active); }
.wa-bulk-row-check { width: 17px; height: 17px; flex-shrink: 0; accent-color: var(--wa-accent); }
.wa-bulk-row-main { min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.wa-bulk-row-name { font-size: var(--fs-14); font-weight: 500; color: var(--wa-text); }
.wa-bulk-row-meta { font-size: var(--fs-12); color: var(--wa-text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.wa-bulk-preview { max-width: 320px; }

.wa-bulk-progress { align-items: center; text-align: center; padding-top: 60px; }
.wa-bulk-progress-bar {
  width: 100%;
  max-width: 360px;
  height: 10px;
  border-radius: 999px;
  background: var(--wa-divider);
  overflow: hidden;
  margin: 0 auto 14px;
}
.wa-bulk-progress-fill { height: 100%; background: var(--wa-accent); transition: width 0.3s ease; }
.wa-bulk-progress p { color: var(--wa-text-muted); font-size: var(--fs-13); }

.wa-bulk-summary { display: flex; gap: 12px; margin-bottom: 8px; }
.wa-bulk-summary-cell {
  flex: 1;
  text-align: center;
  background: var(--wa-panel-2);
  border-radius: var(--radius);
  padding: 14px 8px;
}
.wa-bulk-summary-cell b { display: block; font-size: 22px; color: var(--wa-text); }
.wa-bulk-summary-cell span { font-size: var(--fs-12); color: var(--wa-text-muted); }
.wa-bulk-summary-cell--ok b { color: var(--wa-accent-strong); }
.wa-bulk-summary-cell--bad b { color: #b3261e; }
[data-theme="dark"] .wa-bulk-summary-cell--bad b { color: #f2b8b5; }

/* Simple two-column result lists - name only, no other detail (see the
   explicit "keep it simple" ask this replaced the per-recipient table with). */
.wa-bulk-simple-results { display: flex; gap: 16px; }
.wa-bulk-simple-col {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--wa-divider);
  border-radius: var(--radius);
  overflow: hidden;
}
.wa-bulk-simple-title {
  padding: 10px 14px;
  font-size: var(--fs-13);
  font-weight: var(--fw-semibold);
  background: var(--wa-panel-2);
  border-bottom: 1px solid var(--wa-divider);
}
.wa-bulk-simple-title--ok { color: var(--wa-accent-strong); }
.wa-bulk-simple-title--bad { color: #b3261e; }
[data-theme="dark"] .wa-bulk-simple-title--bad { color: #f2b8b5; }
.wa-bulk-simple-list { list-style: none; margin: 0; padding: 0; max-height: 280px; overflow-y: auto; }
.wa-bulk-simple-list li {
  padding: 8px 14px;
  font-size: var(--fs-14);
  color: var(--wa-text);
  border-bottom: 1px solid var(--wa-divider);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.wa-bulk-simple-list li:last-child { border-bottom: none; }
.wa-bulk-simple-list .wa-bulk-simple-empty { color: var(--wa-text-muted); font-style: italic; }

@media (max-width: 900px) {
  .wa-bulk-body { max-width: none; }
  .wa-bulk-preview { max-width: none; }
}
@media (max-width: 640px) {
  .wa-bulk-search input { font-size: 16px; }
  .wa-bulk-summary { flex-direction: column; }
  .wa-bulk-select-bar { flex-direction: column; align-items: stretch; }
  .wa-bulk-count { margin-right: 0; }
  .wa-bulk-simple-results { flex-direction: column; }
}

/* ---------- Delivery failures log ---------- */
.wa-df-shell { display: flex; flex-direction: column; min-height: 0; }
.wa-df-body { flex: 1; min-height: 0; overflow: auto; padding: 12px 16px 24px; }
.wa-df-table { width: 100%; border-collapse: collapse; font-size: var(--fs-13); }
.wa-df-table th {
  text-align: left;
  padding: 8px 10px;
  color: var(--wa-text-muted);
  font-weight: var(--fw-semibold);
  border-bottom: 1px solid var(--wa-divider);
  position: sticky;
  top: 0;
  background: var(--wa-panel);
  white-space: nowrap;
}
.wa-df-table td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--wa-divider);
  color: var(--wa-text);
  vertical-align: top;
}
.wa-df-table tr:hover td { background: var(--wa-hover); }
.wa-df-when { white-space: nowrap; color: var(--wa-text-muted); }
.wa-df-muted { color: var(--wa-text-muted); }
@media (max-width: 640px) {
  .wa-df-body { padding: 8px; }
  .wa-df-table { font-size: var(--fs-12); }
  .wa-df-table th, .wa-df-table td { padding: 7px 6px; }
}
