:root {
    --bg-black: #030303;
    --bg-card: rgba(15, 15, 15, 0.6);
    --bg-canvas: #000000;
    --border-dim: rgba(255, 255, 255, 0.05);
    --border-active: rgba(255, 255, 255, 0.15);
    --text-primary: #ffffff;
    --text-secondary: #888888;
    --accent: #ff4d00;
    --accent-glow: rgba(255, 77, 0, 0.3);
    
    --font-main: 'Inter', sans-serif;
    --font-condensed: 'Inter Tight', sans-serif;
}

[data-theme="light"] {
    --bg-black: #f5f5f5;
    --bg-card: rgba(255, 255, 255, 0.8);
    --bg-canvas: #ffffff;
    --border-dim: rgba(0, 0, 0, 0.05);
    --border-active: rgba(0, 0, 0, 0.15);
    --text-primary: #111111;
    --text-secondary: #666666;
}

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

body {
    background-color: var(--bg-black);
    color: var(--text-primary);
    font-family: var(--font-main);
    font-size: 13px;
    line-height: 1.4;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    transition: background-color 0.3s ease, color 0.3s ease;
}

h1, h2, h3, button, .label {
    font-family: var(--font-condensed);
    font-weight: 600;
    letter-spacing: -0.02em;
    text-transform: uppercase;
}

.app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

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

.logo {
    font-size: 1.2rem;
    font-weight: 900;
    letter-spacing: 0.1em;
}

.logo span {
    color: var(--accent);
}

.main-nav {
    display: flex;
    gap: 1rem;
}

.nav-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 0.75rem;
    padding: 0.5rem 1rem;
    cursor: pointer;
    transition: all 0.2s ease;
    border-radius: 6px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.nav-btn:hover {
    color: var(--text-primary);
}

.nav-btn.active {
    color: var(--accent);
    background: rgba(255, 77, 0, 0.05);
}

/* Sections */
.section {
    display: none;
    animation: fadeIn 0.4s ease;
}

.section.active {
    display: grid;
    grid-template-columns: 350px 1fr;
    gap: 3rem;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Sidebar Controls */
.controls {
    display: flex;
    flex-direction: column;
    gap: 2rem;
    background: var(--bg-card);
    padding: 2rem;
    border-radius: 12px;
    border: 1px solid var(--border-dim);
    backdrop-filter: blur(20px);
}

.section-title {
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-bottom: 1rem;
    display: block;
    letter-spacing: 0.05em;
}

/* Grid Layouts for References/Gallery */
.grid-layout {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 1.5rem;
}

.section-header {
    grid-column: 1 / -1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

.section-header h2 {
    font-size: 1.5rem;
    font-weight: 800;
}

/* Reference Items */
.reference-selector {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
}

.ref-item {
    aspect-ratio: 1;
    background: #000;
    border: 1px solid var(--border-dim);
    border-radius: 4px;
    cursor: pointer;
    overflow: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    opacity: 0.4;
    position: relative;
}

.ref-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.ref-item:hover {
    opacity: 0.7;
    border-color: var(--border-active);
}

.ref-item.active {
    opacity: 1;
    border-color: var(--accent);
    box-shadow: 0 0 20px var(--accent-glow);
    transform: scale(1.05);
}

.delete-btn {
    position: absolute;
    top: 5px;
    right: 5px;
    background: rgba(255,0,0,0.7);
    color: white;
    border: none;
    border-radius: 40%;
    width: 18px;
    height: 18px;
    font-size: 8px;
    cursor: pointer;
    display: none;
}

.ref-item:hover .delete-btn {
    display: block;
}

/* Gallery Items */
.gallery-item {
    background: var(--bg-card);
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid var(--border-dim);
    transition: transform 0.3s ease;
}

.gallery-item img {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
}

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

/* Settings */
.settings-container {
    background: var(--bg-card);
    padding: 3rem;
    border-radius: 16px;
    border: 1px solid var(--border-dim);
    display: flex;
    flex-direction: column;
    gap: 2.5rem;
    max-width: 600px;
}

.setting-item {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.setting-item label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.theme-toggle, .color-picker {
    display: flex;
    gap: 1rem;
}

.theme-btn {
    flex: 1;
    padding: 1rem;
    border: 1px solid var(--border-dim);
    background: rgba(255,255,255,0.05);
    color: var(--text-primary);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.theme-btn.active {
    border-color: var(--accent);
    background: rgba(255, 77, 0, 0.1);
    color: var(--accent);
}

.color-swatch {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid transparent;
    transition: transform 0.2s ease;
}

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

.color-swatch.active {
    border-color: #fff;
    box-shadow: 0 0 15px rgba(255,255,255,0.3);
}

.custom-color-container {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid var(--border-dim);
    position: relative;
    overflow: hidden;
    cursor: pointer;
    background: rgba(255,255,255,0.05);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.custom-color-container:hover {
    transform: scale(1.1);
    border-color: var(--text-secondary);
}

.custom-color-container input[type="color"] {
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    cursor: pointer;
    opacity: 0;
}

.custom-color-container i {
    font-size: 1rem;
    color: var(--text-secondary);
    pointer-events: none;
}

/* Preview Area */
.preview-container {
    background: var(--bg-canvas);
    border: 1px solid var(--border-dim);
    border-radius: 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    box-shadow: inset 0 0 50px rgba(0,0,0,0.5);
    min-height: 500px;
}

.canvas-wrapper {
    position: relative;
    max-width: 90%;
    max-height: 90%;
}

canvas {
    display: block;
    max-width: 100%;
    height: auto;
    border-radius: 4px;
}

.placeholder-text {
    color: var(--text-secondary);
    font-family: var(--font-condensed);
    text-transform: uppercase;
    letter-spacing: 0.2em;
    opacity: 0.3;
}

/* Actions */
.result-actions {
    position: absolute;
    bottom: 2rem;
    right: 2rem;
    display: flex;
    gap: 1rem;
}

.btn-icon {
    background: rgba(255, 255, 255, 0.1);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.1);
    width: 45px;
    height: 45px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    backdrop-filter: blur(10px);
    transition: background 0.2s ease, transform 0.2s ease;
}

.btn-icon:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.1);
}

/* Upload Area */
.upload-box {
    border: 1px dashed var(--border-dim);
    background: rgba(0, 0, 0, 0.3);
    padding: 1.5rem;
    border-radius: 8px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
}

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

#upload-preview {
    width: 100%;
    height: 80px;
    object-fit: cover;
    border-radius: 4px;
    margin-top: 0.5rem;
}

/* Button */
.btn-generate {
    background: var(--accent);
    color: white;
    border: none;
    padding: 0.9rem 1.5rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 700;
    transition: all 0.3s ease;
}

.btn-generate:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* Loading Overlay */
.loading-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.8);
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 10;
}

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

.scan-line {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: linear-gradient(to bottom, transparent, var(--accent), transparent);
    box-shadow: 0 0 20px var(--accent);
    animation: scan 2s linear infinite;
    opacity: 0.8;
}

@keyframes scan {
    0% { top: -10%; }
    100% { top: 110%; }
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s cubic-bezier(0.68, -0.55, 0.265, 1.55) infinite;
    margin-bottom: 1.5rem;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.hidden { display: none !important; }

/* Result Image */
#result-image {
    max-width: 90%;
    max-height: 85%;
    object-fit: contain;
    border-radius: 8px;
}

/* Error Message */
.error-msg {
    background: rgba(255, 50, 50, 0.1);
    border: 1px solid rgba(255, 50, 50, 0.3);
    color: #ff6b6b;
    padding: 0.75rem 1rem;
    border-radius: 6px;
    font-size: 0.8rem;
    margin-bottom: 0.5rem;
}

/* Label sub */
.label-sub {
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-top: 0.5rem;
    opacity: 0.6;
}

/* API Key Row */
.api-key-row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.api-key-row input {
    flex: 1;
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid var(--border-dim);
    color: var(--text-primary);
    padding: 0.75rem 1rem;
    border-radius: 6px;
    font-family: monospace;
    font-size: 0.8rem;
    outline: none;
    transition: border-color 0.2s;
}

.api-key-row input:focus {
    border-color: var(--accent);
}

.btn-icon-sm {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-dim);
    color: var(--text-secondary);
    width: 36px;
    height: 36px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-icon-sm:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
}

.setting-hint {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.setting-hint a {
    color: var(--accent);
    text-decoration: none;
}

.setting-hint a:hover {
    text-decoration: underline;
}

/* Gallery Item Actions */
.gallery-item {
    position: relative;
}

.gallery-actions {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    gap: 0.5rem;
    padding: 0.75rem;
    background: linear-gradient(transparent, rgba(0,0,0,0.8));
    opacity: 0;
    transition: opacity 0.2s;
    justify-content: flex-end;
}

.gallery-item:hover .gallery-actions {
    opacity: 1;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .app-container {
        padding: 1.5rem;
    }
    
    .section.active {
        gap: 2rem;
    }
}

@media (max-width: 900px) {
    header {
        flex-direction: column;
        gap: 1.5rem;
        text-align: center;
        margin-bottom: 2rem;
    }

    .main-nav {
        width: 100%;
        justify-content: center;
        overflow-x: auto;
        padding-bottom: 0.5rem;
        -webkit-overflow-scrolling: touch;
    }

    .nav-btn {
        flex-shrink: 0;
        white-space: nowrap;
    }

    .section.active {
        grid-template-columns: 1fr;
    }

    .controls {
        order: 2;
    }

    .preview-container {
        order: 1;
        min-height: 400px;
    }
}

@media (max-width: 600px) {
    .app-container {
        padding: 1rem;
    }

    .logo {
        font-size: 1rem;
    }

    .nav-btn {
        padding: 0.4rem 0.8rem;
        font-size: 0.7rem;
    }

    .section-header h2 {
        font-size: 1.2rem;
    }

    .grid-layout {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: 1rem;
    }

    .reference-selector {
        grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
    }

    .settings-container {
        padding: 1.5rem;
    }

    .theme-toggle {
        flex-direction: column;
    }

    .preview-container {
        min-height: 300px;
    }

    .result-actions {
        bottom: 1rem;
        right: 1rem;
    }

    .btn-icon {
        width: 40px;
        height: 40px;
    }
}

/* Fix for very small devices */
@media (max-width: 360px) {
    .grid-layout {
        grid-template-columns: 1fr;
    }
    
    .main-nav {
        justify-content: flex-start;
    }
}
