/* ============================================
   SLEEP MONSTER GAME — STYLES
   ============================================ */

/* --- @font-face: Vastago Grotesk (Headlines) --- */
@font-face {
    font-family: 'Vastago Grotesk';
    src: url('../font/vastago-grotesk-fonts/VastagoGrotesk-Medium.woff2') format('woff2');
    font-weight: 500;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Vastago Grotesk';
    src: url('../font/vastago-grotesk-fonts/VastagoGrotesk-Bold.woff2') format('woff2');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}

/* Vastago Grotesk Heavy (800) and Black (900) removed — brand guidelines specify Medium and Bold only */

/* --- @font-face: Plus Jakarta Sans (Body / Subheads) --- */
@font-face {
    font-family: 'Plus Jakarta Sans';
    src: url('../font/Plus_Jakarta_Sans/PlusJakartaSans-Regular.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

/* Plus Jakarta Sans Medium (500) removed — brand guidelines specify Regular, SemiBold, Bold only */

@font-face {
    font-family: 'Plus Jakarta Sans';
    src: url('../font/Plus_Jakarta_Sans/PlusJakartaSans-SemiBold.woff2') format('woff2');
    font-weight: 600;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Plus Jakarta Sans';
    src: url('../font/Plus_Jakarta_Sans/PlusJakartaSans-Bold.woff2') format('woff2');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}

/* Plus Jakarta Sans ExtraBold (800) removed — brand guidelines specify Regular, SemiBold, Bold only */

/* --- @font-face: Noto Sans Thai (Thai fallback) --- */
@font-face {
    font-family: 'Noto Sans Thai';
    src: url('../font/Noto_Sans_Thai/NotoSansThai-Regular.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Noto Sans Thai';
    src: url('../font/Noto_Sans_Thai/NotoSansThai-Medium.woff2') format('woff2');
    font-weight: 500;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Noto Sans Thai';
    src: url('../font/Noto_Sans_Thai/NotoSansThai-SemiBold.woff2') format('woff2');
    font-weight: 600;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Noto Sans Thai';
    src: url('../font/Noto_Sans_Thai/NotoSansThai-Bold.woff2') format('woff2');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}

/* Noto Sans Thai ExtraBold (800) and Black (900) removed — brand guidelines specify Regular, SemiBold, Bold only */

/* --- Reset & Base --- */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* ResMed brand palettes (core tokens) */
    /* Blissful Blue Palette */
    --blissful-blue: #0080ff;
    --blissful-blue-el: #d0e8ff;
    --blissful-blue-l: #97c7ff;
    --blissful-blue-d: #004ba6;
    --blissful-blue-ed: #102661;

    /* Peaceful Purple Palette */
    --peaceful-purple: #896bff;
    --peaceful-purple-el: #d7d7ff;
    --peaceful-purple-l: #b9b9ff;
    --peaceful-purple-d: #4800b7;
    --peaceful-purple-ed: #371b69;

    /* Majestic Magenta Palette */
    --majestic-magenta: #bc299b;
    --majestic-magenta-el: #fde8ff;
    --majestic-magenta-l: #ffaefd;
    --majestic-magenta-d: #8a036d;
    --majestic-magenta-ed: #51003f;

    /* Renewing Red Palette */
    --renewing-red: #ec135b;
    --renewing-red-el: #ffd1d3;
    --renewing-red-l: #ff91b5;
    --renewing-red-d: #b6003c;
    --renewing-red-ed: #5c001f;

    /* Galaxy Gray Palette */
    --galaxy-gray-el: #f4f3ee;
    --galaxy-gray-l: #e8e7e0;
    --galaxy-gray: #d3d2cb;
    --galaxy-gray-d: #85837a;

    /* ResMed core aliases */
    --resmed-blue: var(--blissful-blue-ed);
    --resmed-mint: #00D5B5;
    --resmed-bright-blue: var(--blissful-blue);

    /* Semantic tokens used by the app (mapped to brand tokens) */
    --clr-primary: var(--resmed-blue);
    /* main brand color */
    --clr-primary-light: var(--peaceful-purple-el);
    --clr-accent: var(--majestic-magenta);
    --clr-accent-glow: rgba(188, 41, 155, 0.3);
    --clr-danger: var(--renewing-red-d);
    /* Score highlight (keeps original gold for visibility) */
    --clr-score: #fde68a;
    --clr-success: #22c55e;
    --clr-bg-dark: #1e1030;
    --clr-text: #f8f0ff;
    --clr-glass: rgba(30, 16, 48, 0.55);
    --clr-glass-border: rgba(167, 139, 250, 0.25);
    /* Primary brand fonts */
    --font-heading: 'Vastago Grotesk', 'Plus Jakarta Sans', 'Noto Sans Thai', system-ui, Arial, sans-serif;
    --font: 'Plus Jakarta Sans', 'Noto Sans Thai', system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    --white-glass: rgba(255, 255, 255, 0.9);

    /* ============================================================
       ITEM SIZES — ปรับขนาดสิ่งของได้ที่นี่
       --item-scale: ปรับเปอร์เซ็นต์ขนาดสิ่งของ (1 = 100%, 1.2 = 120%, 0.8 = 80%)
                     แก้ค่าเดียวจะปรับทุกชิ้นในแพลตฟอร์มนั้นพร้อมกัน
       ============================================================ */
    /* Desktop (default) */
    --item-scale: 1.2;
    --item-pillow: calc(110px * var(--item-scale));
    --item-blindfold: calc(120px * var(--item-scale));
    --item-coffee: calc(74px * var(--item-scale));
    --item-bed: calc(160px * var(--item-scale));
    --item-bolster-w: calc(150px * var(--item-scale));
    --item-bolster-h: calc(85px * var(--item-scale));
    --item-tv-w: calc(200px * var(--item-scale));
    --item-tv-h: calc(120px * var(--item-scale));
    --item-pajamas: calc(150px * var(--item-scale));
    --item-monster: calc(180px * var(--item-scale));
    --item-phone-w: calc(50px * var(--item-scale));
    --item-phone-h: calc(122px * var(--item-scale));

    /* ============================================================
       TEXT SIZE VARIABLES (COMBO, BONUS)
       สามารถปรับขนาดข้อความชม (Combo/Praise) และ Bonus แยกตามแพลตฟอร์มได้ที่นี่
       ============================================================ */
    /* Desktop (คอมพิวเตอร์) */
    --combo-normal-size: clamp(1.8rem, 5vw, 3rem);
    --combo-super-size: clamp(2.2rem, 6vw, 3.5rem);
    --combo-amazing-size: clamp(2.6rem, 7vw, 4.5rem);
    --combo-legendary-size: clamp(3rem, 8vw, 5.5rem);
    --bonus-text-size: clamp(1.2rem, 3.5vw, 1.8rem);

    /* Mobile Portrait (มือถือแนวตั้ง) */
    --mob-combo-normal-size: clamp(1.4rem, 5vw, 2.2rem);
    --mob-combo-super-size: clamp(1.6rem, 5.5vw, 2.6rem);
    --mob-combo-amazing-size: clamp(1.8rem, 6vw, 3rem);
    --mob-combo-legendary-size: clamp(2rem, 6.5vw, 3.2rem);
    --mob-bonus-text-size: clamp(0.85rem, 3vw, 1.3rem);

    /* Mobile Landscape (มือถือแนวนอน) */
    --land-combo-normal-size: clamp(1.1rem, 3.1vw, 1.6rem);
    --land-combo-super-size: clamp(1.2rem, 3.6vw, 2rem);
    --land-combo-amazing-size: clamp(1.4rem, 4vw, 2.2rem);
    --land-combo-legendary-size: clamp(1.6rem, 4.5vw, 2.5rem);
    --land-bonus-text-size: clamp(0.65rem, 2.2vw, 1rem);
}

html,
body {
    width: 100%;
    height: 100%;
    height: 100dvh;
    min-height: 100%;
    overflow: hidden;
    /* Theme-color background instead of #000 — even if any element fails to extend
       under the safe area, the gap blends with the game's purple instead of black. */
    background: #1e1030;
    font-family: var(--font);
    color: var(--clr-text);
    -webkit-user-select: none;
    user-select: none;
    touch-action: none;
    cursor: crosshair;
    /* Block LINE browser's pull-to-dismiss and overscroll bounce gestures */
    overscroll-behavior: none;
    -ms-scroll-chaining: none; /* Edge legacy */
    /* No body padding — individual elements (.screen, #hud) apply their own safe-area
       insets where appropriate. Body padding here used to shrink the content box and
       expose body's bg through the home-indicator area on iOS standalone PWA. */
}

#background {
    /* Make sure the bg image extends edge-to-edge on iOS PWA, including under the
       home indicator and notch areas. */
    width: 100vw;
    height: 100vh;
    height: 100dvh;
}

/* --- Background --- */
#background {
    position: fixed;
    inset: 0;
    background: url('../assets/Background.webp') center/cover no-repeat;
    z-index: 0;
}

body.game-playing #background {
    background: url('../assets/BackgroundDesktop.webp') center/cover no-repeat;
}

@media (orientation: portrait) and (max-width: 768px) {
    body.game-playing #background {
        background: url('../assets/BackgroundPortrait.webp') center/cover no-repeat;
    }
}

#background::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(10, 5, 25, 0.35);
}

/* --- Canvas --- */
#slashCanvas {
    position: fixed;
    inset: 0;
    z-index: 5;
    pointer-events: none;
}

/* --- Screens --- */
.screen {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.5s ease;
    /* Safe-area insets — ป้องกัน notch บังเนื้อหา */
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    /* Allow internal scrolling if content is too tall (especially in landscape) */
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.screen.active {
    opacity: 1;
    pointer-events: auto;
}

/* ==================== START SCREEN ==================== */
#startScreen.screen {
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding-top: clamp(40px, 7vh, 80px);
}

.start-content {
    text-align: center;
    animation: floatIn 0.8s ease-out;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: clamp(16px, 2.5vh, 28px);
    padding: 20px;
    max-width: clamp(320px, 62vw, 820px);
    width: 100%;
    z-index: 13;
    position: relative;
}

.game-title {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: clamp(20px, 4vh, 40px);
    line-height: 1;
}

.title-line1 {
    white-space: nowrap;
    font-family: var(--font-heading);
    font-size: clamp(1.6rem, 5.5vw, 5.5rem);
    word-break: keep-all;
    font-weight: 700;
    letter-spacing: 0.1em;
    background: linear-gradient(135deg, #896bff 0%, #f8f8ff 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    filter: drop-shadow(0 0 30px #896bff44);
    display: block;
}

.title-sub {
    font-family: var(--font);
    font-size: clamp(0.8rem, 2.5vw, 1.3rem);
    font-weight: 600;
    letter-spacing: 0.35em;
    color: var(--clr-primary-light);
    margin-top: 8px;
    opacity: 0.8;
}

/* === Sleep Monster มุมขวาล่าง === */
.monster-corner-img {
    position: absolute;
    /* หักล้าง padding ของ .screen เพื่อให้ตัวมอนสเตอร์ติดขอบจอล่าง/ขวาจริงๆ (แก้ปัญหาช่องว่างใน PWA) */
    bottom: calc(-1 * env(safe-area-inset-bottom));
    right: calc(-1 * env(safe-area-inset-right));
    width: clamp(280px, 42vw, 560px);
    height: clamp(280px, 42vw, 560px);
    object-fit: contain;
    object-position: bottom right;
    pointer-events: none;
    z-index: 12;
}

/* ==================== FULLSCREEN / PWA ADJUSTMENTS ==================== */
@media (orientation: portrait) {
    body.is-fullscreen .monster-corner-img {
        width: clamp(300px, 50vw, 460px);
        height: clamp(300px, 50vw, 460px);
    }
    
    /* เพิ่มช่องว่างระหว่างส่วนต่างๆ ในหน้าแรกแนวตั้งเมื่อเป็น Fullscreen */
    body.is-fullscreen #startScreen.screen .start-content {
        gap: 16px;
    }
    body.is-fullscreen #resmedLogo {
        margin-bottom: 16px;
    }
    body.is-fullscreen .game-title {
        margin-bottom: 16px;
    }
    body.is-fullscreen #startBtn {
        margin-top: 16px;
    }
}

@media (orientation: landscape) {
    body.is-fullscreen .monster-bubble p {
        font-size: clamp(0.8rem, 1.1vw, 1rem);
        line-height: 1.55;
    }
    body.is-fullscreen .bubble-speaker span {
        font-size: clamp(0.75rem, 1vw, 0.9rem);
    }
    body.is-fullscreen .monster-bubble {
        padding: clamp(14px, 2.2vh, 22px) clamp(18px, 2.5vw, 28px);
        gap: clamp(10px, 1.4vh, 16px);
    }
}

/* Fix iOS PWA initial height calculation bug
   --app-height ถูกตั้งโดย inline script ใน index.html จาก window.innerHeight ซึ่งให้ค่า
   ถูกต้องเร็วกว่า 100vh/100dvh/-webkit-fill-available บน iOS standalone (ไม่งั้นจะมีขอบ
   ใต้ home indicator ตอน launch ครั้งแรกจนกว่าจะหมุนจอ) */
@media (display-mode: standalone) {
    html, body {
        height: var(--app-height, 100dvh) !important;
        min-height: var(--app-height, 100dvh) !important;
    }
    #background {
        height: var(--app-height, 100dvh) !important;
    }
}

/* === Speech bubble === */
.monster-bubble {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 24px;
    padding: clamp(16px, 2.2vh, 26px) clamp(20px, 2.8vw, 32px);
    width: 100%;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
    display: flex;
    flex-direction: column;
    gap: clamp(10px, 1.4vh, 16px);
    position: relative;
}

/* หางฟองชี้ลงขวาหา monster */
.monster-bubble::after {
    content: '';
    position: absolute;
    bottom: -14px;
    right: 80px;
    border: 9px solid transparent;
    border-top-color: rgba(255, 255, 255, 0.95);
    border-bottom: 0;
}

/* Speaker label */
.bubble-speaker {
    display: flex;
    align-items: center;
    gap: 10px;
    padding-bottom: clamp(8px, 1vh, 12px);
    border-bottom: 1px solid rgba(137, 107, 255, 0.25);
}

.bubble-avatar {
    width: 36px;
    height: 36px;
    object-fit: contain;
    border-radius: 50%;
    background: rgba(137, 107, 255, 0.15);
    padding: 2px;
    flex-shrink: 0;
}

.bubble-speaker span {
    font-size: clamp(0.8rem, 1.3vw, 0.95rem);
    font-weight: 700;
    color: #4800b7;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.monster-bubble p {
    font-size: clamp(0.85rem, 1.4vw, 1.1rem);
    line-height: 1.7;
    color: #2d1a4a;
    margin: 0;
}

.monster-taunt {
    font-weight: 700 !important;
    color: #b6003c !important;
    font-style: italic;
}

@media (max-width: 480px) {
    .bubble-avatar {
        width: 44px;
        height: 44px;
    }

    .monster-corner-img {
        position: fixed;
        width: 240px;
        height: 240px;
        bottom: 0;
        right: 0;
    }

    #startBtn {
        margin-bottom: clamp(140px, 28vw, 200px);
    }
}

/* --- Buttons --- */
#startBtn {
    margin-top: clamp(12px, 3vh, 36px);
}

.btn-start {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    padding: clamp(14px, 2vh, 20px) clamp(36px, 6vw, 60px);
    font-family: var(--font);
    font-size: clamp(1rem, 2.5vw, 1.4rem);
    font-weight: 700;
    color: #fff;
    background: var(--resmed-bright-blue);
    border: 2px solid rgba(255, 255, 255, 0.12);
    border-radius: 60px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 6px 20px rgba(0, 128, 255, 0.18), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

@media (hover: hover) {
    .btn-start:hover {
        transform: scale(1.06);
        box-shadow: 0 10px 30px rgba(0, 128, 255, 0.22), inset 0 1px 0 rgba(255, 255, 255, 0.06);
    }
}

.btn-start:active, .btn-start.is-pressed {
    transform: scale(0.97);
}

.btn-icon {
    font-size: 1.3em;
}

/* ปุ่มปรึกษาด้านการนอน — primary blue */
.btn-line {
    background: var(--resmed-bright-blue);
    border-color: rgba(255, 255, 255, 0.12);
    box-shadow: 0 6px 20px rgba(0, 128, 255, 0.18), inset 0 1px 0 rgba(255, 255, 255, 0.06);
    text-decoration: none;
    color: #fff;
}

@media (hover: hover) {
    .btn-line:hover {
        box-shadow: 0 10px 30px rgba(0, 128, 255, 0.22), inset 0 1px 0 rgba(255, 255, 255, 0.06);
        color: #fff;
    }
}

/* ปุ่มเล่นอีกครั้ง — secondary outline white */
.btn-restart {
    background: transparent;
    border-color: rgba(255, 255, 255, 0.6);
    box-shadow: none;
}

@media (hover: hover) {
    .btn-restart:hover {
        background: rgba(255, 255, 255, 0.12);
        border-color: #fff;
        box-shadow: none;
    }
}

/* ==================== HUD ==================== */
#hud {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 20;
    display: flex;
    align-items: flex-start;
    /* ให้ไอเทมชิดขอบบน */
    justify-content: space-between;
    /* แยก Timer ไว้ซ้าย Score ไว้ขวา */
    padding: 10px 15px;
    padding-top: calc(10px + env(safe-area-inset-top));
    padding-left: calc(15px + env(safe-area-inset-left));
    padding-right: calc(15px + env(safe-area-inset-right));
    /* ลด Padding เพื่อให้ชิดมุมมากขึ้น */
    pointer-events: none;
}

/* Timer */
#timerContainer {
    flex-grow: 1;
    display: flex;
    align-items: center;
    gap: 0;
    max-width: calc(100% - 100px);
    margin-top: 12px;
    margin-left: -8px;
}

#clockWrapper {
    position: relative;
    width: clamp(60px, 10vw, 90px);
    height: clamp(60px, 10vw, 90px);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    z-index: 10;
    order: -1;
}

#clockIcon {
    width: 100%;
    height: 100%;
    object-fit: contain;
    filter: drop-shadow(0 0 8px rgba(255, 255, 255, 0.3));
}

#timerText {
    position: absolute;
    font-size: clamp(1.1rem, 3.5vw, 2rem);
    font-weight: 700;
    color: var(--clr-success);
    transition: color 0.5s ease, text-shadow 0.5s ease;
    top: 58%;
    left: 50%;
    transform: translate(-50%, -50%);
}

/* Timer Bar */
#timerBarOuter {
    width: 100%;
    max-width: 500px;
    height: clamp(16px, 2.5vh, 24px);
    background: rgba(0, 0, 0, 0.5);
    border-radius: 20px;
    flex-shrink: 1;
    overflow: hidden;
    border: 2px solid rgba(255, 255, 255, 0.15);
    margin-left: -30px;
    z-index: 5;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

#timerBarInner {
    width: 100%;
    height: 100%;
    transform-origin: left center;
    /* scaleX แทน width เพื่อหลีกเลี่ยง layout reflow — compositor-only */
    transition: transform 0.05s linear;
    will-change: transform;
}

#timerBarInner.danger {
    animation: barPulse 0.5s ease-in-out infinite alternate;
}

/* Score */
#scoreContainer {
    margin-top: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    /* ลบ backdrop-filter เพราะ active ตลอด 60s gameplay — ใช้ solid bg แทน */
    background: rgba(30, 16, 48, 0.82);
    border: 1px solid var(--clr-glass-border);
    border-radius: 20px;
    padding: clamp(8px, 1vh, 14px) clamp(16px, 2.5vw, 32px);
}

#scoreLabel {
    font-family: var(--font);
    font-size: clamp(0.55rem, 1.2vw, 0.75rem);
    letter-spacing: 0.2em;
    color: rgba(248, 240, 255, 0.5);
    font-weight: 600;
}

#scoreValue {
    font-family: var(--font-heading);
    font-size: clamp(1.4rem, 4vw, 2.5rem);
    font-weight: 700;
    color: var(--clr-score);
    text-shadow: 0 0 18px rgba(253, 230, 138, 0.25);
    line-height: 1;
}

/* ResMed logo — hidden by default, shown only on start screen */
#resmedLogo {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom));
    left: calc(76px + env(safe-area-inset-left));
    width: 160px;
    height: auto;
    z-index: 40;
    opacity: 0.95;
    pointer-events: none;
    display: none;
}

body.start-active #resmedLogo {
    display: block;
}

/* Landscape mobile — logo smaller */
@media (orientation: landscape) and (max-height: 500px) {
    #resmedLogo {
        width: 110px;
    }
}

/* Portrait mobile — logo above title */
@media (orientation: portrait) and (max-width: 768px) {
    #resmedLogo {
        position: relative;
        bottom: auto;
        top: auto;
        left: auto;
        transform: none;
        width: 140px;
        margin-bottom: 4px;
        flex-shrink: 0;
    }
}

/* Comparison box on game over */
.comparison {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    text-align: center;
    width: 100%;
}

#comparisonText {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    margin: 4px 0 0;
}

.comp-avg {
    font-size: clamp(0.75rem, 1.3vw, 0.88rem);
    font-weight: 400;
    color: rgba(248, 240, 255, 0.55);
}

.comp-diff {
    font-size: clamp(0.88rem, 1.5vw, 1rem);
    font-weight: 700;
}

.comp-diff.above {
    color: #4ade80;
}

.comp-diff.below {
    color: #ef4444;
}

.gameover-monster-bubble {
    background: var(--clr-glass);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--clr-glass-border);
    border-radius: 20px;
    padding: clamp(14px, 2vh, 20px) clamp(16px, 2.5vw, 24px);
    width: 100%;
    max-width: 100%;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    gap: 10px;
    text-align: left;
}

.gameover-monster-bubble .bubble-speaker {
    border-bottom: 1px solid rgba(137, 107, 255, 0.25);
    padding-bottom: 10px;
}

.gameover-monster-bubble .bubble-speaker span {
    color: #b9b9ff;
}

#adviceText {
    font-size: clamp(0.85rem, 1.5vw, 1rem);
    line-height: 1.65;
    color: rgba(248, 240, 255, 0.88);
    margin: 0;
}

.gameover-btns {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    width: 100%;
    margin-top: clamp(6px, 1vh, 12px);
}

.gameover-btns .btn-start {
    width: clamp(240px, 40vw, 340px);
    justify-content: center;
}

/* ==================== COUNTDOWN / TUTORIAL SCREEN ==================== */
/* desktop: number + กล่องรวมเป็น block เดียว center จอ (แบบเดิม) */
#countdownScreen.screen {
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.countdown-content {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: clamp(16px, 3vh, 28px);
    padding: 20px 16px;
    width: 100%;
    max-width: 1000px;
}

.countdown-number {
    font-family: var(--font-heading);
    font-size: clamp(4rem, 14vw, 8rem);
    font-weight: 700;
    color: #ffffff;
    line-height: 1;
    text-shadow: 0 0 50px rgba(137, 107, 255, 0.9), 0 0 20px rgba(255, 255, 255, 0.4);
    animation: countPulse 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* mobile: กล่อง center จอ เลขลอยเหนือกล่องพอดี */
@media (max-width: 768px) {
    .countdown-content {
        gap: 0;
        padding: 16px;
    }

    .countdown-number {
        position: absolute;
        bottom: 100%;
        left: 0;
        right: 0;
        text-align: center;
        padding-bottom: clamp(8px, 1.5vh, 16px);
        font-size: clamp(3rem, 10vw, 7rem);
    }
}

.countdown-number.go {
    color: #4ade80;
    text-shadow: 0 0 50px rgba(74, 222, 128, 0.9), 0 0 20px rgba(74, 222, 128, 0.5);
}

@keyframes countPulse {
    0% {
        transform: scale(1.6);
        opacity: 0.5;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* 3 containers เรียงเป็นบรรทัด อันละแถว ยาวเต็มแนวนอน */
.item-legend {
    display: flex;
    flex-direction: column;
    gap: clamp(8px, 1.5vh, 14px);
    width: 100%;
}

.legend-group {
    background: var(--clr-glass);
    border-radius: 16px;
    padding: clamp(12px, 1.8vh, 20px) clamp(16px, 2.5vw, 28px);
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: clamp(8px, 1.2vh, 12px);
    text-align: center;
}

.legend-group.good {
    border: 1.5px solid rgba(74, 222, 128, 0.45);
}

.legend-group.penalty {
    border: 1.5px solid rgba(251, 146, 60, 0.45);
}

.legend-group.lossy {
    border: 1.5px solid rgba(239, 68, 68, 0.7);
    box-shadow: 0 0 20px rgba(239, 68, 68, 0.2);
}

.legend-group h3 {
    font-family: var(--font-heading);
    font-size: clamp(0.8rem, 1.8vw, 1rem);
    font-weight: 700;
    letter-spacing: 0.04em;
    flex-shrink: 0;
    text-align: center;
}

.legend-group.good h3 {
    color: #4ade80;
}

.legend-group.penalty h3 {
    color: #fb923c;
}

.legend-group.lossy h3 {
    color: #ef4444;
}

/* items เรียงแนวนอนภายใน container */
.legend-items {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: clamp(10px, 2vw, 18px);
    align-items: center;
    justify-content: center;
    flex: 1;
}

.legend-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.legend-item img {
    width: clamp(40px, 6.5vw, 58px);
    height: clamp(40px, 6.5vw, 58px);
    object-fit: contain;
    filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.5));
}

.legend-group.good .legend-item span {
    color: #4ade80;
    font-size: clamp(0.9rem, 1.7vw, 1.05rem);
    font-weight: 700;
}

.legend-group.penalty .legend-item span {
    color: #fb923c;
    font-size: clamp(0.9rem, 1.7vw, 1.05rem);
    font-weight: 700;
}

.legend-group.lossy .legend-item span {
    color: #ef4444;
    font-size: clamp(0.75rem, 1.4vw, 0.9rem);
    font-weight: 700;
    letter-spacing: 0.05em;
    line-height: 1.3;
    text-align: center;
}

.legend-group.lossy .legend-item img {
    width: clamp(55px, 9vw, 75px);
    height: clamp(55px, 9vw, 75px);
    filter: drop-shadow(0 0 12px rgba(239, 68, 68, 0.7));
}

/* ==================== GAME AREA ==================== */
#gameArea {
    position: fixed;
    inset: 0;
    z-index: 3;
    overflow: hidden;
}

/* --- Game Items (ใช้ CSS variables — ปรับค่าที่ :root และ media queries) --- */
.game-item {
    position: absolute;
    width: 80px;
    height: 80px;
    pointer-events: auto;
    cursor: crosshair;
    z-index: 4;
    transition: none;
    will-change: transform;
}

.game-item img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.5));
}

.game-item.pillow {
    width: var(--item-pillow);
    height: var(--item-pillow);
}

.game-item.blindfold {
    width: var(--item-blindfold);
    height: var(--item-blindfold);
}

.game-item.coffee {
    width: var(--item-coffee);
    height: var(--item-coffee);
}

.game-item.bed {
    width: var(--item-bed);
    height: var(--item-bed);
}

.game-item.bolster {
    width: var(--item-bolster-w);
    height: var(--item-bolster-h);
}

.game-item.tv {
    width: var(--item-tv-w);
    height: var(--item-tv-h);
}

.game-item.pajamas {
    width: var(--item-pajamas);
    height: var(--item-pajamas);
}

.game-item.monster {
    width: var(--item-monster);
    height: var(--item-monster);
    filter: drop-shadow(0 0 16px rgba(239, 68, 68, 0.8));
}

.game-item.phone {
    width: var(--item-phone-w);
    height: var(--item-phone-h);
}

/* --- Slash Effect (split halves) --- */
.slash-half {
    position: absolute;
    width: 50%;
    height: 100%;
    overflow: hidden;
    z-index: 4;
    pointer-events: none;
}

.slash-half img {
    width: 200%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
}

.slash-half.left {
    animation: splitLeft 0.7s ease-in forwards;
}

.slash-half.right {
    animation: splitRight 0.7s ease-in forwards;
}

/* --- Score Popup --- */
.score-popup {
    position: absolute;
    font-family: var(--font-heading);
    font-size: clamp(2rem, 6vw, 3.5rem);
    font-weight: 700;
    pointer-events: none;
    z-index: 30;
    animation: scoreFloat 1.5s ease-out forwards;
    --popup-scale: 1;
}

.score-popup.positive {
    color: #fff;
    text-shadow: 0 0 16px #22c55e, 0 2px 4px rgba(0, 0, 0, 0.5);
}

.score-popup.negative {
    color: #fff;
    text-shadow: 0 0 16px #ef4444, 0 2px 4px rgba(0, 0, 0, 0.5);
}

.score-popup.burst {
    font-size: clamp(1rem, 3vw, 1.6rem);
    animation: scoreBurst 1.4s ease-out forwards;
}

/* ==================== GAME OVER SCREEN ==================== */
.gameover-content {
    text-align: center;
    width: 100%;
    max-width: 600px;
    padding: 20px;
    /* Use margin auto to keep centering while allowing scroll */
    margin: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: clamp(10px, 1.5vh, 16px);
    /* Extra padding at bottom to ensure last buttons aren't cut off */
    padding-bottom: 60px;
}

.gameover-title {
    font-family: var(--font-heading);
    font-size: clamp(2rem, 5vw, 3.5rem);
    font-weight: 700;
    background: linear-gradient(135deg, #f87171, #fbbf24);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    filter: drop-shadow(0 0 20px rgba(251, 191, 36, 0.4));
    margin: 0;
}

.final-score-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    background: var(--clr-glass);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--clr-glass-border);
    border-radius: 20px;
    padding: clamp(14px, 2vh, 24px) clamp(32px, 5vw, 60px);
    width: 100%;
}

.final-label {
    font-size: clamp(0.8rem, 1.5vw, 1rem);
    color: rgba(248, 240, 255, 0.6);
    letter-spacing: 0.05em;
}

.final-score {
    font-family: var(--font-heading);
    font-size: clamp(2.2rem, 6vw, 4rem);
    font-weight: 700;
    background: linear-gradient(135deg, #fbbf24, #f59e0b, #fde68a);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    filter: drop-shadow(0 0 25px rgba(245, 158, 11, 0.6));
    line-height: 1;
}

/* ==================== ANIMATIONS ==================== */
@keyframes floatIn {
    from {
        opacity: 0;
        transform: translateY(30px) scale(0.95);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes scoreFloat {
    0% {
        opacity: 1;
        transform: translate(0, 0) scale(var(--popup-scale, 1));
    }

    40% {
        opacity: 1;
        transform: translate(var(--drift-x, 20px), -55px) scale(calc(var(--popup-scale, 1) * 1.15));
    }

    100% {
        opacity: 0;
        transform: translate(var(--drift-x, 20px), -130px) scale(var(--popup-scale, 1));
    }
}

@keyframes scoreBurst {
    0% {
        opacity: 0;
        transform: translate(0, 0) scale(0.3);
    }

    25% {
        opacity: 1;
        transform: translate(0, 0) scale(1.1);
    }

    100% {
        opacity: 0;
        transform: translate(var(--drift-x, 0px), -90px) scale(0.5);
    }
}

@keyframes splitLeft {
    0% {
        transform: translate(0, 0) rotate(0deg);
        opacity: 1;
    }

    100% {
        transform: translate(-60px, 120px) rotate(-25deg);
        opacity: 0;
    }
}

@keyframes splitRight {
    0% {
        transform: translate(0, 0) rotate(0deg);
        opacity: 1;
    }

    100% {
        transform: translate(60px, 120px) rotate(25deg);
        opacity: 0;
    }
}

@keyframes barPulse {
    from {
        opacity: 0.8;
    }

    to {
        opacity: 1;
    }
}

@keyframes shake {
    0%,  100% { transform: translateX(0); }
    20%       { transform: translateX(-8px); }
    40%       { transform: translateX(8px); }
    60%       { transform: translateX(-6px); }
    80%       { transform: translateX(6px); }
}

.shake {
    animation: shake 0.4s ease-in-out;
}

/* ==================== SCREEN FLASH (pre-created, toggled via class) ==================== */
.screen-flash {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 50;
    opacity: 0;
    transition: none;
    will-change: opacity;
}

.flash-penalty { background: rgba(239, 68, 68, 0.15); }
.flash-monster { background: rgba(239, 68, 68, 0.6); }

/* สองอันเหมือนกันทุกอย่าง — สลับกันใช้เพื่อ restart animation โดยไม่ต้อง void offsetWidth */
@keyframes flashFadeA { 0% { opacity: 1; } 100% { opacity: 0; } }
@keyframes flashFadeB { 0% { opacity: 1; } 100% { opacity: 0; } }

.screen-flash.fa { animation: flashFadeA 0.35s ease-out forwards; }
.screen-flash.fb { animation: flashFadeB 0.35s ease-out forwards; }

/* ==================== PARTICLES ==================== */
.particle {
    position: absolute;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    pointer-events: none;
    z-index: 25;
    animation: particleFly 0.6s ease-out forwards;
}

@keyframes particleFly {
    0% {
        opacity: 1;
        transform: translate(0, 0) scale(1);
    }

    100% {
        opacity: 0;
        transform: translate(var(--px), var(--py)) scale(0);
    }
}

/* ==================== RESPONSIVE ==================== */

/* Desktop — prevent scrollbar on game-over screen */
@media (min-width: 1200px) {
    #gameOverScreen.screen {
        overflow: hidden;
        justify-content: center;
        align-items: center;
        padding-top: 0;
    }

    .gameover-content {
        max-width: 680px;
        gap: clamp(10px, 1.8vh, 20px);
        padding: 24px 32px 32px;
    }

    .gameover-title {
        font-size: clamp(2.4rem, 4vw, 3.2rem);
    }

    .final-score-box {
        padding: clamp(16px, 2.5vh, 28px) 24px;
    }

    .final-score {
        font-size: clamp(3.5rem, 7vw, 5rem);
    }

    .gameover-monster-bubble {
        padding: 20px 28px;
    }

    .gameover-btns {
        gap: 16px;
        margin-top: 8px;
    }

    .gameover-btns .btn-start {
        width: clamp(280px, 55vw, 420px);
        font-size: clamp(1rem, 1.8vw, 1.2rem);
        padding: clamp(14px, 2vh, 20px) clamp(32px, 4vw, 56px);
    }
}

@media (max-width: 480px) {
    #startScreen.screen {
        justify-content: flex-start;
        align-items: center;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: calc(env(safe-area-inset-top) + 12px) 0 20px;
    }

    .start-content {
        max-width: 90vw;
        gap: 6px;
        padding: 10px;
    }

    .game-title {
        margin-bottom: 8px;
    }

    #startBtn {
        margin-top: 8px;
    }

    .title-line1 {
        white-space: normal;
        font-size: clamp(3.2rem, 12vw, 4.5rem);
    }

    .title-sub {
        font-size: clamp(1.1rem, 4.5vw, 1.5rem);
    }

    #timerContainer {
        margin-left: -10px;
        margin-top: 12px;
    }

    #clockWrapper {
        width: 58px;
        height: 58px;
    }

    #timerBarOuter {
        width: calc(100vw - 90px);
        height: 16px;
        margin-left: -25px;
    }

    #timerText {
        font-size: 1.2rem;
    }

    #scoreContainer {
        /* ปรับขนาดกล่องคะแนนให้กะทัดรัดขึ้นเพื่อเพิ่มพื้นที่แถบ */
        padding: 6px 12px;
        min-width: 70px;
    }

    /* ========== ITEM SIZES — มือถือแนวตั้ง ========== */
    /* แก้ --item-scale ค่าเดียว ปรับทุกชิ้นพร้อมกัน — base px ใช้จาก desktop :root */
    :root {
        --item-scale: 0.77;
    }

    /* จำกัดขนาดแต้ม combo popup บนมือถือแนวตั้ง */
    .score-popup {
        font-size: 2.6rem;
    }

    .score-popup.burst {
        display: none;
    }

    #scoreValue {
        font-size: 1.2rem;
    }

    #scoreLabel {
        font-size: 0.85rem;
    }

    /* ข้อความ combo / praise / bonus — ขนาดต่างกัน */
    .combo-normal {
        font-size: var(--mob-combo-normal-size);
    }

    .combo-super {
        font-size: var(--mob-combo-super-size);
    }

    .combo-amazing {
        font-size: var(--mob-combo-amazing-size);
    }

    .combo-legendary {
        font-size: var(--mob-combo-legendary-size);
    }

    .bonus-text {
        font-size: var(--mob-bonus-text-size);
    }

    .slash-warning.monster-warn {
        font-size: clamp(1.4rem, 5vw, 2.2rem);
    }

    .slash-warning.penalty-warn {
        font-size: clamp(1rem, 3.5vw, 1.6rem);
    }
}

@media (min-width: 481px) and (max-width: 768px) {
    .title-line1 {
        white-space: normal;
        font-size: clamp(2rem, 8vw, 3.5rem);
    }

    .start-content {
        max-width: 85vw;
    }

    /* ========== ITEM SIZES — Tablet ========== */
    /* แก้ --item-scale ค่าเดียว ปรับทุกชิ้นพร้อมกัน — base px ใช้จาก desktop :root */
    :root {
        --item-scale: 0.8;
    }
}

/* Tablet / desktop landscape — ลดขนาดฟอนต์ในกล่องบับเบิล */
@media (orientation: landscape) and (min-height: 501px) {
    .monster-bubble p {
        font-size: clamp(0.7rem, 0.9vw, 0.85rem);
        line-height: 1.55;
    }

    .bubble-speaker span {
        font-size: clamp(0.65rem, 0.85vw, 0.8rem);
    }

    .monster-bubble {
        padding: clamp(10px, 1.5vh, 16px) clamp(14px, 1.8vw, 22px);
        gap: clamp(6px, 0.8vh, 10px);
    }
}

/* ==================== LANDSCAPE TABLET (iPad แนวนอน) — game over compact ==================== */
@media (min-width: 769px) and (max-width: 1199px) and (orientation: landscape) and (min-height: 501px) {
    #gameOverScreen.screen {
        overflow-y: auto;
    }

    .gameover-content {
        gap: clamp(6px, 1vh, 12px);
        padding: 10px 20px 24px;
        max-width: 520px;
    }

    .gameover-title {
        font-size: clamp(1.6rem, 3.5vw, 2.4rem);
    }

    .final-score-box {
        padding: clamp(10px, 1.4vh, 18px) clamp(24px, 4vw, 48px);
        gap: 4px;
    }

    .final-score {
        font-size: clamp(2.8rem, 6vw, 4rem);
    }

    .gameover-monster-bubble {
        padding: 10px 16px;
    }

    .gameover-monster-bubble p {
        font-size: clamp(0.78rem, 1.2vw, 0.95rem);
        line-height: 1.5;
    }

    .gameover-btns {
        gap: 10px;
        margin-top: 4px;
    }

    .gameover-btns .btn-start {
        padding: clamp(10px, 1.4vh, 16px) clamp(28px, 4vw, 50px);
        font-size: clamp(0.9rem, 1.5vw, 1.1rem);
    }
}

/* ==================== PORTRAIT TABLET (iPad แนวตั้ง) ==================== */
@media (min-width: 769px) and (max-width: 1100px) and (orientation: portrait) {

    /* ใช้ background แนวตั้งเหมือนมือถือ */
    body.game-playing #background {
        background: url('../assets/BackgroundPortrait.webp') center/cover no-repeat;
    }

    /* Logo — ย้ายขึ้นบนสุดของ start screen */
    #resmedLogo {
        position: relative;
        bottom: auto;
        left: auto;
        transform: none;
        width: 160px;
        margin-bottom: 8px;
        flex-shrink: 0;
        order: -1;
    }

    #startScreen.screen {
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
        padding-top: clamp(28px, 4vh, 52px);
    }

    /* Start screen — จำกัด content ไม่ให้กว้างเกินไป */
    .start-content {
        max-width: clamp(400px, 72vw, 680px);
        gap: clamp(20px, 3vh, 36px);
    }

    /* Title ใหญ่ขึ้น */
    .title-line1 {
        font-size: clamp(2.8rem, 7vw, 5rem);
    }

    .title-sub {
        font-size: clamp(1rem, 2.2vw, 1.4rem);
    }

    /* Monster corner image — ใหญ่ขึ้นสมส่วนกับจอ tablet */
    .monster-corner-img {
        width: clamp(320px, 44vw, 480px);
        height: clamp(320px, 44vw, 480px);
    }

    /* Bubble — ใหญ่ขึ้นให้อ่านง่าย */
    .monster-bubble {
        padding: clamp(18px, 2.5vh, 28px) clamp(24px, 3vw, 36px);
        gap: clamp(12px, 1.5vh, 18px);
    }

    .monster-bubble p {
        font-size: clamp(1rem, 1.8vw, 1.2rem);
        line-height: 1.7;
    }

    .bubble-speaker span {
        font-size: clamp(0.9rem, 1.5vw, 1.05rem);
    }

    .bubble-avatar {
        width: 44px;
        height: 44px;
    }

    /* Start button */
    #startBtn {
        margin-top: clamp(8px, 1.5vh, 20px);
        margin-bottom: 0;
    }

    /* Game over — กระจาย content จากบนลงล่างแทนกองกลาง */
    #gameOverScreen.screen {
        justify-content: flex-start;
        align-items: center;
        padding-top: clamp(24px, 4vh, 48px);
    }

    .gameover-content {
        max-width: 600px;
        gap: clamp(14px, 2.2vh, 24px);
        padding: 0 24px clamp(32px, 5vh, 60px);
    }

    .gameover-title {
        font-size: clamp(2.8rem, 7vw, 4rem);
    }

    .final-score-box {
        padding: clamp(22px, 3vh, 36px) clamp(36px, 6vw, 70px);
        gap: 10px;
    }

    .final-label {
        font-size: clamp(1rem, 1.8vw, 1.2rem);
    }

    .final-score {
        font-size: clamp(4rem, 10vw, 6rem);
    }

    .comp-avg {
        font-size: clamp(0.9rem, 1.5vw, 1.05rem);
    }

    .comp-diff {
        font-size: clamp(1rem, 1.8vw, 1.2rem);
    }

    .gameover-monster-bubble {
        padding: 20px 24px;
    }

    .gameover-monster-bubble .bubble-speaker span {
        font-size: clamp(0.9rem, 1.5vw, 1.1rem);
    }

    .gameover-monster-bubble p {
        font-size: clamp(0.95rem, 1.7vw, 1.15rem);
        line-height: 1.7;
    }

    .gameover-btns {
        gap: 16px;
        margin-top: 12px;
    }

    .gameover-btns .btn-start {
        width: clamp(300px, 65vw, 460px);
        font-size: clamp(1.05rem, 2vw, 1.3rem);
        padding: clamp(16px, 2.2vh, 22px) clamp(36px, 5vw, 60px);
    }

    /* มอนสเตอร์มุมขวาล่าง — ใหญ่ขึ้น */
    .monster-corner-img {
        width: clamp(420px, 62vw, 580px);
        height: clamp(420px, 62vw, 580px);
    }

    /* Leaderboard modal */
    .lb-modal-inner {
        max-width: 560px;
    }
}

/* Desktop ใหญ่ — บับเบิลและฟอนต์ใหญ่ขึ้น */
@media (min-width: 1024px) and (orientation: landscape) {
    .monster-bubble p {
        font-size: clamp(1rem, 1.2vw, 1.3rem);
        line-height: 1.7;
    }

    .bubble-speaker span {
        font-size: clamp(0.9rem, 1.05vw, 1.1rem);
    }

    .bubble-avatar {
        width: 48px;
        height: 48px;
    }

    .monster-bubble {
        padding: clamp(20px, 2.2vh, 30px) clamp(28px, 2.8vw, 40px);
        gap: clamp(12px, 1.2vh, 18px);
    }
}

@media (orientation: landscape) and (max-height: 500px) {

    /* Screens — scrollable in landscape */
    #startScreen.screen {
        justify-content: flex-start;
        align-items: center;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: calc(12px + env(safe-area-inset-top)) env(safe-area-inset-right) calc(12px + env(safe-area-inset-bottom)) env(safe-area-inset-left);
    }

    #countdownScreen.screen {
        align-items: center;
        justify-content: center;
        overflow: hidden;
    }

    #gameOverScreen.screen {
        align-items: flex-start;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: calc(12px + env(safe-area-inset-top)) env(safe-area-inset-right) calc(12px + env(safe-area-inset-bottom)) env(safe-area-inset-left);
    }

    /* HUD */
    #hud {
        padding: 4px 10px;
        padding-top: calc(4px + env(safe-area-inset-top));
        padding-left: calc(10px + env(safe-area-inset-left));
        padding-right: calc(10px + env(safe-area-inset-right));
    }

    #timerContainer {
        margin-top: 8px;
        margin-left: -10px;
    }

    #clockWrapper {
        width: 55px;
        height: 55px;
    }

    #timerText {
        font-size: 0.95rem;
    }

    #timerBarOuter {
        height: 14px;
        margin-left: -25px;
        max-width: 200px;
    }

    #scoreContainer {
        padding: 4px 12px;
        margin-top: 4px;
    }

    #scoreValue {
        font-size: 1.1rem;
    }

    /* Start screen */
    .start-content {
        max-width: 580px;
        gap: 10px;
        padding: 10px 16px;
    }

    .game-title {
        margin-bottom: 8px;
    }

    .game-title .title-line1 {
        font-size: clamp(1.4rem, 4.5vw, 2.2rem);
    }

    .title-sub {
        font-size: 0.65rem;
        margin-top: 4px;
    }

    .monster-bubble {
        padding: 8px 14px;
        gap: 4px;
    }

    .monster-bubble p {
        font-size: 0.58rem;
        line-height: 1.4;
    }

    .bubble-speaker span {
        font-size: 0.62rem;
    }

    .bubble-avatar {
        width: 28px;
        height: 28px;
    }

    .monster-corner-img {
        position: fixed;
        width: 220px;
        height: 220px;
        right: 0;
        bottom: 0;
    }

    .btn-start {
        padding: 10px 32px;
        font-size: 0.9rem;
    }

    #startBtn {
        margin-bottom: 10px;
    }

    /* Countdown — legend groups side-by-side, compact */
    .countdown-content {
        padding: 6px 12px;
    }

    .countdown-number {
        font-size: 2.4rem;
        padding-bottom: 6px;
    }

    .item-legend {
        flex-direction: row;
        gap: 8px;
    }

    .legend-group {
        padding: 8px 10px;
        gap: 5px;
    }

    .legend-group h3,
    .legend-group.lossy h3 {
        font-size: 0.65rem;
    }

    .legend-item img {
        width: 32px;
        height: 32px;
    }

    .legend-group .legend-item span {
        font-size: 0.65rem;
    }

    .legend-group.lossy .legend-item img {
        width: 42px;
        height: 42px;
    }

    /* Game over */
    .gameover-content {
        gap: 8px;
        max-width: 540px;
        /* ลดความกว้างของคอนเทนเนอร์ไม่ให้ยาวเกินไป */
    }

    .gameover-title {
        font-size: 1.6rem;
    }

    .final-score-box {
        padding: 8px 24px;
    }

    .final-score {
        font-size: 1.8rem;
    }

    .gameover-monster-bubble {
        padding: 10px 14px;
    }

    #adviceText {
        font-size: 0.7rem;
    }

    .gameover-btns {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
        gap: 12px;
        margin-top: 10px;
        width: 100%;
    }

    .gameover-btns .btn-start {
        flex: 1 1 0px;
        /* ทำให้ปุ่มทั้งสองมีความกว้างเท่ากันเป๊ะ */
        min-width: 140px;
        max-width: 200px;
        padding: 10px 4px;
        font-size: 0.8rem;
    }

    /* ========== ITEM SIZES — มือถือแนวนอน ========== */
    /* แก้ --item-scale ค่าเดียว ปรับทุกชิ้นพร้อมกัน — base px ใช้จาก desktop :root */
    :root {
        --item-scale: 0.72;
    }

    /* จำกัดขนาดแต้ม combo popup บนมือถือแนวนอน */
    .score-popup {
        font-size: clamp(1.2rem, 3.6vw, 2rem);
    }

    .score-popup.burst {
        display: none;
    }

    /* ข้อความ combo / praise / bonus / warning — ขนาดต่างกัน */
    .combo-normal {
        font-size: var(--land-combo-normal-size);
    }

    .combo-super {
        font-size: var(--land-combo-super-size);
    }

    .combo-amazing {
        font-size: var(--land-combo-amazing-size);
    }

    .combo-legendary {
        font-size: var(--land-combo-legendary-size);
    }

    .bonus-text {
        font-size: var(--land-bonus-text-size);
    }

    .slash-warning.monster-warn {
        font-size: clamp(1.1rem, 3.1vw, 1.6rem);
    }

    .slash-warning.penalty-warn {
        font-size: clamp(0.8rem, 2.7vw, 1.2rem);
    }

    /* Sound btn (bottom) */
    .sound-btn {
        width: 36px;
        height: 36px;
        /* ป้องกัน iOS edge-swipe zone (~44px แรกจากซ้าย) บัง touch ตอน notch อยู่ขวา */
        left: max(44px, calc(16px + env(safe-area-inset-left)));
    }

    /* BGM btn (above sound) */
    .bgm-btn {
        width: 36px;
        height: 36px;
        bottom: calc(58px + env(safe-area-inset-bottom));
        left: max(44px, calc(16px + env(safe-area-inset-left)));
    }

    /* Fullscreen btn (top of stack) */
    .fullscreen-btn {
        width: 36px;
        height: 36px;
        bottom: calc(100px + env(safe-area-inset-bottom));
        left: max(44px, calc(16px + env(safe-area-inset-left)));
    }
}

/* ==================== SOUND TOGGLE BUTTON ==================== */
.sound-btn {
    position: fixed;
    bottom: calc(16px + env(safe-area-inset-bottom));
    left: calc(16px + env(safe-area-inset-left));
    z-index: 100;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.2);
    background: rgba(30, 16, 48, 0.6);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, background 0.2s;
    padding: 0;
    touch-action: manipulation;
}

@media (hover: hover) {
    .sound-btn:hover {
        transform: scale(1.1);
        background: rgba(30, 16, 48, 0.85);
    }
}

.sound-btn:active, .sound-btn.is-pressed {
    transform: scale(0.92);
}

.sound-btn svg {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
}

/* ==================== BGM TOGGLE BUTTON (mutes only background music) ==================== */
.bgm-btn {
    position: fixed;
    bottom: calc(68px + env(safe-area-inset-bottom));
    left: calc(16px + env(safe-area-inset-left));
    z-index: 100;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.2);
    background: rgba(30, 16, 48, 0.6);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, background 0.2s;
    padding: 0;
}

@media (hover: hover) {
    .bgm-btn:hover { transform: scale(1.1); background: rgba(30, 16, 48, 0.85); }
}
.bgm-btn:active, .bgm-btn.is-pressed { transform: scale(0.92); }
.bgm-btn svg { width: 22px; height: 22px; flex-shrink: 0; }
.bgm-btn { touch-action: manipulation; }

/* ==================== FULLSCREEN TOGGLE BUTTON ==================== */
.fullscreen-btn {
    position: fixed;
    bottom: calc(120px + env(safe-area-inset-bottom));
    left: calc(16px + env(safe-area-inset-left));
    z-index: 100;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.2);
    background: rgba(30, 16, 48, 0.6);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, background 0.2s;
    padding: 0;
}

@media (hover: hover) {
    .fullscreen-btn:hover {
        transform: scale(1.1);
        background: rgba(30, 16, 48, 0.85);
    }
}

.fullscreen-btn:active, .fullscreen-btn.is-pressed {
    transform: scale(0.92);
}

.fullscreen-btn svg {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
}

/* ==================== COMBO / STRIKE TEXT ==================== */
.combo-text {
    position: absolute;
    transform: translate(-50%, -50%);
    font-family: var(--font-heading);
    font-weight: 700;
    letter-spacing: 0.08em;
    z-index: 35;
    pointer-events: none;
    white-space: nowrap;
    animation: comboFloat 1.7s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.praise-text {
    position: absolute;
    transform: translate(-50%, -50%);
    font-family: var(--font-heading);
    font-weight: 700;
    letter-spacing: 0.08em;
    z-index: 35;
    pointer-events: none;
    white-space: nowrap;
    animation: praiseFloat 1.7s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.combo-normal {
    font-size: var(--combo-normal-size);
    color: #fbbf24;
    text-shadow: 0 0 15px rgba(251, 191, 36, 0.9), 0 0 40px rgba(251, 191, 36, 0.4), 0 4px 8px rgba(0, 0, 0, 0.5);
}

.combo-super {
    font-size: var(--combo-super-size);
    color: #fb923c;
    text-shadow: 0 0 15px rgba(251, 146, 60, 0.9), 0 0 40px rgba(249, 115, 22, 0.4), 0 4px 8px rgba(0, 0, 0, 0.5);
}

.combo-amazing {
    font-size: var(--combo-amazing-size);
    color: #f472b6;
    text-shadow: 0 0 20px rgba(244, 114, 182, 0.9), 0 0 50px rgba(236, 72, 153, 0.4), 0 4px 8px rgba(0, 0, 0, 0.5);
}

.combo-legendary {
    font-size: var(--combo-legendary-size);
    background: linear-gradient(135deg, #fbbf24, #f472b6, #a78bfa);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    filter: drop-shadow(0 0 25px rgba(251, 191, 36, 0.7)) drop-shadow(0 4px 8px rgba(0, 0, 0, 0.5));
}

@keyframes comboFloat {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(2) rotate(-3deg);
    }

    15% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1) rotate(1deg);
    }

    35% {
        opacity: 1;
        transform: translate(-50%, -70%) scale(1.1) rotate(0deg);
    }

    100% {
        opacity: 0;
        transform: translate(-50%, -180%) scale(0.8);
    }
}

@keyframes praiseFloat {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(1.8) rotate(2deg);
    }

    15% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1) rotate(-1deg);
    }

    35% {
        opacity: 1;
        transform: translate(-50%, -90%) scale(1.05) rotate(0deg);
    }

    100% {
        opacity: 0;
        transform: translate(-50%, -250%) scale(0.7);
    }
}

/* ==================== BONUS TEXT ==================== */
.bonus-text {
    position: absolute;
    transform: translate(-50%, 0);
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: var(--bonus-text-size);
    color: #a78bfa;
    text-shadow: 0 0 10px rgba(167, 139, 250, 0.9), 0 0 30px rgba(139, 92, 246, 0.5), 0 2px 4px rgba(0, 0, 0, 0.5);
    z-index: 36;
    pointer-events: none;
    white-space: nowrap;
    animation: bonusFloat 1.7s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

@keyframes bonusFloat {
    0% {
        opacity: 0;
        transform: translate(-50%, 0) scale(1.5);
    }

    15% {
        opacity: 1;
        transform: translate(-50%, 0) scale(1);
    }

    40% {
        opacity: 1;
        transform: translate(-50%, -20%) scale(1.05);
    }

    100% {
        opacity: 0;
        transform: translate(-50%, -120%) scale(0.8);
    }
}

/* ==================== SLASH WARNING TEXT ==================== */
.slash-warning {
    position: absolute;
    transform: translate(-50%, -50%);
    font-family: var(--font-heading);
    font-weight: 700;
    letter-spacing: 0.05em;
    z-index: 40;
    pointer-events: none;
    white-space: nowrap;
    animation: warningPop 1.7s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.monster-warn {
    font-size: clamp(2rem, 7vw, 3.5rem);
    color: #ef4444;
    text-shadow: 0 0 15px rgba(239, 68, 68, 0.9), 0 0 40px rgba(220, 38, 38, 0.6), 0 4px 8px rgba(0, 0, 0, 0.6);
}

.penalty-warn {
    font-size: clamp(1.4rem, 5vw, 2.2rem);
    color: #fb923c;
    text-shadow: 0 0 12px rgba(251, 146, 60, 0.9), 0 0 30px rgba(249, 115, 22, 0.5), 0 3px 6px rgba(0, 0, 0, 0.5);
}

@keyframes warningPop {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(2.5) rotate(-5deg);
    }

    12% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1) rotate(2deg);
    }

    25% {
        transform: translate(-50%, -50%) scale(1.15) rotate(-1deg);
    }

    50% {
        opacity: 1;
        transform: translate(-50%, -70%) scale(1) rotate(0deg);
    }

    100% {
        opacity: 0;
        transform: translate(-50%, -180%) scale(0.7);
    }
}

/* ==================== LEADERBOARD ==================== */
/* Inline button inside the final-score-box */
.lb-link-btn {
    margin-top: 14px;
    padding: 4px 0;
    background: none;
    border: none;
    box-shadow: none;
    color: #fff;
    font-family: var(--font);
    font-size: clamp(0.78rem, 1.7vw, 0.95rem);
    font-weight: 600;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 4px;
    text-decoration-color: rgba(255, 255, 255, 0.5);
    transition: color 0.2s, text-decoration-color 0.2s;
}

@media (hover: hover) {
    .lb-link-btn:hover {
        color: var(--peaceful-purple);
        text-decoration-color: var(--peaceful-purple);
    }
}

.lb-link-btn:active, .lb-link-btn.is-pressed {
    opacity: 0.7;
}

/* Modal — light tint, just blur the live game/screen behind */
.lb-modal {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.55);
    padding: 20px;
}

body.lb-open > *:not(#leaderboardModal):not(#background) {
    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, #2a1a4f 0%, #1e1030 100%);
    border: 2px solid rgba(167, 139, 250, 0.35);
    border-radius: 24px;
    padding: clamp(22px, 4vw, 38px) clamp(20px, 3vw, 32px);
    width: 100%;
    max-width: 520px;
    height: 88vh;
    display: flex;
    flex-direction: column;
    position: relative;
    box-shadow: 0 20px 80px rgba(0, 0, 0, 0.6);
}

.lb-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

@media (hover: hover) {
    .lb-close:hover {
        background: rgba(255, 255, 255, 0.18);
    }
}

.lb-title {
    font-family: var(--font-heading);
    font-size: clamp(1.4rem, 4vw, 1.9rem);
    font-weight: 700;
    color: #fde68a;
    text-align: center;
    margin-bottom: 4px;
}

.lb-subtitle {
    font-size: clamp(0.75rem, 1.7vw, 0.9rem);
    color: rgba(255, 255, 255, 0.6);
    text-align: center;
    margin-bottom: 14px;
}

.lb-current {
    background: rgba(137, 107, 255, 0.25);
    border: 1px solid rgba(137, 107, 255, 0.6);
    border-radius: 12px;
    padding: 10px 14px;
    margin-top: 14px;
    margin-bottom: 0;
    text-align: center;
    color: #f8f0ff;
    font-size: clamp(0.85rem, 2.2vw, 1rem);
    font-weight: 600;
}

.lb-current .rank-num {
    color: #f8f0ff;
    font-weight: 700;
    font-size: 1.2em;
}

.lb-current .rank-num.top-1 { color: #fde68a; }
.lb-current .rank-num.top-2 { color: #cbd5e1; }
.lb-current .rank-num.top-3 { color: #fdba74; }

.lb-list {
    list-style: none;
    padding: 0 4px 0 0;
    margin: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex: 1 1 auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
    will-change: transform;
}

.lb-list::-webkit-scrollbar { width: 6px; }
.lb-list::-webkit-scrollbar-thumb { background: rgba(167, 139, 250, 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(255, 255, 255, 0.04);
    color: #f0e8ff;
    font-size: clamp(0.85rem, 2vw, 1rem);
    transition: background 0.15s;
}

.lb-row .rank {
    font-weight: 700;
    color: rgba(255, 255, 255, 0.55);
    min-width: 36px;
}

.lb-row .score {
    font-weight: 700;
    color: #fff;
}

.lb-row.top-1 { background: rgba(253, 230, 138, 0.18); border: 1px solid rgba(253, 230, 138, 0.5); }
.lb-row.top-1 .rank { color: #fde68a; }
.lb-row.top-2 { background: rgba(203, 213, 225, 0.13); border: 1px solid rgba(203, 213, 225, 0.4); }
.lb-row.top-2 .rank { color: #cbd5e1; }
.lb-row.top-3 { background: rgba(251, 146, 60, 0.13); border: 1px solid rgba(251, 146, 60, 0.4); }
.lb-row.top-3 .rank { color: #fdba74; }

.lb-row.lb-placeholder .rank { color: rgba(255, 255, 255, 0.2); }
.lb-row.lb-placeholder .score { color: rgba(255, 255, 255, 0.2); font-weight: 400; }

.lb-row.is-current {
    background: rgba(137, 107, 255, 0.35);
    border: 1px solid rgba(137, 107, 255, 0.85);
    box-shadow: 0 0 14px rgba(137, 107, 255, 0.4);
}

.lb-empty {
    text-align: center;
    color: rgba(255, 255, 255, 0.5);
    padding: 20px;
}

/* Mobile landscape — compact leaderboard so list rows are visible */
@media (max-width: 768px) {
    .lb-modal {
        padding: 32px 16px;
    }

    .lb-card {
        height: 76vh;
    }
}

@media (orientation: landscape) and (max-height: 500px) {
    /* landscape non-fullscreen: เพิ่ม vertical padding ให้ card ลอยห่างขอบ */
    .lb-modal { padding: 60px 8px; }

    .lb-card {
        height: auto;
        max-height: calc(100vh - 120px);
        max-width: 480px;
        padding: 14px 16px;
        border-radius: 16px;
    }
}

/* landscape fullscreen (standalone PWA หรือ Fullscreen API) — compact กว่า */
@media (orientation: landscape) and (max-height: 500px) and (display-mode: standalone) {
    .lb-modal { padding: 20px 8px; }
    .lb-card { max-height: calc(100vh - 40px); }
}

@media (orientation: landscape) and (max-height: 500px) {
    body.is-fullscreen .lb-modal { padding: 20px 8px; }
    body.is-fullscreen .lb-card { max-height: calc(100vh - 40px); }
}

@media (orientation: landscape) and (max-height: 500px) {

    .lb-close {
        top: 6px;
        right: 6px;
        width: 28px;
        height: 28px;
        font-size: 1.1rem;
    }

    .lb-title {
        font-size: 1.1rem;
        margin-bottom: 1px;
    }

    .lb-subtitle {
        font-size: 0.7rem;
        margin-bottom: 6px;
    }

    .lb-current {
        padding: 5px 10px;
        margin-bottom: 6px;
        font-size: 0.78rem;
        border-radius: 8px;
    }

    .lb-row {
        padding: 5px 10px;
        margin: 2px 0;
        font-size: 0.78rem;
        border-radius: 7px;
    }

    .lb-row .rank { min-width: 28px; }
}

/* ============================================
   SHARE BUTTON
   ============================================ */
.share-btn {
    position: fixed;
    top: calc(16px + env(safe-area-inset-top));
    right: calc(16px + env(safe-area-inset-right));
    z-index: 150;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.2);
    background: rgba(30, 16, 48, 0.6);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    color: #fff;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.2s, background 0.2s;
    padding: 0;
}

.share-btn.visible {
    display: flex;
}

@media (hover: hover) {
    .share-btn:hover {
        transform: scale(1.1);
        background: rgba(30, 16, 48, 0.85);
    }
}

.share-btn:active, .share-btn.is-pressed {
    transform: scale(0.92);
}

.share-btn svg {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
}

.share-toast {
    position: fixed;
    bottom: 32px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    z-index: 300;
    background: rgba(30, 16, 48, 0.92);
    color: #fff;
    font-family: 'Plus Jakarta Sans', sans-serif;
    font-size: 0.9rem;
    padding: 10px 20px;
    border-radius: 24px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s, transform 0.2s;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

.share-toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}