:root {
    --bg-color: #fcf9f2;
    --text-dark: #4a4a4a;
    --text-light: #ffffff;
    
    /* RESTORED UI COLORS */
    --gray: #94a3b8;    /* Inconspicuous gray for top buttons */
    --blue: #4a90e2;    /* Primary blue for active modes & combo text */
    --red: #ef4444;     /* Red for skip hover & warnings */
    --green: #22c55e;   /* Green for the submit score button */
    
    /* Warm Progress Palette */
    --tile-1: #F9F6F2; /* Grey */
    --tile-2: #FEFEC5; /* Yellow */
    --tile-3: #FFD6A5; /* Orange */
    --tile-4: #FF9689; /* Red */
    --tile-5: #D8796E; /* Brown */
    
    --gold: linear-gradient(135deg, #ffd700, #ffec8b, #ffd700);
}

body {
    background-color: var(--bg-color);
    transition: background-color 1s ease;
    font-family: 'Quicksand', sans-serif;
    color: var(--text-dark);
    display: flex;
    justify-content: center;
    align-items: center;
    
    /* CHANGE: 100vh to 100dvh to fix iOS Safari */
    height: 100dvh; 
    
    margin: 0;
    user-select: none;
    overflow: hidden;
    touch-action: none;
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
}

#game-container {
    position: relative; 
    background: white;
    width: 480px; 
    max-width: 95vw; 
    
    /* CHANGE: Use dvh and remove the rigid 600px min-height */
    height: auto;
    max-height: 95dvh; 
    min-height: auto; 
    
    border-radius: 24px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.05);
    display: flex;
    flex-direction: column;
    padding: 20px;
    box-sizing: border-box; 
}

.reward-float {
    position: absolute;
    
    /* CHANGE: Force the text to center across the whole width */
    width: 100%;
    left: 0;
    text-align: center;
    
    color: #000000;
    font-weight: 900;
    font-size: 1.1rem;
    pointer-events: none;
    animation: floatUp 1.5s ease-out forwards;
    z-index: 1000;
    text-shadow: 1px 1px 0px rgba(255,255,255,0.7);
}



/* Explicitly align the text inside each column */
#timer {
    text-align: left;
}

#solve-count {
    text-align: center;
}

#score {
    text-align: right;
}

#stage {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    
    /* Default to NO WRAP so the tiles are forced to shrink as the screen gets smaller */
    flex-wrap: nowrap; 
}

.tile {
    /* Ideal starting size */
    width: 70px;
    height: 70px;
    
    /* Yes, you are allowed to shrink to fit the row */
    flex-shrink: 1; 
    
    /* But DO NOT shrink smaller than 60px */
    min-width: 60px;
    min-height: 60px; 
    
    border-radius: 16px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(1.2rem, 5vw, 1.5rem); 
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    transition: transform 0.1s ease, box-shadow 0.1s ease;
    box-sizing: border-box;
}
/* 1. Make the Operator symbols larger */
.op-btn {
    font-size: clamp(1.8rem, 6vw, 2.2rem); 
    padding: 5px 0; 
}

/* 2. Remove the letter-spacing tweak from the skip button */
#skip-btn { 
    left: 20px; 
    font-weight: 400; 
    font-size: 1.2rem;
}

/* 3. Update Header Grid to 4 Columns */
header {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr; /* Changed from 3 to 4 */
    align-items: center;
    font-size: clamp(0.9rem, 3.5vw, 1.1rem); /* Slightly smaller to fit 4 items */
    font-weight: 700;
    margin-bottom: 20px;
}
#combo-count { text-align: center; color: var(--blue); }

/* 4. Drag & Drop Visuals */
.tile.dragging {
    opacity: 0.5;
    transform: scale(1.05);
    box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}

/* --- THE THRESHOLD TRIGGER --- */
/* When the screen is 380px or smaller, 60px tiles no longer fit on one line */
@media (max-width: 380px) {
    #stage {
        /* Flip the switch to let them break into 2 rows */
        flex-wrap: wrap; 
        align-content: center;
    }
}

.tile:active { transform: scale(0.95); }
.tile.selected { outline: 3px solid #777; transform: scale(1.05); }

/* Combo Colors (Levels 1 through 5) */
.combo-1 { background-color: var(--tile-1); color: var(--text-dark); }
.combo-2 { background-color: var(--tile-2); color: var(--text-dark); }
.combo-3 { background-color: var(--tile-3); color: var(--text-dark); }
.combo-4 { background-color: var(--tile-4); color: var(--text-dark); } /* Dark text reads better on this pastel red */
.combo-5 { background-color: var(--tile-5); color: var(--text-light); } /* Light text for the darker brown */

/* The Solution Tile (Level 5 + Equals 50) */
.gold { 
    background: var(--gold); 
    background-size: 200% 200%;
    animation: shine 2s infinite linear, pulse 1s infinite alternate;
    color: #8b6508; /* Dark gold text for contrast */
    box-shadow: 0 0 20px rgba(255, 215, 0, 0.8), inset 0 0 10px rgba(255, 255, 255, 0.5);
    border: 2px solid #fff;
}

@keyframes shine {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

@keyframes pulse {
    0% { transform: scale(1); }
    100% { transform: scale(1.05); }
}



button {
    font-family: inherit;
    font-size: clamp(1rem, 4vw, 1.25rem); /* Responsive button text */
    font-weight: 700;
    border: none;
    border-radius: 12px;
    background: #f1f5f9;
    color: var(--text-dark);
    padding: 15px 0;
    cursor: pointer;
    transition: background 0.2s;
}

button.selected-op { background: #cbd5e1; outline: 2px solid #94a3b8; }
button:active { background: #e2e8f0; }
#undo-btn { font-size: clamp(0.8rem, 3vw, 1rem); background: #ffe4e6; }

/* Modal Settings */
.hidden { display: none !important; }

#game-over-modal {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(255,255,255,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(4px);
    z-index: 100;
}

/* --- Restored Controls Grid --- */
#controls {
    display: grid;
    grid-template-columns: repeat(5, 1fr); /* Back to 5 equal columns */
    gap: 2%; 
    margin-top: 20px;
}

/* --- Top Corner Buttons (Menu & Skip) --- */
#menu-btn, #skip-btn {
    position: absolute; 
    top: 20px;
    background: none;
    border: none;
    font-size: 1.8rem;
    color: var(--gray);
    cursor: pointer;
    padding: 5px;
    transition: transform 0.2s, color 0.2s;
    display: flex;
    align-items: center;
    z-index: 10;
}

/* Pin to opposite sides */
#menu-btn { right: 20px; }


/* Hover & Active States */
#menu-btn:hover {
    transform: scale(1.15);
    color: var(--blue);
}

#skip-btn:hover {
    transform: scale(1.15);
    color: var(--red); /* Red on hover to warn them it's a penalty */
}

#skip-btn:active, #menu-btn:active {
    transform: scale(0.95);
}

.modal-content ul { 
    list-style: none; 
    padding: 0; 
    margin-bottom: 20px; 
    text-align: left;
}

.modal-content li {
    padding: 5px 0;
    border-bottom: 1px solid #eee;
}

/* --- New Styles for Headers & Modes --- */
#game-container {
    height: auto; /* Allow container to grow with new content */
    min-height: 600px;
}

#header-titles {
    text-align: center;
    margin-bottom: 15px;
}

#header-titles h1 {
    margin: 0 0 5px 0;
    font-size: clamp(1.8rem, 6vw, 2.2rem);
    color: var(--text-dark);
}

#header-titles p {
    margin: 0;
    font-size: clamp(0.85rem, 3vw, 1rem);
    color: #666;
}

/* Fix Modal Width */
.modal-content {
    background: white;
    padding: 30px;
    border-radius: 20px;
    text-align: center;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    
    width: 90%;            /* Adaptive width */
    max-width: 450px;      /* Increased to fit the practice buttons */
}

/* Allow buttons to wrap to a new line on small screens */
#mode-selector {
    display: flex;
    flex-wrap: wrap;       
    justify-content: center;
    gap: 10px;
    margin-bottom: 20px;
}




.mode-btn {
    padding: 8px 16px;
    font-size: clamp(0.8rem, 3vw, 0.9rem);
    border-radius: 20px;
    background: #e2e8f0;
    color: var(--text-dark);
    border: 2px solid transparent;
}

.mode-btn.active {
    background: var(--blue);
    color: var(--text-light);
    border-color: #5c85b5;
    transform: scale(1.05);
}

/* --- New Styles for Score Submission --- */
#score-submission {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 15px;
}

#player-name {
    padding: 12px;
    border-radius: 12px;
    border: 2px solid var(--gray);
    font-family: inherit;
    font-size: 1rem;
    text-align: center;
    outline: none;
    transition: border-color 0.2s;
}

#player-name:focus {
    border-color: var(--blue);
}

#submit-score-btn {
    background: var(--green);
    color: white;
}
#submit-score-btn:active {
    background: #3a7a4d;
}

/* --- Solution Display Styles --- */
#solution-display {
    background: #fff0f0;
    color: var(--red);
    padding: 10px;
    border-radius: 8px;
    font-size: 0.9rem;
    margin: 15px 0;
    border: 1px dashed var(--red);
}

#solution-display strong {
    font-size: 1.1rem;
    letter-spacing: 1px;
    display: inline-block;
    margin-top: 5px;
}




/* --- Menu Modal Specifics --- */
.menu-actions {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 25px;
}

#close-info-btn {
    width: 100%;
    background: var(--blue);
    color: white;
}

#close-info-btn:active {
    background: #5c85b5;
}

#quit-save-btn {
    width: 100%;
    background: #fff0f0;
    color: var(--red);
    border: 2px solid var(--red);
    font-size: 1rem;
}

#quit-save-btn:active {
    background: var(--red);
    color: white;
}

footer {
    position: absolute;
    bottom: 15px;
    font-size: 0.85rem;
    color: #999;
    text-align: center;
    width: 100%;
    font-weight: 500;
}

/* --- Info Modal Specifics --- */
#info-modal {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(255,255,255,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(4px);
    z-index: 200; /* Stays above everything */
}

.info-content {
    text-align: left; /* Easier to read instructions */
}

.info-content h2 {
    text-align: center;
    margin-top: 0;
    color: var(--text-dark);
}

.info-content ul {
    padding-left: 20px;
    margin-bottom: 25px;
    list-style-type: disc;
}

.info-content li {
    font-size: 0.95rem;
    margin-bottom: 12px;
    border-bottom: none; /* overrides the leaderboard li style */
    color: #555;
    line-height: 1.4;
}

#close-info-btn {
    width: 100%;
    background: var(--blue);
    color: white;
}

#close-info-btn:active {
    background: #5c85b5;
}


@keyframes floatUp {
    0% { transform: translateY(0); opacity: 1; }
    100% { transform: translateY(-50px); opacity: 0; }
}

#instruction-overlay {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--blue);
    text-align: center;
    margin-bottom: 10px;
}

/* --- Solution Steps Visualization --- */
#solution-display {
    background: #f8fafc; /* Softer background */
    padding: 15px;
    border-radius: 12px;
    margin: 15px 0;
    border: 2px solid #e2e8f0;
}

#solution-text {
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
}

.solution-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.mini-tile {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.1rem;
    font-weight: 700;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.solution-op {
    color: var(--text-dark);
    font-weight: 900;
    font-size: 1.2rem;
    width: 15px; 
    text-align: center;
}

/* --- Hint Button & Highlights --- */
#hint-btn {
    display: block;
    margin: 10px auto;
    background: #ffedd5;
    color: #c2410c;
    font-size: clamp(0.9rem, 3vw, 1.1rem);
    padding: 8px 24px;
    border-radius: 16px;
    animation: fadeIn 0.5s ease-out;
}

#hint-btn:active {
    background: #fdba74;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}

.hint-highlight {
    /* Bright, pulsing gold/orange border and shadow */
    border: 3px solid #ff9800 !important;
    box-shadow: 0 0 15px 5px rgba(255, 152, 0, 0.6) !important;
    animation: pulseHint 1s infinite alternate;
}

@keyframes pulseHint {
    0% { transform: scale(1); box-shadow: 0 0 15px 5px rgba(255, 152, 0, 0.4) !important; }
    100% { transform: scale(1.08); box-shadow: 0 0 25px 8px rgba(255, 152, 0, 0.9) !important; }
}