/**
 * Snack Apps Mobile Optimization Styles
 * Comprehensive mobile-first responsive design
 */

/* ===== CSS Variables for Mobile ===== */
:root {
  --touch-target-min: 44px;
  --touch-target-comfortable: 48px;
  --mobile-gutter: 16px;
  --mobile-radius: 12px;
  --transition-fast: 150ms;
  --transition-normal: 250ms;
  --safe-area-top: env(safe-area-inset-top, 0px);
  --safe-area-bottom: env(safe-area-inset-bottom, 0px);
  --safe-area-left: env(safe-area-inset-left, 0px);
  --safe-area-right: env(safe-area-inset-right, 0px);
}

/* ===== Base Mobile Optimizations ===== */
html {
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  scroll-behavior: smooth;
}

body {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overscroll-behavior-y: contain;
  padding-top: var(--safe-area-top);
  padding-bottom: var(--safe-area-bottom);
  padding-left: var(--safe-area-left);
  padding-right: var(--safe-area-right);
}

/* Prevent zoom on input focus (iOS) */
input, select, textarea {
  font-size: 16px !important;
}

/* ===== Touch Targets (WCAG 2.5.5) ===== */
button,
a,
input[type="file"],
input[type="submit"],
input[type="button"],
.touch-target,
[role="button"] {
  min-height: var(--touch-target-min);
  min-width: var(--touch-target-min);
  padding: 12px 16px;
}

/* Larger touch targets for primary actions */
.btn-primary,
.btn-secondary,
.upload-area {
  min-height: var(--touch-target-comfortable);
}

/* ===== Touch Feedback ===== */

/* Ripple effect container */
.touch-ripple {
  position: relative;
  overflow: hidden;
}

.touch-ripple::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  transition: width 0.3s ease, height 0.3s ease, opacity 0.3s ease;
  opacity: 0;
  pointer-events: none;
}

.touch-ripple:active::after {
  width: 200%;
  height: 200%;
  opacity: 1;
  transition: 0s;
}

/* Active state feedback */
button:active,
a:active,
.touch-target:active {
  transform: scale(0.97);
  opacity: 0.9;
  transition: transform var(--transition-fast), opacity var(--transition-fast);
}

/* Pressed state for buttons */
.btn-primary:active {
  transform: scale(0.97) translateY(1px);
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
}

.btn-secondary:active {
  background-color: #e2e8f0;
  transform: scale(0.97);
}

/* ===== Mobile Typography ===== */
@media (max-width: 640px) {
  html {
    font-size: 15px;
  }

  h1 {
    font-size: 1.75rem;
    line-height: 1.2;
  }

  h2 {
    font-size: 1.5rem;
    line-height: 1.25;
  }

  h3 {
    font-size: 1.25rem;
    line-height: 1.3;
  }

  p {
    font-size: 1rem;
    line-height: 1.6;
  }

  .text-sm {
    font-size: 0.875rem;
  }

  .text-xs {
    font-size: 0.75rem;
  }
}

/* Extra small screens */
@media (max-width: 359px) {
  html {
    font-size: 14px;
  }

  h1 {
    font-size: 1.5rem;
  }

  .text-3xl { font-size: 1.5rem; }
  .text-4xl { font-size: 1.75rem; }
  .p-6 { padding: 1rem; }
  .p-8 { padding: 1.25rem; }
  .px-4 { padding-left: 0.75rem; padding-right: 0.75rem; }
  .gap-4 { gap: 0.75rem; }
}

/* ===== Mobile Layout ===== */
@media (max-width: 640px) {
  /* Full-width buttons on mobile */
  .btn-primary,
  .btn-secondary {
    width: 100%;
    justify-content: center;
  }

  /* Stack button groups vertically */
  .button-group,
  .flex.gap-4:has(.btn-primary) {
    flex-direction: column;
    gap: 0.75rem;
  }

  /* Reduce padding on mobile */
  .p-6 { padding: 1rem; }
  .p-8 { padding: 1.25rem; }
  .px-6 { padding-left: 1rem; padding-right: 1rem; }
  .py-8 { padding-top: 1.5rem; padding-bottom: 1.5rem; }

  /* Smaller margins */
  .mb-8 { margin-bottom: 1.5rem; }
  .mb-6 { margin-bottom: 1rem; }
  .mt-8 { margin-top: 1.5rem; }

  /* Upload area adjustments */
  .upload-area {
    padding: 1.5rem 1rem;
  }

  .upload-area .text-6xl {
    font-size: 3rem;
  }

  /* Result card adjustments */
  .result-card {
    padding: 1.5rem;
    border-radius: 1rem;
  }

  .result-card .result-value,
  .match-percent {
    font-size: 2.5rem !important;
  }
}

/* ===== Share Buttons Mobile ===== */
@media (max-width: 640px) {
  .share-buttons {
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 0.5rem;
  }

  .share-buttons button {
    padding: 0.75rem 0.5rem;
  }

  .share-buttons button span.text-\[10px\] {
    font-size: 9px;
  }
}

@media (max-width: 359px) {
  .share-buttons {
    grid-template-columns: repeat(3, 1fr) !important;
  }
}

/* ===== Landscape Orientation ===== */
@media (max-height: 500px) and (orientation: landscape) {
  header {
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
  }

  main {
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
  }

  .upload-area {
    padding: 1rem;
  }

  .upload-area .text-6xl {
    font-size: 2rem;
  }

  .result-card {
    padding: 1rem;
  }
}

/* ===== Safe Area Support ===== */
@supports (padding-top: env(safe-area-inset-top)) {
  header {
    padding-top: max(0.75rem, var(--safe-area-top));
  }

  footer {
    padding-bottom: max(1.5rem, var(--safe-area-bottom));
  }

  /* Floating action buttons */
  .fab,
  .floating-button {
    bottom: max(1rem, calc(var(--safe-area-bottom) + 1rem));
  }
}

/* ===== iOS Specific ===== */
@supports (-webkit-touch-callout: none) {
  /* Disable callout on long press */
  img, a {
    -webkit-touch-callout: none;
  }

  /* Smooth scrolling momentum */
  .scrollable {
    -webkit-overflow-scrolling: touch;
  }

  /* Fix input zoom */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="tel"],
  textarea,
  select {
    font-size: 16px;
  }
}

/* ===== Hardware Acceleration ===== */
.hw-accelerate,
.upload-area,
.result-card,
.btn-primary,
.btn-secondary,
.spinner,
header {
  transform: translateZ(0);
  backface-visibility: hidden;
  perspective: 1000px;
}

/* ===== Scroll Optimizations ===== */
.scroll-container {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  scroll-snap-type: y proximity;
}

.scroll-snap-start {
  scroll-snap-align: start;
}

/* Smooth scroll padding for fixed header */
html {
  scroll-padding-top: 80px;
}

/* ===== Pull to Refresh Prevention ===== */
body.prevent-pull-refresh {
  overscroll-behavior-y: contain;
}

/* ===== Loading States (Mobile) ===== */
.skeleton-loading {
  background: linear-gradient(
    90deg,
    #f0f0f0 25%,
    #e0e0e0 50%,
    #f0f0f0 75%
  );
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s infinite;
}

@keyframes skeleton-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* ===== Mobile Navigation ===== */
@media (max-width: 640px) {
  header nav {
    padding: 0.75rem 1rem;
  }

  header nav a.text-xl {
    font-size: 1rem;
  }

  header nav .text-2xl {
    font-size: 1.5rem;
  }

  /* Language selector mobile */
  #langSelector {
    padding: 0.5rem 0.75rem;
    font-size: 0.8rem;
    min-width: 0;
  }

  /* More Apps button mobile */
  #moreAppsBtn {
    padding: 0.5rem 0.75rem;
    font-size: 0.75rem;
  }

  #moreAppsBtn span[aria-hidden="true"] {
    display: none;
  }
}

/* ===== Image Upload Mobile ===== */
@media (max-width: 640px) {
  #previewImage {
    max-height: 300px;
    width: 100%;
    object-fit: contain;
  }

  .upload-area.has-image {
    padding: 0;
  }
}

/* ===== Modal/Overlay Mobile ===== */
.mobile-modal {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
  padding: 0;
}

.mobile-modal-content {
  width: 100%;
  max-height: 90vh;
  background: white;
  border-radius: 1rem 1rem 0 0;
  padding: 1.5rem;
  padding-bottom: max(1.5rem, var(--safe-area-bottom));
  overflow-y: auto;
  animation: slide-up 0.3s ease;
}

@keyframes slide-up {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

/* ===== Touch Swipe Indicator ===== */
.swipe-indicator {
  width: 40px;
  height: 4px;
  background: #d1d5db;
  border-radius: 2px;
  margin: 0 auto 1rem;
}

/* ===== Stats Counter Mobile ===== */
@media (max-width: 640px) {
  .stats-counter {
    padding: 0.5rem 0.75rem;
    font-size: 0.75rem;
  }
}

/* ===== Reduced Motion ===== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  .touch-ripple::after {
    display: none;
  }
}

/* ===== Dark Mode Mobile ===== */
@media (prefers-color-scheme: dark) {
  .skeleton-loading {
    background: linear-gradient(
      90deg,
      #374151 25%,
      #4b5563 50%,
      #374151 75%
    );
    background-size: 200% 100%;
  }

  .mobile-modal-content {
    background: #1f2937;
  }

  .swipe-indicator {
    background: #4b5563;
  }
}

/* ===== PWA Standalone Mode ===== */
@media (display-mode: standalone) {
  header {
    padding-top: max(1rem, var(--safe-area-top));
  }

  body {
    padding-top: 0;
  }
}

/* ===== Orientation Lock Hint ===== */
.orientation-hint {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 10000;
  background: rgba(0, 0, 0, 0.9);
  color: white;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 1rem;
  text-align: center;
  padding: 2rem;
}

@media (max-height: 400px) and (orientation: landscape) {
  .orientation-hint.show-on-landscape {
    display: flex;
  }
}

/* ===== Floating Action Button ===== */
.fab {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  color: white;
  border: none;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  z-index: 1000;
  transition: transform 0.2s, box-shadow 0.2s;
}

.fab:active {
  transform: scale(0.95);
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
}

@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .fab {
    bottom: max(1rem, calc(var(--safe-area-bottom) + 1rem));
  }
}

/* ===== Notch/Camera Cutout Handling ===== */
@media (min-aspect-ratio: 2/1) {
  body {
    padding-left: var(--safe-area-left);
    padding-right: var(--safe-area-right);
  }
}
