/* ============================================================
   CHAT WIDGET — coerente con design system portfolio Achraf
   Variabili: --ink, --paper, --warm, --green, --green-mid,
              --green-light, --muted, --border
   Font: Syne (UI), DM Sans (testo)
   ============================================================ */

/* --- Trigger button --- */
.cw-trigger {
  position: fixed;
  bottom: 28px;
  right: 28px;
  z-index: 9998;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--green);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(28, 92, 68, 0.4);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  outline: none;
}

.cw-trigger:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 28px rgba(28, 92, 68, 0.5);
}

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

.cw-trigger svg {
  width: 24px;
  height: 24px;
  fill: #fff;
  transition: opacity 0.2s;
}

/* Pulse ring attorno al trigger */
.cw-trigger::before {
  content: '';
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  border: 2px solid rgba(28, 92, 68, 0.3);
  animation: cwPulseRing 2.5s ease-out infinite;
}

@keyframes cwPulseRing {
  0%   { transform: scale(1); opacity: 0.8; }
  70%  { transform: scale(1.3); opacity: 0; }
  100% { transform: scale(1.3); opacity: 0; }
}

/* Nascondi pulse quando la chat è aperta */
.cw-trigger.is-open::before {
  display: none;
}

/* --- Container principale --- */
.cw-container {
  position: fixed;
  bottom: 96px;
  right: 28px;
  z-index: 9999;
  width: 360px;
  max-height: 540px;
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 12px 48px rgba(10, 10, 10, 0.18);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Apertura animata */
  transform-origin: bottom right;
  transform: scale(0.85) translateY(12px);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.22s cubic-bezier(0.34, 1.56, 0.64, 1),
              opacity 0.18s ease;
}

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

/* --- Header --- */
.cw-header {
  background: var(--ink);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.cw-header-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--green);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.cw-header-avatar svg {
  width: 18px;
  height: 18px;
  fill: #fff;
}

.cw-header-info {
  flex: 1;
}

.cw-header-name {
  font-family: 'Syne', sans-serif;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  line-height: 1.2;
  letter-spacing: 0.02em;
}

.cw-header-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-family: 'DM Sans', sans-serif;
  font-size: 11px;
  color: rgba(255, 255, 255, 0.55);
  margin-top: 1px;
}

.cw-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green-light);
  animation: cwDotPulse 2s ease-in-out infinite;
  flex-shrink: 0;
}

@keyframes cwDotPulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.4; }
}

.cw-close {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  color: rgba(255, 255, 255, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s;
  outline: none;
}

.cw-close:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
}

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

/* --- Area messaggi --- */
.cw-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
  background: var(--warm);
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.cw-messages::-webkit-scrollbar {
  width: 4px;
}
.cw-messages::-webkit-scrollbar-track {
  background: transparent;
}
.cw-messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* --- Messaggi --- */
.cw-msg {
  display: flex;
  flex-direction: column;
  max-width: 82%;
  animation: cwMsgIn 0.22s ease;
}

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

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

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

.cw-msg-bubble {
  padding: 9px 13px;
  font-family: 'DM Sans', sans-serif;
  font-size: 13.5px;
  line-height: 1.5;
  word-break: break-word;
}

.cw-msg.bot .cw-msg-bubble {
  background: var(--green);
  color: #fff;
  border-radius: 4px 16px 16px 16px;
}

.cw-msg.user .cw-msg-bubble {
  background: var(--ink);
  color: #fff;
  border-radius: 16px 4px 16px 16px;
}

.cw-msg-time {
  font-family: 'DM Sans', sans-serif;
  font-size: 10px;
  color: var(--muted);
  margin-top: 3px;
  padding: 0 4px;
}

.cw-msg.user .cw-msg-time {
  text-align: right;
}

/* --- Typing indicator --- */
.cw-typing {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 13px;
  background: var(--green);
  border-radius: 4px 16px 16px 16px;
  width: fit-content;
  align-self: flex-start;
  animation: cwMsgIn 0.22s ease;
}

.cw-typing span {
  width: 6px;
  height: 6px;
  background: rgba(255, 255, 255, 0.7);
  border-radius: 50%;
  animation: cwTypingDot 1.2s ease-in-out infinite;
}

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

@keyframes cwTypingDot {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40%            { transform: translateY(-5px); opacity: 1; }
}

/* --- Pulsanti contatto (fine conversazione / fallback) --- */
.cw-contact-block {
  padding: 10px 14px 4px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  animation: cwMsgIn 0.25s ease;
}

.cw-contact-label {
  font-family: 'Syne', sans-serif;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  text-align: center;
}

.cw-btn-wa {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--green);
  color: #fff;
  font-family: 'Syne', sans-serif;
  font-size: 13px;
  font-weight: 700;
  padding: 10px 16px;
  border-radius: 8px;
  text-decoration: none;
  letter-spacing: 0.02em;
  transition: background 0.2s, transform 0.15s;
  border: none;
  cursor: pointer;
}

.cw-btn-wa:hover {
  background: var(--green-mid);
  transform: translateY(-1px);
}

.cw-btn-contacts {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: transparent;
  color: var(--ink);
  font-family: 'Syne', sans-serif;
  font-size: 13px;
  font-weight: 700;
  padding: 9px 16px;
  border-radius: 8px;
  text-decoration: none;
  letter-spacing: 0.02em;
  border: 1.5px solid rgba(10, 10, 10, 0.25);
  transition: border-color 0.2s, background 0.15s;
}

.cw-btn-contacts:hover {
  border-color: var(--ink);
  background: rgba(10, 10, 10, 0.04);
}

/* --- Banner limite sessione / blocco --- */
.cw-limit-banner {
  background: var(--warm);
  border-top: 1px solid var(--border);
  padding: 12px 14px;
  text-align: center;
  flex-shrink: 0;
}

.cw-limit-text {
  font-family: 'DM Sans', sans-serif;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 10px;
  line-height: 1.5;
}

/* --- Area input --- */
.cw-input-area {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  background: var(--paper);
  flex-shrink: 0;
}

.cw-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.cw-input {
  flex: 1;
  resize: none;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-family: 'DM Sans', sans-serif;
  font-size: 13.5px;
  color: var(--ink);
  background: var(--paper);
  line-height: 1.45;
  min-height: 38px;
  max-height: 100px;
  outline: none;
  transition: border-color 0.18s;
  overflow-y: auto;
}

.cw-input:focus {
  border-color: var(--green);
}

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

.cw-send {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--green);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.18s, transform 0.15s;
  outline: none;
}

.cw-send:hover:not(:disabled) {
  background: var(--green-mid);
  transform: scale(1.06);
}

.cw-send:disabled {
  background: rgba(10, 10, 10, 0.12);
  cursor: not-allowed;
  transform: none;
}

.cw-send svg {
  width: 16px;
  height: 16px;
  fill: #fff;
}

.cw-send:disabled svg {
  fill: rgba(10, 10, 10, 0.3);
}

.cw-char-count {
  font-family: 'DM Sans', sans-serif;
  font-size: 10.5px;
  color: var(--muted);
  text-align: right;
  margin-top: 4px;
  transition: color 0.15s;
}

.cw-char-count.over {
  color: #c0392b;
}

/* --- Responsive mobile --- */
@media (max-width: 480px) {
  .cw-container {
    right: 0;
    bottom: 0;
    width: 100vw;
    max-height: 90dvh;
    border-radius: 16px 16px 0 0;
    border-bottom: none;
  }

  .cw-trigger {
    bottom: 20px;
    right: 20px;
  }
}
