/* ==========================================================
   Lumi — Floating support chat widget
   ========================================================== */

.chat-widget {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 55;
  font-family: inherit;
  --cw-accent: #0D9488;
  --cw-accent-dark: #0F766E;
  --cw-accent-faint: #F0FDFA;
  --cw-fg: #0F172A;
  --cw-muted: #64748B;
  --cw-border: #E2E8F0;
  --cw-surface: #FFFFFF;
  --cw-surface-2: #F1F5F9;
  --cw-bubble-user: #0D9488;
  --cw-bubble-ai: #F1F5F9;
}

/* ==========================================================
   Launcher button
   ========================================================== */

.chat-launcher {
  position: relative;
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 16px;
  background: var(--cw-accent);
  color: #FFFFFF;
  box-shadow:
    0 4px 12px rgba(13, 148, 136, 0.25),
    0 8px 24px rgba(13, 148, 136, 0.15);
  cursor: pointer;
  transition: transform 0.2s ease, background 0.2s ease, opacity 0.2s ease;
  animation: cw-launcher-in 0.5s cubic-bezier(0.16, 1, 0.3, 1) both;
  overflow: hidden;
  isolation: isolate;
}
.chat-launcher:hover {
  background: var(--cw-accent-dark);
  transform: translateY(-2px);
}
.chat-launcher:active {
  transform: scale(0.94);
}
.chat-launcher:focus-visible {
  outline: 3px solid rgba(13, 148, 136, 0.3);
  outline-offset: 3px;
}

.chat-launcher__icon {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s ease;
}
.chat-launcher__icon--chat {
  opacity: 1;
  transform: scale(1) rotate(0deg);
}
.chat-launcher__icon--close {
  opacity: 0;
  transform: scale(0.6) rotate(-45deg);
}

.chat-launcher__dot {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #10B981;
  border: 2px solid var(--cw-accent);
  z-index: 2;
}
.chat-launcher__dot::after {
  content: "";
  position: absolute;
  inset: -2px;
  border-radius: 50%;
  background: #10B981;
  opacity: 0.6;
  animation: cw-pulse 2s ease-out infinite;
}

@keyframes cw-pulse {
  0% { transform: scale(1); opacity: 0.6; }
  100% { transform: scale(2.4); opacity: 0; }
}

@keyframes cw-launcher-in {
  from { opacity: 0; transform: scale(0.5) translateY(20px); }
  to { opacity: 1; transform: none; }
}

/* ——— Hide launcher when chat is open (all viewports) ——— */
.chat-widget.is-open .chat-launcher {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.7);
  transition: opacity 0.15s ease, transform 0.2s ease;
}

/* ==========================================================
   Proactive greeting bubble
   ========================================================== */

.chat-greeting {
  position: absolute;
  bottom: 70px;
  right: 0;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  max-width: 260px;
  padding: 12px 14px;
  background: var(--cw-surface);
  color: var(--cw-fg);
  font-size: 0.875rem;
  line-height: 1.4;
  border-radius: 14px 14px 4px 14px;
  box-shadow: 0 4px 20px rgba(15, 23, 42, 0.1), 0 1px 3px rgba(15, 23, 42, 0.06);
  border: 1px solid var(--cw-border);
  transform-origin: bottom right;
  animation: cw-greeting-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) both;
  z-index: 1;
}
.chat-greeting.is-hidden {
  display: none;
}
.chat-greeting__text {
  flex: 1;
}
.chat-greeting__close {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: var(--cw-muted);
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s;
}
.chat-greeting__close:hover {
  background: var(--cw-surface-2);
  color: var(--cw-fg);
}

.chat-greeting::after {
  content: "";
  position: absolute;
  bottom: -8px;
  right: 16px;
  width: 12px;
  height: 12px;
  background: var(--cw-surface);
  border-right: 1px solid var(--cw-border);
  border-bottom: 1px solid var(--cw-border);
  transform: rotate(45deg);
}

@keyframes cw-greeting-in {
  from { opacity: 0; transform: scale(0.8) translateY(8px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

/* ==========================================================
   Chat panel
   ========================================================== */

.chat-panel {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 400px;
  height: 620px;
  max-height: calc(100vh - 120px);
  display: flex;
  flex-direction: column;
  background: var(--cw-surface);
  border: 1px solid var(--cw-border);
  border-radius: 20px;
  box-shadow: 0 24px 48px rgba(15, 23, 42, 0.16), 0 4px 12px rgba(15, 23, 42, 0.08);
  overflow: hidden;
  transform-origin: bottom right;
  transform: scale(0.9) translateY(20px);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease;
  z-index: 2;
}
.chat-widget.is-open .chat-panel {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: auto;
}

/* Header */
.chat-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--cw-border);
  background: var(--cw-surface);
  flex-shrink: 0;
}
.chat-panel__identity {
  display: flex;
  align-items: center;
  gap: 10px;
}
.chat-avatar {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--cw-accent-faint);
  color: var(--cw-accent);
  flex-shrink: 0;
}
.chat-panel__meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.chat-panel__meta strong {
  font-size: 0.875rem;
  font-weight: 700;
  color: var(--cw-fg);
  line-height: 1.2;
}
.chat-status {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.75rem;
  color: var(--cw-muted);
}
.chat-status__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #10B981;
  animation: cw-status-pulse 2s ease-in-out infinite;
}
@keyframes cw-status-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.chat-panel__close {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  color: var(--cw-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.chat-panel__close:hover {
  background: var(--cw-surface-2);
  color: var(--cw-fg);
}

/* Messages */
.chat-panel__messages {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
  background: var(--cw-surface);
}
.chat-panel__messages::-webkit-scrollbar {
  width: 6px;
}
.chat-panel__messages::-webkit-scrollbar-thumb {
  background: var(--cw-border);
  border-radius: 3px;
}

.chat-msg {
  display: flex;
  max-width: 85%;
  animation: cw-msg-in 0.32s cubic-bezier(0.16, 1, 0.3, 1) both;
}
.chat-msg--user {
  align-self: flex-end;
  justify-content: flex-end;
}
.chat-msg--ai {
  align-self: flex-start;
}
.chat-msg__bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 0.9375rem;
  line-height: 1.45;
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: pre-wrap;
}
.chat-msg--user .chat-msg__bubble {
  background: var(--cw-bubble-user);
  color: #FFFFFF;
  border-bottom-right-radius: 4px;
}
.chat-msg--ai .chat-msg__bubble {
  background: var(--cw-bubble-ai);
  color: var(--cw-fg);
  border-bottom-left-radius: 4px;
}
@keyframes cw-msg-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

/* Typing indicator */
.chat-typing {
  align-self: flex-start;
  display: inline-flex;
  gap: 4px;
  padding: 12px 16px;
  background: var(--cw-bubble-ai);
  border-radius: 16px 16px 16px 4px;
  animation: cw-msg-in 0.3s cubic-bezier(0.16, 1, 0.3, 1) both;
}
.chat-typing__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--cw-muted);
  animation: cw-typing-bounce 1.2s ease-in-out infinite;
}
.chat-typing__dot:nth-child(2) { animation-delay: 0.15s; }
.chat-typing__dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes cw-typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* Input */
.chat-panel__input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--cw-border);
  background: var(--cw-surface);
  flex-shrink: 0;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}
.chat-textarea {
  flex: 1;
  min-height: 40px;
  max-height: 96px;
  padding: 10px 14px;
  border: 1px solid var(--cw-border);
  border-radius: 20px;
  background: var(--cw-surface-2);
  color: var(--cw-fg);
  font-family: inherit;
  font-size: 0.9375rem;
  line-height: 1.4;
  resize: none;
  outline: none;
  transition: border-color 0.15s, background 0.15s;
  overflow-y: auto;
  scrollbar-width: thin;
}
.chat-textarea::placeholder {
  color: var(--cw-muted);
}
.chat-textarea:focus {
  border-color: var(--cw-accent);
  background: var(--cw-surface);
}
.chat-textarea:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.chat-send {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--cw-accent);
  color: #FFFFFF;
  cursor: pointer;
  transition: background 0.15s, transform 0.15s;
}
.chat-send:hover:not(:disabled) {
  background: var(--cw-accent-dark);
  transform: scale(1.05);
}
.chat-send:active:not(:disabled) {
  transform: scale(0.95);
}
.chat-send:disabled {
  background: var(--cw-surface-2);
  color: var(--cw-muted);
  cursor: not-allowed;
}

/* ==========================================================
   Mobile — full-screen
   ========================================================== */

@media (max-width: 600px) {
  .chat-widget {
    bottom: 84px;
    right: 16px;
  }

  .chat-panel {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100%;
    max-height: none;
    border-radius: 0;
    border: none;
    transform: translateY(100%);
    transform-origin: bottom center;
    z-index: 9999;
  }
  .chat-widget.is-open .chat-panel {
    transform: translateY(0);
  }
  .chat-msg__bubble {
    font-size: 0.9375rem;
  }

  /* The input area rides above the keyboard */
  .chat-panel__input {
    padding-bottom: calc(
      12px + var(--keyboard-inset, 0px) + env(safe-area-inset-bottom)
    );
  }
}

/* ==========================================================
   Body scroll lock while chat is open (mobile)
   ========================================================== */

@media (max-width: 600px) {
  html.has-chat-open,
  body.has-chat-open {
    overflow: hidden;
    overscroll-behavior: none;
  }
}

/* ==========================================================
   Body scroll lock while chat is open (mobile)
   ========================================================== */

@media (max-width: 600px) {
  html.has-chat-open,
  body.has-chat-open {
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
  }
}

/* ==========================================================
   Body scroll lock while chat is open (mobile)
   ========================================================== */

@media (max-width: 600px) {
  html.has-chat-open,
  body.has-chat-open {
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
  }
}
/* ==========================================================
   Hidden state
   ========================================================== */

.chat-widget.is-disabled .chat-launcher { display: none; }
.chat-widget.is-disabled .chat-greeting { display: none; }

body.has-modal-open .chat-widget {
  opacity: 0;
  pointer-events: none;
  transform: translateY(20px);
  transition: opacity 0.2s, transform 0.2s;
}

/* ==========================================================
   Reduced motion
   ========================================================== */

@media (prefers-reduced-motion: reduce) {
  .chat-launcher,
  .chat-panel,
  .chat-msg,
  .chat-typing,
  .chat-greeting {
    animation: none !important;
    transition: opacity 0.1s !important;
  }
  .chat-launcher__dot::after,
  .chat-status__dot,
  .chat-typing__dot {
    animation: none !important;
  }
  .chat-typing__dot {
    opacity: 0.6;
  }
}
/* ==========================================================
   Mobile — lock body scroll when chat is open
   ========================================================== */

@media (max-width: 600px) {
  html.has-chat-open,
  body.has-chat-open {
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
  }
}
