* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

button {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
    transition: transform 0.08s ease, filter 0.08s ease;
}

button:focus,
button:focus-visible,
button:active {
    outline: none;
}

button:active {
    transform: scale(0.95);
    filter: brightness(0.88);
}

:root {
    --safe-area-inset-top: 0px;
    --safe-area-inset-right: 0px;
    --safe-area-inset-bottom: 0px;
    --safe-area-inset-left: 0px;
}

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    min-width: 100%;
    overflow-x: hidden;
}

body {
    color: rgba(255, 255, 255, 0.87);
    background-color: #000000;
    font-family: Inter, Arial, sans-serif;
}

#app {
    position: relative;
    width: 100%;
    min-width: 100%;
    height: 100dvh;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}

#game-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-image: url('./assets/ui/Background.webp?v=20260611');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}

#game-container-bg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
    pointer-events: none;
    z-index: 0;
}

#game-container > canvas {
    /* Let Phaser control CSS sizing; forcing 100% can break DPR rendering. */
    display: block;
    position: relative;
    z-index: 1;
}

/* HTML UI Overlay */
#game-ui {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1000;
    display: none; /* hidden by default, enabled only during gameplay */
}

/* Main Menu Overlay */
#main-menu {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    z-index: 1100;
    pointer-events: auto;
    padding: calc(var(--safe-area-inset-top, 0px) + 12px) calc(var(--safe-area-inset-right, 0px) + 16px) calc(var(--safe-area-inset-bottom, 0px) + 36px) calc(var(--safe-area-inset-left, 0px) + 16px);
}

#main-menu-center {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    width: 100%;
    justify-content: center;
    flex: 1;
    /* Reserve room at the bottom so the centered logo gets pushed upward, but keep
     * it moderate so on tall screens the logo doesn't float far above the Continue
     * button leaving a cavernous gap. */
    padding-bottom: clamp(100px, 17vh, 260px);
}

#main-menu-logo {
    width: clamp(
        calc(170px * var(--ui-scale, 1)),
        calc(42vmin * var(--ui-scale, 1)),
        calc(480px * var(--ui-scale, 1))
    );
    max-width: calc(100vw - var(--safe-area-inset-left, 0px) - var(--safe-area-inset-right, 0px) - 48px);
    max-height: calc((100dvh - var(--safe-area-inset-top, 0px) - var(--safe-area-inset-bottom, 0px)) * 0.33);
    height: auto;
    object-fit: contain;
    margin-bottom: clamp(
        calc(14px * var(--ui-scale, 1)),
        calc(7vh * var(--ui-scale, 1)),
        calc(90px * var(--ui-scale, 1))
    );
}

#main-menu-level-label {
    display: none;
}

#main-menu-level-number {
    font-size: 14px;
}

#main-menu-play-btn {
    position: absolute;
    left: calc(50% + var(--safe-area-center-offset-x, 0px));
    /* Sit a bit higher on tall screens so the logo + button read as a balanced
     * pair rather than the button being stranded at the very bottom. */
    bottom: calc(var(--safe-area-inset-bottom, 0px) + clamp(120px, 15vh, 260px));
    transform: translateX(-50%);
    margin: 0;
    z-index: 2;
}

#main-menu-settings-btn {
    position: absolute;
    top: calc(var(--safe-area-inset-top, 0px) + 12px);
    right: calc(var(--safe-area-inset-right, 0px) + 16px);
}

#copyright-logo {
    position: absolute;
    top: calc(var(--safe-area-inset-top, 0px) + 12px);
    left: calc(var(--safe-area-inset-left, 0px) + 16px);
    width: clamp(56px, 6vw, 88px);
    height: clamp(56px, 6vw, 88px);
    aspect-ratio: 1 / 1;
    object-fit: contain;
    pointer-events: none;
}

#version-label {
    position: absolute;
    bottom: calc(var(--safe-area-inset-bottom, 0px) + 10px);
    left: calc(var(--safe-area-inset-left, 0px) + 14px);
    font-size: clamp(14px, 2vmin, 20px);
    color: rgba(255, 255, 255, 0.5);
    font-weight: 600;
    pointer-events: none;
    z-index: 1;
}

.main-menu-settings-btn-lobby {
    width: clamp(44px, 4.4vw, 64px);
    height: clamp(44px, 4.4vw, 64px);
    border: none;
    padding: 0;
    background: transparent;
    cursor: pointer;
}

.main-menu-settings-icon {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.main-menu-play-btn-lobby {
    position: relative;
    width: clamp(
        calc(210px * var(--ui-scale, 1)),
        calc(40vmin * var(--ui-scale, 1)),
        calc(540px * var(--ui-scale, 1))
    );
    max-width: calc(100vw - var(--safe-area-inset-left, 0px) - var(--safe-area-inset-right, 0px) - 32px);
    border: none;
    padding: 0;
    margin-bottom: 0;
    background: transparent;
    cursor: pointer;
}

.main-menu-play-bg {
    width: 100%;
    height: auto;
    display: block;
    object-fit: contain;
}

.main-menu-play-text-wrap {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
    color: #fff;
    text-shadow: 0 2px 2px rgba(0, 0, 0, 0.45);
    font-weight: 700;
    pointer-events: none;
}

.main-menu-play-title {
    font-size: clamp(
        calc(18px * var(--ui-scale, 1)),
        calc(3.6vmin * var(--ui-scale, 1)),
        calc(44px * var(--ui-scale, 1))
    );
    line-height: 1;
}

.main-menu-play-level {
    font-size: clamp(
        calc(12px * var(--ui-scale, 1)),
        calc(2vmin * var(--ui-scale, 1)),
        calc(26px * var(--ui-scale, 1))
    );
    line-height: 1;
}

/* Portrait: scale the logo + Continue button (and their text) to the viewport so
 * they read large on phones AND on mid/large portraits (tablets, Full-HD Retina
 * windows) instead of staying tiny with a cavernous gap. Small screens hit the
 * vmin term; large screens hit the raised px caps. Landscape keeps the default
 * clamp curve above. */
@media (orientation: portrait) {
    #main-menu-logo {
        width: clamp(
            calc(240px * var(--ui-scale, 1)),
            min(70vmin, var(--safe-area-effective-width, 100vw), var(--safe-area-effective-height, 100dvh)),
            calc(600px * var(--ui-scale, 1))
        );
    }
    .main-menu-play-btn-lobby {
        /* Slightly narrower than the logo (70vmin / 600px cap) so it never
         * dominates the screen — 75vmin made it oversized on small/short
         * portraits like 540x720 where --ui-scale stays 1. The vh term caps it on
         * near-square / short portraits (e.g. 991x996) where a large vmin would
         * otherwise push the width to the full 600px and dominate vertically. */
        width: clamp(
            calc(300px * var(--ui-scale, 1)),
            min(63vmin, var(--safe-area-effective-width, 100vw), var(--safe-area-effective-height, 100dvh)),
            min(calc(600px * var(--ui-scale, 1)), var(--lobby-button-offset-max-width, 46vh))
        );
    }
    .main-menu-play-title {
        font-size: clamp(
            calc(24px * var(--ui-scale, 1)),
            calc(6.2vmin * var(--ui-scale, 1)),
            calc(52px * var(--ui-scale, 1))
        );
    }
    .main-menu-play-level {
        font-size: clamp(
            calc(15px * var(--ui-scale, 1)),
            calc(3.6vmin * var(--ui-scale, 1)),
            calc(30px * var(--ui-scale, 1))
        );
    }
}

@media (orientation: landscape) and (min-width: 800px) and (max-height: 520px) {
    #main-menu-center {
        padding-bottom: clamp(54px, 13vh, 90px);
    }

    #main-menu-logo {
        width: clamp(220px, 22vw, 320px);
        max-height: calc((100dvh - var(--safe-area-inset-top, 0px) - var(--safe-area-inset-bottom, 0px)) * 0.42);
        margin-bottom: clamp(22px, 6vh, 42px);
    }

    #main-menu-play-btn {
        bottom: calc(var(--safe-area-inset-bottom, 0px) + clamp(34px, 9vh, 62px));
    }

    .main-menu-play-btn-lobby {
        width: clamp(190px, 20vw, 280px);
    }

    .main-menu-play-title {
        font-size: clamp(18px, 2.1vw, 28px);
    }

    .main-menu-play-level {
        font-size: clamp(11px, 1.15vw, 16px);
    }
}

@media (orientation: landscape) and (min-width: 100px) and (max-width: 799px) and (max-height: 520px) {
    #main-menu-center {
        padding-bottom: clamp(54px, 13vh, 90px);
    }

    #main-menu-play-btn {
        bottom: calc(var(--safe-area-inset-bottom, 0px) + clamp(34px, 9vh, 62px));
    }
}

/* Let canvas receive pointer/drag everywhere; only buttons are clickable */
#game-ui * {
    pointer-events: none;
}

#game-ui .game-btn,
#game-ui button {
    pointer-events: auto;
}

/* While a card is being dragged, let canvas receive events over buttons too */
body.card-dragging #game-ui .game-btn,
body.card-dragging #game-ui button {
    pointer-events: none;
}

/* Moves Counter Widget (top-left) */
#moves-counter-widget {
    position: absolute;
    top: calc(var(--safe-area-inset-top, 0px) + 12px * var(--ui-scale, 1));
    left: calc(var(--safe-area-inset-left, 0px) + 12px * var(--ui-scale, 1));
    width: clamp(50px, 11vmin, 118px);
    z-index: 10;
    pointer-events: none;
}

.moves-counter-bg {
    display: block;
    width: 100%;
    height: auto;
}

.moves-counter-inner {
    position: absolute;
    inset: 0;
    padding: 0;
}

.moves-counter-label {
    position: absolute;
    top: 7%;
    left: 0;
    right: 0;
    display: block;
    max-width: 100%;
    text-align: center;
    color: rgba(255, 255, 255, 0.85);
    font-size: clamp(15px, 2.8vmin, 30px);
    font-weight: 600;
    letter-spacing: 0.3px;
    line-height: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: clip;
}

.moves-counter-value {
    position: absolute;
    left: 0;
    right: 0;
    top: 58%;
    transform: translateY(-50%);
    display: block;
    max-width: 100%;
    text-align: center;
    color: #fff;
    font-size: clamp(26px, 6.4vmin, 74px);
    font-weight: 900;
    line-height: 1;
    text-shadow: 0 2px 4px rgba(0,0,0,0.3);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: clip;
}

/* Top-right area: Level text + Settings gear */
#top-right-area {
    position: absolute;
    top: calc(var(--safe-area-inset-top, 0px) + 12px * var(--ui-scale, 1));
    right: calc(var(--safe-area-inset-right, 0px) + 12px * var(--ui-scale, 1));
    display: flex;
    align-items: center;
    gap: clamp(8px, 1.5vmin, 16px);
    z-index: 10;
}

.level-text {
    color: #6BDF91;
    font-size: clamp(14px, 2.4vmin, 22px);
    font-weight: 700;
    text-shadow: 0 2px 4px rgba(0,0,0,0.4);
    white-space: nowrap;
    pointer-events: none;
}

.gameplay-settings-btn {
    width: clamp(32px, 5.5vmin, 52px);
    height: clamp(32px, 5.5vmin, 52px);
    border: none;
    background: none;
    cursor: pointer;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease;
    pointer-events: auto;
}

.gameplay-settings-btn:hover {
    transform: scale(1.1);
}

.settings-gear-icon {
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
}

/* Win Screen Overlay — fixed + high z-index so fixed #top-bar (inside #game-ui) cannot sit above and steal clicks */
/* Keep dim fullscreen; only content frame inside respects Famobi safe-area insets. */
#win-overlay {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(15, 12, 8, 0.94);
    z-index: 4000;
    pointer-events: auto;
    touch-action: none;
    overflow: hidden;
    box-sizing: border-box;
}

.win-safe-area-frame {
    position: absolute;
    top: var(--safe-area-inset-top, 0px);
    right: var(--safe-area-inset-right, 0px);
    bottom: var(--safe-area-inset-bottom, 0px);
    left: var(--safe-area-inset-left, 0px);
    /* Allow emergency tiny-width mode to render outside a heavily squeezed safe frame. */
    overflow: visible;
    container-type: size;
}

/* Lose Screen Overlay — same safe-area frame as canvas / win */
#lose-overlay {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100dvh;
    display: none;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at top, rgba(255, 255, 255, 0.04), transparent 55%),
                radial-gradient(circle at bottom, rgba(0, 0, 0, 0.9), rgba(0, 0, 0, 0.98));
    z-index: 4000;
    pointer-events: auto;
    touch-action: none;
    box-sizing: border-box;
}

#lose-card {
    position: relative;
    width: min(75vw, 280px);
    aspect-ratio: 1 / 0.9;
    padding: 18px 24px 28px;
    background-image: url('./assets/ui/Lose/lose_popup.png?v=20260414');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-position: center;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    gap: 0;
    pointer-events: auto;
}

/* Large desktop landscape (e.g. 1920x1080): keep lose popup wider and less tall,
   closer to the compact proportions seen on ~1100x680. */
@media (orientation: landscape) and (min-width: 1600px) and (min-height: 900px) {
    #lose-card {
        width: min(32vw, 360px);
        aspect-ratio: 1 / 0.80;
        padding: 16px 24px 20px;
    }

    #lose-title {
        font-size: clamp(22px, 2.4vmin, 30px);
    }

    #lose-subtitle {
        font-size: clamp(14px, 1.45vmin, 18px);
        max-width: 82%;
        line-height: 1.28;
    }

    .lose-buttons .game-btn {
        height: 50px;
        min-height: 50px;
        padding: 0 14px;
        font-size: 18px;
    }

    #lose-restart-btn {
        width: 172px;
        min-width: 172px;
    }

    #lose-watch-ad-moves-btn,
    #lose-use-joker-btn {
        width: 138px;
        min-width: 138px;
    }

    #lose-card.lose-card--with-reward #lose-restart-btn {
        width: 52px;
        min-width: 52px;
        height: 52px;
        min-height: 52px;
    }

    #lose-card.lose-card--with-reward .lose-restart-icon {
        width: 24px;
        height: 24px;
    }
}

.lose-buttons {
    display: flex;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
    gap: 12px;
    margin-top: auto;
    width: 100%;
}

.lose-buttons .game-btn {
    height: 64px;
    min-height: 64px;
    padding: 0 20px;
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-position: center;
    font-size: 26px;
    font-weight: 700;
    line-height: 1;
    letter-spacing: 0;
    text-align: center;
    white-space: nowrap;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
}

#lose-title {
    margin-top: 0;
    font-size: clamp(20px, 3.4vmin, 34px);
    font-weight: 800;
    letter-spacing: 0.01em;
    text-transform: none;
    color: #822B14;
    text-shadow: none;
    max-width: 100%;
    word-break: break-word;
}

#lose-subtitle {
    max-width: 85%;
    font-size: clamp(16px, 2.4vmin, 26px);
    font-weight: 400;
    color: #685550;
    text-shadow: none;
    line-height: 1.35;
    margin: auto 0;
    word-break: break-word;
}

.lose-restart-icon {
    width: 24px;
    height: 24px;
    object-fit: contain;
    pointer-events: none;
    flex-shrink: 0;
}

.lose-restart-text {
    display: block;
    min-width: 0;
    max-width: calc(100% - 20px);
    white-space: nowrap;
    line-height: 1;
}

#lose-restart-btn {
    background-image: url('./assets/ui/Lose/restart_big_button.png');
    width: 200px;
    min-width: 200px;
}

#lose-restart-btn .lose-restart-icon {
    width: 28px;
    height: 28px;
}

#lose-watch-ad-moves-btn,
#lose-use-joker-btn {
    background-image: url('./assets/ui/Lose/ads_button.png');
    width: 160px;
    min-width: 160px;
    padding: 0 8px;
}

.lose-btn-label {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    white-space: nowrap;
    line-height: 1;
    pointer-events: none;
}

.lose-buttons .lose-reward-ads-badge {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 30px;
    height: 30px;
    pointer-events: none;
    z-index: 3;
}

#lose-card.lose-card--with-reward #lose-restart-btn {
    background-image: url('./assets/ui/Lose/restart_small_button.png');
    width: 60px;
    min-width: 60px;
    height: 60px;
    min-height: 60px;
    padding: 0;
    border-radius: 50%;
}

#lose-card.lose-card--with-reward .lose-restart-text {
    display: none;
}

#lose-card.lose-card--with-reward .lose-restart-icon {
    width: 28px;
    height: 28px;
}

/* Final override (placed after base lose styles): 1920x1080-ish desktop should
   match compact 1100x700 proportions, with wider yellow action button. */
@media (orientation: landscape) and (min-width: 1600px) and (min-height: 900px) {
    #lose-card {
        width: min(32vw, 360px);
        aspect-ratio: 1 / 0.74;
        padding: 16px 24px 20px;
    }

    #lose-title {
        font-size: clamp(24px, 2.8vmin, 34px);
    }

    #lose-subtitle {
        font-size: clamp(17px, 1.95vmin, 23px);
        max-width: 82%;
        line-height: 1.32;
    }

    #lose-card .lose-buttons .game-btn {
        height: 66px;
        min-height: 66px;
        padding: 0 18px;
        font-size: 24px;
    }

    #lose-card.lose-card--with-reward .lose-buttons {
        width: 100%;
        gap: 8px;
        justify-content: space-between;
    }

    #lose-card.lose-card--with-reward #lose-restart-btn {
        width: 70px;
        min-width: 70px;
        height: 70px;
        min-height: 70px;
        flex: 0 0 auto;
    }

    #lose-card.lose-card--with-reward #lose-watch-ad-moves-btn,
    #lose-card.lose-card--with-reward #lose-use-joker-btn {
        width: auto;
        min-width: 0;
        flex: 1 1 auto;
    }

    #lose-card.lose-card--with-reward .lose-restart-icon {
        width: 33px;
        height: 33px;
    }
}

.win-confetti-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}

#win-card {
    position: relative;
    z-index: 1;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    min-height: 0;
    padding: clamp(10px, 2.4vmin, 22px) 12px clamp(12px, 2.8vmin, 26px);
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    gap: 0;
    pointer-events: auto;
    transform-origin: center center;
    /*
     * Fit inside Famobi insets: use frame÷(frame+insets) so we match the same math as main.ts
     * (100cqh/100dvh can disagree with env when dvh ≠ layout viewport).
     * We only use this as a size multiplier for child layout values (not transform on #win-card),
     * so offsets move the popup by the expected amount instead of being visually over-amplified.
     */
    /* Default fallback for browsers that mis-handle container query units. */
    --win-cqh: 100vh;
    --win-cqw: 100vw;
    --win-r-v: calc(
        var(--win-cqh) / (var(--win-cqh) + var(--safe-area-inset-top, 0px) + var(--safe-area-inset-bottom, 0px))
    );
    /*
     * Horizontal insets already shift the safe-area frame itself.
     * We apply horizontal ratio only as a *soft* size factor (not transform-position),
     * so left/right offsets still work normally while extreme L+R can shrink content enough to remain visible.
     */
    --win-r-h: calc(
        var(--win-cqw) / (var(--win-cqw) + var(--safe-area-inset-left, 0px) + var(--safe-area-inset-right, 0px))
    );
    --win-fit-scale-v: calc(var(--win-r-v) * (0.78 + 0.22 * var(--win-r-v)));
    --win-fit-scale-h: calc(0.72 + 0.28 * var(--win-r-h));
    --win-fit-scale: min(1, calc(var(--win-fit-scale-v) * var(--win-fit-scale-h)));
    --win-layout-scale: 1;
    --win-size-scale: calc(var(--win-fit-scale) * var(--win-layout-scale));
    --win-victory-text-mul: var(--win-text-mul);
    /*
     * Layout can shrink hard with safe-area; raw --win-size-scale on font made labels microscopic.
     * Floor text so it stays readable; still trends smaller when offsets are large.
     */
    --win-text-mul: max(0.76, min(1, var(--win-size-scale, 1)));
}

/* Prefer container query units when supported (stable in Chromium; sometimes flaky in Firefox). */
@supports (width: 1cqw) and (height: 1cqh) {
    #win-card {
        /*
         * If 100cqh/100cqw are 0 before layout (container not sized), ratios became 0 and --win-size-scale
         * wiped all win UI text. Floor the query size so ratios stay sane (same intent as main.ts).
         */
        --win-cqh: max(1px, 100cqh);
        --win-cqw: max(1px, 100cqw);
    }
}

.win-top-cluster {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    flex-shrink: 0;
    padding-top: 0.25vmin;
}

.win-level-text {
    font-size: clamp(calc(16px * var(--win-size-scale, 1)), calc(4.8vmin * var(--win-size-scale, 1)), calc(48px * var(--win-size-scale, 1)));
    font-weight: 700;
    color: #FFF9D3;
    -webkit-text-stroke: 2px #000;
    paint-order: stroke fill;
    text-shadow: 0 2px 6px rgba(0, 0, 0, 0.7);
    margin: 0 0 -1.2vmin;
    z-index: 1;
}

.win-ribbon-wrap {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: min(calc(96% * var(--win-size-scale, 1)), calc(90vmin * var(--win-size-scale, 1)));
    max-width: calc(920px * var(--win-size-scale, 1));
    margin-top: 0;
    flex-shrink: 0;
    transform: translateZ(0);
}

/* Ribbon “unwraps” from the center toward the sides when the win overlay opens */
#win-overlay.win-overlay--visible .win-ribbon-wrap {
    animation: winRibbonUnwrap 0.9s cubic-bezier(0.22, 0.82, 0.24, 1) both;
}

@keyframes winRibbonUnwrap {
    from {
        clip-path: inset(0 50% 0 50%);
        opacity: 0.88;
    }

    to {
        clip-path: inset(0 0 0 0);
        opacity: 1;
    }
}

@media (prefers-reduced-motion: reduce) {
    #win-overlay.win-overlay--visible .win-ribbon-wrap {
        animation: none;
        clip-path: none;
        opacity: 1;
    }

    #win-overlay.win-overlay--visible .win-central-layer--shine {
        animation: none;
    }

    #win-overlay.win-overlay--visible .win-central-layer--vip-wrap {
        animation: none;
        transform: translate(-50%, -50%) scaleX(1);
    }

    #win-overlay.win-overlay--visible .win-star-slice {
        animation: none;
        transform: none;
        opacity: 1;
    }
}

.win-ribbon-img {
    width: 100%;
    height: auto;
    display: block;
}

.win-ribbon-text {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 0;
    overflow: hidden;
    /* Viewport-based like moves counter; --win-text-mul softens with offsets without vanishing. */
    font-size: clamp(
        calc(15px * var(--win-text-mul)),
        calc(min(3vmin, 3.6vw) * var(--win-text-mul)),
        calc(48px * var(--win-text-mul))
    );
    font-weight: 800;
    color: #FFF9D3;
    -webkit-text-stroke: 1.5px #102886;
    paint-order: stroke fill;
    text-shadow:
        0 3px 6px rgba(0, 0, 0, 0.5),
        0 0 16px rgba(255, 255, 255, 0.2);
    letter-spacing: 0.02em;
    white-space: nowrap;
    padding-inline: clamp(6px, 1.6vmin, 20px);
    text-transform: uppercase;
    pointer-events: none;
}

/*
 * Layered win art (replaces central_image.png composite).
 * Design canvas matches former central_image.png / shine.png: 1562×1668.
 */
.win-central-stack {
    position: relative;
    width: min(calc(82vmin * var(--win-size-scale, 1)), calc(90% * var(--win-size-scale, 1)));
    max-width: 95%;
    margin: 0 0 0 calc(1.2vmin * var(--win-size-scale, 1));
    flex: 1 1 auto;
    min-height: 0;
    aspect-ratio: 1562 / 1668;
    pointer-events: none;
}

/*
 * Landscape: the stack is width-bound (82vmin), so on a wide-but-short viewport the
 * card grew tall enough to overlap the Continue button — worst on ~16:9 laptops like
 * 1600x900 / 1536x864 (which the old layout-scale breakpoints missed entirely, since
 * they were >1500px wide / >860px tall). Cap the width by viewport height too so the
 * card always shrinks proportionally to the vertical room and the button keeps its
 * clearance on every landscape. On tall/normal screens 82vmin still wins.
 */
@media (orientation: landscape) {
    .win-central-stack {
        width: min(
            calc(82vmin * var(--win-size-scale, 1)),
            calc(90% * var(--win-size-scale, 1)),
            calc(54vh * var(--win-size-scale, 1))
        );
    }

    /* Trim the COMPLETED! ribbon a touch on landscape so it reads smaller and stays
     * balanced with the now height-capped central card. */
    .win-ribbon-wrap {
        width: min(calc(84% * var(--win-size-scale, 1)), calc(72vmin * var(--win-size-scale, 1)));
        max-width: calc(760px * var(--win-size-scale, 1));
    }
}

@media (orientation: portrait) {
    .win-central-stack {
        flex: 1 1 auto;
        min-height: 0;
        height: auto;
        /*
         * Cards are width-bound (82vmin) and then visually enlarged by scale(1.14),
         * which the flex layout does not account for. On a less-tall portrait that made
         * the cards overflow downward and crowd the Continue button. Cap the width by the
         * available viewport height too, so the cards shrink proportionally on short
         * portraits and the button keeps its clearance. On tall portraits 82vmin wins and
         * nothing changes.
         */
        width: min(
            calc(82vmin * var(--win-size-scale, 1)),
            calc(90% * var(--win-size-scale, 1)),
            calc(46vh * var(--win-size-scale, 1))
        );
        max-width: 95%;
        transform: scale(1.14);
        transform-origin: center center;
    }
}

.win-central-layer {
    position: absolute;
    display: block;
    pointer-events: none;
}

/* Back → front: shine, stars, then cards on top */
.win-central-layer--shine {
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    object-fit: contain;
    z-index: 1;
    transform-origin: center center;
}

/* Slow continuous spin behind the cards (same asset center) */
#win-overlay.win-overlay--visible .win-central-layer--shine {
    animation: winShineRotate 22s linear infinite;
}

/*
 * star.png (1423×1130) split into a 2×2 grid; each quadrant pulses on its own schedule (JS sets --star-pulse-*).
 */
.win-star-field {
    position: absolute;
    width: calc(100% * 1423 / 1562);
    aspect-ratio: 1423 / 1130;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%) translate(-2.2vmin, -9.4vmin);
    z-index: 2;
}

.win-star-slice {
    position: absolute;
    background-image: url('./assets/ui/Win/star.png');
    background-repeat: no-repeat;
    width: 50%;
    height: 50%;
    background-size: 200% 200%;
    transform-origin: center center;
    will-change: transform, opacity;
}

.win-star-slice:nth-child(1) {
    left: 0;
    top: 0;
    background-position: 0% 0%;
}

.win-star-slice:nth-child(2) {
    left: 50%;
    top: 0;
    background-position: 100% 0%;
}

.win-star-slice:nth-child(3) {
    left: 0;
    top: 50%;
    background-position: 0% 100%;
}

.win-star-slice:nth-child(4) {
    left: 50%;
    top: 50%;
    background-position: 100% 100%;
}

#win-overlay.win-overlay--visible .win-star-slice {
    animation: winStarPulse var(--star-pulse-duration, 2s) ease-in-out infinite;
    animation-delay: var(--star-pulse-delay, 0s);
}

/* Freeze win popup animations while platform/game pause is active. */
body.platform-paused #win-overlay.win-overlay--visible .win-ribbon-wrap,
body.platform-paused #win-overlay.win-overlay--visible .win-central-layer--shine,
body.platform-paused #win-overlay.win-overlay--visible .win-central-layer--vip-wrap,
body.platform-paused #win-overlay.win-overlay--visible .win-star-slice {
    animation-play-state: paused !important;
}

@keyframes winStarPulse {

    0%,
    100% {
        transform: scale(1);
        opacity: 1;
    }

    50% {
        transform: scale(1.14);
        opacity: 0.3;
    }
}

.win-central-layer--green {
    width: calc(100% * 871 / 1562);
    height: auto;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 3;
}

.win-central-layer--vip-wrap {
    width: calc(100% * 533 / 1562);
    height: auto;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 4;
}

.win-central-layer--vip-img {
    width: 100%;
    height: auto;
    display: block;
}

.win-victory-text {
    position: absolute;
    left: 9%;
    right: 9%;
    bottom: 8%;
    height: 26%;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    box-sizing: border-box;
    padding: 0 clamp(6px, 1.8vmin, 14px);
    overflow: hidden;
    /* Slightly smaller than ribbon; same mul floor so it stays legible. */
    font-size: clamp(
        calc(13px * var(--win-victory-text-mul)),
        calc(min(2.4vmin, 2.9vw) * var(--win-victory-text-mul)),
        calc(40px * var(--win-victory-text-mul))
    );
    font-weight: 800;
    line-height: 1;
    color: #4a3428;
    -webkit-text-stroke: 1px rgba(255, 248, 220, 0.35);
    paint-order: stroke fill;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
    pointer-events: none;
    overflow-wrap: anywhere;
    white-space: nowrap;
    word-break: normal;
    hyphens: auto;
}

/*
 * Face reveal: matches BaseCardView.showCardRevealAnimation — scaleX to 0 then back to 1 (170ms + 170ms, linear).
 * Runs when the win overlay is shown.
 */
#win-overlay.win-overlay--visible .win-central-layer--vip-wrap {
    animation: winVipCardFlip 340ms linear both;
}

@keyframes winShineRotate {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

@keyframes winVipCardFlip {
    0%,
    50% {
        transform: translate(-50%, -50%) scaleX(0);
    }

    100% {
        transform: translate(-50%, -50%) scaleX(1);
    }
}

.win-continue-btn-new {
    position: relative;
    width: min(calc(54vmin * var(--win-size-scale, 1)), calc(92% * var(--win-size-scale, 1)));
    max-width: calc(500px * var(--win-size-scale, 1));
    min-width: calc(180px * var(--win-size-scale, 1));
    border: none;
    padding: 0;
    background: transparent;
    cursor: pointer;
    transition: transform 0.15s ease, filter 0.15s ease;
    margin-top: auto;
    margin-bottom: clamp(12px, 3vmin, 32px);
    flex-shrink: 0;
}

.win-continue-bg {
    width: 100%;
    height: auto;
    display: block;
}

.win-continue-text {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    transform: translateY(-6%);
    font-size: clamp(calc(14px * var(--win-size-scale, 1)), calc(3.5vmin * var(--win-size-scale, 1)), calc(40px * var(--win-size-scale, 1)));
    font-weight: 700;
    color: #FEF9E8;
    -webkit-text-stroke: 2px rgba(0, 0, 0, 0.5);
    paint-order: stroke fill;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.45);
    pointer-events: none;
}

.win-continue-btn-new:hover {
    transform: translateY(-2px);
    filter: brightness(1.1);
}

.win-continue-btn-new:active {
    transform: translateY(0);
    filter: brightness(0.95);
}

/*
 * If both left+right insets are extreme, safe-frame width can collapse.
 * In that case, render win-card at viewport-based width and center it,
 * so user still sees a small but readable popup instead of a vertical line.
 */
@container (max-width: 420px) {
    #win-card {
        width: min(84vw, 320px);
        left: 50%;
        transform: translateX(-50%);
    }
}

@media (orientation: landscape) and (min-width: 700px) and (max-width: 1500px) and (max-height: 860px) {
    #win-card {
        --win-layout-scale: 0.9;
    }
}

@media (orientation: landscape) and (min-width: 700px) and (max-width: 1500px) and (max-height: 720px) {
    #win-card {
        --win-layout-scale: 0.84;
    }
}

@media (max-width: 640px) {
    .win-central-stack {
        width: min(calc(86vmin * var(--win-size-scale, 1)), calc(94% * var(--win-size-scale, 1)));
        max-width: 100%;
    }

    .win-continue-btn-new {
        width: min(calc(64vmin * var(--win-size-scale, 1)), calc(94% * var(--win-size-scale, 1)));
        max-width: calc(440px * var(--win-size-scale, 1));
        min-width: 0;
    }

    .win-continue-text {
        font-size: clamp(calc(14px * var(--win-size-scale, 1)), calc(5.6vmin * var(--win-size-scale, 1)), calc(36px * var(--win-size-scale, 1)));
    }
}

@media (orientation: landscape) and (max-height: 360px), (max-width: 360px) and (max-height: 520px) {
    #win-card {
        --win-victory-text-mul: max(0.46, min(1, var(--win-size-scale, 1)));
        padding-bottom: clamp(6px, 1.4vmin, 12px);
    }

    .win-victory-text {
        left: 5%;
        right: 5%;
        bottom: 6%;
        height: 30%;
        padding-inline: 2px;
        font-size: clamp(
            calc(7px * var(--win-victory-text-mul)),
            calc(min(2vmin, 2.4vw) * var(--win-victory-text-mul)),
            calc(24px * var(--win-victory-text-mul))
        );
        line-height: 0.92;
        -webkit-text-stroke-width: 0.5px;
        word-break: break-word;
    }

    .win-continue-btn-new {
        margin-bottom: clamp(2px, 0.8vmin, 8px);
    }
}

/* Top Bar – legacy selectors kept for TS compatibility */
#top-bar {
    display: none;
}

/* Bottom Panel: fixed to viewport bottom */
#bottom-panel {
    position: fixed;
    bottom: calc(8px + var(--safe-area-inset-bottom, 0px));
    left: calc(50% + var(--safe-area-center-offset-x, 0px));
    height: clamp(76px, 10vmin, 94px);
    width: clamp(160px, 24vmin, 250px);
    transform: translateX(-50%);
    transform-origin: 50% 100%;
    /* The panel is a DOM overlay above the Phaser canvas. It must catch input across
       transparent gaps so cards underneath cannot be clicked or dragged through boosters. */
    pointer-events: auto;
    z-index: 1000;
}

#bottom-panel .game-btn {
    pointer-events: auto;
}

.panel-background {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: fill;
    object-position: center;
}

.panel-buttons {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: clamp(8px, 1.9vmin, 18px);
    z-index: 1;
}

.game-btn {
    position: relative;
    width: clamp(54px, 7.8vmin, 72px);
    height: clamp(54px, 7.8vmin, 72px);
    border: none;
    background: none;
    color: white;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.game-btn:hover {
    transform: translateY(-2px);
    filter: brightness(1.15);
}

.game-btn:active {
    transform: translateY(0);
    filter: brightness(0.9);
}

.btn-bg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
}

.btn-icon {
    position: relative;
    width: 55%;
    height: 55%;
    object-fit: contain;
    pointer-events: none;
    z-index: 1;
}

/* Slightly larger undo arrow in gameplay button. */
#bottom-undo-btn .btn-icon {
    width: 80%;
    height: 80%;
}

.booster-counter-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 40%;
    height: 40%;
    pointer-events: none;
    z-index: 2;
}

.booster-counter-bg {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.booster-counter-count {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #f6f5ef;
    font-weight: 800;
    font-size: clamp(10px, 1.8vmin, 18px);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.55);
}

.booster-ads-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 40%;
    height: 40%;
    object-fit: contain;
    pointer-events: none;
    z-index: 2;
}

.is-hidden {
    display: none !important;
}

/* Responsive – small screens */
@media (max-width: 768px) {
    #moves-counter-widget {
        top: calc(var(--safe-area-inset-top, 0px) + 8px * var(--ui-scale, 1));
        left: calc(var(--safe-area-inset-left, 0px) + 8px * var(--ui-scale, 1));
    }
    #top-right-area {
        top: calc(var(--safe-area-inset-top, 0px) + 8px * var(--ui-scale, 1));
        right: calc(var(--safe-area-inset-right, 0px) + 8px * var(--ui-scale, 1));
    }
}

/* Effective short viewport (e.g. top/bottom banner offsets) – same layout as landscape mobile */
/* Narrow portrait / Famobi offset test: shrink only HTML HUD controls, never gameplay cards. */
@media (max-width: 500px) {
    #moves-counter-widget {
        left: calc(min(var(--safe-area-inset-left, 0px), 32vw) + 8px * var(--ui-scale, 1));
        width: clamp(31px, 14vw, 68px);
    }

    #top-right-area {
        right: calc(min(var(--safe-area-inset-right, 0px), 32vw) + 8px * var(--ui-scale, 1));
    }

    .moves-counter-label {
        top: 8%;
        font-size: clamp(6px, 2.8vw, 13px);
    }

    .moves-counter-value {
        top: 59%;
        font-size: clamp(10px, 5.8vw, 24px);
    }

    #bottom-panel {
        height: clamp(86px, 18vw, 104px);
        width: clamp(195px, 48vw, 238px);
    }

    .panel-buttons {
        gap: clamp(4px, 1.5vw, 8px);
    }

    #bottom-panel .game-btn {
        width: clamp(52px, 12.5vw, 66px);
        height: clamp(52px, 12.5vw, 66px);
    }

    #bottom-panel .booster-counter-count {
        font-size: clamp(8px, 2.2vw, 11px);
    }

    body.layout-hud-heavy-offsets #bottom-panel {
        height: clamp(66px, 15vw, 78px);
        width: clamp(136px, 32vw, 168px);
    }

    body.layout-hud-heavy-bottom-offsets #bottom-panel {
        height: clamp(62px, 13.5vw, 72px);
        width: clamp(128px, 29vw, 158px);
    }

    body.layout-hud-heavy-top-offsets #bottom-panel {
        height: clamp(54px, 11.5vw, 64px);
        width: clamp(112px, 26vw, 140px);
    }

    body.layout-hud-vertical-constrained #bottom-panel {
        height: clamp(52px, 11vw, 60px);
        width: clamp(96px, 22vw, 124px);
    }

    body.layout-hud-heavy-offsets #moves-counter-widget {
        width: clamp(20px, 9vw, 46px);
    }

    body.layout-hud-heavy-offsets .moves-counter-label {
        font-size: clamp(4px, 1.55vw, 7px);
    }

    body.layout-hud-heavy-offsets .moves-counter-value {
        font-size: clamp(9px, 3.8vw, 17px);
    }

    body.layout-hud-heavy-offsets .panel-buttons {
        gap: clamp(4px, 1.2vw, 7px);
    }

    body.layout-hud-heavy-offsets #bottom-panel .game-btn {
        width: clamp(46px, 10.8vw, 56px);
        height: clamp(46px, 10.8vw, 56px);
    }

    body.layout-hud-heavy-bottom-offsets #bottom-panel .game-btn {
        width: clamp(44px, 10.2vw, 54px);
        height: clamp(44px, 10.2vw, 54px);
    }

    body.layout-hud-heavy-top-offsets #bottom-panel .game-btn {
        width: clamp(40px, 9.4vw, 50px);
        height: clamp(40px, 9.4vw, 50px);
    }

    body.layout-hud-vertical-constrained #bottom-panel .game-btn {
        width: clamp(38px, 8.8vw, 48px);
        height: clamp(38px, 8.8vw, 48px);
    }

    body.layout-hud-vertical-constrained .panel-buttons {
        gap: clamp(3px, 1vw, 6px);
    }

    body.layout-hud-heavy-offsets #bottom-panel .booster-counter-count {
        font-size: clamp(7px, 1.7vw, 9px);
    }
}

@media (orientation: portrait) and (max-width: 560px) {
    #main-menu {
        justify-content: center;
        padding:
            calc(var(--safe-area-inset-top, 0px) + 8px)
            calc(var(--safe-area-inset-right, 0px) + 10px)
            calc(var(--safe-area-inset-bottom, 0px) + 18px)
            calc(var(--safe-area-inset-left, 0px) + 10px);
    }

    #main-menu-center {
        flex: 0 1 auto;
        justify-content: flex-start;
        gap: clamp(8px, 2.5vh, 18px);
        padding-bottom: clamp(92px, 16vh, 160px);
    }

    #main-menu-logo {
        width: clamp(
            150px,
            var(--lobby-logo-offset-width, 54vw),
            260px
        );
        max-height: calc(var(--safe-area-effective-height, 100dvh) * 0.22);
        margin-bottom: clamp(16px, 4vh, 34px);
    }

    .main-menu-play-btn-lobby {
        width: clamp(
            144px,
            var(--lobby-button-offset-width, 62vw),
            250px
        );
    }

    #main-menu-play-btn {
        bottom: calc(var(--safe-area-inset-bottom, 0px) + clamp(96px, 15vh, 150px));
    }

    .main-menu-play-title {
        font-size: clamp(18px, 5.2vw, 30px);
    }

    .main-menu-play-level {
        font-size: clamp(12px, 3vw, 18px);
    }

    #version-label {
        left: calc(50% + var(--safe-area-center-offset-x, 0px));
        transform: translateX(-50%);
        bottom: calc(var(--safe-area-inset-bottom, 0px) + 18px);
    }

    body.layout-lobby-extreme-width #main-menu-logo {
        width: clamp(42px, calc(var(--safe-area-effective-width, 100vw) * 0.45), 72px);
        max-width: none;
        max-height: none;
        transform: translateY(clamp(12px, 3vh, 28px));
        margin-bottom: clamp(18px, 5vh, 42px);
    }

    body.layout-lobby-extreme-width .main-menu-play-btn-lobby {
        width: clamp(108px, var(--lobby-button-extreme-width, 132px), 150px);
        max-width: none;
    }

    body.layout-lobby-extreme-width .main-menu-play-title {
        font-size: clamp(16px, 5vw, 22px);
    }

    body.layout-lobby-extreme-width .main-menu-play-level {
        font-size: clamp(10px, 3vw, 14px);
    }
}

@media (orientation: portrait) {
    body:not(.layout-landscape-mobile):not(.layout-landscape-mobile-tight) #bottom-panel .game-btn {
        width: clamp(58px, 13vw, 70px);
        height: clamp(58px, 13vw, 70px);
    }

    body:not(.layout-landscape-mobile):not(.layout-landscape-mobile-tight) #bottom-panel {
        width: clamp(174px, 39vw, 210px);
    }

    body.layout-hud-heavy-offsets:not(.layout-landscape-mobile):not(.layout-landscape-mobile-tight) #bottom-panel .game-btn {
        width: clamp(46px, 10.8vw, 56px);
        height: clamp(46px, 10.8vw, 56px);
    }

    body.layout-hud-heavy-bottom-offsets:not(.layout-landscape-mobile):not(.layout-landscape-mobile-tight) #bottom-panel .game-btn {
        width: clamp(44px, 10.2vw, 54px);
        height: clamp(44px, 10.2vw, 54px);
    }

    body.layout-hud-heavy-top-offsets:not(.layout-landscape-mobile):not(.layout-landscape-mobile-tight) #bottom-panel .game-btn {
        width: clamp(40px, 9.4vw, 50px);
        height: clamp(40px, 9.4vw, 50px);
    }

    body.layout-hud-vertical-constrained:not(.layout-landscape-mobile):not(.layout-landscape-mobile-tight) #bottom-panel .game-btn {
        width: clamp(38px, 8.8vw, 48px);
        height: clamp(38px, 8.8vw, 48px);
    }
}

body.layout-landscape-mobile #moves-counter-widget {
    top: calc(var(--safe-area-inset-top, 0px) + 6px * var(--ui-scale, 1));
    left: calc(var(--safe-area-inset-left, 0px) + 6px * var(--ui-scale, 1));
}
body.layout-landscape-mobile #top-right-area {
    top: calc(var(--safe-area-inset-top, 0px) + 6px * var(--ui-scale, 1));
    right: calc(var(--safe-area-inset-right, 0px) + 6px * var(--ui-scale, 1));
}

body.layout-landscape-mobile #bottom-panel {
    height: clamp(82px, 11vmin, 104px);
    width: clamp(176px, 26vmin, 270px);
}

body.layout-landscape-mobile #bottom-panel .game-btn {
    width: clamp(56px, 7.2vmin, 68px);
    height: clamp(56px, 7.2vmin, 68px);
}

body.layout-landscape-mobile .panel-buttons {
    gap: clamp(8px, 1.6vmin, 16px);
}

/* Very small landscape: smaller panel so gamefield fits */
body.layout-landscape-mobile-tight #bottom-panel {
    height: clamp(48px, 8vmin, 70px);
    width: clamp(130px, 20vmin, 200px);
}
body.layout-landscape-mobile-tight #bottom-panel .game-btn {
    width: clamp(30px, 5vmin, 42px);
    height: clamp(30px, 5vmin, 42px);
}
body.layout-landscape-mobile-tight .panel-buttons {
    gap: clamp(4px, 1vmin, 8px);
}

/* Square-ish landscape screens: keep panel fully inside viewport. */
body.layout-square-ish #bottom-panel {
    bottom: calc(4px + var(--safe-area-inset-bottom, 0px));
}

@media (orientation: landscape) and (min-height: 650px) and (max-height: 900px) {
    #bottom-panel .game-btn {
        width: clamp(48px, 6.2vmin, 56px);
        height: clamp(48px, 6.2vmin, 56px);
    }

    .panel-buttons {
        gap: clamp(6px, 1.2vmin, 12px);
    }
}

/* 1024x768-ish landscape: slightly smaller HUD text/widget to avoid overlap with offsets. */
@media (orientation: landscape) and (max-width: 1100px) and (max-height: 820px) {
    #moves-counter-widget {
        width: clamp(46px, 9.8vmin, 104px);
    }

    .moves-counter-label {
        font-size: clamp(13px, 2.3vmin, 24px);
    }

    .moves-counter-value {
        font-size: clamp(22px, 5.7vmin, 62px);
    }
}

/* Platform pause overlay – created in JS and appended to body, so it never affects #app layout */
@media (max-width: 500px) {
    body.layout-hud-heavy-offsets #moves-counter-widget .moves-counter-label {
        font-size: clamp(4px, 1.35vw, 6px);
        transform: scaleX(0.72);
        transform-origin: center top;
    }
}

body.layout-hud-heavy-offsets.layout-hud-vertical-constrained #moves-counter-widget {
    width: clamp(38px, 8.25vmin, 88px);
}

body.layout-hud-heavy-offsets.layout-hud-vertical-constrained .moves-counter-label {
    font-size: clamp(11px, 2.1vmin, 22px);
}

body.layout-hud-heavy-offsets.layout-hud-vertical-constrained .moves-counter-value {
    font-size: clamp(20px, 4.8vmin, 56px);
}

@media (max-width: 500px) {
    body.layout-hud-heavy-offsets.layout-hud-vertical-constrained #moves-counter-widget {
        width: clamp(15px, 6.75vw, 35px);
    }

    body.layout-hud-heavy-offsets.layout-hud-vertical-constrained .moves-counter-label {
        font-size: clamp(4px, 1.15vw, 5px);
    }

    body.layout-hud-heavy-offsets.layout-hud-vertical-constrained .moves-counter-value {
        font-size: clamp(7px, 2.85vw, 13px);
    }
}

.platform-pause-overlay {
    position: fixed;
    top: var(--safe-area-inset-top, 0px);
    right: var(--safe-area-inset-right, 0px);
    bottom: var(--safe-area-inset-bottom, 0px);
    left: var(--safe-area-inset-left, 0px);
    width: auto;
    height: auto;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.01);
    z-index: 900;
    pointer-events: none;
    box-sizing: border-box;
}
.platform-pause-overlay.visible {
    display: flex;
}
.platform-pause-overlay-label {
    display: none;
}

/* Settings Popup */
#settings-overlay {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100dvh;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 10px;
    box-sizing: border-box;
    background: rgba(0, 0, 0, 0.6);
    z-index: 5000;
    pointer-events: auto;
}

/* Hard input lock while any HTML modal is visible */
body.modal-open #game-container,
body.modal-open #game-container canvas {
    pointer-events: none !important;
}

#settings-card {
    position: relative;
    width: clamp(300px, 42vmin, 480px);
    max-width: 94vw;
    --settings-scale: 1;
    transform: scale(var(--settings-scale));
    transform-origin: center center;
    overflow: visible;
    height: auto;
}


.settings-background {
    display: block;
    pointer-events: none;
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: fill;
}

.settings-inner {
    position: relative;
    /* Keep rows slightly lower without creating a large empty center area. */
    padding: 5% 8% 7%;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    overflow: visible;
}

.settings-header {
    position: relative;
    display: flex;
    justify-content: center;
    margin-bottom: 8%;
    flex-shrink: 0;
}

.settings-title {
    width: 100%;
    text-align: center;
    font-size: clamp(14px, 3.4vmin, 22px);
    font-weight: 800;
    color: #822B14;
    white-space: nowrap;
    max-width: calc(100% - 60px);
    overflow: hidden;
    text-overflow: ellipsis;
}

.settings-close-btn {
    position: absolute;
    top: 3%;
    right: 3%;
    width: clamp(36px, 5.5vmin, 56px);
    height: clamp(36px, 5.5vmin, 56px);
    border: none;
    padding: 0;
    background: transparent;
    cursor: pointer;
    z-index: 5;
    pointer-events: auto;
}

.settings-close-btn img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.settings-content {
    display: flex;
    flex-direction: column;
    gap: 0;
    overflow: visible;
    flex-shrink: 0;
}

.settings-divider {
    width: 100%;
    height: auto;
    display: block;
    pointer-events: none;
    margin: 2px 0;
}

.settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 8px;
}

.settings-row-left {
    display: flex;
    align-items: center;
    gap: clamp(8px, 1.5vmin, 14px);
}

.settings-row-icon {
    width: clamp(24px, 3.5vmin, 38px);
    height: clamp(24px, 3.5vmin, 38px);
    object-fit: contain;
    flex-shrink: 0;
}

.settings-label {
    font-size: clamp(24px, 3.8vmin, 40px);
    font-weight: 600;
    color: #685550;
}

.settings-toggle {
    width: clamp(56px, 8vmin, 88px);
    height: clamp(30px, 4.2vmin, 46px);
    border: none;
    border-radius: 999px;
    padding: 0;
    background: transparent;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.settings-toggle-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.settings-row--language {
    position: relative;
    overflow: visible;
}

.settings-lang-wrap {
    position: relative;
    overflow: visible;
}

.settings-lang-btn {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    /* Narrower than before — matches border art aspect better and reduces corner bleed on wide layouts. */
    width: clamp(55px, 8vmin, 100px);
    height: clamp(40px, 6vmin, 62px);
    padding: 0;
    border: none;
    background: transparent;
    cursor: pointer;
    overflow: hidden;
    border-radius: 10px;
}

.settings-lang-btn-bg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: fill;
}

/*
 * Flag must be clipped to the *inner hole* of selected_language_border.png, not the full button.
 * Full-button clipping leaves flag visible in the bezel (between frame and outer edge).
 * Inset matches the art’s inner window; tweak --settings-lang-flag-inset if the asset changes.
 */
.settings-lang-flag-wrap {
    --settings-lang-flag-inset: 4px;
    --settings-lang-clip-radius: 8px;
    position: absolute;
    inset: var(--settings-lang-flag-inset);
    z-index: 1;
    overflow: hidden;
    border-radius: var(--settings-lang-clip-radius);
    pointer-events: none;
    contain: paint;
    isolation: isolate;
    clip-path: inset(0 round var(--settings-lang-clip-radius));
}

/* Fill clip box exactly; rely on cover+center for proper centering. */
.settings-lang-flag {
    --settings-lang-bleed: clamp(1px, 0.18vmin, 2px);
    position: absolute;
    inset: calc(-1 * var(--settings-lang-bleed));
    width: calc(100% + 2 * var(--settings-lang-bleed));
    height: calc(100% + 2 * var(--settings-lang-bleed));
    object-fit: cover;
    object-position: center;
    display: block;
}

/* 1080p+ desktop: tighten flag mask a bit to prevent tiny corner bleed under frame. */
@media (orientation: landscape) and (min-width: 1600px) and (min-height: 900px) {
    .settings-lang-flag-wrap {
        --settings-lang-flag-inset: 5px;
        --settings-lang-clip-radius: 9px;
    }

    .settings-lang-flag {
        --settings-lang-bleed: 1px;
    }
}

.settings-lang-border {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: fill;
    z-index: 2;
    pointer-events: none;
}

/* Language dropdown */
.settings-lang-dropdown {
    --settings-lang-row: clamp(32px, 4.5vmin, 44px);
    position: absolute;
    top: 0;
    left: auto;
    right: 100%;
    margin-right: 0;
    min-width: 200px;
    width: max-content;
    max-width: min(280px, calc(100vw - 24px));
    z-index: 2000;
    display: none;
    overflow: visible;
    box-sizing: border-box;
}

.settings-lang-dropdown-bg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: fill;
    border-radius: 12px;
    pointer-events: none;
}

.settings-lang-dropdown-inner {
    position: relative;
    display: flex;
    flex-direction: row;
    align-items: stretch;
    gap: 4px;
    padding: 10px 8px;
    max-height: calc(5 * var(--settings-lang-row) + 20px);
    overflow: hidden;
}

.settings-lang-dropdown-scroll {
    flex: 1;
    min-width: 0;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: scroll;
    touch-action: pan-y;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.settings-lang-dropdown-scroll::-webkit-scrollbar {
    display: none;
}

.settings-lang-scrollbar-rail {
    position: relative;
    flex-shrink: 0;
    width: 8px;
    align-self: stretch;
    border-radius: 4px;
    background: rgba(75, 44, 8, 0.12);
    margin: 4px 0;
}

.settings-lang-scrollbar-rail.is-hidden {
    display: none;
}

.settings-lang-scrollbar-thumb {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    width: 100%;
    min-height: 20px;
    border-radius: 4px;
    background: #8a5d2a;
    box-sizing: border-box;
    cursor: grab;
    pointer-events: auto;
}

.settings-lang-scrollbar-thumb.is-dragging {
    cursor: grabbing;
}

@media (orientation: landscape) {
    .settings-lang-dropdown {
        top: 0;
        left: 100%;
        right: auto;
        margin-left: 0;
        margin-right: 0;
    }
}

@media (orientation: portrait) {
    .settings-lang-dropdown-bg {
        transform: scaleX(-1);
        transform-origin: center;
    }
}

.settings-lang-dropdown.is-open {
    display: block;
}

.settings-lang-option {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 4px 10px;
    border: none;
    width: 100%;
    min-width: 0;
    min-height: var(--settings-lang-row);
    box-sizing: border-box;
    background: none;
    font-size: clamp(14px, 2vmin, 20px);
    font-weight: 600;
    color: #822B14;
    text-align: left;
    cursor: pointer;
    touch-action: pan-y;
    border-radius: 6px;
}

.settings-lang-option:hover {
    background: rgba(139, 98, 38, 0.1);
}

.settings-lang-option-flag {
    width: clamp(30px, 4.5vmin, 44px);
    height: clamp(20px, 3vmin, 30px);
    object-fit: contain;
    flex-shrink: 0;
    border-radius: 4px;
}

.settings-lang-option-name {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.settings-lang-option-check {
    position: relative;
    width: clamp(22px, 3vmin, 30px);
    height: clamp(22px, 3vmin, 30px);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.settings-lang-option-check-bg {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.settings-lang-option-check-mark {
    position: absolute;
    inset: 15%;
    width: 70%;
    height: 70%;
    object-fit: contain;
    display: none;
}

.settings-lang-option-check.is-selected .settings-lang-option-check-mark {
    display: block;
}

/* Bottom buttons: Home + Restart */
.settings-bottom-buttons {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: clamp(10px, 2vmin, 20px);
    margin-top: 4%;
    flex-shrink: 0;
}

.settings-home-btn {
    width: clamp(50px, 8vmin, 76px);
    height: clamp(50px, 8vmin, 76px);
    border: none;
    padding: 0;
    background: transparent;
    cursor: pointer;
    flex-shrink: 0;
}

.settings-home-btn img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.settings-restart-btn {
    position: relative;
    width: clamp(160px, 24vmin, 280px);
    height: clamp(50px, 8vmin, 76px);
    border: none;
    padding: 0;
    background: transparent;
    cursor: pointer;
    flex-shrink: 0;
}

.settings-restart-bg {
    width: 100%;
    height: 100%;
    object-fit: fill;
    display: block;
}

.settings-restart-inner {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: clamp(4px, 0.8vmin, 10px);
    pointer-events: none;
}

.settings-restart-icon {
    width: clamp(20px, 3vmin, 32px);
    height: clamp(20px, 3vmin, 32px);
    object-fit: contain;
}

.settings-restart-text {
    font-size: clamp(18px, 3vmin, 32px);
    font-weight: 700;
    color: #fff;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    white-space: nowrap;
    line-height: 1;
}

.settings-restart-text.is-long {
    font-size: clamp(16px, 2.6vmin, 28px);
}

.settings-restart-text.is-very-long {
    font-size: clamp(14px, 2.2vmin, 24px);
}

/* Hide bottom buttons when opened from lobby */
#settings-card.settings--no-menu .settings-bottom-buttons {
    display: none;
}

/* Italian compact */
#settings-card.settings-lang-it .settings-label {
    font-size: clamp(24px, 3.6vmin, 40px);
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}
