/* =========================================================================
   3L · Chat widget · Фаза 3 плана 2026-05-28-ai-chat-agent
   -------------------------------------------------------------------------
   Floating AI-консультант, доступный на основном сайте (не на /start,
   не на /brandbook). Полностью на токенах из tokens.css — никакого
   хардкода цвета и шрифтов.

   Структура:
     .chat-fab           — floating action button (правый-нижний)
     .chat-panel         — panel (desktop card / mobile full-screen)
       .chat-header
       .chat-body
         .chat-welcome
         .chat-msg --user / --assistant
         .chat-typing
       .chat-quick       — chip-row быстрых ответов
       .chat-lead        — inline-форма «оставить контакт»
       .chat-composer    — textarea + send

   Принципы:
   · editorial-сдержанность: cream-surface, dark-text, lime только как сигнал
   · одно display-element на viewport — Instrument Serif italic в welcome
   · никакого декоративного motion: open/close, scale на FAB, dots в typing
   · WCAG AA: текст на cream = invert (#111315), lime только на dark
   ========================================================================= */


/* -------------------------------------------------------------------------
   FAB · floating action button
   ------------------------------------------------------------------------- */

.chat-fab {
  position: fixed;
  right: clamp(16px, 3vw, 28px);
  bottom: clamp(16px, 3vw, 28px);
  z-index: 60;

  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: none;
  padding: 0;
  cursor: pointer;

  background: var(--color-base);
  color: var(--color-text);

  display: inline-flex;
  align-items: center;
  justify-content: center;

  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.18),
    0 12px 28px rgba(14, 16, 18, 0.22);

  transition:
    transform var(--dur-fast) var(--ease-default),
    box-shadow var(--dur-fast) var(--ease-default),
    background var(--dur-fast) var(--ease-default);
}

.chat-fab:hover {
  transform: translateY(-2px);
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.18),
    0 18px 36px rgba(14, 16, 18, 0.28);
}

.chat-fab:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring),
    0 12px 28px rgba(14, 16, 18, 0.22);
}

.chat-fab svg {
  width: 28px;
  height: 28px;
  display: block;
}

/* Pulse сидит чуть выпирая из правого-верхнего края FAB — играет роль
   sticker'а / status-indicator'а. Кольцо box-shadow цвета base даёт чистый
   срез на светлом фоне страницы. */
.chat-fab-pulse {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-base);
}

.chat-fab[hidden] {
  display: none !important;
}

/* при открытой панели FAB прячется (на mobile панель — full screen) */
.chat-widget.is-open .chat-fab {
  display: none;
}


/* -------------------------------------------------------------------------
   PANEL — карточка
   ------------------------------------------------------------------------- */

.chat-panel {
  position: fixed;
  right: clamp(16px, 3vw, 28px);
  bottom: clamp(16px, 3vw, 28px);
  z-index: 61;

  width: min(380px, calc(100vw - 32px));
  height: min(620px, calc(100vh - 56px));

  display: none;
  flex-direction: column;

  background: var(--color-surface);
  color: var(--color-text-invert);
  border-radius: var(--radius-md);
  overflow: hidden;

  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.12),
    0 30px 80px rgba(14, 16, 18, 0.28);

  font-family: var(--font-body);

  transform-origin: bottom right;
  transform: scale(0.96) translateY(8px);
  opacity: 0;
  transition:
    transform var(--dur-base) var(--ease-enter),
    opacity var(--dur-base) var(--ease-enter);
}

.chat-widget.is-open .chat-panel {
  display: flex;
  transform: scale(1) translateY(0);
  opacity: 1;
}

/* Mobile — full screen */
@media (max-width: 639px) {
  .chat-panel {
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100dvh;
    border-radius: 0;
    transform-origin: bottom center;
  }
}


/* -------------------------------------------------------------------------
   HEADER
   ------------------------------------------------------------------------- */

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);

  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-line-invert);

  background: var(--color-surface);
}

.chat-header-title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-family: var(--font-body);
  font-size: var(--fs-eyebrow);
  font-weight: var(--fw-medium);
  letter-spacing: var(--ls-eyebrow);
  text-transform: uppercase;
  color: var(--color-text-invert);
}

.chat-header-title .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(200, 255, 61, 0.18);
}

.chat-header-actions {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

.chat-icon-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  border: none;
  background: transparent;
  color: var(--color-text-invert-muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--dur-fast) var(--ease-default),
              color var(--dur-fast) var(--ease-default);
}

.chat-icon-btn:hover {
  background: var(--color-surface-2);
  color: var(--color-text-invert);
}

.chat-icon-btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.chat-icon-btn svg {
  width: 16px;
  height: 16px;
}


/* -------------------------------------------------------------------------
   BODY · scroll-зона с сообщениями
   ------------------------------------------------------------------------- */

.chat-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.chat-body::-webkit-scrollbar { width: 8px; }
.chat-body::-webkit-scrollbar-thumb {
  background: var(--color-line-invert);
  border-radius: var(--radius-pill);
}
.chat-body::-webkit-scrollbar-track { background: transparent; }


/* -------------------------------------------------------------------------
   WELCOME · первое appearance — editorial, одна строка serif italic
   ------------------------------------------------------------------------- */

.chat-welcome {
  padding: var(--space-2) 0 var(--space-3);
  border-bottom: 1px solid var(--color-line-invert);
  margin-bottom: var(--space-2);
}

.chat-welcome-eyebrow {
  font-size: var(--fs-eyebrow);
  letter-spacing: var(--ls-eyebrow);
  text-transform: uppercase;
  font-weight: var(--fw-medium);
  color: var(--color-text-invert-muted);
  margin-bottom: var(--space-2);
}

.chat-welcome-title {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 28px;
  line-height: 1.1;
  letter-spacing: var(--ls-heading);
  color: var(--color-text-invert);
  margin: 0 0 var(--space-2);
}

.chat-welcome-body {
  font-size: var(--fs-body-s);
  line-height: var(--lh-body);
  color: var(--color-text-invert-muted);
  margin: 0;
}


/* -------------------------------------------------------------------------
   MESSAGES
   ------------------------------------------------------------------------- */

.chat-msg {
  max-width: 92%;
  font-size: var(--fs-body-s);
  line-height: var(--lh-body);
  letter-spacing: var(--ls-body);
  word-wrap: break-word;
  white-space: pre-wrap;

  animation: chatMsgIn var(--dur-base) var(--ease-enter);
}

.chat-msg--user {
  align-self: flex-end;
  background: var(--color-base);
  color: var(--color-text);
  padding: var(--space-2) var(--space-3);
  border-radius: 14px 14px 4px 14px;
}

.chat-msg--assistant {
  align-self: flex-start;
  color: var(--color-text-invert);
  padding: 2px 0;
}

.chat-msg--error {
  align-self: flex-start;
  color: var(--color-text-invert-muted);
  font-style: italic;
  border-left: 2px solid var(--color-accent);
  padding-left: var(--space-2);
}

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

@media (prefers-reduced-motion: reduce) {
  .chat-msg { animation: none; }
  .chat-panel { transition: opacity var(--dur-fast) linear; transform: none; }
}


/* -------------------------------------------------------------------------
   TYPING indicator
   ------------------------------------------------------------------------- */

.chat-typing {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 0;
}

.chat-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-text-invert-muted);
  animation: chatDot 1.2s var(--ease-linear) infinite both;
}

.chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.chat-typing span:nth-child(3) { animation-delay: 0.30s; }

@keyframes chatDot {
  0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
  30%           { opacity: 1;    transform: translateY(-3px); }
}

@media (prefers-reduced-motion: reduce) {
  .chat-typing span { animation: none; opacity: 0.6; }
}


/* -------------------------------------------------------------------------
   QUICK REPLIES — chips
   ------------------------------------------------------------------------- */

.chat-quick {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  padding: 0 var(--space-4) var(--space-2);
}

.chat-quick[hidden] { display: none; }

.chat-quick-chip {
  font-family: var(--font-body);
  font-size: var(--fs-caption);
  line-height: 1;
  letter-spacing: var(--ls-body);
  padding: 8px 12px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--color-line-invert);
  background: transparent;
  color: var(--color-text-invert);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-default),
              color var(--dur-fast) var(--ease-default),
              background var(--dur-fast) var(--ease-default);
}

.chat-quick-chip:hover {
  border-color: var(--color-base);
  background: var(--color-base);
  color: var(--color-text);
}

.chat-quick-chip:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}


/* -------------------------------------------------------------------------
   COMPOSER · ввод
   ------------------------------------------------------------------------- */

.chat-composer {
  border-top: 1px solid var(--color-line-invert);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  display: flex;
  align-items: flex-end;
  gap: var(--space-2);
}

.chat-composer textarea {
  flex: 1 1 auto;
  resize: none;
  border: none;
  background: transparent;
  outline: none;
  font-family: var(--font-body);
  font-size: var(--fs-body-s);
  line-height: 1.4;
  color: var(--color-text-invert);
  padding: var(--space-2) 0;
  max-height: 120px;
  min-height: 36px;
}

.chat-composer textarea::placeholder {
  color: var(--color-text-invert-muted);
}

.chat-send {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--color-base);
  color: var(--color-text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--dur-fast) var(--ease-default),
              transform var(--dur-fast) var(--ease-default);
}

.chat-send:hover:not(:disabled) {
  background: var(--color-accent);
  color: var(--color-text-invert);
  transform: translateY(-1px);
}

.chat-send:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.chat-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.chat-send svg { width: 16px; height: 16px; }


/* -------------------------------------------------------------------------
   LEAD form · inline «оставить контакт»
   ------------------------------------------------------------------------- */

.chat-lead {
  align-self: stretch;
  background: var(--color-surface-2);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.chat-lead-title {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 20px;
  line-height: 1.1;
  color: var(--color-text-invert);
  margin: 0;
}

.chat-lead-body {
  font-size: var(--fs-caption);
  line-height: var(--lh-body);
  color: var(--color-text-invert-muted);
  margin: 0;
}

.chat-lead-field {
  display: block;
}

.chat-lead-field input {
  width: 100%;
  font-family: var(--font-body);
  font-size: var(--fs-body-s);
  line-height: 1.4;
  color: var(--color-text-invert);
  background: var(--color-surface);
  border: 1px solid var(--color-line-invert);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
  transition: border-color var(--dur-fast) var(--ease-default);
}

.chat-lead-field input:focus {
  border-color: var(--color-base);
}

.chat-lead-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-1);
}

.chat-lead-submit {
  font-family: var(--font-body);
  font-size: var(--fs-caption);
  font-weight: var(--fw-medium);
  letter-spacing: var(--ls-body);
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--color-base);
  color: var(--color-text);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease-default);
}

.chat-lead-submit:hover:not(:disabled) {
  background: var(--color-accent);
  color: var(--color-text-invert);
}

.chat-lead-submit:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.chat-lead-cancel {
  background: transparent;
  border: none;
  cursor: pointer;
  font-family: var(--font-body);
  font-size: var(--fs-caption);
  color: var(--color-text-invert-muted);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.chat-lead-error {
  font-size: var(--fs-caption);
  color: #B43434;
  margin: 0;
}

.chat-lead-success {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 18px;
  line-height: 1.2;
  color: var(--color-text-invert);
  margin: 0;
}


/* -------------------------------------------------------------------------
   FOOTER hint — мелким шрифтом
   ------------------------------------------------------------------------- */

.chat-footnote {
  font-size: 11px;
  letter-spacing: var(--ls-body);
  color: var(--color-text-invert-muted);
  text-align: center;
  padding: 6px var(--space-3) var(--space-2);
  background: var(--color-surface);
}

.chat-footnote a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}


/* -------------------------------------------------------------------------
   STATE flags
   ------------------------------------------------------------------------- */

.chat-widget[hidden] { display: none !important; }
