:root {
    /* ── Colors (Dark default) ── */
    --bg-primary: #0f0f23;
    --bg-secondary: #1a1a2e;
    --bg-card: #16162a;
    --bg-elevated: #1e1e3a;
    --text-primary: #e8e8f0;
    --text-secondary: #a0a0b8;
    --text-muted: #6b6b8a;
    --accent-primary: #8b5cf6;
    --accent-secondary: #06b6d4;
    --accent-gradient: linear-gradient(135deg, #8b5cf6, #06b6d4);
    --border-color: rgba(139, 92, 246, 0.2);
    --success: #22c55e;
    --warning: #f59e0b;
    --error: #ef4444;
    --shadow: 0 4px 20px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 40px rgba(0,0,0,0.6);
    --radius: 12px;
    --radius-sm: 8px;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --transition: all 0.2s ease;
}

[data-theme="light"] {
    --bg-primary: #f8f9fc;
    --bg-secondary: #ffffff;
    --bg-card: #ffffff;
    --bg-elevated: #f0f1f5;
    --text-primary: #1a1a2e;
    --text-secondary: #4a4a6a;
    --text-muted: #7a7a9a;
    --accent-primary: #7c3aed;
    --accent-secondary: #0891b2;
    --accent-gradient: linear-gradient(135deg, #7c3aed, #0891b2);
    --border-color: rgba(124, 58, 237, 0.15);
    --success: #16a34a;
    --warning: #d97706;
    --error: #dc2626;
    --shadow: 0 4px 20px rgba(0,0,0,0.08);
    --shadow-lg: 0 8px 40px rgba(0,0,0,0.12);
}

/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; }
body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    transition: background 0.3s ease, color 0.3s ease;
    -webkit-font-smoothing: antialiased;
}

.app { min-height: 100vh; display: flex; flex-direction: column; }

/* ── Header ── */
.app-header {
    position: sticky; top: 0; z-index: 100;
    background: rgba(15,15,35,0.85);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border-color);
    padding: 0.75rem 1.5rem;
    display: flex; align-items: center; justify-content: space-between;
}
[data-theme="light"] .app-header { background: rgba(248,249,252,0.85); }

.header-left, .header-right { display: flex; align-items: center; gap: 1rem; }

.logo {
    display: flex; align-items: center; gap: 0.6rem;
    font-size: 1.25rem; font-weight: 700;
    color: var(--text-primary);
}
.logo svg { width: 28px; height: 28px; stroke: var(--accent-primary); }

.user-badge {
    background: var(--accent-gradient);
    color: white; font-size: 0.8rem; font-weight: 600;
    padding: 0.35rem 0.8rem; border-radius: 20px;
}

.admin-badge {
    background: var(--error);
    color: white; font-size: 0.75rem; font-weight: 600;
    padding: 0.3rem 0.7rem; border-radius: 20px;
}

.theme-toggle {
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.75rem;
    cursor: pointer; color: var(--text-primary);
    font-size: 1rem; transition: var(--transition);
}
.theme-toggle:hover { background: var(--accent-primary); color: white; }
.theme-toggle .theme-icon-dark { display: inline; }
.theme-toggle .theme-icon-light { display: none; }
[data-theme="light"] .theme-toggle .theme-icon-dark { display: none; }
[data-theme="light"] .theme-toggle .theme-icon-light { display: inline; }

/* ── Buttons ── */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
    padding: 0.6rem 1.2rem; border-radius: var(--radius-sm);
    font-size: 0.9rem; font-weight: 600; text-decoration: none;
    border: none; cursor: pointer; transition: var(--transition);
    white-space: nowrap;
}
.btn-primary {
    background: var(--accent-gradient);
    color: white; box-shadow: 0 2px 12px rgba(139,92,246,0.3);
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 4px 20px rgba(139,92,246,0.4); }
.btn-secondary {
    background: var(--bg-elevated); color: var(--text-primary);
    border: 1px solid var(--border-color);
}
.btn-secondary:hover { background: var(--accent-primary); color: white; }
.btn-outline {
    background: transparent; color: var(--text-secondary);
    border: 1px solid var(--border-color);
}
.btn-outline:hover { border-color: var(--accent-primary); color: var(--accent-primary); }
.btn-danger { background: var(--error); color: white; }
.btn-danger:hover { background: #b91c1c; }
.btn-small { padding: 0.35rem 0.75rem; font-size: 0.8rem; }
.btn-large { padding: 0.85rem 1.5rem; font-size: 1rem; }

/* ── Login Page ── */
.login-page {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    padding: 1rem;
    background: radial-gradient(ellipse at top, var(--bg-secondary), var(--bg-primary));
}
.login-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 2.5rem; width: 100%; max-width: 420px;
    box-shadow: var(--shadow-lg);
}
.login-logo { text-align: center; margin-bottom: 2rem; }
.login-logo svg { width: 48px; height: 48px; stroke: var(--accent-primary); margin-bottom: 0.5rem; }
.login-logo h1 { font-size: 1.5rem; font-weight: 700; margin-bottom: 0.3rem; }
.login-logo p { color: var(--text-secondary); font-size: 0.9rem; }

.login-form { display: flex; flex-direction: column; gap: 1.25rem; }

.form-group { display: flex; flex-direction: column; gap: 0.4rem; }
.form-group label {
    font-size: 0.85rem; font-weight: 600; color: var(--text-secondary);
    text-transform: uppercase; letter-spacing: 0.03em;
}
.form-group input, .form-group textarea, .form-group select {
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.75rem 1rem;
    color: var(--text-primary); font-size: 0.95rem; font-family: var(--font);
    transition: var(--transition);
}
.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
    outline: none; border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(139,92,246,0.1);
}
.form-group textarea { resize: vertical; }

.alert {
    padding: 0.75rem 1rem; border-radius: var(--radius-sm);
    font-size: 0.9rem; font-weight: 500;
}
.alert-error { background: rgba(239,68,68,0.1); color: var(--error); border: 1px solid rgba(239,68,68,0.2); }

.login-info { text-align: center; margin-top: 1.5rem; }
.login-info p { color: var(--text-muted); font-size: 0.8rem; }

/* ── Tabs ── */
.tab-bar {
    display: flex; gap: 0.5rem;
    padding: 1rem 1.5rem 0;
    border-bottom: 1px solid var(--border-color);
}
.tab-btn {
    background: transparent; border: none;
    color: var(--text-muted); font-size: 0.95rem; font-weight: 600;
    padding: 0.75rem 1.25rem; cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: var(--transition); position: relative;
}
.tab-btn:hover { color: var(--text-secondary); }
.tab-btn.active { color: var(--accent-primary); border-bottom-color: var(--accent-primary); }

.tab-panel { display: none; padding: 1.5rem; }
.tab-panel.active { display: block; }

/* ── Generate Layout ── */
.generate-layout {
    display: grid; grid-template-columns: minmax(380px, 420px) 1fr; gap: 1.5rem;
    max-width: 1400px; margin: 0 auto;
}

.prompt-textarea {
    min-height: 120px;
    resize: vertical;
}

.control-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
}
.control-card h2 { font-size: 1.1rem; font-weight: 700; margin-bottom: 1.25rem; }
.control-card h3 { font-size: 1rem; font-weight: 600; margin-bottom: 0.75rem; color: var(--text-secondary); }

.mode-select { width: 100%; cursor: pointer; }

/* Upload Zone */
.upload-zone {
    border: 2px dashed var(--border-color);
    border-radius: var(--radius-sm);
    padding: 1.5rem; text-align: center;
    transition: var(--transition); cursor: pointer;
    background: var(--bg-elevated);
}
.upload-zone.dragover { border-color: var(--accent-primary); background: rgba(139,92,246,0.05); }
.upload-placeholder { display: block; }
.upload-icon { font-size: 2rem; margin-bottom: 0.5rem; }
.upload-placeholder p { color: var(--text-secondary); font-size: 0.85rem; }
.upload-link { color: var(--accent-primary); text-decoration: underline; cursor: pointer; }

.upload-preview { display: none; position: relative; }
.upload-preview img { max-width: 100%; border-radius: var(--radius-sm); }
.upload-preview.active { display: block; }
.upload-remove {
    position: absolute; top: -8px; right: -8px;
    background: var(--error); color: white;
    border: none; border-radius: 50%; width: 28px; height: 28px;
    cursor: pointer; font-size: 0.75rem;
}

/* Reference Grid */
.ref-grid {
    display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.5rem;
    max-height: 160px; overflow-y: auto;
}
.ref-item {
    aspect-ratio: 1; border-radius: var(--radius-sm);
    overflow: hidden; cursor: pointer;
    border: 2px solid transparent; transition: var(--transition);
}
.ref-item img { width: 100%; height: 100%; object-fit: cover; }
.ref-item:hover { border-color: var(--accent-primary); }
.ref-item.selected { border-color: var(--accent-secondary); }

/* Queue */
.queue-card { margin-top: 1rem; }
.queue-status { font-size: 0.85rem; }
.queue-empty { color: var(--text-muted); }
.queue-item {
    display: flex; align-items: center; gap: 0.5rem;
    padding: 0.5rem 0; border-bottom: 1px solid var(--border-color);
}
.queue-item:last-child { border-bottom: none; }
.queue-badge {
    font-size: 0.7rem; font-weight: 700; text-transform: uppercase;
    padding: 0.15rem 0.4rem; border-radius: 4px;
}
.queue-badge.txt2img { background: rgba(139,92,246,0.15); color: var(--accent-primary); }
.queue-badge.img2img { background: rgba(6,182,212,0.15); color: var(--accent-secondary); }
.queue-badge.txt2-3d { background: rgba(34,197,94,0.15); color: var(--success); }
.queue-badge.img2-3d { background: rgba(245,158,11,0.15); color: var(--warning); }
.queue-badge.processing { background: var(--accent-gradient); color: white; animation: pulse 1.5s infinite; }

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

/* Result Area */
.generate-result { position: sticky; top: 80px; }
.result-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    min-height: 500px;
}

.result-placeholder {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    min-height: 400px; text-align: center;
}
.placeholder-icon { font-size: 4rem; margin-bottom: 1rem; opacity: 0.5; }
.result-placeholder p { color: var(--text-secondary); font-size: 1.1rem; }
.placeholder-hint { color: var(--text-muted); font-size: 0.85rem; margin-top: 0.5rem; }

.result-loading {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    min-height: 400px;
}
.spinner {
    width: 48px; height: 48px;
    border: 3px solid var(--border-color);
    border-top-color: var(--accent-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.progress-bar {
    width: 200px; height: 4px;
    background: var(--bg-elevated); border-radius: 2px;
    margin-top: 1rem; overflow: hidden;
}
.progress-fill {
    height: 100%; background: var(--accent-gradient);
    border-radius: 2px; width: 0%;
    animation: progress 8s ease-in-out infinite;
}
@keyframes progress {
    0% { width: 0%; }
    50% { width: 70%; }
    100% { width: 95%; }
}

.result-content { animation: fadeIn 0.3s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

.result-media {
    background: var(--bg-elevated); border-radius: var(--radius-sm);
    overflow: hidden; margin-bottom: 1rem;
    display: flex; align-items: center; justify-content: center;
    min-height: 300px;
}
.result-media img { max-width: 100%; max-height: 500px; object-fit: contain; }

.result-meta { display: flex; flex-direction: column; gap: 0.75rem; }
.result-prompt-box {
    background: var(--bg-elevated); border-radius: var(--radius-sm);
    padding: 1rem;
}
.result-prompt-box label { font-size: 0.75rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; }
.prompt-text { color: var(--text-primary); margin: 0.5rem 0; font-size: 0.9rem; line-height: 1.5; }

/* ── Gallery ── */
.gallery-header {
    display: flex; align-items: center; justify-content: space-between;
    flex-wrap: wrap; gap: 1rem; margin-bottom: 1.5rem;
}
.gallery-header h2 { font-size: 1.25rem; }
.gallery-filters { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.filter-select {
    background: var(--bg-elevated); border: 1px solid var(--border-color);
    border-radius: var(--radius-sm); padding: 0.5rem 0.75rem;
    color: var(--text-primary); font-size: 0.85rem; cursor: pointer;
}

.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 1rem;
}
.gallery-item {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    overflow: hidden;
    cursor: pointer;
    transition: var(--transition);
    box-shadow: var(--shadow);
}
.gallery-item:hover { transform: translateY(-4px); box-shadow: var(--shadow-lg); border-color: var(--accent-primary); }
.gallery-thumb {
    aspect-ratio: 1; background: var(--bg-elevated);
    display: flex; align-items: center; justify-content: center;
    overflow: hidden;
}
.gallery-thumb img { width: 100%; height: 100%; object-fit: cover; transition: transform 0.3s ease; }
.gallery-item:hover .gallery-thumb img { transform: scale(1.05); }
.gallery-thumb .thumb-3d { font-size: 3rem; }
.gallery-info { padding: 0.75rem 1rem; }
.gallery-author { font-size: 0.8rem; font-weight: 600; color: var(--accent-primary); }
.gallery-mode { font-size: 0.7rem; color: var(--text-muted); margin-top: 0.2rem; }

.gallery-empty {
    grid-column: 1 / -1;
    text-align: center; padding: 4rem 2rem;
    color: var(--text-muted);
}
.empty-icon { font-size: 3rem; margin-bottom: 0.5rem; opacity: 0.5; }

/* ── Lightbox ── */
.lightbox {
    position: fixed; inset: 0; z-index: 1000;
    background: rgba(0,0,0,0.92);
    display: flex; align-items: center; justify-content: center;
    padding: 2rem;
    animation: fadeIn 0.2s ease;
}
.lightbox-close {
    position: absolute; top: 1rem; right: 1rem;
    background: rgba(255,255,255,0.1); color: white;
    border: none; border-radius: 50%; width: 40px; height: 40px;
    font-size: 1.2rem; cursor: pointer; transition: var(--transition);
}
.lightbox-close:hover { background: rgba(255,255,255,0.2); }
.lightbox-content {
    display: grid; grid-template-columns: 1fr 320px; gap: 2rem;
    max-width: 1200px; width: 100%; max-height: 90vh;
}
.lightbox-media {
    background: var(--bg-elevated); border-radius: var(--radius);
    overflow: hidden; display: flex; align-items: center; justify-content: center;
    min-height: 400px;
}
.lightbox-media img { max-width: 100%; max-height: 70vh; object-fit: contain; }
.lightbox-info {
    background: var(--bg-card); border-radius: var(--radius);
    padding: 1.5rem; display: flex; flex-direction: column; gap: 1rem;
    overflow-y: auto;
}
.lightbox-author { font-size: 1rem; font-weight: 700; color: var(--accent-primary); }
.lightbox-mode { font-size: 0.8rem; color: var(--text-muted); }
.lightbox-prompt-box {
    display: flex; flex-direction: column;
    max-height: 180px;
}
.lightbox-prompt {
    color: var(--text-primary); margin: 0.5rem 0; line-height: 1.5;
    max-height: 150px;
    overflow-y: auto;
    word-wrap: break-word;
    padding-right: 8px;
}
.lightbox-prompt::-webkit-scrollbar {
    width: 6px;
}
.lightbox-prompt::-webkit-scrollbar-track {
    background: transparent;
}
.lightbox-prompt::-webkit-scrollbar-thumb {
    background: var(--border-color); border-radius: 3px;
}
.lightbox-prompt::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}
.lightbox-date { font-size: 0.8rem; color: var(--text-muted); margin-top: auto; }

/* ── Responsive ── */
@media (max-width: 1024px) {
    .generate-layout { grid-template-columns: 1fr; }
    .generate-result { position: static; }
    .lightbox-content { grid-template-columns: 1fr; }
    .admin-layout { grid-template-columns: 1fr; }
}

/* ── Admin Page ── */
.admin-page { padding: 1.5rem; max-width: 900px; margin: 0 auto; }

.admin-header {
    text-align: center; margin-bottom: 2rem;
}
.admin-header h1 { font-size: 1.8rem; margin-bottom: 0.5rem; }
.admin-header p { color: var(--text-muted); }

.admin-layout {
    display: flex; flex-direction: column; gap: 1.5rem;
}

.create-card { max-width: 600px; margin: 0 auto; width: 100%; }
.codes-card { width: 100%; }

.code-input-group {
    display: flex; gap: 0.5rem;
}
.code-input-group input { flex: 1; }
.code-input-group button { white-space: nowrap; }

.hours-selector {
    display: flex; align-items: center; gap: 1rem;
    padding: 0.5rem 0;
}
.hours-selector input[type="range"] {
    flex: 1;
    -webkit-appearance: none; appearance: none;
    height: 6px; background: var(--bg-elevated); border-radius: 3px;
}
.hours-selector input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none;
    width: 18px; height: 18px; background: var(--accent-primary);
    border-radius: 50%; cursor: pointer;
}
.hours-display {
    font-weight: 700; color: var(--accent-primary); min-width: 100px; text-align: right;
}

.form-hint {
    color: var(--text-muted); font-size: 0.8rem; margin-top: 0.2rem;
}

.codes-header {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 1rem;
}
.codes-list { display: flex; flex-direction: column; gap: 0.75rem; }

.code-item {
    display: flex; align-items: center; justify-content: space-between;
    padding: 1rem; background: var(--bg-elevated);
    border: 1px solid var(--border-color); border-radius: var(--radius-sm);
    transition: var(--transition);
}
.code-item:hover { border-color: var(--accent-primary); }
.code-item.expired { opacity: 0.5; }

.code-value {
    font-size: 1.1rem; font-weight: 700; font-family: monospace;
    letter-spacing: 0.05em; color: var(--text-primary);
}
.code-meta {
    display: flex; gap: 0.75rem; margin-top: 0.3rem; flex-wrap: wrap;
}
.code-status { font-size: 0.75rem; font-weight: 600; padding: 0.2rem 0.5rem; border-radius: 4px; }
.code-status.active { background: rgba(34,197,94,0.15); color: var(--success); }
.code-status.expired { background: rgba(239,68,68,0.15); color: var(--error); }
.code-time { font-size: 0.8rem; color: var(--text-muted); }

.codes-loading, .codes-empty {
    text-align: center; padding: 2rem; color: var(--text-muted);
}
.empty-icon { font-size: 2rem; margin-bottom: 0.5rem; opacity: 0.5; }
.empty-hint { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.3rem; }

/* ── Responsive ── */
@media (max-width: 1024px) {
    .generate-layout { grid-template-columns: 1fr; }
    .generate-result { position: static; }
    .lightbox-content { grid-template-columns: 1fr; }
    .admin-layout { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
    .app-header { padding: 0.5rem 1rem; }
    .logo span { display: none; }
    .tab-btn { padding: 0.6rem 0.8rem; font-size: 0.85rem; }
    .tab-panel { padding: 1rem; }
    .gallery-grid { grid-template-columns: repeat(2, 1fr); }
    .gallery-header { flex-direction: column; align-items: flex-start; }
    .login-card { padding: 1.5rem; }
    .admin-page { padding: 1rem; }
    .code-item { flex-direction: column; align-items: flex-start; gap: 0.5rem; }
}

@media (max-width: 480px) {
    .gallery-grid { grid-template-columns: 1fr; }
    .ref-grid { grid-template-columns: repeat(3, 1fr); }
    .control-card { padding: 1rem; }
    .code-input-group { flex-direction: column; }
    .code-input-group button { width: 100%; }
}
