.chat {
  position: fixed;
  right: max(var(--sp-4), var(--safe-right));
  bottom: calc(4rem + var(--safe-bottom));
  z-index: var(--z-chat);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--sp-3);
}

.chat__toggle {
  position: relative;
  width: 60px;
  height: 60px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  box-shadow: none;
  overflow: visible;
  transition: transform var(--transition);
  animation: chat-toggle-bob 2.4s ease-in-out infinite;
}

.chat__toggle-face {
  position: relative;
  z-index: 1;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
  background: #0aa8a0;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.22);
}

.chat__toggle-logo {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  /* сильніший crop — прибирає білу обводку квадратного файлу */
  transform: scale(1.42);
  pointer-events: none;
}

.chat__toggle-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid #0aa8a0;
  opacity: 0;
  pointer-events: none;
  animation: chat-toggle-ring 2.4s ease-out infinite;
}

.chat__toggle-ring--delay {
  animation-delay: 1.2s;
}

.chat__toggle:hover {
  transform: translateY(-3px) scale(1.04);
  animation-play-state: paused;
}

.chat__toggle:hover .chat__toggle-ring {
  animation-play-state: paused;
}

.chat__toggle:hover .chat__toggle-face {
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.28);
}

.chat.is-open .chat__toggle {
  animation: none;
}

.chat.is-open .chat__toggle-ring {
  animation: none;
  opacity: 0;
}

@keyframes chat-toggle-bob {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

@keyframes chat-toggle-ring {
  0% {
    transform: scale(1);
    opacity: 0.5;
  }
  70% {
    opacity: 0.1;
  }
  100% {
    transform: scale(1.55);
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat__toggle,
  .chat__toggle-ring {
    animation: none;
  }
}

.chat__panel {
  display: none;
  flex-direction: column;
  width: min(92vw, 360px);
  height: min(70vh, 520px);
  max-height: calc(100dvh - 120px);
  background: var(--c-card);
  border: 1px solid var(--c-line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

.chat.is-open .chat__panel {
  display: flex;
}

.chat__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  background: var(--c-accent);
  color: var(--c-btn-text);
}

.chat__title {
  font-size: var(--fs-md);
  font-weight: 600;
}

.chat__subtitle {
  font-size: var(--fs-xs);
  opacity: 0.8;
}

.chat__close {
  color: inherit;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
}

.chat__body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: var(--sp-3);
  background: var(--c-paper);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.chat__msg {
  max-width: 82%;
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--radius-md);
  font-size: var(--fs-sm);
  line-height: 1.45;
  word-break: break-word;
}

.chat__msg--customer {
  align-self: flex-end;
  background: var(--c-accent);
  color: var(--c-btn-text);
  border-bottom-right-radius: var(--sp-1);
}

.chat__msg--manager {
  align-self: flex-start;
  background: var(--c-card);
  border: 1px solid var(--c-line);
  border-bottom-left-radius: var(--sp-1);
}

.chat__msg--deleted {
  opacity: 0.85;
}

.chat__deleted {
  display: block;
  font-style: italic;
  color: var(--c-muted);
}

.chat__msg--customer.chat__msg--deleted .chat__deleted {
  color: rgba(255, 255, 255, 0.85);
}

.chat__time {
  display: block;
  margin-top: 2px;
  font-size: 10px;
  opacity: 0.7;
}

.chat__intro {
  padding: var(--sp-3);
  font-size: var(--fs-sm);
  color: var(--c-muted);
  text-align: center;
}

.chat__form {
  border-top: 1px solid var(--c-line-soft);
  padding: var(--sp-2);
  display: grid;
  gap: var(--sp-2);
  background: var(--c-card);
  flex: 0 0 auto;
  max-height: 48%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.chat__lead {
  display: grid;
  gap: var(--sp-2);
  grid-template-columns: 1fr 1fr;
}

.chat__lead[hidden] {
  display: none;
}

.chat__lead input,
.chat__input {
  width: 100%;
  min-height: 44px;
  padding: var(--sp-2) var(--sp-3);
  font-size: 16px;
  border: 1px solid var(--c-line);
  border-radius: var(--radius-md);
  -webkit-appearance: none;
  appearance: none;
}

.chat__row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  gap: var(--sp-2);
  align-items: center;
}

.chat__clip {
  position: relative;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid var(--c-line);
  border-radius: var(--radius-md);
  color: var(--c-head);
  background: var(--c-paper);
  -webkit-tap-highlight-color: transparent;
}

.chat__clip svg {
  pointer-events: none;
}

.chat__file-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  font-size: 16px;
  cursor: pointer;
}

.chat__hint {
  margin: 0;
  font-size: var(--fs-xs);
  line-height: 1.4;
  color: var(--c-muted);
}

.chat__draft {
  display: flex;
  gap: var(--sp-2);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.chat__draft[hidden] {
  display: none;
}

.chat__draft-item {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
  max-width: 100%;
  padding: 4px 6px 4px 4px;
  border: 1px solid var(--c-line);
  border-radius: var(--radius-md);
  background: var(--c-paper);
}

.chat__draft-item img {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 6px;
  flex: 0 0 40px;
}

.chat__draft-name {
  font-size: var(--fs-xs);
  line-height: 1.3;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat__draft-remove {
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
  border-radius: 6px;
  color: var(--c-muted);
  font-size: 18px;
  line-height: 1;
}

.chat__files {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  margin-top: var(--sp-1);
}

.chat__file {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.chat__file img {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-sm, 6px);
}

.chat__file--pdf {
  text-decoration: underline;
}

.file-preview {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  padding: calc(var(--safe-top, 0px) + var(--sp-3)) var(--sp-3) calc(var(--safe-bottom, 0px) + var(--sp-3));
  background: rgba(7, 24, 23, 0.88);
}

.file-preview[hidden] {
  display: none;
}

html.file-preview-open,
html.file-preview-open body {
  overflow: hidden;
}

.file-preview__bar {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 44px;
  color: #fff;
}

.file-preview__name {
  margin: 0;
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--fs-sm);
}

.file-preview button.file-preview__close,
a.file-preview__download {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 12px;
  border-radius: var(--radius-md);
  color: #111827;
  background: #fff;
  border: 1px solid #fff;
  text-decoration: none;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}

.file-preview__close {
  width: 44px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
}

.file-preview__stage {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.file-preview__img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
}

.file-preview__frame {
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: var(--radius-md);
  background: #fff;
}

.file-preview__img[hidden],
.file-preview__frame[hidden] {
  display: none;
}

.chat__send {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: var(--radius-md);
  background: var(--c-accent);
  color: var(--c-btn-text);
}

.chat__send:hover {
  background: var(--c-head);
}

.chat__error {
  font-size: var(--fs-xs);
  color: var(--c-sale);
  padding-inline: var(--sp-2);
}

@media (max-width: 600px) {
  .chat.is-open .chat__panel {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100dvh;
    max-height: none;
    border-radius: 0;
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
  }

  .chat.is-open .chat__toggle {
    display: none;
  }
}