html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    color: #FDFBF7;
    background-color: #0B160F;
    font-family: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

#app {
    position: relative;
    width: 100%;
    height: 100%;
    height: 100dvh;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    background: radial-gradient(circle at center, #182F20 0%, #0B160F 100%);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    box-sizing: border-box;
}

#game-container {
    width: 100%;
    height: 100%;
    max-width: 100vw;
    max-height: 100dvh;
    touch-action: none;
    display: flex;
    justify-content: center;
    align-items: center;
}

#game-container canvas {
    touch-action: none;
    box-shadow: 0 0 35px rgba(0, 0, 0, 0.9), 0 0 15px rgba(212, 175, 55, 0.15);
    max-width: 100vw;
    max-height: 100dvh;
}

/* DAMAGE FLASH SCREEN OVERLAY */
.damage-flash-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 15;
    background: radial-gradient(circle at center, rgba(217, 37, 37, 0.15) 0%, rgba(163, 24, 24, 0.65) 100%);
    opacity: 0;
    transition: opacity 0.08s ease-out;
}

.damage-flash-overlay.active {
    opacity: 1;
    animation: damagePulse 0.3s ease-out forwards;
}

@keyframes damagePulse {
    0% {
        opacity: 0.85;
        transform: scale(1.02);
    }
    100% {
        opacity: 0;
        transform: scale(1);
    }
}

/* HUD ROOT — full-viewport overlay wrapper */
#hud {
    position: absolute;
    /* Track #app's safe-area-padded CONTENT box so the HUD overlay and the
       letterboxed Phaser canvas (inside #game-container in that same box)
       always share identical bounds — no drift on notched devices, and it
       re-flows automatically on dynamic window resize. */
    top: env(safe-area-inset-top);
    left: env(safe-area-inset-left);
    right: env(safe-area-inset-right);
    bottom: env(safe-area-inset-bottom);
    pointer-events: none;
    z-index: 10;
    display: flex;
    justify-content: center;
    align-items: center;
}

#hud button,
#hud a,
#hud input,
#hud [role="button"],
#hud .interactive {
    pointer-events: auto;
}

/*
 * HUD SHELL — constrains ALL overlay content (HUD bars, banners, modals) to
 * the same 540:960 aspect-ratio "safe box" that the letterboxed Phaser canvas
 * occupies. This prevents HUD elements from stretching into extreme corners
 * on ultrawide desktop monitors while remaining perfectly fluid on phones.
 */
.hud-shell {
    position: relative;
    /* 9:16 safe box computed from #app's CONTENT box (viewport minus safe-area
       insets) so it matches the Scale.FIT letterboxed canvas exactly at every
       breakpoint (360/414/768/1024/1440/1920) and on live resize. On desktop
       env() insets are 0, so this reduces to the pure viewport calculation. */
    width: min(calc(100vw - env(safe-area-inset-left) - env(safe-area-inset-right)), calc((100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * (540 / 960)));
    height: min(calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)), calc((100vw - env(safe-area-inset-left) - env(safe-area-inset-right)) * (960 / 540)));
    max-width: 100%;
    max-height: 100%;
    pointer-events: none;
}

.hud-shell > * {
    pointer-events: none;
}

.hud-shell button,
.hud-shell a,
.hud-shell input,
.hud-shell [role="button"],
.hud-shell .interactive {
    pointer-events: auto;
}

/* TOP STATUS BAR */
.top-hud-bar {
    position: absolute;
    top: max(10px, env(safe-area-inset-top));
    left: clamp(8px, 2.5vw, 16px);
    right: clamp(8px, 2.5vw, 16px);
    display: flex;
    flex-wrap: nowrap;
    justify-content: space-between;
    align-items: center;
    gap: clamp(6px, 1.5vw, 12px);
    pointer-events: none;
    z-index: 11;
}

.hud-pill {
    position: relative;
    background: linear-gradient(135deg, rgba(20, 41, 27, 0.92) 0%, rgba(13, 28, 18, 0.96) 100%);
    border: 1.5px solid rgba(212, 175, 55, 0.7);
    border-radius: 9999px;
    padding: clamp(4px, 1vw, 8px) clamp(8px, 2vw, 16px);
    display: flex;
    align-items: center;
    gap: clamp(4px, 1vw, 8px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.6), 0 0 10px rgba(212, 175, 55, 0.2);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    flex-shrink: 1;
    min-width: 0;
}

.cherry-pill {
    border-color: rgba(217, 37, 37, 0.85);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.6), 0 0 12px rgba(217, 37, 37, 0.35);
}

/* FLOATING SCORE POP-UPS */
.floating-popups-container {
    position: absolute;
    top: -12px;
    right: -4px;
    pointer-events: none;
}

.floating-score-popup {
    position: absolute;
    font-size: clamp(0.85rem, 2.4vw, 1.1rem);
    font-weight: 900;
    color: #FFD700;
    text-shadow: 0 2px 6px rgba(0, 0, 0, 0.9), 0 0 8px #FF4D4D;
    white-space: nowrap;
    animation: floatScoreUp 0.85s cubic-bezier(0.18, 0.89, 0.32, 1.28) forwards;
}

@keyframes floatScoreUp {
    0% {
        opacity: 0;
        transform: translateY(10px) scale(0.6);
    }
    20% {
        opacity: 1;
        transform: translateY(-4px) scale(1.25);
    }
    100% {
        opacity: 0;
        transform: translateY(-34px) scale(0.9);
    }
}

.difficulty-pill {
    font-size: clamp(0.6rem, 1.8vw, 0.75rem);
    font-weight: 800;
    letter-spacing: 0.8px;
    text-transform: uppercase;
    padding: clamp(4px, 1vw, 5px) clamp(8px, 1.8vw, 12px);
    white-space: nowrap;
}

.difficulty-pill.diff-easy {
    border-color: #4CAF50;
    color: #A5D6A7;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5), 0 0 8px rgba(76, 175, 80, 0.3);
}

.difficulty-pill.diff-normal {
    border-color: #D4AF37;
    color: #FFD700;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5), 0 0 8px rgba(212, 175, 55, 0.3);
}

.difficulty-pill.diff-hard {
    border-color: #E53935;
    color: #FF8A80;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5), 0 0 8px rgba(229, 57, 53, 0.35);
}

.hud-icon {
    display: block;
    flex-shrink: 0;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.6));
    width: clamp(16px, 4.2vw, 22px);
    height: clamp(16px, 4.2vw, 22px);
}

.hud-number {
    font-size: clamp(0.95rem, 3vw, 1.25rem);
    font-weight: 900;
    color: #FDFBF7;
    letter-spacing: 0.5px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8), 0 0 6px rgba(212, 175, 55, 0.4);
    white-space: nowrap;
}

.hud-unit {
    font-size: clamp(0.65rem, 1.9vw, 0.85rem);
    color: #E5C158;
    font-weight: 700;
}

.hud-controls {
    display: flex;
    gap: clamp(6px, 1.5vw, 8px);
    pointer-events: auto;
    flex-shrink: 0;
}

.hud-btn {
    background: linear-gradient(135deg, rgba(20, 41, 27, 0.92) 0%, rgba(13, 28, 18, 0.96) 100%);
    border: 1.5px solid rgba(212, 175, 55, 0.6);
    border-radius: 50%;
    width: clamp(36px, 9vw, 44px);
    height: clamp(36px, 9vw, 44px);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.6), 0 0 8px rgba(212, 175, 55, 0.2);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    transition: transform 0.18s cubic-bezier(0.4, 0, 0.2, 1), border-color 0.18s ease, background 0.18s ease;
    flex-shrink: 0;
}

.hud-btn:hover {
    transform: scale(1.08);
    border-color: #FFD700;
    background: linear-gradient(135deg, rgba(32, 65, 43, 0.96) 0%, rgba(20, 41, 27, 0.98) 100%);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.7), 0 0 12px rgba(255, 215, 0, 0.4);
}

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

/* ENERGY RUSH PROGRESS BAR */
.energy-rush-container {
    position: absolute;
    top: clamp(56px, 12vh, 76px);
    left: 50%;
    transform: translateX(-50%);
    width: clamp(200px, 60%, 360px);
    background: linear-gradient(135deg, rgba(20, 41, 27, 0.95) 0%, rgba(10, 22, 14, 0.98) 100%);
    border: 1.8px solid #FFD700;
    border-radius: 14px;
    padding: clamp(6px, 1.5vw, 8px) clamp(10px, 2.5vw, 14px) clamp(8px, 2vw, 10px);
    display: flex;
    flex-direction: column;
    gap: 5px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.8), 0 0 16px rgba(255, 215, 0, 0.5);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    pointer-events: none;
    z-index: 12;
    box-sizing: border-box;
}

.energy-rush-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    flex-wrap: wrap;
}

.energy-rush-icon {
    font-size: clamp(0.8rem, 2.2vw, 0.95rem);
    filter: drop-shadow(0 0 4px #FFD700);
}

.energy-rush-label {
    font-size: clamp(0.68rem, 2vw, 0.78rem);
    font-weight: 900;
    color: #FFD700;
    letter-spacing: 1px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
    white-space: nowrap;
}

.energy-rush-sub {
    font-size: clamp(0.58rem, 1.7vw, 0.68rem);
    color: #F4EBD9;
    font-weight: 700;
    letter-spacing: 0.5px;
    white-space: nowrap;
}

.energy-rush-track {
    width: 100%;
    height: 8px;
    background: rgba(0, 0, 0, 0.7);
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid rgba(255, 215, 0, 0.45);
}

.energy-rush-fill {
    height: 100%;
    background: linear-gradient(90deg, #E5C158 0%, #FFD700 50%, #FFF59D 100%);
    border-radius: 4px;
    box-shadow: 0 0 10px #FFD700;
    transition: width 0.08s linear;
}

/* CONTEXTUAL TUTORIAL BANNER */
.tutorial-banner {
    position: absolute;
    bottom: max(20px, calc(env(safe-area-inset-bottom) + 16px));
    left: 50%;
    transform: translateX(-50%);
    width: min(90%, 440px);
    background: linear-gradient(135deg, rgba(20, 41, 27, 0.96) 0%, rgba(10, 22, 14, 0.98) 100%);
    border: 2px solid #D4AF37;
    border-radius: 18px;
    padding: clamp(10px, 2.5vw, 14px) clamp(12px, 3vw, 18px);
    display: flex;
    align-items: center;
    gap: clamp(10px, 2.5vw, 14px);
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.8), 0 0 20px rgba(212, 175, 55, 0.35);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    pointer-events: none;
    z-index: 12;
    box-sizing: border-box;
}

.tutorial-icon-box {
    background: rgba(212, 175, 55, 0.18);
    border: 1.5px solid #D4AF37;
    border-radius: 14px;
    width: clamp(38px, 9vw, 50px);
    height: clamp(38px, 9vw, 50px);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}

.tutorial-icon-box svg {
    width: clamp(20px, 5vw, 28px);
    height: clamp(20px, 5vw, 28px);
}

.tutorial-text-content {
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 0;
}

.tutorial-title {
    font-size: clamp(0.78rem, 2.4vw, 0.95rem);
    font-weight: 900;
    color: #FFD700;
    letter-spacing: 0.6px;
    text-transform: uppercase;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
}

.tutorial-desc {
    font-size: clamp(0.7rem, 2.1vw, 0.85rem);
    color: #F4EBD9;
    line-height: 1.3;
}

/* OVERLAYS & MODALS */
.screen-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: radial-gradient(circle at center, rgba(14, 28, 18, 0.88) 0%, rgba(8, 15, 10, 0.96) 100%);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    pointer-events: auto;
    padding: clamp(10px, 3vw, 16px);
    box-sizing: border-box;
    overflow-y: auto;
    z-index: 20;
}

.menu-card, .modal-card {
    background: linear-gradient(180deg, #183321 0%, #0F2115 100%);
    border: 2px solid #D4AF37;
    border-radius: 24px;
    width: 100%;
    max-width: 440px;
    max-height: calc(100dvh - 32px);
    overflow: visible;
    padding: clamp(10px, 2.5vw, 16px) clamp(12px, 3.5vw, 18px);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    box-shadow: 0 18px 45px rgba(0, 0, 0, 0.85), 0 0 35px rgba(212, 175, 55, 0.25);
    position: relative;
    margin: auto;
}

.leaf-accent {
    margin-bottom: 2px;
    filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.6));
}

.series-badge {
    background: rgba(212, 175, 55, 0.16);
    border: 1px solid #D4AF37;
    border-radius: 9999px;
    padding: 3px clamp(10px, 3vw, 14px);
    font-size: clamp(0.6rem, 1.8vw, 0.7rem);
    font-weight: 900;
    color: #FFD700;
    letter-spacing: 1.4px;
    text-transform: uppercase;
    margin-bottom: 6px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

.game-title {
    margin: 0;
    font-size: clamp(2.1rem, 9vw, 3.2rem);
    font-weight: 900;
    letter-spacing: 2.5px;
    background: linear-gradient(180deg, #FFF6D6 0%, #FFD700 50%, #C59B27 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 4px 16px rgba(0, 0, 0, 0.9);
    font-family: 'Georgia', serif;
}

.game-subtitle {
    font-size: clamp(0.9rem, 3.5vw, 1.15rem);
    color: #F4EBD9;
    font-style: italic;
    font-family: 'Georgia', serif;
    letter-spacing: 0.6px;
    margin-top: 2px;
}

.location-tag {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 6px;
    font-size: clamp(0.66rem, 2.2vw, 0.78rem);
    font-weight: 800;
    color: #E5C158;
    letter-spacing: 0.9px;
    text-transform: uppercase;
    text-align: center;
}

.divider-gold {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    margin: 12px 0;
    color: #D4AF37;
}

.divider-gold .line {
    flex: 1;
    height: 1.5px;
    background: linear-gradient(90deg, transparent, #D4AF37, transparent);
}

.divider-gold .diamond {
    font-size: 0.75rem;
    filter: drop-shadow(0 0 4px rgba(212, 175, 55, 0.6));
}

/* DIFFICULTY SELECTOR */
.difficulty-section {
    width: 100%;
    background: rgba(10, 22, 14, 0.75);
    border: 1.5px solid rgba(212, 175, 55, 0.4);
    border-radius: 16px;
    padding: clamp(8px, 2.2vw, 10px) clamp(10px, 2.5vw, 12px);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
    box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.5);
}

.diff-label-header {
    font-size: clamp(0.64rem, 2vw, 0.72rem);
    font-weight: 900;
    color: #E5C158;
    letter-spacing: 1.1px;
    text-transform: uppercase;
}

.difficulty-selector {
    display: flex;
    gap: clamp(4px, 1.2vw, 6px);
    width: 100%;
}

.diff-tab-btn {
    flex: 1 1 0;
    min-width: 0;
    box-sizing: border-box;
    background: rgba(20, 41, 27, 0.85);
    border: 1.5px solid rgba(212, 175, 55, 0.4);
    border-radius: 12px;
    padding: clamp(6px, 1.8vw, 9px) 2px;
    min-height: 40px;
    cursor: pointer;
    transition: transform 0.18s cubic-bezier(0.4, 0, 0.2, 1), background 0.18s ease, border-color 0.18s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.diff-tab-btn .diff-title {
    font-size: clamp(0.58rem, 1.9vw, 0.72rem);
    font-weight: 800;
    color: #E6D8BA;
    letter-spacing: 0.2px;
    white-space: nowrap;
    text-align: center;
    line-height: 1.1;
}

.diff-tab-btn:hover {
    border-color: #FFD700;
    background: rgba(30, 60, 40, 0.95);
    transform: scale(1.03);
}

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

.diff-tab-btn.active {
    background: linear-gradient(180deg, #244730 0%, #152C1E 100%);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.6);
}

.diff-tab-btn.active.diff-btn-easy {
    border-color: #4CAF50;
    box-shadow: 0 0 10px rgba(76, 175, 80, 0.4);
}
.diff-tab-btn.active.diff-btn-easy .diff-title {
    color: #A5D6A7;
}

.diff-tab-btn.active.diff-btn-normal {
    border-color: #FFD700;
    box-shadow: 0 0 10px rgba(255, 215, 0, 0.4);
}
.diff-tab-btn.active.diff-btn-normal .diff-title {
    color: #FFD700;
}

.diff-tab-btn.active.diff-btn-hard {
    border-color: #E53935;
    box-shadow: 0 0 10px rgba(229, 57, 53, 0.4);
}
.diff-tab-btn.active.diff-btn-hard .diff-title {
    color: #FF8A80;
}

.diff-description {
    font-size: clamp(0.68rem, 2.1vw, 0.75rem);
    color: #F4EBD9;
    font-style: italic;
    line-height: 1.3;
}

.controls-guide-box {
    background: rgba(10, 22, 14, 0.7);
    border: 1.5px solid rgba(212, 175, 55, 0.35);
    border-radius: 16px;
    width: 100%;
    padding: clamp(8px, 2.2vw, 10px) clamp(10px, 3vw, 14px);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
}

.guide-row {
    display: flex;
    align-items: center;
    gap: clamp(8px, 2.5vw, 12px);
}

.key-badge {
    background: rgba(212, 175, 55, 0.2);
    border: 1.2px solid #D4AF37;
    border-radius: 8px;
    padding: 3px 8px;
    font-size: clamp(0.64rem, 2vw, 0.72rem);
    font-weight: 800;
    color: #FFD700;
    min-width: 60px;
    text-align: center;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
    flex-shrink: 0;
}

.guide-text {
    font-size: clamp(0.7rem, 2.2vw, 0.82rem);
    color: #FDFBF7;
    text-align: left;
}

.menu-stats-row {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    width: 100%;
    margin-bottom: 12px;
    font-size: 0.85rem;
    gap: 8px;
}

.stat-item {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.stat-label {
    font-size: clamp(0.6rem, 1.9vw, 0.68rem);
    font-weight: 800;
    color: #E5C158;
    letter-spacing: 0.6px;
}

.stat-val {
    font-size: clamp(0.84rem, 2.6vw, 0.95rem);
    font-weight: 900;
    color: #FDFBF7;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
}

/* BUTTON GROUP */
.menu-button-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
    align-items: center;
}

.start-button {
    position: relative;
    width: 100%;
    max-width: 280px;
    min-height: 56px;
    background: linear-gradient(180deg, #2E6930 0%, #17381A 100%);
    border: 2px solid #FFD700;
    border-radius: 9999px;
    color: #FFFFFF;
    font-size: clamp(1.05rem, 3.4vw, 1.25rem);
    font-weight: 900;
    letter-spacing: 1.8px;
    padding: clamp(13px, 3vw, 15px) clamp(18px, 5vw, 26px);
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.7), 0 0 20px rgba(255, 215, 0, 0.4);
    transition: transform 0.18s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.18s ease, background 0.18s ease;
    overflow: hidden;
    text-transform: uppercase;
    box-sizing: border-box;
}

.start-button:hover {
    transform: translateY(-2px) scale(1.02);
    background: linear-gradient(180deg, #3A833D 0%, #1E4A22 100%);
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.8), 0 0 28px rgba(255, 215, 0, 0.7);
}

.start-button:active {
    transform: scale(0.96);
}

.story-codex-button {
    background: linear-gradient(180deg, rgba(20, 41, 27, 0.92) 0%, rgba(13, 28, 18, 0.96) 100%);
    border: 1.8px solid rgba(212, 175, 55, 0.75);
    border-radius: 9999px;
    color: #FFD700;
    font-size: clamp(0.82rem, 2.6vw, 0.9rem);
    font-weight: 800;
    letter-spacing: 1.1px;
    padding: clamp(11px, 2.8vw, 14px) clamp(18px, 5vw, 22px);
    min-height: 48px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.5), 0 0 12px rgba(212, 175, 55, 0.2);
    transition: transform 0.18s cubic-bezier(0.4, 0, 0.2, 1), border-color 0.18s ease, background 0.18s ease;
    box-sizing: border-box;
}

.story-codex-button:hover {
    background: linear-gradient(180deg, rgba(32, 65, 43, 0.96) 0%, rgba(20, 41, 27, 0.98) 100%);
    border-color: #FFD700;
    transform: translateY(-1px) scale(1.03);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.6), 0 0 16px rgba(255, 215, 0, 0.4);
}

.story-codex-button:active {
    transform: scale(0.96);
}

.pulse-button {
    animation: pulseGlow 2.2s infinite;
}

@keyframes pulseGlow {
    0%, 100% {
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.6), 0 0 16px rgba(255, 215, 0, 0.4);
    }
    50% {
        box-shadow: 0 8px 28px rgba(0, 0, 0, 0.8), 0 0 32px rgba(255, 215, 0, 0.85);
    }
}

/* STORY CHRONICLE MODAL */
.story-modal-card {
    max-width: 460px;
    padding: clamp(16px, 4vw, 24px) clamp(14px, 4vw, 22px);
}

.story-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 6px;
}

.story-series-tag {
    font-size: clamp(0.62rem, 2vw, 0.72rem);
    font-weight: 900;
    color: #E5C158;
    letter-spacing: 1.3px;
}

.story-close-btn {
    background: rgba(212, 175, 55, 0.15);
    border: 1px solid rgba(212, 175, 55, 0.4);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    color: #F4EBD9;
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: transform 0.18s cubic-bezier(0.4, 0, 0.2, 1), color 0.18s, border-color 0.18s;
}

.story-close-btn:hover {
    color: #FFD700;
    border-color: #FFD700;
    transform: scale(1.1);
}

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

.story-chapter-badge {
    background: rgba(212, 175, 55, 0.16);
    border: 1px solid #D4AF37;
    border-radius: 9999px;
    padding: 2px 12px;
    font-size: clamp(0.62rem, 2vw, 0.72rem);
    font-weight: 800;
    color: #FFD700;
    letter-spacing: 0.9px;
    margin-bottom: 4px;
}

.story-title {
    font-size: clamp(1.3rem, 5vw, 1.65rem);
    font-weight: 900;
    color: #FDFBF7;
    font-family: 'Georgia', serif;
    margin: 4px 0 2px 0;
    text-shadow: 0 2px 6px rgba(0, 0, 0, 0.8);
}

.story-region {
    font-size: clamp(0.72rem, 2.3vw, 0.82rem);
    color: #E5C158;
    font-style: italic;
}

.story-vignette {
    margin: 8px 0;
    font-size: clamp(1.8rem, 6vw, 2.2rem);
    filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.6));
}

.story-body-text {
    font-size: clamp(0.82rem, 2.5vw, 0.94rem);
    line-height: 1.6;
    color: #FDFBF7;
    text-align: justify;
    margin: 8px 0 16px 0;
    background: rgba(10, 22, 14, 0.65);
    padding: clamp(12px, 3vw, 14px) clamp(14px, 3.5vw, 16px);
    border-radius: 16px;
    border: 1.5px solid rgba(212, 175, 55, 0.3);
    box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.5);
}

.story-dots {
    display: flex;
    gap: 8px;
    margin-bottom: 14px;
    flex-wrap: wrap;
    justify-content: center;
}

.story-dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: rgba(212, 175, 55, 0.3);
    border: 1px solid #D4AF37;
    cursor: pointer;
    padding: 0;
    transition: transform 0.18s cubic-bezier(0.4, 0, 0.2, 1), background 0.18s ease;
}

.story-dot:hover {
    transform: scale(1.2);
}

.story-dot.active {
    background: #FFD700;
    transform: scale(1.35);
    box-shadow: 0 0 10px #FFD700;
}

.story-nav-actions {
    display: flex;
    gap: 12px;
    width: 100%;
}

.story-nav-btn {
    flex: 1;
    border-radius: 9999px;
    padding: clamp(11px, 2.8vw, 13px) clamp(12px, 3.5vw, 16px);
    min-height: 48px;
    font-size: clamp(0.78rem, 2.4vw, 0.88rem);
    font-weight: 800;
    letter-spacing: 0.9px;
    cursor: pointer;
    transition: transform 0.18s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.18s ease, background 0.18s ease;
    box-sizing: border-box;
}

.story-nav-btn:active:not(:disabled) {
    transform: scale(0.96);
}

.story-nav-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* MODAL ACTIONS & DETAILS */
.modal-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
    margin-top: 14px;
    align-items: center;
}

.modal-btn {
    width: 100%;
    max-width: 280px;
    min-height: 48px;
    border-radius: 9999px;
    padding: clamp(11px, 2.8vw, 12px) clamp(16px, 4vw, 20px);
    font-size: clamp(0.86rem, 2.7vw, 0.98rem);
    font-weight: 800;
    letter-spacing: 1.1px;
    cursor: pointer;
    transition: transform 0.18s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.18s ease, background 0.18s ease;
    box-sizing: border-box;
}

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

.btn-primary {
    background: linear-gradient(180deg, #2E6930 0%, #17381A 100%);
    border: 1.8px solid #FFD700;
    color: #FFFFFF;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.6), 0 0 12px rgba(255, 215, 0, 0.35);
}

.btn-primary:hover {
    background: linear-gradient(180deg, #3A833D 0%, #1E4A22 100%);
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 8px 22px rgba(0, 0, 0, 0.7), 0 0 20px rgba(255, 215, 0, 0.6);
}

.btn-secondary {
    background: linear-gradient(180deg, rgba(20, 41, 27, 0.92) 0%, rgba(13, 28, 18, 0.96) 100%);
    border: 1.5px solid rgba(212, 175, 55, 0.65);
    color: #F4EBD9;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.5);
}

.btn-secondary:hover {
    background: linear-gradient(180deg, rgba(32, 65, 43, 0.96) 0%, rgba(20, 41, 27, 0.98) 100%);
    border-color: #FFD700;
    color: #FFFFFF;
    transform: translateY(-1px) scale(1.02);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.6), 0 0 12px rgba(212, 175, 55, 0.35);
}

/* GAME OVER RESULTS */
.modal-title, .gameover-header {
    font-size: clamp(1.5rem, 5.5vw, 1.9rem);
    font-weight: 900;
    color: #FFD700;
    font-family: 'Georgia', serif;
    letter-spacing: 1.2px;
    margin: 0;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8);
}

.gameover-diff-tag {
    font-size: clamp(0.72rem, 2.3vw, 0.82rem);
    font-weight: 800;
    padding: 3px clamp(10px, 3vw, 14px);
    border-radius: 9999px;
    margin-top: 4px;
    letter-spacing: 0.8px;
    border: 1.5px solid currentColor;
}

.new-record-badge {
    background: linear-gradient(90deg, #D92525, #E5C158);
    color: #FFFFFF;
    font-size: clamp(0.72rem, 2.3vw, 0.82rem);
    font-weight: 900;
    padding: 5px clamp(12px, 3.5vw, 16px);
    border-radius: 9999px;
    margin-top: 6px;
    letter-spacing: 1.1px;
    box-shadow: 0 4px 12px rgba(217, 37, 37, 0.6), 0 0 10px rgba(229, 193, 88, 0.5);
    animation: pulseRecord 1.8s infinite;
    text-align: center;
}

@keyframes pulseRecord {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
}

.results-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: clamp(8px, 2.5vw, 12px);
    width: 100%;
    margin-bottom: 12px;
}

.result-card {
    background: rgba(10, 22, 14, 0.75);
    border: 1.5px solid rgba(212, 175, 55, 0.45);
    border-radius: 16px;
    padding: clamp(10px, 3vw, 14px) clamp(6px, 2vw, 10px);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
    min-width: 0;
}

.result-val {
    font-size: clamp(1.3rem, 4.5vw, 1.7rem);
    font-weight: 900;
    color: #FDFBF7;
    text-shadow: 0 2px 6px rgba(0, 0, 0, 0.8);
}

.result-lbl {
    font-size: clamp(0.64rem, 2vw, 0.76rem);
    font-weight: 800;
    color: #E5C158;
    letter-spacing: 0.6px;
    text-align: center;
}

.records-bar {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    font-size: clamp(0.7rem, 2.2vw, 0.82rem);
    color: #E6D8BA;
    font-weight: 700;
    margin-bottom: 8px;
    text-align: center;
}

.pause-stats {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    width: 100%;
    margin: 14px 0;
    gap: 8px;
}

.pause-stat-box {
    display: flex;
    flex-direction: column;
    gap: 2px;
    align-items: center;
}

.pause-stat-box .label {
    font-size: clamp(0.66rem, 2vw, 0.76rem);
    color: #E5C158;
    font-weight: 800;
}

.pause-stat-box .val {
    font-size: clamp(1.1rem, 3.6vw, 1.35rem);
    color: #FDFBF7;
    font-weight: 900;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.7);
}

.diff-highlight {
    color: #FFD700 !important;
}

.animate-fade-in {
    animation: fadeInModal 0.28s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes fadeInModal {
    from {
        opacity: 0;
        transform: translateY(12px) scale(0.97);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* SMALL-HEIGHT VIEWPORT SAFETY (short landscape phones / small tablets) */
@media (max-height: 480px) {
    .menu-card, .modal-card {
        max-height: 92dvh;
        padding: 12px 14px;
    }
    .game-title {
        font-size: clamp(1.6rem, 7vw, 2.2rem);
    }
    .difficulty-section,
    .controls-guide-box {
        padding: 8px 10px;
        margin-bottom: 8px;
    }
}

/* ======================================================================
 * RESPONSIVE BREAKPOINT ARCHITECTURE
 * Presentation-only tuning layered on top of the base fluid styles above.
 * No game logic, state, or scene code is touched by any rule below.
 * ==================================================================== */

/* 1) SMALL MOBILE (<= 480px) — micro-compact HUD & tap targets */
@media (max-width: 480px) {
    .top-hud-bar {
        top: max(6px, env(safe-area-inset-top));
        left: clamp(4px, 2vw, 8px);
        right: clamp(4px, 2vw, 8px);
        gap: clamp(4px, 1.2vw, 8px);
    }

    .hud-pill {
        padding: clamp(3px, 0.9vw, 6px) clamp(6px, 1.8vw, 10px);
        gap: clamp(3px, 0.8vw, 6px);
    }

    .hud-number {
        font-size: clamp(0.8rem, 3.4vw, 1rem);
    }

    .hud-unit {
        font-size: clamp(0.56rem, 2vw, 0.7rem);
    }

    .hud-btn {
        width: clamp(48px, 12vw, 56px);
        height: clamp(48px, 12vw, 56px);
    }

    .menu-card, .modal-card {
        width: min(94vw, 420px);
        padding: clamp(12px, 4vw, 18px) clamp(10px, 3.5vw, 14px);
        border-radius: 18px;
    }

    .start-button,
    .modal-btn,
    .story-codex-button,
    .story-nav-btn {
        min-height: 56px;
    }

    .diff-tab-btn {
        min-height: 48px;
    }

    .results-grid {
        grid-template-columns: 1fr 1fr;
        gap: 6px;
    }
}

/* 2) MOBILE (<= 767px) — full viewport containment + safe-area insets */
@media (max-width: 767px) {
    #app {
        height: 100dvh;
        width: 100vw;
    }

    #game-container, #game-container canvas {
        max-width: 100vw;
        max-height: 100dvh;
    }

    .hud-shell {
        width: min(calc(100vw - env(safe-area-inset-left) - env(safe-area-inset-right)), calc((100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * (540 / 960)));
        height: min(calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)), calc((100vw - env(safe-area-inset-left) - env(safe-area-inset-right)) * (960 / 540)));
    }

    .screen-overlay {
        padding: clamp(8px, 3vw, 14px);
        padding-top: max(clamp(8px, 3vw, 14px), env(safe-area-inset-top));
        padding-bottom: max(clamp(8px, 3vw, 14px), env(safe-area-inset-bottom));
    }

    .story-body-text {
        text-align: left;
    }

    .tutorial-banner {
        width: min(92%, 400px);
    }
}

/* 3) TABLET (768px - 1023px) — proportional scaling + balanced spacing */
@media (min-width: 768px) and (max-width: 1023px) {
    .menu-card, .modal-card {
        max-width: 480px;
        padding: clamp(20px, 3.5vw, 28px);
    }

    .hud-pill {
        padding: 8px 16px;
    }

    .hud-number {
        font-size: 1.3rem;
    }

    .energy-rush-container {
        width: min(420px, 62%);
    }

    .start-button, .story-codex-button {
        max-width: 320px;
    }

    .modal-btn {
        max-width: 320px;
    }

    .story-modal-card {
        max-width: 500px;
    }
}

/* 4) DESKTOP (1024px+) — centered letterboxed 9:16 presentation */
@media (min-width: 1024px) {
    #app {
        background: radial-gradient(circle at center, #1E3A26 0%, #060B08 100%);
    }

    .hud-shell {
        width: min(calc(100vw - env(safe-area-inset-left) - env(safe-area-inset-right)), calc((100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * (540 / 960)));
        height: min(calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)), calc((100vw - env(safe-area-inset-left) - env(safe-area-inset-right)) * (960 / 540)));
    }

    #game-container canvas {
        border-radius: 6px;
    }

    .hud-btn, .story-close-btn {
        cursor: pointer;
    }

    .menu-card, .modal-card {
        max-width: 460px;
    }
}

/* 5) LARGE DESKTOP (1280px+) — additional breathing room, richer backdrop */
@media (min-width: 1280px) {
    #app {
        background: radial-gradient(circle at center, #223F2A 0%, #050A07 100%);
    }

    .menu-card, .modal-card {
        max-width: 480px;
    }

    .story-modal-card {
        max-width: 520px;
    }
}

/* 6) EXTRA LARGE (1920px+) — strict aspect containment on 4K / ultra-wide */
@media (min-width: 1920px) {
    .hud-shell {
        width: min(calc(100vw - env(safe-area-inset-left) - env(safe-area-inset-right)), calc((100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * (540 / 960)));
        height: min(calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)), calc((100vw - env(safe-area-inset-left) - env(safe-area-inset-right)) * (960 / 540)));
        max-width: 100%;
    }

    #game-container {
        max-width: calc((100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * (540 / 960));
        margin: 0 auto;
    }
}

/* 7) LANDSCAPE MOBILE (short viewport in landscape) — ultra-compact modals */
@media (max-height: 500px) and (orientation: landscape) {
    .screen-overlay {
        align-items: stretch;
        overflow-y: auto;
    }

    .menu-card, .modal-card {
        max-height: 94dvh;
        padding: 8px 12px;
        margin: auto;
        gap: 6px;
    }

    .game-title {
        font-size: clamp(1.4rem, 5vw, 1.9rem);
    }

    .game-subtitle {
        font-size: clamp(0.75rem, 2.6vw, 0.9rem);
    }

    .difficulty-section,
    .controls-guide-box {
        padding: 6px 8px;
        margin-bottom: 6px;
    }

    .difficulty-selector {
        gap: 4px;
    }

    .diff-tab-btn {
        min-height: 40px;
        padding: 6px 4px;
    }

    .guide-row {
        gap: 6px;
    }

    .menu-button-group {
        gap: 6px;
    }

    .start-button, .story-codex-button, .modal-btn, .story-nav-btn {
        min-height: 44px;
        padding: 8px 14px;
    }

    .results-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 6px;
    }

    .result-card {
        padding: 6px 4px;
    }

    .result-val {
        font-size: clamp(1rem, 3vw, 1.3rem);
    }

    .top-hud-bar {
        top: max(4px, env(safe-area-inset-top));
    }

    .energy-rush-container {
        top: clamp(40px, 9vh, 56px);
        padding: 4px 8px 6px;
    }

    .tutorial-banner {
        bottom: max(8px, env(safe-area-inset-bottom));
        padding: 6px 10px;
    }
}