/* Adjustments for Game Landscape Mode (Targeting 800x450 and 800x600) */
@media screen and (max-height: 640px) and (orientation: landscape) {
  #gameScreen {
    display: grid;
    grid-template-columns: 240px 1fr;
    grid-template-rows: auto 1fr;
    gap: 12px 24px;
    padding: 12px 24px;
    align-content: center;
    justify-content: center;
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
    flex-direction: initial;
  }

  /* Left Sidebar: Header Area */
  .game-top-bar {
    grid-column: 1;
    grid-row: 1;
    flex-direction: row;
    width: 100%;
    margin-bottom: 0;
    padding: 0;
    justify-content: space-between;
  }

  .brand-icon {
    font-size: 1.5rem;
  }
  .brand-text {
    font-size: 1.2rem;
  }

  /* Left Sidebar: Stats Area */
  .stats-bar {
    grid-column: 1;
    grid-row: 2;
    flex-direction: column;
    width: 100%;
    max-width: none;
    padding: 12px;
    gap: 12px;
    margin-bottom: 0;
    align-self: start;
  }

  .info-item {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 0 4px;
  }

  .info-item .label {
    margin-bottom: 0;
    font-size: 0.8rem;
  }

  /* Right Side: Game Board */
  .game-wrapper {
    grid-column: 2;
    grid-row: 1 / span 2;
    width: 100%;
    height: 100%;
    /* max-height removed to allow filling 600px screens */
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden; /* Ensure no spillover */
  }

  /* Fix Cards visibility and size */
  .cards {
    height: auto;
    width: auto;
    /* Constrain by height first to fit screen, let width adjust */
    max-height: 100%;
    max-width: 100%;
    aspect-ratio: auto;
  }

  .cards {
    gap: 8px;
  } /* Tighter gap for better fit */

  /* Optimize Medium Level: 6 cols x 4 rows */
  .cards.grid-4x6 {
    grid-template-columns: repeat(6, 1fr) !important;
    grid-template-rows: repeat(4, 1fr) !important;
  }

  /* Optimize Hard Level: 6 cols x 6 rows (tight fit) */
  .cards.grid-6x6 {
    grid-template-columns: repeat(6, 1fr) !important;
    grid-template-rows: repeat(6, 1fr) !important;
    gap: 6px !important;
  }

  /* Dynamically size cards based on available space instead of fixed 50px */
  .cards.grid-6x6 .card {
    width: auto !important;
    height: auto !important;
    max-height: 14vh; /* Backup constraint */
  }
}

/* --- Comprehensive Fixes for Other Screens (800x450 & 800x600) --- */
@media screen and (max-height: 640px) and (orientation: landscape) {
  /* 1. Global Screen Scaling */
  .screen {
    padding: 8px;
    overflow-y: auto;
  }

  /* 2. Glass Panel Adjustments */
  .glass-panel {
    padding: 16px 20px;
    max-width: 650px;
    gap: 10px;
    margin: 0 auto;
  }

  /* 3. Typography Scaling */
  h1 {
    font-size: 2rem;
    margin-bottom: 4px;
  }
  h2 {
    font-size: 1.5rem;
    margin-bottom: 8px;
  }
  h3 {
    font-size: 1.1rem;
  }
  p {
    font-size: 0.9rem;
    line-height: 1.3;
  }

  /* 4. Home Menu - Side by Side Layout */
  #homeScreen .menu-container {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 40px;
    max-width: 700px;
  }

  .game-title {
    margin-bottom: 0;
    text-align: center; /* Center align text and logo */
    display: flex;
    flex-direction: column;
    align-items: center; /* Center flex items horizontally */
    justify-content: center;
    flex: 1;
  }

  .menu-buttons {
    gap: 10px;
    width: 220px; /* Fixed width for buttons column */
    flex: 0 0 auto;
  }

  .menu-btn {
    padding: 10px 16px;
    font-size: 1rem;
  }

  /* 5. Level Selection - Horizontal Layout */
  .level-list {
    flex-direction: row;
    gap: 12px;
    margin-bottom: 12px;
    width: 100%;
    justify-content: center;
  }

  .level-container {
    width: 100%;
    max-width: 650px;
  }

  .level-item {
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 12px;
    flex: 1;
    min-width: 0;
  }

  .level-icon-wrapper {
    margin-right: 0;
    margin-bottom: 8px;
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
  }

  .level-meta {
    justify-content: center;
    font-size: 0.75rem;
    flex-wrap: wrap;
  }

  .play-indicator {
    display: none;
  }

  /* 6. Settings Screen & How To Play - Compact Content */
  .settings-options {
    flex-direction: row;
    gap: 24px;
    justify-content: center;
    width: 100%;
    margin-bottom: 12px;
  }

  .setting-item {
    flex-direction: column;
    align-items: center;
    gap: 8px;
  }

  /* How to Play Grid */
  .instructions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    text-align: left;
  }

  .instruction-step {
    margin-bottom: 0;
    padding: 8px 12px;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 8px;
  }

  .instruction-step h3 {
    font-size: 1rem;
    margin-bottom: 2px;
  }
  .instruction-step p {
    font-size: 0.8rem;
    margin: 0;
  }
  .step-number {
    width: 20px;
    height: 20px;
    font-size: 0.8rem;
    line-height: 20px;
  }

  /* 7. Game Over Screen - Horizontal Stats */
  .game-over-content {
    display: grid;
    grid-template-columns: 1fr 1.2fr;
    grid-template-rows: auto auto;
    gap: 16px;
    align-items: center;
    width: 100%;
    max-width: 700px;
    margin-top: -20px;
  }

  .celebration-wrapper {
    display: none;
  }

  .game-over-header-group {
    grid-column: 1;
    grid-row: 1;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  #gameOverTitle {
    font-size: 1.8rem;
    margin: 0;
    line-height: 1.2;
  }
  #gameOverMessage {
    margin-bottom: 0;
    margin-top: 4px;
  }

  /* Right Side Stats Grid */
  .final-stats {
    grid-column: 2;
    grid-row: 1;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    width: 100%;
    margin: 0;
  }

  .stat-card {
    padding: 6px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    gap: 8px;
  }

  .stat-icon {
    font-size: 1.1rem;
  }
  .stat-label {
    font-size: 0.6rem;
  }
  .stat-value {
    font-size: 1rem;
  }

  /* Buttons at Bottom Spanning Full Width */
  .game-over-buttons {
    grid-column: 1 / span 2;
    grid-row: 2;
    flex-direction: row;
    justify-content: center;
    gap: 16px;
    margin-top: 8px;
    width: 100%;
  }

  /* 8. Pause Screen */
  .pause-container {
    max-width: 400px;
    width: 100%;
  }

  .pause-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    width: 100%;
  }

  /* 9. Back Buttons */
  .back-btn {
    margin-top: 8px;
    padding: 8px 16px;
  }
}
