:root {
    /* Syrian National Identity Color Palette - Optimized Usage */
    --forest-primary: #42B177;    /* Main green - primary actions */
    --forest-secondary: #054239;  /* Dark green - secondary actions */
    --forest-dark: #002623;       /* Darkest green - borders */
    
    --golden-wheat-primary: #edebe0;   /* Light cream - main text */
    --golden-wheat-secondary: #b9a779; /* Golden brown - secondary text */
    --golden-wheat-dark: #988561;      /* Dark golden - muted text */
    
    --deep-umber-primary: #6B2F2A;     /* Red-brown - highlights */
    --deep-umber-secondary: #4A1F1E;   /* Dark red-brown - surfaces */
    --deep-umber-dark: #250F14;        /* Darkest red-brown - backgrounds */
    
    --charcoal-primary: #ffffff;       /* White - game background */
    --charcoal-secondary: #3d3a3b;     /* Dark gray - borders */
    --charcoal-dark: #161616;          /* Black - shadows */

    /* Optimized color assignments using ALL palette colors */
    --game-bg: var(--charcoal-primary);        /* White game background */
    --main-bg: var(--deep-umber-dark);         /* Deep umber for main bg */
    --menu-bg: var(--deep-umber-secondary);    /* Deep umber secondary for menus */
    --surface-bg: var(--deep-umber-primary);   /* Deep umber primary for cards */
    --primary-action: var(--forest-primary);   /* Green for primary buttons */
    --secondary-action: var(--forest-secondary); /* Dark green for secondary */
    --accent-color: var(--golden-wheat-secondary); /* Golden for accents */
    --text-primary: var(--golden-wheat-primary);   /* Light text */
    --text-secondary: var(--golden-wheat-dark);    /* Muted text */
    --border-color: var(--forest-dark);        /* Dark green borders */
    --shadow-color: var(--charcoal-dark);      /* Black shadows */
}

/* Night Mode - Swap some colors */
body.night-mode {
    --main-bg: var(--charcoal-dark);
    --menu-bg: var(--forest-dark);
    --surface-bg: var(--forest-secondary);
    --accent-color: var(--golden-wheat-primary);
    --border-color: var(--deep-umber-primary);
}

/* Reset and Base Styles */
html, body {
    margin: 0;
    padding: 0;
    overflow: hidden;
    background: var(--main-bg);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    height: 100%;
    touch-action: manipulation;
    transition: background 0.3s ease;
}

* {
    box-sizing: border-box;
}

/* Base display properties for game screens */
.game-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none; /* Hidden by default, controlled by JS */
}

/* Loader Screen */
#loader.loader-screen {
    background: var(--main-bg);
    display: flex; /* Default flex, but can be overridden by JS */
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: opacity 0.5s ease;
}

#loader.loader-screen[style*="display: none"] {
    display: none !important;
}

.character-enter {
    position: absolute; /* Keep absolute for animation */
    top: -100px;
    animation: enterFromTop 1s ease-out forwards;
}

@keyframes enterFromTop {
    from {
        top: -100px;
        opacity: 0;
    }
    to {
        top: 50%;
        transform: translateY(-50%);
        opacity: 1;
    }
}

.loader-character-image {
    width: 80px;
    height: 112px;
    background-image: url('https://cdn.jsdelivr.net/gh/mazenovi/tarboush-assets/character_run.png');
    background-size: cover;
    image-rendering: pixelated;
}

/* Game Container - This holds the canvas and all game UI elements */
.game-container {
    position: absolute; /* Position it like other game screens */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex; /* Always flex when active for centering canvas */
    flex-direction: column; /* Allows content to stack if needed */
    justify-content: center;
    align-items: center;
    overflow: hidden;
    background: var(--main-bg);
    padding: 15px;
    /* transition: background 0.3s ease; */ /* Removed here to avoid conflict with JS background swap */
}

/* UI Container (Score, etc.) - Make sure it overlays correctly */
#ui-container {
    position: absolute; /* Essential for overlaying on canvas */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none; /* Allows clicks to pass through to canvas/buttons */
    z-index: 50; /* Above canvas, below modals */
}

/* Character Select Screen */
#characterSelectScreen {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: var(--main-bg);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 20px;
}

.menu-content {
    max-width: 90%;
    width: 100%;
    max-width: 350px;
    text-align: center;
    background: var(--menu-bg);
    padding: 25px;
    border-radius: 12px;
    border: 2px solid var(--border-color);
    box-shadow: 0 8px 24px var(--shadow-color);
}

#characterSelectScreen h1 {
    font-size: clamp(1.8rem, 6vw, 2.2rem);
    margin-bottom: 10px;
    color: var(--primary-action);
    font-weight: 700;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
    font-family: 'Georgia', serif;
}

#characterSelectScreen h2 {
    font-size: clamp(1rem, 3.5vw, 1.2rem);
    margin-bottom: 20px;
    color: var(--text-primary);
    font-weight: 400;
}

.character-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    margin-bottom: 20px;
    width: 100%;
}

.character-slot {
    background: var(--surface-bg);
    border: 2px solid var(--border-color);
    border-radius: 8px;
    padding: 12px 15px;
    display: flex;
    align-items: center;
    width: 100%;
    color: var(--text-primary);
    font-weight: 500;
    font-size: 0.95rem;
    transition: all 0.3s ease;
    text-align: left;
    cursor: default;
}

.character-slot.available {
    background: var(--secondary-action);
    cursor: pointer;
    border-color: var(--primary-action);
}

.character-slot.available:hover,
.character-slot.selected {
    transform: translateY(-2px);
    background: var(--primary-action);
    border-color: var(--accent-color);
    color: var(--charcoal-primary);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.character-slot.grayed-out {
    background: var(--charcoal-secondary);
    opacity: 0.6;
    border-color: var(--charcoal-secondary);
    color: var(--text-secondary);
    position: relative;
}

.character-slot.grayed-out::after {
    content: '✗';
    position: absolute;
    top: 8px;
    right: 12px;
    color: var(--accent-color);
    font-size: 1.2rem;
    font-weight: bold;
}

.character-slot img {
    width: 40px;
    height: 40px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    margin-right: 12px;
    flex-shrink: 0;
}

.character-slot.grayed-out img {
    filter: grayscale(100%);
    opacity: 0.5;
}

.character-slot p {
    margin: 0;
    flex-grow: 1;
    font-size: 0.9rem;
}

#start-game-btn {
    padding: 12px 24px;
    font-size: 1.1rem;
    background: var(--primary-action);
    color: var(--charcoal-primary);
    border: 2px solid var(--primary-action);
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.3s ease;
    font-family: inherit;
    width: 100%;
    max-width: 200px;
}

#start-game-btn:hover {
    background: var(--accent-color);
    border-color: var(--accent-color);
    color: var(--deep-umber-dark);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

/* Ensure consistent active state for all buttons */
button:active {
    transform: scale(0.95) translateY(1px) !important; /* !important to override hover */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4) !important;
}

.coming-soon-text {
    font-size: 0.85rem;
    margin-top: 15px;
    color: var(--text-secondary);
    opacity: 0.8;
    font-weight: 400;
}

/* Game Over Screen */
#gameOver {
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    z-index: 200;
    background: rgba(0, 0, 0, 0.8);
}

#gameOver-content {
    background: var(--menu-bg);
    color: var(--text-primary);
    padding: 25px;
    border-radius: 12px;
    width: 90%;
    max-width: 300px;
    text-align: center;
    border: 2px solid var(--primary-action);
    box-shadow: 0 8px 24px var(--shadow-color);
}

#gameOver h2 {
    margin: 0 0 15px 0;
    color: var(--primary-action);
    font-size: 1.6rem;
    font-weight: 600;
    font-family: 'Georgia', serif;
}

#gameOver p {
    margin: 8px 0;
    font-size: 1rem;
    color: var(--text-primary);
}

#finalScore, #bestScore {
    color: var(--accent-color);
    font-weight: bold;
}

#restartBtn {
    padding: 10px 20px;
    font-size: 1rem;
    background: var(--primary-action);
    color: var(--charcoal-primary);
    border: 2px solid var(--primary-action);
    border-radius: 8px;
    cursor: pointer;
    margin-top: 15px;
    font-weight: 600;
    transition: all 0.3s ease;
    width: 100%;
}

#restartBtn:hover {
    background: var(--accent-color);
    border-color: var(--accent-color);
    color: var(--deep-umber-dark);
    transform: translateY(-2px);
}

/* UI Elements */
#score {
    position: absolute;
    top: 15px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--menu-bg);
    color: var(--text-primary);
    padding: 8px 16px;
    font-weight: 600;
    border-radius: 8px;
    font-size: 0.95rem;
    border: 2px solid var(--primary-action);
    font-family: 'Georgia', serif;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

/* Action Buttons - Much smaller and better positioned */
.action-button {
    position: absolute;
    bottom: 20px;
    width: 50px;
    height: 50px;
    color: var(--charcoal-primary);
    background: var(--primary-action);
    border: 2px solid var(--primary-action);
    border-radius: 10px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    z-index: 100;
    -webkit-tap-highlight-color: transparent;
    transition: all 0.3s ease;
    font-family: inherit;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3);
}

.action-button:hover {
    background: var(--accent-color);
    border-color: var(--accent-color);
    color: var(--deep-umber-dark);
    transform: translateY(-2px);
    box-shadow: 0 5px 12px rgba(0, 0, 0, 0.4);
}

#jumpButton {
    right: 15px;
}

#duckButton {
    left: 15px;
}

/* Canvas Styling */
canvas {
    display: block;
    border: 3px solid var(--border-color);
    border-radius: 10px;
    background: var(--game-bg) !important;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
    z-index: 10; /* Ensure canvas is behind UI, but visible */
}

/* Orientation Warning */
#orientation-warning {
    background: var(--main-bg);
    color: var(--text-primary);
    z-index: 200;
    justify-content: center;
    align-items: center;
    text-align: center;
    font-size: 16px;
    flex-direction: column;
}

.orientation-content {
    padding: 25px;
    background: var(--menu-bg);
    border-radius: 12px;
    border: 2px solid var(--primary-action);
    max-width: 90%;
    box-shadow: 0 8px 24px var(--shadow-color);
}

.rotate-icon {
    font-size: 2.5rem;
    margin-bottom: 10px;
    color: var(--primary-action);
}

/* Debugger */
#debuggerDisplay {
    display: none;
    position: absolute;
    bottom: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.9);
    color: var(--primary-action);
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 11px;
    padding: 6px 10px;
    border-radius: 6px;
    z-index: 9998;
    max-width: 75%;
    border: 1px solid var(--primary-action);
}

/* Animations */
@keyframes bounce {
    0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
    40% { transform: translateY(-6px); }
    60% { transform: translateY(-3px); }
}

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

.character-slot.selected {
    animation: pulse 2s infinite;
}

#start-game-btn:hover {
    animation: bounce 0.6s;
}

/* Responsive Design */
@media (max-height: 500px) {
    .menu-content {
        padding: 15px;
        max-width: 280px;
    }
    
    #characterSelectScreen h1 {
        font-size: 1.5rem;
        margin-bottom: 8px;
    }
    
    #characterSelectScreen h2 {
        font-size: 0.95rem;
        margin-bottom: 12px;
    }
    
    .character-grid {
        gap: 6px;
        margin-bottom: 12px;
    }
    
    .character-slot {
        padding: 8px 10px;
        font-size: 0.85rem;
    }
    
    .character-slot img {
        width: 35px;
        height: 35px;
    }
    
    #start-game-btn {
        padding: 8px 16px;
        font-size: 0.95rem;
    }
    
    .coming-soon-text {
        font-size: 0.8rem;
        margin-top: 8px;
    }
}

@media (max-width: 400px) {
    .action-button {
        width: 45px;
        height: 45px;
        font-size: 12px;
    }
    
    #score {
        font-size: 0.85rem;
        padding: 6px 12px;
    }
    
    #gameOver-content {
        width: 95%;
        padding: 20px;
    }
    
    #gameOver h2 {
        font-size: 1.4rem;
    }
    
    .game-container {
        padding: 10px;
    }
    
    .menu-content {
        padding: 15px;
    }
}
