:root {
    --bg: #000;
    --panel-bg: rgba(0, 0, 0, 0.7);
    --text: #fff;
    --muted-1: #ccc;
    --muted-2: #aaa;
    --muted-3: #777;
    --border: #333;
    --accent: #4CAF50;
    --slider-accent: #0075FF;
    --button-bg: #333;
    --button-bg-hover: #555;
}

body {
    margin: 0;
    padding: 0;
    background-color: var(--bg);
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    font-family: Arial, sans-serif;
}

canvas {
    border: 1px solid var(--border);
    background-color: var(--bg);
}

#viewportOverlay {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 9;
    display: none;
    --vo: 0.95;
    background: radial-gradient(circle at var(--vx, 50%) var(--vy, 50%),
            transparent var(--vr, 150px),
            rgba(0, 0, 0, var(--vo)) calc(var(--vr, 150px) + 1px));
}

.controls {
    position: absolute;
    top: 20px;
    left: 20px;
    color: var(--text);
    z-index: 10;
    background-color: var(--panel-bg);
    padding: 15px;
    border-radius: 10px;
    min-width: 200px;
    /* Responsive width: narrower default, still capped for large screens */
    width: clamp(320px, 32vw, 480px);
    max-width: 520px;
    /* Constrain overall height to keep bottom spacing on 1080p and below */
    max-height: calc(100vh - 40px);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.controls.hidden {
    opacity: 0;
    transform: translateX(-100%);
    pointer-events: none;
}

.controls-header {
    display: flex;
    align-items: center;
    gap: 5px;
    margin-bottom: 10px;
}

/* Let the Hide button expand to fill available space on desktop */
.controls-header #hideBtn {
    flex: 1 1 auto;
}


.controls-note {
    margin-bottom: 10px;
    padding: 5px 8px;
    background-color: rgba(255, 255, 255, 0.05);
    border-radius: 5px;
    border-left: 3px solid var(--slider-accent);
}

.controls-note small {
    color: var(--muted-2);
    font-size: 11px;
    line-height: 1.3;
}

button {
    background-color: var(--button-bg);
    color: var(--text);
    border: none;
    padding: 10px 15px;
    margin: 5px;
    cursor: pointer;
    border-radius: 5px;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

button:hover {
    background-color: var(--button-bg-hover);
    transform: translateY(-1px);
}


.slider-container {
    margin: 10px 0;
}

/* New button hierarchy and icon-label layout */
.btn {
    gap: 8px;
    line-height: 1;
}

.btn .btn__icon {
    display: inline-flex;
}

.btn .btn__label {
    font-weight: 600;
}

.btn-primary {
    background-color: #1f6feb;
}

.btn-primary:hover {
    background-color: #1958be;
}

.btn-secondary {
    background-color: #444;
}

.btn-secondary:hover {
    background-color: #555;
}

.btn-tertiary {
    background-color: #2a2a2a;
}

.btn-tertiary:hover {
    background-color: #3a3a3a;
}

/* Icon-only buttons */
.btn-icon-only {
    padding: 10px;
    min-width: 40px;
    justify-content: center;
}

.btn-icon-only .btn__label {
    display: none;
}

.controls-header #shareBtn .btn__label {
    display: inline-block;
    min-width: 11ch;
    white-space: nowrap;
}

/* Compact buttons on mobile: prioritize icon, allow label to wrap/clip */
@media (max-width: 768px) {
    .controls-header {
        gap: 8px;
    }
    .btn {
        padding: 10px 12px;
        min-width: 0;
    }
    .controls-header .btn-tertiary .btn__label {
        display: none;
    }
}

.slider-container label {
    display: block;
    margin-bottom: 5px;
    font-size: 14px;
}

.slider {
    width: 100%;
    margin: 5px 0;
}

.slider-value {
    font-size: 12px;
    color: var(--muted-1);
}

.text-input {
    width: 100%;
    padding: 8px 10px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    outline: none;
    box-sizing: border-box;
}

.text-input:focus {
    border-color: var(--slider-accent);
}

.dual-range-container {
    position: relative;
    margin: 10px 0;
}

.dual-range-slider {
    position: relative;
    height: 20px;
}

.dual-range-slider input[type="range"] {
    position: absolute;
    width: 100%;
    height: 20px;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    pointer-events: none;
    margin: 0;
}

.dual-range-slider input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    height: 20px;
    width: 20px;
    border-radius: 50%;
    background: var(--slider-accent);
    cursor: pointer;
    pointer-events: all;
    border: 2px solid #fff;
}

.dual-range-slider input[type="range"]::-moz-range-thumb {
    height: 20px;
    width: 20px;
    border-radius: 50%;
    background: var(--slider-accent);
    cursor: pointer;
    pointer-events: all;
    border: 2px solid #fff;
}

.dual-range-slider .range-track {
    position: absolute;
    height: 4px;
    background: var(--border);
    width: 100%;
    top: 8px;
    border-radius: 2px;
}

.dual-range-slider .range-fill {
    position: absolute;
    height: 4px;
    background: var(--slider-accent);
    top: 8px;
    border-radius: 2px;
}

.dual-range-values {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: var(--muted-1);
    margin-top: 5px;
}

.color-section {
    margin-top: 15px;
    padding-top: 15px;
    border-top: 1px solid var(--border);
}

.color-palette {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    margin: 10px 0;
    max-width: 400px;
}

.color-box {
    width: 25px;
    height: 25px;
    border-radius: 3px;
    cursor: pointer;
    border: 2px solid transparent;
    position: relative;
}

.color-box:hover {
    border-color: var(--text);
}

.color-input {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
}

.add-color-btn {
    background-color: #444;
    color: var(--text);
    border: 1px dashed #666;
    width: 25px;
    height: 25px;
    border-radius: 3px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.2s ease;
}

.add-color-btn:hover {
    background-color: var(--button-bg-hover);
    border-color: #888;
    transform: translateY(-1px) scale(1.05);
}


.color-controls {
    margin-top: 10px;
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

.color-count {
    font-size: 12px;
    color: var(--muted-2);
}

.color-info {
    font-size: 11px;
    color: var(--muted-3);
    margin-top: 5px;
    line-height: 1.3;
}

/* Slide-down animation for conditional controls */
.slide-container {
    overflow: hidden;
    transition: max-height 0.3s ease, opacity 0.3s ease;
}

.slide-container.collapsed {
    max-height: 0 !important;
    opacity: 0;
    margin-top: 0 !important;
    margin-bottom: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
}

.slide-container.expanded {
    opacity: 1;
}

/* Auto-calculate max-height for different content types */
.slide-container.single-slider {
    max-height: 80px;
}

.slide-container.dual-slider {
    max-height: 100px;
}

.slide-container.triple-control {
    max-height: 140px;
}

/* For Subliminals: input + 2 sliders + labels */
.slide-container.quad-control {
    max-height: 420px;
}

/* Long list: used by Presets panel */
.slide-container.long-list {
    max-height: none;
    overflow: visible;
}

@keyframes shake {

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

    25% {
        transform: translateX(-2px);
    }

    75% {
        transform: translateX(2px);
    }
}

/* Enable scrolling for desktop when controls get too tall */
.controls-content {
    max-height: none;
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    /* Hide scrollbar but keep functionality */
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE and Edge */
}

.controls-content::-webkit-scrollbar {
    display: none; /* Chrome, Safari, Opera */
}

@media (max-width: 768px) {
    .controls {
        position: fixed;
        top: calc(env(safe-area-inset-top, 0px) + 12px);
        left: 12px;
        right: 12px;
        bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
        width: auto;
        max-width: 720px;
        padding: 12px;
        border-radius: 12px;
        min-width: 0;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }

    .controls.hidden {
        transform: translateY(-140%);
    }

    .controls-header {
        flex-wrap: wrap;
        gap: 8px;
        margin-bottom: 8px;
        order: 3;
        margin-top: 0;
        margin-bottom: 0;
    }

    .controls-content {
        order: 1;
        flex: 1;
        max-height: 85dvh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .controls-note {
        order: 2;
        margin-top: 8px;
        margin-bottom: 8px;
    }

    button {
        padding: 12px 14px;
        flex: 1 1 auto;
        min-width: 0;
        touch-action: manipulation;
    }

    .dual-range-slider {
        height: 28px;
    }

    .dual-range-slider .range-track,
    .dual-range-slider .range-fill {
        top: 12px;
    }

    .dual-range-slider input[type="range"] {
        height: 28px;
    }

    .dual-range-slider input[type="range"]::-webkit-slider-thumb {
        height: 28px;
        width: 28px;
    }

    .dual-range-slider input[type="range"]::-moz-range-thumb {
        height: 28px;
        width: 28px;
    }

    .color-box,
    .add-color-btn {
        width: 32px;
        height: 32px;
    }

    /* Allow more flexibility on mobile */
    .color-palette {
        max-width: 100%;
    }

    .color-count,
    .slider-value,
    .dual-range-values {
        font-size: 13px;
    }

    .controls-note small {
        font-size: 12px;
    }

    .controls-header #shareBtn {
        flex: 0 0 auto;
    }

    /* Taller content on mobile for Subliminals panel */
    .slide-container.quad-control {
        max-height: 520px;
    }

    /* Allow taller space for presets on mobile */
    .slide-container.long-list {
        max-height: none;
        overflow: visible;
    }

    /* Fix text input width on mobile */
    .text-input {
        padding: 6px 8px;
        box-sizing: border-box;
    }
}

/* Presets UI */
.presets-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.preset-importer {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 10px;
}

.preset-importer-main {
    display: flex;
    flex-direction: row;
    gap: 8px;
    align-items: stretch;
    flex-wrap: wrap;
}

.preset-importer-main > button {
    flex: 1 1 auto;
    min-width: 0;
    justify-content: center;
}

.btn-paste-preset {
    justify-content: center;
}

.preset-importer-feedback {
    min-height: 16px;
    font-size: 12px;
    color: var(--muted-2);
    text-align: center;
    width: 100%;
    flex: 1 1 100%;
    /* Prevent long preset names from expanding the panel width */
    overflow-wrap: anywhere;
    word-break: break-word;
    white-space: normal;
    min-width: 0;
}

.preset-lists {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 0;
}

.preset-list-title {
    font-size: 12px;
    color: var(--muted-2);
    margin-bottom: 6px;
}

.preset-list-items {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.preset-row {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 8px;
}

.preset-row .preset-name {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.preset-row .preset-preview {
    flex: 0 1 auto;
    max-width: 50%;
    overflow: hidden;
}

.preset-palette {
    display: flex;
    align-items: center;
    gap: 3px;
}

.preset-swatch {
    width: 18px;
    height: 18px;
    border-radius: 4px;
    border: 1px solid rgba(255,255,255,0.25);
    flex: 0 0 auto;
}

.preset-row .preset-actions {
    display: flex;
    gap: 6px;
}

.preset-row .btn-delete {
    background-color: #4a1f1f;
}

.preset-row .btn-delete:hover {
    background-color: #6a2a2a;
}

/* Two-tap delete confirmation state */
.preset-row .btn-delete.confirm {
    background-color: #7a1c1c;
    color: #ff6b6b;
    box-shadow: 0 0 0 2px rgba(255, 107, 107, 0.25) inset;
    animation: shake 0.18s ease-in-out;
}

.preset-row .btn-delete.confirm:hover {
    background-color: #8b1f1f;
}