:root {
    --primary-color: #E63946;
    --primary-dark: #BC2B36;
    --secondary-color: #F1FAEE;
    --text-dark: #1D3557;
    --success-color: #2A9D8F;
    --warning-color: #E9C46A;
    --glass-bg: rgba(255, 255, 255, 0.90);
    --glass-border: rgba(255, 255, 255, 0.5);
    --font-main: 'Outfit', sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
}

body {
    font-family: var(--font-main);
    background: #000000; 
    color: var(--text-dark);
    height: 100vh;
    overflow: hidden;
}

#game-container {
    position: relative;
    width: 100vw;
    height: 100vh;
    perspective: 1200px;
    background: transparent;
    /* NO z-index here — must not create isolated stacking context.
       world-canvas-container (z-index:1) and fixed portal (z-index:0)
       are compared in the ROOT context so alpha canvas shows portal. */
}

#world-canvas-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1; /* Above totem-portal (z-index 0); alpha canvas lets portal show through transparent pixels */
}

#ui-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    pointer-events: none;
    /* Let clicks pass through to 3D if needed, but we mostly re-enable pointer events on active elements */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.game-header-floating {
    position: absolute;
    top: calc(20px + env(safe-area-inset-top));
    left: calc(20px + env(safe-area-inset-left));
    background: var(--glass-bg);
    padding: 10px 20px;
    border-radius: 12px;
    backdrop-filter: blur(8px);
    text-align: center;
    align-items: center;
    pointer-events: auto;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    width: auto;
    max-width: 350px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.logo {
    font-weight: 700;
    color: var(--primary-color);
    font-size: 1.2rem;
}

.controls-hint {
    font-size: 0.8rem;
    color: #555;
    background: #fff;
    padding: 2px 10px;
    border-radius: 10px;
    align-self: center;
}

.progress-bar-container {
    width: 100%;
    height: 6px;
    background: #e0e0e0;
    border-radius: 3px;
    overflow: hidden;
}

.progress-bar {
    width: 0%;
    height: 100%;
    background: var(--primary-color);
    transition: width 0.5s ease;
}

/* Interaction Prompt */
.interaction-prompt {
    position: absolute;
    bottom: 100px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 15px 30px;
    border-radius: 30px;
    font-size: 1.2rem;
    display: flex; /* STABILITY: Force flex layout globally to prevent transition shifts */
    align-items: center;
    gap: 12px;
    /* animation: bounce 1s infinite; -- Removed for global stability */
    transition: opacity 0.3s ease; /* PURE FADE: removed transform transition */
    opacity: 1;
    transform: none;
}

.interaction-prompt.hidden {
    display: flex !important; /* STABILITY FIX: Keep flex layout during transition */
    opacity: 0;
    transform: none !important; /* PURE FADE: no vertical movement */
    pointer-events: none;
    animation: none !important; /* STABILITY FIX: Stop bouncing while hiding */
}

.interaction-prompt span {
    color: var(--warning-color);
    font-weight: 700;
    border: none;
    padding: 0;
    background: #000000; /* Switch to black to match the new icon asset and the prompt UI */
    border-radius: 50%; /* Force circular clipping in all views */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px; /* Base size for desktop/tablets */
    height: 32px;
    overflow: hidden; /* Ensure corners of the image are clipped */
    flex-shrink: 0;
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-10px);
    }
}

/* Stage Container (Mini-Games) */
.stage-container {
    pointer-events: auto;
    background: var(--glass-bg);
    width: 95%;
    max-width: 900px;
    padding: 30px;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
    max-height: 80vh;
    overflow-y: auto;
}

.stage-container.hidden {
    display: none;
}

.hidden {
    display: none !important;
}

/* Digital Totem Portal – fixed position, under alpha WebGL canvas */
.totem-portal {
    position: fixed;
    width: 1080px;
    height: 1920px;
    pointer-events: none;
    z-index: 0;
    background: #fff;
    top: 0;
    left: 0;
    transform-origin: 0 0;
}


.totem-portal img {
    width: 100%;
    height: 100%;
    border: none;
    object-fit: contain;
    transform: rotate(180deg);
    transform-origin: center center;
}

/* Reuse existing button/input styles */
.btn {
    padding: 15px 30px;
    border: none;
    border-radius: 50px;
    font-family: var(--font-main);
    font-weight: 600;
    cursor: pointer;
    background: var(--secondary-color);
    color: var(--text-dark);
    margin-top: 10px;
}

.btn.primary {
    background: var(--primary-color);
    color: white;
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.scene {
    width: 100%;
    text-align: center;
}

.scene h2 {
    margin-bottom: 15px;
    color: var(--primary-dark);
}

.scene p {
    margin-bottom: 20px;
    line-height: 1.5;
}

/* Mini-games reuse */
.lab-game-area,
.donation-game-area {
    margin: 20px auto;
    position: relative;
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
}

.lab-game-area {
    height: 300px;
    border: 2px dashed #a8dadc;
}

.donation-game-area {
    padding: 10px;
    border: 1px solid #ddd;
}

.rhythm-bar-container {
    width: 100%;
    height: 30px;
    background: #ddd;
    position: relative;
    border-radius: 15px;
    margin: 15px 0;
}

.target-zone {
    position: absolute;
    height: 100%;
    top: 0;
    background: rgba(42, 157, 143, 0.5);
    border-left: 2px solid var(--success-color);
    border-right: 2px solid var(--success-color);
}

.cursor-marker {
    position: absolute;
    height: 100%;
    width: 5px;
    background: var(--primary-color);
    top: 0;
}

.catcher {
    width: 50px;
    height: 50px;
    background: var(--success-color);
    position: absolute;
    bottom: 0;
    border-radius: 5px;
}

/* SVG background replaced with simple color for code brevity or keep SVG if preferred */
.drop {
    width: 30px;
    height: 30px;
    position: absolute;
    font-size: 20px;
    text-align: center;
}

/* Modal */
#modal-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    pointer-events: auto;
}

.modal-content {
    background: white;
    padding: 30px;
    border-radius: 20px;
    text-align: center;
    max-width: 400px;
}

/* Debug Controls */
.debug-controls {
    position: absolute;
    top: 20px;
    right: 20px;
    pointer-events: auto;
    z-index: 100;
}

.btn-debug {
    background: rgba(45, 52, 54, 0.8);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 10px 20px;
    border-radius: 12px;
    font-family: var(--font-main);
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    backdrop-filter: blur(5px);
    transition: all 0.2s ease;
}

.btn-debug:hover {
    background: rgba(45, 52, 54, 1);
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.btn-debug.active {
    background: var(--primary-color);
    border-color: var(--primary-dark);
}

/* MOBILE CONTROLS (Joystick & Use Button) */
.mobile-controls {
    display: none; /* Shown only via JS on mobile */
    position: fixed;
    bottom: 25px;
    left: 25px;
    right: 25px;
    z-index: 2000;
    flex-direction: row-reverse;
    justify-content: space-between;
    align-items: flex-end;
    pointer-events: none;
}

/* USE BUTTON */
.button-use {
    width: 85px;
    height: 85px;
    background: none;
    color: white;
    border: none;
    border-radius: 50%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: none;
    pointer-events: auto;
    user-select: none;
    touch-action: none;
    transition: transform 0.1s;
}

/* NEW: Icon with clipping for generated assets to hide imperfections on edges */
.btn-use-icon {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
    /* Optimized scale (1.6) to fully bleed/hide the black fringe from the new black-background asset */
    transform: scale(1.6); 
    transform-origin: center;
    display: block;
    image-rendering: -webkit-optimize-contrast;
}

/* NEW: CSS Background Image for smooth prompt loading without DOM refresh delays */
.btn-use-bg {
    width: 100%;
    height: 100%;
    background-image: url('btn_use.png');
    background-size: 160%;
    background-position: center;
    border-radius: 50%;
}

.button-use:active {
    transform: scale(0.9);
}

/* JOYSTICK */
.joystick-container {
    width: 150px;
    height: 150px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    position: relative;
    pointer-events: auto;
    touch-action: none;
    box-shadow: inset 0 0 20px rgba(0,0,0,0.2);
}

.joystick-knob {
    width: 65px;
    height: 65px;
    background: linear-gradient(135deg, #ffffff 0%, #bdc3c7 100%);
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    pointer-events: none;
}

/* ORIENTATION WARNING */
#orientation-warning {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #0f0f0f;
    color: white;
    z-index: 9999;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 30px;
    font-family: 'Outfit', sans-serif;
    pointer-events: none; /* Block interactions ONLY when display: flex is set elsewhere */
}

.warning-icon {
    font-size: 80px;
    margin-bottom: 25px;
    animation: rotate-device-anim 2s infinite ease-in-out;
}

@keyframes rotate-device-anim {
    0% { transform: rotate(0deg); }
    50% { transform: rotate(90deg); }
    100% { transform: rotate(0deg); }
}

#orientation-warning h2 {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 12px;
}

#orientation-warning p {
    font-size: 18px;
    opacity: 0.7;
    max-width: 300px;
}

/* Orientation warnings are controlled entirely via JS (style.display)
   to ensure correct sequencing with the minigame overlay. */

/* MOBILE REFINEMENTS */
@media screen and (max-width: 1366px) {
    body.is-mobile .controls-hint {
        display: none !important; /* Hide WSAD/E hints on small screens */
    }

    body.is-mobile .game-header-floating {
        display: none !important;
    }
    
    body.is-mobile .interaction-prompt {
        animation: none !important; 
        font-size: 15px !important;
        padding: 4px 18px !important;
        top: calc(20px + env(safe-area-inset-top)) !important; /* MOVE TO TOP */
        bottom: auto !important;
        left: 50% !important; /* BACK TO CENTER */
        transform: translateX(-50%) !important;
        width: max-content !important;
        max-width: 90vw !important; /* Full width available at top */
        background: rgba(0, 0, 0, 0.75) !important;
        border-radius: 40px !important;
        box-shadow: 0 4px 15px rgba(0,0,0,0.4) !important;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px !important;
        pointer-events: none;
        border: 1px solid rgba(255,255,255,0.1);
        min-height: 34px !important;
        white-space: nowrap !important;
        transform-origin: center top !important;
        text-align: center;
        overflow: hidden !important; 
        transition: opacity 0.3s, transform 0.3s !important;
    }

    body.is-mobile .interaction-prompt.hidden {
        left: 50% !important;
        top: calc(20px + env(safe-area-inset-top)) !important;
        transform: translateX(-50%) translateY(-20px) !important; /* SLIDE UP to hide */
        opacity: 0 !important;
        display: flex !important;
    }
    
    body.is-mobile .interaction-prompt span {
        background: #000000; 
        width: 24px;
        height: 24px;
        border-radius: 50%;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0 !important;
        vertical-align: middle;
        overflow: hidden;
    }

    body.is-mobile .interaction-prompt-label {
        display: inline;
        color: inherit;
        font-weight: inherit;
    }

    body.is-mobile #minigame-overlay > div {
        border-radius: 15px !important;
    }
    body.is-mobile #minigame-overlay > div.fullscreen-minigame {
        width: 100% !important;
        height: 100% !important;
        max-width: none !important;
        max-height: none !important;
        border-radius: 0 !important;
        transform: none !important;
        transition: none !important;
    }
}

@media screen and (max-width: 768px) {
    body.is-mobile .game-header-floating {
        display: none !important;
    }
    body.is-mobile .mobile-controls {
        display: flex; 
        bottom: calc(15px + env(safe-area-inset-bottom)); 
        left: calc(15px + env(safe-area-inset-left));
        right: calc(15px + env(safe-area-inset-right));
        justify-content: space-between;
    }
    body.is-mobile .button-use {
        width: 85px;
        height: 85px;
        background: none;
        border: none;
        padding: 0;
        display: block;
        cursor: pointer;
        outline: none;
        -webkit-tap-highlight-color: transparent;
    }
    .joystick-container {
        width: 130px;
        height: 130px;
    }
}

/* Hide controls if the device is in portrait mode */
@media screen and (orientation: portrait) and (max-width: 1366px) {
    .mobile-controls {
        display: none !important;
    }
}