/* ============================================================
   OK Chat Widget — Redesigned UI (Gemini Recommendations)
   ============================================================ */

:root {
  --ai-primary: #EE8208;
  --ai-secondary: #FFB74D;
  --ai-bg: #FFFFFF;
  --ai-glass: rgba(255, 255, 255, 0.98);
  --ai-border: rgba(0, 0, 0, 0.08);
  --ai-text: #1a1a1a;          /* ← Dark text for readability */
  --ai-text-secondary: #555;   /* ← Softer secondary text */
  --ai-text-muted: #888888;
  --ai-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --ai-radius: 20px;
  --ai-shadow: 0 12px 48px rgba(0, 0, 0, 0.12);
}

.ai-chat-widget {
  position: fixed;
  bottom: 100px;
  right: 30px;
  z-index: 9999;
  font-family: var(--ai-font);
}

/* --- Badge Animation --- */
.chat-badge {
  position: absolute;
  top: -55px;
  right: 0;
  background: linear-gradient(135deg, var(--ai-secondary), var(--ai-primary));
  color: white;
  padding: 10px 20px;
  border-radius: 16px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(238, 130, 8, 0.3);
  animation: float 3s ease-in-out infinite;
  pointer-events: none;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transform: translateY(10px);
  transition: opacity 0.4s ease, transform 0.4s ease, visibility 0.4s;
}

.chat-badge.is-visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.chat-badge::after {
  content: "";
  position: absolute;
  bottom: -6px;
  right: 25px;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 8px solid var(--ai-primary);
}

/* --- Toggle Button --- */
.chat-toggle {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--ai-secondary) 0%, var(--ai-primary) 100%);
  box-shadow: 0 0 0 0 rgba(238, 130, 8, 0.5);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  animation: pulse-ring 2s infinite;
}

.chat-toggle:hover {
  transform: scale(1.1) rotate(5deg);
}

.chat-toggle svg {
  width: 30px;
  height: 30px;
  color: white;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

/* --- Main Window --- */
.chat-window {
  position: absolute;
  bottom: 90px;
  right: 0;
  width: 400px;
  height: 620px;
  background: var(--ai-glass);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--ai-border);
  border-radius: var(--ai-radius);
  box-shadow: var(--ai-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform-origin: bottom right;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.chat-window.active {
  transform: scale(1) translateY(0);
  opacity: 1;
  visibility: visible;
  pointer-events: all;
}

.chat-header {
  background: #FAFAFA;
  padding: 18px 20px;
  border-bottom: 1px solid var(--ai-border);
  display: flex;
  align-items: center;
  gap: 12px;
}

.mobile-only-handle {
  display: none;
}

.status-dot {
  width: 10px;
  height: 10px;
  background: #67A526;
  border-radius: 50%;
  box-shadow: 0 0 8px #67A526;
  animation: blink 2s infinite;
}

.chat-header h3 {
  margin: 0;
  font-size: 15px;
  color: var(--ai-text);
  font-weight: 700;
  letter-spacing: 0.3px;
}

/* Header CTA Button */
.btn-header-action {
  background: var(--ai-primary);
  border: none;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.25s ease;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 4px 14px rgba(238, 130, 8, 0.25);
}

.btn-header-action:hover {
  background: #D97706;
  color: white;
  box-shadow: 0 6px 20px rgba(238, 130, 8, 0.35);
  transform: translateY(-1px);
}

/* --- Chat Area --- */
.chat-messages {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

/* Message Bubbles */
.message {
  max-width: 88%;
  padding: 16px 20px;       /* ← More breathing room */
  border-radius: 16px;
  font-size: 14.5px;
  line-height: 1.65;        /* ← Improved readability */
  position: relative;
  word-wrap: break-word;
  color: var(--ai-text);     /* ← DARK TEXT always */
}

.message.ai {
  background: #f5f5f5;
  color: var(--ai-text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
  border: 1px solid #ebebeb;
}

.message.user {
  background: linear-gradient(135deg, var(--ai-primary), #D97706);
  color: white;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
  box-shadow: 0 4px 14px rgba(238, 130, 8, 0.2);
}

/* Contact Footer inside messages */
.chat-contact-footer {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid #ddd;
  font-size: 0.85em;
  line-height: 1.8;         /* ← More space between Telegram and Phone */
  color: var(--ai-text-secondary);
}

.chat-contact-footer a {
  color: var(--ai-primary);
  text-decoration: none;
  font-weight: 700;
}

.chat-contact-footer a:hover {
  text-decoration: underline;
}

/* Typing Indicator */
.typing-indicator {
  padding: 0 24px 12px;
  font-size: 13px;
  color: var(--ai-text-muted);
  font-style: italic;
  opacity: 0.7;
  display: none;
}

/* --- Suggestion Buttons (Quick Replies) --- */
.chat-suggestions {
  padding: 12px 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  border-top: 1px solid #f0f0f0;
}

/* Default: Secondary style (outline) */
.suggestion-btn {
  background: #fff;
  border: 1.5px solid #e0e0e0;
  color: var(--ai-text);       /* ← DARK text for readability */
  padding: 9px 16px;
  border-radius: 22px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.25s ease;
  white-space: nowrap;
}

.suggestion-btn:hover {
  background: #f5f5f5;
  border-color: #ccc;
  transform: translateY(-2px);
  box-shadow: 0 3px 8px rgba(0,0,0,0.06);
}

/* Primary CTA style for "Записать ребенка" */
.suggestion-btn.glow-sun {
  background: var(--ai-primary);
  color: #fff;
  border-color: var(--ai-primary);
  font-weight: 700;
  box-shadow: 0 4px 14px rgba(238, 130, 8, 0.2);
}

.suggestion-btn.glow-sun:hover {
  background: #D97706;
  border-color: #D97706;
  box-shadow: 0 6px 18px rgba(238, 130, 8, 0.3);
}

/* --- Input Area --- */
.chat-input-area {
  padding: 14px 16px;
  background: #FAFAFA;
  border-top: 1px solid var(--ai-border);
  display: flex;
  align-items: center;
  gap: 0;  /* ← Remove gap, send button is inside the input visually */
  position: relative;
}

.chat-input {
  flex: 1;
  background: white;
  border: 1.5px solid #e0e0e0;
  border-radius: 24px;
  padding: 12px 52px 12px 18px; /* ← Room for send button on the right */
  color: var(--ai-text);
  font-size: 14.5px;
  outline: none;
  transition: all 0.3s;
}

.chat-input::placeholder {
  color: #aaa;
}

.chat-input:focus {
  border-color: var(--ai-primary);
  box-shadow: 0 0 0 3px rgba(238, 130, 8, 0.1);
}

/* Send button inside the input field */
.chat-send {
  position: absolute;
  right: 22px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--ai-primary);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s, background 0.2s;
}

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

.chat-send:hover {
  transform: scale(1.08);
  background: #D97706;
}

/* --- Animations --- */
@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@keyframes pulse-ring {
  0% { box-shadow: 0 0 0 0 rgba(238, 130, 8, 0.5); }
  70% { box-shadow: 0 0 0 15px rgba(238, 130, 8, 0); }
  100% { box-shadow: 0 0 0 0 rgba(238, 130, 8, 0); }
}

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

/* ============================================================
   MOBILE RESPONSIVE
   ============================================================ */
@media (max-width: 600px) {
  .chat-badge { display: none !important; }
  
  .ai-chat-widget {
    bottom: 80px; 
    right: 16px;
    z-index: 10000;
  }

  .chat-toggle {
    width: 58px;
    height: 58px;
  }
  .chat-toggle svg {
    width: 26px;
    height: 26px;
  }

  .chat-window {
    position: fixed;
    bottom: 0; right: 0; left: 0;
    width: 100%;
    height: calc(100dvh - 10px); /* Slightly detached from top for a sheet feel */
    border-radius: 20px 20px 0 0;
    background: white;
    transform: translateY(110%);
    opacity: 0;
    transition: transform 0.4s cubic-bezier(0.19, 1, 0.22, 1), opacity 0.4s ease;
    z-index: 10001; 
    box-shadow: 0 -10px 40px rgba(0,0,0,0.15);
  }

  .chat-window.active {
    transform: translateY(0);
    opacity: 1;
  }

  .mobile-close-btn {
    display: flex !important; 
    color: var(--ai-text) !important;
  }

  .chat-header {
    flex-wrap: wrap; /* allow handle to sit on its own line */
    padding: 14px 20px;
    padding-top: max(14px, env(safe-area-inset-top));
    border-radius: 20px 20px 0 0;
  }

  .mobile-only-handle {
    display: block;
    width: 40px;
    height: 5px;
    background: #ddd;
    border-radius: 3px;
    margin: 0 auto 8px auto;
    flex-basis: 100%; /* force to top of the flex container */
  }

  .chat-messages {
    padding: 16px;
    padding-bottom: 20px;
    gap: 12px;
  }

  .message {
    max-width: 92%;
    padding: 14px 16px;
    font-size: 15px;
  }

  .chat-suggestions {
    padding: 10px 16px;
  }

  .suggestion-btn {
    padding: 10px 14px;
    font-size: 14px;
  }

  .chat-input-area {
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    background: #FAFAFA;
    box-shadow: 0 -4px 12px rgba(0,0,0,0.04);
  }

  .chat-input {
    font-size: 16px; /* Prevents iOS auto-zoom */
    padding: 14px 50px 14px 16px;
  }

  .chat-send {
    right: 22px;
    width: 38px;
    height: 38px;
  }
}

/* --- Swipe Gesture Areas (Mobile) --- */
.chat-swipe-area {
  position: absolute;
  left: 0; width: 100%; height: 44px;
  display: flex; align-items: center; justify-content: center;
  z-index: 10002; touch-action: none; cursor: grab;
}
.chat-swipe-area.bottom { bottom: 0; height: 30px; }
.chat-swipe-handle {
  width: 40px; height: 4px;
  background: rgba(0,0,0,0.12);
  border-radius: 10px;
}
.chat-swipe-area.bottom .chat-swipe-handle { margin-bottom: 8px; }

.chat-window.is-dragging { transition: none !important; }
.chat-window.animate-restore { transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1) !important; transform: translateY(0) !important; }

/* ============================================================
   iOS SAFARI FIXES
   ============================================================ */

/* Prevent iOS rubber-band scroll from leaking behind chat */
@supports (-webkit-touch-callout: none) {
  .chat-window.active {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
  
  .chat-messages {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
  }

  /* iOS safe-area: extra padding at bottom for home indicator */
  .chat-input-area {
    padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
  }
}

/* When iOS keyboard is open, shrink the chat height smoothly */
@media (max-width: 600px) {
  .chat-window.keyboard-open {
    transition: height 0.2s ease !important;
  }

  .chat-window.keyboard-open .chat-suggestions {
    max-height: 60px;
    overflow-y: auto;
    flex-wrap: nowrap;
  }

  .chat-window.keyboard-open .btn-header-action {
    padding: 6px 12px;
    font-size: 12px;
  }
}
