body,
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    touch-action: none;
    /* Prevent zoom/scroll on mobile */
    background: #251e36;
    /* Solid Color Performance Fix */
}

/* Loading Screen */
#loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #111;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: white;
}

.loader {
    border: 5px solid #333;
    border-top: 5px solid #ff4444;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: spin 1s linear infinite;
    margin-bottom: 20px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}


#main-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    width: 100%;
    min-height: 100vh;
    padding-top: calc(env(safe-area-inset-top) + 100px);
    padding-bottom: calc(env(safe-area-inset-bottom) + 30px);
    box-sizing: border-box;
}

#game-container {
    position: relative;
    /* width/height managed by canvas or flex */
    display: flex;
    justify-content: center;
    align-items: center;
}

canvas {
    display: block;
    /* Responsive resizing */
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 1;
    /* Keep square */
    max-width: 460px;
    /* Logical size cap */
    max-height: 460px;
    touch-action: none;
}

#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

#score-board {
    position: absolute;
    top: -50px;
    /* Move above the game container */
    left: 50px;
    /* Adjusted inwards 20->50 */
    color: white;
    font-size: 24px;
    font-weight: bold;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
    z-index: 5;
}

.top-controls {
    position: absolute;
    top: -50px;
    right: 50px;
    /* Adjusted inwards 20->50 */
    display: flex;
    gap: 10px;
    z-index: 5;
}

#retry-btn-top,
#settings-btn {
    width: 40px;
    height: 40px;
    background: transparent;
    border: 2px solid white;
    border-radius: 50%;
    color: white;
    font-size: 20px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.2s;
    backdrop-filter: blur(2px);
    pointer-events: auto;
    /* Ensure buttons are clickable */
}

#retry-btn-top:hover,
#settings-btn:hover {
    background: white;
    color: #333;
    transform: rotate(90deg);
}

#settings-btn {
    font-size: 18px;
}

/* Settings Modal */
#settings-modal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 20;
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(5px);
}

.modal-content {
    background: #3e3852;
    /* Slightly brighter for #6c6192 theme */
    padding: 30px 25px;
    border-radius: 20px;
    border: 2px solid #6c6192;
    text-align: center;
    color: #edebf2;
    /* Brighter text */
    min-width: 240px;
    /* Narrower */
    max-width: 230px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.4);
    position: relative;
}

.close-x {
    position: absolute;
    top: 15px;
    right: 17px;
    background: transparent;
    border: none;
    color: #9088ab;
    font-size: 26px;
    line-height: 1;
    cursor: pointer;
    transition: color 0.2s, transform 0.2s;
}

.close-x:hover {
    color: white;
    transform: scale(1.1);
}

.setting-item {
    margin: 18px 0;
    text-align: left;
}

.setting-item label {
    display: block;
    margin-bottom: 8px;
    font-size: 13px;
    color: #b5adc9;
    font-weight: bold;
}

.setting-item input[type="range"] {
    width: 100%;
    cursor: pointer;
    accent-color: #6c6192;
}

.modal-links {
    margin-top: 25px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    padding-top: 20px;
}

.link-btn {
    background: #4e4863;
    color: #edebf2;
    text-decoration: none;
    font-size: 12px;
    font-weight: bold;
    padding: 10px 4px;
    border-radius: 10px;
    transition: background 0.2s, transform 0.1s;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #6c6192;
}

.link-btn:hover {
    background: #6c6192;
    color: white;
}

.link-btn:active {
    transform: scale(0.97);
}

/* Removed GO Button styling or hidden it */
#go-btn {
    display: none !important;
}

/* Utility to hide elements forcefully */
.hidden {
    display: none !important;
}

/* Header (Game Over + Score) */
#game-header {
    position: fixed;
    top: calc(env(safe-area-inset-top) + 20px);
    left: 0;
    width: 100%;
    text-align: center;
    animation: fadeIn 0.5s;
    z-index: 15;
    pointer-events: none;
}

#game-header h1 {
    font-size: 42px;
    margin: 0 0 5px 0;
    color: #ff4444;
    -webkit-text-stroke: 2px white;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

#game-header p {
    font-size: 28px;
    margin: 0;
    font-weight: bold;
    color: white;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.8);
}

/* Footer (Buttons) */
#game-footer {
    position: absolute;
    top: calc(100% + 10px);
    left: 0;
    width: 100%;
    animation: fadeIn 0.5s;
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
    z-index: 15;
}

#game-footer button {
    padding: 8px 16px;
    font-size: 15px;
    cursor: pointer;
    border-radius: 20px;
    border: 2px solid white;
    background: rgba(255, 255, 255, 0.2);
    color: white;
    font-weight: bold;
    backdrop-filter: blur(5px);
    transition: all 0.2s;
    text-shadow: 1px 1px 2px black;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
}

#game-footer button:hover {
    background: white;
    color: #333;
    transform: scale(1.05);
    text-shadow: none;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Level Up Animation */
.level-up-container {
    position: absolute;
    top: 25%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 50;
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 28px;
    font-weight: bold;
    color: #ffeb3b;
    text-shadow: 0 0 5px #ff9800, 2px 2px 4px #000;
    opacity: 0;
}

.level-up-anim {
    animation: lvlUpPopIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards,
        lvlUpFadeOut 0.5s ease-in 1.5s forwards;
}

.level-up-anim .arrow {
    display: inline-block;
    animation: arrowFloat 0.8s ease-out 0.8s forwards;
}

@keyframes lvlUpPopIn {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0);
    }

    100% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
}

@keyframes lvlUpFadeOut {
    0% {
        opacity: 1;
    }

    100% {
        opacity: 0;
    }
}

@keyframes arrowFloat {
    0% {
        transform: translateY(0);
        opacity: 1;
    }

    100% {
        transform: translateY(-30px);
        opacity: 0;
    }
}

/* Next Balls Preview UI */
#next-preview-container {
    position: absolute;
    top: 5px;
    right: 50px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    color: white;
    font-size: 11px;
    font-weight: bold;
    text-shadow: 1px 1px 2px black;
    z-index: 5;
    background: rgba(0, 0, 0, 0.4);
    padding: 8px 10px;
    border-radius: 15px;
    backdrop-filter: blur(3px);
}

.preview-slot {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-size: cover;
    background-position: center;
    border: 2px solid rgba(255, 255, 255, 0.5);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.preview-arrow {
    color: rgba(255, 255, 255, 0.7);
    font-size: 14px;
}

/* Footer Links */
.footer-links {
    position: absolute;
    bottom: calc(env(safe-area-inset-bottom) + 20px);
    left: 0;
    width: 100%;
    display: flex;
    gap: 15px;
    justify-content: center;
    flex-wrap: wrap;
    z-index: 10;
}

.footer-links a {
    color: rgba(255, 255, 255, 0.7);
    text-decoration: none;
    font-size: 11px;
    transition: color 0.2s;
}

.footer-links a:hover {
    color: white;
}