/* AI Chat Widget Styles */

:root {
  --ai-chat-primary: #0369a1;
  --ai-chat-primary-dark: #075985;
  --ai-chat-bg: #ffffff;
  --ai-chat-overlay-bg: rgba(0, 0, 0, 0.4);
  --ai-chat-shadow: 0 25px 70px rgba(0, 0, 0, 0.35);
  --ai-chat-radius: 16px;
  --ai-chat-transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  --ai-chat-z-overlay: 1005;
  --ai-chat-z-widget: 1010;
  --ai-chat-z-button: 1000;
}

/* Кнопка открытия чата */
.ai-chat-button {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 60px;
  height: 60px;
  background: var(--gradient-primary);
  border: none;
  border-radius: 50%;
  color: white;
  font-size: 1.5rem;
  cursor: pointer;
  box-shadow: 0 8px 25px rgba(3, 105, 161, 0.4);
  transition: var(--ai-chat-transition);
  z-index: var(--ai-chat-z-button);
  display: flex;
  align-items: center;
  justify-content: center;
  will-change: transform;
}

.ai-chat-button,
.ai-chat-send,
.ai-chat-close {
  -webkit-tap-highlight-color: transparent;
}

.ai-chat-button:hover {
  transform: scale(1.1);
  box-shadow: 0 12px 35px rgba(3, 105, 161, 0.5);
}

.ai-chat-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background: #ef4444;
  color: white;
  font-size: 0.65rem;
  font-weight: 700;
  padding: 3px 6px;
  border-radius: 10px;
  border: 2px solid white;
}

/* Кнопка настроек */
.ai-settings-button {
  position: fixed;
  bottom: 100px;
  right: 30px;
  width: 45px;
  height: 45px;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid var(--light-gray);
  border-radius: 50%;
  color: var(--primary);
  font-size: 1.1rem;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  transition: all 0.3s ease;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ai-settings-button:hover {
  background: var(--primary);
  color: white;
  transform: rotate(90deg);
}

/* Overlay затемнения */
.ai-chat-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--ai-chat-overlay-bg);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: var(--ai-chat-z-overlay);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  will-change: opacity;
}

.ai-chat-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

/* Виджет чата */
.ai-chat-widget {
  position: fixed;
  bottom: 100px;
  right: 30px;
  width: 480px;
  height: 650px;
  background: var(--ai-chat-bg);
  border-radius: var(--radius-xl);
  box-shadow: var(--ai-chat-shadow);
  display: none;
  flex-direction: column;
  z-index: var(--ai-chat-z-widget);
  overflow: hidden;
  animation: slideInUp 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  will-change: transform, opacity;
}

.ai-chat-widget.active {
  display: flex;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Заголовок чата */
.ai-chat-header {
  background: var(--gradient-primary);
  color: white;
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.ai-chat-header-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.ai-chat-avatar {
  width: 45px;
  height: 45px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.3rem;
}

.ai-chat-title h4 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.ai-chat-status {
  font-size: 0.85rem;
  opacity: 0.92;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ai-chat-status::before {
  content: '';
  width: 8px;
  height: 8px;
  background: #10b981;
  border-radius: 50%;
  display: inline-block;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.ai-chat-close {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  width: 35px;
  height: 35px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.ai-chat-close:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: rotate(90deg);
}

/* Область сообщений */
.ai-chat-messages {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: #f8fafc;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.ai-chat-messages::-webkit-scrollbar {
  width: 6px;
}

.ai-chat-messages::-webkit-scrollbar-track {
  background: #f1f5f9;
}

.ai-chat-messages::-webkit-scrollbar-thumb {
  background: var(--primary);
  border-radius: 3px;
}

/* Сообщение AI */
.ai-message {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  animation: fadeIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  will-change: transform, opacity;
}

.ai-message-avatar {
  width: 35px;
  height: 35px;
  background: var(--gradient-primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 1rem;
  flex-shrink: 0;
}

.ai-message-content {
  background: white;
  padding: 14px 18px;
  border-radius: 14px;
  max-width: 78%;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.09);
  line-height: 1.65;
}

.ai-message-content p {
  margin: 0;
  line-height: 1.65;
  color: var(--secondary);
  font-size: 0.97rem;
}

/* Сообщение пользователя */
.user-message {
  display: flex;
  justify-content: flex-end;
  animation: fadeIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  will-change: transform, opacity;
}

.user-message-content {
  background: var(--gradient-primary);
  color: white;
  padding: 14px 18px;
  border-radius: 14px;
  max-width: 78%;
  box-shadow: 0 2px 10px rgba(3, 105, 161, 0.25);
}

.user-message-content p {
  margin: 0;
  line-height: 1.65;
  font-size: 0.97rem;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Индикатор печатания */
.typing-indicator {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.typing-indicator .ai-message-content {
  padding: 16px;
  display: flex;
  gap: 5px;
}

.typing-dot {
  width: 8px;
  height: 8px;
  background: var(--primary);
  border-radius: 50%;
  animation: typing 1.4s infinite;
}

.typing-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.7;
  }
  30% {
    transform: translateY(-10px);
    opacity: 1;
  }
}

/* Поле ввода */
.ai-chat-input-wrapper {
  padding: 15px;
  background: white;
  border-top: 1px solid var(--light-gray);
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.ai-chat-input {
  flex: 1;
  border: 2px solid var(--light-gray);
  border-radius: var(--radius-md);
  padding: 12px 15px;
  font-size: 0.95rem;
  resize: none;
  max-height: 120px;
  min-height: 44px;
  font-family: inherit;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  will-change: height;
}

.ai-chat-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(3, 105, 161, 0.1);
}

.ai-chat-send {
  width: 45px;
  height: 45px;
  background: var(--gradient-primary);
  border: none;
  border-radius: 50%;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  flex-shrink: 0;
}

.ai-chat-send:hover {
  transform: scale(1.1);
  box-shadow: 0 5px 15px rgba(3, 105, 161, 0.3);
}

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

/* Настройки API */
.ai-chat-settings {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: white;
  display: none;
  flex-direction: column;
  z-index: 10;
}

.ai-chat-settings.active {
  display: flex;
}

.ai-settings-header {
  background: var(--gradient-primary);
  color: white;
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.ai-settings-header h4 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
}

.ai-settings-close {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  width: 35px;
  height: 35px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.ai-settings-close:hover {
  background: rgba(255, 255, 255, 0.3);
}

.ai-settings-content {
  padding: 30px;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.ai-settings-content label {
  font-weight: 600;
  color: var(--secondary);
  font-size: 0.95rem;
}

.ai-api-input {
  width: 100%;
  padding: 12px 15px;
  border: 2px solid var(--light-gray);
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  transition: border-color 0.3s ease;
}

.ai-api-input:focus {
  outline: none;
  border-color: var(--primary);
}

.ai-settings-note {
  background: rgba(3, 105, 161, 0.05);
  padding: 12px;
  border-radius: var(--radius-md);
  font-size: 0.85rem;
  color: var(--dark-gray);
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.ai-settings-note i {
  color: var(--primary);
}

/* Адаптивность */
@media (max-width: 1024px) {
  .ai-chat-widget {
    width: 420px;
    height: 600px;
  }
}

/* Планшеты и большие телефоны */
@media (max-width: 768px) {
  .ai-chat-overlay {
    backdrop-filter: blur(3px);
  }

  .ai-chat-widget {
    width: calc(100vw - 16px);
    height: calc(100dvh - 80px);
    max-height: 700px;
    right: 8px;
    bottom: 8px;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  }

  .ai-chat-header {
    padding: 16px;
    border-radius: 20px 20px 0 0;
  }

  .ai-chat-title h4 {
    font-size: 1.1rem;
  }

  .ai-chat-status {
    font-size: 0.8rem;
  }

  .ai-chat-messages {
    padding: 16px;
    gap: 14px;
  }

  .ai-chat-input-wrapper {
    padding: 16px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  .ai-chat-input {
    font-size: 16px;
    min-height: 48px;
    padding: 14px 16px;
    border-radius: 12px;
  }

  .ai-chat-send {
    width: 48px;
    height: 48px;
    font-size: 1.1rem;
  }

  .ai-chat-button {
    width: 58px;
    height: 58px;
    right: 16px;
    bottom: max(16px, env(safe-area-inset-bottom));
    font-size: 1.6rem;
  }

  .ai-settings-button {
    right: 16px;
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 70px);
    width: 48px;
    height: 48px;
  }

  .ai-message-content,
  .user-message-content {
    font-size: 0.95rem;
    padding: 13px 16px;
  }
}

/* Телефоны (iPhone, Android) */
@media (max-width: 480px) {
  .ai-chat-overlay.active {
    background: rgba(0, 0, 0, 0.5);
  }

  .ai-chat-widget {
    width: 100vw;
    height: 100dvh;
    right: 0;
    bottom: 0;
    border-radius: 0;
    max-height: none;
  }
  
  .ai-chat-header {
    padding: max(16px, env(safe-area-inset-top)) 16px 16px 16px;
    border-radius: 0;
  }

  .ai-chat-messages {
    padding: 12px 14px;
    gap: 12px;
  }

  .ai-chat-input-wrapper {
    padding: 12px 14px;
    padding-bottom: max(12px, calc(env(safe-area-inset-bottom) + 8px));
    background: white;
    border-top: 1px solid rgba(0, 0, 0, 0.08);
  }

  .ai-chat-input {
    font-size: 16px;
    min-height: 44px;
    padding: 12px 14px;
    border-width: 1.5px;
  }

  .ai-chat-send {
    width: 44px;
    height: 44px;
    font-size: 1rem;
  }
  
  .ai-chat-button {
    width: 56px;
    height: 56px;
    bottom: max(20px, calc(env(safe-area-inset-bottom) + 12px));
    right: 16px;
    font-size: 1.5rem;
    box-shadow: 0 6px 20px rgba(3, 105, 161, 0.35);
  }
  
  .ai-settings-button {
    bottom: calc(max(20px, calc(env(safe-area-inset-bottom) + 12px)) + 66px);
    right: 16px;
    width: 46px;
    height: 46px;
  }

  .ai-message-content,
  .user-message-content {
    max-width: 85%;
    font-size: 0.95rem;
    padding: 12px 15px;
    border-radius: 16px;
  }

  .ai-message-avatar {
    width: 32px;
    height: 32px;
    font-size: 0.9rem;
  }

  .ai-chat-close {
    width: 38px;
    height: 38px;
    font-size: 1.1rem;
  }

  /* Оптимизация для touch */
  .ai-chat-button:active {
    transform: scale(0.92);
    transition: transform 0.1s ease;
  }

  .ai-chat-send:active {
    transform: scale(0.92);
    transition: transform 0.1s ease;
  }

  .ai-chat-close:active {
    transform: scale(0.9) rotate(90deg);
    transition: transform 0.1s ease;
  }

  /* Smooth scrolling */
  .ai-chat-messages {
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
  }
}

/* Очень маленькие телефоны */
@media (max-width: 375px) {
  .ai-chat-header {
    padding: max(14px, env(safe-area-inset-top)) 14px 14px 14px;
  }

  .ai-chat-title h4 {
    font-size: 1.05rem;
  }

  .ai-chat-messages {
    padding: 10px 12px;
  }

  .ai-chat-input-wrapper {
    padding: 10px 12px;
    padding-bottom: max(10px, calc(env(safe-area-inset-bottom) + 6px));
  }

  .ai-message-content,
  .user-message-content {
    max-width: 88%;
    font-size: 0.93rem;
  }
}

/* Landscape режим для телефонов */
@media (max-width: 896px) and (max-height: 414px) and (orientation: landscape) {
  .ai-chat-widget {
    height: 100dvh;
  }

  .ai-chat-header {
    padding: 12px 16px;
  }

  .ai-chat-messages {
    padding: 10px 14px;
  }

  .ai-chat-input-wrapper {
    padding: 10px 14px;
  }

  .ai-chat-input {
    min-height: 40px;
  }
}
