/* ==========================================================================
   Kids' Adventures Chatbot Widget
   White Minimalist Luxury Design System (Coral #C92A37 + Slate 900/700/500)
   Zero-dependency vanilla CSS — z-index 950 (above header 900, below drawer 9999)
   ========================================================================== */

#ka-chatbot-root {
  --ka-chat-coral: var(--color-accent-coral, #C92A37);
  --ka-chat-coral-hover: var(--color-accent-coral-hover, #A8202C);
  --ka-chat-coral-light: var(--color-accent-coral-light, #FFF1F2);
  --ka-chat-slate-900: var(--color-text-main, #0F172A);
  --ka-chat-slate-700: var(--color-text-body, #334155);
  --ka-chat-slate-500: var(--color-text-muted, #64748B);
  --ka-chat-surface: var(--color-bg-surface, #FFFFFF);
  --ka-chat-subtle: var(--color-bg-subtle, #F8FAFC);
  --ka-chat-muted: var(--color-bg-muted, #F1F5F9);
  --ka-chat-green: var(--color-accent-green, #047857);
  --ka-chat-green-dot: #10B981;
  --ka-chat-amber: var(--color-accent-amber, #F59E0B);
  --ka-chat-border: rgba(15, 23, 42, 0.1);
  --ka-chat-font-display: var(--font-display, 'Manrope', 'Inter', system-ui, sans-serif);
  --ka-chat-font-body: var(--font-body, 'Inter', system-ui, sans-serif);
  font-family: var(--ka-chat-font-body);
}

/* Keyframes */
@keyframes kaChatDot {
  0%, 80%, 100% {
    transform: translateY(0);
    opacity: 0.45;
  }
  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

@keyframes kaChatPulse {
  0% {
    box-shadow: 0 10px 26px -4px rgba(15, 23, 42, 0.16), 0 0 0 0 rgba(201, 42, 55, 0.34);
  }
  70% {
    box-shadow: 0 10px 26px -4px rgba(15, 23, 42, 0.16), 0 0 0 12px rgba(201, 42, 55, 0);
  }
  100% {
    box-shadow: 0 10px 26px -4px rgba(15, 23, 42, 0.16), 0 0 0 0 rgba(201, 42, 55, 0);
  }
}

@keyframes kaChatMsgIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ==========================================================================
   Launcher Pill
   ========================================================================== */
.ka-chat-launcher {
  position: fixed;
  right: 1rem;
  bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  z-index: 950;
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.45rem 1rem 0.45rem 0.45rem;
  border-radius: 9999px;
  border: 1.5px solid rgba(201, 42, 55, 0.25);
  background: var(--ka-chat-surface);
  color: var(--ka-chat-slate-900);
  font-family: var(--ka-chat-font-display);
  font-size: 0.875rem;
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.01em;
  cursor: pointer;
  box-shadow: 0 10px 26px -4px rgba(15, 23, 42, 0.16), 0 2px 8px -2px rgba(201, 42, 55, 0.12);
  animation: kaChatPulse 2.8s infinite;
  transition:
    transform 240ms cubic-bezier(0.34, 1.56, 0.64, 1),
    opacity 200ms ease,
    background-color 180ms ease,
    border-color 180ms ease;
}

.ka-chat-launcher:hover {
  background: var(--ka-chat-subtle);
  border-color: var(--ka-chat-coral);
  transform: translateY(-2px);
}

.ka-chat-launcher:active {
  transform: translateY(0) scale(0.97);
}

.ka-chat-launcher:focus-visible {
  outline: 3px solid var(--ka-chat-coral);
  outline-offset: 3px;
}

.ka-chat-launcher.is-hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateY(16px) scale(0.8);
  animation: none;
}

.ka-chat-launcher-avatar-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 9999px;
  background: #FFFFFF;
  padding: 3px;
  flex-shrink: 0;
  border: 1px solid rgba(15, 23, 42, 0.1);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

.ka-chat-launcher-avatar {
  width: 100%;
  height: 100%;
  border-radius: 9999px;
  object-fit: contain;
  display: block;
}

.ka-chat-online-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 11px;
  height: 11px;
  border-radius: 9999px;
  background: var(--ka-chat-green-dot);
  border: 2px solid #FFFFFF;
}

.ka-chat-launcher-label {
  white-space: nowrap;
}

/* ==========================================================================
   Chat Dialog Panel
   ========================================================================== */
.ka-chat-panel {
  position: fixed;
  right: 1rem;
  bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  z-index: 950;
  width: calc(100% - 2rem);
  max-width: 24.5rem;
  height: 74vh;
  max-height: 640px;
  min-height: 380px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-radius: 1.5rem;
  border: 1px solid rgba(201, 42, 55, 0.18);
  background: var(--ka-chat-surface);
  box-shadow: 0 24px 60px -12px rgba(15, 23, 42, 0.32), 0 4px 16px -4px rgba(15, 23, 42, 0.08);
  transform-origin: bottom right;
  opacity: 0;
  transform: translateY(28px) scale(0.92);
  pointer-events: none;
  visibility: hidden;
  transition:
    opacity 220ms ease,
    transform 280ms cubic-bezier(0.22, 1, 0.36, 1),
    visibility 220ms ease;
}

.ka-chat-panel.is-open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
  visibility: visible;
}

/* Header — Crisp White Surface with Signature Brand-Red Top Accent Bar */
.ka-chat-header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.9rem 1rem 0.85rem;
  background: var(--ka-chat-surface);
  color: var(--ka-chat-slate-900);
  border-top: 4px solid var(--ka-chat-coral);
  border-bottom: 1px solid rgba(15, 23, 42, 0.08);
  flex-shrink: 0;
}

.ka-chat-header-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.ka-chat-header-avatar-wrap {
  position: relative;
  width: 42px;
  height: 42px;
  border-radius: 9999px;
  background: #FFFFFF;
  padding: 3px;
  flex-shrink: 0;
  border: 1.5px solid rgba(201, 42, 55, 0.28);
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.06);
}

.ka-chat-header .ka-chat-online-dot {
  border-color: #FFFFFF;
}

.ka-chat-header-avatar {
  width: 100%;
  height: 100%;
  border-radius: 9999px;
  object-fit: contain;
  display: block;
}

.ka-chat-header-meta {
  min-width: 0;
  line-height: 1.25;
}

.ka-chat-header-title {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin: 0;
  font-family: var(--ka-chat-font-display);
  font-size: 0.95rem;
  font-weight: 800;
  color: var(--ka-chat-slate-900);
  letter-spacing: -0.01em;
}

.ka-chat-sparkle {
  width: 15px;
  height: 15px;
  color: var(--ka-chat-amber);
  flex-shrink: 0;
}

.ka-chat-header-role {
  margin: 0.15rem 0 0;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--ka-chat-slate-500);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ka-chat-header-actions {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  flex-shrink: 0;
}

.ka-chat-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 0.55rem;
  border: none;
  background: transparent;
  color: var(--ka-chat-slate-700);
  cursor: pointer;
  transition: background-color 150ms ease, color 150ms ease;
}

.ka-chat-icon-btn:hover {
  background: var(--ka-chat-muted);
  color: var(--ka-chat-slate-900);
}

.ka-chat-icon-btn:focus-visible {
  outline: 2px solid var(--ka-chat-coral);
  outline-offset: 1px;
}

/* ==========================================================================
   Messages Feed
   ========================================================================== */
.ka-chat-messages {
  flex: 1;
  overflow-y: auto;
  background: var(--ka-chat-subtle);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
}

.ka-chat-messages::-webkit-scrollbar {
  width: 6px;
}

.ka-chat-messages::-webkit-scrollbar-thumb {
  background: rgba(15, 23, 42, 0.14);
  border-radius: 9999px;
}

/* System Join Line */
.ka-chat-system {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.25rem 0;
  font-size: 0.7rem;
  color: var(--ka-chat-slate-500);
  text-align: center;
  animation: kaChatMsgIn 240ms ease both;
}

.ka-chat-system-avatar-wrap {
  width: 18px;
  height: 18px;
  border-radius: 9999px;
  background: #FFFFFF;
  padding: 2px;
  border: 1px solid rgba(15, 23, 42, 0.08);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.ka-chat-system-avatar {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 9999px;
}

/* Message Rows */
.ka-chat-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  animation: kaChatMsgIn 240ms ease both;
}

.ka-chat-row--user {
  justify-content: flex-end;
}

.ka-chat-row--bot {
  justify-content: flex-start;
}

.ka-chat-bot-avatar-wrap {
  width: 26px;
  height: 26px;
  border-radius: 9999px;
  background: #FFFFFF;
  padding: 3px;
  border: 1px solid rgba(15, 23, 42, 0.08);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  align-self: flex-end;
}

.ka-chat-bot-avatar {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 9999px;
}

.ka-chat-bubble {
  max-width: 86%;
  padding: 0.75rem 0.95rem;
  border-radius: 1rem;
  font-size: 0.875rem;
  line-height: 1.58;
  word-break: break-word;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
}

.ka-chat-bubble--bot {
  background: var(--ka-chat-surface);
  color: var(--ka-chat-slate-900);
  border: 1px solid rgba(15, 23, 42, 0.09);
  border-bottom-left-radius: 0.25rem;
}

.ka-chat-bubble--user {
  background: var(--ka-chat-coral);
  color: #FFFFFF;
  font-weight: 500;
  white-space: pre-wrap;
  border-bottom-right-radius: 0.25rem;
}

/* Structured Rich Text inside Bot Bubbles */
.ka-chat-p {
  margin: 0 0 0.55rem;
  color: var(--ka-chat-slate-700);
}

.ka-chat-p:first-child {
  color: var(--ka-chat-slate-900);
}

.ka-chat-p:last-child {
  margin-bottom: 0;
}

.ka-chat-list {
  list-style: none;
  margin: 0.35rem 0 0.65rem;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.ka-chat-list:last-child {
  margin-bottom: 0;
}

.ka-chat-li {
  position: relative;
  padding-left: 0.95rem;
  color: var(--ka-chat-slate-700);
  line-height: 1.5;
}

.ka-chat-li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0.52em;
  width: 5px;
  height: 5px;
  border-radius: 9999px;
  background: var(--ka-chat-coral);
}

.ka-chat-strong {
  font-weight: 700;
  color: var(--ka-chat-slate-900);
}

.ka-chat-link {
  color: var(--ka-chat-coral);
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.ka-chat-link:hover {
  color: var(--ka-chat-coral-hover);
}

/* Typing Indicator */
.ka-chat-typing {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 0.72rem 0.95rem;
  border-radius: 1rem;
  border-bottom-left-radius: 0.25rem;
  border: 1px solid rgba(15, 23, 42, 0.09);
  background: var(--ka-chat-surface);
}

.ka-chat-typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 9999px;
  background: #94A3B8;
  display: inline-block;
  animation: kaChatDot 1.2s infinite ease-in-out;
}

.ka-chat-typing-dot:nth-child(2) {
  animation-delay: 0.18s;
}

.ka-chat-typing-dot:nth-child(3) {
  animation-delay: 0.36s;
}

/* ==========================================================================
   Footer: Quick-Reply Menu & Input Controls
   ========================================================================== */
.ka-chat-footer {
  border-top: 1px solid var(--ka-chat-border);
  background: var(--ka-chat-surface);
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  flex-shrink: 0;
}

.ka-chat-options {
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(201, 42, 55, 0.2);
  border-radius: 0.8rem;
  overflow: hidden;
  max-height: 215px;
  overflow-y: auto;
  background: var(--ka-chat-surface);
  animation: kaChatMsgIn 220ms ease both;
}

.ka-chat-option-btn {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
  padding: 0.68rem 0.8rem;
  border: none;
  border-bottom: 1px solid rgba(201, 42, 55, 0.12);
  background: var(--ka-chat-surface);
  color: var(--ka-chat-slate-900);
  font-family: var(--ka-chat-font-body);
  font-size: 0.83rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  transition: background-color 150ms ease, color 150ms ease;
}

.ka-chat-option-btn:last-child {
  border-bottom: none;
}

.ka-chat-option-btn:hover {
  background: var(--ka-chat-coral-light);
}

.ka-chat-option-btn:focus-visible {
  outline: 2px solid var(--ka-chat-coral);
  outline-offset: -2px;
}

.ka-chat-option-icon {
  width: 16px;
  height: 16px;
  color: var(--ka-chat-coral);
  flex-shrink: 0;
}

.ka-chat-option-text {
  flex: 1;
  line-height: 1.3;
}

.ka-chat-option-chevron {
  width: 15px;
  height: 15px;
  color: var(--ka-chat-slate-500);
  flex-shrink: 0;
}

/* Form Input Row */
.ka-chat-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.ka-chat-input {
  flex: 1;
  min-width: 0;
  height: 44px;
  padding: 0 0.9rem;
  border-radius: 0.75rem;
  border: 1px solid rgba(15, 23, 42, 0.14);
  background: var(--ka-chat-subtle);
  color: var(--ka-chat-slate-900);
  font-family: var(--ka-chat-font-body);
  font-size: 0.875rem;
  outline: none;
  transition: border-color 150ms ease, box-shadow 150ms ease, background-color 150ms ease;
}

.ka-chat-input::placeholder {
  color: var(--ka-chat-slate-500);
}

.ka-chat-input:focus {
  border-color: var(--ka-chat-coral);
  background: #FFFFFF;
  box-shadow: 0 0 0 3px rgba(201, 42, 55, 0.16);
}

.ka-chat-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.ka-chat-send-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 0.75rem;
  border: none;
  background: var(--ka-chat-coral);
  color: #FFFFFF;
  flex-shrink: 0;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(201, 42, 55, 0.24);
  transition: background-color 150ms ease, opacity 150ms ease, transform 150ms ease;
}

.ka-chat-send-btn:hover:not(:disabled) {
  background: var(--ka-chat-coral-hover);
}

.ka-chat-send-btn:active:not(:disabled) {
  transform: scale(0.96);
}

.ka-chat-send-btn:disabled {
  opacity: 0.48;
  cursor: not-allowed;
  box-shadow: none;
}

.ka-chat-send-btn:focus-visible {
  outline: 2px solid var(--ka-chat-slate-900);
  outline-offset: 2px;
}

.ka-chat-hint-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  font-size: 0.7rem;
  color: var(--ka-chat-slate-500);
  text-align: center;
  margin: 0;
}

.ka-chat-cancel-lead {
  align-self: flex-start;
  padding: 0.15rem 0.25rem;
  font-size: 0.74rem;
  font-weight: 600;
  color: var(--ka-chat-slate-500);
  background: transparent;
  border: none;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.ka-chat-cancel-lead:hover {
  color: var(--ka-chat-coral);
}

/* ==========================================================================
   Responsive Breakpoints
   ========================================================================== */
@media (min-width: 640px) {
  .ka-chat-launcher {
    right: 1.5rem;
    bottom: 1.5rem;
  }

  .ka-chat-panel {
    right: 1.5rem;
    bottom: 1.5rem;
  }
}

@media (max-width: 480px) {
  .ka-chat-launcher {
    right: 0.75rem;
    bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
    padding: 0.35rem 0.85rem 0.35rem 0.35rem;
    font-size: 0.82rem;
    gap: 0.45rem;
  }

  .ka-chat-launcher-avatar-wrap {
    width: 32px;
    height: 32px;
  }

  .ka-chat-panel {
    right: 0.5rem;
    left: 0.5rem;
    bottom: calc(0.5rem + env(safe-area-inset-bottom, 0px));
    width: auto;
    max-width: none;
    height: min(78vh, 600px);
    border-radius: 1.25rem;
  }

  /* Prevent iOS zoom on focus */
  .ka-chat-input {
    font-size: 16px;
  }
}

/* ==========================================================================
   Reduced Motion
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  .ka-chat-launcher,
  .ka-chat-panel,
  .ka-chat-row,
  .ka-chat-system,
  .ka-chat-options,
  .ka-chat-typing-dot {
    animation: none !important;
    transition: none !important;
  }
}
