/* --- Theming System --- */
:root {
    /* Default Dark Theme */
    --bg-color: #09090b;
    --surface-color: #18181b;
    --surface-hover: #27272a;
    --border-color: #3f3f46;
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --text-main: #f4f4f5;
    --text-muted: #a1a1aa;
    --success: #10b981;
    --error: #ef4444;
    --radius: 12px;
    --font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --preview-bg: #000;
    --modal-bg: #000;
}

/* Automatic Light Theme Detection */
@media (prefers-color-scheme: light) {
    :root {
        --bg-color: #ffffff;
        --surface-color: #f4f4f5;
        --surface-hover: #e4e4e7;
        --border-color: #d4d4d8;
        --text-main: #18181b;
        --text-muted: #71717a;
        --preview-bg: #e4e4e7; /* Slightly darker for canvas contrast */
        --modal-bg: #18181b; /* Keep modal dark for focus */
    }
}

/* --- Reset & Base --- */
* { box-sizing: border-box; margin: 0; padding: 0; outline: none; -webkit-tap-highlight-color: transparent; }

body {
    background-color: var(--bg-color);
    color: var(--text-main);
    font-family: var(--font-family);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: background-color 0.3s, color 0.3s;
}

/* --- Header --- */
header {
    height: 64px;
    padding: 0 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-color);
    flex-shrink: 0;
    z-index: 20;
}

.logo {
    font-size: 1.5rem;
    font-weight: 800;
    letter-spacing: -0.05em;
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-main);
}

.logo svg {
    stroke: var(--primary-color);
}

.api-status {
    font-size: 0.75rem;
    color: var(--success);
    background: rgba(16, 185, 129, 0.1);
    padding: 4px 12px;
    border-radius: 20px;
    border: 1px solid rgba(16, 185, 129, 0.2);
    font-weight: 500;
}

/* --- Main Layout --- */
main {
    display: flex;
    flex: 1;
    height: calc(100vh - 64px);
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 360px;
    background-color: var(--bg-color);
    border-right: 1px solid var(--border-color);
    padding: 1.5rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    flex-shrink: 0;
}

/* Preview Area */
.preview-area {
    flex: 1;
    background-color: var(--preview-bg);
    /* Checkerboard pattern */
    background-image: 
        linear-gradient(45deg, rgba(128,128,128,0.1) 25%, transparent 25%), 
        linear-gradient(-45deg, rgba(128,128,128,0.1) 25%, transparent 25%), 
        linear-gradient(45deg, transparent 75%, rgba(128,128,128,0.1) 75%), 
        linear-gradient(-45deg, transparent 75%, rgba(128,128,128,0.1) 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    padding: 2rem;
    min-width: 0; /* Flexbox fix */
}

/* --- Controls --- */
.control-group { display: flex; flex-direction: column; gap: 0.5rem; }

label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    display: flex;
    justify-content: space-between;
}

.value-display { color: var(--text-main); font-family: monospace; }

input[type="text"], input[type="number"], textarea, select {
    background-color: var(--surface-color);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    padding: 0.75rem;
    border-radius: 8px;
    font-family: inherit;
    font-size: 0.9rem;
    transition: 0.2s;
    width: 100%;
}

textarea { min-height: 80px; resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: var(--primary-color); box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1); }

.row { display: flex; gap: 1rem; }
.row > * { flex: 1; }

/* Range Slider */
input[type="range"] {
    -webkit-appearance: none;
    width: 100%;
    height: 6px;
    background: var(--surface-hover);
    border-radius: 3px;
    margin-top: 8px;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
    margin-top: -6px;
    box-shadow: 0 0 0 2px var(--bg-color);
    transition: transform 0.1s;
}
input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.1); }

/* Generate Button */
button#generate-btn {
    background: linear-gradient(135deg, var(--primary-color), #818cf8);
    color: white;
    border: none;
    padding: 1rem;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    margin-top: auto;
    transition: opacity 0.2s, transform 0.1s;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.25);
}
button#generate-btn:hover { opacity: 0.9; }
button#generate-btn:active { transform: scale(0.98); }
button#generate-btn:disabled { background: var(--surface-hover); color: var(--text-muted); cursor: not-allowed; box-shadow: none; }

/* --- Image Preview --- */
.image-wrapper {
    max-width: 100%;
    max-height: 100%;
    position: relative;
    box-shadow: 0 20px 50px rgba(0,0,0,0.3);
    border-radius: 8px;
    overflow: hidden;
    cursor: zoom-in;
    transition: transform 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.image-wrapper:hover { transform: scale(1.01); }

.image-wrapper img {
    display: block;
    max-width: 100%;
    max-height: 80vh;
    object-fit: contain;
}

.placeholder {
    position: absolute;
    color: var(--text-muted);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    pointer-events: none;
    background: var(--surface-color);
    padding: 2rem;
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 20px rgba(0,0,0,0.05);
}

.loader {
    display: none;
    width: 48px;
    height: 48px;
    border: 4px solid var(--surface-hover);
    border-bottom-color: var(--primary-color);
    border-radius: 50%;
    animation: rotation 1s linear infinite;
    position: absolute;
    z-index: 10;
}
@keyframes rotation { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

.hint-text {
    position: absolute;
    bottom: 20px;
    background: rgba(0,0,0,0.7);
    color: white;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 0.8rem;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
    backdrop-filter: blur(4px);
}
.image-wrapper:hover + .hint-text, .image-wrapper.loaded + .hint-text { opacity: 1; }

/* --- Modal / Editor --- */
.modal {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: var(--modal-bg);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}
.modal.active { opacity: 1; pointer-events: auto; }

.modal-header {
    height: 64px;
    padding: 0 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--bg-color);
    border-bottom: 1px solid var(--border-color);
    color: var(--text-main);
}

.modal-body {
    flex: 1;
    position: relative;
    background: #000; /* Always dark for image editing context */
    overflow: hidden;
}

.modal-footer {
    height: auto;
    min-height: 120px;
    background: var(--bg-color);
    border-top: 1px solid var(--border-color);
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.editor-controls {
    display: flex;
    gap: 2rem;
    justify-content: center;
    flex-wrap: wrap;
}

.slider-group {
    display: flex;
    flex-direction: column;
    gap: 5px;
    width: 120px;
}
.slider-group label { font-size: 0.75rem; color: var(--text-muted); }

.tool-btns {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 0.5rem;
}

.btn {
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    padding: 0.6rem 1rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: 0.2s;
}
.btn:hover { background: var(--surface-hover); }
.btn-primary { background: var(--primary-color); border-color: var(--primary-color); color: white; }
.btn-primary:hover { background: var(--primary-hover); }

/* --- Toast Notifications --- */
.toast-container {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 10px;
    pointer-events: none;
}

.toast {
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    padding: 12px 20px;
    border-radius: 8px;
    font-size: 0.9rem;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    display: flex;
    align-items: center;
    gap: 10px;
    animation: slideUp 0.3s ease forwards;
    pointer-events: auto;
}

.toast.error { border-left: 4px solid var(--error); }
.toast.success { border-left: 4px solid var(--success); }

@keyframes slideUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Cropper Customization */
.cropper-view-box, .cropper-face { border-radius: 0; }
.cropper-modal { opacity: 0.8; background-color: #000; }

/* --- Responsive --- */
@media (max-width: 900px) {
    main { flex-direction: column; overflow-y: auto; height: auto; }
    .sidebar { width: 100%; height: auto; border-right: none; border-bottom: 1px solid var(--border-color); }
    .preview-area { min-height: 400px; }
    .modal-footer { overflow-y: auto; }
}