/* =====================================================================
   animations.css — слой анимаций клиента.
   Подключать ПОСЛЕДНИМ, после css/styles.css.
   Управляется переменной --ax-k:  1 = полная анимация,
   0 = prefers-reduced-motion или data-quality="potato" (всё мгновенно).
   Классы ax-* на элементах выставляет js/animations.js.
   ===================================================================== */
:root {
  --ax-k: 1;
  --ax-out: cubic-bezier(.16, 1, .3, 1);
  --ax-soft: cubic-bezier(.25, 1, .5, 1);
  --ax-spring: cubic-bezier(.34, 1.56, .64, 1);
  --ax-in: cubic-bezier(.4, 0, 1, 1);
}
@media (prefers-reduced-motion: reduce) { :root { --ax-k: 0; } }
:root[data-quality="potato"] { --ax-k: 0; }

/* ---------- keyframes ---------- */
@keyframes ax-overlay-in  { from { opacity: 0 } to { opacity: 1 } }
@keyframes ax-overlay-out { from { opacity: 1 } to { opacity: 0 } }
@keyframes ax-modal-in {
  0%   { opacity: 0; transform: translateY(36px) scale(.9) }
  55%  { opacity: 1; transform: translateY(-5px) scale(1.015) }
  100% { opacity: 1; transform: translateY(0) scale(1) }
}
@keyframes ax-modal-out {
  from { opacity: 1; transform: translateY(0) scale(1) }
  to   { opacity: 0; transform: translateY(20px) scale(.94) }
}
@keyframes ax-sheet-in  { from { transform: translateY(100%) } to { transform: translateY(0) } }
@keyframes ax-sheet-out { from { transform: translateY(0) } to { transform: translateY(100%) } }
@keyframes ax-rise    { from { opacity: 0; transform: translateY(18px) } to { opacity: 1; transform: translateY(0) } }
@keyframes ax-rise-sm { from { opacity: 0; transform: translateY(8px) }  to { opacity: 1; transform: translateY(0) } }
@keyframes ax-fall    { from { opacity: 0; transform: translateY(-10px) } to { opacity: 1; transform: translateY(0) } }
@keyframes ax-menu-in {
  0%   { opacity: 0; transform: translateY(-8px) scale(.86) }
  60%  { opacity: 1; transform: translateY(1px) scale(1.02) }
  100% { opacity: 1; transform: translateY(0) scale(1) }
}
@keyframes ax-menu-out {
  from { opacity: 1; transform: scale(1) }
  to   { opacity: 0; transform: translateY(-4px) scale(.92) }
}
@keyframes ax-pop {
  0%   { transform: scale(.4); opacity: 0 }
  55%  { transform: scale(1.28); opacity: 1 }
  100% { transform: scale(1); opacity: 1 }
}
@keyframes ax-msg-in-sent {
  0%   { opacity: 0; transform: translate(34px, 26px) scale(.7) }
  60%  { opacity: 1; transform: translate(-3px, -4px) scale(1.04) }
  100% { opacity: 1; transform: translate(0, 0) scale(1) }
}
@keyframes ax-msg-in-recv {
  0%   { opacity: 0; transform: translate(-34px, 26px) scale(.7) }
  60%  { opacity: 1; transform: translate(3px, -4px) scale(1.04) }
  100% { opacity: 1; transform: translate(0, 0) scale(1) }
}
@keyframes ax-msg-load {
  from { opacity: 0; transform: translateY(22px) scale(.97) }
  to   { opacity: 1; transform: translateY(0) scale(1) }
}
@keyframes ax-msg-out {
  0%   { opacity: 1; transform: none; max-height: var(--ax-h, 200px) }
  55%  { opacity: 0; transform: translateX(var(--ax-dx, -56px)) scale(.84); max-height: var(--ax-h, 200px) }
  100% { opacity: 0; transform: translateX(var(--ax-dx, -56px)) scale(.84); max-height: 0;
         margin-top: 0; margin-bottom: 0; padding-top: 0; padding-bottom: 0 }
}
@keyframes ax-edit-ring {
  0%   { outline-width: 2px; outline-style: solid; outline-color: var(--primary-glow, rgba(99,102,241,.55)); outline-offset: 0; transform: scale(1) }
  35%  { outline-width: 2px; outline-style: solid; outline-color: var(--primary-glow, rgba(99,102,241,.45)); outline-offset: 5px; transform: scale(1.04) }
  100% { outline-width: 2px; outline-style: solid; outline-color: transparent; outline-offset: 12px; transform: scale(1) }
}
@keyframes ax-bump { 0% { transform: scale(1) } 30% { transform: scale(.84) } 65% { transform: scale(1.12) } 100% { transform: scale(1) } }
@keyframes ax-send-fly {
  0%   { transform: translate(0, 0) scale(1); opacity: 1 }
  38%  { transform: translate(20px, -20px) scale(.5); opacity: 0 }
  39%  { transform: translate(-20px, 20px) scale(.5); opacity: 0 }
  100% { transform: translate(0, 0) scale(1); opacity: 1 }
}
@keyframes ax-chat-in   { from { opacity: 0; transform: translateY(14px) } to { opacity: 1; transform: translateY(0) } }
@keyframes ax-avatar-in { from { opacity: 0; transform: scale(.5) rotate(-12deg) } to { opacity: 1; transform: scale(1) rotate(0) } }
@keyframes ax-page-in   { from { opacity: 0; transform: translateX(32px) } to { opacity: 1; transform: translateX(0) } }
@keyframes ax-app-in    { from { opacity: 0 } to { opacity: 1 } }
@keyframes ax-item-pop  {
  0%   { opacity: .4; transform: scale(.93) }
  55%  { opacity: 1; transform: scale(1.03) }
  100% { opacity: 1; transform: scale(1) }
}

/* =====================================================================
   1. МОДАЛКИ (вход / выход)
   ===================================================================== */
html:root body .modal-overlay:not(.rich-editor-overlay) {
  animation: ax-overlay-in calc(.32s * var(--ax-k)) ease-out backwards !important;
}
html:root body :is(.modal-overlay, #modal-custom) .modal {
  animation: ax-modal-in calc(.48s * var(--ax-k)) var(--ax-out) backwards !important;
}
html:root body :is(.modal-overlay, #modal-custom) .modal > :is(.modal-header, .modal-settings-header) {
  animation: ax-fall calc(.5s * var(--ax-k)) var(--ax-out) calc(var(--ax-k) * 80ms) backwards;
}

/* выход: класс .ax-closing ставит js/animations.js */
html:root body .modal-overlay.ax-closing,
html:root body #modal-custom.ax-closing {
  animation: ax-overlay-out calc(.26s * var(--ax-k)) ease-in forwards !important;
  pointer-events: none !important;
}
html:root body :is(.modal-overlay, #modal-custom).ax-closing .modal {
  animation: ax-modal-out calc(.26s * var(--ax-k)) var(--ax-in) forwards !important;
}
.ax-closing, .ax-closing * { pointer-events: none !important; }

/* мобильные: bottom-sheet */
@media (max-width: 768px) {
  html:root body :is(.modal-overlay, #modal-custom) .modal {
    animation: ax-sheet-in calc(.52s * var(--ax-k)) var(--ax-out) backwards !important;
  }
  html:root body :is(.modal-overlay, #modal-custom).ax-closing .modal {
    animation: ax-sheet-out calc(.3s * var(--ax-k)) var(--ax-in) forwards !important;
  }
}

/* содержимое модалки выезжает каскадом */
html:root body :is(.modal-overlay, #modal-custom):not(.ax-closing) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>* {
  animation: ax-rise calc(.55s * var(--ax-k)) var(--ax-out) backwards;
  animation-delay: calc(var(--ax-k) * (140ms + var(--ax-i, 0) * 36ms));
}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(2){--ax-i:1}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(3){--ax-i:2}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(4){--ax-i:3}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(5){--ax-i:4}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(6){--ax-i:5}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(7){--ax-i:6}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(8){--ax-i:7}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(9){--ax-i:8}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(10){--ax-i:9}
html:root body :is(.modal-overlay,#modal-custom) .modal :is(.modal-body,.modal-scroll-content,.modal-footer,.modal-actions,.modal-settings-footer)>:nth-child(n+11){--ax-i:10}

/* =====================================================================
   2. МЕНЮ, ПИКЕРЫ, ДРОПДАУНЫ
   ===================================================================== */
html:root body :is(#context-menu,#mobile-menu-dropdown,#notifications-dropdown,#user-menu-dropdown,.attachment-menu,.chat-sidebar-menu,.sticker-picker) {
  animation: ax-menu-in calc(.36s * var(--ax-k)) var(--ax-spring) backwards !important;
}
html:root body #context-menu { transform-origin: top left; }
html:root body :is(.attachment-menu, .sticker-picker) { transform-origin: bottom left; }
html:root body :is(#context-menu,#mobile-menu-dropdown,#notifications-dropdown,#user-menu-dropdown,.attachment-menu,.chat-sidebar-menu,.sticker-picker).ax-closing {
  animation: ax-menu-out calc(.18s * var(--ax-k)) var(--ax-in) forwards !important;
}
html:root body #context-menu:not(.ax-closing) :is(.ctx-item, .ctx-divider) {
  animation: ax-rise-sm calc(.34s * var(--ax-k)) var(--ax-out) backwards;
  animation-delay: calc(var(--ax-k) * (50ms + var(--ax-i, 0) * 24ms));
}
html:root body #context-menu :is(.ctx-item, .ctx-divider):nth-child(2){--ax-i:1}
html:root body #context-menu :is(.ctx-item, .ctx-divider):nth-child(3){--ax-i:2}
html:root body #context-menu :is(.ctx-item, .ctx-divider):nth-child(4){--ax-i:3}
html:root body #context-menu :is(.ctx-item, .ctx-divider):nth-child(5){--ax-i:4}
html:root body #context-menu :is(.ctx-item, .ctx-divider):nth-child(6){--ax-i:5}
html:root body #context-menu :is(.ctx-item, .ctx-divider):nth-child(7){--ax-i:6}
html:root body #context-menu :is(.ctx-item, .ctx-divider):nth-child(8){--ax-i:7}
html:root body #context-menu :is(.ctx-item, .ctx-divider):nth-child(9){--ax-i:8}
html:root body #context-menu :is(.ctx-item, .ctx-divider):nth-child(n+10){--ax-i:9}

html:root body :is(.call-overlay.active, .image-editor-overlay.active) {
  animation: ax-overlay-in calc(.36s * var(--ax-k)) ease-out backwards;
}
html:root body :is(.call-overlay, .image-editor-overlay).ax-closing {
  animation: ax-overlay-out calc(.26s * var(--ax-k)) ease-in forwards !important;
}
html:root body .btn-scroll-bottom.show {
  animation: ax-pop calc(.45s * var(--ax-k)) var(--ax-spring) backwards;
}

/* =====================================================================
   3. СООБЩЕНИЯ
   ===================================================================== */
/* новое сообщение (живая отправка/приём) */
html:root body #messages-container .message-row.ax-in {
  transform-origin: 0 100%;
  animation: ax-msg-in-recv calc(.55s * var(--ax-k)) var(--ax-out) backwards !important;
  animation-delay: var(--ax-d, 0ms) !important;
}
html:root body #messages-container .message-row.sent.ax-in {
  transform-origin: 100% 100%;
  animation-name: ax-msg-in-sent !important;
}
/* открытие чата: хвост истории выезжает каскадом */
html:root body #messages-container .message-row.ax-load {
  animation: ax-msg-load calc(.55s * var(--ax-k)) var(--ax-out) backwards !important;
  animation-delay: var(--ax-d, 0ms) !important;
}
html:root body #messages-container.ax-chat-in {
  animation: ax-chat-in calc(.45s * var(--ax-k)) var(--ax-out) backwards;
}
html:root body :is(#chat-header-avatar).ax-hdr { animation: ax-avatar-in calc(.55s * var(--ax-k)) var(--ax-spring) backwards; }
html:root body :is(#chat-header-name, #chat-header-status).ax-hdr { animation: ax-rise-sm calc(.45s * var(--ax-k)) var(--ax-out) calc(var(--ax-k) * 70ms) backwards; }

/* удаление: уезжает в сторону и схлопывается по высоте */
html:root body #messages-container .message-row:is(.animate-delete, .ax-out) {
  --ax-dx: -56px;
  overflow: hidden;
  pointer-events: none;
  animation: ax-msg-out calc(.42s * var(--ax-k)) var(--ax-in) forwards !important;
}
html:root body #messages-container .message-row.sent:is(.animate-delete, .ax-out) { --ax-dx: 56px; }

/* изменение: расходящееся кольцо вокруг пузыря */
html:root body .message-row.ax-edited .message-bubble {
  animation: ax-edit-ring calc(.85s * var(--ax-k)) var(--ax-out) !important;
}

/* реакции */
html:root body .reaction-badge.ax-pop { animation: ax-pop calc(.5s * var(--ax-k)) var(--ax-spring) !important; }

/* кнопка отправки */
html:root body #btn-send-message.ax-sent { animation: ax-bump calc(.5s * var(--ax-k)) var(--ax-spring) !important; }
html:root body #btn-send-message.ax-sent > :is(svg, i) { animation: ax-send-fly calc(.55s * var(--ax-k)) var(--ax-out); }

/* =====================================================================
   4. ОПРОСЫ
   ===================================================================== */
html:root body .poll-voted-bar { animation-delay: calc(var(--ax-k) * var(--ax-i, 0) * 70ms) !important; animation-fill-mode: both !important; }
html:root body .poll-option-voted.ax-poll-new { animation: ax-rise-sm calc(.45s * var(--ax-k)) var(--ax-out) calc(var(--ax-k) * var(--ax-i, 0) * 70ms) backwards; }
html:root body .poll-option-voted.ax-poll-new .poll-voted-check { animation: ax-pop calc(.55s * var(--ax-k)) var(--ax-spring) calc(var(--ax-k) * (180ms + var(--ax-i, 0) * 70ms)) backwards; }
html:root body .poll-option-unvoted {
  transition: transform .2s var(--ax-soft), background-color .2s, box-shadow .2s;
}
html:root body .poll-option-unvoted:hover  { transform: translateX(3px); }
html:root body .poll-option-unvoted:active { transform: scale(.97); }
html:root body .poll-option-unvoted .poll-selector-circle { transition: transform .25s var(--ax-spring); }
html:root body .poll-option-unvoted:active .poll-selector-circle { transform: scale(1.35); }

/* =====================================================================
   5. СПИСОК ЧАТОВ, СТРАНИЦЫ, ВХОД В ПРИЛОЖЕНИЕ
   ===================================================================== */
html:root body .chat-item.animate-pop,
html:root body .chat-item-swipe-wrapper.animate-pop { animation: ax-item-pop calc(.5s * var(--ax-k)) var(--ax-spring) !important; }
html:root body .chat-list > .ax-load {
  animation: ax-rise calc(.5s * var(--ax-k)) var(--ax-out) backwards;
  animation-delay: var(--ax-d, 0ms);
}
/* смена «страницы» внутри универсального окна */
html:root body #modal-custom-body.ax-page-in { animation: ax-page-in calc(.4s * var(--ax-k)) var(--ax-out) backwards; }
/* после входа в аккаунт / при старте приложения */
html.is-authenticated #app { animation: ax-app-in calc(.6s * var(--ax-k)) ease-out backwards; }

/* =====================================================================
   6. ОТКЛИК НА НАЖАТИЕ (нулевая специфичность — не ломает свои стили)
   ===================================================================== */
:where(.btn, .btn-icon, .chat-action-btn, .chat-back-btn, .btn-send, .modal-actions button, .modal-footer button):active { transform: scale(.94); }
:where(.ctx-item, .tg-settings-row, .info-card-row, .member-item, .select-chat-item):active { transform: scale(.985); }
