/**
 * Device-Specific Optimizations
 * Optimizes the site for all devices: mobile, tablet, desktop, touch, etc.
 * FIXED: Removed redundancies, conflicts, and performance issues
 */

/* =============================================================================
   TOUCH DEVICE OPTIMIZATIONS
   ============================================================================= */

/* Larger tap targets for touch devices (minimum 44x44px per WCAG) */
@media (hover: none) and (pointer: coarse) {
  /* Mobile touch devices */

  button,
  a.btn,
  .cookie-consent-btn,
  .nav-dropdown-toggle,
  .menu-toggle,
  #langToggle,
  #themeToggle {
    min-height: 44px;
    min-width: 44px;
    padding: 12px 16px;
  }

  /* Navigation links - adequate touch targets */
  nav a {
    min-height: 44px;
    padding: 10px 12px;
  }

  /* Card interactive elements */
  .card[role="button"],
  .card.clickable {
    min-height: 60px;
    padding: 1rem;
  }

  /* FAQ cards - adequate tap area */
  #faq .card {
    min-height: 80px;
  }

  /* Form elements - larger touch targets */
  input,
  select,
  textarea,
  .form-control {
    min-height: 48px;
    padding: 12px 16px;
    font-size: 16px; /* Prevent iOS zoom */
  }

  /* Checkbox and radio - larger click area */
  input[type="checkbox"],
  input[type="radio"] {
    min-width: 24px;
    min-height: 24px;
    margin: 8px;
  }

  /* Links in text - adequate spacing */
  p a,
  li a {
    padding: 4px 2px;
    margin: -4px -2px;
  }

  /* Pricing card buttons */
  .card .btn {
    min-height: 48px;
    padding: 14px 20px;
  }

  /* Footer links - WCAG compliant */
  footer a {
    min-height: 44px;
    display: inline-block;
    padding: 8px 4px;
  }

  /* Modal close button */
  .modal-close {
    min-width: 44px;
    min-height: 44px;
    padding: 10px;
  }

  /* Carousel/slider controls */
  [id*="carousel-prev"],
  [id*="carousel-next"],
  .slider-control {
    min-width: 48px;
    min-height: 48px;
  }

  /* Social links / icon buttons */
  .social-link,
  .icon-button {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Dropdown toggles */
  .dropdown-toggle,
  [aria-haspopup="true"] {
    min-height: 44px;
    padding: 10px 16px;
  }

  /* Remove hover effects on touch (they stick) */
  a:hover,
  button:hover,
  .btn:hover {
    transition: none;
  }

  /* Better tap highlight - specific elements only */
  button,
  a,
  input,
  select,
  textarea,
  .card[role="button"] {
    -webkit-tap-highlight-color: rgba(91, 138, 255, 0.2);
    tap-highlight-color: rgba(91, 138, 255, 0.2);
  }

  /* Smoother scrolling on touch - scrollable containers */
  .section,
  main,
  .modal,
  [id*="carousel"],
  .image-gallery {
    -webkit-overflow-scrolling: touch;
  }

  /* Disable text selection on UI elements */
  button,
  .btn,
  nav a {
    -webkit-user-select: none;
    user-select: none;
  }

  /* Add spacing between interactive elements */
  button + button,
  .btn + .btn,
  a.btn + a.btn {
    margin-top: 8px;
  }

  /* Ensure adequate spacing in button groups */
  .btn-group button,
  .btn-group .btn {
    margin: 4px 0;
  }

  /* Better spacing for styled navigation links only */
  .card a:not(.btn),
  .feature-link {
    text-decoration: underline;
    text-decoration-thickness: 2px;
    text-underline-offset: 2px;
  }
}

/* =============================================================================
   MOBILE DEVICE OPTIMIZATIONS (< 768px)
   ============================================================================= */

@media (max-width: 767px) {
  /* Reduce animations for better performance - specific elements */
  .card,
  .btn,
  .modal,
  .hero,
  section {
    animation-duration: 0.3s;
    transition-duration: 0.2s;
  }

  /* Full-width sections */
  .section {
    padding: 3rem 1rem;
  }

  /* Readable line lengths */
  p,
  li {
    max-width: 100%;
  }

  /* Larger fonts for readability */
  body {
    font-size: 16px;
    line-height: 1.6;
  }

  h1 {
    font-size: 2rem;
  }

  h2 {
    font-size: 1.5rem;
  }

  h3 {
    font-size: 1.25rem;
  }

  /* Stack elements vertically */
  .flex-row {
    flex-direction: column;
  }

  /* RESPONSIVE IMAGES & MEDIA - CONSOLIDATED */
  img {
    max-width: 100%;
    height: auto;
    display: block;
  }

  video {
    max-width: 100%;
    height: auto;
  }

  iframe {
    max-width: 100%;
  }

  /* HEADER & NAVIGATION */
  /* Removed header padding that was causing vertical scroll */

  header .container {
    padding: 0 1rem;
  }

  .brand {
    font-size: 1.25rem;
  }

  /* Mobile menu button spacing */
  .menu-toggle {
    margin-left: 0.5rem;
  }

  /* HERO SECTION */
  .hero {
    padding: 2rem 1rem 3rem;
    min-height: auto;
  }

  .hero .container {
    padding: 0 1rem;
  }

  /* Hero badges - stack on very small screens */
  .badgebar {
    margin-bottom: 1rem;
  }

  .badge {
    font-size: 0.7rem;
    padding: 0.4rem 0.8rem;
  }

  /* Trust badges/pills - improve wrapping */
  .mini-pill {
    font-size: 0.75rem;
    padding: 0.5rem 0.8rem;
    white-space: normal;
    text-align: center;
  }

  /* Hero headline */
  .headline.xl {
    font-size: clamp(1.75rem, 8vw, 2.25rem);
    line-height: 1.2;
    margin-bottom: 1rem;
  }

  /* Hero CTAs - stack vertically */
  .hero-ctas {
    flex-direction: column;
    width: 100%;
    gap: 0.75rem;
  }

  .hero-ctas .btn {
    width: 100%;
    text-align: center;
    padding: 0.875rem 1.5rem;
    font-size: 1rem;
  }

  /* Hero grid - ensure single column */
  .hero-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  /* PRICING SECTION */
  #pricing .container {
    padding: 0 1rem;
  }

  /* Trust badges in pricing - stack better */
  #pricing .container > div:first-of-type {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }

  #pricing .container > div:first-of-type > div {
    justify-content: center;
    padding: 0.75rem 1rem;
  }

  /* Pricing cards grid - consolidated with features */
  .pricing-grid,
  .grid.two-col,
  .grid.three-col,
  .grid.auto-300 {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  /* Pricing card styling - CONSOLIDATED */
  .card {
    padding: 1.5rem;
  }

  .card .btn {
    width: 100%;
    font-size: 1rem;
  }

  /* FAQ SECTION */
  #faq .card {
    padding: 1.25rem;
  }

  /* FOOTER */
  footer {
    padding: 2rem 0 1.5rem;
    margin-top: 4rem;
  }

  footer .container {
    padding: 0 1rem;
  }

  footer .container > div:first-child {
    grid-template-columns: 1fr;
    gap: 2rem;
  }

  /* Comparison slider and interactive elements */
  #comparison-slider {
    margin: 0 -1rem;
    width: calc(100% + 2rem);
  }

  /* Signal legend - stack items */
  .signal-legend {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  .signal-item {
    padding: 0.75rem;
    font-size: 0.85rem;
  }

  /* Carousel controls - position inside on mobile */
  [id*="carousel-prev"],
  [id*="carousel-next"] {
    position: relative;
    left: auto;
    right: auto;
    top: auto;
    transform: none;
    margin: 0.5rem;
  }

  /* Carousel wrapper for mobile controls */
  [id*="carousel"] {
    margin: 0;
    padding: 0.5rem 0;
  }

  /* Container adjustments - no !important */
  .container {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  /* Reduce large spacing - specific elements only */
  .hero,
  .section,
  .pricing-grid,
  .grid {
    gap: 1rem;
  }

  /* Modal improvements */
  .modal-content {
    width: 95%;
    max-width: 95%;
    margin: 1rem;
    padding: 1.5rem;
  }

  /* FORM OPTIMIZATIONS - CONSOLIDATED */
  /* Stack form fields vertically */
  form .grid,
  form .flex-row {
    flex-direction: column;
    grid-template-columns: 1fr;
  }

  /* Full width form elements */
  form input,
  form select,
  form textarea,
  form button {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  /* Better form spacing */
  form label {
    display: block;
    margin-bottom: 0.5rem;
  }

  form input,
  form select,
  form textarea {
    margin-bottom: 1rem;
  }

  /* Form buttons */
  form .btn,
  form button[type="submit"] {
    width: 100%;
    margin-top: 0.5rem;
  }

  /* Inline form buttons - stack */
  form .btn + .btn {
    margin-left: 0;
    margin-top: 0.75rem;
  }

  /* Button groups - stack vertically */
  .btn-group {
    flex-direction: column;
    width: 100%;
  }

  .btn-group .btn {
    width: 100%;
    margin-bottom: 0.5rem;
  }

  /* HORIZONTAL SCROLL PREVENTION - SAFE APPROACH */
  body,
  html {
    overflow-x: hidden;
  }

  /* Fix tables on mobile */
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Fix pre/code blocks */
  pre,
  code {
    max-width: 100%;
    overflow-x: auto;
    word-wrap: break-word;
    white-space: pre-wrap;
  }

  /* Fix long URLs and text - specific elements */
  p,
  span,
  div,
  li {
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
  }

  /* Aspect ratio containers for videos */
  .video-container,
  .embed-container {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 */
    height: 0;
    overflow: hidden;
  }

  .video-container iframe,
  .video-container video,
  .embed-container iframe,
  .embed-container video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  /* Image galleries - horizontal scroll */
  .image-gallery,
  [id*="carousel"] {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  /* Styled scrollbar for galleries */
  .image-gallery::-webkit-scrollbar,
  [id*="carousel"]::-webkit-scrollbar {
    height: 4px;
  }

  .image-gallery::-webkit-scrollbar-track,
  [id*="carousel"]::-webkit-scrollbar-track {
    background: transparent;
  }

  .image-gallery::-webkit-scrollbar-thumb,
  [id*="carousel"]::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
  }

  /* TYPOGRAPHY IMPROVEMENTS */
  p,
  li,
  td {
    line-height: 1.7;
  }

  p + p {
    margin-top: 1rem;
  }

  ul,
  ol {
    padding-left: 1.5rem;
  }

  li {
    margin-bottom: 0.5rem;
  }

  h1, h2, h3, h4, h5, h6 {
    margin-top: 1.5rem;
    margin-bottom: 0.75rem;
    line-height: 1.3;
  }

  h1:first-child,
  h2:first-child,
  h3:first-child {
    margin-top: 0;
  }

  a {
    word-break: break-word;
  }

  strong,
  b {
    font-weight: 600;
  }

  blockquote {
    margin: 1.5rem 0;
    padding: 1rem;
    border-left: 4px solid var(--brand);
    font-style: italic;
  }
}

/* =============================================================================
   VERY SMALL MOBILE (< 480px) - Extra Small Screens
   ============================================================================= */

@media (max-width: 479px) {
  /* Even more compact spacing */
  .section {
    padding: 2rem 0.75rem;
  }

  .hero {
    padding: 1.5rem 0.75rem 2.5rem;
  }

  /* Smaller headlines on tiny screens */
  .headline.xl {
    font-size: clamp(1.5rem, 7vw, 2rem);
  }

  .headline.lg {
    font-size: 1.35rem;
  }

  /* Compact badges */
  .badge {
    font-size: 0.65rem;
    padding: 0.35rem 0.7rem;
  }

  .mini-pill {
    font-size: 0.7rem;
    padding: 0.4rem 0.7rem;
  }

  /* Buttons maintain WCAG 44px minimum */
  .btn {
    padding: 0.75rem 1.25rem;
    font-size: 0.95rem;
    min-height: 44px;
  }

  /* Tighter card padding */
  .card {
    padding: 1rem;
  }

  /* Smaller modal */
  .modal-content {
    width: 98%;
    margin: 0.5rem;
    padding: 1rem;
  }

  /* Reduce header size */
  .brand {
    font-size: 1.1rem;
  }

  /* Header buttons - MAINTAIN WCAG 44px */
  header button,
  .menu-toggle,
  #themeToggle,
  #langToggle {
    min-width: 44px;
    min-height: 44px;
  }

  /* Compact footer */
  footer {
    padding: 1.5rem 0 1rem;
  }

  footer .container > div {
    gap: 1.5rem;
  }

  /* Footer links - MAINTAIN WCAG 44px */
  footer a {
    display: inline-block;
    padding: 0.5rem 0;
    min-height: 44px;
    line-height: 1.5;
  }
}

/* =============================================================================
   MEDIUM MOBILE (480px - 767px) - Standard Mobile Phones
   ============================================================================= */

@media (min-width: 480px) and (max-width: 767px) {
  /* Override some very-small-mobile rules for better spacing */
  .hero {
    padding: 2.5rem 1rem 3rem;
  }

  .headline.xl {
    font-size: clamp(1.85rem, 7vw, 2.25rem);
  }

  .card {
    padding: 1.35rem;
  }

  .btn {
    padding: 0.825rem 1.5rem;
    font-size: 0.975rem;
    min-height: 44px;
  }

  .modal-content {
    width: 90%;
    max-width: 450px;
    padding: 1.5rem;
  }

  /* Hero CTAs - can be side by side if space */
  .hero-ctas {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .hero-ctas .btn {
    flex: 1 1 calc(50% - 0.5rem);
    min-width: 140px;
  }
}

/* =============================================================================
   TABLET OPTIMIZATIONS (768px - 1024px)
   ============================================================================= */

@media (min-width: 768px) and (max-width: 1024px) {
  /* Balanced spacing */
  .section {
    padding: 4rem 2rem;
  }

  /* Comfortable reading width */
  p,
  li {
    max-width: 70ch;
  }

  /* Two-column layouts where appropriate */
  .grid-2 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 2rem;
  }

  /* Touch-friendly sizing - WCAG compliant 44px */
  button,
  a.btn {
    min-height: 44px;
    padding: 12px 20px;
  }

  /* Header improvements */
  header .container {
    padding: 0 1.5rem;
  }

  /* Hero section for tablets */
  .hero {
    padding: 3rem 2rem 4rem;
  }

  .hero .container {
    max-width: 700px;
    margin: 0 auto;
  }

  /* Hero CTAs - keep them horizontal on tablet */
  .hero-ctas {
    justify-content: center;
    gap: 1rem;
  }

  .hero-ctas .btn {
    width: auto;
    min-width: 180px;
  }

  /* Pricing section - 2 columns on tablet */
  .pricing-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 1.5rem;
  }

  /* FAQ - 2 columns on tablet */
  #faq .grid.auto-300 {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Features grid - 2 columns */
  .grid.three-col {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Footer - 2 columns */
  footer .container > div:first-child {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Container padding */
  .container {
    padding-left: 1.5rem;
    padding-right: 1.5rem;
  }
}

/* =============================================================================
   LANDSCAPE MOBILE (Phones in landscape < 896px)
   ============================================================================= */

@media (max-width: 896px) and (max-height: 500px) and (orientation: landscape) {
  /* Reduce vertical padding for landscape phones only */
  .section {
    padding: 2rem 1rem;
  }

  .hero {
    min-height: auto;
    padding: 1.5rem 1rem;
  }

  /* Removed header padding that was causing vertical scroll */

  /* Side-by-side content for landscape */
  .content-split {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
  }
}

/* =============================================================================
   PORTRAIT TABLET (iPad, etc.)
   ============================================================================= */

@media (min-width: 768px) and (max-width: 1024px) and (orientation: portrait) {
  /* Vertical emphasis - don't fight with standard tablet rules */
  .section {
    padding: 5rem 2rem;
  }
}

/* =============================================================================
   LARGE DESKTOP OPTIMIZATIONS (> 1920px)
   ============================================================================= */

@media (min-width: 1921px) {
  /* Limit content width for readability */
  .section .container {
    max-width: 1600px;
  }

  body {
    font-size: 18px;
  }

  .section {
    padding: 6rem 3rem;
  }
}

/* =============================================================================
   SMART TV / LARGE SCREENS (> 2560px)
   ============================================================================= */

@media (min-width: 2560px) {
  body {
    font-size: 20px;
  }

  .container {
    max-width: 1800px;
  }

  .section {
    padding: 8rem 4rem;
  }

  button,
  .btn {
    min-height: 56px;
    padding: 16px 32px;
    font-size: 1.1em;
  }
}

/* =============================================================================
   HIGH-DPI / RETINA DISPLAYS
   ============================================================================= */

@media (-webkit-min-device-pixel-ratio: 2),
       (min-resolution: 192dpi) {
  /* Crisp shadows - specific elements only */
  .card,
  .btn {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  /* Sharper borders on specific UI elements */
  .card,
  .btn,
  input,
  select,
  textarea {
    border-width: 0.5px;
  }
}

/* =============================================================================
   REDUCED MOTION (Accessibility + Battery Saving)
   ============================================================================= */

@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;
  }

  [data-parallax] {
    transform: none !important;
  }

  #particles-canvas {
    display: none;
  }
}

/* =============================================================================
   LOW POWER MODE (Battery Saving)
   ============================================================================= */

html[data-capability="low"] {
  #particles-canvas,
  .aurora-background,
  .animated-gradient {
    display: none !important;
  }

  /* Simpler transitions - specific animated elements */
  .card,
  .btn,
  .modal {
    transition: none !important;
    animation: none !important;
  }

  body::before {
    animation: none !important;
  }
}

/* =============================================================================
   SLOW NETWORK (2G/3G)
   ============================================================================= */

html[data-connection="slow"] {
  img[loading="lazy"] {
    background: var(--bg-soft);
  }

  video[autoplay]:not(#heroVideo) {
    display: none;
  }

  .loading-skeleton {
    display: block;
  }
}

/* =============================================================================
   PRINT OPTIMIZATION
   ============================================================================= */

@media print {
  header,
  footer,
  nav,
  .cookie-consent-banner,
  .sp-chatbot-toggle,
  #particles-canvas,
  .aurora-background,
  button,
  video {
    display: none !important;
  }

  * {
    background: white !important;
    color: black !important;
  }

  .section {
    padding: 1rem 0;
    page-break-inside: avoid;
  }

  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #666;
  }

  h1, h2, h3 {
    page-break-after: avoid;
  }
}

/* =============================================================================
   DARK MODE PREFERENCES (System-level)
   ============================================================================= */

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
  }
}

@media (prefers-color-scheme: light) {
  html:not([data-theme]) {
    color-scheme: light;
  }
}

/* =============================================================================
   HIGH CONTRAST MODE (Accessibility)
   ============================================================================= */

@media (prefers-contrast: high) {
  /* Stronger borders - specific elements */
  button,
  input,
  select,
  textarea,
  .card,
  .btn {
    border-width: 2px !important;
  }

  body {
    color: #000 !important;
  }

  html[data-theme="dark"] body {
    color: #fff !important;
  }

  /* Visible focus indicators */
  *:focus {
    outline: 3px solid currentColor !important;
    outline-offset: 2px !important;
  }
}

/* =============================================================================
   FOLDABLE/DUAL-SCREEN DEVICES
   ============================================================================= */

@media (horizontal-viewport-segments: 2) {
  .container {
    display: grid;
    grid-template-columns: env(viewport-segment-width 0 0) env(viewport-segment-width 1 0);
    gap: env(viewport-segment-left 1 0);
  }
}

/* =============================================================================
   INPUT METHOD DETECTION
   ============================================================================= */

/* Mouse/trackpad users get hover effects */
@media (hover: hover) and (pointer: fine) {
  a:hover,
  button:hover,
  .btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }
}

/* Touch + stylus users (Surface, iPad Pro with Apple Pencil) */
@media (hover: none) and (pointer: fine) {
  .interactive-element {
    cursor: pointer;
  }
}

/* Coarse pointer (finger) - need bigger targets */
@media (pointer: coarse) {
  button,
  a.btn,
  .clickable {
    min-width: 44px;
    min-height: 44px;
  }
}

/* =============================================================================
   DEVICE-SPECIFIC FIXES
   ============================================================================= */

/* iOS Safari specific */
@supports (-webkit-touch-callout: none) {
  /* Smooth scrolling containers */
  body,
  .section,
  main {
    -webkit-overflow-scrolling: touch;
  }

  /* Fix iOS input zoom - handled in touch query above */

  /* Fix iOS safe area - body only, header uses padding not safe-area */
  body {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  /* Footer safe area */
  footer {
    padding-bottom: calc(2rem + env(safe-area-inset-bottom));
  }

  /* Header uses regular padding to avoid double-up */
  header {
    padding-left: max(0.5rem, env(safe-area-inset-left));
    padding-right: max(0.5rem, env(safe-area-inset-right));
  }
}

/* Android Chrome specific */
@supports (-webkit-appearance: none) and (not (-webkit-touch-callout: none)) {
  /* Fix Android Chrome address bar height changes */
  .hero {
    min-height: calc(100vh - 56px);
  }
}

/* =============================================================================
   PERFORMANCE TIER OPTIMIZATIONS (Applied via JS)
   ============================================================================= */

html[data-capability="high"] {
  --animation-speed: 1s;
  --transition-speed: 0.3s;
}

html[data-capability="medium"] {
  --animation-speed: 0.5s;
  --transition-speed: 0.2s;

  video:not(#heroVideo) {
    display: none;
  }
}

html[data-capability="low"] {
  --animation-speed: 0.1s;
  --transition-speed: 0.1s;

  #particles-canvas,
  .aurora-background,
  video:not(#heroVideo),
  .animated-gradient {
    display: none !important;
  }
}

/* =============================================================================
   NETWORK-AWARE LOADING
   ============================================================================= */

html[data-connection="2g"],
html[data-connection="3g"],
html[data-connection="slow-2g"] {
  video[autoplay]:not(#heroVideo) {
    display: none;
  }

  body::before,
  .aurora-background {
    display: none;
  }
}

html[data-save-data="true"] {
  video:not(#heroVideo),
  #particles-canvas,
  .aurora-background {
    display: none !important;
  }

  /* No background images on decorative elements */
  .hero::before,
  .section::before {
    background-image: none !important;
  }
}
