/* ==========================================================================
   animations.css — плавные анимации, GIF-библиотека, голосовые сообщения.
   Подключается ПОСЛЕ style.css и только дополняет его. Чтобы отключить всё
   разом — уберите <link> на этот файл в index.html.
   ========================================================================== */

:root {
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-smooth: cubic-bezier(0.4, 0, 0.2, 1);
}

/* --------------------------------------------------------------------------
   1. Плавная смена темы (класс вешается на <html> на ~0.5 с из JS)
   -------------------------------------------------------------------------- */
html.theme-transition *,
html.theme-transition *::before,
html.theme-transition *::after {
  transition: background-color 0.45s var(--ease-smooth), border-color 0.45s var(--ease-smooth),
    color 0.35s var(--ease-smooth), fill 0.35s var(--ease-smooth), box-shadow 0.45s var(--ease-smooth) !important;
}

/* --------------------------------------------------------------------------
   2. Сообщения: появляются только НОВЫЕ (а не вся история при открытии чата)
   -------------------------------------------------------------------------- */
.message-bubble { animation: none; }

.message-bubble.msg-enter {
  animation: msgInRight 0.38s var(--ease-spring) both;
  transform-origin: right bottom;
}
.message-bubble.incoming.msg-enter {
  animation-name: msgInLeft;
  transform-origin: left bottom;
}
@keyframes msgInRight {
  from { opacity: 0; transform: translate(18px, 14px) scale(0.86); }
  to   { opacity: 1; transform: none; }
}
@keyframes msgInLeft {
  from { opacity: 0; transform: translate(-18px, 14px) scale(0.86); }
  to   { opacity: 1; transform: none; }
}

/* Плавное появление переписки при переключении чата */
.chat-switch { animation: chatSwitch 0.34s var(--ease-out); }
@keyframes chatSwitch {
  from { opacity: 0; transform: translateX(14px); }
  to   { opacity: 1; transform: none; }
}

.messages-container { scroll-behavior: auto; }

/* Плавное появление «Сегодня» */
.date-divider { animation: fadeIn 0.4s var(--ease-out) both; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* --------------------------------------------------------------------------
   3. Список чатов
   -------------------------------------------------------------------------- */
.chat-item {
  transition: background-color 0.2s var(--ease-smooth), transform 0.22s var(--ease-out);
}
.chat-item:hover { transform: translateX(3px); }
.chat-item:active { transform: scale(0.98); }

/* каскадное появление при первой загрузке */
.chat-list.stagger .chat-item { animation: itemIn 0.45s var(--ease-out) both; }
.chat-list.stagger .chat-item:nth-child(1)  { animation-delay: 0ms; }
.chat-list.stagger .chat-item:nth-child(2)  { animation-delay: 40ms; }
.chat-list.stagger .chat-item:nth-child(3)  { animation-delay: 80ms; }
.chat-list.stagger .chat-item:nth-child(4)  { animation-delay: 120ms; }
.chat-list.stagger .chat-item:nth-child(5)  { animation-delay: 160ms; }
.chat-list.stagger .chat-item:nth-child(6)  { animation-delay: 200ms; }
.chat-list.stagger .chat-item:nth-child(7)  { animation-delay: 240ms; }
.chat-list.stagger .chat-item:nth-child(8)  { animation-delay: 280ms; }
.chat-list.stagger .chat-item:nth-child(n+9) { animation-delay: 320ms; }
@keyframes itemIn {
  from { opacity: 0; transform: translateX(-16px); }
  to   { opacity: 1; transform: none; }
}

.unread-badge { animation: badgePop 0.35s var(--ease-spring); }
@keyframes badgePop { from { transform: scale(0); } to { transform: scale(1); } }

/* --------------------------------------------------------------------------
   4. Кнопки: мягкий отклик на наведение и нажатие
   -------------------------------------------------------------------------- */
.icon-btn, .rail-btn, .input-action-btn, .send-btn, .btn-primary, .btn-secondary,
.popover-item, .emoji-cat-btn, .folder-tab, .auth-tab-btn {
  transition: transform 0.2s var(--ease-spring), background-color 0.2s var(--ease-smooth),
    color 0.2s var(--ease-smooth), box-shadow 0.25s var(--ease-smooth), border-color 0.2s var(--ease-smooth),
    opacity 0.2s var(--ease-smooth);
}
.icon-btn:hover, .rail-btn:hover, .input-action-btn:hover { transform: translateY(-1px) scale(1.06); }
.icon-btn:active, .rail-btn:active, .input-action-btn:active,
.btn-primary:active, .btn-secondary:active, .folder-tab:active { transform: scale(0.92); }
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 6px 18px var(--accent-ring); }
.send-btn:hover { transform: scale(1.08) rotate(-8deg); }
.send-btn:active { transform: scale(0.9); }
.popover-item:hover { transform: translateX(3px); }

/* Иконка на кнопке темы поворачивается */
#theme-toggle .icon { transition: transform 0.5s var(--ease-spring); }
#theme-toggle:hover .icon { transform: rotate(25deg); }

/* --------------------------------------------------------------------------
   5. Модальные окна, панели, всплывающие меню
   -------------------------------------------------------------------------- */
.modal-backdrop.active { animation: backdropIn 0.25s var(--ease-smooth) both; }
@keyframes backdropIn { from { opacity: 0; } to { opacity: 1; } }

.modal-dialog { animation: modalPop 0.42s var(--ease-spring) both; }
@keyframes modalPop {
  from { opacity: 0; transform: translateY(26px) scale(0.92); }
  to   { opacity: 1; transform: none; }
}

.popover, .emoji-picker-popover {
  transform-origin: left bottom;
  animation: popIn 0.28s var(--ease-spring) both;
}
.emoji-picker-popover { transform-origin: right bottom; }
@keyframes popIn {
  from { opacity: 0; transform: translateY(14px) scale(0.92); }
  to   { opacity: 1; transform: none; }
}

.info-drawer:not(.hidden) { animation: drawerIn 0.35s var(--ease-out) both; }
@keyframes drawerIn {
  from { opacity: 0; transform: translateX(28px); }
  to   { opacity: 1; transform: none; }
}

.attachment-preview-bar { animation: slideUpIn 0.3s var(--ease-out) both; }
@keyframes slideUpIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: none; }
}

.lightbox-img { animation: zoomIn 0.4s var(--ease-spring) both; }
@keyframes zoomIn {
  from { opacity: 0; transform: scale(0.85); }
  to   { opacity: 1; transform: none; }
}

.emoji-item { transition: transform 0.15s var(--ease-spring), background-color 0.15s; }
.emoji-item:hover { transform: scale(1.25); }

/* Заглушка «Выберите чат» */
.chat-placeholder > *, #chat-placeholder > * { animation: slideUpIn 0.6s var(--ease-out) both; }

/* --------------------------------------------------------------------------
   6. GIF в сообщениях и библиотека GIF
   -------------------------------------------------------------------------- */
.message-media-container.is-gif { position: relative; }
.message-media-container.is-gif .message-media-img {
  max-width: 260px; min-width: 120px; transition: transform 0.25s var(--ease-out);
}
.message-media-container.is-gif .message-media-img:hover { transform: scale(1.015); }

.gif-badge {
  position: absolute; left: 8px; top: 8px; padding: 2px 7px;
  font-size: 10.5px; font-weight: 800; letter-spacing: 0.04em; color: #fff;
  background: rgba(0, 0, 0, 0.55); border-radius: 6px; pointer-events: none;
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.gif-save-btn {
  position: absolute; right: 8px; top: 8px; width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  color: #fff; background: rgba(0, 0, 0, 0.55); border-radius: 50%;
  opacity: 0; transform: scale(0.7);
  transition: opacity 0.2s, transform 0.25s var(--ease-spring), background-color 0.2s;
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.message-media-container.is-gif:hover .gif-save-btn,
.gif-save-btn:focus-visible { opacity: 1; transform: scale(1); }
.gif-save-btn:hover { background: var(--accent-blue); }
.gif-save-btn.saved { background: var(--online-green, #22c55e); opacity: 1; transform: scale(1); }
@media (hover: none) { .gif-save-btn { opacity: 1; transform: scale(1); } }

.gif-btn { width: auto; min-width: 38px; padding: 0 8px; font-size: 11.5px; font-weight: 800; letter-spacing: 0.04em; }
.gif-btn.active { color: var(--accent-blue-light); background: var(--bg-hover); }

.gif-picker-popover { width: 360px; height: 400px; }
.gif-picker-header { display: flex; align-items: center; gap: 6px; }
.gif-picker-header .emoji-search-box { flex: 1; min-width: 0; }
.gif-upload-btn { flex-shrink: 0; }

.gif-grid {
  flex: 1; min-height: 0; overflow-y: auto; padding: 8px 10px 12px;
  display: grid; grid-template-columns: repeat(2, 1fr); grid-auto-rows: 124px; gap: 8px; align-content: start;
}
.gif-cell {
  position: relative; height: 100%; padding: 0; overflow: hidden;
  border-radius: var(--radius-md); background: var(--input-bg);
  animation: gifCellIn 0.4s var(--ease-out) both; animation-delay: calc(var(--i, 0) * 30ms);
  transition: transform 0.22s var(--ease-spring), box-shadow 0.22s;
}
.gif-cell img { width: 100%; height: 100%; object-fit: cover; display: block; }
.gif-cell:hover { transform: scale(1.04); box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35); z-index: 1; }
.gif-cell:active { transform: scale(0.96); }
@keyframes gifCellIn { from { opacity: 0; transform: scale(0.9); } to { opacity: 1; transform: none; } }

.gif-skeleton {
  animation: none;
  background: linear-gradient(100deg, var(--input-bg) 30%, var(--bg-hover) 50%, var(--input-bg) 70%);
  background-size: 200% 100%; animation: shimmer 1.2s linear infinite;
}
@keyframes shimmer { from { background-position: 120% 0; } to { background-position: -120% 0; } }

.gif-empty {
  grid-column: 1 / -1; padding: 48px 16px; text-align: center;
  color: var(--text-muted); font-size: 13.5px; line-height: 1.6;
}

/* --------------------------------------------------------------------------
   7. Голосовые сообщения: плеер
   -------------------------------------------------------------------------- */
.voice-player { display: flex; align-items: center; gap: 12px; min-width: 230px; padding: 2px 0; }
.voice-play-btn {
  position: relative; width: 42px; height: 42px; flex-shrink: 0; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent-blue); color: #fff;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.14), 0 2px 8px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s var(--ease-spring), background-color 0.2s;
}
.voice-play-btn:hover { transform: scale(1.08); }
.voice-play-btn:active { transform: scale(0.92); }
.voice-play-btn .icon { position: absolute; width: 18px; height: 18px; fill: currentColor; stroke: none; transition: opacity 0.2s, transform 0.25s var(--ease-spring); }
.voice-play-btn .voice-icon-play { margin-left: 2px; }
.voice-play-btn .voice-icon-pause { opacity: 0; transform: scale(0.4) rotate(-45deg); }
.voice-player.playing .voice-icon-play { opacity: 0; transform: scale(0.4) rotate(45deg); }
.voice-player.playing .voice-icon-pause { opacity: 1; transform: none; }
.voice-player.playing .voice-play-btn::after {
  content: ""; position: absolute; inset: -2px; border-radius: 50%;
  border: 2px solid var(--accent-blue); animation: pulseRing 1.5s ease-out infinite; pointer-events: none;
}
@keyframes pulseRing { 0% { transform: scale(1); opacity: 0.7; } 100% { transform: scale(1.5); opacity: 0; } }

.voice-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.voice-wave { display: flex; align-items: center; gap: 2px; height: 30px; cursor: pointer; touch-action: none; }
.voice-bar {
  flex: 1; min-width: 2px; max-width: 4px; border-radius: 2px;
  background: var(--text-muted); opacity: 0.55;
  transition: background-color 0.18s, opacity 0.18s, transform 0.18s var(--ease-spring);
}
.voice-bar.played { background: var(--accent-blue); opacity: 1; }
.message-bubble.outgoing .voice-bar { background: var(--text-out-time); opacity: 0.4; }
.message-bubble.outgoing .voice-bar.played { background: var(--text-out-time); opacity: 1; }
.voice-player.playing .voice-bar.played:last-of-type,
.voice-player.playing .voice-bar.played + .voice-bar:not(.played) { transform: scaleY(1.25); }

.voice-meta { display: flex; align-items: center; justify-content: space-between; font-size: 12px; font-variant-numeric: tabular-nums; }
.voice-time { color: var(--text-secondary); }
.message-bubble.outgoing .voice-time { color: var(--text-out-time); }
.voice-speed-btn {
  min-width: 34px; padding: 1px 7px; font-size: 11px; font-weight: 700; border-radius: 999px;
  color: var(--accent-blue-light); background: var(--bg-hover);
  transition: transform 0.2s var(--ease-spring), background-color 0.2s;
}
.voice-speed-btn:hover { transform: scale(1.08); }
.voice-speed-btn:active { transform: scale(0.92); }
.message-bubble.outgoing .voice-speed-btn { color: var(--text-out-time); background: rgba(255, 255, 255, 0.12); }

/* --------------------------------------------------------------------------
   8. Голосовые сообщения: панель записи
   -------------------------------------------------------------------------- */
.voice-recorder-bar {
  display: none; flex: 1; min-width: 0; align-items: center; gap: 10px;
  min-height: 46px; padding: 0 14px 0 6px;
  background: var(--input-bg); border: 1px solid var(--border-color, var(--border-subtle));
  border-radius: 24px;
}
.input-panel.recording .input-box-wrapper { display: none; }
.input-panel.recording .voice-recorder-bar { display: flex; animation: recBarIn 0.32s var(--ease-spring) both; }
@keyframes recBarIn { from { opacity: 0; transform: translateX(24px) scaleX(0.9); } to { opacity: 1; transform: none; } }

.voice-cancel-btn { color: var(--danger-red); flex-shrink: 0; }
.voice-cancel-btn:hover { background: rgba(239, 68, 68, 0.14); }
.voice-rec-dot {
  width: 11px; height: 11px; flex-shrink: 0; border-radius: 50%;
  background: var(--danger-red); animation: recBlink 1.1s ease-in-out infinite;
}
@keyframes recBlink { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.35; transform: scale(0.75); } }
.voice-rec-timer { min-width: 38px; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; color: var(--text-primary); }

.voice-rec-wave { flex: 1; min-width: 0; height: 28px; display: flex; align-items: center; gap: 2px; overflow: hidden; }
.rec-bar {
  flex: 1; height: 100%; max-width: 4px; min-width: 2px; border-radius: 2px;
  background: var(--accent-blue); transform: scaleY(0.08); transition: transform 0.09s linear;
}

.input-panel.recording .send-btn { animation: sendPulse 1.4s ease-out infinite; }
@keyframes sendPulse {
  0%   { box-shadow: 0 0 0 0 var(--accent-ring); }
  70%  { box-shadow: 0 0 0 14px rgba(0, 0, 0, 0); }
  100% { box-shadow: 0 0 0 0 rgba(0, 0, 0, 0); }
}

#mic-btn.is-recording { color: var(--danger-red); }
#mic-btn.is-uploading { opacity: 0.5; pointer-events: none; }

/* --------------------------------------------------------------------------
   9. Мелочи
   -------------------------------------------------------------------------- */
.toast-notice { transition: transform 0.45s var(--ease-spring), opacity 0.25s; }

@media (max-width: 480px) {
  .gif-picker-popover { width: calc(100% - 24px); }
  .voice-player { min-width: 200px; }
}

/* Уважаем системную настройку «уменьшить движение» */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important; animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important; scroll-behavior: auto !important;
  }
}
