/*
 * PKPF Cricket Academy — "12th Man" Chat Widget
 * Brand-consistent, mobile-first, a11y-complete, reduced-motion aware.
 * Zero cost to LCP: nothing renders until the launcher button is clicked.
 *
 * Colour tokens match website brand palette:
 *   Dark background   #0A0A0A
 *   Brand red         #B91C1C
 *   Brand orange      #F97316
 *   Gold              #F59E0B
 *   Text light        #F5F5F5
 *   Muted             #9CA3AF
 */

/* ── Custom properties ──────────────────────────────────────────────────────── */
:root {
  --cw-bg:         #111111;
  --cw-surface:    #1A1A1A;
  --cw-border:     #2A2A2A;
  --cw-primary:    #F97316;   /* brand orange — active/interactive */
  --cw-accent:     #B91C1C;   /* brand red — launcher / destructive */
  --cw-text:       #F5F5F5;
  --cw-muted:      #9CA3AF;
  --cw-user-bg:    #F97316;
  --cw-user-text:  #0A0A0A;
  --cw-bot-bg:     #2A2A2A;
  --cw-bot-text:   #F5F5F5;
  --cw-radius:     0.75rem;
  --cw-radius-sm:  0.375rem;
  --cw-z:          9999;
  --cw-shadow:     0 8px 32px rgba(0,0,0,0.5);
  --cw-transition: 0.2s ease;
  --cw-panel-w:    360px;
  --cw-panel-h:    520px;
}

/* ── Launcher button ─────────────────────────────────────────────────────────── */
#cw-launcher {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  width: 3.25rem;
  height: 3.25rem;
  min-width: 44px;
  min-height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--cw-accent);
  color: #fff;
  font-size: 1.5rem;
  cursor: pointer;
  z-index: var(--cw-z);
  box-shadow: var(--cw-shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--cw-transition), transform var(--cw-transition);
  /* SVG cricket-ball icon rendered via JS — pure CSS fallback is the emoji */
}

#cw-launcher:hover,
#cw-launcher:focus-visible {
  background: #991B1B;
  outline: 3px solid var(--cw-primary);
  outline-offset: 2px;
  transform: scale(1.06);
}

#cw-launcher[aria-expanded="true"] {
  background: #374151;
}

/* ── Chat panel ──────────────────────────────────────────────────────────────── */
#cw-panel {
  position: fixed;
  bottom: 5.5rem;
  right: 1.5rem;
  width: var(--cw-panel-w);
  max-height: var(--cw-panel-h);
  display: flex;
  flex-direction: column;
  border-radius: var(--cw-radius);
  background: var(--cw-bg);
  border: 1px solid var(--cw-border);
  box-shadow: var(--cw-shadow);
  z-index: var(--cw-z);
  overflow: hidden;
  /* hidden until opened */
  visibility: hidden;
  opacity: 0;
  transform: translateY(8px) scale(0.97);
  transition: opacity var(--cw-transition), transform var(--cw-transition), visibility 0s var(--cw-transition);
  font-family: inherit;
}

#cw-panel.cw-open {
  visibility: visible;
  opacity: 1;
  transform: translateY(0) scale(1);
  transition: opacity var(--cw-transition), transform var(--cw-transition), visibility 0s 0s;
}

/* Mobile: full-width sheet */
@media (max-width: 420px) {
  #cw-panel {
    right: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    max-height: 90vh;
    border-radius: var(--cw-radius) var(--cw-radius) 0 0;
  }

  #cw-launcher {
    bottom: 1rem;
    right: 1rem;
  }
}

/* ── Panel header ────────────────────────────────────────────────────────────── */
#cw-header {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.875rem 1rem;
  background: var(--cw-surface);
  border-bottom: 1px solid var(--cw-border);
  flex-shrink: 0;
}

#cw-header-icon {
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  background: var(--cw-accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
}

#cw-header-info {
  flex: 1;
  min-width: 0;
}

#cw-header-name {
  font-weight: 700;
  font-size: 0.9375rem;
  color: var(--cw-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#cw-header-sub {
  font-size: 0.75rem;
  color: var(--cw-muted);
  white-space: nowrap;
}

#cw-close {
  background: none;
  border: none;
  color: var(--cw-muted);
  font-size: 1.25rem;
  cursor: pointer;
  padding: 0.25rem;
  border-radius: var(--cw-radius-sm);
  line-height: 1;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--cw-transition);
  flex-shrink: 0;
}

#cw-close:hover,
#cw-close:focus-visible {
  color: var(--cw-text);
  outline: 2px solid var(--cw-primary);
  outline-offset: 2px;
}

/* ── Messages area ───────────────────────────────────────────────────────────── */
#cw-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.875rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  scroll-behavior: smooth;
}

/* Custom scrollbar (cosmetic, non-blocking) */
#cw-messages::-webkit-scrollbar { width: 4px; }
#cw-messages::-webkit-scrollbar-track { background: transparent; }
#cw-messages::-webkit-scrollbar-thumb { background: var(--cw-border); border-radius: 2px; }

/* ── Individual message bubbles ───────────────────────────────────────────────── */
.cw-msg {
  max-width: 85%;
  padding: 0.5625rem 0.75rem;
  border-radius: var(--cw-radius);
  font-size: 0.875rem;
  line-height: 1.5;
  word-break: break-word;
}

.cw-msg-bot {
  background: var(--cw-bot-bg);
  color: var(--cw-bot-text);
  border-bottom-left-radius: var(--cw-radius-sm);
  align-self: flex-start;
}

.cw-msg-user {
  background: var(--cw-user-bg);
  color: var(--cw-user-text);
  border-bottom-right-radius: var(--cw-radius-sm);
  align-self: flex-end;
  font-weight: 500;
}

/* Typing indicator */
.cw-typing {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 0.625rem 0.75rem;
  background: var(--cw-bot-bg);
  border-radius: var(--cw-radius);
  border-bottom-left-radius: var(--cw-radius-sm);
  align-self: flex-start;
  max-width: 5rem;
}

.cw-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--cw-muted);
  animation: cw-bounce 1.2s infinite;
}

.cw-typing span:nth-child(2) { animation-delay: 0.2s; }
.cw-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes cw-bounce {
  0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
  40%           { transform: scale(1);   opacity: 1;   }
}

/* ── WhatsApp quick-action bar ───────────────────────────────────────────────── */
#cw-wa-bar {
  padding: 0.5rem 1rem;
  background: var(--cw-surface);
  border-top: 1px solid var(--cw-border);
  border-bottom: 1px solid var(--cw-border);
  flex-shrink: 0;
}

#cw-wa-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  width: 100%;
  padding: 0.5rem;
  background: #15803D;
  color: #fff;
  border: none;
  border-radius: var(--cw-radius-sm);
  font-size: 0.8125rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--cw-transition);
  min-height: 44px;
}

#cw-wa-btn:hover,
#cw-wa-btn:focus-visible {
  background: #166534;
  outline: 2px solid var(--cw-primary);
  outline-offset: 2px;
}

/* ── Lead capture prompt ─────────────────────────────────────────────────────── */
#cw-lead-prompt {
  padding: 0.75rem 1rem;
  background: var(--cw-surface);
  border-top: 1px solid var(--cw-border);
  display: none;   /* shown by JS after 2-3 exchanges */
  flex-shrink: 0;
}

#cw-lead-prompt p {
  font-size: 0.8125rem;
  color: var(--cw-muted);
  margin: 0 0 0.5rem;
}

#cw-lead-form {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.cw-lead-input {
  width: 100%;
  padding: 0.5rem 0.625rem;
  background: var(--cw-border);
  border: 1px solid #3A3A3A;
  border-radius: var(--cw-radius-sm);
  color: var(--cw-text);
  font-size: 0.8125rem;
  box-sizing: border-box;
}

.cw-lead-input:focus {
  outline: 2px solid var(--cw-primary);
  outline-offset: -1px;
  border-color: transparent;
}

.cw-lead-input::placeholder { color: var(--cw-muted); }

/* Honeypot field — visually hidden, not display:none (some bots detect that) */
.cw-hp {
  position: absolute;
  left: -9999px;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

#cw-lead-submit {
  padding: 0.5rem 0.75rem;
  background: var(--cw-primary);
  color: #0A0A0A;
  border: none;
  border-radius: var(--cw-radius-sm);
  font-size: 0.8125rem;
  font-weight: 700;
  cursor: pointer;
  min-height: 44px;
  transition: background var(--cw-transition);
}

#cw-lead-submit:hover,
#cw-lead-submit:focus-visible {
  background: #EA580C;
  outline: 2px solid #fff;
  outline-offset: 2px;
}

#cw-lead-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#cw-lead-dismiss {
  background: none;
  border: none;
  color: var(--cw-muted);
  font-size: 0.75rem;
  cursor: pointer;
  padding: 0.25rem 0;
  text-align: center;
  width: 100%;
  min-height: 44px;
}

#cw-lead-dismiss:hover,
#cw-lead-dismiss:focus-visible {
  color: var(--cw-text);
}

/* ── Input area ──────────────────────────────────────────────────────────────── */
/* Suggested-prompt chips — shown above the input, hidden after first message */
#cw-chips {
  display: none;
  flex-wrap: wrap;
  gap: 0.375rem;
  padding: 0.625rem 1rem 0;
  flex-shrink: 0;
}

#cw-chips.cw-chips-visible {
  display: flex;
}

.cw-chip {
  font: inherit;
  font-size: 0.8125rem;
  line-height: 1.2;
  color: var(--cw-text);
  background: var(--cw-surface);
  border: 1px solid var(--cw-border);
  border-radius: 999px;
  padding: 0.375rem 0.75rem;
  cursor: pointer;
  transition: background var(--cw-transition), border-color var(--cw-transition);
  max-width: 100%;
  text-align: left;
}

.cw-chip:hover {
  background: var(--cw-border);
  border-color: var(--cw-primary);
}

.cw-chip:focus-visible {
  outline: 2px solid var(--cw-primary);
  outline-offset: 2px;
}

#cw-input-area {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  background: var(--cw-surface);
  border-top: 1px solid var(--cw-border);
  flex-shrink: 0;
  align-items: flex-end;
}

#cw-input {
  flex: 1;
  padding: 0.5625rem 0.75rem;
  background: var(--cw-border);
  border: 1px solid #3A3A3A;
  border-radius: var(--cw-radius-sm);
  color: var(--cw-text);
  font-size: 0.875rem;
  resize: none;
  min-height: 40px;
  max-height: 100px;
  box-sizing: border-box;
  line-height: 1.4;
  font-family: inherit;
}

#cw-input:focus {
  outline: 2px solid var(--cw-primary);
  outline-offset: -1px;
  border-color: transparent;
}

#cw-input::placeholder { color: var(--cw-muted); }

#cw-send {
  width: 2.25rem;
  height: 2.25rem;
  min-width: 44px;
  min-height: 44px;
  border-radius: var(--cw-radius-sm);
  background: var(--cw-primary);
  color: #0A0A0A;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
  transition: background var(--cw-transition);
}

#cw-send:hover,
#cw-send:focus-visible {
  background: #EA580C;
  outline: 2px solid #fff;
  outline-offset: 2px;
}

#cw-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Reduced-motion overrides ────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  #cw-panel,
  #cw-launcher,
  #cw-wa-btn,
  #cw-lead-submit,
  #cw-send,
  #cw-close {
    transition: none;
  }

  .cw-typing span {
    animation: none;
    opacity: 1;
  }

  #cw-messages {
    scroll-behavior: auto;
  }
}
