: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;
}

#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%;
    }
}