:root {
    --bg-color: #050806;
    --matrix-green: #00ff66;
    --matrix-green-glow: rgba(0, 255, 102, 0.4);
    --matrix-dim: #006622;
    --cyber-cyan: #00f0ff;
    --cyber-cyan-glow: rgba(0, 240, 255, 0.4);
    --glitch-red: #ff0055;
    --glitch-red-glow: rgba(255, 0, 85, 0.4);
    --panel-bg: rgba(5, 12, 8, 0.8);
    --panel-border: rgba(0, 255, 102, 0.2);
    --terminal-font: 'Share Tech Mono', monospace;
    --header-font: 'Orbitron', sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
    outline: none;
}

body {
    background-color: var(--bg-color);
    color: #e0eee0;
    font-family: var(--terminal-font);
    overflow: hidden;
    width: 100vw;
    height: 100vh;
}

#game-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* CRT Scanline & Retro Filters */
.crt-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        rgba(18, 16, 16, 0) 50%, 
        rgba(0, 0, 0, 0.25) 50%
    );
    background-size: 100% 4px;
    z-index: 99;
    pointer-events: none;
}

.vignette-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(
        circle,
        rgba(0, 0, 0, 0) 50%,
        rgba(0, 0, 0, 0.75) 100%
    );
    z-index: 98;
    pointer-events: none;
}

/* Glitch Flash Red effect on Crash */
.glitch-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 0, 85, 0.3);
    z-index: 97;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s ease-out;
}

.glitch-active {
    animation: flash-glitch 0.4s ease-out;
}

@keyframes flash-glitch {
    0% { opacity: 0; }
    10% { opacity: 1; }
    20% { opacity: 0.2; }
    30% { opacity: 0.8; }
    40% { opacity: 0; }
    100% { opacity: 0; }
}

/* Canvas background */
#gameCanvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    display: block;
}

/* UI Panel General */
.ui-panel {
    position: relative;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 10;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.6);
}

/* HUD Header Styling */
#hud {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 96%;
    max-width: 1200px;
    margin: 15px auto 0;
    padding: 10px 20px;
    border-radius: 6px;
    border-bottom: 2px solid var(--matrix-green);
    animation: border-glow 4s infinite alternate;
}

@keyframes border-glow {
    0% { border-color: rgba(0, 255, 102, 0.2); }
    100% { border-color: rgba(0, 255, 102, 0.6); }
}

.hud-left {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.stat-group {
    display: flex;
    align-items: center;
    font-size: 14px;
}

.label {
    opacity: 0.6;
    letter-spacing: 1px;
    margin-right: 8px;
}

.value {
    font-family: var(--header-font);
    font-weight: 700;
    letter-spacing: 1px;
}

.text-green {
    color: var(--matrix-green);
    text-shadow: 0 0 8px var(--matrix-green-glow);
}

.text-cyan {
    color: var(--cyber-cyan);
    text-shadow: 0 0 8px var(--cyber-cyan-glow);
}

.text-red {
    color: var(--glitch-red);
    text-shadow: 0 0 8px var(--glitch-red-glow);
}

.hud-center {
    display: flex;
    flex-direction: column;
    align-items: center;
}

#safe-key-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: rgba(0, 240, 255, 0.05);
    border: 1px solid rgba(0, 240, 255, 0.2);
    padding: 4px 15px;
    border-radius: 4px;
}

#safe-key-container .label {
    font-size: 10px;
    margin-right: 0;
    color: var(--cyber-cyan);
    font-weight: bold;
}

#safe-key-display {
    font-family: var(--header-font);
    font-size: 26px;
    font-weight: 900;
    color: var(--cyber-cyan);
    text-shadow: 0 0 12px var(--cyber-cyan-glow);
    animation: key-pulse 2s infinite alternate;
}

@keyframes key-pulse {
    0% { transform: scale(0.95); opacity: 0.8; }
    100% { transform: scale(1.08); opacity: 1; text-shadow: 0 0 18px var(--cyber-cyan); }
}

.hud-right {
    display: flex;
    align-items: center;
}

.icon-btn {
    background: transparent;
    border: 1px solid rgba(0, 255, 102, 0.3);
    color: var(--matrix-green);
    padding: 6px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.icon-btn:hover {
    background: rgba(0, 255, 102, 0.1);
    border-color: var(--matrix-green);
    color: #fff;
    box-shadow: 0 0 8px var(--matrix-green-glow);
}

.hidden {
    display: none !important;
}

/* Stability Progress Bar */
#stability-wrapper {
    position: relative;
    width: 96%;
    max-width: 1200px;
    margin: 8px auto 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 10px;
}

#stability-label {
    font-size: 11px;
    opacity: 0.6;
    letter-spacing: 1px;
}

.progress-bar-bg {
    flex-grow: 1;
    height: 8px;
    background: rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(0, 255, 102, 0.2);
    border-radius: 4px;
    overflow: hidden;
}

#stability-bar {
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, var(--matrix-green), var(--cyber-cyan));
    box-shadow: 0 0 10px var(--matrix-green-glow);
    transition: width 0.1s linear, background-color 0.3s ease, box-shadow 0.3s ease;
}

#stability-bar.warning {
    background: linear-gradient(90deg, #ffaa00, #ff5500);
    box-shadow: 0 0 10px rgba(255, 170, 0, 0.5);
}

#stability-bar.danger {
    background: linear-gradient(90deg, #ff3300, var(--glitch-red));
    box-shadow: 0 0 10px var(--glitch-red-glow);
    animation: bar-danger-pulse 0.5s infinite alternate;
}

@keyframes bar-danger-pulse {
    0% { opacity: 0.6; }
    100% { opacity: 1; }
}

/* Fullscreen Overlays (Start / Game Over) */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(5, 8, 6, 0.92);
    z-index: 50;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    padding: 20px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.4s ease;
}

.overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.terminal-decor {
    position: absolute;
    font-size: 12px;
    opacity: 0.3;
    font-family: var(--terminal-font);
    pointer-events: none;
}

.top-left { top: 20px; left: 20px; }
.bottom-right { bottom: 20px; right: 20px; }

/* Glitch Title Styling */
.glitch-title {
    font-family: var(--header-font);
    font-size: 3.5rem;
    font-weight: 900;
    letter-spacing: 4px;
    color: #fff;
    position: relative;
    margin-bottom: 5px;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
}

.glitch-title::before,
.glitch-title::after {
    content: attr(data-text);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: transparent;
}

/* RGB Split Glitch */
.glitch-title::before {
    left: 2px;
    text-shadow: -2px 0 var(--cyber-cyan);
    clip: rect(44px, 450px, 56px, 0);
    animation: glitch-anim 5s infinite linear alternate-reverse;
}

.glitch-title::after {
    left: -2px;
    text-shadow: -2px 0 var(--glitch-red), 0 0 10px rgba(255,0,85,0.4);
    clip: rect(85px, 450px, 140px, 0);
    animation: glitch-anim2 5s infinite linear alternate-reverse;
}

.red-glitch {
    color: var(--glitch-red);
    text-shadow: 0 0 15px rgba(255, 0, 85, 0.5);
}

@keyframes glitch-anim {
    0% { clip: rect(15px, 9999px, 66px, 0); transform: skew(0.5deg); }
    10% { clip: rect(30px, 9999px, 105px, 0); transform: skew(-0.5deg); }
    20% { clip: rect(80px, 9999px, 5px, 0); transform: skew(0.8deg); }
    30% { clip: rect(10px, 9999px, 75px, 0); transform: skew(-1deg); }
    40% { clip: rect(55px, 9999px, 95px, 0); transform: skew(0.2deg); }
    50% { clip: rect(120px, 9999px, 20px, 0); transform: skew(-0.8deg); }
    60% { clip: rect(45px, 9999px, 140px, 0); transform: skew(0.6deg); }
    70% { clip: rect(95px, 9999px, 50px, 0); transform: skew(-0.2deg); }
    80% { clip: rect(5px, 9999px, 125px, 0); transform: skew(1deg); }
    90% { clip: rect(130px, 9999px, 85px, 0); transform: skew(-0.6deg); }
    100% { clip: rect(25px, 9999px, 110px, 0); transform: skew(0.4deg); }
}

@keyframes glitch-anim2 {
    0% { clip: rect(95px, 9999px, 120px, 0); transform: skew(-0.5deg); }
    10% { clip: rect(40px, 9999px, 10px, 0); transform: skew(0.5deg); }
    20% { clip: rect(130px, 9999px, 95px, 0); transform: skew(-0.8deg); }
    30% { clip: rect(5px, 9999px, 85px, 0); transform: skew(1deg); }
    40% { clip: rect(110px, 9999px, 45px, 0); transform: skew(-0.2deg); }
    50% { clip: rect(20px, 9999px, 130px, 0); transform: skew(0.8deg); }
    60% { clip: rect(75px, 9999px, 15px, 0); transform: skew(-0.6deg); }
    70% { clip: rect(15px, 9999px, 95px, 0); transform: skew(0.2deg); }
    80% { clip: rect(140px, 9999px, 55px, 0); transform: skew(-1deg); }
    90% { clip: rect(50px, 9999px, 125px, 0); transform: skew(0.6deg); }
    100% { clip: rect(85px, 9999px, 35px, 0); transform: skew(-0.4deg); }
}

.subtitle {
    font-size: 14px;
    letter-spacing: 4px;
    margin-bottom: 30px;
    opacity: 0.8;
}

/* Instructions card */
.instructions-card {
    background: rgba(0, 255, 102, 0.03);
    border: 1px solid rgba(0, 255, 102, 0.15);
    border-radius: 8px;
    padding: 20px;
    max-width: 500px;
    width: 90%;
    margin-bottom: 25px;
    text-align: left;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.instruction-row {
    display: flex;
    align-items: center;
    gap: 15px;
    font-size: 14px;
    line-height: 1.5;
}

.instruction-row .icon {
    font-size: 22px;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 30px;
}

.instruction-row strong {
    font-weight: bold;
}

/* Controls Guide */
.controls-guide {
    display: flex;
    gap: 20px;
    width: 90%;
    max-width: 500px;
    margin-bottom: 35px;
    justify-content: space-between;
}

.guide-item {
    flex: 1;
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 6px;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.guide-item .device {
    font-size: 10px;
    letter-spacing: 1px;
    opacity: 0.5;
    font-weight: bold;
}

.guide-item .keys {
    font-size: 12px;
    color: #fff;
}

/* Action button styles */
.action-btn {
    font-family: var(--header-font);
    background: transparent;
    border: 2px solid var(--matrix-green);
    color: var(--matrix-green);
    padding: 14px 36px;
    font-size: 18px;
    font-weight: 700;
    letter-spacing: 2px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.25s ease;
    box-shadow: 0 0 10px var(--matrix-green-glow);
    margin-bottom: 12px;
}

.action-btn:hover {
    background: var(--matrix-green);
    color: var(--bg-color);
    box-shadow: 0 0 20px var(--matrix-green);
    text-shadow: none;
}

.restart-action {
    border-color: var(--glitch-red);
    color: var(--glitch-red);
    box-shadow: 0 0 10px var(--glitch-red-glow);
}

.restart-action:hover {
    background: var(--glitch-red);
    color: #fff;
    box-shadow: 0 0 20px var(--glitch-red);
}

.text-pulse {
    animation: text-pulse-animation 1.5s infinite alternate;
}

@keyframes text-pulse-animation {
    0% { transform: scale(1); }
    100% { transform: scale(1.03); }
}

.restart-note {
    font-size: 11px;
    opacity: 0.4;
    letter-spacing: 1px;
}

/* Game Over Screen items */
.error-code {
    font-size: 12px;
    color: var(--glitch-red);
    letter-spacing: 2px;
    margin-bottom: 30px;
    background: rgba(255, 0, 85, 0.05);
    border: 1px solid rgba(255, 0, 85, 0.15);
    padding: 4px 15px;
    border-radius: 4px;
}

.results-container {
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 6px;
    padding: 15px 25px;
    width: 90%;
    max-width: 400px;
    margin-bottom: 35px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.result-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
}

/* Mobile Controls Styling (Overlay buttons) */
#mobile-controls {
    display: none; /* Hidden on desktop, shown on mobile touch media query */
    position: absolute;
    bottom: 25px;
    left: 0;
    width: 100%;
    z-index: 40;
    justify-content: space-between;
    padding: 0 30px;
    pointer-events: none; /* Allows canvas interactions except on buttons */
}

.mobile-btn {
    pointer-events: auto; /* Active for touch clicks */
    background: rgba(5, 12, 8, 0.5);
    border: 1px solid rgba(0, 255, 102, 0.3);
    color: var(--matrix-green);
    width: 70px;
    height: 70px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
    transition: background 0.1s, border-color 0.1s, transform 0.1s;
}

.mobile-btn:active {
    background: rgba(0, 255, 102, 0.2);
    border-color: var(--matrix-green);
    transform: scale(0.92);
    box-shadow: 0 0 15px var(--matrix-green);
}

/* Responsiveness Media Queries */
@media (max-width: 768px) {
    .glitch-title {
        font-size: 2.3rem;
        letter-spacing: 2px;
    }
    
    #hud {
        padding: 8px 12px;
    }
    
    #safe-key-display {
        font-size: 22px;
    }
    
    .instructions-card {
        padding: 15px;
    }
    
    .instruction-row {
        font-size: 12px;
    }
    
    .controls-guide {
        flex-direction: column;
        gap: 8px;
        margin-bottom: 25px;
    }
    
    /* Show mobile virtual buttons on portrait screens */
    #mobile-controls {
        display: flex;
    }
}

/* Utility classes */
strong {
    font-weight: 700;
}
