:root {
    --primary: #6a1b9a;
    --accent: #ffeb3b;
    --danger: #f44336;
    --success: #4caf50;
    --dark: #212121;
    --light: #fafafa;
    --font-main: 'Outfit', sans-serif;
}

* {
    box-sizing: border-box;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

body,
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    background-color: #000;
    overflow: hidden;
    font-family: var(--font-main);
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
}

#game-container {
    position: relative;
    width: 100%;
    height: 100%;
    /* Mobile full height */
    height: 100dvh;
    /* Desktop constraints */
    max-width: 420px;
    max-height: 900px;
    /* Simulate modern phone height */
    margin: 0 auto;
    background: #444;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.8);
    overflow: hidden;
}

/* Desktop/Landscape Constraints */
@media (min-aspect-ratio: 1/1) {
    #game-container {
        height: 90dvh; 
        max-height: 850px;
        border-radius: 20px;
        margin-top: auto;
        margin-bottom: auto;
    }
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
    background: #1a1a2e;
    /* Solid color for better performance */
    /* Hardware acceleration hints for Android */
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    will-change: contents;
    /* Disable image smoothing for crisper edges */
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
}

.intro-canvas {
    background: transparent !important;
    vertical-align: middle;
    width: auto !important;
    height: auto !important;
    display: inline-block !important;
}

#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

#hud {
    position: absolute;
    top: 0;
    width: 100%;
    padding: 15px;
    display: flex;
    justify-content: space-between;
    font-weight: 900;
    font-size: 1.2rem;
    z-index: 10;
    pointer-events: none;
    overflow: hidden;
}

.hud-item {
    background: rgba(0, 0, 0, 0.6);
    padding: 8px 12px;
    border-radius: 20px;
    color: white;
    text-shadow: 1px 1px 0 #000;
    white-space: nowrap;
    backdrop-filter: blur(4px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 0.9rem;
    max-width: 45%;
    overflow: hidden;
    text-overflow: ellipsis;
}

.hud-item.right {
    color: var(--accent);
}

/* Screens - SLEEK GLASS STYLE */
.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Match parent container exactly */
    background: radial-gradient(circle at center, rgba(60, 20, 80, 0.9), rgba(10, 5, 15, 0.95));
    backdrop-filter: blur(8px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    pointer-events: auto;
    transition: opacity 0.3s;
    z-index: 20;
    padding: 20px 10px;
    padding-bottom: 25px;
    /* Reduced padding for more content space */
    max-height: 100dvh;
    overflow: hidden;
    box-sizing: border-box;
    overflow-y: auto;
}

/* Allow scrolling on game over screen specifically */
#game-over-screen {
    overflow-y: auto;
    justify-content: flex-start;
    /* Stack from top */
    padding-bottom: 20px;
}

.screen.hidden {
    opacity: 0;
    pointer-events: none;
}

.title-box {
    flex-shrink: 0;
    margin-top: 0;
    margin-bottom: 10px;
}

.title-box h1 {
    font-size: clamp(1.8rem, 5vw, 2.5rem);
    line-height: 0.9;
    text-align: center;
    background: linear-gradient(135deg, #e1bee7 0%, #ab47bc 50%, #7b1fa2 100%);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin: 0;
    filter: drop-shadow(0 4px 0px rgba(0, 0, 0, 0.5));
    text-transform: uppercase;
    font-weight: 900;
}

.title-box p {
    text-align: center;
    font-size: 1rem;
    color: #e1bee7;
    margin: 5px 0 0 0;
    font-weight: 300;
    letter-spacing: 1px;
}

.pc-warning {
    color: var(--accent) !important;
    font-weight: 900 !important;
    font-size: 1.2rem !important;
    text-transform: uppercase;
    display: none;
    /* Hidden by default, JS shows on desktop */
    animation: pulse 2s infinite;
}

.hero-box {
    /* Removed flex: 1 to prevent huge gaps */
    flex: 0 1 auto;
    min-height: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    margin-top: 15px;
    margin-bottom: 5px;
}

.hero-img {
    width: 80%;
    max-width: 320px;
    height: auto;
    object-fit: contain;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    animation: float 6s ease-in-out infinite;
}

@keyframes float {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-10px);
    }
}

button {
    margin-top: 10px;
    margin-bottom: 0;
    padding: 15px 50px;
    font-size: 1.6rem;
    font-family: var(--font-main);
    font-weight: 900;
    color: #fff;
    background: linear-gradient(135deg, #8e24aa, #4a148c);
    border: none;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 10px 20px rgba(74, 20, 140, 0.5), inset 0 2px 0 rgba(255, 255, 255, 0.2);
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: transform 0.1s, box-shadow 0.1s;
    position: relative;
    overflow: hidden;
}

button::after {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
    transform: rotate(45deg);
    animation: shine 3s infinite;
}

@keyframes shine {
    0% {
        transform: translateX(-100%) rotate(45deg);
    }

    100% {
        transform: translateX(100%) rotate(45deg);
    }
}

button:active {
    transform: scale(0.96);
    box-shadow: 0 5px 10px rgba(74, 20, 140, 0.5);
}

.pulse-btn {
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(171, 71, 188, 0.7);
    }

    70% {
        box-shadow: 0 0 0 20px rgba(171, 71, 188, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(171, 71, 188, 0);
    }
}

/* Tutorial */
.tutorial-box {
    margin: 10px 0 10px 0;
    background: rgba(255, 255, 255, 0.05);
    padding: 10px 25px;
    border-radius: 20px;
    width: 80%;
    max-width: 320px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(5px);
}

.tut-row {
    display: grid;
    grid-template-columns: 80px 30px 1fr;
    /* Fixed icon width aligns arrows */
    align-items: center;
    gap: 25px;
    margin-bottom: 15px;
    font-size: 1.1rem;
    color: #eee;
}

.tut-row:nth-child(2) {
    margin-bottom: 25px;
}

.icon {
    font-size: 1.5rem;
    text-align: center;
    display: flex;
    justify-content: center;
    /* Center icons in box */
    align-items: center;
    gap: 2px;
    /* Slight gap */
}

.arrow {
    display: inline-block;
}

/* 2x2 Grid for compact waste display */
.waste-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0px;
    justify-items: center;
    align-items: center;
    margin: 0 auto;
}

/* Pull items even closer */
.waste-grid>* {
    margin: -8px;
}

/* Force spider to be black silhouette */
.spider-icon {
    filter: brightness(0);
}

/* Extra tight for good grapes */
.icon.match-grape {
    gap: 0;
}

.icon.match-grape .intro-canvas {
    margin: 0 -5px;
}

.desc {
    font-weight: 700;
    color: #fff;
    text-align: center;
}

.arrow {
    font-size: 1.2rem;
    color: var(--accent);
    opacity: 0.8;
    text-align: center;
    display: flex;
    justify-content: center;
}

.grape-preview {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 35%, #ab47bc, #4a148c);
    display: inline-block;
    box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.3);
    vertical-align: middle;
    margin: 0 2px;
}

.unripe-preview {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 35%, #c5e1a5, #7cb342);
    display: inline-block;
    box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.3);
    vertical-align: middle;
    margin: 0 2px;
}

.raisin-preview {
    width: 10px;
    height: 10px;
    border-radius: 40% 60% 60% 40% / 40% 50% 50% 60%;
    background: radial-gradient(circle at 35% 35%, #5d4037, #3e2723);
    display: inline-block;
    box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.3);
    vertical-align: middle;
    margin: 0 2px;
}

.grape-bunch-icon {
    width: 35px;
    height: 35px;
    vertical-align: middle;
    filter: drop-shadow(1px 1px 2px rgba(0, 0, 0, 0.3));
}

.leaf-icon {
    width: 30px;
    height: auto;
    object-fit: contain;
    filter: drop-shadow(1px 1px 2px rgba(0, 0, 0, 0.3));
    vertical-align: middle;
    margin: 0 2px;
}

/* Leaderboard */
.leaderboard {
    margin-top: 20px;
    width: 90%;
    max-width: 350px;
    background: rgba(0, 0, 0, 0.6);
    border-radius: 15px;
    padding: 15px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.leaderboard h3 {
    text-align: center;
    margin: 0 0 10px 0;
    color: var(--accent);
    text-transform: uppercase;
    font-size: 1rem;
    /* Smaller */
    letter-spacing: 2px;
}

#game-over-screen h1 {
    font-size: clamp(2.5rem, 8vw, 3rem);
    /* Reduced max from 3.5rem to 3rem */
    margin: 5px 0;
    /* Minimal margin */
}

#level-title {
    font-size: clamp(2.5rem, 8vw, 3.5rem);
    margin: 10px 0;
    text-align: center;
    background: linear-gradient(135deg, #e1bee7 0%, #ab47bc 50%, #7b1fa2 100%);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    filter: drop-shadow(0 4px 0px rgba(0, 0, 0, 0.5));
    text-transform: uppercase;
    font-weight: 900;
}

.final-score-box {
    background: rgba(0, 0, 0, 0.6);
    border: 2px solid var(--accent);
    padding: 10px 20px;
    /* Compact padding */
    border-radius: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-shadow: 0 0 20px rgba(255, 235, 59, 0.2);
    margin-bottom: 10px;
    flex-shrink: 0;
}

.final-score-box p {
    color: #ccc;
    text-transform: uppercase;
    font-size: 0.9rem;
    letter-spacing: 2px;
    margin: 0;
}

.final-score-box span {
    font-size: 3rem;
    /* Smaller but still big */
    font-weight: 900;
    color: var(--accent);
    text-shadow: 0 0 10px rgba(255, 235, 59, 0.5);
    line-height: 1;
}

#leaderboard-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

#leaderboard-list li {
    display: flex;
    justify-content: space-between;
    padding: 4px 8px;
    /* Compact padding */
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 0.9rem;
    /* Compact font */
    text-transform: uppercase;
}

#leaderboard-list li:last-child {
    border-bottom: none;
}

#leaderboard-list li:nth-child(1) {
    color: #FFD700;
    font-weight: bold;
}

#leaderboard-list li:nth-child(2) {
    color: #C0C0C0;
    font-weight: bold;
}

#leaderboard-list li:nth-child(3) {
    color: #CD7F32;
    font-weight: bold;
}

/* Leaderboard Compact Container */
.leaderboard {
    margin-top: 10px;
    padding: 10px;
    width: 95%;
    max-width: 350px;
    background: rgba(0, 0, 0, 0.6);
    border-radius: 15px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.leaderboard h3 {
    font-size: 1rem;
    margin-bottom: 5px;
    text-align: center;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 2px;
}

/* Input Compact */
#new-high-score-input,
#high-score-form {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    margin: 10px 0;
    width: 100%;
}

input[type="text"] {
    padding: 8px;
    font-size: 1.2rem;
    text-align: center;
    border-radius: 10px;
    border: 2px solid var(--accent);
    background: rgba(0, 0, 0, 0.5);
    color: white;
    font-family: var(--font-main);
    text-transform: uppercase;
    width: 70%;
    outline: none;
    transition: border-color 0.3s;
}

input[type="text"]:focus {
    border-color: #fff;
    background: rgba(0, 0, 0, 0.7);
}

.new-record {
    color: var(--accent);
    font-weight: 900;
    font-size: 1.4rem;
    text-transform: uppercase;
    margin: 0;
    filter: drop-shadow(0 0 10px rgba(255, 215, 0, 0.5));
    animation: flash 1s infinite alternate;
}

@keyframes flash {
    from {
        opacity: 0.6;
        transform: scale(1);
    }

    to {
        opacity: 1;
        transform: scale(1.05);
    }
}

.hidden {
    display: none !important;
}

/* Loading Overlay */
#loading-overlay {
    background: rgba(0, 0, 0, 0.4);
    /* Partial transparency to see empty conveyor */
    backdrop-filter: blur(2px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.loading-box {
    background: rgba(0, 0, 0, 0.85);
    padding: 20px;
    border-radius: 20px;
    border: 2px solid var(--accent);
    /* Flexbox centering is more robust on Android */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    width: 80%;
    /* Conservative width */
    max-width: 280px;
    overflow: hidden;
    /* This should work now without transform */
    position: relative;
    /* Ensure stacking context */
    z-index: 1001;
    /* Removed animation to prevent transform conflicts on Android */
}

@keyframes popIn {
    from {
        opacity: 0;
        transform: scale(0.8);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

.loading-box h2 {
    color: var(--accent);
    margin: 0 0 20px 0;
    text-transform: uppercase;
    font-size: 1.2rem;
    /* Smaller font to help fit */
    letter-spacing: 1px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
    /* FORCE WRAPPING */
    white-space: normal;
    word-wrap: break-word;
    overflow-wrap: break-word;
    width: 100%;
    line-height: 1.4;
}

.progress-container {
    width: 140px;
    /* Fixed width */
    height: 16px;
    /* Slightly smaller */
    background: rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    /* Reduce radius */
    overflow: hidden;
    margin: 0;
    /* Flexbox handles centering */
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-sizing: border-box;
    position: relative;
    /* Stay inside parent */
    z-index: 1002;
}

/* Ensure border is included in width */


#loading-bar {
    width: 0%;
    height: 100%;
    background: linear-gradient(90deg, #ab47bc, #7b1fa2);
    /* Removed shadow to prevent bleeding */
    transition: width 0.1s linear;
}