/* Design system tokens & CSS variables */
:root {
    --bg-app: #0b0c13;
    --bg-panel: rgba(17, 18, 30, 0.7);
    --bg-card: rgba(26, 28, 46, 0.5);
    --border-color: rgba(255, 255, 255, 0.08);
    --text-primary: #f1f2f6;
    --text-secondary: #a4b0be;
    --primary-glow: rgba(108, 92, 231, 0.3);
    
    --primary: #7f5af0;
    --primary-hover: #9370db;
    --secondary: #2cb67d;
    --secondary-hover: #239063;
    --danger: #ff5e57;
    --danger-hover: #ff3838;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
}

body {
    background-color: var(--bg-app);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
}

/* App Layout */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* Header */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border-color);
    background: rgba(11, 12, 19, 0.9);
    backdrop-filter: blur(10px);
    z-index: 10;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-logo {
    font-size: 28px;
    color: var(--primary);
    text-shadow: 0 0 10px var(--primary-glow);
    animation: logoGlow 4s infinite alternate;
}

.brand-text h1 {
    font-size: 20px;
    font-weight: 600;
    letter-spacing: 0.5px;
    background: linear-gradient(135deg, #fff 30%, var(--primary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.brand-text p {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 2px;
}

.server-status {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, 0.04);
    padding: 6px 12px;
    border-radius: 20px;
    border: 1px solid var(--border-color);
    font-size: 13px;
    font-weight: 500;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

.status-dot.red {
    background-color: var(--danger);
    box-shadow: 0 0 8px var(--danger);
}

.status-dot.green {
    background-color: var(--secondary);
    box-shadow: 0 0 8px var(--secondary);
}

/* Workspace */
.app-workspace {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Panels */
.panel {
    display: flex;
    flex-direction: column;
    background: var(--bg-panel);
    border-right: 1px solid var(--border-color);
    overflow: hidden;
}

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

.panel-header h2 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.badge {
    background: var(--primary);
    color: #fff;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 700;
}

.panel-body {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.panel-body::-webkit-scrollbar {
    width: 6px;
}
.panel-body::-webkit-scrollbar-thumb {
    background-color: rgba(255, 255, 255, 0.05);
    border-radius: 3px;
}

/* Left Panel */
.left-panel {
    width: 320px;
    min-width: 300px;
}

/* Center Panel (Canvas) */
.center-panel {
    flex: 1;
    background: #06070a;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

/* Right Panel */
.right-panel {
    width: 380px;
    min-width: 350px;
    border-right: none;
    border-left: 1px solid var(--border-color);
}

/* Cards & Forms */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 16px;
    backdrop-filter: blur(8px);
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
}

.form-group label {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
}

.label-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

.label-row label, .label-row span {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
}

/* Inputs styling */
.custom-textarea {
    width: 100%;
    height: 100px;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 12px;
    color: var(--text-primary);
    font-size: 14px;
    resize: none;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.custom-textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 8px var(--primary-glow);
}

.custom-input {
    width: 100%;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 12px;
    color: var(--text-primary);
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
}

.custom-input:focus {
    border-color: var(--primary);
}

/* Custom Slider */
.custom-slider {
    -webkit-appearance: none;
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.1);
    outline: none;
    margin: 8px 0;
}

.custom-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
    box-shadow: 0 0 6px var(--primary-glow);
    transition: transform 0.1s;
}

.custom-slider::-webkit-slider-thumb:hover {
    transform: scale(1.2);
}

/* Upload Dropzone */
.dropzone {
    border: 2px dashed var(--border-color);
    border-radius: 12px;
    padding: 24px 16px;
    text-align: center;
    position: relative;
    cursor: pointer;
    background: rgba(255, 255, 255, 0.01);
    transition: background 0.2s, border-color 0.2s;
}

.dropzone:hover {
    background: rgba(255, 255, 255, 0.03);
    border-color: var(--primary);
}

.file-input {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
}

.upload-icon {
    font-size: 32px;
    color: var(--primary);
    display: block;
    margin-bottom: 8px;
}

.dropzone-text .primary-text {
    font-size: 14px;
    font-weight: 500;
}

.dropzone-text .secondary-text {
    font-size: 11px;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* Images Grid */
.images-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    width: 100%;
}

.grid-item {
    position: relative;
    aspect-ratio: 1;
    border-radius: 8px;
    overflow: hidden;
    border: 2px solid transparent;
    cursor: pointer;
    background: rgba(0, 0, 0, 0.3);
}

.grid-item.active {
    border-color: var(--primary);
}

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

.grid-item .badge-index {
    position: absolute;
    top: 4px;
    left: 4px;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 600;
}

.grid-item .btn-remove {
    position: absolute;
    top: 4px;
    right: 4px;
    background: rgba(255, 94, 87, 0.8);
    color: #fff;
    border: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 10px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Inpaint controls */
.inpaint-toggle-header {
    display: flex;
    align-items: center;
    gap: 12px;
}

.inpaint-tools {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 14px;
    transition: opacity 0.2s;
}

.inpaint-tools.disabled {
    opacity: 0.3;
    pointer-events: none;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 20px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-switch .slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.1);
    transition: .3s;
}

.toggle-switch .slider:before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .3s;
}

.toggle-switch input:checked + .slider {
    background-color: var(--primary);
}

.toggle-switch input:checked + .slider:before {
    transform: translateX(16px);
}

.toggle-switch .slider.round {
    border-radius: 20px;
}

.toggle-switch .slider.round:before {
    border-radius: 50%;
}

.tool-buttons {
    display: flex;
    gap: 8px;
}

/* Canvas Workstation */
.canvas-wrapper {
    width: 90%;
    height: 90%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.canvas-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    color: var(--text-secondary);
}

.placeholder-icon {
    font-size: 64px;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.05);
}

.canvas-container {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    border: 1px solid var(--border-color);
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    border-radius: 8px;
    overflow: hidden;
    cursor: crosshair;
}

#imageCanvas {
    display: block;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

#maskCanvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0.55;
    pointer-events: none;
}

/* Brush Cursor Overlay */
.brush-cursor {
    position: absolute;
    border: 1px solid #fff;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    pointer-events: none;
    transform: translate(-50%, -50%);
    display: none;
}

/* Model Button swapper */
.model-toggle {
    display: flex;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--border-color);
    padding: 4px;
    border-radius: 8px;
    width: 100%;
}

.model-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 8px 12px;
    font-size: 13px;
    font-weight: 600;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

.model-btn.active {
    background: var(--primary);
    color: #fff;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 16px;
    font-size: 14px;
    font-weight: 600;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
    outline: none;
    text-decoration: none;
}

.btn:active {
    transform: scale(0.98);
}

.btn-primary {
    background-color: var(--primary);
    color: #fff;
}

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

.btn-secondary {
    background-color: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background-color: rgba(255, 255, 255, 0.08);
}

.btn-danger {
    background-color: rgba(255, 94, 87, 0.15);
    color: var(--danger);
    border: 1px solid rgba(255, 94, 87, 0.2);
}

.btn-danger:hover {
    background-color: rgba(255, 94, 87, 0.25);
}

.btn-large {
    width: 100%;
    padding: 12px 20px;
    font-size: 15px;
}

.btn-sm {
    padding: 6px 10px;
    font-size: 11px;
    border-radius: 6px;
}

.btn-glow {
    box-shadow: 0 0 16px var(--primary-glow);
}

.btn-glow:hover {
    box-shadow: 0 0 24px var(--primary);
}

/* Accordion */
.accordion-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
}

.accordion-arrow {
    transition: transform 0.2s;
}

.accordion-content {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease-out, margin-top 0.2s;
}

.accordion-content.open {
    max-height: 500px;
    margin-top: 16px;
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

/* Loading Overlay */
.loading-overlay {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    background: rgba(11, 12, 19, 0.85);
    backdrop-filter: blur(8px);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    padding: 40px 20px;
    width: 100%;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255, 255, 255, 0.1);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 1s infinite linear;
}

.loading-text {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
}

/* Results & Comparison slider widget */
.result-box {
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 100%;
}

.result-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.result-header h3 {
    font-size: 15px;
    font-weight: 600;
}

/* Before / After Slider Widget */
.comparison-container {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    border: 1px solid var(--border-color);
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    border-radius: 8px;
    overflow: hidden;
    user-select: none;
    cursor: ew-resize;
    display: none;
}

.comparison-container img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: fill;
    pointer-events: none;
}

.comparison-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 100%;
    overflow: hidden;
    border-right: 2px solid #fff;
    box-shadow: 0 0 10px rgba(0,0,0,0.5);
    z-index: 2;
}

.comparison-overlay img {
    position: absolute;
    top: 0;
    left: 0;
    max-width: none;
    object-fit: fill;
}

.comparison-handle {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 36px;
    height: 36px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
    z-index: 3;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.comparison-handle::before, .comparison-handle::after {
    content: "";
    position: absolute;
    width: 0;
    height: 0;
    border: 5px solid transparent;
}

.comparison-handle::before {
    border-right-color: #333;
    left: 5px;
}

.comparison-handle::after {
    border-left-color: #333;
    right: 5px;
}

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

@keyframes logoGlow {
    from { text-shadow: 0 0 5px var(--primary-glow); }
    to { text-shadow: 0 0 15px var(--primary); }
}

/* ==========================================================================
   Responsive & Mobile Adaptations
   ========================================================================== */

/* Stack panel layout on tablets/mobile devices */
@media (max-width: 900px) {
    .app-workspace {
        flex-direction: column !important;
        overflow-y: auto !important;
    }

    .panel {
        width: 100% !important;
        min-width: 0 !important;
        max-width: none !important;
        height: auto !important;
        min-height: 0 !important;
        border-right: none !important;
        border-left: none !important;
        border-bottom: 1px solid var(--border-color);
        flex: none !important;
        overflow: visible !important;
    }

    .panel-body {
        height: auto !important;
        overflow: visible !important;
        flex: none !important;
        padding: 16px !important;
        gap: 16px !important;
    }

    /* Stack panels: Canvas/Editor on top, then Gallery, then Settings */
    .center-panel {
        order: 1 !important;
        height: 55vh !important;
        min-height: 360px !important;
        max-height: 500px !important;
        background: #06070a !important;
        position: relative !important;
        border-bottom: 1px solid var(--border-color) !important;
    }

    .left-panel {
        order: 2 !important;
    }

    .right-panel {
        order: 3 !important;
        border-bottom: none !important;
    }
}

/* Header adaptation for narrow screens */
@media (max-width: 650px) {
    .app-header {
        flex-wrap: wrap !important;
        gap: 12px !important;
        padding: 12px 16px !important;
    }

    .brand {
        flex: 1 1 auto !important;
    }

    .brand-text p {
        display: none !important; /* Hide subtitle to save vertical screen space */
    }

    .mode-container {
        order: 3 !important;
        width: 100% !important;
        margin-top: 4px !important;
        justify-content: center !important;
    }

    .mode-toggle-group {
        width: 100% !important;
        justify-content: center !important;
    }

    .mode-toggle-group .model-btn {
        flex: 1 !important;
        text-align: center !important;
    }

    .server-status {
        flex: 0 0 auto !important;
    }
}

/* Form parameters stack on small mobile screens */
@media (max-width: 480px) {
    .form-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    .panel-body {
        padding: 12px !important;
    }
    
    .dropzone {
        padding: 24px 16px !important;
    }
    
    .dropzone-text p.secondary-text {
        font-size: 11px !important;
    }
}
