/* ──────────────────────────────────────────────
   Mobile-specific styles & Touch controls
   ────────────────────────────────────────────── */

/* ── Prevent iOS auto-zoom on input focus ───── */
@media screen and (max-width: 768px) {
    input, select, textarea {
        font-size: 16px !important; /* prevents iOS zoom on focus */
    }
}

/* ── Minimum touch target sizes (48×48 per Google) */
@media (pointer: coarse) {
    a, button, [role="button"], input[type="checkbox"], input[type="radio"] {
        min-height: 48px;
        min-width: 48px;
    }
    /* Ensure tap targets have spacing */
    nav a, nav button {
        padding: 12px 8px;
    }
}

/* ── Content-visibility for off-screen cards (CLS / LCP) */
@media (max-width: 768px) {
    .game-card {
        content-visibility: auto;
        contain-intrinsic-size: 0 180px;
    }
}

/* ── Reduce motion for users who prefer it ──── */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ── Smooth font rendering on mobile ────────── */
@media (max-width: 768px) {
    body {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-rendering: optimizeSpeed;
        overflow-x: hidden; /* prevent horizontal scroll */
    }
    html {
        overflow-x: hidden;
    }
    /* Ensure images are responsive / don't cause overflow */
    img, video, canvas, svg {
        max-width: 100%;
        height: auto;
    }
}

/* ── Improve text readability on small screens ─ */
@media (max-width: 480px) {
    body {
        font-size: 16px;
        line-height: 1.5;
        word-break: break-word;
        -webkit-hyphens: auto;
        hyphens: auto;
    }
    p, li, dd {
        max-width: 100%;
        overflow-wrap: break-word;
    }
}

/* ── Safe-area insets for notched phones ────── */
@supports (padding-top: env(safe-area-inset-top)) {
    header {
        padding-top: calc(0.6rem + env(safe-area-inset-top));
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
    footer {
        padding-bottom: calc(2rem + env(safe-area-inset-bottom));
    }
    #main-content {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
}

/* ── Hamburger menu ─────────────────────────── */
.hamburger-btn {
    display: none;
    background: none;
    border: none;
    cursor: pointer;
    padding: 6px;
    color: var(--color-text-primary, #f1f3f5);
    font-size: 1.5rem;
    line-height: 1;
    z-index: 201;
}

@media (max-width: 600px) {
    .hamburger-btn {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    header {
        position: sticky;
        top: 0;
        z-index: 200;
    }

    header nav {
        position: relative;
    }

    header nav > a,
    header nav > button#theme-toggle {
        /* Mobile nav links hidden by default, shown in mobile drawer */
    }

    .mobile-nav-drawer {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: var(--color-modal-overlay, rgba(0, 0, 0, 0.72));
        z-index: 300;
        align-items: flex-start;
        justify-content: flex-end;
    }

    .mobile-nav-drawer.open {
        display: flex;
    }

    .mobile-nav-menu {
        background: var(--color-bg-secondary, #18181b);
        border-left: 1px solid var(--color-border-primary, #26262b);
        width: 260px;
        max-width: 80vw;
        height: 100%;
        padding: 4.5rem 1.5rem 2rem;
        display: flex;
        flex-direction: column;
        gap: 0.25rem;
        animation: slideInRight 0.25s ease;
        overflow-y: auto;
    }

    @keyframes slideInRight {
        from { transform: translateX(100%); }
        to { transform: translateX(0); }
    }

    .mobile-nav-menu a,
    .mobile-nav-menu button {
        display: block;
        width: 100%;
        text-align: left;
        padding: 0.85rem 1rem;
        border-radius: 8px;
        font-size: 1rem;
        color: var(--color-text-primary, #f1f3f5);
        background: transparent;
        border: none;
        text-decoration: none;
        cursor: pointer;
        transition: background 0.15s;
    }

    .mobile-nav-menu a:hover,
    .mobile-nav-menu button:hover {
        background: var(--color-bg-tertiary, #1f1f23);
    }

    .mobile-nav-close {
        position: absolute;
        top: 1rem;
        right: 1rem;
        background: none;
        border: none;
        color: var(--color-text-primary, #f1f3f5);
        font-size: 1.5rem;
        cursor: pointer;
        padding: 4px 8px;
        z-index: 301;
    }

    /* Hide desktop nav links on mobile */
    header nav a,
    header nav #theme-toggle {
        display: none;
    }
}

/* ── Touch D-Pad ────────────────────────────── */
.touch-dpad {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    margin: 16px auto 8px;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

.dpad-middle {
    display: flex;
    align-items: center;
    gap: 2px;
}

.dpad-btn {
    width: 56px;
    height: 56px;
    border-radius: 12px;
    border: 2px solid var(--color-border-primary, #3a3a40);
    background: var(--color-bg-elevated, #26262b);
    color: var(--color-text-primary, #f1f3f5);
    font-size: 1.4rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.1s, transform 0.1s;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
}

.dpad-btn:active,
.dpad-btn.active {
    background: var(--color-primary, #667eea);
    transform: scale(0.92);
}

.dpad-center {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--color-bg-tertiary, #1a1a1d);
    border: 2px solid var(--color-border-primary, #3a3a40);
}

/* ── Touch Action Buttons ───────────────────── */
.touch-action-btn {
    position: fixed;
    bottom: 32px;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    border: 2px solid var(--color-border-primary, #3a3a40);
    background: var(--color-bg-elevated, #26262b);
    color: var(--color-text-primary, #f1f3f5);
    font-size: 1.3rem;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: background 0.1s, transform 0.1s;
}

.touch-action-btn.active {
    background: var(--color-primary, #667eea);
    transform: scale(0.9);
}

.touch-action-btn.touch-action-right {
    right: 20px;
}
.touch-action-btn.touch-action-left {
    left: 20px;
}

/* ── Prevent text selection during game ─────── */
.game-container {
    -webkit-user-select: none;
    user-select: none;
}

/* ── Canvas touch handling ──────────────────── */
.game-canvas {
    touch-action: none;
    -webkit-touch-callout: none;
}

/* ── Mobile tap hint ────────────────────────── */
.mobile-tap-hint {
    display: none;
    text-align: center;
    padding: 8px 12px;
    margin: 8px auto;
    background: var(--color-bg-tertiary, #1f1f23);
    border-radius: 8px;
    font-size: 0.8rem;
    color: var(--color-text-secondary, #aaa);
    max-width: 300px;
}

@media (pointer: coarse) {
    .mobile-tap-hint {
        display: block;
    }
    /* Hide keyboard-specific instructions on touch devices */
    .keyboard-only-hint {
        display: none;
    }
}

@media (pointer: fine) {
    .touch-dpad,
    .touch-action-btn {
        display: none !important;
    }
}

/* ── Improved game header for mobile ────────── */
@media (max-width: 600px) {
    .game-header {
        flex-direction: column;
        gap: 0.5rem;
        text-align: center;
        padding-bottom: 10px;
        margin-bottom: 10px;
    }

    .game-title {
        font-size: 1.3rem;
    }

    .game-container {
        padding: 12px 8px;
        border-radius: 8px;
        margin: 0;
    }

    .game-canvas {
        max-width: 100%;
        max-height: calc(100vh - 280px);
        width: auto !important;
        height: auto !important;
    }

    .game-instructions {
        padding: 10px;
        font-size: 0.85rem;
        margin-top: 12px;
    }

    .game-instructions h4 {
        font-size: 0.95rem;
    }

    .game-instructions p {
        font-size: 0.8rem;
        margin: 3px 0;
    }

    .controls {
        flex-wrap: wrap;
        gap: 8px;
        margin-top: 12px;
    }

    .controls button {
        flex: 1 1 auto;
        min-width: 100px;
        padding: 10px 12px;
        font-size: 0.9rem;
    }

    /* Stats rows */
    .tetris-stats,
    .target-stats,
    .space-stats,
    .dice-stats,
    .pong-stats,
    .balloon-stats,
    .flappy-stats {
        justify-content: center;
        gap: 8px;
        font-size: 0.85rem;
    }

    .tetris-stats > div,
    .target-stats > div,
    .space-stats > div,
    .dice-stats > div {
        font-size: 0.85rem;
    }

    /* Difficulty buttons */
    .difficulty-controls {
        flex-wrap: wrap;
        gap: 6px;
    }
    .diff-btn {
        padding: 6px 12px;
        font-size: 0.8rem;
    }

    /* Bet / prediction buttons */
    .bet-btn, .pred-btn {
        padding: 6px 10px;
        font-size: 0.75rem;
    }

    /* Game-specific layouts */
    .tetris-game-area {
        flex-direction: column;
        align-items: center;
        gap: 10px;
    }

    .dice-game-area {
        flex-direction: column;
        align-items: center;
    }

    .dice-controls {
        min-width: unset;
        width: 100%;
    }

    #main-content {
        padding: 0.75rem;
    }
}

/* ── Improved game card grid on mobile ──────── */
@media (max-width: 600px) {
    .games-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 0.6rem !important;
        margin-top: 0.75rem !important;
    }

    .game-card {
        padding: 0.85rem 0.6rem !important;
        min-height: unset !important;
        border-radius: 10px;
    }

    .game-card .game-icon {
        font-size: 2rem;
        margin-bottom: 0.3rem;
    }

    .game-card h3 {
        font-size: 0.85rem;
    }

    .game-card .game-desc {
        font-size: 0.7rem;
        margin-top: 0.25rem;
    }

    .game-card .play-button {
        padding: 0.4rem 0.6rem;
        font-size: 0.7rem;
        margin-top: 0.5rem;
    }
}

/* Very small phones */
@media (max-width: 360px) {
    .games-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 0.5rem !important;
    }

    .game-card {
        padding: 0.7rem 0.5rem !important;
    }

    .game-card .game-icon {
        font-size: 1.7rem;
    }

    .game-card h3 {
        font-size: 0.78rem;
    }

    .dpad-btn {
        width: 48px;
        height: 48px;
        font-size: 1.2rem;
    }

    .dpad-center {
        width: 48px;
        height: 48px;
    }

    .touch-action-btn {
        width: 54px;
        height: 54px;
        font-size: 1.1rem;
    }
}

/* ── New Game Mobile Styles ─────────────────── */

/* Tic-Tac-NO board */
@media (max-width: 480px) {
    .ttt-board {
        max-width: 280px !important;
    }
    .ttt-cell {
        min-height: 70px !important;
        font-size: 2rem !important;
    }
}

/* Hangman keyboard */
@media (max-width: 480px) {
    .hangman-key {
        width: 32px !important;
        height: 38px !important;
        font-size: 0.85rem !important;
    }
}

/* Wordle keyboard & board */
@media (max-width: 480px) {
    .wordle-kb-btn {
        width: 28px !important;
        height: 42px !important;
        font-size: 0.8rem !important;
    }
    #wordle-board > div > div {
        font-size: 1.3rem !important;
    }
}

/* Connect Four board */
@media (max-width: 480px) {
    #c4-board {
        max-width: 300px !important;
    }
    #c4-drop-arrows {
        max-width: 300px !important;
    }
}

/* Battleship grids */
@media (max-width: 600px) {
    #bs-player-grid, #bs-ai-grid {
        gap: 1px !important;
    }
    .bs-cell {
        width: 32px !important;
        height: 32px !important;
        font-size: 14px !important;
    }
}
@media (max-width: 400px) {
    .bs-cell {
        width: 26px !important;
        height: 26px !important;
        font-size: 11px !important;
    }
}

/* Rock Paper Cheater */
@media (max-width: 480px) {
    .rps-choice-btn {
        width: 65px !important;
        height: 65px !important;
        font-size: 2rem !important;
    }
    #rps-player-display, #rps-ai-display {
        font-size: 3rem !important;
        width: 80px !important;
        height: 80px !important;
    }
}

/* Quiz Show answer grid */
@media (max-width: 480px) {
    #quiz-answers {
        grid-template-columns: 1fr !important;
    }
    .quiz-answer-btn {
        font-size: 0.9rem !important;
        padding: 12px !important;
    }
}

/* Typing Test */
@media (max-width: 480px) {
    #typing-target, #typing-display {
        font-size: 1rem !important;
        padding: 14px !important;
    }
    #typing-input {
        font-size: 1rem !important;
    }
}

/* Self-Crashing Car mobile controls */
@media (max-width: 600px) {
    #car-controls-mobile {
        margin-top: 8px !important;
    }
    #car-controls-mobile button {
        min-height: 52px;
    }
}
@media (pointer: fine) {
    #car-controls-mobile {
        display: none !important;
    }
}

/* AI thought bubble universal mobile */
@media (max-width: 480px) {
    .ai-thought-bubble {
        font-size: 0.8rem !important;
        padding: 8px !important;
    }
    .ai-warning {
        font-size: 0.8rem !important;
        padding: 8px !important;
    }
}
