/**
 * Grid System - Resales Modern Plugin
 * 
 * Advanced grid layout system for property displays
 * - Flexible column configurations (1-6 columns)
 * - Multiple layout modes (grid, list, masonry, featured)
 * - View density options (compact, comfortable, spacious)
 * - Responsive breakpoints
 * - Auto-fit and fixed column layouts
 * 
 * @package ResalesModern
 * @version 1.1.0
 * @since 1.1.0
 * 
 * Dependencies:
 * - variables.css (CSS custom properties)
 * - base.css (Base styles)
 * - components.css (Component library)
 */

/* =========================================================================
   CSS VARIABLES - GRID SPECIFIC
   ========================================================================= */
:root {
  /* Grid Gap Options */
  --grid-gap-compact: var(--space-4);          /* 16px */
  --grid-gap-comfortable: var(--space-6);      /* 24px - default */
  --grid-gap-spacious: var(--space-8);         /* 32px */
  
  /* Grid Min Widths */
  --grid-min-width-compact: 280px;
  --grid-min-width-comfortable: 320px;         /* default */
  --grid-min-width-spacious: 360px;
  
  /* Card Heights for Masonry */
  --card-height-base: 20px;
  --card-row-span-default: 25;                 /* ~500px */
  
  /* List View Specific */
  --list-image-width: 300px;
  --list-image-width-sm: 200px;
  
  /* Featured Layout */
  --featured-main-ratio: 2;                    /* Main card takes 2x space */
}

/* =========================================================================
   BASE GRID CONTAINER
   ========================================================================= */

/* Main Grid Container */
.rm-grid {
  display: grid;
  gap: var(--grid-gap-comfortable);
  width: 100%;
  margin: 0 auto;
  position: relative;
}

/* Grid States */
.rm-grid--loading {
  opacity: 0.6;
  pointer-events: none;
}

.rm-grid--empty {
  min-height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* =========================================================================
   GRID COLUMNS - FIXED CONFIGURATIONS
   ========================================================================= */

/* 1 Column */
.rm-grid-cols-1 {
  grid-template-columns: repeat(1, 1fr);
}

/* 2 Columns */
.rm-grid-cols-2 {
  grid-template-columns: repeat(2, 1fr);
}

/* 3 Columns (Default) */
.rm-grid-cols-3 {
  grid-template-columns: repeat(3, 1fr);
}

/* 4 Columns */
.rm-grid-cols-4 {
  grid-template-columns: repeat(4, 1fr);
}

/* 5 Columns */
.rm-grid-cols-5 {
  grid-template-columns: repeat(5, 1fr);
}

/* 6 Columns */
.rm-grid-cols-6 {
  grid-template-columns: repeat(6, 1fr);
}

/* =========================================================================
   AUTO-FIT RESPONSIVE GRID
   ========================================================================= */

/* Auto-fit with minimum width */
.rm-grid-auto {
  grid-template-columns: repeat(auto-fill, minmax(var(--grid-min-width-comfortable), 1fr));
}

/* Auto-fit variations */
.rm-grid-auto-compact {
  grid-template-columns: repeat(auto-fill, minmax(var(--grid-min-width-compact), 1fr));
}

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

/* =========================================================================
   VIEW DENSITY OPTIONS
   ========================================================================= */

/* Compact View - Smaller gaps and cards */
.rm-grid-density-compact {
  gap: var(--grid-gap-compact);
}

.rm-grid-density-compact .rm-property-card {
  --property-image-height: 200px;
}

.rm-grid-density-compact .rm-property-card__content {
  padding: var(--space-4);
}

/* Comfortable View - Default */
.rm-grid-density-comfortable {
  gap: var(--grid-gap-comfortable);
}

/* Spacious View - Extra gaps and larger cards */
.rm-grid-density-spacious {
  gap: var(--grid-gap-spacious);
}

.rm-grid-density-spacious .rm-property-card {
  --property-image-height: 280px;
}

.rm-grid-density-spacious .rm-property-card__content {
  padding: var(--space-8);
}

/* =========================================================================
   LAYOUT MODE: GRID (DEFAULT)
   ========================================================================= */

/* Grid Layout - Standard card display */
.rm-layout-grid .rm-property-card {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.rm-layout-grid .rm-property-card__image-container {
  width: 100%;
  aspect-ratio: 4 / 3;
}

.rm-layout-grid .rm-property-card__content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* =========================================================================
   LAYOUT MODE: LIST VIEW
   ========================================================================= */

/* List Layout - Horizontal card display */
.rm-layout-list .rm-grid {
  grid-template-columns: 1fr;
  gap: var(--space-6);
}

.rm-layout-list .rm-property-card {
  display: grid;
  grid-template-columns: var(--list-image-width) 1fr;
  gap: var(--space-6);
  height: auto;
}

.rm-layout-list .rm-property-card__image-container {
  width: 100%;
  height: 100%;
  min-height: 280px;
}

.rm-layout-list .rm-property-card__content {
  display: flex;
  flex-direction: column;
  padding: var(--space-6);
}

.rm-layout-list .rm-property-card__badges {
  position: absolute;
  top: var(--space-4);
  left: var(--space-4);
}

.rm-layout-list .rm-property-card__actions {
  margin-top: auto;
}

/* List View - Compact Density */
.rm-layout-list.rm-grid-density-compact .rm-property-card {
  grid-template-columns: var(--list-image-width-sm) 1fr;
  gap: var(--space-4);
}

.rm-layout-list.rm-grid-density-compact .rm-property-card__image-container {
  min-height: 200px;
}

.rm-layout-list.rm-grid-density-compact .rm-property-card__content {
  padding: var(--space-4);
}

/* List View - Responsive */
@media (max-width: 767px) {
  .rm-layout-list .rm-property-card {
    grid-template-columns: 1fr;
    gap: 0;
  }
  
  .rm-layout-list .rm-property-card__image-container {
    min-height: 240px;
  }
}

/* =========================================================================
   LAYOUT MODE: MASONRY
   ========================================================================= */

/* Masonry Layout - Pinterest-style staggered grid */
.rm-layout-masonry .rm-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--grid-min-width-comfortable), 1fr));
  grid-auto-rows: var(--card-height-base);
  gap: var(--space-6);
}

.rm-layout-masonry .rm-property-card {
  grid-row-end: span var(--card-row-span, var(--card-row-span-default));
}

/* Masonry - Different card sizes based on content */
.rm-layout-masonry .rm-property-card[data-size="small"] {
  --card-row-span: 20;  /* ~400px */
}

.rm-layout-masonry .rm-property-card[data-size="medium"] {
  --card-row-span: 25;  /* ~500px */
}

.rm-layout-masonry .rm-property-card[data-size="large"] {
  --card-row-span: 30;  /* ~600px */
}

/* Masonry - Image fills card */
.rm-layout-masonry .rm-property-card__image-container {
  width: 100%;
  height: auto;
  aspect-ratio: auto;
}

.rm-layout-masonry .rm-property-card__image-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Masonry - Responsive columns */
@media (max-width: 767px) {
  .rm-layout-masonry .rm-grid {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .rm-layout-masonry .rm-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 1024px) {
  .rm-layout-masonry .rm-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 1440px) {
  .rm-layout-masonry .rm-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* =========================================================================
   LAYOUT MODE: FEATURED
   ========================================================================= */

/* Featured Layout - Large featured card + grid */
.rm-layout-featured .rm-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: var(--space-6);
}

/* Featured Card - Takes 2 columns x 2 rows */
.rm-layout-featured .rm-property-card:first-child {
  grid-column: span 3;
  grid-row: span 2;
}

/* Featured card image is taller */
.rm-layout-featured .rm-property-card:first-child .rm-property-card__image-container {
  aspect-ratio: 16 / 10;
  min-height: 400px;
}

/* Regular cards in grid */
.rm-layout-featured .rm-property-card:not(:first-child) {
  grid-column: span 3;
}

/* Featured - Responsive */
@media (max-width: 767px) {
  .rm-layout-featured .rm-grid {
    grid-template-columns: 1fr;
  }
  
  .rm-layout-featured .rm-property-card:first-child,
  .rm-layout-featured .rm-property-card:not(:first-child) {
    grid-column: span 1;
    grid-row: span 1;
  }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .rm-layout-featured .rm-grid {
    grid-template-columns: repeat(4, 1fr);
  }
  
  .rm-layout-featured .rm-property-card:first-child {
    grid-column: span 4;
    grid-row: span 1;
  }
  
  .rm-layout-featured .rm-property-card:not(:first-child) {
    grid-column: span 2;
  }
}

/* =========================================================================
   LAYOUT MODE: CAROUSEL
   ========================================================================= */

/* Carousel Layout - Horizontal scrolling */
.rm-layout-carousel .rm-grid {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  gap: var(--space-6);
  padding-bottom: var(--space-4);
}

.rm-layout-carousel .rm-property-card {
  flex: 0 0 320px;
  scroll-snap-align: start;
}

/* Carousel - Hide scrollbar */
.rm-layout-carousel .rm-grid::-webkit-scrollbar {
  height: 8px;
}

.rm-layout-carousel .rm-grid::-webkit-scrollbar-track {
  background: var(--color-bg-light);
  border-radius: var(--radius-full);
}

.rm-layout-carousel .rm-grid::-webkit-scrollbar-thumb {
  background: var(--color-border-dark);
  border-radius: var(--radius-full);
}

.rm-layout-carousel .rm-grid::-webkit-scrollbar-thumb:hover {
  background: var(--color-primary);
}

/* Carousel - Wider cards on larger screens */
@media (min-width: 768px) {
  .rm-layout-carousel .rm-property-card {
    flex: 0 0 360px;
  }
}

@media (min-width: 1024px) {
  .rm-layout-carousel .rm-property-card {
    flex: 0 0 400px;
  }
}

/* =========================================================================
   RESPONSIVE GRID BREAKPOINTS
   ========================================================================= */

/* Mobile (< 768px) - Force 1 column for most layouts */
@media (max-width: 767px) {
  .rm-grid-cols-2,
  .rm-grid-cols-3,
  .rm-grid-cols-4,
  .rm-grid-cols-5,
  .rm-grid-cols-6 {
    grid-template-columns: 1fr;
  }
  
  /* Allow 2 columns for compact density */
  .rm-grid-density-compact.rm-grid-cols-2,
  .rm-grid-density-compact.rm-grid-cols-3,
  .rm-grid-density-compact.rm-grid-cols-4 {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Tablet (768px - 1023px) */
@media (min-width: 768px) and (max-width: 1023px) {
  .rm-grid-cols-3,
  .rm-grid-cols-4,
  .rm-grid-cols-5,
  .rm-grid-cols-6 {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Desktop (1024px - 1439px) */
@media (min-width: 1024px) and (max-width: 1439px) {
  .rm-grid-cols-4,
  .rm-grid-cols-5,
  .rm-grid-cols-6 {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* Wide Desktop (1440px+) - Show more columns */
@media (min-width: 1440px) {
  .rm-grid-cols-5 {
    grid-template-columns: repeat(4, 1fr);
  }
  
  .rm-grid-cols-6 {
    grid-template-columns: repeat(5, 1fr);
  }
}

/* Extra Wide (1920px+) - Full 6 columns */
@media (min-width: 1920px) {
  .rm-grid-cols-6 {
    grid-template-columns: repeat(6, 1fr);
  }
}

/* =========================================================================
   GRID ANIMATIONS & TRANSITIONS
   ========================================================================= */

/* Layout Transition */
.rm-grid {
  transition: gap var(--duration-300) var(--ease-in-out);
}

.rm-property-card {
  transition: transform var(--duration-200) var(--ease-out),
              opacity var(--duration-200) var(--ease-in-out);
}

/* Fade in cards on load */
.rm-property-card {
  opacity: 0;
  animation: fadeInUp var(--duration-300) var(--ease-out) forwards;
}

.rm-property-card:nth-child(1) { animation-delay: 0ms; }
.rm-property-card:nth-child(2) { animation-delay: 50ms; }
.rm-property-card:nth-child(3) { animation-delay: 100ms; }
.rm-property-card:nth-child(4) { animation-delay: 150ms; }
.rm-property-card:nth-child(5) { animation-delay: 200ms; }
.rm-property-card:nth-child(6) { animation-delay: 250ms; }
.rm-property-card:nth-child(n+7) { animation-delay: 300ms; }

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Disable animations if user prefers reduced motion */
@media (prefers-reduced-motion: reduce) {
  .rm-grid,
  .rm-property-card {
    transition: none;
    animation: none;
  }
  
  .rm-property-card {
    opacity: 1;
    transform: none;
  }
}

/* =========================================================================
   GRID UTILITIES
   ========================================================================= */

/* Full width grid item */
.rm-grid-item-full {
  grid-column: 1 / -1;
}

/* Span 2 columns */
.rm-grid-item-span-2 {
  grid-column: span 2;
}

/* Span 3 columns */
.rm-grid-item-span-3 {
  grid-column: span 3;
}

/* Center single item */
.rm-grid-center-single {
  display: flex;
  justify-content: center;
}

.rm-grid-center-single > * {
  max-width: 400px;
}

/* =========================================================================
   EMPTY STATE
   ========================================================================= */

.rm-grid-empty {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-16) var(--space-4);
  text-align: center;
  color: var(--color-text-light);
}

.rm-grid-empty__icon {
  width: 80px;
  height: 80px;
  margin-bottom: var(--space-6);
  color: var(--color-border);
}

.rm-grid-empty__title {
  font-family: var(--font-primary);
  font-size: var(--text-2xl);
  font-weight: var(--font-semibold);
  color: var(--color-text);
  margin-bottom: var(--space-3);
}

.rm-grid-empty__description {
  font-size: var(--text-base);
  color: var(--color-subtext);
  max-width: 500px;
  margin-bottom: var(--space-6);
}

.rm-grid-empty__action {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-6);
  font-weight: var(--font-semibold);
  color: var(--color-primary);
  background: var(--color-accent);
  border-radius: var(--radius-full);
  text-decoration: none;
  transition: var(--transition-colors);
}

.rm-grid-empty__action:hover {
  background: var(--color-accent-hover);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

/* =========================================================================
   LOADING STATE
   ========================================================================= */

.rm-grid-loading {
  position: relative;
  min-height: 400px;
}

.rm-grid-loading::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  border: 4px solid var(--color-border-light);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* =========================================================================
   ACCESSIBILITY
   ========================================================================= */

/* Screen reader only text for grid info */
.rm-grid-sr-info {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* Focus visible for grid items */
.rm-grid:focus-visible {
  outline: var(--border-width-2) solid var(--color-primary);
  outline-offset: var(--focus-ring-offset);
}

/* =========================================================================
   PRINT STYLES
   ========================================================================= */

@media print {
  .rm-grid {
    display: block;
  }
  
  .rm-property-card {
    display: block;
    page-break-inside: avoid;
    margin-bottom: var(--space-6);
  }
  
  .rm-layout-carousel .rm-grid {
    display: block;
    overflow: visible;
  }
}
