/* ============================================================================
   UNIFIED DESIGN SYSTEM - CLEAN DARK THEME
   Eye-friendly, modern, and professional
   ============================================================================ */

/* ============================================================================
   DESIGN TOKENS
   ============================================================================ */

:root {
    /* Colors - Eye-friendly dark theme (not pure black) */
    --bg-primary: #0f0f0f;
    --bg-secondary: #1a1a1a;
    --bg-tertiary: #252525;
    --bg-elevated: #2a2a2a;
    --bg-hover: #333333;
    
    /* Accent Colors — Preach Agency brand orange */
    --accent-magenta: #f1ae38;
    --accent-magenta-light: #f5c462;
    --accent-magenta-dark: #d99b2e;
    --accent-orange: #f1ae38;
    --accent-orange-light: #f5c462;
    --accent-orange-dark: #d99b2e;
    
    /* Text Colors - High contrast for readability */
    --text-primary: #ffffff;
    --text-secondary: #d1d5db;
    --text-tertiary: #9ca3af;
    --text-disabled: #6b7280;
    
    /* Border Colors */
    --border-subtle: rgba(255, 255, 255, 0.08);
    --border-default: rgba(255, 255, 255, 0.12);
    --border-hover: rgba(255, 255, 255, 0.2);
    --border-accent: rgba(241, 174, 56, 0.3);
    
    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.5);
    --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.6);
    --shadow-glow-magenta: 0 0 20px rgba(241, 174, 56, 0.2);
    --shadow-glow-orange: 0 0 20px rgba(241, 174, 56, 0.2);
    
    /* Spacing */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 16px;
    --space-lg: 24px;
    --space-xl: 32px;
    --space-2xl: 48px;
    
    /* Border Radius */
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    
    /* Typography */
    --font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
    --font-size-xs: 12px;
    --font-size-sm: 14px;
    --font-size-base: 16px;
    --font-size-lg: 18px;
    --font-size-xl: 20px;
    --font-size-2xl: 24px;
    --font-size-3xl: 32px;
    
    /* Transitions */
    --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-base: 250ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 350ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* ============================================================================
   RESET & BASE
   ============================================================================ */

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

html {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

body {
    font-family: var(--font-family);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
    margin: 0;
    padding: 0;
}

/* ============================================================================
   LAYOUT
   ============================================================================ */

/* ── Always-open sidebar ── */
.app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 220px;
    background: var(--bg-secondary, #1a1a1a);
    border-right: 1px solid var(--border-subtle);
    z-index: 900;
    display: flex;
    flex-direction: column;
    padding: var(--space-xl) 0;
    box-sizing: border-box;
    overflow-y: auto;
}

/* Page content sits to the right of the sidebar */
.app-container {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    width: calc(100% - 220px);
    max-width: calc(100% - 220px);
    margin-left: 220px;
    overflow-x: hidden;
}

.app-sidebar-title {
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    color: var(--text-tertiary);
    padding: 0 var(--space-lg) var(--space-md);
    margin: 0;
}

.app-sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 0 var(--space-sm);
}

.app-nav-link {
    padding: 10px 14px;
    background: transparent;
    border: none;
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    text-decoration: none;
    font-size: var(--font-size-sm);
    font-weight: 500;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    gap: 10px;
    white-space: nowrap;
}

.app-nav-link:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.app-nav-link.active {
    background: linear-gradient(135deg, rgba(241, 174, 56, 0.12), rgba(241, 174, 56, 0.12));
    color: var(--text-primary);
}

.app-nav-link .nav-icon {
    font-size: 16px;
    width: 20px;
    text-align: center;
    flex-shrink: 0;
}

/* ── Sidebar Balance Section ── */
.sidebar-balance {
    margin-top: auto;
    padding: var(--space-md) var(--space-sm);
    border-top: 1px solid var(--border-subtle);
}

/* ── Sidebar Daily Progress ── */
.sidebar-progress {
    margin-top: var(--space-md);
    padding: var(--space-md) var(--space-sm);
    border-top: 1px solid var(--border-subtle);
    border-bottom: 1px solid var(--border-subtle);
}

.sidebar-progress-title {
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    color: var(--text-tertiary);
    padding: 0;
    margin: 0;
}

.sidebar-progress-head {
    padding: 0 var(--space-sm) 6px;
}

.sidebar-progress-controls {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 var(--space-sm) var(--space-sm);
}

.sidebar-progress-sort-btn {
    border: 1px solid var(--border-subtle);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border-radius: 999px;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 0.2px;
    padding: 3px 8px;
    cursor: pointer;
    white-space: nowrap;
}

.sidebar-progress-sort-arrow-btn {
    min-width: 36px;
    font-size: 18px;
    font-weight: 900;
    line-height: 1;
    padding: 2px 10px;
}

.sidebar-progress-sort-btn:hover {
    border-color: rgba(241, 174, 56, 0.55);
    color: var(--text-primary);
    background: rgba(241, 174, 56, 0.12);
}

.sidebar-progress-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    overflow: visible;
    padding-bottom: 0;
}

.sidebar-progress-tabs {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 6px;
    margin: 0 0 8px 0;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 2px;
    scrollbar-width: thin;
    scrollbar-color: rgba(241, 174, 56, 0.45) rgba(15, 23, 42, 0.55);
}

.sidebar-progress-tabs::-webkit-scrollbar {
    height: 8px;
}

.sidebar-progress-tabs::-webkit-scrollbar-track {
    background: rgba(15, 23, 42, 0.55);
    border-radius: 999px;
}

.sidebar-progress-tabs::-webkit-scrollbar-thumb {
    background: linear-gradient(90deg, rgba(241, 174, 56, 0.55), rgba(217, 155, 46, 0.55));
    border-radius: 999px;
    border: 1px solid rgba(148, 163, 184, 0.18);
}

.sidebar-progress-tabs::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(90deg, rgba(241, 174, 56, 0.78), rgba(217, 155, 46, 0.78));
}

.sidebar-progress-tab {
    position: relative;
    overflow: hidden;
    padding: 6px 12px;
    border: 1px solid var(--border-subtle);
    border-radius: 999px;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-size: 12px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 30px;
    min-width: 112px;
}

.sidebar-progress-tab-fill {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 0%;
    background: rgba(245, 158, 11, 0.9);
    opacity: 0.35;
    pointer-events: none;
}

.sidebar-progress-tab.progress-red .sidebar-progress-tab-fill {
    background: rgba(239, 68, 68, 0.9);
}

.sidebar-progress-tab.progress-yellow .sidebar-progress-tab-fill {
    background: rgba(245, 158, 11, 0.9);
}

.sidebar-progress-tab.progress-green .sidebar-progress-tab-fill {
    background: rgba(16, 185, 129, 0.9);
}

.sidebar-progress-tab-label {
    position: relative;
    z-index: 1;
}

.sidebar-progress-tab-label {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-progress-tab.active {
    border-color: rgba(241, 174, 56, 0.6);
    color: var(--text-primary);
    background: rgba(241, 174, 56, 0.12);
}

.sidebar-progress-row {
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-md);
    overflow: hidden;
    min-width: 0;
    max-width: none;
    width: 100%;
    flex: initial;
}

.sidebar-progress-fill {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 0%;
    opacity: 0.35;
    pointer-events: none;
    transition: width 0.25s ease;
}

.sidebar-progress-name {
    color: var(--text-secondary);
    font-size: var(--font-size-xs);
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    position: relative;
    z-index: 1;
}

.sidebar-progress-count {
    color: var(--text-primary);
    font-size: var(--font-size-xs);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    position: relative;
    z-index: 1;
}

.sidebar-progress-open-btn {
    border: 1px solid var(--border-subtle);
    background: rgba(255, 255, 255, 0.06);
    color: var(--text-secondary);
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    line-height: 1;
    padding: 3px 6px;
    position: relative;
    z-index: 1;
}

.sidebar-progress-open-btn:hover {
    background: rgba(241, 174, 56, 0.18);
    color: var(--text-primary);
}

.setup-reel-group-header {
    margin: 10px 0 6px;
    padding: 6px 10px;
    border: 1px solid rgba(241, 174, 56, 0.35);
    border-radius: 8px;
    background: rgba(241, 174, 56, 0.08);
    color: var(--text-primary);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.2px;
}

.sidebar-progress-row.progress-red {
    border: 1px solid rgba(239, 68, 68, 0.7);
    background: rgba(127, 29, 29, 0.35);
}
.sidebar-progress-row.progress-red .sidebar-progress-fill {
    background: rgba(239, 68, 68, 0.9);
}

.sidebar-progress-row.progress-yellow {
    border: 1px solid rgba(245, 158, 11, 0.7);
    background: rgba(120, 53, 15, 0.35);
}
.sidebar-progress-row.progress-yellow .sidebar-progress-fill {
    background: rgba(245, 158, 11, 0.9);
}

.sidebar-progress-row.progress-green {
    border: 1px solid rgba(16, 185, 129, 0.7);
    background: rgba(6, 78, 59, 0.35);
}
.sidebar-progress-row.progress-green .sidebar-progress-fill {
    background: rgba(16, 185, 129, 0.9);
}

.sidebar-progress-empty {
    color: var(--text-disabled);
    font-size: var(--font-size-xs);
    padding: 2px 10px 0;
}

.sidebar-balance-title {
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    color: var(--text-tertiary);
    padding: 0 var(--space-sm) var(--space-sm);
    margin: 0;
}

.balance-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-md);
    transition: background var(--transition-fast);
}

.balance-item:hover {
    background: var(--bg-hover);
}

.balance-label {
    font-size: var(--font-size-xs);
    color: var(--text-secondary);
    font-weight: 500;
}

.balance-value {
    font-size: var(--font-size-sm);
    font-weight: 600;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
}

.balance-value.loading {
    color: var(--text-disabled);
    font-size: var(--font-size-xs);
}

.balance-value.error {
    color: #ef4444;
    font-size: var(--font-size-xs);
}

.balance-value.low {
    color: #f59e0b;
}

.balance-value.depleted {
    color: #ef4444;
}

.balance-item.clickable {
    cursor: pointer;
}

.balance-item.clickable:hover .balance-value {
    text-decoration: underline;
}

/* ── FAL Balance Popup ── */
.fal-balance-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(0,0,0,.55);
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeIn .15s ease;
}

.fal-balance-modal {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    width: min(380px, 90vw);
    box-shadow: 0 12px 40px rgba(0,0,0,.35);
    overflow: hidden;
}

.fal-popup-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-md) var(--space-lg);
    border-bottom: 1px solid var(--border-color);
    font-weight: 600;
    font-size: var(--font-size-sm);
}

.fal-popup-close {
    background: none;
    border: none;
    color: var(--text-tertiary);
    font-size: 20px;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}
.fal-popup-close:hover { color: var(--text-primary); }

.fal-popup-body {
    padding: var(--space-sm) var(--space-lg);
    max-height: 300px;
    overflow-y: auto;
}

.fal-popup-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid var(--border-subtle, rgba(255,255,255,.06));
}
.fal-popup-row:last-child { border-bottom: none; }

.fal-popup-label {
    font-size: var(--font-size-xs);
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 220px;
}

.fal-popup-cost {
    font-size: var(--font-size-sm);
    font-weight: 600;
    color: var(--text-primary);
    flex-shrink: 0;
    margin-left: var(--space-md);
}

.fal-popup-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-md) var(--space-lg);
    border-top: 1px solid var(--border-color);
    font-size: var(--font-size-sm);
    font-weight: 600;
}

.fal-popup-total {
    font-size: var(--font-size-base);
    color: var(--accent-primary, #6366f1);
}

.error-text { color: #ef4444; font-size: var(--font-size-xs); }

.fal-popup-notice {
    padding: var(--space-md) var(--space-lg);
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    line-height: 1.6;
    text-align: center;
}
.fal-popup-notice p { margin: 0 0 8px 0; }
.fal-popup-notice p:last-child { margin-bottom: 0; }

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Legacy — keep for any remaining references */
.app-header { display: none; }
.app-header-content { display: none; }

.app-main {
    flex: 1;
    width: 100%;
    max-width: 100%;
    margin: 0;
    padding: var(--space-xl) var(--space-xl) var(--space-2xl);
    display: flex;
    flex-direction: column;
    gap: 0;
    box-sizing: border-box;
    overflow-x: hidden;
}

/* Page-level content wrapper (used inside app-main) */
.app-content {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.app-page-title {
    font-size: var(--font-size-2xl);
    font-weight: 700;
    color: var(--text-primary);
    margin: 0 0 var(--space-xl) 0;
}

/* ============================================================================
   CARDS & SECTIONS
   ============================================================================ */

.app-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: var(--space-xl);
    transition: all var(--transition-base);
}

.app-card:hover {
    border-color: var(--border-default);
    box-shadow: var(--shadow-md);
}

.app-section {
    background: var(--bg-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: var(--space-lg);
    margin-bottom: var(--space-lg);
    width: 100%;
    box-sizing: border-box;
}

.app-section-header {
    margin-bottom: var(--space-lg);
    width: 100%;
}

.app-section-title {
    font-size: var(--font-size-xl);
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 var(--space-sm) 0;
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.app-section-subtitle {
    font-size: var(--font-size-sm);
    color: var(--text-tertiary);
    margin: 0;
    line-height: 1.6;
}

/* ============================================================================
   BUTTONS
   ============================================================================ */

.app-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    padding: 8px 16px;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: var(--font-size-sm);
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: all var(--transition-base);
    text-decoration: none;
    white-space: nowrap;
    line-height: 1.4;
}

.app-btn:hover {
    background: var(--bg-hover);
    border-color: var(--border-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

.app-btn:active {
    transform: translateY(0);
}

.app-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    pointer-events: none;
}

.app-btn-primary {
    background: linear-gradient(135deg, var(--accent-magenta), var(--accent-orange));
    border-color: transparent;
    color: #fff;
    font-weight: 600;
    box-shadow: 0 2px 8px rgba(241, 174, 56, 0.25);
}

.app-btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(241, 174, 56, 0.35);
}

.app-btn-secondary {
    background: var(--bg-tertiary);
    border-color: var(--border-default);
}

.app-btn-secondary:hover {
    background: var(--bg-hover);
    border-color: var(--border-hover);
}

.app-btn-large {
    padding: 12px 24px;
    font-size: var(--font-size-base);
}

.app-btn-icon {
    font-size: var(--font-size-lg);
    line-height: 1;
}

/* ============================================================================
   FORMS
   ============================================================================ */

.app-label {
    display: block;
    font-size: var(--font-size-sm);
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: var(--space-sm);
}

.app-input,
.app-textarea,
.app-select {
    width: 100%;
    padding: 8px 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: var(--font-size-sm);
    font-family: inherit;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
    line-height: 1.5;
}

.app-input:focus,
.app-textarea:focus,
.app-select:focus {
    outline: none;
    border-color: var(--accent-magenta);
    box-shadow: 0 0 0 2px rgba(241, 174, 56, 0.1);
}

.app-input::placeholder,
.app-textarea::placeholder {
    color: var(--text-disabled);
}

.app-textarea {
    min-height: 120px;
    resize: vertical;
    line-height: 1.6;
}

.app-select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%239ca3af' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--space-md) center;
    padding-right: var(--space-2xl);
}

/* ============================================================================
   GRID LAYOUTS
   ============================================================================ */

.app-grid {
    display: grid;
    gap: var(--space-lg);
    width: 100%;
}

.app-grid-2 {
    grid-template-columns: repeat(2, 1fr);
}

.app-grid-3 {
    grid-template-columns: repeat(3, 1fr);
}

.app-grid-auto {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}

/* ============================================================================
   IMAGE & VIDEO GRIDS - CORRECT ASPECT RATIOS
   ============================================================================ */

/* Universal image grid - preserves aspect ratios */
.image-grid,
.generated-images-grid,
.history-grid,
.gallery-grid,
.file-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)) !important;
    gap: var(--space-md);
    width: 100%;
    box-sizing: border-box;
}

/* Ensure specific containers are grids */
#batch-history-container,
#video-history-container,
#kling-history-container,
#video-image-gallery-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)) !important;
    gap: var(--space-md);
    width: 100%;
    box-sizing: border-box;
}

/* Image sandbox queue — 2 per row (info cards) */
#sandbox-queue-container {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr) !important;
    gap: var(--space-md);
    width: 100%;
    box-sizing: border-box;
}

/* Batch history preview */
.batch-history-preview {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)) !important;
    gap: var(--space-md);
    width: 100%;
    box-sizing: border-box;
}

/* If JS creates nested grid, ensure it's also a grid */
.image-grid .image-grid,
.image-grid .generated-images-grid,
.image-grid .history-grid,
.history-grid .image-grid,
.history-grid .generated-images-grid,
.history-grid .history-grid,
.generated-images-grid .image-grid,
.generated-images-grid .generated-images-grid,
.generated-images-grid .history-grid,
#batch-history-container .image-grid,
#batch-history-container .generated-images-grid,
#video-history-container .history-grid,
#video-history-container .image-grid,
#kling-history-container .history-grid,
#kling-history-container .image-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)) !important;
    gap: var(--space-md);
    width: 100%;
    box-sizing: border-box;
}

/* Nested grids in batch history */
.batch-history-preview .image-grid {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)) !important;
}

/* Image container - preserves natural aspect ratio */
.image-item,
.generated-image-square,
.history-item,
.gallery-item,
.file-item {
    position: relative;
    border-radius: var(--radius-md);
    overflow: hidden;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    cursor: pointer;
    transition: all var(--transition-base);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 150px;
}

.image-item:hover,
.generated-image-square:hover,
.history-item:hover,
.gallery-item:hover,
.file-item:hover {
    transform: scale(1.02);
    border-color: var(--accent-magenta);
    box-shadow: var(--shadow-md);
}

/* Images - preserve aspect ratio, show full image */
.image-item img,
.generated-image-square img,
.history-item img,
.gallery-item img,
.file-item img {
    width: 100%;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
    background: var(--bg-primary);
}

/* Videos - preserve aspect ratio */
.image-item video,
.generated-image-square video,
.history-item video,
.file-item video {
    width: 100%;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
    background: var(--bg-primary);
}

/* Video items - preserve natural aspect ratio */
.history-item.video-item,
.file-item.video-item {
    min-height: 150px;
    /* Aspect ratio will be set dynamically by JavaScript from video dimensions */
}

.history-item.video-item video,
.file-item.video-item video {
    width: 100%;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
}

/* Placeholder styling */
.image-item.placeholder,
.generated-image-square.placeholder,
.history-item.placeholder {
    background: var(--bg-secondary);
    border-style: dashed;
    border-color: var(--border-subtle);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-tertiary);
    font-size: 32px;
    min-height: 200px;
    animation: pulse 2s ease-in-out infinite;
}

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

/* ============================================================================
   BADGES & TAGS
   ============================================================================ */

.app-badge {
    display: inline-flex;
    align-items: center;
    padding: var(--space-xs) var(--space-sm);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    font-size: var(--font-size-xs);
    font-weight: 600;
    color: var(--text-secondary);
}

.app-badge-primary {
    background: linear-gradient(135deg, rgba(241, 174, 56, 0.2), rgba(241, 174, 56, 0.2));
    border-color: var(--accent-magenta);
    color: var(--accent-magenta-light);
}

/* ============================================================================
   MODAL
   ============================================================================ */

.app-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-lg);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-base);
}

.app-modal.active {
    opacity: 1;
    pointer-events: all;
}

.app-modal-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
}

.app-modal-content {
    position: relative;
    width: 100%;
    max-width: min(1200px, 95vw);
    max-height: 90vh;
    background: var(--bg-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform: scale(0.95);
    transition: transform var(--transition-base);
    margin: var(--space-lg);
    box-sizing: border-box;
}

.app-modal.active .app-modal-content {
    transform: scale(1);
}

.app-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-lg) var(--space-xl);
    border-bottom: 1px solid var(--border-subtle);
}

.app-modal-title {
    font-size: var(--font-size-xl);
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.app-modal-close {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    font-size: var(--font-size-xl);
    cursor: pointer;
    transition: all var(--transition-base);
}

.app-modal-close:hover {
    background: var(--bg-hover);
    border-color: var(--border-hover);
    color: var(--text-primary);
    transform: rotate(90deg);
}

.app-modal-body {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-xl);
}

.app-modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-md);
    padding: var(--space-lg) var(--space-xl);
    border-top: 1px solid var(--border-subtle);
}

/* ============================================================================
   UTILITIES
   ============================================================================ */

.app-empty-state {
    text-align: center;
    padding: var(--space-2xl);
    color: var(--text-tertiary);
}

.app-empty-state-icon {
    font-size: 48px;
    margin-bottom: var(--space-md);
    opacity: 0.5;
}

.app-empty-state-text {
    font-size: var(--font-size-base);
    color: var(--text-secondary);
}

.app-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-2xl);
    color: var(--text-tertiary);
}

.app-loading-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border-subtle);
    border-top-color: var(--accent-magenta);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* ============================================================================
   SCROLLBAR
   ============================================================================ */

::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

::-webkit-scrollbar-track {
    background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
    background: var(--bg-tertiary);
    border-radius: 5px;
    border: 2px solid var(--bg-primary);
}

::-webkit-scrollbar-thumb:hover {
    background: var(--bg-hover);
}

/* ============================================================================
   COMPONENT OVERRIDES (for compatibility with existing JS)
   ============================================================================ */

/* Ensure existing components work with new design */
.container {
    background: var(--bg-primary);
    min-height: 100vh;
    width: 100%;
    max-width: 100%;
}

.compact-header {
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-subtle);
    width: 100%;
}

.compact-main {
    background: var(--bg-primary);
    width: 100%;
    max-width: 100%;
}

.sandbox-container {
    display: flex;
    flex-direction: column;
    gap: var(--space-xl);
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.sandbox-section {
    background: var(--bg-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: var(--space-xl);
    width: 100%;
}

.batch-creator,
.editor-container,
.generations-container {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

/* Notification container */
.notification-container {
    position: fixed;
    top: var(--space-lg);
    right: var(--space-lg);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    max-width: 400px;
}

.notification {
    padding: var(--space-md);
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    animation: slideIn var(--transition-base) ease-out;
}

.notification.success {
    border-color: #10b981;
    background: rgba(16, 185, 129, 0.1);
}

.notification.error {
    border-color: #ef4444;
    background: rgba(239, 68, 68, 0.1);
}

/* ============================================================================
   RESPONSIVE
   ============================================================================ */

@media (max-width: 1024px) {
    .app-grid-3 {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .image-grid,
    .generated-images-grid,
    .history-grid,
    .gallery-grid,
    .file-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    }
}

@media (max-width: 768px) {
    .app-main {
        padding: var(--space-md) var(--space-md) var(--space-xl);
    }
    
    /* sidebar is same on mobile */
    
    .app-grid-2,
    .app-grid-3 {
        grid-template-columns: 1fr;
    }
    
    .app-section {
        padding: var(--space-md);
    }
    
    .image-grid,
    .generated-images-grid,
    .history-grid,
    .gallery-grid,
    .file-grid {
        grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
        gap: var(--space-sm);
    }
}

/* ============================================================================
   GENERATIONS PAGE MEDIA GRID
   ============================================================================ */

.gen-media-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: var(--space-md);
    padding: var(--space-lg);
    width: 100%;
    box-sizing: border-box;
}

.gen-media-item {
    background: var(--bg-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    overflow: hidden;
    transition: all var(--transition-base);
    cursor: pointer;
    display: flex;
    flex-direction: column;
}

.gen-media-item:hover {
    transform: translateY(-4px);
    border-color: var(--accent-magenta);
    box-shadow: var(--shadow-md);
}

.gen-media-wrapper {
    position: relative;
    width: 100%;
    min-height: 150px;
    background: var(--bg-primary);
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

.gen-media-wrapper img {
    width: 100%;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
}

.gen-media-wrapper video {
    width: 100%;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
}

.gen-media-label {
    padding: var(--space-sm) var(--space-md);
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    background: var(--bg-tertiary);
    border-top: 1px solid var(--border-subtle);
}

.gen-media-error {
    padding: var(--space-md);
    color: var(--text-tertiary);
    text-align: center;
    font-size: var(--font-size-sm);
}

@media (max-width: 1024px) {
    .gen-media-grid {
        grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
        gap: var(--space-sm);
        padding: var(--space-md);
    }
}

@media (max-width: 768px) {
    .gen-media-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        gap: var(--space-sm);
        padding: var(--space-md);
    }
}

/* ============================================================================
   VIDEO CREATOR SELECTED IMAGES PREVIEW
   ============================================================================ */

.video-selected-images-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: var(--space-md);
    width: 100%;
    box-sizing: border-box;
}

.video-selected-image-item {
    position: relative;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    overflow: hidden;
    transition: all var(--transition-base);
    min-height: 150px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.video-selected-image-item:hover {
    border-color: var(--accent-magenta);
    transform: scale(1.02);
    box-shadow: var(--shadow-md);
}

.video-selected-image-item img {
    width: 100%;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
}

.video-selected-image-remove {
    position: absolute;
    top: var(--space-sm);
    right: var(--space-sm);
    background: rgba(239, 68, 68, 0.9);
    color: white;
    border: none;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    font-size: var(--font-size-lg);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-base);
    z-index: 10;
    opacity: 0;
}

.video-selected-image-item:hover .video-selected-image-remove {
    opacity: 1;
}

.video-selected-image-remove:hover {
    background: #ef4444;
    transform: scale(1.1);
}

/* ============================================================================
   ANIMATIONS
   ============================================================================ */

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

.app-fade-in {
    animation: fadeIn var(--transition-base) ease-out;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

.app-slide-in {
    animation: slideIn var(--transition-base) ease-out;
}

/* ============================================================================
   Rate buttons (good / bad) – shared across image-sandbox & video-generation
   ============================================================================ */
.rate-buttons-wrap {
    position: absolute;
    top: 6px;
    right: 6px;
    z-index: 2;
    display: flex;
    gap: 4px;
}
.rate-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.25);
    background: rgba(0,0,0,0.5);
    color: rgba(255,255,255,0.7);
    font-size: 0.85rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: all 0.15s ease;
    backdrop-filter: blur(4px);
}
.rate-btn:hover { background: rgba(0,0,0,0.7); }
.rate-btn.btn-good:hover { border-color: #10b981; color: #10b981; }
.rate-btn.btn-bad:hover  { border-color: #ef4444; color: #ef4444; }
.rate-btn.active-good { border-color: #10b981; background: #10b981; color: white; }
.rate-btn.active-bad  { border-color: #ef4444; background: #ef4444; color: white; }
.rate-btn.checked { border-color: #10b981; background: #10b981; color: white; box-shadow: 0 0 0 2px rgba(16,185,129,0.22); }
.rate-btn.active-good:hover,
.rate-btn.active-bad:hover { filter: brightness(1.15); }

.history-daily-move-select {
    position: absolute;
    top: 6px;
    left: 6px;
    z-index: 2;
    max-width: 62%;
    height: 28px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.35);
    background: rgba(0, 0, 0, 0.58);
    color: #fff;
    font-size: 11px;
    padding: 0 8px;
    backdrop-filter: blur(4px);
}

.history-daily-move-select:focus {
    outline: none;
    border-color: rgba(241, 174, 56, 0.85);
}

/* Video history cards use slightly larger buttons */
.video-history-card .rate-buttons-wrap { top: 8px; right: 8px; gap: 5px; }
.video-history-card .rate-btn { width: 32px; height: 32px; font-size: 1rem;
    border-color: rgba(255,255,255,0.3); background: rgba(0,0,0,0.55); color: rgba(255,255,255,0.8); }
.video-history-card .rate-btn:hover { background: rgba(0,0,0,0.75); }
.video-history-card .rate-btn.active-good { border-color: #10b981; background: #10b981; color: white; }
.video-history-card .rate-btn.active-bad  { border-color: #ef4444; background: #ef4444; color: white; }
.video-history-card .rate-btn.active-good:hover,
.video-history-card .rate-btn.active-bad:hover { filter: brightness(1.15); }

/* Legacy compat – .good-check still used by older image-sandbox markup */
.good-check-wrap { position: absolute; top: 6px; right: 6px; z-index: 2; display: flex; gap: 4px; }
.good-check {
    width: 26px; height: 26px; border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.3); background: rgba(0,0,0,0.45);
    color: rgba(255,255,255,0.6); font-size: 0.8rem; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    padding: 0; transition: all 0.15s ease;
}
.good-check:hover { background: rgba(0,0,0,0.6); color: #10b981; border-color: #10b981; }
.good-check.checked { background: #10b981; border-color: #10b981; color: white; }

