/**
 * ════════════════════════════════════════════════════════════════════════════════
 * QENEX UNIVERSAL RESPONSIVE ENGINE v1.0
 * Advanced Responsive System with Container Queries
 * ════════════════════════════════════════════════════════════════════════════════
 *
 * @author QENEX LTD | UK #16523814
 * @created 2026-01-23
 */

/* ════════════════════════════════════════════════════════════════════════════════
   VIEWPORT BREAKPOINTS
   ════════════════════════════════════════════════════════════════════════════════ */

:root {
  /* Standard Breakpoints */
  --bp-xs: 320px;   /* Small phones */
  --bp-sm: 480px;   /* Large phones */
  --bp-md: 768px;   /* Tablets */
  --bp-lg: 1024px;  /* Small desktops */
  --bp-xl: 1280px;  /* Large desktops */
  --bp-2xl: 1536px; /* Extra large screens */
  --bp-3xl: 1920px; /* 4K and ultrawide */

  /* Container Max Widths */
  --container-xs: 100%;
  --container-sm: 540px;
  --container-md: 720px;
  --container-lg: 960px;
  --container-xl: 1140px;
  --container-2xl: 1320px;
  --container-3xl: 1600px;

  /* Fluid Typography Scale */
  --fluid-min-width: 320;
  --fluid-max-width: 1920;
  --fluid-min-size: 16;
  --fluid-max-size: 20;

  /* Safe Area Insets (for notched devices) */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

/* ════════════════════════════════════════════════════════════════════════════════
   CONTAINER QUERY SYSTEM
   ════════════════════════════════════════════════════════════════════════════════ */

/* Container Context */
.container-query {
  container-type: inline-size;
  container-name: component;
}

.container-query-block {
  container-type: size;
  container-name: component-block;
}

/* Card Container Queries */
.card-responsive {
  container-type: inline-size;
  container-name: card;
}

@container card (min-width: 400px) {
  .card-responsive .card-content {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 1.5rem;
  }

  .card-responsive .card-image {
    aspect-ratio: 1;
    border-radius: 1rem;
  }
}

@container card (min-width: 600px) {
  .card-responsive .card-content {
    grid-template-columns: 1fr 3fr;
    gap: 2rem;
  }

  .card-responsive .card-title {
    font-size: 1.5rem;
  }
}

/* Feature Grid Container Queries */
.features-responsive {
  container-type: inline-size;
  container-name: features;
}

@container features (min-width: 400px) {
  .features-responsive .feature-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@container features (min-width: 700px) {
  .features-responsive .feature-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@container features (min-width: 1000px) {
  .features-responsive .feature-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* Stats Container Queries */
.stats-responsive {
  container-type: inline-size;
  container-name: stats;
}

@container stats (max-width: 500px) {
  .stats-responsive .stat-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .stats-responsive .stat-value {
    font-size: 2.5rem;
  }
}

@container stats (min-width: 500px) {
  .stats-responsive .stat-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@container stats (min-width: 800px) {
  .stats-responsive .stat-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ════════════════════════════════════════════════════════════════════════════════
   FLUID TYPOGRAPHY
   ════════════════════════════════════════════════════════════════════════════════ */

html {
  /* Base fluid font size: 16px at 320px → 20px at 1920px */
  font-size: clamp(
    1rem,
    calc(1rem + ((1vw - 0.2rem) * 0.25)),
    1.25rem
  );
}

/* Fluid Type Scale */
.fluid-xs   { font-size: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem); }
.fluid-sm   { font-size: clamp(0.875rem, 0.8rem + 0.375vw, 1rem); }
.fluid-base { font-size: clamp(1rem, 0.9rem + 0.5vw, 1.125rem); }
.fluid-lg   { font-size: clamp(1.125rem, 1rem + 0.625vw, 1.375rem); }
.fluid-xl   { font-size: clamp(1.25rem, 1.1rem + 0.75vw, 1.625rem); }
.fluid-2xl  { font-size: clamp(1.5rem, 1.2rem + 1.5vw, 2.25rem); }
.fluid-3xl  { font-size: clamp(1.875rem, 1.4rem + 2.375vw, 3rem); }
.fluid-4xl  { font-size: clamp(2.25rem, 1.5rem + 3.75vw, 4.5rem); }
.fluid-5xl  { font-size: clamp(3rem, 1.8rem + 6vw, 6rem); }
.fluid-6xl  { font-size: clamp(3.75rem, 2rem + 8.75vw, 8rem); }

/* ════════════════════════════════════════════════════════════════════════════════
   RESPONSIVE GRID SYSTEM
   ════════════════════════════════════════════════════════════════════════════════ */

.grid-responsive {
  display: grid;
  gap: var(--grid-gap, 1.5rem);
  grid-template-columns: repeat(var(--grid-cols, 1), 1fr);
}

/* Auto-fit Grid - fills available space */
.grid-auto-fit {
  display: grid;
  gap: var(--grid-gap, 1.5rem);
  grid-template-columns: repeat(auto-fit, minmax(var(--grid-min, 280px), 1fr));
}

/* Auto-fill Grid - maintains minimum width */
.grid-auto-fill {
  display: grid;
  gap: var(--grid-gap, 1.5rem);
  grid-template-columns: repeat(auto-fill, minmax(var(--grid-min, 280px), 1fr));
}

/* Responsive column classes */
@media (min-width: 480px) {
  .sm\:grid-cols-2 { --grid-cols: 2; }
  .sm\:grid-cols-3 { --grid-cols: 3; }
}

@media (min-width: 768px) {
  .md\:grid-cols-2 { --grid-cols: 2; }
  .md\:grid-cols-3 { --grid-cols: 3; }
  .md\:grid-cols-4 { --grid-cols: 4; }
}

@media (min-width: 1024px) {
  .lg\:grid-cols-2 { --grid-cols: 2; }
  .lg\:grid-cols-3 { --grid-cols: 3; }
  .lg\:grid-cols-4 { --grid-cols: 4; }
  .lg\:grid-cols-5 { --grid-cols: 5; }
  .lg\:grid-cols-6 { --grid-cols: 6; }
}

@media (min-width: 1280px) {
  .xl\:grid-cols-3 { --grid-cols: 3; }
  .xl\:grid-cols-4 { --grid-cols: 4; }
  .xl\:grid-cols-5 { --grid-cols: 5; }
  .xl\:grid-cols-6 { --grid-cols: 6; }
}

/* ════════════════════════════════════════════════════════════════════════════════
   DEVICE-SPECIFIC OPTIMIZATIONS
   ════════════════════════════════════════════════════════════════════════════════ */

/* iPhone Specific (includes Dynamic Island) */
@supports (padding: env(safe-area-inset-top)) {
  .safe-area-padding {
    padding-top: max(1rem, var(--safe-top));
    padding-right: max(1rem, var(--safe-right));
    padding-bottom: max(1rem, var(--safe-bottom));
    padding-left: max(1rem, var(--safe-left));
  }

  .nav-safe-top {
    padding-top: calc(1rem + var(--safe-top));
  }

  .footer-safe-bottom {
    padding-bottom: calc(1rem + var(--safe-bottom));
  }
}

/* iPhone SE, 12 mini, 13 mini (375px) */
@media (max-width: 375px) {
  :root {
    --spacing-base: 0.75rem;
  }

  .compact-mobile .hero-title {
    font-size: 2rem;
  }

  .compact-mobile .btn {
    padding: 0.75rem 1.25rem;
    font-size: 0.875rem;
  }
}

/* Standard iPhone (390px - 428px) */
@media (min-width: 390px) and (max-width: 428px) {
  .iphone-optimize .nav-items {
    gap: 0.5rem;
  }
}

/* iPhone Pro Max, Plus models (430px+) */
@media (min-width: 430px) and (max-width: 480px) {
  .large-phone .card-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* iPad Mini (744px) */
@media (min-width: 744px) and (max-width: 820px) {
  .ipad-mini-optimize {
    --container-padding: 2rem;
  }
}

/* iPad Air, Pro 11" (820px - 1024px) */
@media (min-width: 820px) and (max-width: 1024px) {
  .ipad-optimize .sidebar {
    width: 280px;
  }

  .ipad-optimize .main-content {
    padding: 2rem;
  }
}

/* iPad Pro 12.9" (1024px - 1366px) */
@media (min-width: 1024px) and (max-width: 1366px) {
  .ipad-pro-optimize .container {
    max-width: 1140px;
  }
}

/* Android-specific touch targets */
@media (pointer: coarse) {
  .touch-target {
    min-height: 48px;
    min-width: 48px;
  }

  .touch-spacing {
    gap: 0.75rem;
  }

  /* Larger tap targets for buttons */
  .btn-touch {
    padding: 1rem 1.5rem;
    min-height: 52px;
  }

  /* Better touch scrolling */
  .scroll-touch {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
  }
}

/* High-resolution displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  .retina-border {
    border-width: 0.5px;
  }
}

/* ════════════════════════════════════════════════════════════════════════════════
   ORIENTATION HANDLING
   ════════════════════════════════════════════════════════════════════════════════ */

/* Portrait Mode */
@media (orientation: portrait) {
  .portrait-stack {
    flex-direction: column;
  }

  .portrait-full-width {
    width: 100%;
  }

  .portrait-hide {
    display: none;
  }
}

/* Landscape Mode */
@media (orientation: landscape) {
  .landscape-row {
    flex-direction: row;
  }

  .landscape-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 280px;
  }

  .landscape-hide {
    display: none;
  }

  /* Mobile landscape (phones) */
  @media (max-height: 500px) {
    .mobile-landscape .hero {
      min-height: auto;
      padding: 2rem 0;
    }

    .mobile-landscape .nav {
      padding: 0.5rem;
    }
  }
}

/* ════════════════════════════════════════════════════════════════════════════════
   RESPONSIVE SPACING
   ════════════════════════════════════════════════════════════════════════════════ */

/* Fluid spacing scale */
.space-fluid-xs { --space: clamp(0.25rem, 0.2rem + 0.25vw, 0.5rem); }
.space-fluid-sm { --space: clamp(0.5rem, 0.4rem + 0.5vw, 1rem); }
.space-fluid-md { --space: clamp(1rem, 0.8rem + 1vw, 2rem); }
.space-fluid-lg { --space: clamp(1.5rem, 1.2rem + 1.5vw, 3rem); }
.space-fluid-xl { --space: clamp(2rem, 1.5rem + 2.5vw, 5rem); }
.space-fluid-2xl { --space: clamp(3rem, 2rem + 5vw, 8rem); }

.p-fluid { padding: var(--space); }
.m-fluid { margin: var(--space); }
.gap-fluid { gap: var(--space); }

/* Section spacing */
.section-spacing {
  padding-top: clamp(3rem, 2rem + 5vw, 8rem);
  padding-bottom: clamp(3rem, 2rem + 5vw, 8rem);
}

.section-spacing-sm {
  padding-top: clamp(2rem, 1.5rem + 2.5vw, 4rem);
  padding-bottom: clamp(2rem, 1.5rem + 2.5vw, 4rem);
}

/* ════════════════════════════════════════════════════════════════════════════════
   RESPONSIVE VISIBILITY
   ════════════════════════════════════════════════════════════════════════════════ */

/* Hide on specific breakpoints */
@media (max-width: 479px) {
  .hide-xs { display: none !important; }
}

@media (min-width: 480px) and (max-width: 767px) {
  .hide-sm { display: none !important; }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .hide-md { display: none !important; }
}

@media (min-width: 1024px) and (max-width: 1279px) {
  .hide-lg { display: none !important; }
}

@media (min-width: 1280px) {
  .hide-xl { display: none !important; }
}

/* Show only on specific breakpoints */
.show-xs, .show-sm, .show-md, .show-lg, .show-xl {
  display: none !important;
}

@media (max-width: 479px) {
  .show-xs { display: block !important; }
  .show-xs-flex { display: flex !important; }
  .show-xs-grid { display: grid !important; }
}

@media (min-width: 480px) and (max-width: 767px) {
  .show-sm { display: block !important; }
  .show-sm-flex { display: flex !important; }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .show-md { display: block !important; }
  .show-md-flex { display: flex !important; }
}

@media (min-width: 1024px) and (max-width: 1279px) {
  .show-lg { display: block !important; }
  .show-lg-flex { display: flex !important; }
}

@media (min-width: 1280px) {
  .show-xl { display: block !important; }
  .show-xl-flex { display: flex !important; }
}

/* ════════════════════════════════════════════════════════════════════════════════
   RESPONSIVE IMAGES
   ════════════════════════════════════════════════════════════════════════════════ */

.img-responsive {
  width: 100%;
  height: auto;
  display: block;
}

.img-cover {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.img-contain {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* Aspect ratio containers */
.aspect-square { aspect-ratio: 1; }
.aspect-video { aspect-ratio: 16 / 9; }
.aspect-portrait { aspect-ratio: 3 / 4; }
.aspect-wide { aspect-ratio: 21 / 9; }

@media (max-width: 767px) {
  .aspect-square-mobile { aspect-ratio: 1; }
  .aspect-video-mobile { aspect-ratio: 16 / 9; }
}

/* ════════════════════════════════════════════════════════════════════════════════
   RESPONSIVE NAVIGATION
   ════════════════════════════════════════════════════════════════════════════════ */

.nav-responsive {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
}

.nav-menu {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.nav-toggle {
  display: none;
  background: none;
  border: none;
  padding: 0.5rem;
  cursor: pointer;
}

@media (max-width: 767px) {
  .nav-toggle {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .nav-toggle span {
    width: 24px;
    height: 2px;
    background: currentColor;
    transition: all 0.3s ease;
  }

  .nav-menu {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--void-dark, #0a0a1f);
    flex-direction: column;
    justify-content: center;
    gap: 2rem;
    transform: translateX(-100%);
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 100;
  }

  .nav-menu.is-open {
    transform: translateX(0);
  }

  .nav-menu a {
    font-size: 1.5rem;
  }
}

/* ════════════════════════════════════════════════════════════════════════════════
   PRINT STYLES
   ════════════════════════════════════════════════════════════════════════════════ */

@media print {
  * {
    background: transparent !important;
    color: black !important;
    box-shadow: none !important;
    text-shadow: none !important;
  }

  .no-print {
    display: none !important;
  }

  .print-only {
    display: block !important;
  }

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

  .container {
    max-width: 100% !important;
    padding: 0 !important;
  }

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

  img {
    max-width: 100% !important;
    page-break-inside: avoid;
  }
}

/* ════════════════════════════════════════════════════════════════════════════════
   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;
  }
}

/* ════════════════════════════════════════════════════════════════════════════════
   HIGH CONTRAST MODE
   ════════════════════════════════════════════════════════════════════════════════ */

@media (prefers-contrast: high) {
  :root {
    --glass-border-light: rgba(255, 255, 255, 0.3);
    --glass-border-medium: rgba(255, 255, 255, 0.5);
  }

  .btn {
    border: 2px solid currentColor;
  }

  a {
    text-decoration: underline;
  }
}

/* ════════════════════════════════════════════════════════════════════════════════
   DARK/LIGHT MODE SUPPORT
   ════════════════════════════════════════════════════════════════════════════════ */

@media (prefers-color-scheme: light) {
  .auto-theme {
    --void-deep: #f8fafc;
    --void-dark: #f1f5f9;
    --void-mid: #e2e8f0;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --glass-bg: rgba(0, 0, 0, 0.03);
    --glass-border: rgba(0, 0, 0, 0.08);
  }
}
