/* WordTres - Mobile Specific Styles */
@supports (-webkit-touch-callout: none) {
    /* iOS specific styles */
    :root {
        --safe-area-inset-top: env(safe-area-inset-top, 0px);
        --safe-area-inset-right: env(safe-area-inset-right, 0px);
        --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
        --safe-area-inset-left: env(safe-area-inset-left, 0px);
    }
}

/* Reset and base styles */
* {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    position: fixed;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    touch-action: manipulation;
    background-color: #1a1a1a;
    
    /* Enhanced safe area support */
    height: 100vh;
    height: -webkit-fill-available;
    min-height: 100vh;
    min-height: -webkit-fill-available;
}

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
    
    /* Prevent zoom and bounce */
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

/* Game Container */
.game-container {
    width: 100%;
    max-width: 100%;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: -webkit-fill-available; /* For iOS Safari */
    min-height: 100vh;
    justify-content: flex-start;
    overflow: hidden;
}

/* Header adjustments */
.game-header {
    padding: 8px 15px;
    flex-shrink: 0;
}

/* Adjust score and level display */
#score, #level, #wordDisplay {
    font-size: 0.9em;
    margin: 4px 0;
}

/* Canvas Styling */
#gameCanvas {
    display: block;
    width: 100%;
    height: auto;
    margin: 0 auto;
    touch-action: none;
    background: #2a2a2a;
    flex-shrink: 1;
    
    /* Dynamic sizing for mobile */
    max-width: 100vw;
    max-height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 200px);
    
    /* Prevent context menu and selection */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    
    /* Crisp rendering on mobile */
    image-rendering: -webkit-optimize-contrast;
    image-rendering: -webkit-crisp-edges;
    image-rendering: pixelated;
}

/* Ensure game controls are visible on mobile */
.game-controls {
    display: flex !important;
    width: 100%;
    flex-shrink: 0;
    z-index: 100;
}

/* Show all controls on mobile */
@media (hover: none) and (pointer: coarse), (max-width: 768px) {
    .game-controls {
        display: flex !important;
        position: sticky;
        bottom: 0;
        background: rgba(102, 126, 234, 0.1);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        border-top: 2px solid rgba(255, 255, 255, 0.2);
        padding: 10px;
        margin: 0;
        z-index: 100;
    }
    
    .all-controls {
        display: flex !important;
        width: 100%;
        gap: 8px;
        justify-content: space-between;
    }
    
    .control-btn {
        display: flex !important;
        flex: 1;
        padding: 12px 8px;
        font-size: 0.9rem;
        min-height: 50px;
        border-radius: 8px;
        font-weight: bold;
        text-align: center;
        justify-content: center;
        align-items: center;
    }
}

/* Mobile Portrait - Traditional media query as fallback */
@media screen and (max-width: 768px) {
    .game-container {
        padding: 5px;
        max-width: 100vw;
        height: 100vh;
        height: -webkit-fill-available;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }
    
    .game-main {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
        padding: 5px 0;
        overflow: hidden;
    }
    
    .game-controls {
        flex-shrink: 0;
        width: 100%;
        padding: 8px 5px;
        margin-top: auto;
        background: rgba(0, 0, 0, 0.1);
        border-top: 1px solid rgba(255, 255, 255, 0.1);
    }

    .game-header h1 {
        font-size: 1.5rem;
    }

    #gameCanvas {
        width: calc(100vw - 20px);
        max-width: 350px;
        height: auto;
        max-height: calc(100vh - 280px); /* Leave space for header, cube preview, info, and controls */
        flex-shrink: 1;
    }
    
    .falling-cube-preview {
        padding: 8px 0;
        flex-shrink: 0;
    }
    
    .game-info {
        min-height: 50px;
        padding: 8px;
        flex-shrink: 0;
        margin-bottom: 5px;
    }

    .control-btn {
        padding: 10px 6px;
        font-size: 0.85rem;
        min-height: 45px;
        border-radius: 6px;
        font-weight: bold;
    }

    .all-controls {
        gap: 8px;
    }

    .game-info {
        min-height: 60px;
        padding: 10px;
    }
    
    /* Game over buttons on mobile */
    .tutorial-controls {
        flex-direction: column;
        gap: 10px;
    }
    
    .start-btn, .leaderboard-btn {
        padding: 12px 20px;
        font-size: 1rem;
        min-width: auto;
        width: 100%;
    }
}

/* Extra small mobile screens */
@media screen and (max-width: 480px) {
    .game-container {
        padding: 3px;
    }
    
    .game-header {
        padding: 5px 10px;
    }
    
    .falling-cube-preview {
        padding: 5px 0;
    }
    
    #gameCanvas {
        max-height: calc(100vh - 260px); /* Even less space for very small screens */
        width: calc(100vw - 16px);
    }
    
    .control-btn {
        padding: 8px 4px;
        font-size: 0.8rem;
        min-height: 40px;
    }
    
    .all-controls {
        gap: 6px;
    }
    
    .game-controls {
        padding: 6px 3px;
    }
    
    .game-info {
        min-height: 45px;
        padding: 6px;
        font-size: 0.9rem;
    }
}

/* Mobile Landscape */
@media screen and (max-height: 500px) and (orientation: landscape) {
    .game-container {
        flex-direction: row;
        max-width: 100vw;
        height: 100vh;
    }

    .game-main {
        flex: 1;
        padding: 10px;
    }

    .game-header {
        writing-mode: vertical-lr;
        padding: 0 10px;
        border-bottom: none;
        border-right: 2px solid rgba(255, 255, 255, 0.3);
        min-width: 120px;
    }

    .game-controls {
        flex-direction: column;
        width: auto;
        max-width: none;
        margin-top: 10px;
        margin-left: 10px;
        gap: 5px;
    }

    .control-btn {
        writing-mode: horizontal-tb;
        min-width: 80px;
        padding: 12px;
        font-size: 0.9rem;
        min-height: 50px;
    }

    #gameCanvas {
        width: auto;
        height: calc(100vh - 20px);
        max-height: 400px;
    }

    /* Landscape mobile controls */
    .mobile-controls {
        flex-direction: row !important;
        gap: 10px;
        padding: 10px;
    }

    .movement-controls, .action-controls {
        flex-direction: column;
        gap: 8px;
    }

    .move-btn, .action-btn {
        padding: 15px 18px;
        min-width: 70px;
        min-height: 55px;
        font-size: 1rem;
    }
}

/* Touch-specific improvements */
.control-btn, .move-btn, .action-btn {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    cursor: pointer;
    transition: all 0.1s ease;
}

/* Ensure canvas is properly sized on iOS */
#gameCanvas {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    user-select: none;
}

/* iPhone-specific optimizations */
@supports (-webkit-touch-callout: none) {
    /* iOS specific styles */
    .mobile-controls {
        /* Ensure controls are above the keyboard area */
        padding-bottom: max(15px, env(safe-area-inset-bottom)) !important;
    }

    .move-btn, .action-btn {
        /* Better touch targets for iOS */
        min-height: 70px !important;
        border-width: 3px !important;
    }
}

/* Prevent text selection and zoom on double tap */
* {
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

/* Improve button visibility */
.move-btn, .action-btn {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

/* High DPI display support */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    #gameCanvas {
        image-rendering: -webkit-crisp-edges;
        image-rendering: pixelated;
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    .game-over-content {
        background: #333;
        color: white;
    }
    
    .game-over h2 {
        color: #667eea;
    }
}

/* Dark mode styles */
@media (prefers-color-scheme: dark) {
    .game-container {
        background: #1a1a1a;
        color: #ffffff;
    }
    
    .control-btn {
        background: #333;
        color: #fff;
        border: 2px solid #555;
    }
    
    .control-btn:hover, .control-btn:active {
        background: #555;
        border-color: #777;
    }
}

/* Reduce motion for accessibility */
@media (prefers-reduced-motion: reduce) {
    .control-btn {
        transition: none;
    }
    
    .control-btn:hover,
    .control-btn:active {
        transform: none;
    }
}

/* Mobile-specific leaderboard styles */
@media (max-width: 600px) {
    .modal {
        padding: 10px;
    }
    
    .modal-content {
        max-width: 95vw;
        max-height: 85vh;
    }
    
    .modal-header {
        padding: 15px;
    }
    
    .modal-header h2 {
        font-size: 1.3rem;
    }
    
    .leaderboard-tabs {
        flex-direction: row;
    }
    
    .tab-btn {
        padding: 12px 8px;
        font-size: 0.9rem;
    }
    
    .leaderboard-content {
        padding: 15px;
        max-height: 300px;
    }
    
    .score-item {
        padding: 10px;
        gap: 10px;
    }
    
    .rank {
        font-size: 1rem;
        min-width: 30px;
    }
    
    .player-name {
        font-size: 0.9rem;
        gap: 6px;
    }
    
    .country-flag {
        width: 20px;
        height: 15px;
    }
    
    .player-details {
        font-size: 0.7rem;
    }
    
    .score-value {
        font-size: 1rem;
    }
    
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }
    
    .stat-number {
        font-size: 1rem;
    }
    
    .stat-label {
        font-size: 0.7rem;
    }
    
    .leaderboard-stats {
        padding: 15px;
    }
    
    /* Make close button more touch-friendly on mobile */
    .close-btn {
        min-width: 44px !important;
        min-height: 44px !important;
        font-size: 1.5rem !important;
        display: flex !important;
        align-items: center;
        justify-content: center;
        -webkit-tap-highlight-color: rgba(255, 255, 255, 0.2);
        touch-action: manipulation;
    }
    
    .close-btn:hover,
    .close-btn:active {
        background: rgba(255, 255, 255, 0.3) !important;
        transform: scale(1.1);
    }
}

/* Enhanced mobile browser fixes */
html {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    
    /* Prevent bounce and pull-to-refresh */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
}

body {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
    
    /* Prevent zoom on input focus */
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    
    /* Enhanced touch behavior */
    touch-action: manipulation;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    
    /* Prevent bounce scrolling */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
}
