/**
 * Open Chest V2 — flex shelf layout (same positioning model as smashGoldenEggsV2 egg-arc).
 */

.treasure-body {
    color: var(--theme-text-body, #e8f4ff);
    background-color: var(--theme-bg-deep, #061018);
    background-image:
        linear-gradient(180deg, var(--theme-overlay, rgba(4, 12, 24, 0.35)) 0%, var(--theme-overlay-deep, rgba(4, 10, 18, 0.72)) 100%),
        var(--treasure-photo, none),
        radial-gradient(ellipse 80% 55% at 50% 18%, var(--theme-bg-mid, #1a4a7a) 0%, var(--theme-bg, #081420) 52%, var(--theme-bg-deep, #040a12) 100%);
    background-size: cover, cover, cover;
    background-position: center, center, center;
    background-repeat: no-repeat;
}

.treasure-body.has-treasure-photo {
    background-color: var(--theme-bg-deep, #061018);
    background-image:
        linear-gradient(180deg, var(--theme-overlay, rgba(4, 12, 24, 0.22)) 0%, var(--theme-overlay-deep, rgba(4, 10, 18, 0.55)) 100%),
        var(--treasure-photo);
    background-size: cover, cover;
    background-position: center, center;
}

.treasure-scene {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    padding:
        calc(0.35rem + env(safe-area-inset-top))
        clamp(0.65rem, 3vw, 1.1rem)
        calc(0.25rem + env(safe-area-inset-bottom));
    box-sizing: border-box;
    overflow: hidden;
    gap: 0.2rem;
}

/* ── Header (compact) ── */

.treasure-head {
    position: relative;
    z-index: 4;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.65rem;
    flex-shrink: 0;
}

.treasure-brand {
    min-width: 0;
    flex: 1;
}

.treasure-eyebrow {
    margin: 0;
    color: var(--theme-accent, #9fd0ff);
    font-family: 'Noto Serif SC', serif;
    font-size: clamp(0.58rem, 2.1vw, 0.72rem);
    letter-spacing: 0.14em;
    line-height: 1.2;
}

.treasure-brand h1 {
    margin: 0.05rem 0 0;
    color: var(--theme-coin-light, #f5e6b8);
    font-family: Cinzel, 'Noto Serif SC', serif;
    font-size: clamp(1.2rem, 5.2vw, 1.75rem);
    letter-spacing: 0.08em;
    line-height: 1.05;
    text-shadow: 0 0 22px var(--theme-glow-soft, rgba(126, 200, 255, 0.35));
}

.treasure-sub {
    margin: 0.05rem 0 0;
    color: var(--theme-text-soft, #b8d9f5);
    font-family: 'Noto Serif SC', serif;
    font-size: clamp(0.68rem, 2.4vw, 0.82rem);
    letter-spacing: 0.14em;
    line-height: 1.2;
}

.treasure-token {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 3.6rem;
    padding: 0.32rem 0.5rem;
    border: 1px solid var(--theme-accent-soft, rgba(126, 200, 255, 0.45));
    border-radius: 0.55rem;
    background: var(--theme-panel-deep, rgba(8, 20, 32, 0.78));
    box-shadow: inset 0 0 14px var(--theme-glow-soft, rgba(69, 202, 252, 0.12));
}

.treasure-token strong {
    color: var(--theme-coin-light, #ffe9a8);
    font-family: Cinzel, serif;
    font-size: 1rem;
    line-height: 1;
}

.treasure-token small {
    margin-top: 0.1rem;
    color: var(--theme-accent, #9fd0ff);
    font-size: 0.5rem;
    letter-spacing: 0.12em;
}

/* ── Stage: 2×2 grid fills vertical space ── */

.treasure-stage {
    position: relative;
    z-index: 2;
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    width: 100%;
}

.treasure-play {
    position: relative;
    z-index: 2;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    min-height: 0;
    width: 100%;
    gap: clamp(1.5rem, 5vh, 2.5rem);
    padding: 0.15rem 0 0.35rem;
}

.treasure-glow {
    position: absolute;
    left: 50%;
    top: 48%;
    width: min(96vw, 26rem);
    height: min(72vw, 34rem);
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background:
        radial-gradient(ellipse at center, var(--theme-glow-soft, rgba(126, 200, 255, 0.22)), transparent 55%),
        radial-gradient(ellipse at center, var(--theme-accent-soft, rgba(126, 200, 255, 0.12)), transparent 70%);
    pointer-events: none;
}

.treasure-bed {
    position: absolute;
    left: 50%;
    top: 58%;
    width: min(90%, 22rem);
    height: min(28vw, 9rem);
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background:
        radial-gradient(ellipse at 50% 40%, var(--theme-glow-soft, rgba(126, 200, 255, 0.18)), transparent 48%),
        radial-gradient(ellipse at 50% 55%, var(--theme-panel-deep, rgba(8, 20, 32, 0.55)), transparent 62%),
        radial-gradient(ellipse at 50% 75%, var(--theme-bg-deep, #040a12) 0%, transparent 70%);
    box-shadow: 0 22px 36px rgba(0, 0, 0, 0.35);
    pointer-events: none;
}

.chest-shelf {
    position: relative;
    z-index: 3;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-auto-rows: minmax(0, 1fr);
    gap: clamp(0.85rem, 2.5vh, 1.35rem) clamp(1rem, 4.5vw, 1.65rem);
    width: min(82vw, 15.5rem);
    height: min(calc(100dvh - 16rem), 42dvh, 21rem);
    margin-bottom: clamp(0.25rem, 1vh, 0.5rem);
    box-sizing: border-box;
}

.chest-btn {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    width: 100%;
    min-height: 0;
    padding: 0;
    border: 0;
    background: transparent;
    cursor: pointer;
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

.chest-btn.is-disabled {
    pointer-events: none;
}

.chest-window {
    position: relative;
    display: block;
    width: min(100%, 6rem);
    height: auto;
    max-height: 100%;
    aspect-ratio: 240 / 272;
    overflow: visible;
    transform-origin: 50% 100%;
    transition: transform 0.22s ease;
}

.chest-btn:not(.is-open):not(.is-opening):hover .chest-window,
.chest-btn:not(.is-open):not(.is-opening):focus .chest-window {
    transform: translateY(-6px) scale(1.03);
}

.chest-sprite {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 240 / 272;
    pointer-events: none;
    vertical-align: top;
    filter: drop-shadow(0 12px 18px rgba(0, 0, 0, 0.5));
}

.chest-btn.is-opening .chest-window {
    animation: chestShake 0.14s ease-in-out 2;
}

.chest-hint {
    position: relative;
    z-index: 2;
    margin-top: 0.15rem;
    color: var(--theme-text-soft, rgba(232, 244, 255, 0.88));
    font-size: clamp(0.48rem, 2vw, 0.68rem);
    letter-spacing: 0.08em;
    text-shadow: 0 1px 4px #000;
    white-space: nowrap;
}

.chest-btn.is-open .chest-hint,
.chest-btn.is-opening .chest-hint {
    opacity: 0;
}

.chest-prize {
    position: absolute;
    left: 50%;
    top: 14%;
    z-index: 2;
    width: 52%;
    transform: translateX(-50%);
    pointer-events: none;
}

.chest-prize .prize-img {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 1;
    object-fit: contain;
    filter: drop-shadow(0 0 12px var(--theme-glow, rgba(255, 213, 79, 0.85)));
}

.chest-btn.is-open .chest-prize {
    animation: prizeRise 0.55s cubic-bezier(0.22, 1, 0.36, 1);
}

.chest-btn.winner .chest-window {
    animation: winnerGlow 1.6s ease-in-out infinite;
}

.chest-btn.winner::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 38%;
    width: 130%;
    height: 130%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background: radial-gradient(circle, var(--theme-glow-soft, rgba(255, 215, 120, 0.35)), transparent 68%);
    pointer-events: none;
    animation: winnerRing 0.8s ease-out;
}

.treasure-cta {
    position: relative;
    z-index: 4;
    flex-shrink: 0;
    margin: clamp(0.5rem, 1.8vh, 1.1rem) 0 clamp(0.35rem, 1.2vh, 0.75rem);
    padding: 0.5rem 1.35rem;
    border: 1px solid var(--theme-accent-soft, rgba(126, 200, 255, 0.55));
    border-radius: 999px;
    color: var(--theme-on-accent, #081420);
    background: linear-gradient(180deg, var(--theme-coin-light, #ffe9a8), var(--theme-accent-dark, #d4a017));
    font-family: Cinzel, 'Noto Serif SC', serif;
    font-size: 0.74rem;
    letter-spacing: 0.14em;
    cursor: pointer;
}

.treasure-scene.no-tokens .chest-btn {
    opacity: 0.5;
    pointer-events: none;
}

/* ── Winners: mobile table / desktop horizontal marquee ── */

.treasure-feed {
    position: relative;
    z-index: 4;
    flex-shrink: 0;
    width: 100%;
    padding: clamp(0.45rem, 1.5vh, 0.85rem) 0 0.1rem;
    margin-top: clamp(0.25rem, 1vh, 0.5rem);
}

.treasure-feed-badge {
    flex-shrink: 0;
    padding: 0.18rem 0.45rem;
    border-radius: 0.35rem;
    color: var(--theme-on-accent, #081420);
    background: linear-gradient(180deg, var(--theme-coin-light, #ffe9a8), var(--theme-accent-dark, #d4a017));
    font-family: Cinzel, serif;
    font-size: 0.54rem;
    letter-spacing: 0.12em;
    line-height: 1.2;
}

/* Mobile — full-width scrolling table */
.treasure-feed-mobile {
    display: block;
    width: 100%;
}

.treasure-feed-mobile-head {
    margin-bottom: 0.3rem;
}

.treasure-feed-table-wrap {
    width: 100%;
    border: 1px solid var(--theme-accent-soft, rgba(126, 200, 255, 0.22));
    border-radius: 0.45rem;
    background: var(--theme-panel-deep, rgba(6, 14, 24, 0.55));
    overflow: hidden;
}

.treasure-feed-table-head {
    border-bottom: 1px solid var(--theme-accent-soft, rgba(126, 200, 255, 0.2));
}

.treasure-feed-table-viewport {
    --feed-row-h: 1.34rem;
    overflow: hidden;
    width: 100%;
    height: calc(var(--feed-row-h) * 5);
    mask-image: linear-gradient(180deg, transparent 0%, #000 5%, #000 95%, transparent 100%);
    -webkit-mask-image: linear-gradient(180deg, transparent 0%, #000 5%, #000 95%, transparent 100%);
}

.treasure-feed-table-track {
    animation: feedScrollY 20s linear infinite;
}

.treasure-feed-table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
}

.treasure-feed-table thead th {
    padding: 0.28rem 0.45rem;
    color: var(--theme-accent, #9fd0ff);
    background: var(--theme-panel-deep, rgba(8, 20, 32, 0.92));
    font-family: Cinzel, serif;
    font-size: 0.52rem;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-align: left;
}

.treasure-feed-table tbody td {
    height: var(--feed-row-h, 1.34rem);
    padding: 0 0.45rem;
    color: var(--theme-text-body, #d8ecff);
    font-size: 0.62rem;
    line-height: var(--feed-row-h, 1.34rem);
    border-bottom: 1px solid rgba(126, 200, 255, 0.08);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    vertical-align: middle;
}

.treasure-feed-table tbody td:first-child {
    color: var(--theme-coin-light, #f5e6b8);
}

.treasure-feed-table-head thead th:first-child,
.treasure-feed-table tbody td:first-child {
    width: 42%;
}

.treasure-feed-table-head thead th:last-child,
.treasure-feed-table tbody td:last-child {
    width: 58%;
}

/* Desktop — one-row horizontal marquee */
.treasure-feed-desktop {
    display: none;
    align-items: center;
    gap: 0.55rem;
    width: 100%;
    min-height: 1.85rem;
}

.treasure-feed-desktop .treasure-feed-viewport {
    overflow: hidden;
    flex: 1;
    min-width: 0;
    height: auto;
    mask-image: linear-gradient(90deg, transparent, #000 6%, #000 94%, transparent);
    -webkit-mask-image: linear-gradient(90deg, transparent, #000 6%, #000 94%, transparent);
}

.treasure-feed-desktop .treasure-feed-track {
    display: flex;
    flex-direction: row;
    gap: 1.6rem;
    width: max-content;
    color: var(--theme-text-body, #d8ecff);
    font-size: 0.7rem;
    line-height: 1.25;
    animation: feedScrollX 20s linear infinite;
}

.treasure-feed-item {
    flex-shrink: 0;
    white-space: nowrap;
}

@keyframes feedScrollY {
    from { transform: translateY(0); }
    to { transform: translateY(-50%); }
}

@keyframes feedScrollX {
    from { transform: translateX(0); }
    to { transform: translateX(-50%); }
}

@keyframes chestShake {
    0%, 100% { transform: rotate(0deg); }
    25% { transform: rotate(-6deg); }
    75% { transform: rotate(6deg); }
}

@keyframes prizeRise {
    from { opacity: 0; transform: translate(-50%, 18px) scale(0.55); }
    to { opacity: 1; transform: translate(-50%, 0) scale(1); }
}

@keyframes winnerGlow {
    0%, 100% { filter: drop-shadow(0 0 10px var(--theme-glow, rgba(255, 215, 120, 0.45))); }
    50% { filter: drop-shadow(0 0 22px var(--theme-glow, rgba(255, 215, 120, 0.85))); }
}

@keyframes winnerRing {
    from { opacity: 0.85; transform: translate(-50%, -50%) scale(0.35); }
    to { opacity: 0; transform: translate(-50%, -50%) scale(1.15); }
}

/* Mobile — push chests + button lower on screen */
@media (max-width: 767px) {
    .treasure-play {
        justify-content: flex-start;
        padding-top: clamp(2rem, 11vh, 5.5rem);
        padding-bottom: 0.2rem;
    }

    .treasure-glow {
        top: 54%;
    }

    .treasure-bed {
        top: 64%;
    }
}

@media (min-width: 768px) {
    .treasure-feed-mobile {
        display: none;
    }

    .treasure-feed-desktop {
        display: flex;
    }

    .treasure-glow {
        top: 44%;
        width: min(90vw, 36rem);
        height: min(48vw, 22rem);
    }

    .treasure-bed {
        top: 52%;
        width: min(82%, 34rem);
        height: min(26vw, 10rem);
    }

    .chest-shelf {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        grid-template-rows: repeat(2, minmax(0, 1fr));
        gap: clamp(1rem, 2.2vh, 1.5rem) clamp(1.15rem, 2.5vw, 1.85rem);
        width: min(88vw, 34rem);
        height: min(calc(100dvh - 11rem), 50dvh, 28rem);
    }

    .chest-window {
        width: min(100%, 9.2rem);
        margin: 0 auto;
    }
}

@media (max-height: 700px) {
    .treasure-eyebrow {
        display: none;
    }

    .treasure-sub {
        display: none;
    }

    .chest-shelf {
        height: min(calc(100dvh - 12rem), 38dvh, 18rem);
    }

    .treasure-feed-table-viewport {
        --feed-row-h: 1.24rem;
    }
}

@media (min-height: 820px) {
    .chest-shelf {
        height: min(calc(100dvh - 15rem), 44dvh, 22rem);
    }
}

@media (min-width: 768px) and (min-height: 820px) {
    .chest-shelf {
        height: min(calc(100dvh - 11rem), 54dvh, 30rem);
    }

    .chest-window {
        width: min(100%, 9.8rem);
    }
}

/* ── Custom result overlay (replaces SweetAlert) ── */

.treasure-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    box-sizing: border-box;
    pointer-events: none;
}

.treasure-overlay.is-active {
    pointer-events: auto;
}

.treasure-overlay[hidden] {
    display: none !important;
}

.treasure-overlay-backdrop {
    position: absolute;
    inset: 0;
    background: var(--theme-overlay-deep, rgba(4, 10, 18, 0.78));
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    opacity: 0;
    transition: opacity 0.28s ease;
}

.treasure-overlay.is-active .treasure-overlay-backdrop {
    opacity: 1;
}

.treasure-loading {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
}

.treasure-loading[hidden],
.treasure-reveal[hidden] {
    display: none !important;
}

.treasure-loading-logo {
    width: min(5.5rem, 28vw);
    height: auto;
    animation: treasurePulse 1.1s ease-in-out infinite;
}

.treasure-loading-ring {
    display: block;
    width: 3.2rem;
    height: 3.2rem;
    border: 3px solid var(--theme-accent-soft, rgba(126, 200, 255, 0.25));
    border-top-color: var(--theme-accent, #7ec8ff);
    border-radius: 50%;
    animation: treasureSpin 0.75s linear infinite;
}

.treasure-reveal {
    position: relative;
    z-index: 3;
    width: min(100%, 19rem);
    animation: revealSlideUp 0.42s cubic-bezier(0.22, 1, 0.36, 1);
}

.treasure-reveal-card {
    padding: 1.15rem 1rem 1rem;
    border: 1px solid var(--theme-accent-soft, rgba(126, 200, 255, 0.4));
    border-radius: 1rem;
    text-align: center;
    background:
        radial-gradient(ellipse 90% 45% at 50% 0%, var(--theme-glow-soft, rgba(69, 202, 252, 0.14)), transparent 55%),
        linear-gradient(180deg, var(--theme-bg-mid, #12304a) 0%, var(--theme-bg, #081420) 52%, var(--theme-bg-deep, #040a12) 100%);
    box-shadow: 0 24px 48px rgba(0, 0, 0, 0.55);
}

.treasure-reveal.is-win .treasure-reveal-card {
    border-color: var(--theme-accent, rgba(255, 215, 120, 0.55));
    box-shadow:
        0 24px 48px rgba(0, 0, 0, 0.55),
        0 0 32px var(--theme-glow-soft, rgba(255, 215, 120, 0.18));
}

.treasure-reveal.is-lose .treasure-reveal-card {
    border-color: var(--theme-accent-soft, rgba(126, 200, 255, 0.35));
}

.treasure-reveal.is-error .treasure-reveal-card {
    border-color: rgba(255, 120, 120, 0.45);
}

.treasure-reveal-badge {
    display: inline-block;
    margin-bottom: 0.65rem;
    padding: 0.2rem 0.55rem;
    border-radius: 999px;
    font-family: Cinzel, serif;
    font-size: 0.58rem;
    letter-spacing: 0.16em;
}

.treasure-reveal.is-win .treasure-reveal-badge {
    color: var(--theme-on-accent, #1a1206);
    background: linear-gradient(180deg, var(--theme-coin-light, #ffe9a8), var(--theme-accent-dark, #d4a017));
}

.treasure-reveal.is-lose .treasure-reveal-badge {
    color: var(--theme-text-soft, #c8e4ff);
    background: var(--theme-panel-deep, rgba(8, 20, 32, 0.85));
    border: 1px solid var(--theme-accent-soft, rgba(126, 200, 255, 0.35));
}

.treasure-reveal.is-error .treasure-reveal-badge {
    color: #ffd4d4;
    background: rgba(80, 20, 20, 0.65);
    border: 1px solid rgba(255, 120, 120, 0.35);
}

.treasure-reveal-frame {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 7.5rem;
    height: 7.5rem;
    margin: 0 auto 0.75rem;
    border-radius: 50%;
    background:
        radial-gradient(circle at 50% 35%, var(--theme-glow-soft, rgba(255, 215, 120, 0.2)), transparent 62%),
        var(--theme-panel-deep, rgba(8, 20, 32, 0.72));
    border: 2px solid var(--theme-accent-soft, rgba(126, 200, 255, 0.35));
}

.treasure-reveal.is-win .treasure-reveal-frame {
    border-color: var(--theme-accent, rgba(255, 215, 120, 0.55));
    animation: winnerGlow 1.8s ease-in-out infinite;
}

.treasure-reveal-prize {
    width: 68%;
    height: 68%;
    object-fit: contain;
    filter: drop-shadow(0 0 12px var(--theme-glow, rgba(255, 213, 79, 0.75)));
}

.treasure-reveal.is-error .treasure-reveal-frame {
    display: none;
}

.treasure-reveal-title {
    margin: 0 0 0.35rem;
    color: var(--theme-coin-light, #f5e6b8);
    font-family: Cinzel, 'Noto Serif SC', serif;
    font-size: 1.15rem;
    letter-spacing: 0.08em;
    line-height: 1.2;
}

.treasure-reveal.is-lose .treasure-reveal-title {
    color: var(--theme-text, #e8f4ff);
}

.treasure-reveal-msg {
    margin: 0 0 0.95rem;
    color: var(--theme-text-soft, #b8d9f5);
    font-family: 'Cormorant Garamond', 'Noto Serif SC', serif;
    font-size: 0.95rem;
    line-height: 1.45;
}

.treasure-reveal-actions {
    display: flex;
    justify-content: center;
}

.treasure-reveal-btn {
    min-width: 8.5rem;
    padding: 0.62rem 1.5rem;
    border: 1px solid var(--theme-accent, rgba(126, 200, 255, 0.55));
    border-radius: 999px;
    color: var(--theme-on-accent, #081420);
    background: linear-gradient(180deg, var(--theme-coin-light, #ffe9a8), var(--theme-accent-dark, #d4a017));
    font-family: Cinzel, 'Noto Serif SC', serif;
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    cursor: pointer;
}

.treasure-reveal.is-lose .treasure-reveal-btn,
.treasure-reveal.is-error .treasure-reveal-btn {
    color: var(--theme-text, #e8f4ff);
    background: linear-gradient(180deg, var(--theme-bg-mid, #12304a), var(--theme-bg-deep, #081420));
    border-color: var(--theme-accent-soft, rgba(126, 200, 255, 0.45));
}

@keyframes revealSlideUp {
    from { opacity: 0; transform: translateY(24px) scale(0.96); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes treasurePulse {
    0%, 100% { opacity: 0.75; transform: scale(0.96); }
    50% { opacity: 1; transform: scale(1); }
}

@keyframes treasureSpin {
    to { transform: rotate(360deg); }
}
