/**
 * D-Art Contact Widget — модалка заявки + плавающий AI-чат (glass, как на сайте).
 */

#cw-root.cw {
  --cw-ease: cubic-bezier(0.22, 1, 0.36, 1);
  --cw-dur: 0.38s;
  --cw-dur-chat: 0.48s;
  --cw-fab-size: 3.35rem;
  position: relative;
  z-index: var(--cw-z, 50);
  pointer-events: none;
}

.cw-fab,
.cw-chat,
.cw-start-modal {
  pointer-events: auto;
}

.cw-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1;
  background: rgba(15, 23, 42, 0.12);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 0;
  visibility: hidden;
  transition:
    opacity var(--cw-dur) var(--cw-ease),
    visibility var(--cw-dur) var(--cw-ease);
  pointer-events: none;
}

.cw-backdrop--visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.cw-start-modal {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 3;
  width: min(26rem, calc(100vw - 2rem));
  max-height: min(92vh, 36rem);
  overflow: auto;
  padding: 1.5rem 1.35rem 1.35rem;
  border-radius: var(--radius-lg, 24px);
  box-shadow:
    var(--shadow-elevated, 0 12px 35px rgba(15, 23, 42, 0.09)),
    inset 0 1px 0 rgba(255, 255, 255, 0.75);
  background: linear-gradient(
    165deg,
    rgba(255, 255, 255, 0.62) 0%,
    rgba(255, 255, 255, 0.48) 50%,
    rgba(248, 250, 252, 0.42) 100%
  );
  border: 1px solid rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  opacity: 0;
  visibility: hidden;
  transform: translate3d(-50%, calc(-50% + 14px), 0) scale(0.98);
  transition:
    opacity var(--cw-dur) var(--cw-ease),
    transform var(--cw-dur) var(--cw-ease),
    visibility var(--cw-dur) var(--cw-ease);
  pointer-events: none;
}

.cw-start-modal--open {
  opacity: 1;
  visibility: visible;
  transform: translate3d(-50%, -50%, 0) scale(1);
  pointer-events: auto;
}

.cw-start-modal__title {
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  color: var(--text, #0f172a);
  margin: 0 0 0.45rem;
  padding-right: 2rem;
  line-height: 1.2;
}

.cw-start-modal__lead {
  font-size: 0.9rem;
  line-height: 1.55;
  color: var(--muted, #475569);
  margin: 0 0 1.15rem;
}

.cw-start-form {
  margin: 0;
}

.cw-start-form__fields {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.cw-field {
  margin-bottom: 1rem;
}

.cw-field__label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: #334155;
  margin-bottom: 0.4rem;
}

.cw-input {
  width: 100%;
  margin: 0;
  padding: 0.78rem 0.95rem;
  border-radius: var(--radius-md, 18px);
  border: 1px solid rgba(15, 23, 42, 0.1);
  background: rgba(255, 255, 255, 0.72);
  color: var(--text, #0f172a);
  font-family: inherit;
  font-size: 0.95rem;
  line-height: 1.45;
  transition:
    border-color 0.2s ease,
    box-shadow 0.2s ease,
    background 0.2s ease;
  box-sizing: border-box;
}

.cw-input::placeholder {
  color: #94a3b8;
}

.cw-input:focus {
  outline: none;
  border-color: rgba(167, 139, 250, 0.55);
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.18);
  background: rgba(255, 255, 255, 0.92);
}

.cw-input--invalid {
  border-color: rgba(239, 68, 68, 0.55);
}

.cw-field__error {
  display: block;
  min-height: 1.15rem;
  margin-top: 0.35rem;
  font-size: 0.78rem;
  line-height: 1.35;
  color: #dc2626;
}

.cw-form__error {
  margin: 0 0 0.75rem;
  padding: 0.55rem 0.65rem;
  border-radius: 12px;
  font-size: 0.82rem;
  line-height: 1.45;
  color: #991b1b;
  background: rgba(254, 226, 226, 0.65);
  border: 1px solid rgba(252, 165, 165, 0.5);
}

.cw-start-form__submit {
  width: 100%;
  justify-content: center;
  margin-top: 0.25rem;
  padding: 0.92rem 1.2rem;
  font-size: 0.95rem;
  font-weight: 600;
}

.cw-start-form__submit:disabled {
  opacity: 0.72;
  cursor: not-allowed;
}

.cw-start-form__success {
  text-align: center;
  padding: 0.5rem 0.25rem 0.25rem;
  animation: cw-success-in 0.45s var(--cw-ease) both;
}

@keyframes cw-success-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.cw-start-form__success-icon {
  width: 3rem;
  height: 3rem;
  margin: 0 auto 0.85rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 1.35rem;
  font-weight: 700;
  color: #059669;
  background: linear-gradient(145deg, rgba(167, 250, 220, 0.9), rgba(167, 139, 250, 0.25));
  border: 1px solid rgba(16, 185, 129, 0.35);
  box-shadow: 0 8px 24px rgba(16, 185, 129, 0.15);
}

.cw-start-form__success-title {
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text, #0f172a);
  margin: 0 0 0.45rem;
}

.cw-start-form__success-lead {
  font-size: 0.9rem;
  line-height: 1.55;
  color: var(--muted, #475569);
  margin: 0 0 1.1rem;
}

.cw-start-form__success-btn {
  width: 100%;
  max-width: 14rem;
  justify-content: center;
  margin: 0 auto;
}

.cw-start-modal__close {
  position: absolute;
  top: 0.55rem;
  right: 0.55rem;
  width: 2.25rem;
  height: 2.25rem;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.55);
  color: #64748b;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.2s ease, color 0.2s ease, transform 0.2s ease;
}

.cw-start-modal__close:hover {
  background: rgba(255, 255, 255, 0.92);
  color: #0f172a;
}

.cw-start-modal__close:active {
  transform: scale(0.94);
}

.cw--reduce-motion .cw-start-modal,
.cw--reduce-motion .cw-backdrop {
  transition-duration: 0.01ms !important;
}

.cw--reduce-motion .cw-start-form__success {
  animation: none;
}

button.btn.btn-glass {
  font: inherit;
  cursor: pointer;
  color: inherit;
}

/* ---------- AI chat panel + FAB ---------- */

.cw-chat {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right, 0));
  bottom: calc(max(1rem, env(safe-area-inset-bottom, 0)) + var(--cw-fab-size) + 0.65rem);
  z-index: 4;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  width: min(22rem, calc(100vw - 2rem));
  max-height: min(78vh, 32rem);
  padding: 1.25rem 1.15rem 1.1rem;
  border-radius: var(--radius-lg, 24px);
  box-shadow: var(--shadow-soft, 0 12px 35px rgba(15, 23, 42, 0.09));
  opacity: 0;
  visibility: hidden;
  transform: translate3d(1.1rem, 1.35rem, 0) scale(0.97);
  transition:
    opacity var(--cw-dur-chat) var(--cw-ease),
    transform var(--cw-dur-chat) var(--cw-ease),
    visibility var(--cw-dur-chat) var(--cw-ease);
  pointer-events: none;
  will-change: opacity, transform;
}

.cw-chat--open {
  opacity: 1;
  visibility: visible;
  transform: translate3d(0, 0, 0) scale(1);
  pointer-events: auto;
  will-change: auto;
}

.cw-chat__title {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text, #0f172a);
  margin: 0;
  padding-right: 1.75rem;
}

.cw-chat__hint {
  font-size: 0.84rem;
  line-height: 1.5;
  color: var(--muted, #475569);
  margin: 0;
}

.cw-chat__thread {
  flex: 1 1 auto;
  min-height: 9rem;
  max-height: min(42vh, 16.5rem);
  overflow-x: hidden;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.55rem 0.45rem;
  margin: 0 -0.15rem;
  border-radius: var(--radius-md, 18px);
  background: rgba(248, 250, 252, 0.65);
  border: 1px solid rgba(15, 23, 42, 0.07);
  scroll-behavior: smooth;
}

.cw-chat__thread::-webkit-scrollbar {
  width: 6px;
}

.cw-chat__thread::-webkit-scrollbar-thumb {
  background: rgba(100, 116, 139, 0.35);
  border-radius: 99px;
}

.cw-msg-row {
  display: flex;
  width: 100%;
  flex-shrink: 0;
}

.cw-msg-row--user {
  justify-content: flex-end;
}

.cw-msg-row--assistant {
  justify-content: flex-start;
}

.cw-msg-row--typing {
  justify-content: flex-start;
}

.cw-msg {
  max-width: min(88%, 17rem);
  padding: 0.5rem 0.72rem;
  border-radius: 14px;
  font-size: 0.875rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  animation: cw-msg-enter 0.42s var(--cw-ease) both;
}

@keyframes cw-msg-enter {
  from {
    opacity: 0;
    transform: translate3d(0, 10px, 0);
  }
  to {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
}

.cw-msg--user {
  background: linear-gradient(145deg, rgba(167, 139, 250, 0.35), rgba(167, 139, 250, 0.18));
  color: var(--text, #0f172a);
  border: 1px solid rgba(167, 139, 250, 0.28);
  border-bottom-right-radius: 5px;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.6) inset;
}

.cw-msg--assistant {
  background: rgba(255, 255, 255, 0.92);
  color: var(--text, #0f172a);
  border: 1px solid rgba(15, 23, 42, 0.07);
  border-bottom-left-radius: 5px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.cw-typing {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.42rem 0.65rem;
  border-radius: 14px;
  border-bottom-left-radius: 5px;
  background: rgba(255, 255, 255, 0.85);
  border: 1px solid rgba(15, 23, 42, 0.07);
  font-size: 0.8rem;
  color: var(--muted, #64748b);
}

.cw-typing__dots {
  display: inline-flex;
  gap: 3px;
  align-items: center;
}

.cw-typing__dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: rgba(100, 116, 139, 0.55);
  animation: cw-typing-bounce 1.15s ease-in-out infinite;
}

.cw-typing__dot:nth-child(2) {
  animation-delay: 0.12s;
}

.cw-typing__dot:nth-child(3) {
  animation-delay: 0.24s;
}

.cw-typing__label {
  font-size: 0.78rem;
  letter-spacing: 0.01em;
}

@keyframes cw-typing-bounce {
  0%,
  70%,
  100% {
    transform: translateY(0);
    opacity: 0.55;
  }
  35% {
    transform: translateY(-3px);
    opacity: 1;
  }
}

#cw-root.cw--reduce-motion .cw-typing__dot {
  animation: none;
  opacity: 0.7;
}

.cw-chat__input {
  width: 100%;
  min-height: 4.25rem;
  max-height: 9rem;
  margin: 0;
  padding: 0.75rem 0.85rem;
  border-radius: var(--radius-md, 18px);
  border: 1px solid rgba(15, 23, 42, 0.1);
  background: rgba(255, 255, 255, 0.65);
  color: var(--text, #0f172a);
  font-family: inherit;
  font-size: 0.92rem;
  line-height: 1.5;
  resize: vertical;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.cw-chat__input::placeholder {
  color: #94a3b8;
}

.cw-chat__input:focus {
  outline: none;
  border-color: rgba(167, 139, 250, 0.55);
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.2);
}

.cw-chat__send {
  align-self: stretch;
  margin-top: 0.15rem;
}

.cw-chat__send:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.cw-chat__input:disabled {
  opacity: 0.75;
}

.cw-chat__close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  z-index: 2;
  width: 2.25rem;
  height: 2.25rem;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.55);
  color: #64748b;
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.2s ease, color 0.2s ease, transform 0.2s ease;
}

.cw-chat__close:hover {
  background: rgba(255, 255, 255, 0.92);
  color: #0f172a;
}

.cw-chat__close:active {
  transform: scale(0.94);
}

.cw-fab {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right, 0));
  bottom: max(1rem, env(safe-area-inset-bottom, 0));
  z-index: 5;
  width: var(--cw-fab-size);
  height: var(--cw-fab-size);
  padding: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  color: #334155;
  transition:
    transform 0.28s var(--cw-ease),
    box-shadow 0.28s var(--cw-ease);
  -webkit-tap-highlight-color: transparent;
  -webkit-appearance: none;
  appearance: none;
  border: none;
  background: linear-gradient(160deg, rgba(255, 255, 255, 0.92), rgba(255, 255, 255, 0.42));
  box-shadow:
    0 12px 32px rgba(15, 23, 42, 0.14),
    inset 0 1px 0 rgba(255, 255, 255, 0.95);
  touch-action: manipulation;
}

.cw-fab:focus {
  outline: none;
}

.cw-fab:focus-visible {
  outline: 2px solid rgba(167, 139, 250, 0.45);
  outline-offset: 3px;
}

.cw-fab__icon {
  display: flex;
  color: #1e293b;
}

.cw-fab:hover {
  box-shadow: 0 16px 38px rgba(167, 139, 250, 0.32);
}

.cw-fab:active {
  transform: scale(0.96);
}

.cw-fab[aria-expanded="true"] {
  color: #0f172a;
  box-shadow: 0 14px 36px rgba(167, 139, 250, 0.38);
}

@keyframes cw-fab-breathe {
  0%,
  100% {
    transform: translateY(0) scale(1);
    box-shadow: 0 12px 32px rgba(15, 23, 42, 0.14);
  }
  50% {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 16px 38px rgba(167, 139, 250, 0.22);
  }
}

#cw-root.cw:not(.cw--reduce-motion) .cw-fab[aria-expanded="false"] {
  animation: cw-fab-breathe 4.2s ease-in-out infinite;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.cw--reduce-motion .cw-fab {
  animation: none !important;
}

.cw--reduce-motion .cw-chat,
.cw--reduce-motion .cw-start-modal,
.cw--reduce-motion .cw-backdrop {
  transition-duration: 0.01ms !important;
}

#cw-root.cw--reduce-motion .cw-msg {
  animation: none;
}

@media (max-width: 380px) {
  .cw-chat {
    width: calc(100vw - 1.5rem);
    right: 0.75rem;
  }
}
