/**
 * Magical Grid System - Scroll-Safe Layout Foundation
 * Part of LEGENDARY UI Design System v2.0
 * Built for Aria's Educational Games Platform
 */

:root {
  /* Grid System Tokens */
  --grid-columns: 12;
  --grid-gap: var(--space-4);
  --grid-gap-mobile: var(--space-3);
  --grid-container-max: var(--container-game, 1200px);
  
  /* Layout Heights - Viewport-based for scroll prevention */
  --viewport-safe-height: calc(100vh - var(--space-16));
  --content-max-height: calc(100vh - var(--space-20));
  --game-screen-height: calc(100vh - var(--space-12));
  --header-height: var(--nav-height, 60px);
  --footer-height: 80px;
  
  /* Game Layout Breakpoints */
  --bp-mobile: 480px;
  --bp-tablet: 768px;
  --bp-desktop: 1024px;
  --bp-large: 1200px;
}

/* ================================
   CORE GRID FOUNDATION
   ================================ */

.grid-container {
  width: 100%;
  max-width: var(--grid-container-max);
  margin: 0 auto;
  padding: 0 var(--space-4);
  
  /* Prevent horizontal overflow */
  overflow-x: hidden;
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-columns), 1fr);
  gap: var(--grid-gap);
  width: 100%;
  
  /* Ensure grid doesn't cause vertical overflow */
  align-content: start;
}

.grid-flex {
  display: flex;
  flex-wrap: wrap;
  gap: var(--grid-gap);
  width: 100%;
}

/* ================================
   VIEWPORT-SAFE LAYOUTS
   ================================ */

.layout-fullscreen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  
  /* Prevent bounce scrolling on iOS */
  -webkit-overflow-scrolling: touch;
}

.layout-game-screen {
  min-height: var(--game-screen-height);
  max-height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  
  /* Smooth scrolling behavior */
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--color-magical-purple) transparent;
}

.layout-game-screen::-webkit-scrollbar {
  width: 8px;
}

.layout-game-screen::-webkit-scrollbar-track {
  background: transparent;
}

.layout-game-screen::-webkit-scrollbar-thumb {
  background: var(--gradient-magical);
  border-radius: 4px;
}

.layout-content-safe {
  max-height: var(--content-max-height);
  overflow-y: auto;
  padding: var(--space-4);
}

/* ================================
   MAGICAL GAME LAYOUTS
   ================================ */

/* Main Menu Layout */
.layout-main-menu {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: var(--viewport-safe-height);
  gap: var(--space-6);
  padding: var(--space-4);
  align-items: center;
  text-align: center;
}

.menu-header {
  grid-row: 1;
  padding: var(--space-4) 0;
}

.menu-content {
  grid-row: 2;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-6);
  align-content: center;
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
}

.menu-footer {
  grid-row: 3;
  padding: var(--space-2) 0;
}

/* Game Play Layout */
.layout-game-play {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: var(--game-screen-height);
  gap: var(--space-4);
}

.game-header {
  background: var(--bg-glass);
  backdrop-filter: blur(10px);
  padding: var(--space-4);
  border-radius: var(--radius-lg);
}

.game-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: var(--space-6);
  min-height: 0; /* Allow flex shrinking */
}

.game-controls {
  background: var(--bg-glass);
  backdrop-filter: blur(10px);
  padding: var(--space-4);
  border-radius: var(--radius-lg);
  display: flex;
  justify-content: center;
  gap: var(--space-4);
}

/* Dashboard Layout */
.layout-dashboard {
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-template-rows: auto 1fr;
  gap: var(--space-6);
  min-height: var(--viewport-safe-height);
  padding: var(--space-4);
}

.dashboard-sidebar {
  grid-column: 1;
  grid-row: 1 / -1;
  background: var(--bg-card);
  border-radius: var(--radius-xl);
  padding: var(--space-6);
  
  /* Prevent sidebar from becoming too tall */
  max-height: var(--content-max-height);
  overflow-y: auto;
}

.dashboard-header {
  grid-column: 2;
  grid-row: 1;
}

.dashboard-main {
  grid-column: 2;
  grid-row: 2;
  min-height: 0; /* Allow content to shrink */
  overflow-y: auto;
}

/* ================================
   RESPONSIVE GRID HELPERS
   ================================ */

/* Column Spans */
.col-1 { grid-column: span 1; }
.col-2 { grid-column: span 2; }
.col-3 { grid-column: span 3; }
.col-4 { grid-column: span 4; }
.col-5 { grid-column: span 5; }
.col-6 { grid-column: span 6; }
.col-7 { grid-column: span 7; }
.col-8 { grid-column: span 8; }
.col-9 { grid-column: span 9; }
.col-10 { grid-column: span 10; }
.col-11 { grid-column: span 11; }
.col-12 { grid-column: span 12; }

/* Flex Column Classes */
.flex-1 { flex: 1; }
.flex-2 { flex: 2; }
.flex-3 { flex: 3; }
.flex-none { flex: none; }
.flex-auto { flex: auto; }

/* Gap Utilities */
.gap-0 { gap: 0; }
.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.gap-6 { gap: var(--space-6); }
.gap-8 { gap: var(--space-8); }

/* ================================
   GAME-SPECIFIC LAYOUTS
   ================================ */

/* Math Star Layouts */
.layout-math-problem {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-6);
  min-height: 60vh;
  padding: var(--space-6);
  text-align: center;
}

.math-problem-display {
  font-size: clamp(2rem, 5vw, 4rem);
  font-weight: var(--weight-bold);
  color: var(--color-wonder-teal);
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.math-answer-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-4);
  width: 100%;
  max-width: 400px;
}

/* Spell Quest Layouts */
.layout-spelling-game {
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: var(--space-4);
  min-height: var(--game-screen-height);
  padding: var(--space-4);
}

.spell-word-display {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 200px;
  font-size: clamp(1.5rem, 4vw, 3rem);
  font-weight: var(--weight-bold);
  color: var(--color-magic-purple);
  text-align: center;
}

.spell-input-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-6);
}

/* Science Lab Layouts */
.layout-lab-experiment {
  display: grid;
  grid-template-columns: 1fr 2fr;
  grid-template-rows: auto 1fr;
  gap: var(--space-6);
  min-height: var(--game-screen-height);
  padding: var(--space-4);
}

.lab-tools {
  grid-column: 1;
  grid-row: 1 / -1;
  background: var(--bg-card);
  border-radius: var(--radius-xl);
  padding: var(--space-4);
  max-height: var(--content-max-height);
  overflow-y: auto;
}

.lab-workspace {
  grid-column: 2;
  grid-row: 1 / -1;
  background: var(--bg-surface);
  border-radius: var(--radius-xl);
  padding: var(--space-6);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

/* Art Corner Layouts */
.layout-art-studio {
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: var(--space-4);
  min-height: var(--game-screen-height);
}

.art-toolbar {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  padding: var(--space-4);
  background: var(--bg-glass);
  backdrop-filter: blur(10px);
  border-radius: var(--radius-lg);
}

.art-canvas-area {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: var(--space-4);
  min-height: 0;
}

.art-palette {
  display: flex;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-4);
  background: var(--bg-glass);
  backdrop-filter: blur(10px);
  border-radius: var(--radius-lg);
}

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

/* Mobile Layout Adjustments */
@media (max-width: 480px) {
  .grid-container {
    padding: 0 var(--space-3);
  }
  
  .grid {
    gap: var(--grid-gap-mobile);
  }
  
  .layout-dashboard {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto 1fr;
  }
  
  .dashboard-sidebar {
    grid-column: 1;
    grid-row: 2;
    max-height: 300px;
  }
  
  .dashboard-main {
    grid-column: 1;
    grid-row: 3;
  }
  
  .layout-lab-experiment {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  
  .lab-tools {
    grid-column: 1;
    grid-row: 1;
    max-height: 200px;
  }
  
  .lab-workspace {
    grid-column: 1;
    grid-row: 2;
  }
  
  /* Stack all columns on mobile */
  .col-1, .col-2, .col-3, .col-4, .col-5, .col-6,
  .col-7, .col-8, .col-9, .col-10, .col-11, .col-12 {
    grid-column: 1 / -1;
  }
  
  /* Reduce menu content columns */
  .menu-content {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: var(--space-4);
  }
}

/* Tablet Layout Adjustments */
@media (min-width: 481px) and (max-width: 768px) {
  .layout-dashboard {
    grid-template-columns: 200px 1fr;
  }
  
  .menu-content {
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  }
  
  /* Tablet column adjustments */
  .col-9, .col-10, .col-11, .col-12 {
    grid-column: 1 / -1;
  }
}

/* Desktop Optimizations */
@media (min-width: 1024px) {
  .layout-main-menu {
    gap: var(--space-8);
    padding: var(--space-8);
  }
  
  .menu-content {
    max-width: 1000px;
    gap: var(--space-8);
  }
  
  .game-content {
    padding: var(--space-8);
  }
}

/* ================================
   UTILITY CLASSES
   ================================ */

/* Height Utilities */
.h-screen { height: 100vh; }
.h-screen-safe { height: var(--viewport-safe-height); }
.h-game { height: var(--game-screen-height); }
.h-auto { height: auto; }
.h-full { height: 100%; }

.min-h-screen { min-height: 100vh; }
.min-h-screen-safe { min-height: var(--viewport-safe-height); }
.min-h-game { min-height: var(--game-screen-height); }

.max-h-content { max-height: var(--content-max-height); }
.max-h-screen { max-height: 100vh; }

/* Overflow Utilities */
.overflow-hidden { overflow: hidden; }
.overflow-auto { overflow: auto; }
.overflow-scroll { overflow: scroll; }
.overflow-x-hidden { overflow-x: hidden; }
.overflow-y-auto { overflow-y: auto; }

/* Position Utilities */
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }
.sticky { position: sticky; }

/* Flex Utilities */
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-row { flex-direction: row; }
.flex-wrap { flex-wrap: wrap; }
.flex-nowrap { flex-wrap: nowrap; }

.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.justify-around { justify-content: space-around; }
.justify-start { justify-content: flex-start; }
.justify-end { justify-content: flex-end; }

.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.items-end { align-items: flex-end; }
.items-stretch { align-items: stretch; }

/* Text Alignment */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }

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

/* High contrast mode support */
@media (prefers-contrast: high) {
  .grid,
  .grid-flex,
  .layout-game-screen {
    border: 2px solid black;
  }
  
  .dashboard-sidebar,
  .lab-tools,
  .art-toolbar,
  .art-palette {
    background: white;
    border: 2px solid black;
  }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  .layout-game-screen {
    scroll-behavior: auto;
  }
}

/* Print styles */
@media print {
  .layout-fullscreen,
  .layout-game-screen {
    min-height: auto;
    max-height: none;
    overflow: visible;
  }
  
  .game-controls,
  .art-toolbar,
  .art-palette {
    display: none;
  }
}

/* ================================
   SCROLL PREVENTION PATTERNS
   ================================ */

/* Prevent body scroll when game is active */
.game-active {
  overflow: hidden;
}

.game-active .layout-game-screen {
  overflow-y: auto;
}

/* Ensure content never exceeds viewport */
.content-contained {
  max-height: calc(100vh - var(--header-height) - var(--footer-height));
  overflow-y: auto;
}

/* Focus trap for keyboard navigation */
.focus-trap {
  position: relative;
}

.focus-trap::before,
.focus-trap::after {
  content: '';
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* Screen reader friendly grid announcements */
.grid[role="grid"] {
  outline: none;
}

.grid[role="grid"]:focus {
  outline: 3px solid var(--color-wonder-teal);
  outline-offset: 2px;
}