/**
 * ============================================
 * ALPHA KIDS - UNIVERSAL MOBILE GAME FRAMEWORK
 * ============================================
 *
 * Supports all 55+ games across 4 age levels:
 * - Age 3: 6 game modes
 * - Age 4: 8 game modes
 * - Age 5: 15 game modes
 * - Age 6: 26 game modes
 *
 * ARCHITECTURE:
 * - Fixed game stage: 1280×720px (16:9 landscape)
 * - Intelligent scaling for all screen sizes
 * - Touch-friendly (minimum 52px targets)
 * - Safe area support (notched devices)
 * - Orientation handling (landscape preferred)
 *
 * SUPPORTED DEVICES:
 * - iPhone SE (375×667) → iPhone 15 Pro Max (430×932)
 * - Small Android (360×640) → Large tablets (1024×768)
 * - Landscape & Portrait modes
 * ============================================
 */

/* ========================================
   RESET & BASE STYLES
   ======================================== */

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    min-height: 100vh;
    min-height: 100dvh; /* Dynamic viewport height for mobile */
    overflow: hidden;
    position: fixed; /* Prevent scrolling on mobile */
    -webkit-text-size-adjust: 100%;
    -webkit-overflow-scrolling: touch;
}

body {
    font-family: 'Outfit', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: url('assets/wallpaper_space.png') no-repeat center center fixed;
    background-size: cover;
}

/* ========================================
   GAME STAGE ARCHITECTURE
   ======================================== */

/* App shell - handles safe areas */
#app-shell {
    position: fixed;
    inset: 0;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;

    /* Safe area insets for notched devices (iPhone X+) */
    padding:
        env(safe-area-inset-top, 0)
        env(safe-area-inset-right, 0)
        env(safe-area-inset-bottom, 0)
        env(safe-area-inset-left, 0);
}

/* Game stage container - scaled by JavaScript */
#game-stage {
    position: absolute;
    width: 1280px;
    height: 720px;
    left: 50%;
    top: 50%;
    transform-origin: center center;
    /* transform is set by JavaScript: translate(-50%, -50%) scale(X) */
    background: transparent;
    overflow: visible; /* Allow elements to extend slightly if needed */
}

/* App content - all game content lives here at 1280×720 */
#app {
    width: 1280px;
    height: 720px;
    position: relative;
    overflow: hidden;
}

/* ========================================
   RESPONSIVE SCALING VARIABLES
   ======================================== */

:root {
    /* Stage dimensions */
    --stage-width: 1280px;
    --stage-height: 720px;
    --stage-scale: 1; /* Updated by JavaScript */

    /* Touch target minimum (WCAG 2.2 Level AAA + toddler buffer) */
    --min-touch-target: 52px;

    /* Spacing system (scales with viewport) */
    --gap-xs: clamp(6px, 0.8vw, 10px);
    --gap-sm: clamp(10px, 1.2vw, 16px);
    --gap-md: clamp(14px, 1.8vw, 24px);
    --gap-lg: clamp(20px, 2.5vw, 36px);
    --gap-xl: clamp(28px, 3.5vw, 48px);

    /* Typography (scales with viewport) */
    --font-title: clamp(28px, 3.5vw, 48px);
    --font-body: clamp(20px, 2.2vw, 32px);
    --font-button: clamp(22px, 2.5vw, 36px);
    --font-instruction: clamp(18px, 2vw, 28px);
    --font-small: clamp(16px, 1.8vw, 24px);

    /* Interactive elements */
    --btn-height: clamp(52px, 8vh, 84px);
    --btn-width: clamp(100px, 14vw, 180px);
    --btn-icon-size: clamp(32px, 4vw, 52px);

    /* Game elements */
    --tile-size: clamp(72px, 10vw, 130px);
    --emoji-size: clamp(48px, 6vw, 80px);
    --card-size: clamp(100px, 12vw, 160px);
}

/* ========================================
   COMMON GAME ELEMENTS
   ======================================== */

/* Header bar (top navigation) */
.header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: clamp(60px, 10vh, 90px);
    padding: var(--gap-sm) var(--gap-md);
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 100;
    background: linear-gradient(180deg, rgba(0,0,0,0.3) 0%, transparent 100%);
}

/* Game viewport (main play area) */
.game-viewport {
    position: absolute;
    top: clamp(60px, 10vh, 90px);
    left: var(--gap-md);
    right: var(--gap-md);
    bottom: clamp(80px, 12vh, 120px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--gap-sm);
}

/* Footer controls */
.footer {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: clamp(80px, 12vh, 120px);
    padding: var(--gap-sm) var(--gap-md);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--gap-md);
    z-index: 100;
    background: linear-gradient(0deg, rgba(0,0,0,0.3) 0%, transparent 100%);
}

/* ========================================
   BUTTONS & INTERACTIVE ELEMENTS
   ======================================== */

button, .btn {
    min-width: var(--btn-width);
    min-height: var(--btn-height);
    padding: var(--gap-sm) var(--gap-md);
    font-size: var(--font-button);
    font-weight: 700;
    border: none;
    border-radius: 16px;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;

    /* Touch optimization */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

button:active, .btn:active {
    transform: scale(0.95);
}

/* Tile/Card elements (draggable items) */
.tile, .card, .game-item {
    min-width: var(--tile-size);
    min-height: var(--tile-size);
    border-radius: 12px;
    cursor: grab;
    touch-action: none; /* Allow drag gestures */
    transition: transform 0.2s;
}

.tile:active, .card:active, .game-item:active {
    cursor: grabbing;
    transform: scale(1.05);
}

/* Drop zones */
.drop-zone {
    min-width: calc(var(--tile-size) + 20px);
    min-height: calc(var(--tile-size) + 20px);
    border: 3px dashed rgba(255, 255, 255, 0.5);
    border-radius: 16px;
    transition: all 0.3s;
}

.drop-zone.highlight {
    border-color: #4CAF50;
    background: rgba(76, 175, 80, 0.2);
    transform: scale(1.05);
}

/* ========================================
   MOBILE SCREEN SIZE SPECIFIC FIXES
   ======================================== */

/* Very small phones (iPhone SE, small Android) */
@media (max-width: 375px) and (max-height: 667px) {
    :root {
        --gap-sm: 8px;
        --gap-md: 12px;
        --gap-lg: 16px;
        --font-title: 24px;
        --font-body: 18px;
        --font-button: 20px;
        --tile-size: 60px;
    }
}

/* Small phones in portrait */
@media (max-width: 430px) and (orientation: portrait) {
    :root {
        --gap-sm: 10px;
        --gap-md: 14px;
        --tile-size: 65px;
    }
}

/* Medium phones (iPhone 12/13/14) */
@media (min-width: 375px) and (max-width: 430px) and (max-height: 932px) {
    :root {
        --tile-size: clamp(70px, 11vw, 110px);
        --btn-height: clamp(54px, 8.5vh, 80px);
    }
}

/* Large phones/small tablets */
@media (min-width: 768px) {
    :root {
        --tile-size: clamp(90px, 10vw, 140px);
        --emoji-size: clamp(60px, 6vw, 90px);
    }
}

/* ========================================
   ORIENTATION HANDLING
   ======================================== */

/* Portrait mode warning overlay */
@media (orientation: portrait) {
    .portrait-prompt {
        display: flex !important;
        position: fixed;
        inset: 0;
        z-index: 9999;
        background: rgba(0, 0, 0, 0.9);
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: var(--gap-lg);
        color: white;
        text-align: center;
        padding: var(--gap-lg);
    }

    .portrait-prompt .rotate-icon {
        font-size: 80px;
        animation: rotate-hint 2s ease-in-out infinite;
    }

    .portrait-prompt h2 {
        font-size: var(--font-title);
        margin: 0;
    }

    .portrait-prompt p {
        font-size: var(--font-body);
        margin: 0;
        opacity: 0.8;
    }
}

@keyframes rotate-hint {
    0%, 100% { transform: rotate(0deg); }
    50% { transform: rotate(90deg); }
}

/* Landscape mode (preferred) */
@media (orientation: landscape) {
    .portrait-prompt {
        display: none !important;
    }
}

/* ========================================
   PREVENT OVERFLOW ON ALL DEVICES
   ======================================== */

/* Ensure nothing overflows the stage */
#app > * {
    max-width: 100%;
    max-height: 100%;
}

/* Clamp all positioned elements within stage */
.game-viewport > * {
    max-width: 100%;
    max-height: 100%;
}

/* Prevent text from causing overflow */
.game-viewport, .header, .footer {
    overflow: hidden;
}

/* ========================================
   ACCESSIBILITY
   ======================================== */

/* Ensure focus indicators for keyboard navigation */
button:focus-visible, .btn:focus-visible {
    outline: 3px solid #2196F3;
    outline-offset: 2px;
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ========================================
   LOADING STATE
   ======================================== */

#gameLoader {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: url('assets/wallpaper_space.png') no-repeat center center fixed;
    background-size: cover;
    z-index: 10000;
}

#gameLoader.hidden {
    display: none;
}

/* ========================================
   DEBUG MODE (optional)
   ======================================== */

.debug-info {
    position: fixed;
    top: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.8);
    color: #0f0;
    padding: 10px;
    font-family: monospace;
    font-size: 12px;
    border-radius: 4px;
    z-index: 99999;
    pointer-events: none;
}

/* ========================================
   DEVICE-SPECIFIC FIXES
   ======================================== */

/* iOS-specific fixes */
@supports (-webkit-touch-callout: none) {
    /* Fix iOS Safari viewport height issues */
    html {
        height: -webkit-fill-available;
    }

    body {
        height: -webkit-fill-available;
        min-height: -webkit-fill-available;
    }

    /* Prevent iOS zoom on input focus */
    input, select, textarea {
        font-size: 16px !important;
    }

    /* Fix iOS rubber band scrolling */
    body {
        overscroll-behavior: none;
    }
}

/* Android-specific fixes */
@media (max-width: 768px) {
    /* Fix Android Chrome address bar height changes */
    html {
        min-height: 100vh;
        min-height: -webkit-fill-available;
    }
}
