/* ============================================
   xatboot — n8n Chatbot Widget (Pipedrive-style)
   CSS Custom Properties — all tokens are
   overridable from the host site.
   ============================================ */

:root,
[data-cw-root] {
  /* — Brand — */
  --cw-primary: #0E5AF2;
  --cw-primary-hover: #0C4AD0;
  --cw-primary-light: #EEF2FB;

  /* — Surfaces — */
  --cw-bg: #ffffff;
  --cw-bg-overlay: rgba(0, 0, 0, 0.35);

  /* — Text — */
  --cw-text-primary: #1a1a1a;
  --cw-text-secondary: #666666;
  --cw-text-muted: #999999;
  --cw-text-on-primary: #ffffff;

  /* — Bubbles — */
  --cw-bubble-bot-bg: #f0f0f0;
  --cw-bubble-bot-text: #1a1a1a;
  --cw-bubble-user-bg: var(--cw-primary);
  --cw-bubble-user-text: #ffffff;

  /* — Layout — */
  --cw-toggle-size: 56px;
  --cw-panel-width: 380px;
  --cw-panel-height: 560px;
  --cw-panel-gap: 16px;
  --cw-radius-sm: 8px;
  --cw-radius-md: 12px;
  --cw-radius-lg: 18px;
  --cw-radius-full: 50%;
  --cw-bottom-offset: 20px;
  --cw-side-offset: 20px;

  /* — Shadows — */
  --cw-shadow-toggle: 0 4px 12px rgba(0, 0, 0, 0.15);
  --cw-shadow-panel: 0 8px 32px rgba(0, 0, 0, 0.18);

  /* — Animation — */
  --cw-duration: 0.3s;
  --cw-easing: cubic-bezier(0.4, 0, 0.2, 1);

  /* — Typography — */
  --cw-font: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
    Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue',
    sans-serif;
  --cw-font-size: 14px;
  --cw-font-size-sm: 12px;
  --cw-font-size-header: 16px;
  --cw-font-weight-normal: 400;
  --cw-font-weight-medium: 500;
  --cw-font-weight-semibold: 600;
  --cw-line-height: 1.5;
}

/* ============================================
   Base — reset only inside widget scope
   ============================================ */

.cw-widget *,
.cw-widget *::before,
.cw-widget *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.cw-widget {
  position: relative;
  z-index: 2147483000;
  font-family: var(--cw-font);
  font-size: var(--cw-font-size);
  line-height: var(--cw-line-height);
  color: var(--cw-text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ============================================
   Toggle button — floating circle
   ============================================ */

.cw-toggle-wrap {
  position: fixed;
  bottom: var(--cw-bottom-offset);
  right: var(--cw-side-offset);
  z-index: 2147483001;
  display: flex;
  align-items: center;
  gap: 10px;
}

.cw-toggle-wrap--left {
  right: auto;
  left: var(--cw-side-offset);
  flex-direction: row-reverse;
}

.cw-toggle-label {
  background: var(--cw-bg);
  color: var(--cw-text-primary);
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: var(--cw-font-weight-medium);
  white-space: nowrap;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  animation: cw-label-pulse 3s ease-in-out infinite;
  opacity: 1;
  transition: opacity var(--cw-duration) var(--cw-easing);
}

.cw-toggle-label:empty {
  display: none;
}

@keyframes cw-label-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.03); }
}

.cw-toggle-wrap--hidden .cw-toggle-label {
  opacity: 0;
  pointer-events: none;
}

.cw-toggle {
  width: var(--cw-toggle-size);
  height: var(--cw-toggle-size);
  border: none;
  border-radius: var(--cw-radius-full);
  background: var(--cw-primary);
  color: var(--cw-text-on-primary);
  cursor: pointer;
  box-shadow: var(--cw-shadow-toggle);
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    transform var(--cw-duration) var(--cw-easing),
    opacity var(--cw-duration) var(--cw-easing),
    box-shadow 0.2s ease;
  outline: none;
}

.cw-toggle:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px color-mix(in srgb, var(--cw-primary) 40%, transparent);
}

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

.cw-toggle:active {
  transform: scale(0.96);
}

.cw-toggle svg {
  width: 24px;
  height: 24px;
  fill: currentColor;
}

/* Position variants */
.cw-toggle-wrap--left {
  right: auto;
  left: var(--cw-side-offset);
}

/* Hidden state — when panel is open */
.cw-toggle--hidden {
  opacity: 0;
  transform: scale(0.8);
  pointer-events: none;
}

/* ============================================
   Panel — the chat window
   ============================================ */

.cw-panel {
  position: fixed;
  bottom: var(--cw-bottom-offset);
  right: var(--cw-side-offset);
  z-index: 2147483002;
  width: var(--cw-panel-width);
  height: var(--cw-panel-height);
  max-height: calc(100vh - var(--cw-bottom-offset) * 2);
  background: var(--cw-bg);
  border-radius: var(--cw-radius-md);
  box-shadow: var(--cw-shadow-panel);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(16px) scale(0.97);
  transition:
    opacity var(--cw-duration) var(--cw-easing),
    transform var(--cw-duration) var(--cw-easing);
  pointer-events: none;
}

.cw-panel--left {
  right: auto;
  left: var(--cw-side-offset);
}

.cw-panel--open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: all;
}

/* ============================================
   Header
   ============================================ */

.cw-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  flex-shrink: 0;
}

.cw-header__avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--cw-radius-full);
  object-fit: contain;
  flex-shrink: 0;
}

.cw-header__info {
  flex: 1;
  min-width: 0;
}

.cw-header__title {
  font-size: var(--cw-font-size-header);
  font-weight: var(--cw-font-weight-semibold);
  color: var(--cw-text-primary);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cw-header__subtitle {
  font-size: var(--cw-font-size-sm);
  color: var(--cw-text-muted);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cw-header__reset,
.cw-header__close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--cw-text-secondary);
  cursor: pointer;
  border-radius: var(--cw-radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease;
  outline: none;
  font-size: 16px;
}

.cw-header__reset:hover {
  background: rgba(0, 0, 0, 0.06);
  color: var(--cw-primary);
}

.cw-header__close:hover {
  background: rgba(0, 0, 0, 0.06);
}

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

.cw-header__close svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

/* ============================================
   Messages container
   ============================================ */

.cw-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

/* Custom scrollbar */
.cw-messages::-webkit-scrollbar {
  width: 6px;
}
.cw-messages::-webkit-scrollbar-track {
  background: transparent;
}
.cw-messages::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.12);
  border-radius: 3px;
}
.cw-messages::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.2);
}

/* ============================================
   Message bubbles
   ============================================ */

.cw-bubble {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: var(--cw-radius-lg);
  font-size: var(--cw-font-size);
  line-height: var(--cw-line-height);
  word-wrap: break-word;
  white-space: pre-wrap;
  animation: cw-fade-in 0.25s ease;
}

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

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

/* Typing indicator */
.cw-typing {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 16px;
  background: var(--cw-bubble-bot-bg);
  border-radius: var(--cw-radius-lg);
  border-bottom-left-radius: var(--cw-radius-sm);
  animation: cw-fade-in 0.2s ease;
}

.cw-typing__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--cw-text-muted);
  animation: cw-bounce 1.4s ease-in-out infinite;
}

.cw-typing__dot:nth-child(2) {
  animation-delay: 0.2s;
}

.cw-typing__dot:nth-child(3) {
  animation-delay: 0.4s;
}

/* ============================================
   Lead form
   ============================================ */

.cw-lead-form {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  gap: 14px;
}

.cw-lead-form__title {
  font-size: 16px;
  font-weight: var(--cw-font-weight-semibold);
  color: var(--cw-text-primary);
  margin-bottom: 4px;
  text-align: center;
}

.cw-lead-form__subtitle {
  font-size: var(--cw-font-size-sm);
  color: var(--cw-text-muted);
  text-align: center;
  max-width: 260px;
  margin-bottom: 8px;
  line-height: 1.4;
}

.cw-field {
  width: 100%;
  max-width: 280px;
}

.cw-field__input {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid rgba(0, 0, 0, 0.1);
  border-radius: var(--cw-radius-sm);
  font-family: var(--cw-font);
  font-size: var(--cw-font-size);
  line-height: var(--cw-line-height);
  color: var(--cw-text-primary);
  background: var(--cw-bg);
  outline: none;
  transition: border-color 0.15s ease;
}

.cw-field__input::placeholder {
  color: var(--cw-text-muted);
}

.cw-field__input:focus {
  border-color: var(--cw-primary);
}

.cw-lead-form__btn {
  width: 100%;
  max-width: 280px;
  padding: 11px 20px;
  border: none;
  border-radius: var(--cw-radius-sm);
  background: var(--cw-primary);
  color: white;
  font-family: var(--cw-font);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
  margin-top: 4px;
}

.cw-lead-form__btn:hover {
  background: var(--cw-primary-hover);
}

.cw-lead-form__btn:disabled {
  background: rgba(0, 0, 0, 0.1);
  color: var(--cw-text-muted);
  cursor: not-allowed;
}

/* ============================================
   Input area
   ============================================ */

.cw-input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid rgba(0, 0, 0, 0.06);
  flex-shrink: 0;
}

.cw-input__field {
  flex: 1;
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: var(--cw-radius-lg);
  padding: 9px 14px;
  font-family: var(--cw-font);
  font-size: var(--cw-font-size);
  line-height: var(--cw-line-height);
  color: var(--cw-text-primary);
  background: var(--cw-bg);
  resize: none;
  outline: none;
  max-height: 120px;
  min-height: 40px;
  transition: border-color 0.15s ease;
}

.cw-input__field::placeholder {
  color: var(--cw-text-muted);
}

.cw-input__field:focus {
  border-color: var(--cw-primary);
}

.cw-input__send {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--cw-radius-full);
  background: var(--cw-primary);
  color: var(--cw-text-on-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition:
    background 0.15s ease,
    transform 0.15s ease;
  outline: none;
}

.cw-input__send:hover {
  background: var(--cw-primary-hover);
}

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

.cw-input__send:active {
  transform: scale(0.92);
}

.cw-input__send:disabled {
  background: rgba(0, 0, 0, 0.1);
  color: var(--cw-text-muted);
  cursor: not-allowed;
}

.cw-input__send svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

/* ============================================
   Welcome / start screen
   ============================================ */

.cw-welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  text-align: center;
  gap: 12px;
}

.cw-welcome__icon {
  width: 56px;
  height: 56px;
  border-radius: var(--cw-radius-full);
  background: var(--cw-primary-light);
  display: flex;
  align-items: center;
  justify-content: center;
}

.cw-welcome__icon svg {
  width: 28px;
  height: 28px;
  fill: var(--cw-primary);
}

.cw-welcome__title {
  font-size: 18px;
  font-weight: var(--cw-font-weight-semibold);
  color: var(--cw-text-primary);
}

.cw-welcome__text {
  font-size: var(--cw-font-size);
  color: var(--cw-text-secondary);
  max-width: 280px;
  line-height: var(--cw-line-height);
}

.cw-welcome__btn {
  margin-top: 8px;
  padding: 10px 24px;
  border: none;
  border-radius: var(--cw-radius-lg);
  background: var(--cw-primary);
  color: var(--cw-text-on-primary);
  font-family: var(--cw-font);
  font-size: var(--cw-font-size);
  font-weight: var(--cw-font-weight-medium);
  cursor: pointer;
  transition: background 0.15s ease;
  outline: none;
}

.cw-welcome__btn:hover {
  background: var(--cw-primary-hover);
}

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

/* ============================================
   Error state
   ============================================ */

.cw-end-indicator {
  align-self: center;
  padding: 8px 14px;
  margin: 4px 0 8px 0;
  background: var(--cw-bg-secondary, #f5f5f5);
  color: var(--cw-text-muted, #666);
  border-radius: var(--cw-radius-md);
  font-size: var(--cw-font-size-xs, 11px);
  text-align: center;
  max-width: 90%;
  opacity: 0.7;
  animation: cw-fade-in 0.3s ease;
}

.cw-error {
  align-self: center;
  padding: 10px 14px;
  margin: 8px 0;
  background: #fff0f0;
  color: #c62828;
  border-radius: var(--cw-radius-md);
  font-size: var(--cw-font-size-sm);
  text-align: center;
  max-width: 90%;
  animation: cw-fade-in 0.25s ease;
}

/* ============================================
   Notifications (replace browser alerts)
   ============================================ */

.cw-notification {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  margin: 8px 16px;
  border-radius: var(--cw-radius-sm);
  font-size: var(--cw-font-size-sm);
  animation: cw-slide-down 0.25s ease;
  flex-shrink: 0;
}

.cw-notification--error {
  background: #fef2f2;
  color: #dc2626;
  border: 1px solid #fecaca;
}

.cw-notification--warning {
  background: #fffbeb;
  color: #d97706;
  border: 1px solid #fde68a;
}

.cw-notification--success {
  background: #f0fdf4;
  color: #16a34a;
  border: 1px solid #bbf7d0;
}

.cw-notification__icon {
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.cw-notification__text {
  flex: 1;
  line-height: 1.4;
}

.cw-notification__close {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  border: none;
  background: transparent;
  color: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--cw-radius-sm);
  opacity: 0.6;
  transition: opacity 0.15s ease;
  padding: 0;
}

.cw-notification__close:hover {
  opacity: 1;
}

.cw-notification__close svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}

@keyframes cw-slide-down {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ============================================
   Confirm dialog (replaces browser confirm)
   ============================================ */

.cw-confirm-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  animation: cw-fade-in 0.2s ease;
  border-radius: var(--cw-radius-md);
}

.cw-confirm {
  background: var(--cw-bg);
  border-radius: var(--cw-radius-md);
  padding: 20px;
  width: calc(100% - 32px);
  max-width: 280px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  animation: cw-scale-in 0.2s ease;
}

.cw-confirm__text {
  font-size: var(--cw-font-size);
  color: var(--cw-text-primary);
  text-align: center;
  margin-bottom: 16px;
  line-height: 1.5;
}

.cw-confirm__actions {
  display: flex;
  gap: 8px;
}

.cw-confirm__btn {
  flex: 1;
  padding: 10px 16px;
  border: none;
  border-radius: var(--cw-radius-sm);
  font-family: var(--cw-font);
  font-size: var(--cw-font-size);
  font-weight: var(--cw-font-weight-medium);
  cursor: pointer;
  transition: background 0.15s ease;
}

.cw-confirm__btn--cancel {
  background: var(--cw-smoke, #f0f0f0);
  color: var(--cw-text-primary);
}

.cw-confirm__btn--cancel:hover {
  background: #e0e0e0;
}

.cw-confirm__btn--ok {
  background: var(--cw-primary);
  color: var(--cw-text-on-primary);
}

.cw-confirm__btn--ok:hover {
  background: var(--cw-primary-hover);
}

@keyframes cw-scale-in {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* ============================================
   Animations
   ============================================ */

/* ============================================
   Loader
   ============================================ */

.cw-loader {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  animation: cw-fade-in 0.2s ease;
}

.cw-loader__spinner {
  width: 28px;
  height: 28px;
  border: 3px solid rgba(0, 0, 0, 0.08);
  border-top-color: var(--cw-primary);
  border-radius: 50%;
  animation: cw-spin 0.7s linear infinite;
}

@keyframes cw-spin {
  to { transform: rotate(360deg); }
}

@keyframes cw-fade-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes cw-bounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
  }
  30% {
    transform: translateY(-6px);
  }
}

/* ============================================
   Responsive — mobile fullscreen
   ============================================ */

@media (max-width: 480px) {
  .cw-toggle-wrap {
    bottom: 16px;
    right: 16px;
  }

  .cw-toggle-wrap--left {
    right: auto;
    left: 16px;
  }

  .cw-toggle-label {
    display: none;
  }

  .cw-panel {
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
  }
}
