/* Modern Tetris - Responsive Styles */

/* Mobile First Approach */

/* Hide desktop-only elements on mobile */
@media (max-width: 767px) {
    .desktop-only {
        display: none !important;
    }
}

/* Small Mobile Devices (320px - 480px) */
@media (max-width: 480px) {
    .app {
        padding: 5px;
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    .game-screen {
        padding: 5px;
        padding-top: 70px; /* Space for progression header on mobile too */
        padding-bottom: env(safe-area-inset-bottom);
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for mobile browsers */
    }
    
    
    .logo {
        height: 40px;
    }
    
    .logo-container {
        gap: 10px;
    }
    
    .title {
        font-size: 2rem;
        margin-bottom: 10px;
    }
    
    /* Better mobile controls hint */
    .controls-hint {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 8px;
        font-size: 0.75rem;
        padding: 12px;
        background: rgba(0, 255, 255, 0.1);
        border-radius: 8px;
        border: 1px solid rgba(0, 255, 255, 0.3);
        margin-bottom: 15px;
    }
    
    .controls-hint span {
        padding: 6px 10px;
        font-size: 0.7rem;
        background: rgba(0, 255, 255, 0.2);
        border-radius: 4px;
        text-align: center;
        flex: 1;
        min-width: 120px;
    }
    
    /* Header controls with better spacing */
    .header-controls {
        gap: 12px;
    }
    
    .btn-icon {
        padding: 10px;
        font-size: 1.1rem;
    }
    
    /* Hide desktop layout, show compact mobile layout */
    .game-sidebar {
        display: none !important;
    }
    
    .info-panel, .next-panel {
        display: block !important;
    }
    
    /* Compact two-column layout */
    .game-container {
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: min-content 1fr;
        gap: 0.3em;
        max-width: 100%;
        padding: 0 0.3em;
        padding-bottom: calc(0.3em + env(safe-area-inset-bottom));
        height: calc(100vh - 48px - env(safe-area-inset-bottom));
        height: calc(100dvh - 48px - env(safe-area-inset-bottom));
        max-height: calc(100vh - 48px - env(safe-area-inset-bottom));
        max-height: calc(100dvh - 48px - env(safe-area-inset-bottom));
    }
    
    /* Info panel (hold + stats combined) */
    .info-panel {
        grid-column: 1;
        grid-row: 1;
    }
    
    .hold-stats-card {
        background: var(--glass-bg);
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.15);
        border-radius: 6px;
        padding: 0.4em;
        display: flex;
        flex-direction: column;
        gap: 0.3em;
        max-height: 5.5em;
    }
    
    .hold-section {
        text-align: center;
    }
    
    .hold-section h4 {
        margin: 0 0 0.2em 0;
        font-size: 0.65em;
        color: var(--neon-blue);
        font-weight: 600;
        line-height: 1;
    }
    
    .hold-piece {
        width: 2.2em;
        height: 2.2em;
        margin: 0 auto;
        background: rgba(0, 0, 0, 0.3);
        border: 1px solid rgba(255, 255, 255, 0.15);
        border-radius: 3px;
    }
    
    .stats-section {
        display: flex;
        flex-direction: column;
        gap: 0.15em;
    }
    
    .stat-compact {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.05em 0;
        line-height: 1.2;
    }
    
    .stat-label-small {
        font-size: 0.6em;
        color: var(--text-secondary);
        font-weight: 500;
    }
    
    .stat-value-small {
        font-size: 0.7em;
        color: var(--neon-blue);
        font-weight: 600;
        font-family: 'Orbitron', monospace;
    }
    
    /* Next pieces panel */
    .next-panel {
        grid-column: 2;
        grid-row: 1;
    }
    
    .next-pieces-card {
        background: var(--glass-bg);
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.15);
        border-radius: 6px;
        padding: 0.4em;
        text-align: center;
        max-height: 5.5em;
    }
    
    .next-pieces-card h4 {
        margin: 0 0 0.2em 0;
        font-size: 0.65em;
        color: var(--neon-blue);
        font-weight: 600;
        line-height: 1;
    }
    
    .next-pieces-compact {
        display: flex;
        flex-direction: row;
        gap: 0.3em;
        align-items: center;
        justify-content: center;
    }
    
    .next-piece-small {
        width: 1.6em;
        height: 1.6em;
        background: rgba(0, 0, 0, 0.3);
        border: 1px solid rgba(255, 255, 255, 0.15);
        border-radius: 2px;
    }
    
    /* Game board spans both columns */
    .game-board {
        grid-column: 1 / -1;
        grid-row: 2;
        display: flex;
        justify-content: center;
        align-items: flex-start;
        height: 100%;
        max-height: calc(100vh - 85px - env(safe-area-inset-bottom));
        max-height: calc(100dvh - 85px - env(safe-area-inset-bottom));
        padding-bottom: calc(2px + env(safe-area-inset-bottom));
    }
    
    #game-canvas {
        width: 100%;
        max-width: min(280px, calc(100vw - 16px));
        height: calc(100vh - 85px - env(safe-area-inset-bottom));
        height: calc(100dvh - 85px - env(safe-area-inset-bottom));
        max-height: min(560px, calc(100vh - 85px - env(safe-area-inset-bottom)));
        max-height: min(560px, calc(100dvh - 85px - env(safe-area-inset-bottom)));
    }
    
    #overlay-title {
        font-size: 1.8rem;
    }
    
    #overlay-message {
        font-size: 1rem;
    }
    
    .btn-primary, .btn-secondary {
        padding: 15px 24px;
        font-size: 1rem;
        margin: 8px;
        min-height: 50px;
        border-radius: 8px;
    }
}

/* Large Mobile / Small Tablet (481px - 768px) */
@media (min-width: 481px) and (max-width: 768px) {
    /* Hide desktop layout, show compact layout */
    .game-sidebar {
        display: none !important;
    }
    
    .info-panel, .next-panel {
        display: block !important;
    }
    
    .game-container {
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: auto auto;
        gap: 20px;
        max-width: 500px;
        margin: 0 auto;
    }
    
    .info-panel {
        grid-column: 1;
        grid-row: 1;
    }
    
    .hold-stats-card {
        padding: 15px;
    }
    
    .hold-piece {
        width: 55px;
        height: 55px;
    }
    
    .stat-label-small {
        font-size: 0.8rem;
    }
    
    .stat-value-small {
        font-size: 0.9rem;
    }
    
    .next-panel {
        grid-column: 2;
        grid-row: 1;
    }
    
    .next-pieces-card {
        padding: 15px;
    }
    
    .next-piece-small {
        width: 40px;
        height: 40px;
    }
    
    .game-board {
        grid-column: 1 / -1;
        grid-row: 2;
        display: flex;
        justify-content: center;
    }
    
    #game-canvas {
        width: 100%;
        max-width: 350px;
        height: 700px;
    }
    
    .title {
        font-size: 3rem;
    }
    
    .controls-hint {
        font-size: 0.8rem;
    }
}

/* Tablets (769px - 1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
    /* Use traditional 3-column layout for tablets */
    .info-panel, .next-panel {
        display: none;
    }
    
    .game-sidebar {
        display: block !important;
    }
    
    .game-container {
        grid-template-columns: 180px 1fr 180px;
        gap: 25px;
        max-width: 800px;
    }
    
    #game-canvas {
        width: 350px;
        height: 700px;
    }
    
    .hold-area, .next-area, .stats {
        padding: 18px;
    }
    
    .hold-piece, .next-piece {
        width: 70px;
        height: 70px;
    }
    
    .title {
        font-size: 3.5rem;
    }
}

/* Desktop (1025px and up) */
@media (min-width: 1025px) {
    /* Use traditional 3-column layout for desktop */
    .info-panel, .next-panel {
        display: none;
    }
    
    .game-sidebar {
        display: block !important;
    }
    
    .game-container {
        grid-template-columns: 220px 1fr 220px;
        gap: 35px;
    }
    
    #game-canvas {
        width: 400px;
        height: 800px;
    }
    
    .title {
        font-size: 4rem;
    }
}

/* Large Desktop (1441px and up) */
@media (min-width: 1441px) {
    /* Use traditional 3-column layout for large desktop */
    .info-panel, .next-panel {
        display: none;
    }
    
    .game-sidebar {
        display: block !important;
    }
    
    .game-container {
        grid-template-columns: 250px 1fr 250px;
        gap: 40px;
    }
    
    #game-canvas {
        width: 450px;
        height: 900px;
    }
    
    .hold-area, .next-area, .stats {
        padding: 25px;
    }
    
    .hold-piece, .next-piece {
        width: 90px;
        height: 90px;
    }
}

/* Landscape Orientation for Mobile */
@media (max-height: 600px) and (orientation: landscape) {
    .app {
        padding: 10px;
    }
    
    .header {
        margin-bottom: 15px;
    }
    
    .title {
        font-size: 2rem;
        margin-bottom: 5px;
    }
    
    .controls-hint {
        display: none;
    }
    
    .game-container {
        grid-template-columns: 150px 1fr 150px;
        gap: 15px;
        max-width: 100%;
    }
    
    #game-canvas {
        width: 300px;
        height: 450px;
    }
    
    .hold-area, .next-area, .stats {
        padding: 10px;
        font-size: 0.85rem;
    }
    
    .hold-area h3, .next-area h3 {
        font-size: 0.9rem;
        margin-bottom: 10px;
    }
    
    .hold-piece, .next-piece {
        width: 50px;
        height: 50px;
    }
    
    .stat {
        padding: 6px 0;
    }
    
    .stat-value {
        font-size: 0.9rem;
    }
}

/* High DPI / Retina Displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    #game-canvas {
        image-rendering: -webkit-crisp-edges;
        image-rendering: crisp-edges;
    }
}

/* Reduced Motion Preference */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .title {
        animation: none;
        background: var(--neon-blue);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
    }
    
    .animate-glow {
        animation: none;
        box-shadow: var(--shadow-glow);
    }
    
    .animate-ghost {
        animation: none;
        opacity: 0.4;
    }
}

/* Dark Mode (System Preference) */
@media (prefers-color-scheme: dark) {
    :root {
        --primary-bg: #000000;
        --secondary-bg: #111111;
        --accent-bg: #1a1a1a;
    }
}

/* Light Mode Override */
@media (prefers-color-scheme: light) {
    :root {
        --primary-bg: #1a1a2e;
        --secondary-bg: #16213e;
        --accent-bg: #0f0f1a;
    }
}

/* Focus States for Keyboard Navigation */
@media (any-hover: none) {
    .btn-primary:focus,
    .btn-secondary:focus {
        outline: 2px solid var(--neon-blue);
        outline-offset: 2px;
    }
    
    .hold-piece:focus,
    .next-piece:focus {
        outline: 1px solid var(--neon-blue);
    }
}

/* Touch Device Optimizations */
@media (hover: none) and (pointer: coarse) {
    /* Improve button touch targets and feedback */
    .btn-primary, .btn-secondary, .btn-icon, .btn-close {
        min-height: 44px; /* Apple's recommended minimum touch target */
        min-width: 44px;
        touch-action: manipulation; /* Prevents double-tap zoom */
        user-select: none; /* Prevents text selection on long press */
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0); /* Remove tap highlight */
    }
    
    /* Active state for better touch feedback */
    .btn-primary.touch-active,
    .btn-secondary.touch-active,
    .btn-icon.touch-active,
    .btn-close.touch-active {
        transform: scale(0.95);
        opacity: 0.8;
        transition: all 0.1s ease;
    }
    
    /* Remove hover effects that don't work on touch */
    .controls-hint span:hover {
        background: var(--glass-bg);
        box-shadow: none;
    }
    
    .btn-primary:hover,
    .btn-secondary:hover {
        transform: none;
    }
    
    .hold-area:hover::before,
    .next-area:hover::before,
    .stats:hover::before {
        opacity: 0;
    }
    
    /* Better touch targets for modal interactions */
    .name-input-content, .leaderboard-content, .settings-content, .help-content {
        touch-action: manipulation;
    }
    
    /* Improve input field touch experience */
    input, select, textarea {
        touch-action: manipulation;
        min-height: 44px;
        font-size: 16px; /* Prevents zoom on iOS */
    }
    
    /* Game canvas touch optimization */
    #game-canvas {
        touch-action: none; /* Allow custom touch handling */
        user-select: none;
    }
    
    /* Tab buttons in modals */
    .tab-button, .settings-tab-button {
        min-height: 44px;
        touch-action: manipulation;
    }
    
    /* Leaderboard entries should be easier to see on touch */
    .leaderboard-entry {
        min-height: 50px;
    }
}

/* Modal Responsive Styles */
@media (max-width: 480px) {
    .name-input-content, .leaderboard-content {
        padding: 15px;
        margin: 10px;
        max-width: calc(100% - 20px);
        width: 100%;
    }
    
    .leaderboard-content {
        max-height: 90vh;
        display: flex;
        flex-direction: column;
    }
    
    .leaderboard-header {
        flex-shrink: 0;
        padding-bottom: 10px;
        margin-bottom: 10px;
    }
    
    .leaderboard-header h2 {
        font-size: 1.3rem;
    }
    
    .name-input-content h2 {
        font-size: 1.5rem;
    }
    
    .score-summary {
        grid-template-columns: 1fr;
        gap: 15px;
    }
    
    .input-buttons {
        flex-direction: column;
        gap: 10px;
    }
    
    .btn-primary, .btn-secondary {
        width: 100%;
        margin: 5px 0;
        min-height: 50px;
    }
    
    /* Leaderboard mobile card layout */
    .leaderboard-list {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 5px;
        max-height: calc(90vh - 200px);
    }
    
    .leaderboard-entry {
        display: block;
        position: relative;
        padding: 15px;
        font-size: 0.9rem;
        background: linear-gradient(135deg, rgba(255, 255, 255, 0.08) 0%, rgba(255, 255, 255, 0.03) 100%);
        border: 1px solid rgba(0, 212, 255, 0.2);
        margin-bottom: 12px;
    }
    
    .leaderboard-entry .entry-rank {
        position: absolute;
        top: 15px;
        left: 15px;
        font-size: 1.8rem;
        font-weight: bold;
        width: 40px;
        text-align: center;
    }
    
    .leaderboard-entry .entry-name {
        margin-left: 60px;
        font-size: 1.1rem;
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: 8px;
        word-break: break-word;
    }
    
    .leaderboard-entry .entry-score {
        margin-left: 60px;
        font-size: 1.3rem;
        font-weight: bold;
        color: var(--neon-green);
        text-shadow: 0 0 5px currentColor;
        margin-bottom: 10px;
    }
    
    /* Mobile stats row */
    .leaderboard-entry .entry-lines,
    .leaderboard-entry .entry-level {
        display: inline-block;
        margin-left: 60px;
        margin-right: 15px;
        font-size: 0.85rem;
        color: var(--text-secondary);
    }
    
    .leaderboard-entry .entry-lines::before {
        content: 'Lines: ';
        opacity: 0.7;
    }
    
    .leaderboard-entry .entry-level::before {
        content: 'Lvl: ';
        opacity: 0.7;
    }
    
    .leaderboard-tabs {
        flex-direction: row;
        gap: 5px;
        padding: 3px;
        flex-shrink: 0;
    }
    
    .tab-button {
        padding: 10px;
        font-size: 0.85rem;
    }
    
    /* Mode selector for mobile */
    .mode-selector-container {
        margin: 10px 0;
        padding: 5px;
    }
    
    .mode-selector {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }
    
    .mode-button {
        padding: 10px 8px;
        font-size: 0.8rem;
    }
    
    .mode-button .mode-icon {
        font-size: 1.2rem;
        margin-bottom: 3px;
    }
}

@media (min-width: 481px) and (max-width: 768px) {
    .name-input-content, .leaderboard-content {
        padding: 25px;
        max-width: 90%;
    }
    
    .score-summary {
        grid-template-columns: repeat(3, 1fr);
    }
    
    .leaderboard-list {
        max-height: 60vh;
        overflow-y: auto;
    }
    
    .leaderboard-entry {
        display: grid;
        grid-template-columns: 50px 2fr 1fr 60px 60px;
        gap: 10px;
        align-items: center;
        padding: 12px;
        font-size: 0.95rem;
    }
    
    .leaderboard-entry .entry-rank {
        position: static;
        font-size: 1.3rem;
    }
    
    .leaderboard-entry .entry-name {
        margin: 0;
        font-size: 1rem;
    }
    
    .leaderboard-entry .entry-score {
        margin: 0;
        font-size: 1.1rem;
        text-align: right;
    }
    
    .leaderboard-entry .entry-lines,
    .leaderboard-entry .entry-level {
        display: block;
        margin: 0;
        text-align: center;
        font-size: 0.9rem;
    }
    
    .leaderboard-entry .entry-lines::before,
    .leaderboard-entry .entry-level::before {
        content: '';
    }
    
    .leaderboard-tabs {
        flex-direction: row;
    }
    
    .mode-selector {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* Battle 2P specific responsive adjustments */
.battle2p-active .game-container {
    display: flex;
    align-items: center;
    justify-content: center;
    max-width: 100%;
    padding: 0;
}

.battle2p-active #game-canvas {
    display: none !important;
}

.battle2p-active .game-sidebar {
    display: none !important;
}

.battle2p-active .info-panel,
.battle2p-active .next-panel {
    display: none !important;
}

/* Print Styles */
@media print {
    .app {
        background: white;
        color: black;
    }
    
    .game-container {
        grid-template-columns: 1fr;
        gap: 20px;
    }
    
    .game-sidebar {
        display: none;
    }
    
    #game-canvas {
        border: 2px solid black;
        background: white;
    }
    
    .particles-container {
        display: none;
    }
    
    .name-input-overlay, .leaderboard-overlay {
        display: none !important;
    }
}