body, html {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    overflow: hidden; /* Prevents scrollbars if content overflows */
}

body {
    background-image: url('backgroundbehindcanvas.png');
    background-size: 100% 100%; /* Stretches image to fill entire viewport */
    background-position: center; /* Centers the image (still applies, though less impactful with 100% 100%) */
    background-repeat: no-repeat; /* Prevents tiling */
    background-color: #1a1a1a; /* Fallback color if image fails to load */
}

#gameCanvas {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    border: 2px solid #fff;
    cursor: none; /* Hides default cursor always; custom crosshair handled in JS */
    background: transparent; /* Ensures canvas uses only JS-drawn background */
}

#audioControls {
    position: absolute;
    top: 10px;
    left: calc(50% + 410px);
    background: rgba(0, 0, 0, 0.7);
    padding: 10px;
    border: 2px solid #fff;
    border-radius: 5px;
    color: white;
    font-family: Arial, sans-serif;
    width: 200px;
    box-sizing: border-box;
    text-align: center;
}

#audioControls button {
    display: block;
    margin: 5px auto;
    padding: 5px 10px;
    font-size: 14px;
    cursor: pointer;
}

#ui {
    color: white;
    font-family: Arial, sans-serif;
    position: absolute;
    top: 10px;
    left: calc(50% - 610px); /* Adjusted to place right edge 10px left of canvas */
    background: rgba(0, 0, 0, 0.7); /* Semi-transparent black background */
    padding: 10px; /* Add some padding inside the box */
    border: 2px solid #fff; /* White border for contrast, matching buildable items */
    border-radius: 5px; /* Optional: slight rounding for aesthetics */
    width: 200px; /* Fixed width to contain all UI elements */
    box-sizing: border-box; /* Ensures padding doesn’t increase the size */
}

#mainMenu {
    position: absolute;
    width: 800px; /* Match canvas width */
    height: 600px; /* Match canvas height */
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.8);
    color: white;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    z-index: 10; /* Ensure it’s above canvas */
}

#mainMenu h1 {
    margin: 0 0 20px;
}

/* Pause Overlay Styles */
#pauseOverlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5); /* Matches #endGameOverlay */
    z-index: 9; /* Below pauseMenu but above canvas */
}

/* Updated Pause Menu Styles */
#pauseMenu {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.7); /* Matches #ui and #endGameBox */
    padding: 20px;
    color: white;
    text-align: center;
    border: 2px solid #fff; /* Matches #ui and #endGameBox */
    border-radius: 5px; /* Matches #ui and #endGameBox */
    width: 300px; /* Matches #endGameBox */
    box-sizing: border-box;
    z-index: 10; /* Above overlay, matches #mainMenu */
}

#pauseMenu h2 {
    margin: 0 0 15px;
    font-size: 24px;
    color: #FFFF00; /* Yellow, matches #endGameBox h2 */
}

.pause-buttons {
    display: flex;
    justify-content: space-around;
    gap: 10px;
    margin-top: 10px;
}

.pause-buttons button {
    flex: 1;
    background: linear-gradient(145deg, rgba(255, 255, 255, 0.9), rgba(200, 200, 200, 0.8));
    border: 2px solid #fff;
    border-radius: 8px;
    color: #000;
    padding: 12px 16px;
    font-size: 16px;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.pause-buttons button:hover {
    background: linear-gradient(145deg, rgba(255, 255, 255, 1), rgba(220, 220, 220, 0.9));
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.hidden {
    display: none !important; /* Add !important to ensure it overrides */
}

button {
    padding: 10px 20px;
    margin: 5px;
    font-size: 16px;
    cursor: pointer;
}

#startButton {
    padding: 15px 30px;
    font-size: 20px;
}

/* Buildable Info Styles */
#buildable-info {
    position: absolute;
    top: 620px; /* Below canvas + border + padding */
    left: 50%;
    transform: translateX(-50%);
    width: 720px; /* 4 * 170px + 3 * 10px gap */
    display: flex;
    justify-content: center;
    gap: 10px; /* Space between boxes */
}

.buildable-item {
    width: 170px;
    height: 140px; /* Increased to accommodate larger images */
    background: #000000; /* Default black background */
    border: 2px solid #fff; /* White border for contrast */
    padding: 5px;
    box-sizing: border-box;
    text-align: center;
    font-family: Arial, sans-serif;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow: hidden; /* Prevent text overflow */
}

.buildable-item .item-key {
    color: white; /* Default color for unhighlighted state */
    font-size: 14px;
    margin: -2px 0 2px; /* Negative top margin to move text upward, keep bottom margin */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.buildable-item[style*="background-color: rgba(255, 255, 255, 0.8)"] .item-key {
    color: #000000; /* Black when highlighted for contrast */
}

.buildable-item .item-name {
    color: yellow; /* Default color for unhighlighted state */
    font-size: 16px;
    font-weight: bold;
    margin: 2px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.buildable-item[style*="background-color: rgba(255, 255, 255, 0.8)"] .item-name {
    color: #4169E1; /* Royal Blue when highlighted to signify "unlocked" */
}

.buildable-item .item-cost {
    color: green; /* Default green, stays the same when highlighted */
    font-size: 14px;
    margin: 2px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.buildable-item .buildable-image {
    display: block;
    margin: 5px auto 0; /* Center horizontally, add top margin for spacing */
    width: 64px; /* Fixed width for consistency, matches largest sprite */
    height: 64px; /* Fixed height, larger than original for visibility */
    object-fit: contain; /* Ensures image fits within bounds without distortion */
}

/* Specific adjustment for Electric Fence image in buildable box */
.buildable-item:nth-child(3) .buildable-image {
    width: 82px; /* Set to 82px, between 80px and 96px */
    height: 82px; /* Maintain aspect ratio */
    margin-top: -5px; /* Reduced from 5px to move image down further */
}

/* End Game Overlay and Box Styles */
#endGameOverlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5); /* Semi-transparent black tint */
    z-index: 14; /* Below endGameBox but above everything else */
}

#endGameBox {
    position: absolute;
    top: 40%; /* Moved up from 50% to fit within canvas */
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.7); /* Matches #ui */
    border: 2px solid #fff; /* Matches #ui and .buildable-item */
    border-radius: 5px; /* Matches #ui */
    padding: 20px;
    width: 300px; /* Wider than #ui for more space */
    box-sizing: border-box;
    color: white;
    font-family: Arial, sans-serif;
    text-align: center;
    z-index: 15; /* Above overlay */
}

#endGameBox h2 {
    margin: 0 0 15px;
    font-size: 24px;
    color: #FFFF00; /* Yellow to stand out, like wave countdown */
}

#endGameStats p {
    margin: 5px 0;
    font-size: 16px;
    text-align: left; /* Align stats neatly */
}

#endGameStats span {
    color: #1DA1F2; /* Twitter/X blue for emphasis, matches watermark link */
    font-weight: bold;
}

#endGameOK {
    background: rgba(255, 255, 255, 0.8); /* Matches highlighted buildable item */
    border: 2px solid #fff; /* Matches other borders */
    border-radius: 5px; /* Matches #ui */
    color: #000; /* Black text for contrast */
    padding: 10px 20px;
    margin-top: 15px;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.3s; /* Smooth hover effect */
}

#endGameOK:hover {
    background: rgba(255, 255, 255, 1); /* Brighter on hover */
}

/* Watermark Styles */
#watermark {
    position: absolute;
    bottom: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.7); /* Semi-transparent black background */
    padding: 15px; /* Scaled from 10px to 15px (1.5x) */
    border: 2px solid #fff; /* White border for contrast */
    border-radius: 5px; /* Slight rounding for aesthetics */
    display: flex;
    align-items: center;
    z-index: 5; /* Above canvas but below menus */
}

#watermark-logo {
    width: 48px; /* Scaled from 32px to 48px (1.5x) */
    height: 48px; /* Scaled from 32px to 48px (1.5x) */
    margin-right: 15px; /* Scaled from 10px to 15px (1.5x) */
}

#watermark p {
    margin: 0;
    color: white;
    font-family: Arial, sans-serif;
    font-size: 21px; /* Scaled from 14px to 21px (1.5x) */
}

#watermark a {
    color: #1DA1F2; /* Twitter/X blue */
    text-decoration: none; /* Remove underline */
}

#watermark a:hover {
    text-decoration: underline; /* Underline on hover for usability */
}

/* Support Watermark Styles */
#support-watermark {
    position: absolute;
    bottom: 10px;
    left: 10px; /* Changed from right to left */
    background: rgba(0, 0, 0, 0.7); /* Matches #watermark */
    padding: 15px; /* Matches #watermark */
    border: 2px solid #fff; /* Matches #watermark */
    border-radius: 5px; /* Matches #watermark */
    display: flex;
    align-items: flex;
    align-items: center;
    z-index: 5; /* Matches #watermark */
}

#support-watermark-logo {
    width: 48px; /* Matches #watermark-logo */
    height: 48px; /* Matches #watermark-logo */
    margin-right: 15px; /* Matches #watermark-logo */
}

#support-watermark p {
    margin: 0;
    color: white;
    font-family: Arial, sans-serif;
    font-size: 21px; /* Matches #watermark p */
}

#support-watermark a {
    color: #1DA1F2; /* Matches #watermark a (Twitter/X blue) */
    text-decoration: none; /* Matches #watermark a */
}

#support-watermark a:hover {
    text-decoration: underline; /* Matches #watermark a:hover */
}

/* Quit Confirmation Overlay */
#quitConfirmationOverlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5); /* Matches #pauseOverlay and #endGameOverlay */
    z-index: 11; /* Above #pauseMenu (z-index: 10) but below #quitConfirmation */
}

/* Quit Confirmation Dialog */
#quitConfirmation {
    position: absolute;
    top: 45%; /* Adjusted to align bottom with pause menu and stay within canvas */
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.7); /* Matches #pauseMenu and #endGameBox */
    padding: 20px;
    color: white;
    text-align: center;
    border: 2px solid #fff; /* Matches #pauseMenu and #endGameBox */
    border-radius: 5px; /* Matches #pauseMenu and #endGameBox */
    width: 300px; /* Matches #pauseMenu and #endGameBox */
    box-sizing: border-box;
    z-index: 12; /* Above #quitConfirmationOverlay */
}

#quitConfirmation h2 {
    margin: 0 0 15px;
    font-size: 24px;
    color: #FFFF00; /* Yellow, matches #pauseMenu h2 and #endGameBox h2 */
}

#quitConfirmation p {
    margin: 10px 0;
    font-size: 16px;
}

#quitConfirmation button {
    background: rgba(255, 255, 255, 0.8); /* Matches #pauseMenu button and #endGameOK */
    border: 2px solid #fff; /* Matches #pauseMenu button and #endGameOK */
    border-radius: 5px; /* Matches #pauseMenu button and #endGameOK */
    color: #000; /* Matches #pauseMenu button and #endGameOK */
    padding: 10px 20px;
    margin: 5px;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.3s; /* Matches #pauseMenu button and #endGameOK */
}

#quitConfirmation button:hover {
    background: rgba(255, 255, 255, 1); /* Matches #pauseMenu button:hover and #endGameOK:hover */
}

/* Help Overlay */
#helpOverlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5); /* Matches #pauseOverlay and #endGameOverlay */
    z-index: 11; /* Above #pauseMenu (z-index: 10) but below #helpDialog */
}

/* Help Dialog */
#helpDialog {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.7); /* Matches #pauseMenu and #endGameBox */
    padding: 20px;
    color: white;
    text-align: left;
    border: 2px solid #fff; /* Matches #pauseMenu and #endGameBox */
    border-radius: 5px; /* Matches #pauseMenu and #endGameBox */
    width: 500px; /* Wider for content */
    max-height: 80vh;
    overflow-y: auto;
    box-sizing: border-box;
    z-index: 12; /* Above #helpOverlay */
}

#helpDialog h2 {
    margin: 0 0 15px;
    font-size: 24px;
    color: #FFFF00; /* Yellow, matches #pauseMenu h2 and #endGameBox h2 */
    text-align: center;
}

#helpDialog h3 {
    margin: 15px 0 5px;
    font-size: 18px;
    color: #FFFF00;
}

#helpDialog ul {
    margin: 5px 0 15px;
    padding-left: 20px;
}

#helpDialog li {
    margin: 5px 0;
    font-size: 14px;
}

#helpDialog p {
    margin: 10px 0;
    font-size: 16px;
    text-align: center;
}

#helpDialog button {
    background: rgba(255, 255, 255, 0.8); /* Matches #pauseMenu button and #endGameOK */
    border: 2px solid #fff; /* Matches #pauseMenu button and #endGameOK */
    border-radius: 5px; /* Matches #pauseMenu button and #endGameOK */
    color: #000; /* Matches #pauseMenu button and #endGameOK */
    padding: 10px 20px;
    margin: 10px auto 0;
    font-size: 16px;
    cursor: pointer;
    display: block;
    transition: background 0.3s; /* Matches #pauseMenu button and #endGameOK */
}

#helpDialog button:hover {
    background: rgba(255, 255, 255, 1); /* Matches #pauseMenu button:hover and #endGameOK:hover */
}