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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    line-height: 1.6;
}

/* Header bar layout */
.header-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

header {
    text-align: center;
    margin-bottom: 40px;
    color: white;
}

/* Theme toggle icons */
#themeToggle .icon { display: inline-block; vertical-align: middle; }
#themeToggle .icon-sun { display: none; }
#themeToggle .icon-moon { display: inline; }
body[data-theme="dark"] #themeToggle .icon-sun { display: inline; }
body[data-theme="dark"] #themeToggle .icon-moon { display: none; }

header h1 {
    font-size: 2.5rem;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

header p {
    font-size: 1.1rem;
    opacity: 0.9;
}

/* Upload Section */
.upload-section {
    margin-bottom: 40px;
}

.upload-area {
    border: 3px dashed #ffffff80;
    border-radius: 20px;
    padding: 60px 20px;
    text-align: center;
    background: rgba(255, 255, 255, 0.1);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    cursor: pointer;
    transition: all 0.3s ease;
}

.upload-area:hover {
    border-color: #ffffff;
    background: rgba(255, 255, 255, 0.2);
}

.upload-area.dragover {
    border-color: #4CAF50;
    background: rgba(76, 175, 80, 0.1);
}

.upload-content {
    color: white;
}

.upload-icon {
    width: 48px;
    height: 48px;
    margin-bottom: 20px;
}

.upload-content p {
    font-size: 1.2rem;
    margin-bottom: 10px;
}

.upload-content small {
    opacity: 0.8;
}

/* Upload progress */
.upload-progress {
    position: relative;
    width: min(420px, 90%);
    height: 10px;
    margin: 16px auto 0;
    background: rgba(255,255,255,0.25);
    border-radius: 999px;
    overflow: hidden;
    transform: translateZ(0);
    transition: opacity 0.2s ease;
}
.upload-progress[aria-hidden="true"] {
    opacity: 0;
    pointer-events: none;
}
.upload-progress .bar {
    position: absolute;
    left: 0; top: 0; bottom: 0;
    width: 0%;
    background: linear-gradient(135deg, #4caf50, #66bb6a);
    border-radius: inherit;
    transition: width 0.2s ease;
}
.upload-progress .percent {
    position: absolute;
    top: -22px;
    right: 0;
    font-size: 0.85rem;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0,0,0,0.4);
}

/* Controls Section */
.controls-section {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    margin-bottom: 40px;
}

.preview-container, .controls-container {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 15px;
    padding: 25px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.preview-container h3, .controls-container h4 {
    margin-bottom: 20px;
    color: #333;
}

.preview-area {
    display: flex;
    justify-content: center;
    align-items: center;
    background: #f8f9fa;
    border-radius: 10px;
    padding: 20px;
}

#previewCanvas {
    border-radius: 0px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    background: white;
    cursor: crosshair;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    width: 400px;
    height: 400px;
}

/* Control Groups */
.control-group {
    margin-bottom: 30px;
}

.aspect-ratio-controls {
    display: flex;
    gap: 15px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

.aspect-ratio-option {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 15px;
    background: #f8f9fa;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    border: 2px solid transparent;
}

.aspect-ratio-option:hover {
    background: #e9ecef;
}

.aspect-ratio-option input[type="radio"] {
    width: 16px;
    height: 16px;
    accent-color: #667eea;
}

.aspect-ratio-option input[type="radio"]:checked + span {
    color: #667eea;
    font-weight: 600;
}

.aspect-ratio-option:has(input[type="radio"]:checked) {
    background: #667eea20;
    border-color: #667eea;
}

.aspect-ratio-option span {
    font-size: 0.9rem;
    color: #333;
    transition: color 0.3s ease;
}

/* Naming Controls */
.naming-controls {
    background: #f8f9fa;
    padding: 20px;
    border-radius: 10px;
    border: 1px solid #e9ecef;
}

.naming-controls input[type="text"] {
    flex: 1;
    padding: 10px;
    border: 2px solid #e9ecef;
    border-radius: 6px;
    font-size: 1rem;
    transition: border-color 0.3s ease;
}

.naming-controls input[type="text"]:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.naming-format-controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin: 15px 0;
}

.naming-format-option {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px;
    background: white;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.3s ease;
    border: 2px solid transparent;
}

.naming-format-option:hover {
    background: #f8f9fa;
}

.naming-format-option input[type="radio"] {
    width: 16px;
    height: 16px;
    accent-color: #667eea;
}

.naming-format-option input[type="radio"]:checked + span {
    color: #667eea;
    font-weight: 600;
}

.naming-format-option:has(input[type="radio"]:checked) {
    background: #667eea15;
    border-color: #667eea;
}

.naming-preview {
    background: white;
    padding: 12px;
    border-radius: 6px;
    border: 2px solid #e9ecef;
    display: flex;
    align-items: center;
    gap: 8px;
}

.naming-preview code {
    background: #667eea20;
    color: #667eea;
    padding: 4px 8px;
    border-radius: 4px;
    font-family: 'Consolas', 'Monaco', monospace;
    font-weight: 600;
}

.naming-note {
    background: #e3f2fd;
    color: #1565c0;
    padding: 12px;
    border-radius: 6px;
    border: 1px solid #bbdefb;
    font-size: 0.9rem;
    margin-top: 12px;
}

.naming-note strong {
    color: #0d47a1;
}

/* Corner Preview */
.corner-preview-container {
    display: flex;
    justify-content: center;
    margin-bottom: 15px;
}

.corner-preview {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background: #f8f9fa;
    border-radius: 10px;
    border: 2px solid #e9ecef;
}

.corner-preview canvas {
    border-radius: 0px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    background: white;
    width: 400px;
    height: 400px;
}

.corner-preview-label {
    font-size: 0.9rem;
    color: #666;
    font-weight: 500;
}

.control-row {
    display: flex;
    align-items: center;
    gap: 15px;
    margin-bottom: 15px;
}

.control-row label {
    min-width: 80px;
    font-weight: 500;
    color: #555;
}

.control-row input[type="range"] {
    flex: 1;
    height: 6px;
    border-radius: 3px;
    background: #ddd;
    outline: none;
    cursor: pointer;
}

.control-row input[type="range"]::-webkit-slider-thumb {
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

.control-row input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
    border: none;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

.control-row span {
    min-width: 40px;
    font-weight: 600;
    color: #667eea;
}

/* Size Grid */
.size-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}

.size-option {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px;
    background: #f8f9fa;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s ease, border-color 0.2s ease;
}

.size-option:hover {
    background: #e9ecef;
    transform: translateY(-2px);
    box-shadow: 0 4px 14px rgba(0,0,0,0.08);
}

.size-option input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: #667eea;
}

.size-option span {
    font-weight: 500;
    color: #333;
}

.size-option:active {
    transform: translateY(0) scale(0.98);
}

/* Buttons */
.action-buttons {
    display: flex;
    gap: 15px;
    margin-top: 20px;
}

.btn {
    flex: 1;
    padding: 15px 20px;
    border: none;
    border-radius: 10px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}

/* Compact button variant for small inline controls */
.btn-compact {
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 0.9rem;
    flex: 0 0 auto;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
    background: #6c757d;
    color: white;
}

.btn-secondary:hover {
    background: #5a6268;
    transform: translateY(-2px);
}

.btn-ico {
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
    color: white;
}

.btn-ico:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
}

/* Results Section */
.results-section {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 15px;
    padding: 30px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.results-section h3 {
    margin-bottom: 25px;
    color: #333;
    text-align: center;
}

/* Section header with inline controls */
.section-header {
    display: flex;
    align-items: center;
    gap: 12px;
    justify-content: space-between;
    flex-wrap: wrap;
    margin-bottom: 12px;
}

.section-header h4 {
    margin: 0;
}

.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #eef2ff;
    color: #4452c5;
    border: 1px solid #dce3ff;
    border-radius: 999px;
    padding: 6px 10px;
    font-size: 0.85rem;
    font-weight: 600;
}

.results-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 20px;
}

.result-item {
    background: white;
    border-radius: 10px;
    padding: 20px;
    text-align: center;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    transition: transform 0.3s ease;
}

.result-item:hover {
    transform: translateY(-5px);
}

.result-item canvas {
    border-radius: 8px;
    margin-bottom: 15px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.result-item .size-label {
    font-weight: 600;
    color: #333;
    margin-bottom: 10px;
}

.result-item .download-btn {
    background: #28a745;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background 0.3s ease;
}

.result-item .download-btn:hover {
    background: #218838;
}

/* Dark theme overrides */
[data-theme="dark"] body, body[data-theme="dark"] {
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
    color: #e5e7eb; /* base text color in dark */
}

[data-theme="dark"] header {
    color: #e2e8f0;
}

[data-theme="dark"] .preview-container,
[data-theme="dark"] .controls-container,
[data-theme="dark"] .results-section {
    background: rgba(17, 24, 39, 0.9);
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}
[data-theme="dark"] .preview-area {
    background: #0b1220;
}
[data-theme="dark"] .controls-container h4,
[data-theme="dark"] .results-section h3,
[data-theme="dark"] .preview-container h3 {
    color: #e5e7eb;
}
[data-theme="dark"] .naming-controls {
    background: #0f172a;
    border-color: #1f2937;
}
[data-theme="dark"] .naming-controls input[type="text"] {
    background: #0b1220;
    border-color: #1f2937;
    color: #e5e7eb;
}
[data-theme="dark"] .naming-controls input[type="text"]::placeholder {
    color: #94a3b8;
}
[data-theme="dark"] .aspect-ratio-option,
[data-theme="dark"] .size-option,
[data-theme="dark"] .naming-format-option {
    background: #0f172a;
}
[data-theme="dark"] .aspect-ratio-option:hover,
[data-theme="dark"] .size-option:hover,
[data-theme="dark"] .naming-format-option:hover {
    background: #111827;
}
[data-theme="dark"] .badge {
    background: #1f2937;
    color: #93c5fd;
    border-color: #111827;
}
[data-theme="dark"] .result-item {
    background: #0f172a;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5);
}
[data-theme="dark"] .result-item .size-label {
    color: #e5e7eb;
}

/* Corner preview note text */
[data-theme="dark"] .corner-preview .note {
    color: #cbd5e1;
}

/* Additional dark text overrides */
[data-theme="dark"] .size-option span,
[data-theme="dark"] .aspect-ratio-option span,
[data-theme="dark"] .naming-format-option span {
    color: #e5e7eb;
}

[data-theme="dark"] .control-row label {
    color: #cbd5e1;
}
[data-theme="dark"] .control-row span {
    color: #93c5fd; /* accent count/value */
}

[data-theme="dark"] .naming-preview {
    background: #0b1220;
    border-color: #1f2937;
    color: #e5e7eb;
}

/* Upload area ready state */
.upload-area.ready {
    border-color: #4CAF50;
    background: rgba(76, 175, 80, 0.12);
}
[data-theme="dark"] .upload-area.ready {
    border-color: #22c55e;
    background: rgba(34, 197, 94, 0.12);
}

/* Responsive Design */
@media (max-width: 768px) {
    .controls-section {
        grid-template-columns: 1fr;
    }
    
    .size-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .action-buttons {
        flex-direction: column;
    }
    
    .results-grid {
        grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    }

    /* Ensure canvases fit on small screens */
    #previewCanvas,
    .corner-preview canvas {
        width: 100% !important;
        height: auto !important;
        max-width: 100% !important;
        display: block;
    }

    .corner-preview {
        padding: 6px;
    }

    /* Prevent horizontal overflow on controls */
    .control-row {
        flex-wrap: wrap;
    }
    .control-row label {
        min-width: 100%;
        margin-bottom: 6px;
    }
    .naming-controls input[type="text"] {
        width: 100%;
        min-width: 0;
    }
    .controls-container,
    .preview-container {
        padding: 18px;
    }

    .section-header {
        gap: 10px;
    }
}

@media (max-width: 480px) {
    .container {
        padding: 15px;
    }
    
    header h1 {
        font-size: 2rem;
    }
    
    .upload-area {
        padding: 40px 15px;
    }
    
    .size-grid {
        grid-template-columns: 1fr;
    }

    /* Allow control rows to wrap so inputs don't overflow */
    .control-row {
        flex-wrap: wrap;
    }

    .control-row label {
        min-width: 100%;
    }

    .naming-controls input[type="text"] {
        width: 100%;
    }

    /* Prevent long naming previews from pushing layout horizontally */
    .naming-preview {
        overflow-x: auto;
    }
    .naming-preview code {
        white-space: nowrap;
    }
    html, body {
        overflow-x: hidden;
    }

    .section-header {
        flex-direction: column;
        align-items: stretch;
    }
    .section-header .btn-compact {
        width: 100%;
    }
}
