/* ========================================
   MOBILE-OPTIMIZED LAYOUT
   ======================================== */

/* Base mobile overrides */
@media (max-width: 768px) {
  /* Safe area handling for notches */
  body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* Hide desktop-only elements */
  .topbar {
    padding: 12px 16px;
    flex-wrap: wrap;
  }

  .logo {
    font-size: clamp(32px, 8vw, 48px) !important;
  }

  /* Welcome/Landing screens - make them scrollable */
  .welcome-grid {
    grid-template-columns: 1fr;
    gap: 16px;
    padding: 16px;
  }

  .hero {
    min-height: auto;
    padding: 24px;
  }

  .side-panel {
    min-height: auto;
    max-width: 100%;
  }

  /* Menu cards - stack on mobile */
  .menu-grid {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  .solo-mode-grid,
  .multi-mode-grid {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  /* Feature grid - single column */
  .hero > div[style*="grid-template-columns"] {
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
  }

  /* ========================================
     GAME SCREEN - MOBILE LAYOUT
     ======================================== */

  .game-layout {
    display: flex !important;
    flex-direction: column !important;
    grid-template-columns: 1fr !important;
    gap: 0 !important;
    padding: 0 !important;
    height: 100vh;
    max-height: 100dvh;
    overflow: hidden;
  }

  /* Hide side panels by default on mobile */
  .game-side {
    display: none;
  }

  /* Main game area takes full screen */
  .game-mid {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 8px;
    background: transparent;
    border: none;
    border-radius: 0;
    overflow: hidden;
    min-height: 0;
  }

  /* Panel title - very compact */
  .game-mid .panel-title {
    margin-bottom: 6px;
    flex-wrap: wrap;
    gap: 6px;
    padding: 4px 8px;
  }

  .game-mid .panel-title .title {
    font-size: 18px !important;
  }

  .game-mid .panel-title .subtitle {
    font-size: 12px;
    display: none; /* Hide subtitle to save space */
  }

  /* Action buttons - smaller and wrap */
  .panel-title > div:last-child {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  .panel-title .button.small {
    padding: 8px 12px;
    font-size: 11px;
  }

  /* ========================================
     BOARD & TILES - MOBILE OPTIMIZED
     ======================================== */

  .board-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 8px;
    min-height: 0;
  }

  /* Word display - compact */
  .word-box {
    position: relative;
    z-index: 15;
    padding: 8px 12px;
    margin: 0 0 4px 0;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.95);
    backdrop-filter: blur(16px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
  }

  .word-current {
    font-size: clamp(20px, 4.5vw, 28px) !important;
    min-height: 32px !important;
    letter-spacing: 0.08em;
  }

  /* Grid shell - centered and responsive */
  .grid-shell {
    position: relative;
    padding: 8px;
    border-radius: 14px;
    background: rgba(0, 0, 0, 0.3);
    margin: 0 auto;
    width: 100%;
    max-width: 400px;
  }

  /* Responsive tile sizing - slightly smaller */
  :root {
    --tile-size: min(16vw, 64px);
    --tile-gap: min(1.8vw, 7px);
    --tile-font: clamp(18px, 4.2vw, 30px);
  }

  .grid {
    display: grid;
    grid-template-columns: repeat(5, var(--tile-size)) !important;
    gap: var(--tile-gap) !important;
    justify-content: center;
  }

  .tile {
    width: var(--tile-size);
    height: var(--tile-size);
    font-size: var(--tile-font) !important;
    border-radius: 12px;
    border-width: 2px;
  }

  /* Depth tiles (for Pileup mode) */
  .depth-tile {
    width: var(--tile-size);
    height: var(--tile-size);
  }

  .depth-letter {
    font-size: var(--tile-font) !important;
  }

  .depth-ghost {
    font-size: clamp(12px, 3vw, 16px) !important;
  }

  /* Floating score animation */
  .floating-score {
    font-size: clamp(24px, 6vw, 36px) !important;
  }

  /* ========================================
     ACTION BUTTONS - FIXED BOTTOM
     ======================================== */

  .action-row {
    position: relative;
    bottom: 0;
    left: 0;
    right: 0;
    display: grid;
    grid-template-columns: 1fr 1fr 2fr;
    gap: 6px;
    padding: 8px;
    background: linear-gradient(180deg, transparent, rgba(0, 0, 0, 0.95) 20%);
    backdrop-filter: blur(8px);
    z-index: 10;
    margin-top: auto;
  }

  .action-row .button {
    padding: 12px 8px !important;
    font-size: 12px !important;
    border-radius: 10px;
    white-space: nowrap;
  }

  .action-row .button .btn-label {
    display: block;
  }

  .action-row .button .hotkey-hint {
    display: none; /* Hide keyboard hints on mobile */
  }

  .action-row .button.primary {
    font-size: 14px !important;
    font-weight: 800;
  }

  /* ========================================
     STATS DRAWER - SWIPEABLE
     ======================================== */

  .stats-drawer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 50vh;
    background: linear-gradient(180deg, rgba(10, 10, 10, 0.98), rgba(10, 10, 10, 1));
    border-top: 1px solid rgba(0, 255, 255, 0.2);
    border-radius: 16px 16px 0 0;
    padding: 20px 16px;
    transform: translateY(calc(100% - 60px));
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 20;
    box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.6);
  }

  .stats-drawer.expanded {
    transform: translateY(0);
  }

  .stats-drawer-handle {
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
    cursor: grab;
  }

  .stats-drawer-handle:active {
    cursor: grabbing;
  }

  .stats-drawer-peek {
    display: flex;
    align-items: center;
    justify-content: space-around;
    padding-top: 12px;
  }

  .stats-drawer-peek-stat {
    text-align: center;
  }

  .stats-drawer-peek-label {
    font-size: 10px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }

  .stats-drawer-peek-value {
    font-size: 18px;
    font-weight: 800;
    color: var(--neon-cyan);
    margin-top: 2px;
  }

  .stats-drawer-content {
    margin-top: 16px;
    max-height: calc(50vh - 100px);
    overflow-y: auto;
  }

  /* Timer box in drawer */
  .stats-drawer .timer-box {
    margin-bottom: 16px;
  }

  /* Status strip in drawer */
  .stats-drawer .status-strip {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .stats-drawer .stat {
    min-height: 70px;
    padding: 12px;
  }

  /* ========================================
     START OVERLAY - MOBILE OPTIMIZED
     ======================================== */

  .start-overlay {
    padding: 24px;
  }

  .start-overlay .title {
    font-size: clamp(28px, 7vw, 40px) !important;
  }

  .overlay-note {
    font-size: 14px;
    text-align: center;
    max-width: 300px;
  }

  .countdown {
    font-size: clamp(56px, 15vw, 88px) !important;
  }

  /* ========================================
     END MODAL - MOBILE OPTIMIZED
     ======================================== */

  .end-modal {
    padding: 12px;
  }

  .end-card {
    width: 100%;
    max-height: calc(100dvh - 24px);
    padding: 16px;
  }

  .end-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .end-player-card {
    padding: 16px;
  }

  .result-score {
    font-size: 42px !important;
  }

  /* ========================================
     LEADERBOARDS - MOBILE OPTIMIZED
     ======================================== */

  .lb-wrap {
    padding: 12px;
  }

  .lb-mode-grid {
    grid-template-columns: 1fr;
  }

  .lb-row {
    grid-template-columns: 30px 1fr 80px 60px;
    gap: 8px;
    padding: 10px 12px;
    font-size: 13px;
  }

  .lb-rank {
    font-size: 16px;
  }

  .lb-score {
    font-size: 16px;
  }

  .lb-words {
    font-size: 11px;
  }

  /* ========================================
     PROFILE & CUSTOMIZATION
     ======================================== */

  .profile {
    padding: 8px 10px;
    gap: 10px;
  }

  .avatar {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  .rating strong {
    font-size: 16px;
  }

  .customize-modal {
    padding: 16px !important;
  }

  .customize-layout {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .customize-preview {
    order: 2; /* Put preview below options on mobile */
  }

  .customize-options {
    order: 1;
  }

  .choice-grid {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)) !important;
  }

  /* ========================================
     MULTIPLAYER SCREENS
     ======================================== */

  .vs-grid {
    gap: 10px;
  }

  .vs-score {
    font-size: 28px !important;
  }

  .code-box {
    font-size: clamp(32px, 8vw, 48px);
    padding: 16px;
  }

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

  .hide-mobile {
    display: none !important;
  }

  .show-mobile {
    display: block !important;
  }
}

/* Very small screens (< 360px) */
@media (max-width: 360px) {
  :root {
    --tile-size: min(16vw, 58px);
    --tile-gap: min(1.5vw, 6px);
    --tile-font: clamp(18px, 4vw, 26px);
  }

  .word-current {
    font-size: 20px !important;
  }

  .action-row {
    padding: 8px;
    gap: 6px;
  }

  .action-row .button {
    padding: 12px 6px !important;
    font-size: 12px !important;
  }
}

/* Landscape mode on mobile */
@media (max-width: 768px) and (orientation: landscape) {
  .game-mid {
    padding: 8px;
  }

  .board-wrap {
    flex-direction: row;
    align-items: center;
  }

  .grid-shell {
    max-width: 50%;
  }

  .word-box {
    position: relative;
    margin-bottom: 0;
    margin-right: 12px;
  }

  .action-row {
    position: fixed;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    width: auto;
    flex-direction: column;
    background: rgba(0, 0, 0, 0.9);
    border-radius: 12px 0 0 12px;
    grid-template-columns: 1fr;
  }
}

/* Tablet (768px - 1024px) - Hybrid layout */
@media (min-width: 768px) and (max-width: 1024px) {
  .game-layout {
    grid-template-columns: 280px 1fr !important;
    gap: 16px;
  }

  .game-side:first-child {
    display: block;
  }

  .game-side:last-child {
    display: none;
  }

  :root {
    --tile-size: min(14vw, 80px);
    --tile-gap: 10px;
  }
}
