/* Desktop background and game centering */
@media screen and (min-width: 721px) {
    body {
        position: relative;
        min-height: 100vh;
        min-height: 100dvh;
        box-sizing: border-box;
        justify-content: center;

        background-color: transparent;
    }

    .game-presentation {
        flex-shrink: 0;
    }

    .game-container {
        flex-shrink: 0;
        margin: 0 auto;
    }
}

@media screen and (orientation: landscape) and (max-height: 600px) and (hover: none) {
    body {
        margin: 0;
        overflow: hidden;
    }

    h1 {
        display: none !important;
    }

    .game-container {
        width: min(100vw, 150dvh);
        height: auto;
        aspect-ratio: 3 / 2;
        margin: 0 auto;
        border-radius: 0;
    }

    .mobile-controls-container {
        display: flex !important;
    }
}

/* Mobile and tablet: landscape touch controls */
@media screen and (orientation: landscape) and (pointer: coarse) {
    .turn-device-overlay {
        display: none !important;
    }

    .game-container {
        width: min(100vw, 150dvh);
        height: auto;
        aspect-ratio: 3 / 2;
        margin: 0 auto;
    }

    .mobile-controls-container {
        display: flex !important;
    }
}

/* Prevent touch buttons from appearing on desktop */
@media screen and (pointer: fine) {
    .mobile-controls-container {
        display: none !important;
    }
}

/* Mobile landscape UI */
@media screen and (orientation: landscape) and (max-height: 600px) {

    /* Menu button */
    .game-menu {
        top: 8px;
        right: 20px;
    }

    /* Touch controls */
    .mobile-controls-container {
        bottom: 12px;
        padding: 0 clamp(20px, 4vw, 40px);
    }

    .touch-btn {
        width: 54px;
        height: 54px;
        font-size: 10px;
        flex-shrink: 0;
    }

    .touch-group-left,
    .touch-group-right {
        gap: 20px;
    }

    /* Start button */
    .western-start-button {
        min-width: 142px;
        padding: 8px 16px;
        font-size: clamp(14px, 3.6vh, 18px);
    }

    .start-screen-layout {
        padding: 0 22px 20px;
    }

    /* Win / Lose buttons */
    .end-buttons-group {
        gap: 12px;
        max-width: 90%;
    }

    .end-revive-row,
    .end-secondary-row {
        gap: 10px;
        max-width: 100%;
    }

    .end-secondary-row .western-ending-button {
        min-width: 0;
        width: min(42vw, 150px);
    }

    #retryLevelBtn {
        max-width: 100%;
        padding: 9px 14px;
        font-size: clamp(12px, 3.4vh, 16px);
    }

    .western-ending-button {
        min-height: 40px;
        padding: 7px 11px;
        font-size: clamp(12px, 3vh, 14px);
        white-space: nowrap;
    }

    #restartScreen:has(.end-credits:not(.d-none)) {
        padding: 0 14px 12px;
    }

    .credit-entry {
        width: 100%;
        font-size: clamp(14px, 4.1vh, 22px);
    }

    .credits-copy {
        inset: 20% 16% 19%;
    }

    .credit-role {
        font-size: 0.46em;
        letter-spacing: 0.12em;
    }

    .credit-note {
        font-size: 0.58em;
    }

    /* Hide touch controls outside gameplay */
    .game-container:has(#startScreen:not(.d-none)) .mobile-controls-container,
    .game-container:has(#restartScreen:not(.d-none)) .mobile-controls-container {
        display: none !important;
    }

    /* Keep the close button fixed while only How to Play content scrolls. */
    #infoDialog .dialog-card {
        width: min(94%, 520px);
        max-height: calc(100% - 18px);
        padding: 12px 18px;
    }

    #infoDialog .dialog-card h2 {
        margin-bottom: 7px;
        font-size: 20px;
    }

    #infoDialog .dialog-content h3 {
        margin: 7px 0 3px;
        font-size: 16px;
    }

    #infoDialog .dialog-content p {
        margin: 3px 0;
        font-size: 13px;
    }

    #infoDialog .guide-icon,
    #infoDialog .guide-icon-pair {
        width: 24px;
        height: 24px;
        flex-basis: 24px;
    }

    #infoDialog .guide-icon-pair .guide-icon,
    #infoDialog .guide-copy-with-icon .guide-icon {
        width: 20px;
        height: 20px;
    }

    .legal-card {
        max-height: 85%;
        padding: 12px 18px;
    }

    .legal-content {
        font-size: 12px;
    }

    .bgm-menu {
        top: 8px;
        left: 12px;
    }

    .sound-toggle-btn {
        top: 8px;
        right: 110px;
        width: 36px;
        height: 36px;
    }

    .game-menu summary,
    .bgm-menu summary {
        width: 82px;
        min-width: 82px;
        height: 36px;
        padding: 0 10px;
        font-size: 13px;
        border-width: 2px;
        border-radius: 10px;
    }
}

/* Shared phone/tablet touch layout. This overrides pointer:fine on hybrid tablets. */
.game-presentation:fullscreen {
    width: 100vw;
    height: 100dvh;
    min-height: 100dvh;
    max-width: none;
    box-sizing: border-box;
    overflow: hidden;
    background-image: url('../img/9_intro_outro_screens/start/intro-backdrop.webp');
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
}

/* Rotate hint only on touch devices without hover (phones, tablets) held upright */
@media screen and (orientation: portrait) and (hover: none) {
    .turn-device-overlay {
        display: flex !important;
    }

    h1,
    .game-container,
    .mobile-controls-container {
        display: none !important;
    }
}

@media screen and (orientation: landscape) and (any-pointer: coarse) {
    :root {
        --touch-button-size: clamp(56px, 10vw, 68px);
        --touch-button-gap: clamp(10px, 2.5dvh, 16px);
        --touch-edge-space: max(12px, env(safe-area-inset-left), env(safe-area-inset-right));
        --touch-bottom-space: max(16px, env(safe-area-inset-bottom));
        --touch-canvas-clearance: 8px;
        --touch-control-zone: calc(
            var(--touch-button-size) + var(--touch-edge-space) + var(--touch-canvas-clearance)
        );
    }

    body {
        min-height: 100dvh;
        overflow: hidden;
        justify-content: center;
    }

    .game-presentation {
        width: 100%;
        min-height: 100dvh;
        box-sizing: border-box;
        justify-content: center;
    }

    .game-presentation h1 {
        display: none;
    }

    .game-container {
        width: min(
            calc(100vw - (var(--touch-control-zone) * 2)),
            150dvh
        );
        height: auto;
        aspect-ratio: 3 / 2;
        margin: 0 auto;
        border-radius: 0;
    }

    .mobile-controls-container {
        display: flex !important;
        padding: 0 var(--touch-edge-space) var(--touch-bottom-space);
    }

    .touch-group-left,
    .touch-group-right {
        gap: var(--touch-button-gap);
    }

    .touch-group-right {
        align-items: flex-end;
        gap: clamp(12px, 2.5dvh, 18px);
    }

    .touch-group-left {
        flex-direction: row;
    }

    .touch-btn {
        width: var(--touch-button-size);
        height: var(--touch-button-size);
        font-size: clamp(10px, 2.5dvh, 14px);
        flex-shrink: 0;
    }

    /* Keep the right edge anchored while the action targets extend left. */
    #btnThrow,
    #btnJump {
        width: calc(var(--touch-button-size) + var(--touch-button-size));
        border-radius: 999px;
    }

    body:has(#startScreen:not(.d-none)) .mobile-controls-container,
    body:has(#restartScreen:not(.d-none)) .mobile-controls-container {
        display: none !important;
    }
}

/* Phone landscape: keep top controls in the side space beside the 3:2 game. */
@media screen and (orientation: landscape) and (any-pointer: coarse) and
    (max-height: 599px) {
    :root {
        --phone-top-edge: max(6px, env(safe-area-inset-left), env(safe-area-inset-right));
        --phone-top-button-width: 66px;
        --phone-top-button-height: 36px;
        --phone-top-button-gap: 8px;
    }

    .game-container .bgm-menu,
    .game-container .game-menu,
    .game-container .sound-toggle-btn {
        position: fixed;
    }

    .game-container .bgm-menu {
        top: var(--phone-top-edge);
        left: var(--phone-top-edge);
        z-index: 70;
        transform: none;
    }

    .game-container .game-menu {
        top: var(--phone-top-edge);
        right: var(--phone-top-edge);
        z-index: 80;
    }

    .game-container .sound-toggle-btn {
        top: calc(var(--phone-top-edge) + var(--phone-top-button-height) + var(--phone-top-button-gap));
        right: var(--phone-top-edge);
        z-index: 70;
        width: 36px;
        height: 36px;
    }

    .game-container .game-menu > summary,
    .game-container .bgm-menu > summary {
        width: var(--phone-top-button-width);
        min-width: var(--phone-top-button-width);
        height: var(--phone-top-button-height);
        min-height: var(--phone-top-button-height);
        padding: 0 4px;
        font-size: 12px;
    }

    .game-container .game-menu .top-nav-bar {
        z-index: 81;
        max-height: calc(100dvh - var(--phone-top-edge) - var(--phone-top-button-height) - 12px);
    }
}

@media (hover: none) and (pointer: coarse) {
    .start-controls-guide {
        display: none;
    }
}

/* Larger touch screens enlarge this same layout and keep both groups at the bottom. */
@media screen and (orientation: landscape) and (any-pointer: coarse) and (min-width: 721px) and (min-height: 600px) {
    :root {
        --touch-button-size: clamp(76px, 8vw, 96px);
        --touch-button-gap: clamp(14px, 2.5dvh, 22px);
        --touch-edge-space: max(18px, env(safe-area-inset-left), env(safe-area-inset-right));
        --touch-bottom-space: max(22px, env(safe-area-inset-bottom));
        --touch-control-zone: calc(var(--touch-button-size) + (var(--touch-edge-space) * 2));
        --tablet-canvas-width: min(
            calc(100vw - (var(--touch-control-zone) * 2)),
            calc(150dvh - 180px)
        );
        --tablet-canvas-height: calc(var(--tablet-canvas-width) * 0.666667);
        --tablet-canvas-edge: max(12px, calc((100vw - var(--tablet-canvas-width)) * 0.5));
        --tablet-canvas-top: calc((100dvh - var(--tablet-canvas-height)) * 0.5);
        --tablet-top-clearance: 8px;
        --tablet-menu-width: 140px;
        --tablet-menu-height: 58px;
        --tablet-top-edge: min(
            32px,
            max(0px, calc(var(--tablet-canvas-top) - var(--tablet-top-clearance) - var(--tablet-menu-height)))
        );
        --tablet-top-gap: 12px;
    }

    .game-presentation {
        gap: 20px;
        padding: 16px 0;
    }

    .game-presentation h1 {
        display: block;
    }

    .game-container {
        width: var(--tablet-canvas-width);
    }

    .game-container .bgm-menu,
    .game-container .game-menu,
    .game-container .sound-toggle-btn {
        position: fixed;
    }

    .game-container .bgm-menu {
        top: var(--tablet-top-edge);
        left: var(--tablet-canvas-edge);
        z-index: 70;
        transform: none;
    }

    .game-container .game-menu {
        top: var(--tablet-top-edge);
        right: var(--tablet-canvas-edge);
        z-index: 80;
    }

    .game-container .sound-toggle-btn {
        top: var(--tablet-top-edge);
        right: calc(var(--tablet-canvas-edge) + var(--tablet-menu-width) + var(--tablet-top-gap));
        z-index: 70;
        width: var(--tablet-menu-height);
        height: var(--tablet-menu-height);
    }

    .game-container .game-menu > summary,
    .game-container .bgm-menu > summary {
        width: var(--tablet-menu-width);
        min-width: var(--tablet-menu-width);
        height: var(--tablet-menu-height);
        min-height: var(--tablet-menu-height);
        font-size: 18px;
    }

    .game-container:has(#startScreen:not(.d-none)) .game-menu > summary,
    .game-container:has(#startScreen:not(.d-none)) .bgm-menu > summary,
    .game-container:has(#startScreen.d-none) .game-menu > summary {
        width: var(--tablet-menu-width);
        min-width: var(--tablet-menu-width);
        height: var(--tablet-menu-height);
        min-height: var(--tablet-menu-height);
        font-size: 18px;
    }

    .game-container .game-menu .top-nav-bar {
        width: min(200px, calc(100vw - 24px));
        z-index: 81;
        max-height: calc(100dvh - var(--tablet-top-edge) - var(--tablet-menu-height) - 12px);
    }

    .game-container .bgm-panel {
        width: min(224px, calc(100vw - 24px));
    }

    .game-container .game-menu .nav-icon-btn,
    .game-container .playlist-track {
        min-height: 44px;
        font-size: 16px;
    }

    /* Match the larger tablet controls with the run-wide DOM HUD. */
    .game-container #sombreroCounter {
        top: 64px;
        right: 24px;
        gap: 8px;
        max-width: 240px;
    }

    .game-container .sombrero-icon {
        width: 56px;
        height: 56px;
    }

    .game-container #coinCollectedCounter {
        top: 128px;
        right: 24px;
        gap: 8px;
        font-size: 24px;
        line-height: 42px;
    }

    .game-container #coinCollectedCounter img {
        width: 42px;
        height: 42px;
    }

    #gameContainer:not(:has(#sombreroCounter .sombrero-icon)) #coinCollectedCounter {
        top: 64px;
    }
}

/* Desktop fullscreen: maximize game canvas only */
@media screen and (pointer: fine) and (min-width: 1025px) {

    .game-presentation:fullscreen {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0;
    }

    .game-presentation:fullscreen > h1 {
        display: none;
    }

    .game-presentation:fullscreen .game-container {
        width: min(100vw, 150dvh);
        height: auto;
        aspect-ratio: 3 / 2;
        margin: 0;
        border-radius: 0;
        box-shadow: none;
    }

    .game-presentation:fullscreen .mobile-controls-container {
        display: none !important;
    }
}

/* Keep the document h1 available to assistive technology without layout space. */
.game-presentation > h1 {
    display: block !important;
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}
