/* Mobile Responsive Styles for Chiki Learning Adventure */

/* Prevent double-tap zoom globally */
* {
    touch-action: manipulation;
}

/* Touch-friendly base - ensure minimum touch targets */
@media (max-width: 1024px) {
    /* Increase tap target sizes for touch devices */
    .answer-btn {
        min-height: 80px;
        min-width: 80px;
        font-size: 32px;
        touch-action: manipulation;
    }

    .nav-btn, .btn {
        min-height: 48px;
        min-width: 120px;
        padding: 14px 20px;
        font-size: 18px;
        touch-action: manipulation;
    }

    /* Ensure mode cards are touch-friendly */
    .mode-card {
        min-height: 80px;
        padding: 12px;
        touch-action: manipulation;
    }

    /* Ensure table rows are touch-friendly */
    .adventure-row {
        min-height: 48px;
    }

    .btn-play-small,
    .btn-delete-small {
        min-height: 44px;
        min-width: 44px;
        padding: 8px 12px;
        touch-action: manipulation;
    }

    /* Hide keyboard hints on touch devices */
    .key-hint {
        display: none;
    }

    /* Adjust spacing for smaller screens */
    #bottom-section {
        padding: 8px 12px;
    }

    #bottom-content {
        gap: 15px;
    }

    #answer-options {
        gap: 10px;
    }

    /* Make navigation controls stack vertically on tablets */
    #nav-controls {
        flex-direction: column;
        gap: 10px;
        min-width: 140px;
    }
}

/* Tablet landscape and smaller (1024px and below) */
@media (max-width: 1024px) and (orientation: landscape) {
    /* Remove 16:9 constraint on tablets too */
    #game-container {
        aspect-ratio: unset;
        max-width: 100%;
        max-height: 100%;
        width: 100%;
        height: 100%;
    }

    /* Reduce vertical padding to maximize content area */
    #top-section {
        height: 70%;
    }

    #bottom-section {
        height: 30%;
    }

    #chiki-area {
        padding: 10px;
    }

    #menu-title {
        font-size: 14px;
    }

    #chiki-image {
        max-height: 70%;
    }
}

/* Mobile devices (768px and below) */
@media (max-width: 768px) {
    /* Adjust game container to use full screen */
    #game-container {
        max-width: 100%;
        max-height: 100%;
    }

    /* Reduce top section height to give more space to answers */
    #top-section {
        height: 65%;
    }

    #bottom-section {
        height: 35%;
    }

    /* Stack layout vertically in portrait mode */
    #chiki-area {
        width: 20%;
        padding: 8px;
    }

    #content-area {
        width: 60%;
        padding: 12px;
    }

    #progress-area {
        width: 20%;
        padding: 8px;
    }

    #content-area.expanded {
        width: 80%;
    }

    /* Smaller star bucket */
    #star-bucket {
        width: 80px;
        height: 140px;
    }

    /* Compact answer buttons - 2x2 grid on small screens */
    #answer-options {
        flex-wrap: wrap;
        max-width: 100%;
    }

    .answer-btn {
        flex: 1 1 45%;
        min-width: 140px;
        max-width: 48%;
        height: 100px;
    }

    .answer-content {
        font-size: 32px;
    }

    /* Typography adjustments */
    h1 {
        font-size: 28px;
    }

    h2 {
        font-size: 24px;
    }

    h3 {
        font-size: 18px;
    }

    .letter-card {
        width: 100px;
        height: 120px;
        font-size: 64px;
    }

    .word-display {
        font-size: 48px;
        letter-spacing: 4px;
    }

    .object-image {
        max-width: 200px;
        max-height: 200px;
    }

    /* Menu table adjustments */
    .adventure-table th {
        font-size: 10px;
        padding: 8px 10px;
    }

    .adventure-table td {
        font-size: 13px;
        padding: 10px;
    }

    .adventure-name {
        font-size: 14px;
    }

    .mode-card {
        min-width: 90px;
        padding: 8px 12px;
    }

    .mode-card .mode-icon {
        font-size: 24px;
    }

    .mode-card .mode-name {
        font-size: 11px;
    }
}

/* Portrait orientation - optimize vertical space */
@media (max-width: 768px) and (orientation: portrait) {
    /* Give even more space to content in portrait */
    #top-section {
        height: 60%;
        flex-direction: column;
    }

    #bottom-section {
        height: 40%;
    }

    /* Stack content vertically */
    #chiki-area {
        width: 100%;
        height: 25%;
        flex-direction: row;
        padding: 8px 12px;
    }

    #menu-title {
        font-size: 16px;
        margin-bottom: 0;
        margin-right: 10px;
        text-align: left;
    }

    #chiki-display {
        flex: 0 0 auto;
        width: 80px;
    }

    #chiki-image {
        max-width: 70px;
        max-height: 70px;
    }

    #content-area {
        width: 100%;
        height: 50%;
        padding: 10px 12px;
    }

    #content-area.expanded {
        height: 75%;
    }

    #progress-area {
        width: 100%;
        height: 25%;
        padding: 8px 12px;
    }

    #progress-area.hidden {
        height: 0;
    }

    #star-bucket {
        width: 100px;
        height: 120px;
    }

    /* Answer buttons - 2x2 grid */
    #bottom-content {
        flex-direction: column;
        gap: 10px;
    }

    #nav-controls {
        flex-direction: row;
        gap: 12px;
        width: 100%;
        justify-content: center;
    }

    .nav-btn {
        flex: 1;
        max-width: 150px;
    }

    #answer-options {
        width: 100%;
        flex-wrap: wrap;
        justify-content: space-between;
    }

    .answer-btn {
        flex: 1 1 48%;
        min-width: 48%;
        max-width: 48%;
        height: 90px;
        margin-bottom: 4px;
    }
}

/* Small mobile devices (480px and below) */
@media (max-width: 480px) {
    /* Further compact everything */
    #top-section {
        height: 58%;
    }

    #bottom-section {
        height: 42%;
        padding: 6px 8px;
    }

    #chiki-area {
        padding: 6px 8px;
    }

    #content-area {
        padding: 8px;
    }

    #progress-area {
        padding: 6px 8px;
    }

    /* Even smaller fonts */
    h1 {
        font-size: 22px;
    }

    h2 {
        font-size: 20px;
    }

    .letter-card {
        width: 80px;
        height: 100px;
        font-size: 48px;
    }

    .word-display {
        font-size: 36px;
        letter-spacing: 2px;
    }

    .object-image {
        max-width: 150px;
        max-height: 150px;
    }

    .answer-btn {
        height: 80px;
        font-size: 28px;
    }

    .answer-content {
        font-size: 28px;
    }

    .letter-display {
        font-size: 56px !important;
    }

    .word-display {
        font-size: 24px !important;
    }

    .option-image {
        max-width: 80px !important;
        max-height: 80px !important;
    }

    .nav-btn {
        padding: 12px 16px;
        font-size: 15px;
        min-width: 100px;
    }

    /* Compact menu */
    .menu-container {
        padding: 12px;
        gap: 10px;
    }

    .adventure-list {
        max-height: 300px;
    }

    .mode-card {
        min-width: 80px;
        padding: 6px 8px;
    }

    .mode-card .mode-icon {
        font-size: 20px;
    }

    .mode-card .mode-name {
        font-size: 10px;
    }

    .mode-card .mode-desc {
        font-size: 8px;
    }

    #star-bucket {
        width: 80px;
        height: 100px;
    }
}

/* Landscape mobile - fill full viewport, floating nav */
@media (max-width: 900px) and (orientation: landscape) {
    /* Remove 16:9 constraint — fill the full viewport */
    #game-container {
        aspect-ratio: unset;
        max-width: 100%;
        max-height: 100%;
        width: 100%;
        height: 100%;
    }

    /* Top section fills almost everything */
    #top-section {
        height: 80%;
        flex-direction: row;
    }

    /* Bottom section — thin strip for answer buttons only */
    #bottom-section {
        height: 20%;
        padding: 4px 60px;
        background: linear-gradient(180deg, #E0F4FF 0%, #B0E0FF 100%);
    }

    /* Float nav buttons to corners, outside the bottom bar flow */
    #nav-controls {
        display: contents;
    }

    #btn-menu {
        position: fixed;
        bottom: 6px;
        left: 6px;
        z-index: 200;
        padding: 6px 12px;
        font-size: 11px;
        min-width: auto;
        min-height: 32px;
        opacity: 0.7;
        border-radius: 6px;
        background: rgba(255,255,255,0.9);
        border: 1px solid #ccc;
    }

    #btn-replay {
        position: fixed;
        bottom: 6px;
        right: 6px;
        z-index: 200;
        padding: 6px 12px;
        font-size: 11px;
        min-width: auto;
        min-height: 32px;
        opacity: 0.7;
        border-radius: 6px;
        background: rgba(255,255,255,0.9);
        border: 1px solid #ccc;
    }

    #btn-menu:active, #btn-replay:active {
        opacity: 1;
    }

    /* New Adventure button — also floating */
    #btn-new-adventure {
        position: fixed;
        bottom: 6px;
        right: 6px;
        z-index: 200;
        padding: 8px 16px;
        font-size: 13px;
    }

    /* When no answer bar needed (menu/setup/complete), collapse bottom section */
    #game-container.no-answer-bar #bottom-section {
        height: 0;
        padding: 0;
        overflow: hidden;
    }

    #game-container.no-answer-bar #top-section {
        height: 100%;
    }

    /* Compact Chiki area in landscape */
    #chiki-area {
        width: 12%;
        height: 100%;
        flex-direction: column;
        padding: 6px;
    }

    #chiki-display {
        width: auto;
    }

    #chiki-image {
        max-width: 100%;
        max-height: 55%;
    }

    #menu-title {
        font-size: 10px;
        margin-bottom: 2px;
    }

    /* Content area takes the lion's share */
    #content-area {
        width: 73%;
        height: 100%;
        padding: 6px 10px;
        overflow: hidden;
    }

    #content-area.expanded {
        width: 88%;
    }

    #progress-area {
        width: 15%;
        height: 100%;
        padding: 6px;
    }

    /* Setup screen */
    .setup-container {
        padding: 2px 6px;
    }

    .setup-container h2 {
        font-size: 16px;
        margin-bottom: 4px;
    }

    .mode-section {
        margin-bottom: 4px;
        gap: 2px;
    }

    .mode-section h3 {
        font-size: 12px;
        margin: 0;
    }

    .mode-grid {
        gap: 5px;
    }

    .mode-card {
        min-width: 60px;
        padding: 4px 6px;
        border-radius: 6px;
        border-width: 2px;
    }

    .mode-card .mode-icon {
        font-size: 16px;
        margin-bottom: 1px;
    }

    .mode-card .mode-name {
        font-size: 9px;
    }

    .mode-card .mode-desc {
        display: none;
    }

    /* Answer buttons — horizontal strip */
    #bottom-content {
        flex-direction: row;
        gap: 8px;
        justify-content: center;
        width: 100%;
    }

    #answer-options {
        flex-wrap: nowrap;
        flex: 1;
        justify-content: center;
    }

    .answer-btn {
        flex: 1;
        max-width: 120px;
        height: 50px !important;
        min-height: 0 !important;
        border-radius: 8px;
        border-width: 3px;
    }

    /* Override inline styles on answer images (W1/W2 set max-height: 120px inline) */
    .answer-content img,
    .option-image {
        max-height: 40px !important;
        max-width: 70px !important;
        object-fit: contain !important;
    }

    /* L1 target letter - 180px is way too big for landscape */
    .target-letter {
        font-size: 100px !important;
    }

    /* L2 sound display - reduce margins and sizes */
    .sound-display {
        gap: 8px !important;
        margin-bottom: 12px !important;
    }

    .sound-icon {
        font-size: 50px !important;
    }

    .phoneme-text {
        font-size: 32px !important;
    }

    /* W1 - target word is 96px, too big */
    .target-word {
        font-size: 52px !important;
        letter-spacing: 4px !important;
    }

    /* L3/W3 - content images too large */
    .target-image {
        max-width: 80px !important;
        max-height: 80px !important;
    }

    .word-image-container {
        min-height: 70px !important;
        margin-bottom: 8px !important;
    }

    /* L3 word display under image */
    .l3-display .word-display,
    .word-display {
        font-size: 32px !important;
        letter-spacing: 3px !important;
    }

    /* W3 letter boxes */
    .letter-box {
        width: 45px !important;
        height: 52px !important;
        font-size: 28px !important;
    }

    .spelling-progress {
        gap: 6px !important;
        margin-bottom: 8px !important;
    }

    .answer-content {
        font-size: 24px;
    }

    /* Override JS-injected font sizes for answer buttons */
    .answer-btn .letter-display {
        font-size: 30px !important;
    }

    .answer-btn .word-display {
        font-size: 18px !important;
    }

    /* Emoji fallback in W1/W2 buttons */
    .emoji-fallback {
        font-size: 30px !important;
    }

    /* Star bucket */
    #star-bucket {
        width: 50px;
        height: 80px;
    }

    /* Typography */
    h1 { font-size: 18px; }
    h2 { font-size: 16px; }
    h3 { font-size: 13px; }

    .letter-card {
        width: 60px;
        height: 75px;
        font-size: 36px;
    }

    .word-display {
        font-size: 24px;
        letter-spacing: 2px;
    }

    .object-image {
        max-width: 100px;
        max-height: 100px;
    }

    /* ===== Tutorial game scaling for landscape mobile ===== */

    /* General instructions text */
    .instruction {
        font-size: 18px !important;
        margin-bottom: 8px !important;
    }

    /* T2: Find Chiki - Doors */
    .doors-container {
        height: 100%;
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
    }

    .doors-row {
        gap: 10px !important;
    }

    .door-slot {
        width: 70px !important;
        height: 110px !important;
    }

    /* Fix door open animation — reduce swing angle, tighten perspective */
    .door {
        perspective: 300px !important;
    }

    .door.open .door-panel {
        transform: rotateY(-80deg) !important;
    }

    .hidden-chiki {
        width: 55px !important;
    }

    .door-number {
        font-size: 16px !important;
        margin-top: 3px !important;
    }

    .door-handle {
        width: 10px !important;
        height: 10px !important;
        right: 12px !important;
    }

    /* T3: Stepping Stones */
    .lava-container {
        height: 100% !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
    }

    .lava-field {
        height: auto !important;
        max-height: 120px !important;
        align-items: center !important;
    }

    .shore {
        width: 50px !important;
        height: 40px !important;
    }

    .lava {
        padding: 10px !important;
        gap: 10px !important;
    }

    .stepping-chiki {
        width: 45px !important;
        bottom: 30px !important;
    }

    .stone {
        width: 45px !important;
        height: 26px !important;
    }

    .stone-number {
        font-size: 18px !important;
    }

    .goal-marker {
        font-size: 12px !important;
    }

    /* T4: Flower Garden */
    .garden-container {
        height: 100% !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
    }

    /* T4: garden-scene (not garden-area — was wrong class name) */
    .garden-scene {
        height: 160px !important;
    }

    .pots-row {
        bottom: 10px !important;
    }

    .pot-wrapper {
        transform: scale(0.55) !important;
        transform-origin: bottom center !important;
    }

    .garden-scene .sun {
        width: 35px !important;
        height: 35px !important;
        top: 5px !important;
    }

    .seed-indicator {
        margin-top: 6px !important;
    }

    /* T5: Dance Party */
    .dance-container {
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
        gap: 10px !important;
        height: 100% !important;
        width: 100% !important;
    }

    .dance-container .instruction {
        position: absolute !important;
        top: 2px !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
        font-size: 14px !important;
        z-index: 10 !important;
        white-space: nowrap !important;
    }

    .stage-scene {
        height: 100% !important;
        max-height: 140px !important;
        flex: 1 !important;
        border-radius: 8px !important;
    }

    .dance-floor {
        height: 30px !important;
    }

    .disco-ball {
        width: 30px !important;
        height: 30px !important;
    }

    .dance-chiki {
        width: 60px !important;
    }

    .stage-chiki-area {
        height: 80px !important;
    }

    .move-indicator {
        flex: 0 0 auto !important;
    }

    .move-cards {
        display: flex !important;
        flex-direction: column !important;
        gap: 4px !important;
    }

    .move-card {
        width: 50px !important;
        height: 35px !important;
        padding: 2px 4px !important;
        min-width: auto !important;
    }

    .move-card .move-icon {
        font-size: 16px !important;
    }

    .move-card .move-name {
        display: none !important;
    }

    .move-card .move-key {
        display: none !important;
    }

    /* T6: Lava Firefighter (fixed class names: firefighter-scene, volcano-row) */
    .firefighter-scene {
        height: 100% !important;
        width: 100% !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
        align-items: center !important;
        overflow: hidden !important;
    }

    .firefighter-scene .instruction {
        font-size: 15px !important;
        margin-bottom: 6px !important;
    }

    .volcano-row {
        gap: 18px !important;
        margin-bottom: 4px !important;
    }

    /* Reduce actual volcano sizes (transform:scale doesn't affect layout) */
    .volcano {
        width: 55px !important;
        height: 65px !important;
    }

    .volcano-body {
        width: 55px !important;
        height: 42px !important;
    }

    .volcano-crater {
        width: 26px !important;
        height: 12px !important;
    }

    .volcano-number {
        font-size: 20px !important;
        margin-top: 2px !important;
    }

    .lava-pool {
        height: 18px !important;
        margin: 0 20px !important;
        border-radius: 6px !important;
    }

    .chiki-firefighter {
        margin-top: 6px !important;
    }

    #firefighter-chiki {
        width: 55px !important;
    }

    .water-meter {
        height: 60px !important;
    }

    /* T7: Slime Splash (fixed class names: slime-scene, dispenser-row, etc.) */
    .slime-scene {
        height: 100% !important;
        width: 100% !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
        align-items: center !important;
        overflow: hidden !important;
        position: relative !important;
    }

    .slime-scene .instruction {
        font-size: 15px !important;
        margin-bottom: 8px !important;
    }

    .dispenser-row {
        gap: 22px !important;
        margin-bottom: 6px !important;
    }

    /* Reduce actual dispenser sizes */
    .dispenser-tank {
        width: 45px !important;
        height: 30px !important;
    }

    .dispenser-nozzle {
        width: 14px !important;
        height: 10px !important;
    }

    .drip-container {
        top: 40px !important;
        height: 35px !important;
    }

    .dispenser-number {
        font-size: 18px !important;
        margin-top: 4px !important;
    }

    /* Chiki target — reduce margin so it doesn't push below viewport */
    .chiki-target {
        margin-top: 10px !important;
        display: inline-block !important;
    }

    #slime-chiki {
        width: 80px !important;
    }

    .ground {
        height: 12px !important;
    }

    /* T1: Free Play — reduce button sizes for landscape */
    .free-play-container {
        gap: 8px !important;
    }

    .sound-button {
        width: 100px !important;
        height: 110px !important;
    }

    .button-number {
        font-size: 40px !important;
    }

    .button-label {
        font-size: 14px !important;
        margin-top: 2px !important;
    }

    .palette-indicator {
        padding: 6px 15px !important;
        border-radius: 20px !important;
    }

    .palette-icon {
        font-size: 22px !important;
    }

    .palette-name {
        font-size: 16px !important;
    }

    .done-button-container {
        margin-top: 4px !important;
    }

    /* W2: image-to-word — image container too tall */
    .w2-display .image-container {
        min-height: 70px !important;
        margin-bottom: 6px !important;
    }

    /* General gameplay content area — fit within viewport, no scrolling */
    #gameplay-content {
        width: 100% !important;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        overflow: hidden;
    }

    /* Prevent content area from scrolling in landscape gameplay */
    #game-container.no-answer-bar #content-area {
        overflow: hidden;
    }

    /* Complete screen scaling */
    .complete-container h1 {
        font-size: 22px !important;
        margin: 0 !important;
    }

    .complete-container img {
        width: 70px !important;
    }

    .stats-display {
        padding: 8px 16px !important;
    }

    .stats-display div:first-child {
        font-size: 24px !important;
    }
}

/* Hide keyboard labels and desktop hints on touch devices */
@media (hover: none) and (pointer: coarse), (max-width: 1024px) {
    .key-label {
        display: none;
    }

    .nav-hint {
        display: none;
    }
}

/* Touch-specific enhancements */
@media (hover: none) and (pointer: coarse) {
    /* Remove hover effects on touch devices */
    .answer-btn:hover,
    .nav-btn:hover,
    .btn:hover,
    .mode-card:hover {
        transform: none;
        box-shadow: initial;
    }

    /* Add active/pressed states for touch feedback */
    .answer-btn:active:not(.disabled) {
        transform: scale(0.95);
        background: #f0f0f0;
    }

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

    .mode-card:active {
        transform: scale(0.98);
    }

    /* Increase touch target padding */
    button {
        padding: 12px 20px;
    }

    /* Speed up only UI transition animations, not game animations */
    .nav-btn, .btn, .mode-card, .answer-btn {
        transition-duration: 0.15s !important;
    }

    /* Smoother scrolling */
    #content-area {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }
}

/* Hide scrollbars on mobile for cleaner look */
@media (max-width: 768px) {
    #content-area::-webkit-scrollbar {
        width: 4px;
    }

    #content-area::-webkit-scrollbar-thumb {
        background: rgba(0,0,0,0.2);
        border-radius: 2px;
    }

    .adventure-list::-webkit-scrollbar {
        width: 4px;
    }

    .adventure-list::-webkit-scrollbar-thumb {
        background: rgba(0,0,0,0.2);
        border-radius: 2px;
    }
}

/* Prevent text selection on touch devices */
@media (hover: none) {
    .answer-btn,
    .nav-btn,
    .btn,
    .mode-card {
        -webkit-user-select: none;
        user-select: none;
        -webkit-tap-highlight-color: transparent;
    }
}

/* Safe area insets for notched devices (iOS) */
@supports (padding: env(safe-area-inset-top)) {
    body {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
}
