// ==========================================================================
// LAYOUT PATTERNS
// ==========================================================================
// Common layout patterns and organizational structures
// Theme-agnostic layout behaviors
// ==========================================================================

// STACK LAYOUT PATTERN
.stack {
  display: flex;
  flex-direction: column;
  gap: var(--stack-gap, 16px);

  &.stack-sm {
    gap: var(--stack-gap-sm, 8px);
  }

  &.stack-lg {
    gap: var(--stack-gap-lg, 24px);
  }

  &.stack-xl {
    gap: var(--stack-gap-xl, 32px);
  }

  &.stack-horizontal {
    flex-direction: row;
    align-items: center;
  }

  &.stack-center {
    align-items: center;
  }

  &.stack-end {
    align-items: flex-end;
  }

  &.stack-stretch {
    align-items: stretch;
  }
}

// CLUSTER LAYOUT PATTERN (Flexbox wrap)
.cluster {
  display: flex;
  flex-wrap: wrap;
  gap: var(--cluster-gap, 16px);
  align-items: center;

  &.cluster-start {
    justify-content: flex-start;
  }

  &.cluster-center {
    justify-content: center;
  }

  &.cluster-end {
    justify-content: flex-end;
  }

  &.cluster-between {
    justify-content: space-between;
  }

  &.cluster-around {
    justify-content: space-around;
  }

  &.cluster-evenly {
    justify-content: space-evenly;
  }

  &.cluster-baseline {
    align-items: baseline;
  }

  &.cluster-stretch {
    align-items: stretch;
  }
}

// SIDEBAR LAYOUT PATTERN
.sidebar-layout {
  display: flex;
  gap: var(--sidebar-gap, 32px);
  align-items: flex-start;

  .sidebar {
    flex-shrink: 0;
    width: var(--sidebar-width, 250px);
    min-width: var(--sidebar-min-width, 200px);
  }

  .main-content {
    flex: 1;
    min-width: 0; // Prevents flex overflow
  }

  &.sidebar-right {
    flex-direction: row-reverse;
  }

  &.sidebar-sticky {
    .sidebar {
      position: sticky;
      top: var(--sidebar-sticky-top, 20px);
      align-self: flex-start;
    }
  }

  // Responsive behavior
  &.sidebar-collapse-md {
    @media (max-width: 768px) {
      flex-direction: column;
      gap: var(--sidebar-gap-mobile, 24px);

      .sidebar {
        width: 100%;
        position: static;
      }
    }
  }

  &.sidebar-collapse-lg {
    @media (max-width: 992px) {
      flex-direction: column;
      gap: var(--sidebar-gap-mobile, 24px);

      .sidebar {
        width: 100%;
        position: static;
      }
    }
  }
}

// SWITCHER LAYOUT PATTERN (Responsive stack/row)
.switcher {
  display: flex;
  flex-wrap: wrap;
  gap: var(--switcher-gap, 16px);

  > * {
    flex-grow: 1;
    flex-basis: calc((var(--switcher-threshold, 30rem) - 100%) * 999);
    min-width: var(--switcher-min-width, 250px);
  }

  &.switcher-no-grow > * {
    flex-grow: 0;
  }
}

// COVER LAYOUT PATTERN (Centered content)
.cover {
  display: flex;
  flex-direction: column;
  min-height: var(--cover-min-height, 100vh);
  padding: var(--cover-padding, 24px);

  > * {
    margin-top: var(--cover-spacing, 16px);
    margin-bottom: var(--cover-spacing, 16px);
  }

  > :first-child:not(.cover-centered) {
    margin-top: 0;
  }

  > :last-child:not(.cover-centered) {
    margin-bottom: 0;
  }

  .cover-centered {
    margin-top: auto;
    margin-bottom: auto;
  }
}

// IMPOSTER LAYOUT PATTERN (Absolute positioning)
.imposter {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  &.imposter-contain {
    max-width: calc(100% - var(--imposter-margin, 32px));
    max-height: calc(100% - var(--imposter-margin, 32px));
    overflow: auto;
  }

  &.imposter-fixed {
    position: fixed;
  }

  &.imposter-top {
    top: var(--imposter-offset, 20px);
    transform: translate(-50%, 0);
  }

  &.imposter-bottom {
    top: auto;
    bottom: var(--imposter-offset, 20px);
    transform: translate(-50%, 0);
  }

  &.imposter-left {
    left: var(--imposter-offset, 20px);
    transform: translate(0, -50%);
  }

  &.imposter-right {
    left: auto;
    right: var(--imposter-offset, 20px);
    transform: translate(0, -50%);
  }
}

// GRID LAYOUT PATTERNS
.auto-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(var(--auto-grid-min-width, 250px), 1fr));
  gap: var(--auto-grid-gap, 24px);

  &.auto-grid-fill {
    grid-template-columns: repeat(auto-fill, minmax(var(--auto-grid-min-width, 250px), 1fr));
  }

  &.auto-grid-dense {
    grid-auto-flow: dense;
  }
}

.reel {
  display: flex;
  overflow-x: auto;
  gap: var(--reel-gap, 16px);
  padding-bottom: var(--reel-scrollbar-space, 8px);

  > * {
    flex-shrink: 0;
    width: var(--reel-item-width, 250px);
  }

  &.reel-auto-width > * {
    width: auto;
    min-width: var(--reel-min-width, 200px);
  }

  // Snap scrolling
  &.reel-snap {
    scroll-snap-type: x mandatory;

    > * {
      scroll-snap-align: start;
    }
  }

  // Hide scrollbar
  &.reel-no-scrollbar {
    scrollbar-width: none;
    -ms-overflow-style: none;

    &::-webkit-scrollbar {
      display: none;
    }
  }
}

// HOLY GRAIL LAYOUT
.holy-grail {
  display: grid;
  grid-template-areas:
    "header header header"
    "nav main aside"
    "footer footer footer";
  grid-template-rows: auto 1fr auto;
  grid-template-columns: var(--nav-width, 200px) 1fr var(--aside-width, 200px);
  min-height: 100vh;
  gap: var(--holy-grail-gap, 0);

  .header {
    grid-area: header;
  }

  .nav {
    grid-area: nav;
  }

  .main {
    grid-area: main;
    min-width: 0;
  }

  .aside {
    grid-area: aside;
  }

  .footer {
    grid-area: footer;
  }

  // Responsive collapse
  @media (max-width: 768px) {
    grid-template-areas:
      "header"
      "nav"
      "main"
      "aside"
      "footer";
    grid-template-columns: 1fr;
    grid-template-rows: auto auto 1fr auto auto;
  }
}

// PANCAKE STACK LAYOUT (Header, flexible content, footer)
.pancake-stack {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: var(--pancake-min-height, 100vh);

  .header {
    /* Header content */
  }

  .main {
    /* Main content - grows to fill space */
  }

  .footer {
    /* Footer content */
  }
}

// MASONRY LAYOUT
.masonry {
  columns: var(--masonry-columns, 3);
  column-gap: var(--masonry-gap, 24px);

  > * {
    break-inside: avoid;
    margin-bottom: var(--masonry-gap, 24px);
  }

  &.masonry-2 {
    columns: 2;
  }

  &.masonry-4 {
    columns: 4;
  }

  &.masonry-5 {
    columns: 5;
  }

  // Responsive columns
  @media (max-width: 768px) {
    columns: 1;
  }

  @media (min-width: 769px) and (max-width: 1024px) {
    columns: 2;
  }
}

// FLEX EQUAL HEIGHT PATTERN
.equal-height {
  display: flex;
  
  > * {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  &.equal-height-baseline {
    align-items: baseline;
  }

  &.equal-height-center {
    align-items: center;
  }

  &.equal-height-end {
    align-items: flex-end;
  }
}

// STICKY FOOTER PATTERN
.sticky-footer {
  display: flex;
  flex-direction: column;
  min-height: 100vh;

  .content {
    flex: 1;
  }

  .footer {
    margin-top: auto;
  }
}

// ASPECT RATIO CONTAINERS
.aspect-ratio {
  position: relative;
  width: 100%;

  &::before {
    content: '';
    display: block;
    padding-top: var(--aspect-ratio, 56.25%); /* 16:9 default */
  }

  > * {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  &.aspect-1-1 {
    --aspect-ratio: 100%; /* 1:1 */
  }

  &.aspect-4-3 {
    --aspect-ratio: 75%; /* 4:3 */
  }

  &.aspect-3-2 {
    --aspect-ratio: 66.67%; /* 3:2 */
  }

  &.aspect-16-9 {
    --aspect-ratio: 56.25%; /* 16:9 */
  }

  &.aspect-21-9 {
    --aspect-ratio: 42.86%; /* 21:9 */
  }
}

// INTRINSIC RATIO (modern aspect-ratio support)
@supports (aspect-ratio: 16 / 9) {
  .intrinsic-ratio {
    aspect-ratio: var(--intrinsic-ratio, 16 / 9);
    
    &.intrinsic-1-1 {
      aspect-ratio: 1 / 1;
    }

    &.intrinsic-4-3 {
      aspect-ratio: 4 / 3;
    }

    &.intrinsic-3-2 {
      aspect-ratio: 3 / 2;
    }

    &.intrinsic-16-9 {
      aspect-ratio: 16 / 9;
    }

    &.intrinsic-21-9 {
      aspect-ratio: 21 / 9;
    }
  }
}

// QUANTITY QUERIES (Layout based on number of items)
.quantity-query {
  display: flex;
  flex-wrap: wrap;
  gap: var(--quantity-gap, 16px);

  /* Single item - full width */
  &:has(> :nth-child(1):nth-last-child(1)) > * {
    flex-basis: 100%;
  }

  /* Two items - 50% each */
  &:has(> :nth-child(2):nth-last-child(1)) > * {
    flex-basis: calc(50% - var(--quantity-gap, 16px) / 2);
  }

  /* Three items - 33% each */
  &:has(> :nth-child(3):nth-last-child(1)) > * {
    flex-basis: calc(33.333% - var(--quantity-gap, 16px) * 2 / 3);
  }

  /* Four or more items - 25% each */
  &:has(> :nth-child(4)) > * {
    flex-basis: calc(25% - var(--quantity-gap, 16px) * 3 / 4);
  }
}

// RESPONSIVE UTILITIES FOR PATTERNS
@media (max-width: 576px) {
  .stack-horizontal-mobile {
    flex-direction: row;
    align-items: center;
  }

  .stack-vertical-mobile {
    flex-direction: column;
    align-items: stretch;
  }

  .cluster-stack-mobile {
    flex-direction: column;
    align-items: stretch;
  }
}

@media (max-width: 768px) {
  .equal-height {
    flex-direction: column;
  }

  .switcher {
    flex-direction: column;
  }
}

// ACCESSIBILITY IMPROVEMENTS
@media (prefers-reduced-motion: reduce) {
  .sticky-footer,
  .cover,
  .imposter {
    transition: none;
  }
}

// PRINT OPTIMIZATIONS
@media print {
  .reel {
    display: block;
    overflow: visible;
  }

  .masonry {
    columns: 1;
  }

  .holy-grail {
    display: block;
  }
}