* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #ffffff;
    --surface: #f8f8f8;
    --border: #e5e5e5;
    --text: #1a1a1a;
    --text-secondary: #666666;
    --accent: #000000;
    --accent-hover: #333333;
}

[data-theme="dark"] {
    --bg: #1a1a1a;
    --surface: #2a2a2a;
    --border: #404040;
    --text: #e5e5e5;
    --text-secondary: #999999;
    --accent: #ffffff;
    --accent-hover: #cccccc;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    overflow-x: hidden;
    transition: background-color 0.3s, color 0.3s;
}

.container {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

header {
    padding: 20px 32px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

header h1 {
    font-size: 18px;
    font-weight: 600;
    letter-spacing: -0.5px;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 16px;
}


.flag-icon {
    width: 20px;
    height: 20px;
    object-fit: contain;
}


.btn-icon {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    font-size: 16px;
}

.btn-icon:hover {
    background: var(--bg);
    border-color: var(--accent);
}

.main-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.sidebar {
    width: 280px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 24px;
    overflow-y: auto;
}


.controls {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group label {
    font-size: 12px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

.resolution-display {
    font-size: 24px;
    font-weight: 300;
    color: var(--text);
    margin: 4px 0;
}

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

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

input[type="range"]::-moz-range-thumb {
    width: 16px;
    height: 16px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
    border: none;
}

.custom-input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 14px;
    margin-top: 4px;
    background: var(--surface);
    color: var(--text);
}

.custom-input:focus {
    outline: none;
    border-color: var(--accent);
}

.palette-select {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 14px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
}

.palette-select:focus {
    outline: none;
    border-color: var(--accent);
}

.palette-select option {
    background: var(--surface);
    color: var(--text);
}

.palette-select optgroup {
    background: var(--surface);
    color: var(--text);
}

.palette-preview {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 12px;
    min-height: 24px;
    align-items: center;
}

.palette-preview span {
    width: 100%;
    line-height: 1.2;
}

.palette-preview .color-dot {
    aspect-ratio: 1;
    border-radius: 4px;
    border: 1px solid var(--border);
    cursor: pointer;
    transition: transform 0.1s;
    position: relative;
    min-width: 24px;
    min-height: 24px;
    pointer-events: all;
}

.palette-preview .color-dot:hover {
    transform: scale(1.2);
    z-index: 10;
    border-color: var(--accent);
}

.palette-preview .color-dot .remove-dot {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 14px;
    height: 14px;
    background: var(--accent);
    color: white;
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    cursor: pointer;
    pointer-events: all;
    z-index: 20;
}

.palette-preview .color-dot:hover .remove-dot {
    display: flex;
}

.custom-palette-container {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}

.palette-colors {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
    gap: 4px;
    margin-bottom: 12px;
}

.color-chip {
    aspect-ratio: 1;
    border-radius: 4px;
    border: 1px solid var(--border);
    cursor: pointer;
    position: relative;
    transition: transform 0.1s;
}

.color-chip:hover {
    transform: scale(1.1);
}

.color-chip input[type="color"] {
    position: absolute;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
}

.color-chip .remove-color {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 16px;
    height: 16px;
    background: var(--accent);
    color: white;
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    cursor: pointer;
}

.color-chip:hover .remove-color {
    display: flex;
}

/* Color Picker */
.color-picker {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.15);
    z-index: 10000;
    width: 320px;
}

.picker-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}

.picker-title {
    font-size: 14px;
    font-weight: 600;
}

.picker-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.picker-close:hover {
    color: var(--text);
}

.picker-content {
    padding: 16px;
}

.picker-spectrum {
    width: 100%;
    height: 150px;
    border: 1px solid var(--border);
    border-radius: 4px;
    position: relative;
    cursor: crosshair;
    background: linear-gradient(to right, #fff, transparent),
                linear-gradient(to top, #000, transparent);
}

.spectrum-cursor {
    position: absolute;
    width: 12px;
    height: 12px;
    border: 2px solid white;
    border-radius: 50%;
    box-shadow: 0 0 0 1px rgba(0,0,0,0.3), 0 2px 4px rgba(0,0,0,0.2);
    transform: translate(-50%, -50%);
    pointer-events: none;
}

.picker-hue {
    width: 100%;
    height: 24px;
    margin: 12px 0;
    border: 1px solid var(--border);
    border-radius: 4px;
    position: relative;
    cursor: pointer;
    background: linear-gradient(to right, 
        #ff0000 0%, 
        #ffff00 17%, 
        #00ff00 33%, 
        #00ffff 50%, 
        #0000ff 67%, 
        #ff00ff 83%, 
        #ff0000 100%);
}

.hue-cursor {
    position: absolute;
    width: 8px;
    height: 100%;
    border: 2px solid white;
    border-radius: 3px;
    box-shadow: 0 0 0 1px rgba(0,0,0,0.3);
    transform: translateX(-50%);
    pointer-events: none;
}

.picker-values {
    display: flex;
    gap: 12px;
    align-items: center;
    margin: 12px 0;
}

.color-preview {
    width: 48px;
    height: 48px;
    border: 1px solid var(--border);
    border-radius: 4px;
    flex-shrink: 0;
}

.hex-input {
    flex: 1;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 14px;
    font-family: monospace;
    text-transform: uppercase;
    background: var(--bg);
    color: var(--text);
}

.hex-input:focus {
    outline: none;
    border-color: var(--accent);
}

.picker-actions {
    display: flex;
    gap: 8px;
    margin-top: 16px;
}

.picker-actions button {
    flex: 1;
    padding: 10px;
    font-size: 14px;
}


.actions {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding-top: 24px;
}

.btn-primary {
    width: 100%;
    padding: 12px 16px;
    background: var(--accent);
    color: var(--bg);
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: background 0.2s;
}

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

.btn-primary svg {
    width: 16px;
    height: 16px;
    stroke: var(--bg);
}

.btn-secondary {
    width: 100%;
    padding: 12px 16px;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-secondary:hover {
    background: var(--bg);
    border-color: var(--accent);
}

.btn-danger {
    width: 100%;
    padding: 12px 16px;
    background: #ff4444;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-danger:hover {
    background: #cc0000;
}

.btn-text {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px;
    text-decoration: underline;
    transition: opacity 0.2s;
}

.btn-text:hover {
    opacity: 1 !important;
    color: #ff4444;
}

/* Image History */
.image-history {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 8px 0;
    min-height: 60px;
}

.image-history::-webkit-scrollbar {
    height: 6px;
}

.image-history::-webkit-scrollbar-track {
    background: var(--border);
    border-radius: 3px;
}

.image-history::-webkit-scrollbar-thumb {
    background: var(--text-secondary);
    border-radius: 3px;
}

.history-item {
    flex-shrink: 0;
    width: 50px;
    height: 50px;
    border: 2px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    background-size: cover;
    background-position: center;
    transition: all 0.2s;
    position: relative;
}

.history-item:hover {
    border-color: var(--accent);
    transform: scale(1.05);
}

.history-item.active {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent);
}

.history-item .remove-history {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 16px;
    height: 16px;
    background: var(--accent);
    color: white;
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    cursor: pointer;
}

.history-item:hover .remove-history {
    display: flex;
}

/* Export Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.modal-content {
    background: var(--bg);
    border-radius: 12px;
    width: 90%;
    max-width: 500px;
    max-height: 90vh;
    border: 1px solid var(--border);
    overflow: auto;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

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

.modal-header h2 {
    font-size: 18px;
    font-weight: 600;
    margin: 0;
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
}

.modal-close:hover {
    background: var(--surface);
}

.modal-body {
    padding: 20px;
}

.export-group {
    margin-bottom: 24px;
}

.export-group label {
    display: block;
    font-size: 12px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.format-options {
    display: grid;
    gap: 8px;
}

.format-option {
    display: flex;
    align-items: center;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    background: var(--bg);
}

.format-option:hover {
    background: var(--surface);
    border-color: var(--accent);
}

.format-option:has(input:checked) {
    border-color: var(--accent);
    background: var(--surface);
}

.format-option input {
    margin-right: 12px;
}

.format-option span {
    font-weight: 500;
    margin-right: 8px;
}

.format-option small {
    color: var(--text-secondary);
    font-size: 11px;
    margin-left: auto;
}

.scale-options {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 8px;
}

.scale-btn {
    padding: 8px 16px;
    border: 1px solid var(--border);
    background: white;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s;
}

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

.scale-btn.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.scale-input {
    width: 80px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    background: var(--surface);
    color: var(--text);
}

.export-size {
    display: block;
    color: var(--text-secondary);
    font-size: 12px;
}

.modal-footer {
    padding: 20px;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 8px;
    justify-content: flex-end;
}

.canvas-area {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 32px;
    background: var(--bg);
    position: relative;
}

.canvas-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

#pixelCanvas {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
}

.drop-overlay {
    position: absolute;
    inset: 32px;
    background: rgba(0, 0, 0, 0.95);
    backdrop-filter: blur(10px);
    color: white;
    display: none;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    border-radius: 12px;
    border: 2px dashed rgba(255, 255, 255, 0.3);
}

.drop-overlay p {
    font-size: 18px;
    font-weight: 500;
}

.drop-overlay.active {
    display: flex;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 40px;
    transition: opacity 0.2s;
}

.empty-state:hover {
    opacity: 0.7;
}

.empty-state svg {
    width: 48px;
    height: 48px;
    opacity: 0.3;
}

.empty-state p {
    font-size: 14px;
}

.canvas-container.has-image .empty-state {
    display: none;
}

@media (max-width: 768px) {
    .main-layout {
        flex-direction: column;
    }
    
    .sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid var(--border);
        max-height: 50vh;
    }
}