/* -------------------------------
   Global & Dark Theme
--------------------------------- */
:root {
    --primary: #6366f1;
    --primary-light: #8587f4;
    --primary-dark: #4f46e5;
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-light: #334155;
    --text: #f1f5f9;
    --text-secondary: #94a3b8;
    --border: #475569;
    --success: #10b981;
    --icon-size: 16px;
    /* --radius-2xs: 0px;
    --radius-xs: 4px;
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 10px;
    --radius-xl: 12px;
    --radius-round: 100%; */
    --radius-2xs: 0px;
    --radius-xs: 0px;
    --radius-sm: 0px;
    --radius-md: 0px;
    --radius-lg: 0px;
    --radius-xl: 0px;
    --radius-round: 0px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: system-ui, 'Segoe UI', Roboto, sans-serif;
    user-select: none;
}

/* width */
::-webkit-scrollbar {
    width: 10px;
    border-radius: var(--radius-md);
}

/* Track */
::-webkit-scrollbar-track {
    background: #2e3848;
    border-radius: var(--radius-md);
}

/* Handle */
::-webkit-scrollbar-thumb {
    background: #1a2333;
    border-radius: var(--radius-md);
}

/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
    background: #171e2c;
}

input:focus {
    outline: none !important;
    border-color: #7c869c !important;
}

/*********** Baseline, reset styles ***********/
input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    cursor: pointer;
    width: 25rem;
}

/* Removes default focus */
input[type="range"]:focus {
    outline: none;
}

/******** Chrome, Safari, Opera and Edge Chromium styles ********/
/* slider track */
input[type="range"]::-webkit-slider-runnable-track {
    background-color: #2e3848;
    border-radius: 0rem;
    height: 0.4rem;
}

/* slider thumb */
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    /* Override default look */
    appearance: none;
    margin-top: -0.2rem;
    /* Centers thumb on the track */
    background-color: var(--primary);
    border-radius: 0rem;
    height: 0.8rem;
    width: 0.8rem;
}

input[type="range"]:focus::-webkit-slider-thumb {
    outline: 1px solid var(--primary);
    outline-offset: 0.125rem;
}

/*********** Firefox styles ***********/
/* slider track */
input[type="range"]::-moz-range-track {
    background-color: green;
    border-radius: 0rem;
    height: 0.5rem;
}

/* slider thumb */
input[type="range"]::-moz-range-thumb {
    background-color: var(--primary);
    border: none;
    /*Removes extra border that FF applies*/
    border-radius: 0rem;
    height: 1rem;
    width: 1rem;
}

input[type="range"]:focus::-moz-range-thumb {
    outline: 3px solid var(--primary);
    outline-offset: 0.125rem;
}

input[type=checkbox],
input[type=radio] {
    accent-color: var(--primary);
    height: 1.1em;
    width: 1.1em;
}

body {
    background: var(--bg);
    color: #e6edf3;
    display: flex;
    justify-content: center;
    padding: 0.7rem;
    line-height: 1.6;
}

label {
    display: block;
}

.container {
    max-width: 1600px;
    width: 100%;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 0 0.5rem;
    margin-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}

.header h1 {
    font-size: 1.8rem;
    background: linear-gradient(135deg, var(--primary), var(--primary-light));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

.header .header-buttons {
    display: flex;
    gap: 0.4rem;
}

.save-btn {
    background: var(--success);
    color: white;
    border: none;
    padding: 0.5rem 0.6rem;
    border-radius: var(--radius-md);
    font-size: 0.8rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: background 0.3s;
}

.save-btn:hover {
    background: #0da271;
}

.square-btn {
    background: var(--surface-light);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-size: 0.8rem;
    cursor: pointer;
    transition: background 0.3s;
    text-decoration: none;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
}

.square-btn:hover {
    background: var(--surface);
}

/* -------------------------------
   Main Layout (Controls Left / Preview Right)
--------------------------------- */
.workspace {
    display: grid;
    grid-template-columns: 300px 1fr;
    gap: 1rem;
}

/* Control Panel */
.panel-section {
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border);
}

.panel-section h3 {
    font-size: 1.1rem;
    margin-bottom: 0.6rem;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.controls-panel {
    background: var(--surface);
    border-radius: var(--radius-xl);
    padding: 1rem;
    max-height: calc(100vh - 95px);
    overflow: auto;
    position: sticky;
    top: 10px;
}

/* Control Row */
.control-row {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin-bottom: 15px;
}

.control-row label,
.control-row .slider-label {
    font-size: 0.92rem;
    font-weight: 600;
    color: #bdc5d6;
}

/* Sliders */
input[type="range"] {
    width: 100%;
    accent-color: var(--primary);
    background: #2d313e;
    height: 6px;
    border-radius: var(--radius-xs);
}

/* Radio Buttons */
.radio-group {
    display: flex;
    gap: 1.8rem;
}

.radio-group label {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    font-weight: normal;
    color: #d3dae8;
}

/* Radio Buttons */
.checkbox-group {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.checkbox-group label {
    font-weight: normal;
    color: #d3dae8;
    line-height: 1;
}

/* Dropzone */
.dropzone {
    background: var(--bg);
    border: 2px dashed #5f6a7e;
    border-radius: var(--radius-lg);
    padding: 0.5rem;
    text-align: center;
    transition: 0.2s;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 0.85rem;
    position: relative;
}

.dropzone:hover {
    border-color: var(--primary);
    background: #2f3543;
}

.dropzone [type="file"] {
    display: block;
    position: absolute;
    inset: 0;
    opacity: 0;
}

.drop-label {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
    cursor: pointer;
}

.drop-label .icon {
    font-size: 2rem;
}

.drop-label .small {
    font-size: 0.8rem;
    color: #9aa1b5;
}

/* Pattern Grid - 5 columns */
.pattern-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 0.6rem;
    /* margin-top: 0.3rem; */
    /* max-height: 260px; */
    /* overflow-y: auto; */
    /* padding: 0.2rem; */
}

.pattern-item {
    border: 2px solid var(--border);
    aspect-ratio: 1;
    border-radius: var(--radius-sm);
    background: #2b303e;
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: border-color 0.3s;
    cursor: pointer;
    overflow: hidden;
}

.pattern-item:hover {
    border-color: var(--primary);
}

.pattern-item.selected {
    border-color: var(--primary);
    background: #2e3442;
}

.pattern-preview {
    width: 100%;
    aspect-ratio: 1 / 1;
    object-fit: contain;
    image-rendering: crisp-edges;
    image-rendering: pixelated;
    background: #0e1015;
}

.pattern-name {
    font-size: 0.7rem;
    margin-top: 0.2rem;
    color: #ccd3e0;
    text-align: center;
    word-break: break-word;
    display: none;
}

/* Select & File Input */
select,
input[type="file"] {
    background: #2d313e;
    color: white;
    border: 1px solid #555d70;
    padding: 0.6rem 0.8rem;
    border-radius: var(--radius-md);
    font-size: 0.9rem;
}

/* Buttons */
.button-group {
    display: flex;
    gap: 1rem;
    margin-top: 0.8rem;
}

.btn {
    flex: 1;
    padding: 0.85rem 0.5rem;
    border: none;
    border-radius: var(--radius-lg);
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
    transition: 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.3rem;
    line-height: 1;
}

.btn.primary {
    background: var(--primary);
    color: white;
}

.btn.primary:hover {
    background: var(--primary-light);
}

.btn.secondary {
    background: #333947;
    color: #eceff8;
}

.btn.secondary:hover {
    background: #40475a;
}

.btn.btn-icon {
    flex: 0;
    padding: 0.5rem !important;
    --icon-size: 18px;
}

.btn.btn-icon .loading-icon {
    animation: loading 2s linear infinite;
}

/* -------------------------------
   Preview Area - CSS controlled size only!
   Canvas dimensions are NOT changed programmatically
--------------------------------- */
.preview-area {
    background: var(--surface);
    border-radius: var(--radius-xl);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: start;
    padding: 1rem;
    position: relative;
}

.preview-canvas {
    width: 100%;
    max-height: calc(100vh - 115px);
    object-fit: contain;
    /* max-width: 700px; */
    /* Large, CSS-only sizing */
    height: auto;
    /* border-radius: var(--radius-xl); */
    /* box-shadow: 0 8px 25px rgba(0, 0, 0, 0.7); */

    /* CRITICAL: Preserve pixel perfect rendering */
    image-rendering: crisp-edges;
    /* Fallback */
    image-rendering: pixelated;
    /* Modern browsers */

    /* Do NOT set width/height attributes in JS, only CSS */
    display: block;
    /* background: #090b0f; */
}

.preview-canvas.maximize {
    max-height: none;
}

.stats {
    margin-top: 1rem;
    font-size: 0.9rem;
    color: #b3bac9;
    align-self: flex-start;
    width: 100%;
    padding: 0.2rem 0.5rem;
}

/* Error Message */
.error {
    background: #631c1c;
    color: #ffd6d6;
    padding: 0.4rem 1rem;
    border-radius: var(--radius-lg);
    margin: 0 0.5rem 0.5rem;
    border-left: 4px solid #f56565;
    width: 100%;
}

.hidden {
    display: none;
}

/* Responsive */
@media (max-width: 800px) {
    .workspace {
        grid-template-columns: 1fr;
    }

    .preview-canvas {
        max-width: 100%;
    }
}

/* Pattern Editor Styles */
.pattern-editor-details {
    margin-top: 1rem;
    border: 1px solid #3a4052;
    border-radius: var(--radius-md);
    /* padding: 0 0.5rem; */
    margin-bottom: 15px;
}

.pattern-editor-details summary {
    cursor: pointer;
    color: #bdc5d6;
    font-weight: 600;
    padding: 0.5rem;
    user-select: none;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.pattern-editor-details summary:hover {
    color: var(--primary);
}

.pattern-editor {
    padding: 0.5rem;
}

.editor-controls {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.editor-input-group {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.editor-input-group label {
    font-size: 0.8rem;
    color: #9aa1b5;
}

input[type=text],
.editor-input-group input,
#editor-layer-number {
    background: #2d313e;
    color: white;
    border: 1px solid #555d70;
    padding: 0.3rem 0.5rem;
    border-radius: var(--radius-xs);
}

.editor-layer-control {
    grid-column: span 2;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.editor-layer-control .range {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.editor-layer-control .range input[type="range"] {
    flex: 3;
}

.editor-layer-control .range input[type="number"] {
    flex: 1;
    min-width: 60px;
}

.pattern-preview-section {
    margin: 1rem 0;
    text-align: center;
}

.pattern-preview-section label {
    display: block;
    font-size: 0.9rem;
    color: #bdc5d6;
    margin-bottom: 0.5rem;
}

.pattern-preview-canvas {
    width: 64px;
    height: 64px;
    border: 2px solid #3a4052;
    border-radius: var(--radius-xs);
    background: #0e1015;
    margin: 0 auto;
}

.pattern-grid-container {
    margin: 1rem 0;
    overflow-x: auto;
    text-align: center;
}

.pattern-grid-canvas {
    display: inline-block;
    border: 1px solid #3a4052;
    cursor: crosshair;
    max-width: 100%;
}

.editor-buttons {
    display: flex;
    gap: 0.8rem;
    margin-top: 1rem;
    flex-direction: column;
}

.editor-buttons .btn.small {
    padding: 0.5rem 0.8rem;
    font-size: 0.85rem;
    background: #2d313e;
    color: #e6edf3;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    flex: 1;
}

.editor-buttons .btn.small:hover {
    background: #3a4052;
}

.editor-buttons .btn.small.primary {
    background: var(--primary);
    color: white;
}

.editor-buttons .btn.small.primary:hover {
    background: var(--primary-dark);
}

.editor-buttons .btn.small.warning {
    background: #7f1d1d;
    color: #fecaca;
    border: 1px solid #ef4444;
}

.editor-buttons .btn.small.warning:hover {
    background: #991b1b;
    color: white;
}

/* Delete icon for custom patterns */
.pattern-item {
    position: relative;
}

.pattern-item.custom .delete-pattern {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 16px;
    height: 16px;
    background: rgba(0, 0, 0, 0.7);
    color: #ff6b6b;
    border-radius: var(--radius-round);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
    border: 1px solid #ff6b6b;
}

.pattern-item.custom:hover .delete-pattern {
    opacity: 1;
}

.pattern-item.custom .delete-pattern:hover {
    background: #ff6b6b;
    color: white;
}

.m-0 {
    margin: 0;
}

.p-0 {
    padding: 0;
}

.icon {
    width: var(--icon-size);
    height: var(--icon-size);
    stroke: currentColor;
    fill: none;
}

h3 .icon,
summary .icon {
    --icon-size: 22px;
}

/* Palette Grid */
.palette-grid {
    width: 100%;
    display: grid;
    /* grid-template-columns: repeat(3, 1fr); */
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.6rem;
    /* margin-top: 0.5rem;
    margin-bottom: 0.5rem;
    max-height: 300px;
    padding: 0.2rem; */
    overflow-y: auto;
}

.palette-item {
    min-width: 0;
    border: 2px solid transparent;
    border-radius: var(--radius-md);
    padding: 0.3rem;
    background: #2b303e;
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: 0.1s;
    cursor: pointer;
    position: relative;
}

.palette-item.loading {
    pointer-events: none;
    user-select: none;
}

.palette-item.loading::before {
    content: '';
    display: block;
    position: absolute;
    inset: 0;
    background: #00000094;
}

.palette-item.loading::after {
    content: '';
    display: block;
    position: absolute;
    inset: 0;
    z-index: 5;
    background-size: 25px;
    background-repeat: no-repeat;
    background-position: center;
    background-image: url("data:image/svg+xml,%3Csvg width='24px' height='24px' viewBox='0 0 24 24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M12 3V6M12 18V21M6 12H3M21 12H18M5.63672 5.63672L7.75977 7.75977M16.2422 16.2422L18.3633 18.3633M18.3652 5.63477L16.2441 7.75586M7.75781 16.2422L5.63477 18.3652' stroke='%23fff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    animation: loading 2s linear infinite;
}

.palette-item:hover {
    background: #3a4052;
}

.palette-item.selected {
    border-color: #8b5cf6;
    background: #2e3442;
}

.palette-preview {
    width: 100%;
    max-width: 100%;
    min-height: 17px;
    max-height: 20px;
    /* aspect-ratio: 1 / 1; */
    object-fit: contain;
    image-rendering: crisp-edges;
    image-rendering: pixelated;
    border-radius: var(--radius-xs);
    margin-bottom: 0.2rem;
}

.palette-name {
    font-size: 0.6rem;
    color: #ccd3e0;
    text-align: center;
    font-weight: 500;
}

.palette-colors {
    font-size: 0.6rem;
    color: #9aa1b5;
    text-align: center;
}

/* Mini color bar for palette preview */
.palette-minibar {
    width: 100%;
    height: 8px;
    display: flex;
    margin-top: 0.2rem;
    border-radius: var(--radius-xs);
    overflow: hidden;
}

.palette-minibar-color {
    height: 100%;
    flex: 1;
}

/* Palette upload */
.palette-upload-container {
    margin-top: 0.8rem;
}

.palette-strip {
    display: flex;
    height: 30px;
    border-radius: var(--radius-xs);
    overflow: hidden;
    margin-top: 0.3rem;
    margin-bottom: 0.5rem;
}

.palette-strip-color {
    height: 100%;
    flex: 1;
}

/* Palette info */
.palette-info {
    font-size: 0.8rem;
    color: #9aa1b5;
    margin-bottom: 0.5rem;
    font-style: italic;
}

.d-block {
    display: block;
}

.slider-label {
    display: block;
    position: relative;
}

.slider-label button {
    position: absolute;
    right: 0;
    top: 6px;
    left: auto;
    bottom: auto;
    background: transparent;
    border: none;
    color: currentColor;
    outline: none;
    cursor: pointer;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s ease;
}

.slider-label button:focus {
    outline: none;
}

.slider-label:hover button {
    opacity: 0.5;
    visibility: visible;
}

.slider-label button:hover {
    opacity: 0.9;
}

.slider-label button .icon {
    --icon-size: 14px;
}

@keyframes loading {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

#togglePreviewScaleBtn {
    position: absolute;
    width: 30px;
    height: 30px;
    top: 5px;
    right: 5px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}