/* ============================================================
   CHAT WIDGET  –  Live Chat UI Component  v1.0
   Floating bottom-right chat with avatar cluster + full panel.

   CRISP INTEGRATION: search for [CRISP] in chat-widget.js
   ============================================================ */

/* ── CSS VARIABLES ──────────────────────────────────────────── */
:root {
  --cw-primary:       #0EA5E9;
  --cw-primary-dark:  #0284C7;
  --cw-online:        #22C55E;
  --cw-bg:            #FFFFFF;
  --cw-surface:       #F8FAFC;
  --cw-border:        #E2E8F0;
  --cw-text:          #1E293B;
  --cw-muted:         #64748B;
  --cw-shadow:        0 20px 60px rgba(14,165,233,.15), 0 8px 24px rgba(0,0,0,.08);
  --cw-radius:        20px;
  --cw-av-active:     56px;
  --cw-av-small:      33px;
  --cw-z:             9999;
}

/* ── WIDGET ROOT ─────────────────────────────────────────────── */
.cw-root {
  position: fixed;
  bottom: 28px;
  right: 28px;
  z-index: var(--cw-z);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0px;
  pointer-events: none;   /* children re-enable events individually */
}

/* ── AVATAR CLUSTER ──────────────────────────────────────────── */
.cw-cluster {
  position: relative;
  display: flex;
  align-items: center;
  cursor: pointer;
  pointer-events: all;
  opacity: 0;
  transform: translateY(18px) scale(0.82);
  transition: opacity .45s ease, transform .45s cubic-bezier(.34,1.4,.64,1);
}
.cw-cluster.cw-visible {
  opacity: 1;
  transform: translateY(0) scale(1);
}
.cw-cluster:focus-visible { outline: 2px solid var(--cw-primary); outline-offset: 4px; border-radius: 12px; }

/* Individual avatars — overlapping horizontal stack */
.cw-av {
  position: relative;
  flex-shrink: 0;
  border-radius: 50%;
  overflow: hidden;
  border: 2.5px solid #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.14);
  margin-right: -14px;     /* overlap siblings */
  transition: width    .42s cubic-bezier(.34,1.4,.64,1),
              height   .42s cubic-bezier(.34,1.4,.64,1),
              transform .42s cubic-bezier(.34,1.4,.64,1),
              border-color .3s ease,
              box-shadow   .3s ease,
              opacity      .3s ease;
}
.cw-av:last-child { margin-right: 0; }
.cw-av img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* ── POSITION-BASED SIZING (never changes) ── */
.cw-av.cw-center {
  width:  58px;
  height: 58px;
  z-index: 10;
  transform: translateY(-4px);
}
.cw-av.cw-center::after {
  content: '';
  position: absolute;
  bottom: 3px; right: 3px;
  width: 12px; height: 12px;
  background: var(--cw-online);
  border-radius: 50%;
  border: 2px solid #fff;
}

.cw-av.cw-left,
.cw-av.cw-right {
  width:  36px;
  height: 36px;
  opacity: .85;
}
.cw-av.cw-left::after,
.cw-av.cw-right::after {
  content: '';
  position: absolute;
  bottom: 1px; right: 1px;
  width: 9px; height: 9px;
  background: var(--cw-online);
  border-radius: 50%;
  border: 2px solid #fff;
}

/* ── ACTIVE HIGHLIGHT (border + glow only, no size) ── */
.cw-av.cw-active {
  border-color: var(--cw-primary);
  box-shadow: 0 6px 20px rgba(14,165,233,.36);
}

/* ── INACTIVE STATE ── */
.cw-av.cw-inactive {
  cursor: pointer;
}

/* Whole-cluster hover: boost center glow */
.cw-cluster:hover .cw-av.cw-center {
  box-shadow: 0 6px 20px rgba(14,165,233,.55);
}

/* Side avatar hover */
.cw-av.cw-left:hover,
.cw-av.cw-right:hover {
  opacity: 1 !important;
  transform: translateY(-4px) scale(1.08);
  box-shadow: 0 5px 14px rgba(0,0,0,.2);
  border-color: var(--cw-primary) !important;
  z-index: 11;
}

/* ── FLOATING LABELS ── */
.cw-labels {
  position: absolute;
  bottom: calc(100% + 10px);
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 5px;
  pointer-events: none;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .35s ease .15s, transform .35s ease .15s;
  white-space: nowrap;
}
.cw-cluster.cw-visible .cw-labels {
  opacity: 1;
  transform: translateY(0);
}
/* Hide labels once the message bubble is shown */
.cw-cluster.cw-bubble-up .cw-labels {
  opacity: 0 !important;
  pointer-events: none;
}
.cw-label-available {
  background: var(--cw-online);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 100px;
  display: flex;
  align-items: center;
  gap: 6px;
  letter-spacing: .01em;
  box-shadow: 0 2px 8px rgba(34,197,94,.3);
  border: 1px solid rgba(255,255,255,.2);
}
.cw-label-available::before {
  content: '';
  width: 6px; height: 6px;
  background: rgba(255,255,255,.75);
  border-radius: 50%;
  animation: cw-pulse 1.6s infinite;
}
.cw-label-cta {
  background: #fff;
  color: var(--cw-text);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 100px;
  box-shadow: 0 2px 12px rgba(0,0,0,.1);
}
@keyframes cw-pulse {
  0%,100% { opacity:1; transform:scale(1); box-shadow: 0 0 0 0 rgba(255,255,255,.4); }
  50%      { opacity:.8; transform:scale(1.1); box-shadow: 0 0 0 6px rgba(255,255,255,.1); }
}

/* ── MESSAGE BUBBLE ─────────────────────────────────────────── */
.cw-bubble {
  pointer-events: all;
  cursor: pointer;
  background: var(--cw-bg);
  border-radius: 18px 18px 4px 18px;
  position: relative;
  padding: 15px 16px 13px;
  box-shadow: var(--cw-shadow);
  width:300px;
  display: none;
  opacity: 0;
  transform: translateY(14px) scale(.94);
}
.cw-bubble.cw-visible {
  display: block;
}
.cw-bubble.cw-animate {
  animation: cw-bubbleIn .42s cubic-bezier(.34,1.4,.64,1) forwards;
}
@keyframes cw-bubbleIn {
  from { opacity:0; transform:translateY(14px) scale(.9); }
  to   { opacity:1; transform:translateY(0) scale(1); }
}
.cw-bubble:focus-visible { outline: 2px solid var(--cw-primary); outline-offset: 3px; }

.cw-bubble-close {
  position: absolute;
  top: 9px; right: 9px;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: none;
  border: none;
  color: var(--cw-muted);
  font-size: 11px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .2s ease, color .2s ease;
  z-index: 1;
}
.cw-bubble-close:hover { background: var(--cw-border); color: var(--cw-text); }

.cw-bubble-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.cw-bubble-av {
  width: 36px; height: 36px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--cw-primary);
  flex-shrink: 0;
}
.cw-bubble-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--cw-text);
  line-height: 1.2;
}
.cw-bubble-role { font-size: 11px; color: var(--cw-muted); }

.cw-bubble-msgs { margin-bottom: 12px; }
.cw-bubble-msg  {
  font-size: 13.5px;
  color: var(--cw-text);
  line-height: 1.55;
  margin: 0;
}
.cw-bubble-msg + .cw-bubble-msg { margin-top: 1px; }

.cw-bubble-input-row {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1.5px solid var(--cw-border);
  border-radius: 100px;
  padding: 5px 5px 5px 14px;
  background: var(--cw-surface);
  transition: border-color .2s ease, background .2s ease;
}
.cw-bubble-input-row:focus-within {
  border-color: var(--cw-primary);
  background: #fff;
}
.cw-bubble-input {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  outline: none;
  font-size: 13px;
  color: var(--cw-text);
  font-family: inherit;
  line-height: 1.4;
}
.cw-bubble-input::placeholder { color: var(--cw-muted); }
.cw-bubble-send {
  flex-shrink: 0;
  width: 30px; height: 30px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--cw-primary), var(--cw-primary-dark));
  border: none;
  color: #fff;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .2s ease, box-shadow .2s ease;
}
.cw-bubble-send:hover  { transform: scale(1.1); box-shadow: 0 4px 12px rgba(14,165,233,.42); }
.cw-bubble-send:active { transform: scale(.94); }

/* ── FULL CHAT PANEL ────────────────────────────────────────── */
.cw-panel {
  position: fixed;
  bottom: 28px;
  right: 28px;
  z-index: var(--cw-z);
  width: 358px;
  max-height: 545px;
  display: flex;
  flex-direction: column;
  background: var(--cw-bg);
  border-radius: var(--cw-radius);
  box-shadow: var(--cw-shadow);
  overflow: hidden;
  /* closed state */
  opacity: 0;
  transform: translateY(20px) scale(.94);
  pointer-events: none;
  transition: opacity .35s cubic-bezier(.34,1.2,.64,1),
              transform .35s cubic-bezier(.34,1.2,.64,1);
}
.cw-panel.cw-open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: all;
}

/* Panel header */
.cw-panel-hdr {
  background: linear-gradient(135deg, var(--cw-primary) 0%, var(--cw-primary-dark) 100%);
  padding: 15px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}
.cw-panel-av-wrap { position: relative; flex-shrink: 0; }
.cw-panel-av {
  width: 44px; height: 44px;
  border-radius: 50%;
  object-fit: cover;
  border: 2.5px solid rgba(255,255,255,.45);
  display: block;
}
.cw-panel-dot {
  position: absolute;
  bottom: 1px; right: 1px;
  width: 11px; height: 11px;
  background: var(--cw-online);
  border-radius: 50%;
  border: 2px solid var(--cw-primary-dark);
}
.cw-panel-info { flex: 1; min-width: 0; }
.cw-panel-name { font-size: 14.5px; font-weight: 700; color: #fff; line-height: 1.2; }
.cw-panel-role { font-size: 11.5px; color: rgba(255,255,255,.75); margin-top: 1px; }
.cw-panel-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: rgba(255,255,255,.85);
  margin-top: 3px;
}
.cw-panel-status::before {
  content: '';
  width: 6px; height: 6px;
  background: var(--cw-online);
  border-radius: 50%;
  flex-shrink: 0;
}
.cw-close-btn {
  margin-left: auto;
  background: rgba(255,255,255,.15);
  border: none;
  border-radius: 50%;
  width: 32px; height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .2s ease;
}
.cw-close-btn:hover  { background: rgba(255,255,255,.28); }
.cw-close-btn:active { background: rgba(255,255,255,.4); }

/* Messages area */
.cw-msgs {
  flex: 1;
  overflow-y: auto;
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--cw-surface);
  scroll-behavior: smooth;
}
.cw-msgs::-webkit-scrollbar        { width: 4px; }
.cw-msgs::-webkit-scrollbar-track  { background: transparent; }
.cw-msgs::-webkit-scrollbar-thumb  { background: var(--cw-border); border-radius: 4px; }

/* Message rows */
.cw-row {
  display: flex;
  align-items: flex-end;
  gap: 9px;
  max-width: 86%;
  opacity: 0;
  transform: translateY(8px);
  animation: cw-msgIn .3s ease forwards;
}
@keyframes cw-msgIn {
  to { opacity:1; transform:translateY(0); }
}
.cw-row.cw-agent { align-self: flex-start; }
.cw-row.cw-user  { align-self: flex-end; flex-direction: row-reverse; }

.cw-row-av {
  width: 28px; height: 28px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}
.cw-row-body { display: flex; flex-direction: column; }
.cw-msg-bub {
  padding: 9px 13px;
  border-radius: 14px;
  font-size: 13.5px;
  line-height: 1.55;
}
.cw-row.cw-agent .cw-msg-bub {
  background: #fff;
  color: var(--cw-text);
  border-radius: 4px 14px 14px 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.cw-row.cw-user .cw-msg-bub {
  background: var(--cw-primary);
  color: #fff;
  border-radius: 14px 4px 14px 14px;
}
.cw-msg-time {
  font-size: 10px;
  color: var(--cw-muted);
  margin-top: 3px;
  padding: 0 3px;
}
.cw-row.cw-agent .cw-msg-time { text-align: left; }
.cw-row.cw-user  .cw-msg-time { text-align: right; }

/* Typing indicator */
.cw-typing {
  display: flex;
  align-items: flex-end;
  gap: 9px;
  align-self: flex-start;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
.cw-typing.cw-typing-on { opacity: 1; }
.cw-typing-dots {
  display: flex;
  gap: 4px;
  background: #fff;
  padding: 10px 14px;
  border-radius: 4px 14px 14px 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.cw-typing-dot {
  width: 7px; height: 7px;
  background: var(--cw-muted);
  border-radius: 50%;
  animation: cw-bounce 1.35s infinite;
}
.cw-typing-dot:nth-child(2) { animation-delay: .18s; }
.cw-typing-dot:nth-child(3) { animation-delay: .36s; }
@keyframes cw-bounce {
  0%,60%,100% { transform:translateY(0); opacity:.4; }
  30%          { transform:translateY(-5px); opacity:1; }
}

/* Input area */
.cw-input-row {
  flex-shrink: 0;
  padding: 11px 13px;
  background: #fff;
  border-top: 1px solid var(--cw-border);
  display: flex;
  align-items: flex-end;
  gap: 9px;
}
.cw-textarea {
  flex: 1;
  border: 1.5px solid var(--cw-border);
  border-radius: 22px;
  padding: 9px 14px;
  font-size: 13.5px;
  color: var(--cw-text);
  background: var(--cw-surface);
  resize: none;
  outline: none;
  max-height: 96px;
  line-height: 1.5;
  font-family: inherit;
  transition: border-color .2s ease, background .2s ease;
  overflow-y: auto;
}
.cw-textarea::placeholder { color: var(--cw-muted); }
.cw-textarea:focus { border-color: var(--cw-primary); background: #fff; }

.cw-send-btn {
  width: 38px; height: 38px;
  flex-shrink: 0;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--cw-primary), var(--cw-primary-dark));
  border: none;
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  align-self: flex-end;
  transition: transform .2s ease, box-shadow .2s ease;
}
.cw-send-btn:hover  { transform: scale(1.09); box-shadow: 0 4px 14px rgba(14,165,233,.42); }
.cw-send-btn:active { transform: scale(.95); }

/* Powered by strip */
.cw-powered {
  flex-shrink: 0;
  text-align: center;
  font-size: 10.5px;
  color: var(--cw-muted);
  padding: 6px;
  background: #fff;
  border-top: 1px solid var(--cw-border);
}
.cw-powered strong { color: var(--cw-text); }

/* ── RESPONSIVE  ────────────────────────────────────────────── */
@media (max-width: 480px) {
  .cw-root {
    bottom: 16px;
    right: 16px;
  }
  .cw-panel {
    bottom: 0;
    right: 0;
    width: 100vw;
    max-height: 88vh;
    border-radius: var(--cw-radius) var(--cw-radius) 0 0;
  }
  .cw-bubble {
    max-width: calc(100vw - 80px);
  }
}
