/* ==========================================================================
   Mobile Chat UI Optimizations — URBot.net
   Bottom-anchored input, full-width bubbles, safe areas, swipe gestures,
   pull-to-refresh indicator, and reduced spacing for small screens.
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. SAFE AREA INSETS — notched phones (iPhone X+, Galaxy S21+, Pixel)
   -------------------------------------------------------------------------- */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* --------------------------------------------------------------------------
   2. BOTTOM-ANCHORED CHAT INPUT
   -------------------------------------------------------------------------- */
.chat-input-bar,
.urbot-chat-input-bar,
.ulp-input-bar {
  position: sticky;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: var(--bg-primary, #ffffff);
  border-top: 1px solid var(--border, rgba(0,0,0,0.08));
  padding: 8px 12px calc(8px + var(--safe-bottom));
  display: flex;
  align-items: center;
  gap: 8px;
  transition: transform 0.15s ease;
}

/* When keyboard is open, ensure input stays visible */
.chat-input-bar.keyboard-open,
.urbot-chat-input-bar.keyboard-open,
.ulp-input-bar.keyboard-open {
  position: fixed;
  bottom: 0;
  transform: translateY(0);
}

.chat-input-bar input,
.chat-input-bar textarea,
.urbot-chat-input-bar input,
.urbot-chat-input-bar textarea,
.ulp-input-bar input,
.ulp-input-bar textarea {
  flex: 1;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 22px;
  border: 1.5px solid var(--border, rgba(0,0,0,0.12));
  font-size: 16px; /* Prevents iOS zoom on focus */
  background: var(--bg-secondary, #f8fafc);
  color: var(--text, #1a1a2e);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.chat-input-bar input:focus,
.chat-input-bar textarea:focus,
.urbot-chat-input-bar input:focus,
.urbot-chat-input-bar textarea:focus {
  border-color: var(--accent, #6366f1);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

/* Send button */
.chat-send-btn,
.ulp-send {
  min-width: 44px;
  min-height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--accent, #6366f1);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.chat-send-btn:active,
.ulp-send:active {
  transform: scale(0.92);
}

/* --------------------------------------------------------------------------
   3. TOUCH TARGETS — min 44x44px for all interactive elements
   -------------------------------------------------------------------------- */
@media (pointer: coarse) {
  a, button, [role="button"], input[type="submit"], input[type="button"],
  .btn, .cta-btn, .tier-btn, .buy-btn, .checkout-btn,
  select, .chat-send-btn, .ulp-send,
  .nav-link, .tab-link, .filter-chip {
    min-height: 44px;
    min-width: 44px;
  }

  /* Ensure adequate spacing between tappable items */
  .nav-link + .nav-link,
  .filter-chip + .filter-chip,
  .tab-link + .tab-link {
    margin-left: 4px;
  }
}

/* --------------------------------------------------------------------------
   4. FULL-WIDTH MESSAGE BUBBLES on narrow screens
   -------------------------------------------------------------------------- */
@media (max-width: 480px) {
  .chat-message,
  .ulp-message,
  .message-bubble {
    max-width: 92% !important;
    border-radius: 16px !important;
  }

  .chat-message.user,
  .ulp-message.user,
  .message-bubble.user {
    margin-left: auto;
    margin-right: 4px;
    border-bottom-right-radius: 4px !important;
  }

  .chat-message.bot,
  .ulp-message.bot,
  .message-bubble.bot {
    margin-right: auto;
    margin-left: 4px;
    border-bottom-left-radius: 4px !important;
  }

  /* Tighter padding inside messages */
  .chat-message,
  .ulp-message,
  .message-bubble {
    padding: 10px 14px !important;
    font-size: 15px;
    line-height: 1.55;
  }

  /* Chat container takes full viewport minus input bar */
  .chat-messages,
  .ulp-messages,
  .chat-body {
    padding: 8px 4px;
  }
}

/* --------------------------------------------------------------------------
   5. PULL-TO-REFRESH VISUAL INDICATOR
   -------------------------------------------------------------------------- */
.urbot-ptr-indicator {
  position: fixed;
  top: 0;
  left: 50%;
  transform: translateX(-50%) translateY(-60px);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg-card, #fff);
  box-shadow: 0 2px 12px rgba(0,0,0,0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10001;
  transition: transform 0.25s cubic-bezier(0.22, 1, 0.36, 1);
  pointer-events: none;
}

.urbot-ptr-indicator.pulling {
  transform: translateX(-50%) translateY(20px);
}

.urbot-ptr-indicator.refreshing {
  transform: translateX(-50%) translateY(20px);
}

.urbot-ptr-spinner {
  width: 20px;
  height: 20px;
  border: 2.5px solid var(--border, #e2e8f0);
  border-top-color: var(--accent, #6366f1);
  border-radius: 50%;
  animation: none;
}

.urbot-ptr-indicator.pulling .urbot-ptr-spinner {
  /* Rotate based on pull distance via CSS custom property */
  transform: rotate(calc(var(--ptr-progress, 0) * 360deg));
}

.urbot-ptr-indicator.refreshing .urbot-ptr-spinner {
  animation: urbot-ptr-spin 0.7s linear infinite;
}

@keyframes urbot-ptr-spin {
  to { transform: rotate(360deg); }
}

/* --------------------------------------------------------------------------
   6. SWIPE GESTURE ZONES
   -------------------------------------------------------------------------- */
.urbot-swipe-zone {
  position: fixed;
  top: 0;
  left: 0;
  width: 20px;
  height: 100%;
  z-index: 9999;
  pointer-events: auto;
  -webkit-user-select: none;
  user-select: none;
}

.urbot-swipe-back-indicator {
  position: fixed;
  top: 50%;
  left: 0;
  transform: translateX(-40px) translateY(-50%);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent, #6366f1);
  opacity: 0;
  transition: transform 0.2s ease, opacity 0.2s ease;
  pointer-events: none;
  z-index: 10002;
  display: flex;
  align-items: center;
  justify-content: center;
}

.urbot-swipe-back-indicator.active {
  opacity: 0.9;
  transform: translateX(10px) translateY(-50%);
}

.urbot-swipe-back-indicator::after {
  content: '';
  display: block;
  width: 10px;
  height: 10px;
  border-left: 2.5px solid #fff;
  border-bottom: 2.5px solid #fff;
  transform: rotate(45deg);
  margin-left: 3px;
}

/* --------------------------------------------------------------------------
   7. REDUCED SPACING ON SMALL SCREENS
   -------------------------------------------------------------------------- */
@media (max-width: 480px) {
  .hero-section, .section, section, .container {
    padding-left: 12px !important;
    padding-right: 12px !important;
  }

  h1 { font-size: 1.6rem !important; }
  h2 { font-size: 1.3rem !important; }
  h3 { font-size: 1.1rem !important; }

  /* Stack tier cards vertically */
  .tier-grid, .pricing-grid {
    grid-template-columns: 1fr !important;
    gap: 12px;
  }

  /* Compact feature lists */
  .feature-list li, .features li {
    padding: 6px 0;
    font-size: 0.9rem;
  }

  /* Reduce avatar sizes */
  .bot-avatar, .hero-bot-avatar, .avatar-img {
    max-width: 72px;
    max-height: 72px;
  }
}

/* --------------------------------------------------------------------------
   8. LANDSCAPE SMALL SCREEN (phone rotated)
   -------------------------------------------------------------------------- */
@media (max-height: 500px) and (orientation: landscape) {
  .chat-input-bar,
  .urbot-chat-input-bar,
  .ulp-input-bar {
    padding-top: 4px;
    padding-bottom: calc(4px + var(--safe-bottom));
  }

  .chat-messages,
  .ulp-messages,
  .chat-body {
    max-height: calc(100vh - 56px - var(--safe-bottom));
  }
}

/* --------------------------------------------------------------------------
   9. iOS OVERSCROLL FIX — prevent rubber-banding in chat
   -------------------------------------------------------------------------- */
@supports (-webkit-touch-callout: none) {
  .chat-messages,
  .ulp-messages,
  .chat-body {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
  }
}

/* --------------------------------------------------------------------------
   10. REDUCED MOTION SUPPORT
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .urbot-ptr-indicator,
  .urbot-swipe-back-indicator,
  .chat-input-bar,
  .urbot-chat-input-bar {
    transition: none !important;
  }

  .urbot-ptr-spinner {
    animation: none !important;
  }
}
