:root {
    --bg-color: #0f0f13;
    --ui-bg: rgba(20, 20, 25, 0.8);
    --ui-border: rgba(255, 255, 255, 0.1);
    --text-color: #e0e0e0;
    --accent-color: #3b82f6;
    --accent-hover: #2563eb;
}

* {
    box-sizing: border-box;
    user-select: none;
    -webkit-user-select: none;
}

body,
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: var(--bg-color);
    font-family: 'Inter', sans-serif;
    font-family: 'Inter', sans-serif;
    color: var(--text-color);
    overscroll-behavior: none;
    /* Prevent pull-to-refresh */
    touch-action: none;
    /* Disable browser handling of gestures */
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: pixelated;
    /* Crucial for sharp pixels */
    touch-action: none;
    /* Explicitly disable touch actions on canvas */
}

#ui-container {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 95%;
    max-width: 1000px;
    pointer-events: none;
    /* Let clicks pass through to canvas where not on UI */
}

.toolbar {
    background: rgba(20, 20, 25, 0.3);
    backdrop-filter: blur(4px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    padding: 12px 20px;
    display: flex;
    gap: 16px;
    /* Reduced gap */
    align-items: center;
    justify-content: flex-start;
    /* Changed from space-between to avoid spreading too much */
    pointer-events: auto;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    max-width: 100%;
    flex-wrap: nowrap;
    /* Prevent wrapping */
}

/* Ensure element buttons take remaining space */
.tool-group:nth-child(3) {
    /* Elements group is now 3rd (Category, Search, Elements) */
    flex-grow: 1;
    min-width: 0;
    /* Allow shrinking if absolutely necessary, but flex-grow pushes it */
    overflow: hidden;
}

/* Search group */
.tool-group:nth-child(2) {
    flex-shrink: 0;
    /* Don't shrink search */
    min-width: 200px;
}

.tool-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
    /* Allow shrinking for flex items */
}

.tool-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #888;
    font-weight: 600;
    white-space: nowrap;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#element-search {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    color: white;
    padding: 8px 12px;
    font-family: 'Inter', sans-serif;
    font-size: 14px;
    outline: none;
    width: 200px;
    align-self: center;
    transition: border-color 0.2s;
}

#element-search:focus {
    border-color: rgba(255, 255, 255, 0.5);
}

#element-selector {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    padding-bottom: 10px;
    width: 100%;
    /* Hide scrollbar but keep functionality */
    scrollbar-width: none;
    /* Firefox */
    -ms-overflow-style: none;
    /* IE and Edge */
}

.element-buttons {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    touch-action: pan-x;
    /* Explicitly allow horizontal panning */
    padding-bottom: 4px;
    /* Space for scrollbar */
    width: 100%;
    /* Take full available width */
    scrollbar-width: thin;
    scrollbar-color: var(--accent-color) transparent;
}

.element-buttons::-webkit-scrollbar {
    height: 4px;
}

.element-buttons::-webkit-scrollbar-track {
    background: transparent;
}

.element-buttons::-webkit-scrollbar-thumb {
    background-color: var(--accent-color);
    border-radius: 2px;
}

.element-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 8px 12px;
    color: var(--text-color);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s ease;
    font-weight: 500;
    white-space: nowrap;
    /* Prevent text wrapping */
    flex-shrink: 0;
    /* Prevent buttons from shrinking */
}

.element-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.element-btn.active {
    background: var(--accent-color);
    color: white;
    box-shadow: 0 0 10px rgba(59, 130, 246, 0.4);
}

.scroll-indicator {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 40px;
    background: linear-gradient(to right, transparent, var(--ui-bg));
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    color: var(--accent-color);
    opacity: 0;
    transition: opacity 0.3s;
    border-top-right-radius: 8px;
    border-bottom-right-radius: 8px;
}

.scroll-indicator.visible {
    opacity: 1;
    animation: bounce-right 1s infinite;
}

@keyframes bounce-right {

    0%,
    100% {
        transform: translateX(0);
    }

    50% {
        transform: translateX(3px);
    }
}

.action-btn {
    background: rgba(239, 68, 68, 0.2);
    color: #fca5a5;
    border: 1px solid rgba(239, 68, 68, 0.3);
    border-radius: 8px;
    padding: 8px 16px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    transition: all 0.2s;
    height: 36px;
    align-self: flex-end;
}

/* FPS Counter */
#fps-counter {
    position: absolute;
    top: 10px;
    left: 10px;
    color: white;
    background: rgba(0, 0, 0, 0.5);
    padding: 4px 8px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 12px;
    pointer-events: none;
    z-index: 2000;
}

/* Brush Controls */
.brush-controls {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 1000;
}

.brush-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #aaa;
    padding: 8px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    text-transform: uppercase;
    transition: all 0.2s;
}

.brush-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.brush-btn.active {
    background: rgba(255, 255, 255, 0.3);
    color: white;
    border-color: white;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
}

/* Map Size Controls */
.size-controls {
    position: absolute;
    left: 10px;
    /* Moved to left to avoid overlap */
    top: 40%;
    /* Above brush controls */
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 1000;
}

.size-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #aaa;
    padding: 8px;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 12px;
    font-weight: bold;
    text-align: center;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.size-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.size-btn.active {
    background: rgba(255, 255, 255, 0.3);
    color: white;
    border-color: white;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
}

#clear-btn,
#eraser-btn {
    background: transparent !important;
    border: 1px solid rgba(255, 50, 50, 0.5);
    color: #ff5555;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
}

#clear-btn:hover,
#eraser-btn:hover {
    background: rgba(255, 50, 50, 0.1);
    border-color: #ff5555;
}

#eraser-btn.active {
    background: rgba(255, 50, 50, 0.2);
    color: #ff5555;
    border-color: #ff5555;
    box-shadow: 0 0 10px rgba(255, 50, 50, 0.3);
}

.action-btn:hover {
    background: rgba(239, 68, 68, 0.3);
    color: #fff;
}

input[type="range"] {
    appearance: none;
    -webkit-appearance: none;
    width: 100px;
    height: 4px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 2px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    background: var(--accent-color);
    border-radius: 50%;
    cursor: pointer;
    transition: transform 0.1s;
}

.controls-group {
    margin-left: auto;
    min-width: max-content;
}

.controls-group .tool-label {
    margin-bottom: 4px;
}

#brush-size {
    width: 80px;
}

#controls {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: nowrap;
}

.tool-group:last-child {
    margin-left: auto;
    /* Push controls to the right */
    display: flex;
    flex-direction: row;
    /* Horizontal layout for size slider */
    align-items: center;
    gap: 10px;
}

.tool-group:last-child .tool-label {
    border-bottom: none;
    /* Remove underline for horizontal layout */
    margin-bottom: 0;
    margin-right: 5px;
}

.tool-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: max-content;
    /* Prevent shrinking */
}

/* Ensure element buttons take remaining space but don't get crushed */
.tool-group:nth-child(2) {
    flex-grow: 1;
    min-width: 200px;
    overflow: hidden;
}

#category-menu button:hover {
    background: rgba(255, 255, 255, 0.1);
    color: white;
}

@media (max-width: 600px) {
    .toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 16px;
    }

    .element-buttons {
        justify-content: flex-start;
        /* Allow scrolling on mobile */
    }

    .tool-group {
        align-items: stretch;
        width: 100%;
    }

    #element-search {
        width: 100%;
    }
}

/* Settings Menu */
.settings-menu {
    position: absolute;
    bottom: 80px;
    right: 20px;
    width: 90vw;
    max-width: 450px;
    max-height: 65vh;
    overflow-y: auto;
    background: rgba(25, 25, 30, 0.95);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 16px;
    color: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    z-index: 2000;
    display: none;
    flex-direction: column;
    gap: 16px;
    animation: slide-up 0.2s ease-out;
}

@keyframes slide-up {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
    font-size: 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding-bottom: 12px;
}

.icon-btn-small {
    background: transparent;
    border: none;
    color: #888;
    cursor: pointer;
    font-size: 16px;
    padding: 4px;
    border-radius: 4px;
}

.icon-btn-small:hover {
    background: rgba(255, 255, 255, 0.1);
    color: white;
}

.setting-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.setting-label {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.setting-label-header {
    font-weight: 500;
    font-size: 14px;
    margin-bottom: 8px;
}

.setting-desc {
    font-size: 12px;
    color: #888;
}

/* Toggle Switch */
.switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #333;
    transition: .4s;
    border-radius: 24px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked+.slider {
    background-color: var(--accent-color);
}

input:checked+.slider:before {
    transform: translateX(20px);
}

/* Quality Selector */
.quality-selector {
    display: flex;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 6px;
    padding: 2px;
}

.quality-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: #888;
    padding: 6px;
    font-size: 12px;
    cursor: pointer;
    border-radius: 4px;
    font-weight: 500;
    transition: all 0.2s;
}

.quality-btn:hover {
    color: white;
}

.quality-btn.active {
    background: rgba(255, 255, 255, 0.1);
    color: white;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

#settings-btn:hover {
    border-color: rgba(255, 255, 255, 0.5);
    color: white;
    background: rgba(255, 255, 255, 0.05);
}

#custom-template {
    appearance: none;
    -webkit-appearance: none;
}

#custom-template option {
    color: black;
    background: white;
}

/* Tool Settings specific inputs */
.settings-select,
.settings-input {
    background: rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    padding: 6px;
    border-radius: 4px;
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    outline: none;
    max-width: 150px;
}

.settings-select option {
    background: #222;
    color: white;
}

/* Joystick Controls */
.joystick-zone {
    position: absolute;
    bottom: 50px;
    left: 20px;
    width: 150px;
    height: 150px;
    z-index: 1000;
    pointer-events: auto;
    /* Allow touch explicitly */
}

.joystick-base {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 120px;
    height: 120px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    border: 2px solid rgba(255, 255, 255, 0.2);
    display: none;
    /* Show only when touched */
}

.joystick-knob {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 50px;
    height: 50px;
    background: rgba(255, 255, 255, 0.4);
    border-radius: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}