/**
 * Mobile Enhancements v2.0
 * 
 * General mobile optimizations for all devices including:
 * - Android
 * - iOS (supplemented by ios-enhancements.css)
 * - Touch devices
 * - Small screens
 */

/* ==================== Mobile Reset & Base ==================== */

/* Consistent box-sizing */
*,
*::before,
*::after {
  box-sizing: border-box;
}

/* Remove default tap highlight */
* {
  -webkit-tap-highlight-color: transparent;
}

/* Prevent horizontal overflow */
html,
body {
  overflow-x: hidden;
  max-width: 100vw;
}

/* ==================== Touch-Friendly Sizing ==================== */

/* Minimum touch target size (48dp recommended by Material Design) */
.touch-target {
  min-width: 48px;
  min-height: 48px;
  padding: 8px;
}

.touch-target-sm {
  min-width: 40px;
  min-height: 40px;
  padding: 6px;
}

.touch-target-lg {
  min-width: 56px;
  min-height: 56px;
  padding: 12px;
}

/* ==================== Mobile Typography ==================== */

/* Prevent text size adjustment on orientation change */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* Responsive base font size */
@media screen and (max-width: 374px) {
  html {
    font-size: 14px;
  }
}

@media screen and (min-width: 375px) and (max-width: 424px) {
  html {
    font-size: 15px;
  }
}

@media screen and (min-width: 425px) and (max-width: 767px) {
  html {
    font-size: 16px;
  }
}

/* ==================== Mobile Scrolling ==================== */

/* Smooth scrolling container */
.mobile-scroll {
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  scroll-behavior: smooth;
}

/* Hide scrollbar on mobile */
.mobile-scroll-hidden::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
}

.mobile-scroll-hidden {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

/* Thin scrollbar for mobile */
.mobile-scroll-thin::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}

.mobile-scroll-thin::-webkit-scrollbar-track {
  background: transparent;
}

.mobile-scroll-thin::-webkit-scrollbar-thumb {
  background: rgba(100, 116, 139, 0.3);
  border-radius: 4px;
}

/* ==================== Mobile Forms ==================== */

/* Prevent zoom on input focus */
input,
select,
textarea {
  font-size: 16px;
}

/* Mobile-optimized input */
.mobile-input {
  font-size: 16px;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid #334155;
  background: #1e293b;
  color: #f1f5f9;
  width: 100%;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.mobile-input:focus {
  outline: none;
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.mobile-input::placeholder {
  color: #64748b;
}

/* Larger textarea for mobile */
.mobile-textarea {
  min-height: 100px;
  resize: vertical;
}

/* ==================== Mobile Buttons ==================== */

.mobile-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 24px;
  min-height: 48px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 15px;
  touch-action: manipulation;
  transition: all 0.2s;
  cursor: pointer;
  border: none;
}

.mobile-button:active {
  transform: scale(0.97);
  opacity: 0.9;
}

.mobile-button-primary {
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  color: white;
}

.mobile-button-secondary {
  background: #1e293b;
  color: #f1f5f9;
  border: 1px solid #334155;
}

.mobile-button-danger {
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  color: white;
}

/* Icon button */
.mobile-icon-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: #94a3b8;
  transition: all 0.2s;
  touch-action: manipulation;
}

.mobile-icon-button:active {
  background: rgba(100, 116, 139, 0.2);
  transform: scale(0.95);
}

/* ==================== Mobile Cards ==================== */

.mobile-card {
  background: #1e293b;
  border-radius: 16px;
  border: 1px solid #334155;
  padding: 16px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

/* ==================== Mobile Modal ==================== */

.mobile-modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 100;
}

.mobile-modal {
  position: fixed;
  left: 8px;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: #1e293b;
  border-radius: 20px;
  border: 1px solid #334155;
  max-height: 85vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  z-index: 101;
}

/* Bottom sheet modal */
.mobile-bottom-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #1e293b;
  border-radius: 24px 24px 0 0;
  border: 1px solid #334155;
  border-bottom: none;
  max-height: 90vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  z-index: 101;
  padding-bottom: env(safe-area-inset-bottom, 16px);
}

.mobile-bottom-sheet-handle {
  width: 36px;
  height: 4px;
  background: #475569;
  border-radius: 2px;
  margin: 12px auto;
}

/* ==================== Mobile Navigation ==================== */

.mobile-nav-bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #0f172a;
  border-top: 1px solid #1e293b;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 50;
}

.mobile-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 16px;
  color: #64748b;
  font-size: 12px;
  touch-action: manipulation;
  transition: color 0.2s;
}

.mobile-nav-item.active {
  color: #6366f1;
}

/* ==================== Mobile Sidebar ==================== */

.mobile-sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 90;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, visibility 0.3s;
}

.mobile-sidebar-overlay.open {
  opacity: 1;
  visibility: visible;
}

.mobile-sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 85%;
  max-width: 320px;
  background: #0f172a;
  border-right: 1px solid #1e293b;
  transform: translateX(-100%);
  transition: transform 0.3s ease-out;
  z-index: 91;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

.mobile-sidebar.open {
  transform: translateX(0);
}

/* ==================== Mobile Chat ==================== */

.mobile-chat-container {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100vh; /* Fallback */
  overflow: hidden;
}

.mobile-chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 16px;
  padding-bottom: 80px; /* Space for input */
}

.mobile-chat-input-container {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #0f172a;
  border-top: 1px solid #1e293b;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  z-index: 40;
}

.mobile-chat-input {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  background: #1e293b;
  border-radius: 24px;
  padding: 8px 12px;
  border: 1px solid #334155;
}

.mobile-chat-input textarea {
  flex: 1;
  background: transparent;
  border: none;
  resize: none;
  color: #f1f5f9;
  font-size: 16px;
  line-height: 1.5;
  max-height: 120px;
  min-height: 24px;
}

.mobile-chat-input textarea:focus {
  outline: none;
}

.mobile-chat-send-button {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  color: white;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  touch-action: manipulation;
}

/* ==================== Mobile Message Bubble ==================== */

.mobile-message {
  max-width: 85%;
  margin-bottom: 12px;
  animation: message-fade-in 0.2s ease-out;
}

.mobile-message-user {
  margin-left: auto;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  color: white;
  border-radius: 20px 20px 4px 20px;
  padding: 12px 16px;
}

.mobile-message-assistant {
  margin-right: auto;
  background: #1e293b;
  color: #f1f5f9;
  border-radius: 20px 20px 20px 4px;
  padding: 12px 16px;
  border: 1px solid #334155;
}

@keyframes message-fade-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ==================== Mobile Pull to Refresh ==================== */

.mobile-ptr-indicator {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%) translateY(-100%);
  padding: 12px;
  color: #94a3b8;
  transition: transform 0.2s;
}

.mobile-ptr-indicator.pulling {
  transform: translateX(-50%) translateY(0);
}

/* ==================== Mobile Loading States ==================== */

.mobile-skeleton {
  background: linear-gradient(90deg, #1e293b 25%, #334155 50%, #1e293b 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s ease-in-out infinite;
  border-radius: 8px;
}

@keyframes skeleton-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* ==================== Mobile Responsive Grid ==================== */

.mobile-grid {
  display: grid;
  gap: 12px;
}

.mobile-grid-1 {
  grid-template-columns: 1fr;
}

.mobile-grid-2 {
  grid-template-columns: repeat(2, 1fr);
}

@media screen and (min-width: 480px) {
  .mobile-grid-3-sm {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* ==================== Orientation-Specific ==================== */

@media screen and (orientation: landscape) and (max-height: 500px) {
  /* Compact layout for landscape phones */
  .mobile-chat-messages {
    padding: 8px;
    padding-bottom: 60px;
  }
  
  .mobile-chat-input-container {
    padding: 8px 16px;
  }
  
  .mobile-message {
    margin-bottom: 8px;
  }
  
  .mobile-message-user,
  .mobile-message-assistant {
    padding: 8px 12px;
  }
}

/* ==================== Dark Mode Adjustments ==================== */

@media (prefers-color-scheme: dark) {
  .mobile-input {
    background: #1e293b;
    border-color: #334155;
    color: #f1f5f9;
  }
  
  .mobile-card {
    background: #1e293b;
    border-color: #334155;
  }
}

/* Light mode */
@media (prefers-color-scheme: light) {
  .mobile-input {
    background: #ffffff;
    border-color: #e2e8f0;
    color: #0f172a;
  }
  
  .mobile-card {
    background: #ffffff;
    border-color: #e2e8f0;
  }
}

/* ==================== Buttery Smooth Animations (Android/iOS) ==================== */

/* Spring animation for bouncy feel */
.spring-pop {
  animation: springPop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes springPop {
  0% {
    transform: scale(0.8);
    opacity: 0;
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* Smooth slide up for modals */
.slide-up-smooth {
  animation: slideUpSmooth 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes slideUpSmooth {
  from {
    transform: translateY(100%);
    opacity: 0.5;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* Fade scale for popups */
.fade-scale-smooth {
  animation: fadeScaleSmooth 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes fadeScaleSmooth {
  from {
    transform: scale(0.95);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

/* ==================== Avatar Specific Animations ==================== */

.avatar-bounce {
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.avatar-bounce:active {
  transform: scale(0.92);
}

.avatar-glow-pulse {
  animation: avatarGlowPulse 2s ease-in-out infinite;
}

@keyframes avatarGlowPulse {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.4);
  }
  50% {
    box-shadow: 0 0 0 8px rgba(99, 102, 241, 0);
  }
}

/* ==================== Touch Feedback ==================== */

/* Subtle haptic-like visual feedback */
.touch-feedback {
  position: relative;
  overflow: hidden;
}

.touch-feedback::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at var(--touch-x, 50%) var(--touch-y, 50%), 
    rgba(255, 255, 255, 0.2) 0%, 
    transparent 60%);
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}

.touch-feedback:active::after {
  opacity: 1;
}

/* ==================== Smooth Content Transitions ==================== */

.content-fade {
  transition: opacity 0.2s ease-out, transform 0.2s ease-out;
}

.content-fade.entering {
  opacity: 0;
  transform: translateY(10px);
}

.content-fade.entered {
  opacity: 1;
  transform: translateY(0);
}

/* ==================== Mobile-First Smooth Scrolling ==================== */

.smooth-scroll-mobile {
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: y proximity;
}

.scroll-snap-item {
  scroll-snap-align: start;
  scroll-snap-stop: normal;
}

/* Hide scrollbar but keep functionality */
.scrollbar-hidden-mobile {
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.scrollbar-hidden-mobile::-webkit-scrollbar {
  display: none;
}

/* ==================== Performance Optimizations ==================== */

/* Force GPU acceleration for smooth animations */
.gpu-render {
  transform: translate3d(0, 0, 0);
  -webkit-transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  perspective: 1000px;
  will-change: transform, opacity;
}

/* Optimize large lists */
.virtualized-list-item {
  contain: layout style paint;
}

/* ==================== Gesture-Friendly Elements ==================== */

/* Swipeable card */
.swipe-card {
  touch-action: pan-x pan-y;
  user-select: none;
  -webkit-user-select: none;
}

/* Draggable element */
.draggable {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
}

.draggable:active {
  cursor: grabbing;
}

/* ==================== Android Specific ==================== */

@supports not (-webkit-touch-callout: none) {
  /* Android-specific optimizations */
  .mobile-button:active {
    /* Slightly longer press animation for Android */
    transition: transform 0.1s;
  }
  
  /* Android ripple effect */
  .android-ripple {
    position: relative;
    overflow: hidden;
  }
  
  .android-ripple::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.3);
    transform: translate(-50%, -50%);
    transition: width 0.3s ease-out, height 0.3s ease-out;
  }
  
  .android-ripple:active::before {
    width: 200%;
    height: 200%;
  }
}

/* ==================== iOS Specific ==================== */

@supports (-webkit-touch-callout: none) {
  /* iOS-specific rubber band prevention */
  .no-bounce {
    overscroll-behavior: none;
    -webkit-overflow-scrolling: auto;
  }
  
  /* iOS safe area handling */
  .ios-safe-top {
    padding-top: env(safe-area-inset-top, 20px);
  }
  
  .ios-safe-bottom {
    padding-bottom: env(safe-area-inset-bottom, 20px);
  }
  
  .ios-safe-all {
    padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) 
             env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
  }
  
  /* iOS notch-aware header */
  .ios-header {
    padding-top: max(12px, env(safe-area-inset-top));
  }
  
  /* iOS home indicator aware footer */
  .ios-footer {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
}

/* ==================== Reduced Motion ==================== */

@media (prefers-reduced-motion: reduce) {
  .spring-pop,
  .slide-up-smooth,
  .fade-scale-smooth,
  .avatar-glow-pulse {
    animation: none !important;
  }
  
  .avatar-bounce,
  .content-fade,
  .touch-feedback::after {
    transition: none !important;
  }
}

/* ==================== Print Styles ==================== */

@media print {
  .mobile-nav-bottom,
  .mobile-chat-input-container,
  .mobile-sidebar-overlay,
  .mobile-sidebar {
    display: none !important;
  }
}

