/* ============================================================
   SLEEP MAZE - ท่องโลกในความฝัน
   Fonts & Design tokens
   ============================================================ */

/* ---- Vastago Grotesk (Headlines) ---- */
@font-face {
    font-family: 'Vastago Grotesk';
    src: url('font/vastago-grotesk-fonts/VastagoGrotesk-Medium.otf') format('opentype');
    font-weight: 500;
    font-style: normal;
    font-display: block;
}
@font-face {
    font-family: 'Vastago Grotesk';
    src: url('font/vastago-grotesk-fonts/VastagoGrotesk-Bold.otf') format('opentype');
    font-weight: 700;
    font-style: normal;
    font-display: block;
}
/* ---- Plus Jakarta Sans (Body / Subheads) ---- */
@font-face {
    font-family: 'Plus Jakarta Sans';
    src: url('font/Plus_Jakarta_Sans/PlusJakartaSans-Regular.ttf') format('truetype');
    font-weight: 400;
    font-style: normal;
    font-display: block;
}
@font-face {
    font-family: 'Plus Jakarta Sans';
    src: url('font/Plus_Jakarta_Sans/PlusJakartaSans-Medium.ttf') format('truetype');
    font-weight: 500;
    font-style: normal;
    font-display: block;
}
@font-face {
    font-family: 'Plus Jakarta Sans';
    src: url('font/Plus_Jakarta_Sans/PlusJakartaSans-SemiBold.ttf') format('truetype');
    font-weight: 600;
    font-style: normal;
    font-display: block;
}
@font-face {
    font-family: 'Plus Jakarta Sans';
    src: url('font/Plus_Jakarta_Sans/PlusJakartaSans-Bold.ttf') format('truetype');
    font-weight: 700;
    font-style: normal;
    font-display: block;
}
/* ---- Noto Sans Thai (Thai fallback) ---- */
@font-face {
    font-family: 'Noto Sans Thai';
    src: url('font/Noto_Sans_Thai/NotoSansThai-Regular.ttf') format('truetype');
    font-weight: 400;
    font-style: normal;
    font-display: block;
}
@font-face {
    font-family: 'Noto Sans Thai';
    src: url('font/Noto_Sans_Thai/NotoSansThai-Medium.ttf') format('truetype');
    font-weight: 500;
    font-style: normal;
    font-display: block;
}
@font-face {
    font-family: 'Noto Sans Thai';
    src: url('font/Noto_Sans_Thai/NotoSansThai-SemiBold.ttf') format('truetype');
    font-weight: 600;
    font-style: normal;
    font-display: block;
}
@font-face {
    font-family: 'Noto Sans Thai';
    src: url('font/Noto_Sans_Thai/NotoSansThai-Bold.ttf') format('truetype');
    font-weight: 700;
    font-style: normal;
    font-display: block;
}

:root {
    /* Font stacks */
    --font-heading: 'Vastago Grotesk', 'Noto Sans Thai', Arial, sans-serif;
    --font-body:    'Plus Jakarta Sans', 'Noto Sans Thai', Arial, sans-serif;

    /* Text (deep purple ink reads on pastel backgrounds) */
    --ink:        #2d1b4e;
    --ink-soft:   #5a4280;
    --ink-muted:  #8373ab;
    --paper:      #ffffff;

    /* Surfaces */
    --glass:         rgba(255, 255, 255, 0.72);
    --glass-strong:  rgba(255, 255, 255, 0.85);
    --glass-soft:    rgba(255, 255, 255, 0.5);
    --glass-border:  rgba(255, 255, 255, 0.7);

    /* Soft white center vignette layered above the maze pattern on start,
       gameover, intro "found item", and reveal screens — fades the maze out
       near the middle so foreground content stays easy to read. */
    --maze-vignette: radial-gradient(circle at center, rgba(255,255,255,0.92) 0%, rgba(255,255,255,0.55) 45%, rgba(255,255,255,0) 88%);

    /* Faint rounded maze-line texture for flat white/blue screens (start,
       tutorial, gameover, victory, result, intro "found item", reveal,
       question) — subtle enough not to compete with foreground content.
       One single maze stretched to fill the whole screen (preserveAspectRatio
       none), not a repeated tile, so corridors read as one continuous maze. */
    --maze-pattern: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1600' height='1400' viewBox='0 0 1600 1400'%3E%3Cg fill='none' stroke='%23b8e1fc' stroke-width='14' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='0' y1='0' x2='1600' y2='0'/%3E%3Cline x1='0' y1='100' x2='100' y2='100'/%3E%3Cline x1='200' y1='100' x2='400' y2='100'/%3E%3Cline x1='700' y1='100' x2='800' y2='100'/%3E%3Cline x1='900' y1='100' x2='1000' y2='100'/%3E%3Cline x1='1400' y1='100' x2='1500' y2='100'/%3E%3Cline x1='100' y1='200' x2='300' y2='200'/%3E%3Cline x1='400' y1='200' x2='500' y2='200'/%3E%3Cline x1='800' y1='200' x2='900' y2='200'/%3E%3Cline x1='1000' y1='200' x2='1100' y2='200'/%3E%3Cline x1='1200' y1='200' x2='1300' y2='200'/%3E%3Cline x1='100' y1='300' x2='400' y2='300'/%3E%3Cline x1='500' y1='300' x2='800' y2='300'/%3E%3Cline x1='1100' y1='300' x2='1200' y2='300'/%3E%3Cline x1='1300' y1='300' x2='1400' y2='300'/%3E%3Cline x1='100' y1='400' x2='200' y2='400'/%3E%3Cline x1='300' y1='400' x2='500' y2='400'/%3E%3Cline x1='600' y1='400' x2='900' y2='400'/%3E%3Cline x1='1200' y1='400' x2='1400' y2='400'/%3E%3Cline x1='0' y1='500' x2='100' y2='500'/%3E%3Cline x1='300' y1='500' x2='500' y2='500'/%3E%3Cline x1='800' y1='500' x2='1000' y2='500'/%3E%3Cline x1='1100' y1='500' x2='1300' y2='500'/%3E%3Cline x1='1500' y1='500' x2='1600' y2='500'/%3E%3Cline x1='100' y1='600' x2='400' y2='600'/%3E%3Cline x1='500' y1='600' x2='600' y2='600'/%3E%3Cline x1='900' y1='600' x2='1200' y2='600'/%3E%3Cline x1='1400' y1='600' x2='1500' y2='600'/%3E%3Cline x1='200' y1='700' x2='300' y2='700'/%3E%3Cline x1='400' y1='700' x2='500' y2='700'/%3E%3Cline x1='800' y1='700' x2='1100' y2='700'/%3E%3Cline x1='1300' y1='700' x2='1400' y2='700'/%3E%3Cline x1='0' y1='800' x2='200' y2='800'/%3E%3Cline x1='500' y1='800' x2='600' y2='800'/%3E%3Cline x1='700' y1='800' x2='1000' y2='800'/%3E%3Cline x1='1100' y1='800' x2='1200' y2='800'/%3E%3Cline x1='1500' y1='800' x2='1600' y2='800'/%3E%3Cline x1='100' y1='900' x2='400' y2='900'/%3E%3Cline x1='500' y1='900' x2='600' y2='900'/%3E%3Cline x1='900' y1='900' x2='1100' y2='900'/%3E%3Cline x1='1200' y1='900' x2='1300' y2='900'/%3E%3Cline x1='1400' y1='900' x2='1500' y2='900'/%3E%3Cline x1='100' y1='1000' x2='500' y2='1000'/%3E%3Cline x1='800' y1='1000' x2='1000' y2='1000'/%3E%3Cline x1='1100' y1='1000' x2='1200' y2='1000'/%3E%3Cline x1='1300' y1='1000' x2='1400' y2='1000'/%3E%3Cline x1='0' y1='1100' x2='700' y2='1100'/%3E%3Cline x1='800' y1='1100' x2='1100' y2='1100'/%3E%3Cline x1='1400' y1='1100' x2='1500' y2='1100'/%3E%3Cline x1='300' y1='1200' x2='400' y2='1200'/%3E%3Cline x1='700' y1='1200' x2='800' y2='1200'/%3E%3Cline x1='900' y1='1200' x2='1000' y2='1200'/%3E%3Cline x1='400' y1='1300' x2='500' y2='1300'/%3E%3Cline x1='600' y1='1300' x2='700' y2='1300'/%3E%3Cline x1='1000' y1='1300' x2='1400' y2='1300'/%3E%3Cline x1='0' y1='1400' x2='1600' y2='1400'/%3E%3Cline x1='0' y1='0' x2='0' y2='1400'/%3E%3Cline x1='100' y1='100' x2='100' y2='200'/%3E%3Cline x1='100' y1='300' x2='100' y2='400'/%3E%3Cline x1='100' y1='600' x2='100' y2='700'/%3E%3Cline x1='100' y1='900' x2='100' y2='1000'/%3E%3Cline x1='100' y1='1200' x2='100' y2='1400'/%3E%3Cline x1='200' y1='0' x2='200' y2='100'/%3E%3Cline x1='200' y1='400' x2='200' y2='600'/%3E%3Cline x1='200' y1='700' x2='200' y2='800'/%3E%3Cline x1='200' y1='1100' x2='200' y2='1300'/%3E%3Cline x1='300' y1='400' x2='300' y2='500'/%3E%3Cline x1='300' y1='700' x2='300' y2='800'/%3E%3Cline x1='300' y1='1200' x2='300' y2='1400'/%3E%3Cline x1='400' y1='100' x2='400' y2='300'/%3E%3Cline x1='400' y1='600' x2='400' y2='900'/%3E%3Cline x1='500' y1='100' x2='500' y2='200'/%3E%3Cline x1='500' y1='300' x2='500' y2='400'/%3E%3Cline x1='500' y1='500' x2='500' y2='600'/%3E%3Cline x1='500' y1='800' x2='500' y2='900'/%3E%3Cline x1='500' y1='1100' x2='500' y2='1300'/%3E%3Cline x1='600' y1='100' x2='600' y2='300'/%3E%3Cline x1='600' y1='500' x2='600' y2='800'/%3E%3Cline x1='600' y1='900' x2='600' y2='1100'/%3E%3Cline x1='600' y1='1200' x2='600' y2='1300'/%3E%3Cline x1='700' y1='0' x2='700' y2='200'/%3E%3Cline x1='700' y1='400' x2='700' y2='800'/%3E%3Cline x1='700' y1='900' x2='700' y2='1100'/%3E%3Cline x1='700' y1='1300' x2='700' y2='1400'/%3E%3Cline x1='800' y1='200' x2='800' y2='300'/%3E%3Cline x1='800' y1='500' x2='800' y2='700'/%3E%3Cline x1='800' y1='800' x2='800' y2='1000'/%3E%3Cline x1='800' y1='1100' x2='800' y2='1300'/%3E%3Cline x1='900' y1='100' x2='900' y2='400'/%3E%3Cline x1='900' y1='1200' x2='900' y2='1300'/%3E%3Cline x1='1000' y1='200' x2='1000' y2='500'/%3E%3Cline x1='1000' y1='1000' x2='1000' y2='1100'/%3E%3Cline x1='1000' y1='1200' x2='1000' y2='1300'/%3E%3Cline x1='1100' y1='0' x2='1100' y2='200'/%3E%3Cline x1='1100' y1='300' x2='1100' y2='600'/%3E%3Cline x1='1100' y1='700' x2='1100' y2='1000'/%3E%3Cline x1='1100' y1='1100' x2='1100' y2='1200'/%3E%3Cline x1='1200' y1='0' x2='1200' y2='100'/%3E%3Cline x1='1200' y1='200' x2='1200' y2='300'/%3E%3Cline x1='1200' y1='600' x2='1200' y2='700'/%3E%3Cline x1='1200' y1='1000' x2='1200' y2='1300'/%3E%3Cline x1='1300' y1='100' x2='1300' y2='200'/%3E%3Cline x1='1300' y1='300' x2='1300' y2='400'/%3E%3Cline x1='1300' y1='500' x2='1300' y2='1200'/%3E%3Cline x1='1400' y1='100' x2='1400' y2='300'/%3E%3Cline x1='1400' y1='400' x2='1400' y2='600'/%3E%3Cline x1='1400' y1='700' x2='1400' y2='900'/%3E%3Cline x1='1400' y1='1100' x2='1400' y2='1300'/%3E%3Cline x1='1500' y1='100' x2='1500' y2='400'/%3E%3Cline x1='1500' y1='600' x2='1500' y2='800'/%3E%3Cline x1='1500' y1='900' x2='1500' y2='1100'/%3E%3Cline x1='1500' y1='1200' x2='1500' y2='1400'/%3E%3Cline x1='1600' y1='0' x2='1600' y2='1400'/%3E%3C/g%3E%3C/svg%3E");

    /* Accents */
    --accent-warm:    #3b82f6;   /* crisp blue — primary CTA */
    --accent-warm-2:  #1d4ed8;   /* deep blue — CTA gradient end */
    --accent-gold:    #f5a623;
    --accent-cool:    #8e8cff;   /* lavender */
    --accent-success: #2bd199;   /* mint */
    --accent-danger:  #ff6b8b;   /* soft coral-red */

    /* Shadows */
    --shadow-soft: 0 6px 22px rgba(130, 100, 190, 0.22);
    --shadow-lift: 0 14px 38px rgba(130, 100, 190, 0.3);

    /* Radii */
    --r-sm: 12px;
    --r-md: 16px;
    --r-lg: 22px;
    --r-pill: 50px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

html {
    background: transparent;
    height: 100%;
    overscroll-behavior: none;
}

/* Background gradient as a fixed pseudo-element — `position: fixed; inset: 0`
   ALWAYS fills the visible viewport including iOS PWA home-indicator strip,
   regardless of how html / body compute their height. This is the only
   approach that survives the iOS standalone "wrong height on first launch"
   bug; rotating + back used to be needed because we depended on html height. */
html::before {
    content: '';
    position: fixed;
    inset: 0;
    z-index: -100;
    background: linear-gradient(135deg, #bae6fd 0%, #ffffff 50%, #bae6fd 100%);
    pointer-events: none;
}

body {
    font-family: var(--font-body);
    background: transparent;
    color: var(--ink);
    overflow: hidden;
    /* `position: fixed; inset: 0` ครอบ visible viewport เสมอบน iOS PWA
       standalone — ไม่พึ่งการคำนวณ height ของ html/body ที่ iOS รายงานผิด
       ในจังหวะแรกหลัง launch แนวตั้ง */
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    height: 100vh;
    height: 100dvh;
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
    /* `manipulation` = ปกติแต่ตัด double-tap-to-zoom — กันท่ากดจอ 2 ครั้ง
       แล้วซูมเองโดยไม่ตั้งใจ (iOS Safari ไม่ honor user-scalable=no เสมอ
       เนื่องจาก accessibility — touch-action เป็นวิธีที่ใช้ได้แน่นอน) */
    touch-action: manipulation;
    -ms-touch-action: manipulation;
}

/* iOS PWA standalone — the JS-measured --app-height (set by inline script in
   <head>) is more accurate than 100vh / 100dvh during first launch in
   portrait. Backgrounds use html::before above which doesn't depend on this
   value, but layout still benefits from a correct body height. */
@media (display-mode: standalone) {
    html, body {
        height: var(--app-height, 100dvh) !important;
        min-height: var(--app-height, 100dvh) !important;
    }
}

#gameCanvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: block;
    image-rendering: -webkit-optimize-contrast;
}

/* ---- HUD ---- */
#hud {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 10;
}

.hud-panel {
    position: fixed;
    z-index: 10;
    background: rgba(255, 255, 255, 0.88);
    border-radius: var(--r-md);
    padding: 8px 16px;
    color: var(--ink);
    display: flex;
    flex-direction: column;
    align-items: center;
    border: 1px solid var(--glass-border);
    box-shadow: var(--shadow-soft);
    pointer-events: auto;
    transition: opacity 0.3s;
}

#hud-top-left {
    top: calc(12px + env(safe-area-inset-top));
    left: calc(12px + env(safe-area-inset-left));
    align-items: flex-start;
    padding: 6px 12px;
    min-width: 105px;
}

.score-panel {
    display: flex;
    flex-direction: row;
    gap: 16px;
}

.score-group {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

#hud-top-right .score-group {
    align-items: flex-end;
}

.hud-value {
    font-variant-numeric: tabular-nums;
    min-width: 2.5ch;
    text-align: left;
}

#hud-top-right .hud-value {
    text-align: right;
}

.hud-value.secondary {
    color: var(--ink-soft);
    text-shadow: none;
}

#hud-top-right {
    top: calc(12px + env(safe-area-inset-top));
    right: calc(12px + env(safe-area-inset-right));
    align-items: flex-end;
    justify-content: flex-end;
    padding: 6px 12px;
    min-width: 105px;
}

/* Hearts row — top center, no panel background */
#hud-hearts {
    top: calc(12px + env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    flex-direction: row;
    gap: 6px;
    padding: 0;
    background: transparent;
    border: none;
    box-shadow: none;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
}

.heart {
    width: 28px;
    height: 28px;
    overflow: visible;
    filter: drop-shadow(0 2px 4px rgba(200, 16, 46, 0.45));
    transition: filter 0.35s ease, transform 0.25s ease;
}

.heart .heart-fill   { fill: #ff3a5c; transition: fill 0.35s ease; }
.heart .heart-stroke { fill: none; stroke: #c81f3d; stroke-width: 1.4; stroke-linejoin: round; transition: stroke 0.35s ease; }
.heart .heart-shine  { fill: rgba(255, 255, 255, 0.55); transition: fill 0.35s ease; }
.heart .heart-spark  { fill: rgba(255, 255, 255, 0.85); transition: fill 0.35s ease; }

.heart.lost {
    filter: none;
}
.heart.lost .heart-fill   { fill: #c5c1cc; }
.heart.lost .heart-stroke { stroke: #8e8995; }
.heart.lost .heart-shine  { fill: rgba(255, 255, 255, 0.25); }
.heart.lost .heart-spark  { fill: rgba(255, 255, 255, 0.35); }

.heart.pop {
    animation: heartPop 0.45s ease;
}

@keyframes heartPop {
    0% { transform: scale(1); }
    40% { transform: scale(1.4); }
    100% { transform: scale(1); }
}

.inventory-slots {
    display: flex;
    gap: 8px;
    margin-top: 4px;
}

.inv-slot {
    width: 60px;
    height: 60px;
    border-radius: var(--r-sm);
    background: var(--glass-soft);
    border: 1.5px dashed rgba(141, 120, 180, 0.55);
    color: var(--ink);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    line-height: 1.2;
    transition: all 0.3s ease;
}

.inv-slot.empty {
    opacity: 0.55;
}

.inv-slot.filled {
    border: 1.5px solid var(--accent-success);
    background: rgba(43, 209, 153, 0.18);
    box-shadow: 0 0 16px rgba(43, 209, 153, 0.45);
    opacity: 1;
}

.inv-slot-text {
    font-size: 0.5rem;
    color: var(--ink-soft);
    margin-top: 2px;
}

.inv-slot.filled .inv-slot-text {
    font-size: 0.45rem;
    font-weight: 700;
    color: #0f7052;
    text-align: center;
    word-break: break-all;
    line-height: 1;
}

#hud-level {
    display: none !important;
}

#level-name-text {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--ink);
    letter-spacing: 0.5px;
}

#hud-bottom-left {
    bottom: calc(16px + env(safe-area-inset-bottom));
    left: calc(12px + env(safe-area-inset-left));
    flex-direction: row;
    gap: 10px;
    padding: 8px 14px;
}

#hud-bottom-right {
    bottom: calc(16px + env(safe-area-inset-bottom));
    right: calc(12px + env(safe-area-inset-right));
    flex-direction: row;
    gap: 10px;
    padding: 8px 14px;
}

.hud-label {
    font-size: 0.55rem;
    color: var(--ink-muted);
    letter-spacing: 2px;
    text-transform: uppercase;
    font-weight: 700;
}

.hud-value {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--accent-warm);
    text-shadow: 0 1px 8px rgba(255, 154, 90, 0.3);
}

.hud-info-emoji {
    font-size: 1.6rem;
    line-height: 1;
    filter: drop-shadow(0 2px 4px rgba(130, 100, 190, 0.3));
}

img.hud-info-emoji {
    width: 1.9rem;
    height: 1.9rem;
    object-fit: contain;
    display: block;
}

.hud-info-detail {
    display: flex;
    flex-direction: column;
}

.hud-info-title {
    font-size: 0.65rem;
    font-weight: 700;
    color: var(--ink);
}

.hud-info-sub {
    font-size: 0.55rem;
    color: var(--ink-muted);
}

.hud-hidden {
    opacity: 0 !important;
    pointer-events: none !important;
}

/* ---- Right Controls Container ---- */
.right-controls {
    position: fixed;
    bottom: calc(16px + env(safe-area-inset-bottom));
    right: calc(16px + env(safe-area-inset-right));
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: 12px;
    transition: bottom 0.15s, right 0.15s;
    pointer-events: none;
}
.right-controls > button {
    pointer-events: auto;
}

body.is-playing .right-controls {
    bottom: calc(74px + env(safe-area-inset-bottom));
}

/* ---- Fullscreen toggle ---- */
.fullscreen-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--glass-strong);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(12px) saturate(1.3);
    -webkit-backdrop-filter: blur(12px) saturate(1.3);
    color: var(--ink);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-soft);
    padding: 0;
    transition: transform 0.15s, background 0.15s;
    -webkit-appearance: none;
    appearance: none;
    outline: none;
}

.fullscreen-btn.available { display: flex; }
.fullscreen-btn:active { transform: scale(0.92); }

.fullscreen-btn svg {
    width: 20px;
    height: 20px;
}

.fullscreen-btn .fs-icon-exit { display: none; }
body.is-fullscreen .fullscreen-btn .fs-icon-enter { display: none; }
body.is-fullscreen .fullscreen-btn .fs-icon-exit { display: block; }

/* ---- Audio toggle buttons (sound + bgm) ---- */
.audio-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--glass-strong);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(12px) saturate(1.3);
    -webkit-backdrop-filter: blur(12px) saturate(1.3);
    color: var(--ink);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-soft);
    padding: 0;
    transition: transform 0.15s, background 0.15s;
    -webkit-appearance: none;
    appearance: none;
    outline: none;
}

.audio-btn:active { transform: scale(0.92); }

.audio-btn svg {
    width: 20px;
    height: 20px;
}

/* Toggle which icon is visible based on muted-state class on the button */
.audio-btn .icon-sound-off,
.audio-btn .icon-bgm-off { display: none; }
.audio-btn.muted .icon-sound-on,
.audio-btn.muted .icon-bgm-on { display: none; }
.audio-btn.muted .icon-sound-off,
.audio-btn.muted .icon-bgm-off { display: block; }

@media (max-width: 768px), (pointer: coarse) {
    .right-controls {
        right: calc(12px + env(safe-area-inset-right));
        bottom: calc(12px + env(safe-area-inset-bottom));
        gap: 10px;
    }
    body.is-playing .right-controls {
        bottom: calc(70px + env(safe-area-inset-bottom));
    }
    .fullscreen-btn, .audio-btn {
        width: 36px;
        height: 36px;
    }
    .fullscreen-btn svg, .audio-btn svg { width: 18px; height: 18px; }
}

@media (orientation: landscape) and (max-height: 500px) {
    .right-controls {
        right: calc(12px + env(safe-area-inset-right));
        bottom: calc(12px + env(safe-area-inset-bottom));
        gap: 8px;
    }
    body.is-playing .right-controls {
        bottom: calc(60px + env(safe-area-inset-bottom));
    }
    .fullscreen-btn, .audio-btn {
        width: 34px;
        height: 34px;
    }
    .fullscreen-btn svg, .audio-btn svg { width: 16px; height: 16px; }
}

/* ---- Skip-level button ---- */
#skip-level-btn {
    position: fixed;
    bottom: calc(16px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    z-index: 30;
    background: var(--glass-strong);
    color: var(--ink);
    border: 1.5px solid var(--glass-border);
    border-radius: var(--r-pill);
    padding: 9px 20px;
    font-size: 0.88rem;
    font-weight: 600;
    font-family: var(--font-body);
    cursor: pointer;
    backdrop-filter: blur(12px) saturate(1.3);
    -webkit-backdrop-filter: blur(12px) saturate(1.3);
    box-shadow: var(--shadow-soft);
    display: none;
    -webkit-appearance: none;
    appearance: none;
    outline: none;
    transition: transform 0.15s, background 0.15s, box-shadow 0.15s;
    letter-spacing: 0.5px;
    white-space: nowrap;
}

#skip-level-btn:active {
    transform: translateX(-50%) scale(0.94);
    background: rgba(255, 255, 255, 0.95);
}

body.is-playing #skip-level-btn {
    display: inline-block;
}

/* ---- D-Pad ---- */
#dpad {
    position: fixed;
    bottom: calc(16px + env(safe-area-inset-bottom));
    left: calc(16px + env(safe-area-inset-left));
    z-index: 25;
    display: none;
}

/* ── Joystick ── */
#joystick-inner {
    display: none;
    width: 110px;
    height: 110px;
    border-radius: 50%;
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    backdrop-filter: blur(12px) saturate(1.3);
    -webkit-backdrop-filter: blur(12px) saturate(1.3);
    box-shadow: var(--shadow-soft);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    position: relative;
}
body.use-joystick #joystick-inner { display: block; }

#joystick-knob {
    position: absolute;
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    box-shadow: 0 2px 8px rgba(0,0,0,0.25);
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: transform 0.08s ease, background 0.1s;
    pointer-events: none;
}
#joystick-inner.joystick-active #joystick-knob {
    background: rgba(150,150,160,0.75);
    border-color: rgba(150,150,160,0.9);
}

/* ── D-Pad ── */
#dpad-inner {
    display: none;
    grid-template-areas: ". up ." "left center right" ". down .";
    grid-template-columns: repeat(3, 52px);
    grid-template-rows: repeat(3, 52px);
    gap: 4px;
}
body.use-dpad #dpad-inner { display: grid; }

#dpad-up    { grid-area: up; }
#dpad-left  { grid-area: left; }
.dpad-center { grid-area: center; }
#dpad-right { grid-area: right; }
#dpad-down  { grid-area: down; }

.dpad-btn {
    width: 52px;
    height: 52px;
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    border-radius: var(--r-md);
    color: var(--ink);
    font-size: 1.2rem;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    box-shadow: var(--shadow-soft);
    transition: background 0.1s, transform 0.08s;
    -webkit-appearance: none;
    appearance: none;
    outline: none;
}
.dpad-btn:active {
    background: rgba(140,140,152,0.85);
    color: var(--ink);
    transform: scale(0.92);
}
.dpad-btn.dpad-pressing {
    background: rgba(140,140,152,0.85);
    color: var(--ink);
    transform: scale(0.92);
}
/* ระหว่าง touch-slide: ยกเลิก :active ของ browser บนปุ่มที่ไม่ใช่ pressing จริง */
#dpad-inner.touch-active .dpad-btn:active:not(.dpad-pressing) {
    background: var(--glass-strong);
    color: var(--ink);
    transform: none;
}
.dpad-center {
    width: 52px;
    height: 52px;
    background: rgba(255,255,255,0.08);
    border-radius: var(--r-md);
}

/* ---- Start-screen wave canvas ---- */
#start-wave-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}
#start-overlay .overlay-content {
    position: relative;
    z-index: 1;
}

/* ---- Overlays ---- */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 100;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    overflow-y: auto;
    /* iOS momentum scroll + keep scroll inside the overlay so swiping at the
       edge doesn't bubble up to the LINE / Safari close gesture.
       `pan-y pinch-zoom` allows vertical scroll + pinch but blocks
       double-tap-to-zoom (same as `touch-action: manipulation` does on body). */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y pinch-zoom;
    background: linear-gradient(135deg, rgba(186, 230, 253, 0.94) 0%, rgba(255, 255, 255, 0.96) 50%, rgba(186, 230, 253, 0.94) 100%);
    backdrop-filter: blur(20px) saturate(1.3);
    -webkit-backdrop-filter: blur(20px) saturate(1.3);
}

.overlay.hidden {
    display: none !important;
}

/* Overlays with opaque gradient backgrounds don't need backdrop-filter —
   removing it avoids GPU blur recomputation that caused jank. */
#start-overlay,
#tutorial-overlay,
#intro-overlay.with-bg,
#result-overlay,
#gameover-overlay,
#victory-overlay {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
}

#tutorial-overlay,
#result-overlay,
#victory-overlay,
#gameover-overlay {
    background: linear-gradient(135deg, #bae6fd 0%, #ffffff 50%, #bae6fd 100%);
}

/* Start screen only: soft white vignette above the maze so the centred
   content card stays easy to read. */
#start-overlay {
    background-image: var(--maze-vignette), var(--maze-pattern), linear-gradient(135deg, #bae6fd 0%, #ffffff 50%, #bae6fd 100%);
    background-size: 100% 100%, cover, auto;
    background-position: center, center, center;
    background-repeat: no-repeat, no-repeat, no-repeat;
}

#intro-overlay,
#question-overlay {
    background: transparent;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
}

/* When the intro is showing the "Found your CPAP" placeholder card, add the
   same blue/white wash that the start + game-over screens use. */
#intro-overlay.with-bg {
    background: linear-gradient(135deg, rgba(186, 230, 253, 0.94) 0%, rgba(255, 255, 255, 0.96) 50%, rgba(186, 230, 253, 0.94) 100%);
}

.overlay-content {
    text-align: center;
    color: var(--ink);
    max-width: 440px;
    width: 90%;
    padding: 40px 28px;
    margin: auto 0;
    animation: fadeInUp 0.6s ease-out;
}

.result-content {
    max-width: 700px;
}

@media (min-width: 769px) {
    .overlay-content {
        max-width: 560px;
    }
    .result-content {
        max-width: 760px;
    }
}

@media (pointer: fine) {
    #result-overlay {
        scrollbar-gutter: stable;
    }
    .share-btn {
        right: calc(16px + env(safe-area-inset-right));
    }
    .result-details {
        flex-direction: row;
        gap: 12px;
    }
    .result-item {
        flex: 1;
        min-width: 0;
    }
}

@media (min-width: 769px) and (pointer: fine) {
    .result-content {
        max-width: 900px;
        width: 95%;
    }
    .result-btn-group {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 4px;
        margin-top: 4px;
    }
    .result-btn-group button {
        width: 550px !important;
        display: block !important;
    }
    .result-details {
        flex-direction: row !important;
        gap: 16px;
    }
    .result-item {
        flex: 1;
        min-width: 0;
    }
    .result-image-placeholder {
        width: min(240px, 100%) !important;
        height: 180px !important;
    }
    .result-image-placeholder.multi-img {
        width: min(480px, 100%) !important;
    }
    .result-image-placeholder.three-img {
        width: min(400px, 100%) !important;
    }
    .product-img-label {
        font-size: 0.8rem !important;
    }
}

/* Start Screen */
.title-logo {
    width: 180px;
    height: auto;
    display: block;
    margin: 0 auto 18px;
}

#game-title {
    font-family: var(--font-heading);
    font-size: 3.2rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent-warm) 0%, var(--accent-warm-2) 50%, var(--accent-cool) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin: 8px 0;
    line-height: 1.2;
    letter-spacing: -0.02em;
    filter: drop-shadow(0 4px 14px rgba(59, 130, 246, 0.35));
}

.subtitle {
    font-size: 1.15rem;
    color: var(--ink-soft);
    margin-bottom: 20px;
    font-weight: 700;
}

/* Responsive line-break helpers:
   - .br-portrait: shown only on mobile portrait
   - .br-wide: shown on desktop + mobile landscape (hidden on portrait)
   - .br-not-landscape: shown on desktop + mobile portrait (hidden on landscape) */
.br-portrait { display: none; }
.br-wide { display: inline; }
.br-not-landscape { display: inline; }

.desc {
    font-size: 0.85rem;
    color: var(--ink-muted);
    line-height: 1.7;
    margin-bottom: 30px;
}

.primary-btn {
    background: linear-gradient(135deg, var(--accent-warm), var(--accent-warm-2));
    color: #fff;
    border: none;
    padding: 16px 52px;
    border-radius: var(--r-pill);
    font-size: 1.15rem;
    font-weight: 700;
    font-family: var(--font-body);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 8px 24px rgba(59, 130, 246, 0.45);
    letter-spacing: 1px;
    text-shadow: 0 1px 2px rgba(29, 78, 216, 0.4);
}

.primary-btn:hover {
    transform: translateY(-3px) scale(1.04);
    box-shadow: 0 14px 34px rgba(59, 130, 246, 0.6);
}

.primary-btn:active {
    transform: scale(0.97);
}

.secondary-btn {
    background: var(--glass-strong);
    color: var(--ink);
    border: 1.5px solid rgba(141, 120, 180, 0.4);
    padding: 12px 40px;
    border-radius: var(--r-pill);
    font-size: 0.95rem;
    font-weight: 600;
    font-family: var(--font-body);
    cursor: pointer;
    transition: transform 0.2s, background 0.2s, border-color 0.2s;
    letter-spacing: 1px;
    margin-top: 14px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

.secondary-btn:hover {
    background: #fff;
    border-color: var(--accent-cool);
    transform: translateY(-2px);
}

.secondary-btn:active {
    transform: scale(0.97);
}



/* ==================== TUTORIAL ==================== */
.tutorial-content {
    max-width: 640px;
}

.tut-title {
    font-family: var(--font-heading);
    font-size: 2rem;
    font-weight: 700;
    color: var(--ink);
    margin-bottom: 4px;
    letter-spacing: -0.01em;
}

.tut-subtitle {
    color: var(--ink-soft);
    margin-bottom: 18px;
    font-size: 0.92rem;
    line-height: 1.5;
}

.tut-grid {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 20px;
}

.tut-card {
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    border-radius: var(--r-md);
    padding: 14px 18px;
    text-align: left;
    box-shadow: var(--shadow-soft);
}

.tut-card h3 {
    font-family: var(--font-heading);
    font-size: 1rem;
    font-weight: 700;
    color: var(--ink);
    margin-bottom: 10px;
    letter-spacing: 0.02em;
}

.tut-card.tut-danger {
    border-color: rgba(255, 107, 139, 0.45);
}
.tut-card.tut-danger h3 {
    color: var(--accent-danger);
}

.tut-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 0;
    margin: 0;
}

.tut-list li {
    display: flex;
    align-items: center;
    gap: 12px;
}

.tut-emoji {
    font-size: 1.5rem;
    flex-shrink: 0;
    line-height: 1.3;
    width: 36px;
    text-align: center;
    filter: drop-shadow(0 2px 4px rgba(130, 100, 190, 0.25));
}

.tut-dot-icon {
    color: #ffd700;
    font-size: 2.5rem;
    line-height: 0.6;
    text-shadow: 0 0 10px rgba(255, 215, 0, 0.6);
}

.tut-hearts {
    font-size: 1.6rem;
    line-height: 1;
}

.tut-img {
    width: 38px;
    height: 38px;
    object-fit: contain;
    flex-shrink: 0;
    filter: drop-shadow(0 2px 6px rgba(255, 107, 139, 0.4));
}

.tut-list li > div {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1;
    min-width: 0;
}

.tut-list b {
    font-size: 0.92rem;
    color: var(--ink);
    font-weight: 700;
}

.tut-list small {
    font-size: 0.76rem;
    color: var(--ink-muted);
    line-height: 1.5;
}

.tut-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    flex-wrap: wrap;
}

.tut-keys-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.tut-key-row {
    display: flex;
    gap: 4px;
    justify-content: center;
}

.tut-key-blank {
    display: inline-block;
    width: 32px;
    height: 32px;
}

.tut-controls kbd {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    background: linear-gradient(180deg, #ffffff, #e2e8f0);
    border: 1px solid #94a3b8;
    border-bottom-width: 2.5px;
    border-radius: 6px;
    color: var(--ink);
    font-size: 0.85rem;
    font-weight: 700;
    font-family: var(--font-body);
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.1);
}

.tut-or {
    color: var(--ink-muted);
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 1px;
    text-transform: uppercase;
}

.tut-joystick-illus {
    width: 70px;
    height: 70px;
    border-radius: 50%;
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    box-shadow: var(--shadow-soft);
    position: relative;
    flex-shrink: 0;
}
.tut-joystick-knob {
    position: absolute;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
    top: 50%;
    left: 50%;
    transform: translate(calc(-50% + 10px), calc(-50% - 10px));
}

.tut-hint {
    color: var(--ink-muted);
    font-size: 0.78rem;
    text-align: center;
}

.tut-card-skip {
    grid-column: 1 / -1;
}
.tut-skip-body {
    display: flex;
    align-items: center;
    gap: 16px;
}
.tut-skip-body small {
    color: var(--ink-muted);
    font-size: 0.76rem;
    line-height: 1.4;
}
.tut-skip-btn-preview {
    flex-shrink: 0;
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    border-radius: var(--r-pill);
    padding: 7px 18px;
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--ink);
    letter-spacing: 0.5px;
    white-space: nowrap;
    box-shadow: var(--shadow-soft);
}

/* ---- Control Picker ---- */
.ctrl-picker-content {
    max-width: 440px;
    width: 100%;
    text-align: center;
    padding: 32px 24px 28px;
}
.ctrl-picker-title {
    font-size: 1.6rem;
    font-weight: 700;
    margin-bottom: 6px;
}
.ctrl-picker-sub {
    color: var(--ink-muted);
    font-size: 0.9rem;
    margin-bottom: 28px;
}
.ctrl-options {
    display: flex;
    gap: 16px;
    justify-content: center;
}
.ctrl-option {
    flex: 1;
    max-width: 180px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    padding: 28px 16px 22px;
    background: var(--glass);
    border: 2px solid var(--glass-border);
    border-radius: var(--r-card);
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s, transform 0.1s;
    font-family: var(--font-body);
    color: var(--ink);
    -webkit-appearance: none;
    appearance: none;
    outline: none;
}
.ctrl-option:hover  { border-color: var(--accent-warm); background: var(--glass-strong); }
.ctrl-option:active { transform: scale(0.96); }
.ctrl-option b    { font-size: 1rem; }
.ctrl-option small { color: var(--ink-muted); font-size: 0.78rem; }

/* Joystick illustration inside picker */
.ctrl-illus-joystick {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    box-shadow: var(--shadow-soft);
    position: relative;
}
.ctrl-illus-knob {
    position: absolute;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

/* D-pad illustration inside picker */
.ctrl-illus-dpad {
    display: grid;
    grid-template-columns: repeat(3, 22px);
    grid-template-rows: repeat(3, 22px);
    gap: 3px;
}
.ctrl-illus-dpad span {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--glass-strong);
    border: 1px solid var(--glass-border);
    border-radius: 5px;
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--ink);
    box-shadow: var(--shadow-soft);
}
.ctrl-illus-dpad span:empty { background: transparent; border: none; box-shadow: none; }
.ctrl-dpad-mid {
    background: rgba(255,255,255,0.08);
    border: 1px solid var(--glass-border);
    border-radius: 5px;
}

/* Show desktop or mobile control hint based on viewport / pointer */
.tut-mobile-only { display: none; }

@media (max-width: 768px), (pointer: coarse) {
    .tut-desktop-only { display: none; }
    .tut-mobile-only  { display: flex; flex-direction: column; align-items: center; gap: 10px; }
}

/* Compact for landscape phones */
@media (orientation: landscape) and (max-height: 500px) {
    .tutorial-content { max-width: 720px; }
    .tut-title { font-size: 1.4rem; }
    .tut-subtitle { font-size: 0.78rem; margin-bottom: 10px; }
    .tut-grid { gap: 8px; margin-bottom: 12px; }
    .tut-card { padding: 10px 14px; }
    .tut-card h3 { font-size: 0.85rem; margin-bottom: 6px; }
    .tut-list { gap: 6px; }
    .tut-list b { font-size: 0.8rem; }
    .tut-list small { font-size: 0.66rem; line-height: 1.4; }
    .tut-emoji { font-size: 1.2rem; width: 28px; }
    .tut-img { width: 30px; height: 30px; }
    .tut-controls kbd { width: 26px; height: 26px; font-size: 0.72rem; border-radius: 5px; }
    .tut-key-blank { width: 26px; height: 26px; }
    .tut-joystick-illus { width: 58px; height: 58px; }
    .tut-joystick-knob { width: 24px; height: 24px; }
}

/* Intro Screen */
.intro-content {
    animation: zoomPulse 0.6s ease-out;
}

#intro-image-placeholder {
    /* Always 1:1 regardless of viewport; size varies per breakpoint */
    width: 240px;
    max-width: 240px;
    aspect-ratio: 1 / 1;
    height: auto;
    margin: 0 auto 14px;
    display: none;
    font-size: 0.95rem;
}

#intro-image-placeholder.hidden { display: none !important; }

/* Desktop: keep intro title + subtitle on a single line each */
@media (min-width: 769px) {
    .intro-content {
        max-width: 760px;
        width: auto;
    }
    #intro-level,
    #intro-name {
        white-space: nowrap;
    }
}

.big-emoji {
    font-size: 4rem;
    margin-bottom: 12px;
    filter: drop-shadow(0 6px 14px rgba(130, 100, 190, 0.3));
}

.intro-content h2 {
    font-family: var(--font-heading);
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 8px;
    color: #ffffff;
    text-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
    letter-spacing: -0.01em;
}

.intro-subtitle {
    font-size: 1.1rem;
    color: rgba(255, 255, 255, 0.85);
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
    font-weight: 700;
}

/* Tap-to-continue hint on device-found card */
.intro-tap-hint {
    margin-top: 24px;
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--accent);
    letter-spacing: 0.02em;
    animation: tap-hint-blink 1.4s ease-in-out infinite;
}

@keyframes tap-hint-blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0.3; }
}

/* When "found device" card shows on the light gradient backdrop,
   flip white text to dark so it doesn't vanish on the near-white bg */
#intro-overlay.with-bg .intro-content h2 {
    color: var(--ink);
    text-shadow: none;
}
#intro-overlay.with-bg .intro-subtitle {
    color: var(--ink-soft);
    text-shadow: none;
}

/* Question Modal */
.question-content {
    background: var(--glass-strong);
    border: 1.5px solid var(--glass-border);
    border-radius: var(--r-lg);
    padding: 36px 28px;
    backdrop-filter: blur(24px) saturate(1.3);
    -webkit-backdrop-filter: blur(24px) saturate(1.3);
    box-shadow: 0 20px 55px rgba(130, 100, 190, 0.3);
    color: var(--ink);
}

.question-content h2 {
    font-size: 1.15rem;
    font-weight: 600;
    line-height: 1.7;
    margin-bottom: 28px;
    color: var(--ink);
}

/* Question progress bar — keeps the player oriented across the 10-question journey */
.q-progress {
    margin-bottom: 22px;
}

.q-progress-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    margin-bottom: 6px;
}

.q-progress-label {
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 1.5px;
    text-transform: uppercase;
    color: var(--ink-muted);
}

.q-progress-text {
    font-size: 0.78rem;
    font-weight: 700;
    color: var(--accent-warm);
    font-variant-numeric: tabular-nums;
}

.q-progress-track {
    height: 8px;
    background: rgba(141, 120, 180, 0.18);
    border-radius: 999px;
    overflow: hidden;
    box-shadow: inset 0 1px 2px rgba(60, 40, 100, 0.1);
}

.q-progress-fill {
    height: 100%;
    width: 0%;
    border-radius: 999px;
    background: linear-gradient(90deg, var(--accent-warm), var(--accent-warm-2));
    box-shadow: 0 0 8px rgba(59, 130, 246, 0.5);
    transition: width 0.55s cubic-bezier(0.22, 1, 0.36, 1);
}

.q-buttons {
    display: flex;
    gap: 12px;
}

.q-btn {
    flex: 1;
    padding: 14px 10px;
    border: none;
    border-radius: var(--r-md);
    font-size: 0.95rem;
    font-weight: 700;
    font-family: var(--font-body);
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
    line-height: 1.4;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}

.q-btn-yes {
    background: linear-gradient(135deg, #6de8b5, var(--accent-success));
    box-shadow: 0 6px 20px rgba(43, 209, 153, 0.45);
}

.q-btn-no {
    background: linear-gradient(135deg, #ff8fa5, var(--accent-danger));
    box-shadow: 0 6px 20px rgba(255, 107, 139, 0.45);
}

.q-btn:hover {
    transform: translateY(-2px);
}

.q-btn:active {
    transform: scale(0.96);
}

/* Game Over & Result */
.gameover-content h2,
.victory-content h2,
.result-content h2 {
    font-family: var(--font-heading);
    font-size: 2.2rem;
    font-weight: 700;
    margin-bottom: 8px;
    color: var(--ink);
    letter-spacing: -0.01em;
}

.result-content h2 {
    font-size: 1.6rem;
    margin-bottom: 6px;
}

.result-content .victory-sub {
    margin-bottom: 14px;
}

.result-details {
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--glass-soft);
    padding: 12px;
    border-radius: var(--r-md);
    margin-bottom: 14px;
    border: 1px solid var(--glass-border);
}

.result-item {
    flex: 1;
    min-width: 0;
    background: var(--glass-strong);
    padding: 10px;
    border-radius: var(--r-sm);
    border: 1px solid var(--glass-border);
    box-shadow: var(--shadow-soft);
    display: flex;
    flex-direction: column;
}

.result-label {
    display: block;
    font-size: 0.7rem;
    color: var(--ink-muted);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 700;
}

.result-item h3 {
    font-family: var(--font-heading);
    font-size: 1.05rem;
    color: var(--accent-warm);
    margin: 0;
    font-weight: 700;
    line-height: 1.2;
}

.result-note {
    font-size: 0.75rem;
    color: var(--ink-muted);
    margin-top: 6px;
    line-height: 1.4;
    white-space: nowrap;
    overflow: hidden;
    display: block;
}

.result-image-placeholder {
    width: min(120px, 100%);
    height: 90px;
    flex-shrink: 0;
    background: rgba(141, 120, 180, 0.12);
    border: 2px dashed rgba(141, 120, 180, 0.45);
    border-radius: var(--r-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--ink-muted);
    font-size: 0.75rem;
    font-weight: 400;
    margin: 6px auto;
    letter-spacing: 0.5px;
    overflow: hidden;
}
.product-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    border-radius: var(--r-sm);
}
.result-image-placeholder.multi-img {
    width: min(240px, 100%);
    gap: 6px;
    padding: 4px;
    align-items: stretch;
}
.result-image-placeholder.three-img {
    width: min(320px, 100%);
}
.result-image-placeholder.multi-img > .product-img {
    flex: 1;
    min-width: 0;
    width: 0;
}
.product-img-item {
    flex: 1;
    min-width: 0;
    width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
}
.product-img-item .product-img {
    flex: 1;
    width: 100%;
    min-height: 0;
}
.product-img-label {
    font-size: 0.6rem;
    font-weight: 600;
    color: var(--ink-muted);
    letter-spacing: 0.2px;
    line-height: 1.2;
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

.result-content .score-display {
    margin-bottom: 18px;
    font-size: 1.1rem;
}

/* Portrait + desktop: cards stack vertically; cap 1:1 placeholder by viewport
   height so both cards + h2 + button still fit on screen together. */
@media (orientation: portrait) and (max-width: 768px) {
    .result-image-placeholder { width: min(120px, 100%); }
    .result-image-placeholder.multi-img { width: min(240px, 100%); }
    .result-image-placeholder.three-img { width: min(300px, 100%); }
}
@media (orientation: portrait) and (max-height: 720px) {
    .result-image-placeholder { width: min(90px, 100%); height: 67.5px; }
    .result-image-placeholder.multi-img { width: min(180px, 100%); }
    .result-image-placeholder.three-img { width: min(260px, 100%); }
}

.go-sub,
.victory-sub {
    font-size: 0.95rem;
    color: var(--ink-soft);
    margin-bottom: 10px;
    font-weight: 700;
}

.score-display {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--accent-warm);
    margin-bottom: 28px;
    text-shadow: 0 2px 10px rgba(255, 154, 90, 0.3);
}

.gameover-content .primary-btn,
.victory-content .primary-btn {
    margin-top: 8px;
}

/* ---- Item Reveal Screen ---- */
#reveal-overlay {
    background: linear-gradient(135deg, #bae6fd 0%, #ffffff 50%, #bae6fd 100%);
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.reveal-scene {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    gap: 16px;
}

.reveal-rays {
    position: absolute;
    width: 60vmax;
    height: 60vmax;
    top: 50%;
    left: 50%;
    background: repeating-conic-gradient(
        rgba(255, 253, 224, 0.45) 0deg 20deg,
        transparent 20deg 40deg
    );
    mask-image: radial-gradient(
        circle at center,
        black 0%,
        black 40%,
        transparent 52%
    );
    -webkit-mask-image: radial-gradient(
        circle at center,
        black 0%,
        black 40%,
        transparent 52%
    );
    animation: revealSpin 40s linear infinite;
    will-change: transform;
    pointer-events: none;
}

.reveal-glow {
    position: absolute;
    width: 260px;
    height: 260px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.45) 0%, rgba(255, 255, 255, 0.15) 50%, transparent 70%);
    animation: revealGlowPulse 2s ease-in-out infinite;
    will-change: transform;
    pointer-events: none;
    z-index: 1;
}

.reveal-img-wrap {
    position: relative;
    z-index: 2;
    width: 200px;
    height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.reveal-img-wrap::before {
    content: '';
    position: absolute;
    inset: -40px;
    border-radius: 50%;
    background: radial-gradient(circle at center, rgba(255, 255, 255, 0.5) 40%, transparent 75%);
    z-index: -1;
}
.reveal-img-wrap.multi-img {
    width: 340px;
    height: 170px;
    gap: 12px;
}

.reveal-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.reveal-label {
    position: relative;
    z-index: 2;
    font-size: 0.85rem;
    font-weight: 600;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: var(--ink-soft);
    margin: 0;
}

.reveal-name {
    position: relative;
    z-index: 2;
    font-family: var(--font-heading);
    font-size: 2rem;
    font-weight: 700;
    color: var(--ink);
    text-shadow: 0 2px 12px rgba(255, 200, 60, 0.4);
    margin: 0;
    text-align: center;
}

/* Reveal entry animation */
.reveal-scene.reveal-in .reveal-img-wrap {
    animation: revealItemIn 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
.reveal-scene.reveal-in .reveal-label {
    animation: revealTextIn 0.5s ease-out 0.3s both;
}
.reveal-scene.reveal-in .reveal-name {
    animation: revealTextIn 0.5s ease-out 0.45s both;
}
/* Reveal exit animation */
.reveal-scene.reveal-out {
    animation: revealSceneOut 0.6s ease-in forwards;
}

@keyframes revealSpin {
    from { transform: translate(-50%, -50%) rotate(0deg); }
    to   { transform: translate(-50%, -50%) rotate(360deg); }
}
@keyframes revealGlowPulse {
    0%, 100% { transform: scale(1);    opacity: 0.8; }
    50%       { transform: scale(1.15); opacity: 1;   }
}
@keyframes revealItemIn {
    from { transform: scale(0.3); opacity: 0; }
    to   { transform: scale(1);   opacity: 1; }
}
@keyframes revealTextIn {
    from { transform: translateY(16px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}
@keyframes revealSceneOut {
    from { opacity: 1; transform: scale(1); }
    to   { opacity: 0; transform: scale(0.55); }
}

@media (max-width: 768px), (pointer: coarse) {
    .reveal-img-wrap { width: 160px; height: 160px; }
    .reveal-img-wrap.multi-img { width: 280px; height: 140px; }
    .reveal-glow     { width: 200px; height: 200px; }
    .reveal-name     { font-size: 1.5rem; }
}

/* ---- Share Button ---- */
.share-btn {
    position: fixed;
    top: calc(14px + env(safe-area-inset-top));
    right: calc(14px + env(safe-area-inset-right));
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: var(--glass-strong);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(12px) saturate(1.3);
    -webkit-backdrop-filter: blur(12px) saturate(1.3);
    box-shadow: var(--shadow-soft);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 200;
    color: var(--ink);
    transition: transform 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
    padding: 0;
}
.share-btn.visible { display: flex; }
.share-btn svg { width: 20px; height: 20px; }
.share-btn:hover  { background: var(--glass); transform: scale(1.1); box-shadow: 0 4px 16px rgba(59,130,246,0.25); }
.share-btn:active { transform: scale(0.92); }

@media (max-width: 768px), (pointer: coarse) {
    .share-btn { width: 40px; height: 40px; }
    .share-btn svg { width: 18px; height: 18px; }
}

/* ---- Share Toast ---- */
#share-toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    background: rgba(30, 30, 40, 0.88);
    color: #fff;
    font-size: 0.88rem;
    padding: 10px 22px;
    border-radius: var(--r-pill);
    white-space: nowrap;
    z-index: 9999;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.25s ease, transform 0.25s ease;
    backdrop-filter: blur(8px);
}
#share-toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ---- Animations ---- */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(35px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-18px); }
}

@keyframes zoomPulse {
    0% {
        opacity: 0;
        transform: scale(0.7);
    }
    70% {
        transform: scale(1.05);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.3); opacity: 0.7; }
}

/* ---- Desktop: never show D-pad ---- */
@media (min-width: 769px) and (pointer: fine) {
    #dpad {
        display: none !important;
    }
}

/* Portrait mobile: swap to the portrait-friendly break position */
@media (max-width: 768px) and (orientation: portrait) {
    .br-portrait { display: inline; }
    .br-wide { display: none; }

    #joystick-inner { width: 100px; height: 100px; }
    #joystick-knob  { width: 42px;  height: 42px;  }
    #dpad-inner { grid-template-columns: repeat(3, 44px); grid-template-rows: repeat(3, 44px); }
    .dpad-btn, .dpad-center { width: 44px; height: 44px; font-size: 1rem; }

    /* Bigger hearts on portrait phones */
    #hud-hearts { gap: 8px; }
    .heart {
        width: 32px;
        height: 32px;
    }

    /* Skip-level: pinned to the very bottom edge between the corner HUDs */
    #skip-level-btn {
        bottom: calc(16px + env(safe-area-inset-bottom));
        padding: 5px 14px;
        font-size: 0.72rem;
    }
}

/* ---- Mobile Responsive (portrait / touch) ---- */
@media (max-width: 768px), (pointer: coarse) {
    #dpad {
        display: block;
        bottom: calc(90px + env(safe-area-inset-bottom));
        left: calc(12px + env(safe-area-inset-left));
        right: auto;
    }

    /* Portrait: D-pad bottom-left above Sleep Monster warning; target HUD bottom-right */
    #hud-bottom-left {
        bottom: calc(16px + env(safe-area-inset-bottom));
        left: calc(12px + env(safe-area-inset-left));
    }

    #hud-bottom-right {
        bottom: calc(16px + env(safe-area-inset-bottom));
        right: calc(12px + env(safe-area-inset-right));
    }

    #hud-top-left,
    #hud-top-right {
        min-width: 88px;
    }

    #hud-hearts {
        gap: 4px;
        padding: 0;
    }

    .heart {
        width: 22px;
        height: 22px;
    }

    .hud-panel {
        padding: 6px 12px;
        border-radius: 10px;
    }

    .hud-value {
        font-size: 1.1rem;
    }

    .hud-info-emoji {
        font-size: 1.3rem;
    }

    img.hud-info-emoji {
        width: 1.6rem;
        height: 1.6rem;
    }

    .hud-info-title {
        font-size: 0.6rem;
    }

    .hud-info-sub {
        font-size: 0.5rem;
    }

    #game-title {
        font-size: 2.4rem;
    }

    .title-emoji {
        font-size: 3.5rem;
    }

    .desc {
        font-size: 0.78rem;
        max-width: 320px;
        margin-left: auto;
        margin-right: auto;
    }

    /* On narrow phones, let the text wrap naturally instead of the fixed <br>
       points which cause ragged lines. */
    .desc br {
        display: none;
    }

    .title-logo {
        width: 150px;
        margin-bottom: 14px;
    }

    .subtitle {
        font-size: 0.95rem;
        margin-bottom: 16px;
    }

    .primary-btn {
        padding: 14px 40px;
        font-size: 1.05rem;
    }

    .question-content h2 {
        font-size: 1rem;
    }

    .q-btn {
        font-size: 0.82rem;
        padding: 12px 8px;
    }
}

/* ---- Narrow phone (portrait) ---- */
@media (max-width: 380px) and (orientation: portrait) {
    .hud-panel {
        padding: 5px 10px;
    }

    .hud-value {
        font-size: 1rem;
    }

    #hud-top-left,
    #hud-top-right {
        min-width: 82px;
    }

    #dpad { bottom: calc(80px + env(safe-area-inset-bottom)); }
    #joystick-inner { width: 96px; height: 96px; }
    #joystick-knob  { width: 40px; height: 40px; }
    #dpad-inner { grid-template-columns: repeat(3, 40px); grid-template-rows: repeat(3, 40px); }
    .dpad-btn, .dpad-center { width: 40px; height: 40px; font-size: 0.9rem; }

    .inv-slot {
        width: 50px;
        height: 50px;
        font-size: 1.2rem;
    }
}

/* Mobile landscape: hide breaks that should only appear on desktop + portrait */
@media (orientation: landscape) and (max-height: 500px) {
    .br-not-landscape { display: none; }
}

/* ---- Phone landscape ---- */
@media (orientation: landscape) and (max-height: 500px) {
    /* Joystick on left side, raised so Sleep Monster warning sits below it */
    #dpad {
        display: block;
        bottom: calc(70px + env(safe-area-inset-bottom));
        left: calc(12px + env(safe-area-inset-left));
        right: auto;
        top: auto;
    }
    #joystick-inner { width: 100px; height: 100px; }
    #joystick-knob  { width: 42px;  height: 42px;  }
    #dpad-inner { grid-template-columns: repeat(3, 42px); grid-template-rows: repeat(3, 42px); gap: 3px; }
    .dpad-btn, .dpad-center { width: 42px; height: 42px; font-size: 0.95rem; border-radius: 12px; }

    /* Compact HUD */
    #hud-top-left,
    #hud-top-right {
        top: calc(8px + env(safe-area-inset-top));
        min-width: 88px;
    }

    #hud-top-left { left: calc(8px + env(safe-area-inset-left)); }
    #hud-top-right { right: calc(8px + env(safe-area-inset-right)); }

    .hud-panel {
        padding: 5px 10px;
        border-radius: 10px;
    }

    .hud-value { font-size: 1rem; }
    .hud-value.secondary { font-size: 0.85rem; }
    .hud-label { font-size: 0.5rem; letter-spacing: 1.5px; }

    /* Shrink inventory */
    .inv-slot {
        width: 40px;
        height: 40px;
        font-size: 1rem;
    }

    .inv-slot-text { font-size: 0.38rem; }
    .inv-slot.filled .inv-slot-text { font-size: 0.36rem; }

    /* Landscape: Sleep Monster warning at bottom-left (below D-pad), target HUD bottom-right */
    #hud-bottom-left {
        bottom: calc(12px + env(safe-area-inset-bottom));
        left: calc(12px + env(safe-area-inset-left));
        padding: 5px 10px;
    }

    #hud-bottom-right {
        bottom: calc(12px + env(safe-area-inset-bottom));
        right: calc(12px + env(safe-area-inset-right));
        padding: 5px 10px;
    }

    .hud-info-emoji { font-size: 1.15rem; }
    img.hud-info-emoji { width: 1.4rem; height: 1.4rem; }
    .hud-info-title { font-size: 0.58rem; }
    .hud-info-sub { font-size: 0.48rem; }

    /* Overlays fit shorter screens */
    .overlay-content {
        padding: 18px 22px;
        max-width: 520px;
    }

    .title-emoji {
        font-size: 2.6rem;
        margin-bottom: 4px;
    }

    #game-title {
        font-size: 1.9rem;
        margin: 4px 0;
    }

    .subtitle {
        font-size: 0.85rem;
        margin-bottom: 8px;
    }

    .desc {
        font-size: 0.72rem;
        margin-bottom: 14px;
    }

    .title-logo {
        width: 110px;
        margin-bottom: 6px;
    }

    .primary-btn {
        padding: 11px 32px;
        font-size: 0.95rem;
    }

    .secondary-btn {
        padding: 9px 28px;
        font-size: 0.8rem;
        margin-top: 8px;
    }

    .controls-info {
        margin-top: 10px;
        font-size: 0.65rem;
        gap: 16px;
    }

    .big-emoji {
        font-size: 2.4rem;
        margin-bottom: 4px;
    }

    .intro-content h2 { font-size: 1.4rem; }
    .intro-subtitle { font-size: 0.85rem; }

    .question-content {
        padding: 20px 22px;
    }

    .question-content h2 {
        font-size: 0.9rem;
        margin-bottom: 14px;
        line-height: 1.5;
    }

    .q-btn {
        font-size: 0.8rem;
        padding: 10px 8px;
    }

    .gameover-content h2,
    .victory-content h2,
    .result-content h2 {
        font-size: 1.5rem;
        margin-bottom: 4px;
    }

    .result-content h2 {
        font-size: 1.3rem;
        margin-bottom: 12px;
    }

    .go-sub, .victory-sub {
        font-size: 0.8rem;
    }

    .score-display {
        font-size: 1rem;
        margin-bottom: 14px;
    }

    .result-details {
        flex-direction: row;
        gap: 8px;
        padding: 8px;
        margin-bottom: 8px;
    }

    .result-item {
        flex: 1;
        min-width: 0;
    }

    .result-item {
        padding: 8px;
    }

    .result-item h3 { font-size: 0.9rem; }
    .result-label { font-size: 0.55rem; letter-spacing: 0.5px; }
    .result-image-placeholder {
        width: min(120px, 100%);
        font-size: 0.6rem;
        margin: 3px auto;
    }
    .result-image-placeholder.multi-img {
        width: min(240px, 100%);
    }
    .result-image-placeholder.three-img {
        width: min(300px, 100%);
    }

    /* Intro placeholder stays 1:1; size bounded by viewport height */
    #intro-image-placeholder {
        width: auto;
        max-width: 34vh;
        height: 34vh;
        max-height: 34vh;
        aspect-ratio: 1 / 1;
        font-size: 0.65rem;
        margin: 0 auto 6px;
    }

    .result-content .score-display {
        font-size: 0.9rem;
        margin-bottom: 10px;
    }
}

/* Landscape phone: skip-level moves up next to the score panel (top-right) */
@media (orientation: landscape) and (max-height: 500px) {
    #skip-level-btn {
        top: calc(8px + env(safe-area-inset-top));
        right: calc(106px + env(safe-area-inset-right));
        bottom: auto;
        left: auto;
        transform: none;
        padding: 6px 14px;
        font-size: 0.78rem;
    }
    #skip-level-btn:active {
        transform: scale(0.94);
    }
}

/* ---- ResMed maze logo ---- */
#maze-logo {
    position: fixed;
    z-index: 10;
    pointer-events: none;
    object-fit: contain;
    /* Desktop & landscape default: beside #hud-top-left, positioned via JS */
    height: 38px;
    width: auto;
    opacity: 1;
    transition: opacity 0.3s;
}

/* Mobile portrait: center between hearts row and maze */
@media (max-width: 768px) and (orientation: portrait),
       (pointer: coarse) and (orientation: portrait) {
    #maze-logo {
        top: calc(50px + env(safe-area-inset-top));
        left: 50%;
        transform: translateX(-50%);
        height: 24px;
    }
}

/* Phone landscape: match compact HUD top offset, smaller logo */
@media (orientation: landscape) and (max-height: 500px) {
    #maze-logo {
        top: calc(8px + env(safe-area-inset-top));
        height: 22px;
    }
}

/* ---- Very short landscape (small phones) ---- */
@media (orientation: landscape) and (max-height: 380px) {
    #dpad { bottom: calc(60px + env(safe-area-inset-bottom)); }
    #joystick-inner { width: 88px; height: 88px; }
    #joystick-knob  { width: 36px; height: 36px; }
    #dpad-inner { grid-template-columns: repeat(3, 38px); grid-template-rows: repeat(3, 38px); }
    .dpad-btn, .dpad-center { width: 38px; height: 38px; }

    #hud-bottom-left {
        bottom: calc(10px + env(safe-area-inset-bottom));
    }

    .inv-slot {
        width: 36px;
        height: 36px;
        font-size: 0.9rem;
    }
}

/* Base Fixes & Modifications */

/* Equalize button sizes inside Game Over and Result panels */
.gameover-content button,
.result-content button {
    width: 100%;
    margin-right: 0 !important;
    margin-left: 0 !important;
    box-sizing: border-box;
    display: block;
}

.gameover-content .primary-btn,
.gameover-content .secondary-btn,
.result-content .primary-btn,
.result-content .secondary-btn {
    padding: 16px 32px;
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: 1px;
    margin: 0;
}

.gameover-content .secondary-btn,
.result-content .primary-btn,
.result-content .secondary-btn {
    margin-top: 12px;
}

@media (max-width: 768px), (pointer: coarse) {
    .gameover-content .primary-btn,
    .gameover-content .secondary-btn,
    .result-content .primary-btn,
    .result-content .secondary-btn {
        padding: 14px 24px;
        font-size: 1rem;
    }
}

@media (orientation: landscape) and (max-height: 500px) {
    .gameover-content .primary-btn,
    .gameover-content .secondary-btn,
    .result-content .primary-btn,
    .result-content .secondary-btn {
        padding: 11px 22px;
        font-size: 0.9rem;
    }
    .gameover-content .secondary-btn,
    .result-content .primary-btn,
    .result-content .secondary-btn {
        margin-top: 8px;
    }
}

/* Match the lift animation on all buttons inside Game Over + Result. */
.gameover-content .primary-btn:hover,
.gameover-content .secondary-btn:hover,
.result-content .lb-link-btn:hover,
.result-content .primary-btn:hover,
.result-content .secondary-btn:hover {
    transform: translateY(-3px) scale(1.04);
}
.gameover-content .primary-btn:active,
.gameover-content .secondary-btn:active,
.result-content .lb-link-btn:active,
.result-content .primary-btn:active,
.result-content .secondary-btn:active {
    transform: scale(0.97);
}

/* Result: "ดูอันดับ" → text link underline */
.result-content .lb-link-btn {
    background: none !important;
    border: none !important;
    box-shadow: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    color: var(--ink-soft) !important;
    font-size: 0.88rem !important;
    font-weight: 500 !important;
    text-decoration: underline !important;
    text-decoration-color: rgba(90, 66, 128, 0.45) !important;
    text-underline-offset: 3px;
    padding: 6px 8px !important;
    margin: -22px auto 20px !important;
    display: block !important;
    width: fit-content !important;
    cursor: pointer;
    transition: color 0.2s, text-decoration-color 0.2s !important;
    letter-spacing: normal !important;
    transform: none !important;
}
.result-content .lb-link-btn:hover {
    background: none !important;
    border: none !important;
    box-shadow: none !important;
    color: var(--accent-warm) !important;
    text-decoration-color: var(--accent-warm) !important;
    transform: none !important;
}
.result-content .lb-link-btn:active {
    transform: none !important;
    opacity: 0.7;
}

/* Result: "ออกเดินทางอีกครั้ง" → glass outline (เดิมของ lb-link-btn) */
.result-content .primary-btn {
    background: var(--glass-strong);
    color: var(--ink);
    border: 1.5px solid rgba(141, 120, 180, 0.4);
    text-shadow: none;
    box-shadow: 0 4px 18px rgba(100, 80, 160, 0.08);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}
.result-content .primary-btn:hover {
    background: #fff;
    border-color: var(--accent-cool);
    box-shadow: 0 8px 24px rgba(100, 80, 160, 0.15);
}

/* Result: "สอบถามเพิ่มเติม" → LINE green */
.result-content .secondary-btn {
    background: linear-gradient(135deg, #06C755 0%, #00A848 100%);
    color: #ffffff;
    border: none;
    text-shadow: 0 1px 2px rgba(0, 80, 30, 0.35);
    box-shadow: 0 8px 24px rgba(6, 199, 85, 0.45);
}
.result-content .secondary-btn:hover {
    background: linear-gradient(135deg, #06C755 0%, #00A848 100%);
    border: none;
    box-shadow: 0 14px 34px rgba(6, 199, 85, 0.6);
}


#dpad.dpad-hidden {
    display: none !important;
}

/* ==================== TOUCH DEVICE OVERRIDES ====================
   On touch devices a tap fires :hover and :hover sticks (no mouseleave) —
   the lift/scale transform freezes until the user taps somewhere else.

   Solution: `:hover:not(:active)` resets the styles back to base ONLY when
   the finger is no longer down. While the finger is still down, :active
   matches but :not(:active) doesn't, so the regular :active transforms
   (scale-down press feedback) keep working normally. */
@media (hover: none) {
    .primary-btn:hover:not(:active),
    .gameover-content .primary-btn:hover:not(:active),
    .result-content .primary-btn:hover:not(:active) {
        transform: none;
        box-shadow: 0 8px 24px rgba(59, 130, 246, 0.45);
    }

    .secondary-btn:hover:not(:active),
    .gameover-content .secondary-btn:hover:not(:active) {
        transform: none;
        background: var(--glass-strong);
        border-color: rgba(141, 120, 180, 0.4);
    }

    .lb-link-btn:hover:not(:active),
    .gameover-content .lb-link-btn:hover:not(:active) {
        transform: none;
        background: linear-gradient(135deg, #fbbf24, #f59e0b);
        box-shadow: 0 6px 18px rgba(245, 158, 11, 0.35);
    }

    .result-content .lb-link-btn:hover:not(:active) {
        transform: none;
        background: none;
        border: none;
        box-shadow: none;
        color: var(--accent-warm);
    }

    .result-content .secondary-btn:hover:not(:active) {
        transform: none;
        background: linear-gradient(135deg, #06C755 0%, #00A848 100%);
        box-shadow: 0 8px 24px rgba(6, 199, 85, 0.45);
        border: none;
    }

    .audio-btn:hover:not(:active),
    .fullscreen-btn:hover:not(:active) {
        transform: none;
        background: var(--glass-strong);
    }

    .lb-close:hover:not(:active) {
        transform: none;
        background: rgba(255, 255, 255, 0.7);
    }
}

/* landscape mobile — ชิดคะแนนเหมือน portrait โดยลด gap ที่มาจาก score margin + button padding */
@media (orientation: landscape) and (max-height: 500px) {
    .result-content .score-display {
        margin-bottom: 0 !important;
    }
    .result-content .lb-link-btn {
        margin-top: 0 !important;
        padding-top: 2px !important;
    }
}

/* ==================== LEADERBOARD ==================== */
/* Inline button to open the leaderboard modal */
.lb-link-btn {
    margin: 4px auto 14px;
    padding: 9px 20px;
    background: linear-gradient(135deg, #fbbf24, #f59e0b);
    border: 1px solid #fbbf24;
    border-radius: 999px;
    color: #2d1b0e;
    font-family: var(--font-body);
    font-size: 0.92rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    cursor: pointer;
    box-shadow: 0 6px 18px rgba(245, 158, 11, 0.35);
    transition: transform 0.15s, box-shadow 0.15s;
    display: inline-block;
}

.lb-link-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 26px rgba(245, 158, 11, 0.5);
}

.lb-link-btn:active {
    transform: scale(0.96);
}

/* Modal — same gradient as the end-screen overlays */
.lb-modal {
    position: fixed;
    inset: 0;
    z-index: 300;
    display: none;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y pinch-zoom;
    background: linear-gradient(135deg, rgb(186, 230, 253) 0%, rgb(255, 255, 255) 50%, rgb(186, 230, 253) 100%);
    padding: 20px;
}

/* Hide every other top-level element when the leaderboard modal is open. */
body.lb-open > *:not(#leaderboard-modal) {
    visibility: hidden;
}

.lb-modal.active {
    display: flex;
    animation: lbFadeIn 0.25s ease-out;
}

@keyframes lbFadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.lb-card {
    background: linear-gradient(160deg, rgba(255,255,255,0.96) 0%, rgba(245,238,255,0.96) 100%);
    border: 1.5px solid rgba(141, 120, 180, 0.35);
    border-radius: 22px;
    padding: clamp(22px, 4vw, 36px) clamp(20px, 3vw, 30px);
    width: 100%;
    max-width: 520px;
    height: 88vh;
    display: flex;
    flex-direction: column;
    position: relative;
    box-shadow: 0 20px 70px rgba(60, 40, 100, 0.35);
}

.lb-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid rgba(141, 120, 180, 0.35);
    background: rgba(255, 255, 255, 0.7);
    color: var(--ink);
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, transform 0.15s;
}

.lb-close:hover {
    background: #ffffff;
    transform: scale(1.05);
}

.lb-title {
    font-family: var(--font-heading);
    font-size: clamp(1.4rem, 4vw, 1.9rem);
    font-weight: 700;
    color: var(--ink);
    text-align: center;
    margin-bottom: 4px;
    letter-spacing: -0.01em;
}

.lb-subtitle {
    font-size: clamp(0.75rem, 1.7vw, 0.88rem);
    color: var(--ink-muted);
    text-align: center;
    margin-bottom: 14px;
}

.lb-list {
    list-style: none;
    padding: 0 4px 0 0;
    margin: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex: 1 1 auto;
}

.lb-list::-webkit-scrollbar { width: 6px; }
.lb-list::-webkit-scrollbar-thumb { background: rgba(141, 120, 180, 0.4); border-radius: 3px; }

.lb-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    margin: 4px 0;
    border-radius: 10px;
    background: rgba(141, 120, 180, 0.08);
    color: var(--ink);
    font-size: clamp(0.85rem, 2vw, 1rem);
}

.lb-row .rank {
    font-weight: 700;
    color: var(--ink-muted);
    min-width: 40px;
}

.lb-row .score {
    font-weight: 700;
    color: var(--accent-warm);
}

.lb-row.top-1 { background: rgba(253, 224, 71, 0.28); border: 1px solid rgba(234, 179, 8, 0.55); }
.lb-row.top-1 .rank { color: #b45309; }
.lb-row.top-2 { background: rgba(203, 213, 225, 0.32); border: 1px solid rgba(148, 163, 184, 0.55); }
.lb-row.top-2 .rank { color: #475569; }
.lb-row.top-3 { background: rgba(251, 146, 60, 0.22); border: 1px solid rgba(234, 88, 12, 0.5); }
.lb-row.top-3 .rank { color: #9a3412; }

.lb-row.lb-placeholder .rank { color: rgba(90, 66, 128, 0.3); }
.lb-row.lb-placeholder .score { color: rgba(90, 66, 128, 0.3); }

.lb-row.is-current {
    background: rgba(59, 130, 246, 0.22);
    border: 1px solid rgba(59, 130, 246, 0.7);
    box-shadow: 0 0 14px rgba(59, 130, 246, 0.35);
}

.lb-empty {
    text-align: center;
    color: var(--ink-muted);
    padding: 22px;
}

.lb-current {
    background: rgba(59, 130, 246, 0.16);
    border: 1px solid rgba(59, 130, 246, 0.55);
    border-radius: 12px;
    padding: 10px 14px;
    margin-top: 14px;
    text-align: center;
    color: var(--ink);
    font-size: clamp(0.85rem, 2.2vw, 1rem);
    font-weight: 600;
}
.lb-current:empty { display: none; }

.lb-current .rank-num {
    color: var(--accent-warm);
    font-weight: 700;
    font-size: 1.2em;
}

.lb-current .rank-num.top-1 { color: #d97706; }
.lb-current .rank-num.top-2 { color: #475569; }
.lb-current .rank-num.top-3 { color: #c2410c; }

@media (max-width: 768px) {
    .lb-card { height: 78vh; }
}

@media (orientation: landscape) and (max-height: 500px) {
    .lb-modal { padding: 14px 10px; }
    .lb-card {
        height: 88vh;
        max-width: 480px;
        padding: 14px 16px;
        border-radius: 16px;
    }
    .lb-close { top: 6px; right: 6px; width: 28px; height: 28px; font-size: 1.1rem; }
    .lb-title { font-size: 1.1rem; }
    .lb-subtitle { font-size: 0.7rem; margin-bottom: 6px; }
    .lb-row { padding: 5px 10px; margin: 2px 0; font-size: 0.78rem; border-radius: 7px; }
    .lb-row .rank { min-width: 28px; }
    .lb-current { padding: 5px 10px; font-size: 0.78rem; border-radius: 8px; }
}