/* ============================================================
   CaseReady Chat Widget — Brand Refresh
   Palette + typography sourced from frontend/css/main.css
   (CSS variables on :root cascade globally; no duplication needed).
   ============================================================ */

/* ---- Reset / Base ----------------------------------------- */
#cr-chat-widget * {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* ---- Floating trigger button ------------------------------ */
#cr-chat-trigger {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--accent-green);
  border: none;
  cursor: pointer;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0, 184, 148, 0.35),
              0 8px 20px rgba(0, 0, 0, 0.12);
  transition: box-shadow var(--transition-speed) ease,
              transform var(--transition-speed) ease;
}

#cr-chat-trigger:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 16px rgba(0, 184, 148, 0.5),
              0 10px 24px rgba(0, 0, 0, 0.15);
}

#cr-chat-trigger svg {
  width: 28px;
  height: 28px;
  fill: var(--white);
}

#cr-chat-trigger .cr-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 18px;
  height: 18px;
  background: var(--warning-red);
  border-radius: 50%;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  color: var(--white);
  font-family: var(--font-body);
  border: 2px solid var(--white);
}

#cr-chat-trigger .cr-badge.visible {
  display: flex;
}

/* ---- Chat panel ------------------------------------------- */
#cr-chat-panel {
  position: fixed;
  bottom: 100px;
  right: 24px;
  width: 380px;
  max-height: 620px;
  background: var(--white);
  border: 1px solid rgba(26, 35, 50, 0.08);
  border-radius: var(--border-radius);
  display: flex;
  flex-direction: column;
  z-index: 9998;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1),
              0 16px 40px rgba(0, 0, 0, 0.08);
  transform: translateY(20px);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.25s ease, opacity 0.25s ease;
  font-family: var(--font-body);
  color: var(--primary-navy);
}

#cr-chat-panel.open {
  transform: translateY(0);
  opacity: 1;
  pointer-events: all;
}

/* ---- Panel header ----------------------------------------- */
#cr-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--light-gray);
  flex-shrink: 0;
}

#cr-chat-title {
  color: var(--primary-navy);
  font-family: var(--font-heading);
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.3px;
}

#cr-chat-close {
  background: transparent;
  border: none;
  color: var(--neutral-gray);
  font-size: 18px;
  cursor: pointer;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  font-family: var(--font-body);
  line-height: 1;
}

#cr-chat-close:hover {
  background: var(--light-gray);
  color: var(--primary-navy);
}

/* ---- Brand avatar (P17) ----------------------------------- */
#cr-avatar-container {
  display: flex;
  justify-content: center;
  padding: 16px 0 12px;
  flex-shrink: 0;
}

/* Outer wrapper carries the idle pendulum sway. Pulse + glow run on the
   inner image so the two animations compose without transform conflict. */
.chatbot-avatar {
  width: 200px;
  height: 200px;
  position: relative;
  display: inline-block;
  flex-shrink: 0;
  animation: chatbot-avatar-sway 14s ease-in-out infinite;
  transform-origin: center center;
  will-change: transform;
}

.chatbot-avatar__image {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  transition: filter 200ms ease-out;
  will-change: transform, filter;
}

/* Idle: gentle pendulum sway — narrow arc (±18°), 14s round trip.
   75% reduction from earlier ±70°; subtle motion, easy on the eye. */
@keyframes chatbot-avatar-sway {
  0%   { transform: rotate(-18deg); }
  50%  { transform: rotate(18deg); }
  100% { transform: rotate(-18deg); }
}

/* Responding state — JS adds .is-responding on each bot reply and sets
   per-pulse CSS variables (--p1..--p5, --pulse-duration) so each pulse
   sequence differs in cadence and amplitude. ~2400ms target — long
   enough that the rhythm reads as "speaking" rather than a single twitch. */
.chatbot-avatar.is-responding .chatbot-avatar__image {
  animation:
    chatbot-avatar-pulse var(--pulse-duration, 2400ms) ease-out 1,
    chatbot-avatar-glow  var(--pulse-duration, 2400ms) ease-out 1;
}

/* Five-peak speech-rhythm pulse. Scale peaks are CSS vars with safe
   fallbacks; JS randomizes them per fire. */
@keyframes chatbot-avatar-pulse {
  0%   { transform: scale(1); }
  /* Pulse 1 — quick opener */
  9%   { transform: scale(var(--p1, 1.07)); }
  17%  { transform: scale(1); }
  /* Pulse 2 — quick second beat */
  26%  { transform: scale(var(--p2, 1.10)); }
  34%  { transform: scale(1); }
  /* Pulse 3 — small mid-phrase */
  46%  { transform: scale(var(--p3, 1.06)); }
  54%  { transform: scale(1); }
  /* Pulse 4 — emphatic peak */
  72%  { transform: scale(var(--p4, 1.16)); }
  /* Pulse 5 — settle */
  86%  { transform: scale(var(--p5, 1.04)); }
  100% { transform: scale(1); }
}

/* Violet glow breathes in rough sync with the five pulses but with its
   own irregular envelope, so glow + scale don't lock-step mechanically. */
@keyframes chatbot-avatar-glow {
  0%   { filter: drop-shadow(0 0 4px  rgba(124, 58, 237, 0.20)); }
  10%  { filter: drop-shadow(0 0 14px rgba(124, 58, 237, 0.55)); }
  20%  { filter: drop-shadow(0 0 8px  rgba(124, 58, 237, 0.35)); }
  30%  { filter: drop-shadow(0 0 16px rgba(124, 58, 237, 0.60)); }
  45%  { filter: drop-shadow(0 0 10px rgba(124, 58, 237, 0.40)); }
  55%  { filter: drop-shadow(0 0 14px rgba(124, 58, 237, 0.55)); }
  72%  { filter: drop-shadow(0 0 22px rgba(124, 58, 237, 0.75)); }
  85%  { filter: drop-shadow(0 0 12px rgba(124, 58, 237, 0.45)); }
  100% { filter: drop-shadow(0 0 0    rgba(124, 58, 237, 0));    }
}

/* Legacy STANDBY/PROCESSING indicator — hidden post-brand-refresh.
   JS still toggles its text/class harmlessly; visibility suppressed here
   to avoid touching chat-widget.js. */
#cr-status-label {
  display: none;
}

/* ---- Message area ----------------------------------------- */
#cr-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 120px;
  max-height: 200px;
  scrollbar-width: thin;
  scrollbar-color: rgba(99, 110, 114, 0.3) transparent;
}

#cr-messages::-webkit-scrollbar {
  width: 4px;
}

#cr-messages::-webkit-scrollbar-track {
  background: transparent;
}

#cr-messages::-webkit-scrollbar-thumb {
  background: rgba(99, 110, 114, 0.3);
  border-radius: 2px;
}

/* Message bubbles */
.cr-msg {
  display: flex;
  flex-direction: column;
  max-width: 86%;
  font-size: 14px;
  line-height: 1.5;
}

.cr-msg.user {
  align-self: flex-end;
  align-items: flex-end;
}

.cr-msg.bot {
  align-self: flex-start;
  align-items: flex-start;
}

.cr-msg-bubble {
  padding: 9px 13px;
  border-radius: 14px;
  font-family: var(--font-body);
  word-break: break-word;
}

.cr-msg.user .cr-msg-bubble {
  background: var(--accent-green);
  color: var(--white);
  border-bottom-right-radius: 4px;
}

.cr-msg.bot .cr-msg-bubble {
  background: var(--light-gray);
  color: var(--primary-navy);
  border-bottom-left-radius: 4px;
}

.cr-msg-time {
  font-size: 10px;
  color: var(--neutral-gray);
  margin-top: 3px;
  font-family: var(--font-body);
  opacity: 0.75;
}

/* Typing indicator */
.cr-typing-dots {
  display: inline-flex;
  gap: 4px;
  padding: 10px 13px;
  background: var(--light-gray);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
}

.cr-typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--neutral-gray);
  animation: cr-dot-pulse 1.2s infinite ease-in-out;
}

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

@keyframes cr-dot-pulse {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.8); }
  40%           { opacity: 1;    transform: scale(1); }
}

/* ---- Divider --------------------------------------------- */
.cr-divider {
  height: 1px;
  background: var(--light-gray);
  margin: 0 14px;
  flex-shrink: 0;
}

/* ---- Input area ------------------------------------------ */
#cr-input-area {
  display: flex;
  gap: 8px;
  padding: 12px 14px 14px;
  flex-shrink: 0;
}

#cr-input {
  flex: 1;
  background: var(--white);
  border: 1px solid rgba(26, 35, 50, 0.15);
  border-radius: var(--border-radius);
  color: var(--primary-navy);
  font-family: var(--font-body);
  font-size: 14px;
  padding: 9px 12px;
  outline: none;
  resize: none;
  height: 38px;
  transition: border-color 0.2s, box-shadow 0.2s;
  caret-color: var(--accent-green);
}

#cr-input::placeholder {
  color: var(--neutral-gray);
  opacity: 0.7;
}

#cr-input:focus {
  border-color: var(--accent-green);
  box-shadow: 0 0 0 3px rgba(0, 184, 148, 0.15);
}

#cr-send-btn {
  background: var(--accent-green);
  border: none;
  border-radius: var(--border-radius);
  color: var(--white);
  cursor: pointer;
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition-speed),
              box-shadow var(--transition-speed),
              transform var(--transition-speed);
  flex-shrink: 0;
}

#cr-send-btn:hover:not(:disabled) {
  transform: scale(1.05);
  box-shadow: 0 4px 8px rgba(0, 184, 148, 0.3);
}

#cr-send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#cr-send-btn svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

/* ---- Disclaimer footer ------------------------------------ */
#cr-disclaimer {
  font-family: var(--font-body);
  font-size: 10px;
  color: var(--neutral-gray);
  text-align: center;
  padding: 0 14px 12px;
  line-height: 1.5;
  flex-shrink: 0;
  letter-spacing: 0.3px;
}

/* ============================================================
   Mobile — Galaxy S9 and similar (max-width: 480px)
   ============================================================ */
@media (max-width: 480px) {
  #cr-chat-panel {
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    max-height: 90vh;
    border-radius: 16px 16px 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
  }

  #cr-chat-trigger {
    bottom: 16px;
    right: 16px;
    width: 52px;
    height: 52px;
  }

  .chatbot-avatar {
    width: 160px;
    height: 160px;
  }

  #cr-avatar-container {
    padding: 12px 0 8px;
  }

  #cr-messages {
    max-height: 160px;
  }

  #cr-input {
    font-size: 16px; /* Prevent iOS zoom */
  }
}

/* ============================================================
   Reduced motion
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .cr-typing-dots span {
    animation: none;
    opacity: 0.6;
  }
  /* No sway, no pulse, no glow animation — static avatar. */
  .chatbot-avatar,
  .chatbot-avatar.is-responding .chatbot-avatar__image {
    animation: none;
  }
  /* Static violet glow as a non-animated "responding" cue. */
  .chatbot-avatar.is-responding .chatbot-avatar__image {
    filter: drop-shadow(0 0 12px rgba(124, 58, 237, 0.55));
  }
}
