:root {
    --bg-color: #050510;
    --ui-bg: rgba(10, 10, 20, 0.95);
    --highlight: #f0f;
    --glow: #0ff;
    --warning: #ff3333;
    --score-col: #ffee00;
    --bomb-col: #ff3300;
    --gold-col: #ffd700;
    --purple-col: #d000ff;
    --brake-col: #ffaa00;
    --cryo-col: #2255ff; 
    --overdrive-col: #39ff14;
    --grid-width: 100vw; /* Updated by JS */
}

/* FIXED: Added the '*' selector here */
* {
    /* Removes the grey highlight box when tapping buttons on mobile */
    -webkit-tap-highlight-color: transparent;
    
    /* Prevents the browser from inflating font sizes on rotation */
    -webkit-text-size-adjust: 100%; 
}

body {
    margin: 0;
    padding: 0; /* Safety */
    background-color: var(--bg-color);
    color: white;
    font-family: 'Courier New', Courier, monospace;
    font-family: 'Rajdhani', sans-serif;
    overflow: hidden;
    overscroll-behavior: none; 
    display: flex;
    flex-direction: column;
    height: 100vh; 
    height: 100svh; 
    width: 100%; /* CHANGED from 100vw to 100% to fix shifting */
    max-width: 100%; /* Double Safety */
    user-select: none;
}

/* --- Screens & Overlays --- */
#start-screen, #pause-screen, #fail-overlay, #rules-screen, #leaderboard-screen {
    position: absolute;
    top: 0; left: 0; 
    /* CHANGED: Use width 100% instead of right:0 */
    width: 100%; 
    height: 100%; 
    background: rgba(5, 5, 16, 0.95);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 100;
    text-align: center;
    overflow: hidden;
}

#fail-overlay, #pause-screen, #leaderboard-screen {
    background: rgba(0,0,0,0.9);
    backdrop-filter: blur(5px);
}

/* Matrix Background Canvas */
#bg-matrix {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: -1; 
    opacity: 0.6; 
}

.hidden { display: none !important; }

.title-text {
    font-size: 3.5rem;
    color: var(--glow);
    text-shadow: 0 0 20px var(--glow), 0 0 40px var(--highlight);
    margin-bottom: 10px;
    letter-spacing: 4px;
    font-weight: bold;
    text-transform: uppercase;
    position: relative;
    z-index: 10;
}

.high-score-label {
    font-size: 1.2rem;
    color: var(--score-col);
    margin-bottom: 30px;
    text-shadow: 0 0 10px var(--score-col);
    z-index: 10;
}

.overlay-title {
    font-size: 3rem;
    margin-bottom: 20px;
    text-transform: uppercase;
    letter-spacing: 2px;
}

/* Subtler High Score Style */
.new-high-score {
    font-size: 1.2rem;
    color: #fff;
    letter-spacing: 2px;
    margin-bottom: 5px;
    font-weight: normal;
    animation: pulse-soft 2s infinite alternate;
}

.leaderboard-msg {
    font-size: 1.2rem;
    color: var(--glow);
    margin-bottom: 20px;
    font-weight: bold;
    text-shadow: 0 0 10px var(--glow);
}

@keyframes pulse-soft {
    from { opacity: 0.6; }
    to { opacity: 1.0; text-shadow: 0 0 10px var(--glow); }
}

.footer {
    position: relative; 
    margin-top: 20px;
    text-align: center;
    font-size: 0.8rem;
    color: #666;
    line-height: 1.5;
}

.footer a { 
    color: #888; 
    text-decoration: none; 
    transition: color 0.2s;
}

.footer a:hover {
    color: var(--glow);
    text-shadow: 0 0 5px var(--glow);
}

#leaderboard-content {
    width: 80%;
    max-width: 500px;
    height: 50vh; 
    overflow-y: auto;
    border: 1px solid var(--glow);
    padding: 10px;
    border-radius: 8px;
    background: rgba(0,0,0,0.5);
    margin-bottom: 20px;
    scrollbar-width: thin; 
    scrollbar-color: var(--glow) transparent;
}

.lb-table {
    width: 100%;
    border-collapse: collapse;
    color: #fff;
    font-family: 'Courier New', Courier, monospace;
}

.lb-table th {
    border-bottom: 2px solid var(--glow);
    padding: 10px;
    color: var(--glow);
}

.lb-table td {
    padding: 8px;
    border-bottom: 1px solid #333;
}

.lb-rank { color: var(--highlight); font-weight: bold; }
.lb-score { color: var(--score-col); text-align: right; }

/* Input Box Pulse Animation */
@keyframes input-pulse {
    0% { 
        box-shadow: 0 0 5px var(--glow); 
        border-color: var(--glow); 
        transform: scale(1.0);
    }
    50% { 
        box-shadow: 0 0 25px var(--highlight); 
        border-color: var(--highlight); 
        transform: scale(1.05);
    }
    100% { 
        box-shadow: 0 0 5px var(--glow); 
        border-color: var(--glow); 
        transform: scale(1.0);
    }
}

#name-input {
    background: transparent;
    border: 2px solid var(--glow);
    color: white;
font-family: 'Roboto Mono', 'Courier New', Courier, monospace;
    font-size: 1.5rem;
    padding: 10px;
    text-align: center;
    width: 200px;
    margin-bottom: 20px;
    text-transform: uppercase;
    outline: none;
    border-radius: 4px;
}
#name-input:focus {
    box-shadow: 0 0 15px var(--glow);
}

/* --- Rules Page Styling --- */
#rules-content {
    max-width: 600px;
    width: 90%;
    max-height: 60vh; 
    overflow-y: auto; 
    text-align: left;
    font-size: 1rem;
    line-height: 1.4;
    color: #ddd;
    margin-bottom: 30px; 
    background: rgba(0,0,0,0.5);
    padding: 20px;
    border: 1px solid #333;
    border-radius: 10px;
}
#rules-content h3 { color: var(--glow); margin-top: 0; border-bottom: 1px solid #333; padding-bottom: 5px;}
#rules-content ul { padding-left: 20px; }
#rules-content li { margin-bottom: 8px; }
.rule-highlight { color: var(--highlight); font-weight: bold; }
.rule-bomb { color: var(--bomb-col); font-weight: bold; }
.rule-purple { color: var(--purple-col); font-weight: bold; }
.rule-brake { color: var(--brake-col); font-weight: bold; }
.rule-cryo { color: var(--cryo-col); font-weight: bold; }
.rule-overdrive { color: var(--overdrive-col); font-weight: bold; }

/* --- Buttons & Toggles --- */
.difficulty-container {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}

.diff-btn {
    font-size: 1rem;
    padding: 10px 15px;
    border: 2px solid #555;
    color: #888;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    font-family: 'Courier New', Courier, monospace;
    font-weight: bold;
    text-transform: uppercase;
    transition: all 0.2s;
    min-width: 80px;
}

.diff-btn.selected {
    border-color: var(--glow);
    color: var(--glow);
    box-shadow: 0 0 10px var(--glow);
    background: rgba(0, 255, 255, 0.1);
}

/* --- NEW COLOR CODED BUTTONS --- */
#btn-zen.selected {
    border-color: #44ff44;
    color: #44ff44;
    box-shadow: 0 0 10px #44ff44;
    background: rgba(68, 255, 68, 0.1);
}
#btn-neon.selected {
    border-color: #00ffff;
    color: #00ffff;
    box-shadow: 0 0 10px #00ffff;
    background: rgba(0, 255, 255, 0.1);
}
#btn-locke.selected {
    border-color: #ff3333;
    color: #ff3333;
    box-shadow: 0 0 10px #ff3333;
    background: rgba(255, 51, 51, 0.1);
}

.toggle-btn {
    font-size: 0.9rem;
    padding: 8px 15px;
    margin-bottom: 20px;
    border: 1px solid #555;
    color: #666;
    background: transparent;
    border-radius: 4px;
    cursor: pointer;
    font-family: 'Courier New', Courier, monospace;
    transition: all 0.2s;
}
.toggle-btn.active {
    border-color: var(--warning);
    color: var(--warning);
    box-shadow: 0 0 5px var(--warning);
}

.menu-btn {
    font-size: 1.5rem;
    padding: 15px 40px;
    border: 2px solid var(--glow);
    color: var(--glow);
    background: rgba(0,0,0,0.5);
    box-shadow: 0 0 15px rgba(0, 255, 255, 0.2);
    border-radius: 8px;
    cursor: pointer;
    font-family: 'Courier New', Courier, monospace;
    font-weight: bold;
    text-transform: uppercase;
    transition: all 0.1s;
    margin-bottom: 15px;
    min-width: 220px;
    
    /* NEW: Constraints to prevent sticking out */
    max-width: 90%; 
    box-sizing: border-box; 
    
    position: relative;
    z-index: 10;
}
.menu-btn:active {
    background: var(--glow);
    color: black;
    box-shadow: 0 0 30px var(--glow);
}

.overlay-btn {
    font-size: 1.2rem;
    padding: 12px 30px;
    margin: 10px;
    background: transparent;
    border: 2px solid white;
    color: white;
    border-radius: 6px;
    cursor: pointer;
    font-family: 'Courier New', Courier, monospace;
    font-weight: bold;
    text-transform: uppercase;
    min-width: 200px;
}
.overlay-btn:active {
    background: white;
    color: black;
}

.overlay-btn.secondary {
    border-color: #666;
    color: #aaa;
}
.overlay-btn.secondary:active {
    background: #666;
    color: white;
}

/* --- Top UI --- */
#ui-top {
    flex: 0 0 70px; 
    display: grid;
    /* 120px Left, 1fr Center, 120px Right */
    grid-template-columns: 120px minmax(0, 1fr) 120px; 
    align-items: center;
    background: var(--ui-bg);
    border-bottom: 1px solid #333;
    z-index: 10;
    box-shadow: 0 0 20px rgba(0, 255, 255, 0.1);
    padding: 0 10px;
    gap: 5px;
}

@media (max-width: 600px) {
    /* Slimmer UI for mobile */
    #ui-top {
        grid-template-columns: 80px minmax(0, 1fr) 80px;
        padding: 0 5px;
        flex: 0 0 50px; /* Reduced from 60px */
    }
    .title-text { font-size: 2.5rem; }
    #rules-content { font-size: 0.9rem; }
    
    #ui-bottom {
        min-height: 50px; /* Reduced from 60px */
        padding: 5px 20px; /* Reduced padding */
        padding-bottom: calc(5px + env(safe-area-inset-bottom)); 
    }
}

#stats-container {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    padding-left: 5px;
    overflow: hidden;
}

.stats-sub-row {
    display: flex;
    flex-direction: row;
    gap: 10px; 
    align-items: center;
    margin-top: 2px;
}

#score-display {
    font-size: 1.2rem;
    color: var(--score-col);
    text-shadow: 0 0 5px var(--score-col);
    font-weight: bold;
}

#multiplier-display {
    font-size: 1.0rem; 
    color: #666;
    font-weight: bold;
    margin: 0;
    transition: all 0.2s;
}

#level-display {
    font-size: 0.9rem;
    color: #aaa;
    margin-top: 2px;
}

#hc-indicator {
    font-size: 0.8rem;
    color: var(--warning);
    margin-top: 2px;
    font-weight: bold;
    display: none;
}

#current-word-container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden; 
}

#current-word {
    font-size: 2rem;
    font-weight: bold;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--highlight);
    text-shadow: 0 0 10px var(--highlight), 0 0 20px var(--highlight);
    white-space: nowrap;
    transition: color 0.2s;
    text-align: center;
    line-height: 1;
}

.flame-active {
    animation: burn 0.1s infinite alternate ease-in-out;
}
@keyframes burn {
    0% { text-shadow: 0 0 10px currentColor; transform: scale(1.0); }
    100% { text-shadow: 0 0 20px currentColor, 0 0 30px currentColor; transform: scale(1.02); }
}

#top-controls {
    display: flex;
    justify-content: flex-end;
    gap: 15px;
    align-items: center;
    flex: 0 0 auto;
    width: 90px;
    padding-right: 5px;
}

.icon-btn {
    font-size: 32px; 
    color: #666;
    cursor: pointer;
    transition: color 0.2s;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn svg {
    stroke: currentColor; 
    transition: stroke 0.2s;
}
.icon-btn:hover { color: white; }

/* --- Canvas Area --- */
#game-container {
    flex: 1;
    position: relative;
    width: 100%;
    overflow: hidden;
    background: radial-gradient(circle at center, #1a1a2e 0%, #050510 100%);
    touch-action: none; /* Strict lock: No scrolling/zooming allowed inside the grid area */
}

#gameCanvas {
    display: block;
    width: 100%;
    height: 100%;
    cursor: crosshair;
}

/* --- Bottom UI --- */
#ui-bottom {
    flex: 0 0 auto; 
    display: flex;
    justify-content: center; /* CHANGED from space-between */
    gap: 40px; /* ADDED: Keeps them apart but centered */
    align-items: flex-start;
    background: var(--ui-bg);
    padding: 15px 30px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom)); 
    min-height: 70px;
    border-top: 1px solid #333;
    z-index: 10;
}

.btn {
    background: transparent;
    color: white;
    padding: 12px 0;
    width: 140px; 
    font-size: 1.2rem;
    font-family: 'Courier New', Courier, monospace;
    font-weight: bold;
    text-transform: uppercase;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.1s;
    touch-action: manipulation; /* Disables double-tap zoom */
    position: relative;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* NEW: Disabled State */
.btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
    border-color: #555 !important;
    color: #555 !important;
    box-shadow: none !important;
}

#btn-undo {
    border: 2px solid #ffaa00;
    color: #ffaa00;
    box-shadow: 0 0 10px rgba(255, 170, 0, 0.3);
}
#btn-undo:active {
    background: #ffaa00;
    color: black;
    box-shadow: 0 0 20px #ffaa00;
}

#btn-launch {
    border: 2px solid #44ff44;
    color: #44ff44;
    box-shadow: 0 0 10px rgba(68, 255, 68, 0.3);
}
#btn-launch:active {
    background: #44ff44;
    color: black;
    box-shadow: 0 0 20px #44ff44;
}

.pulse-anim {
    animation: input-pulse 1.5s infinite ease-in-out;
}

/* Tutorial Overlay - Transparent & Top Aligned */
#tutorial-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: transparent; 
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding-top: 85px; 
    z-index: 90;
    pointer-events: none; 
}

/* Updated Tutorial Box */
.tutorial-box {
    background: rgba(5, 5, 10, 0.95);
    border: 1px solid var(--glow);
    padding: 20px;
    border-radius: 0 0 15px 15px;
    width: 90%;
    max-width: 500px;
    
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8);
    pointer-events: auto;
    backdrop-filter: blur(5px);
    transition: opacity 0.3s ease;
}

#tutorial-text {
    width: 100%;
    margin-bottom: 15px;
    display: flex;          
    flex-direction: column;  
    align-items: center;    
    gap: 15px;             
}

.tut-title {
    font-size: 1.4rem;
    font-weight: bold;
    color: var(--glow);
    text-shadow: 0 0 10px var(--glow);
    text-transform: uppercase;
    text-align: center;
}

.tut-body {
    font-size: 1.0rem;
    color: white;
    line-height: 1.5;
    text-align: center;
}

#tutorial-btn {
    padding: 8px 30px;
    font-size: 0.9rem;
    margin-top: 5px !important;
    min-width: 120px;
}

/* Update Dialog - Above Menu */
#update-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.85);
    display: flex;
    justify-content: center;
    align-items: center; 
    z-index: 200; /* Higher than Start Screen (100) */
    backdrop-filter: blur(5px);
}

/* --- Rotation Lock Overlay --- */
#rotate-overlay {
    display: none; /* Hidden by default */
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(5, 5, 16, 0.98); /* Opaque background */
    z-index: 9999; /* Highest priority */
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    backdrop-filter: blur(10px);
}

@keyframes rotate-icon {
    0% { transform: rotate(0deg); opacity: 0.5; }
    50% { transform: rotate(180deg); opacity: 1.0; text-shadow: 0 0 20px var(--glow); }
    100% { transform: rotate(360deg); opacity: 0.5; }
}

/* Only trigger on devices that are in landscape AND have a small vertical height (Phones) */
@media screen and (orientation: landscape) and (max-height: 500px) {
    #rotate-overlay {
        display: flex;
    }
}