/* ===== CSS Variables ===== */
:root {
    --primary: #f59e0b;
    --primary-hover: #d97706;
    --primary-light: rgba(245, 158, 11, 0.15);
    --primary-glow: rgba(245, 158, 11, 0.4);
    --bg-dark: #0a0a0b;
    --bg-card: #151517;
    --bg-card-hover: #1c1c1f;
    --bg-input: #1a1a1d;
    --text-primary: #ffffff;
    --text-secondary: #a1a1aa;
    --text-muted: #71717a;
    --border-color: #27272a;
    --border-light: #3f3f46;
    --error: #ef4444;
    --success: #22c55e;
    --glass-bg: rgba(21, 21, 23, 0.9);
    --glass-border: rgba(255, 255, 255, 0.06);
    --shadow-lg: 0 25px 50px -12px rgba(0, 0, 0, 0.6);
    --shadow-glow: 0 0 40px -10px var(--primary-glow);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --radius-full: 9999px;
}

/* ===== Reset & Base ===== */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-dark);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Background gradient */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background:
        radial-gradient(ellipse 80% 50% at 20% 10%, rgba(245, 158, 11, 0.12) 0%, transparent 50%),
        radial-gradient(ellipse 60% 40% at 80% 90%, rgba(168, 85, 247, 0.08) 0%, transparent 50%),
        radial-gradient(ellipse 50% 50% at 50% 50%, rgba(6, 182, 212, 0.05) 0%, transparent 60%);
    pointer-events: none;
    z-index: -1;
}

/* ===== Utility Classes ===== */
.hidden {
    display: none !important;
}

.glass {
    background: var(--glass-bg);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border: 1px solid var(--glass-border);
}

.glass-light {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
}

/* ===== Page Layout ===== */
.page {
    display: none;
    min-height: 100vh;
    min-height: 100dvh;
}

.page.active {
    display: block;
}

/* ===== Login Page ===== */
.login-container {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.login-card {
    width: 100%;
    max-width: 400px;
    padding: 40px 32px;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg), var(--shadow-glow);
    animation: fadeInUp 0.5s ease-out;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(24px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 8px;
}

.logo-icon {
    font-size: 44px;
    animation: float 3s ease-in-out infinite;
}

@keyframes float {

    0%,
    100% {
        transform: translateY(0) rotate(0deg);
    }

    50% {
        transform: translateY(-8px) rotate(5deg);
    }
}

.logo h1 {
    font-size: 26px;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary) 0%, #fcd34d 50%, var(--primary) 100%);
    background-size: 200% auto;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    animation: shimmer 3s linear infinite;
}

@keyframes shimmer {
    to {
        background-position: 200% center;
    }
}

.subtitle {
    text-align: center;
    color: var(--text-secondary);
    margin-bottom: 32px;
    font-size: 14px;
}

.input-group {
    margin-bottom: 20px;
}

.input-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.input-group input {
    width: 100%;
    padding: 14px 16px;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 16px;
    transition: all 0.2s ease;
}

.input-group input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.input-group input::placeholder {
    color: var(--text-muted);
}

.error-message {
    margin-top: 16px;
    padding: 12px 16px;
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.2);
    border-radius: var(--radius-md);
    color: var(--error);
    font-size: 14px;
    text-align: center;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 24px;
    font-size: 15px;
    font-weight: 600;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s ease;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary) 0%, #f97316 100%);
    color: #000;
    width: 100%;
    position: relative;
    overflow: hidden;
}

.btn-primary::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s ease;
}

.btn-primary:hover::before {
    left: 100%;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 32px -8px rgba(245, 158, 11, 0.5);
}

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

.btn-primary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
    padding: 8px 16px;
    border-radius: var(--radius-full);
}

.btn-ghost:hover {
    background: rgba(255, 255, 255, 0.06);
    color: var(--text-primary);
}

.btn-large {
    padding: 16px 32px;
    font-size: 16px;
    margin-top: 20px;
    border-radius: var(--radius-lg);
}

/* ===== App Header ===== */
.app-header {
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 24px;
    border-bottom: 1px solid var(--glass-border);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo-icon-small {
    font-size: 26px;
}

.app-title {
    font-size: 17px;
    font-weight: 600;
    background: linear-gradient(135deg, var(--primary), #fcd34d);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* ===== App Main ===== */
.app-main {
    max-width: 1400px;
    margin: 0 auto;
    padding: 32px 40px;
}

/* ===== Main Content: Left-Right Layout ===== */
.main-content {
    display: grid;
    grid-template-columns: 2fr 3fr;
    gap: 40px;
    align-items: stretch;
}

/* Prompt section on right (in DOM order first for mobile) */
.prompt-section {
    order: 2;
}

/* Upload section on left */
.upload-section {
    order: 1;
    position: sticky;
    top: 80px;
}

.section-title {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 16px;
    color: var(--text-primary);
}

.section-title svg {
    color: var(--primary);
    flex-shrink: 0;
}

.section-title .hint {
    font-size: 12px;
    font-weight: 400;
    color: var(--text-muted);
}

/* ===== Upload Section ===== */
.upload-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
}

.upload-slot {
    position: relative;
    aspect-ratio: 1;
}

.file-input {
    display: none;
}

.upload-label {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: var(--bg-card);
    border: 2px dashed var(--border-color);
    border-radius: var(--radius-lg);
    cursor: pointer;
    transition: all 0.2s ease;
    overflow: hidden;
}

.upload-label:hover {
    border-color: var(--primary);
    background: var(--bg-card-hover);
    border-style: solid;
}

.upload-label:active {
    transform: scale(0.98);
}

.upload-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    color: var(--text-muted);
    padding: 8px;
}

.upload-placeholder svg {
    opacity: 0.4;
    width: 24px;
    height: 24px;
}

.upload-placeholder span {
    font-size: 12px;
    font-weight: 500;
    text-align: center;
}

.image-preview {
    position: relative;
    width: 100%;
    height: 100%;
}

.image-preview img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.image-badge {
    position: absolute;
    top: 6px;
    left: 6px;
    padding: 3px 8px;
    background: var(--primary);
    color: #000;
    font-size: 11px;
    font-weight: 700;
    border-radius: var(--radius-full);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.remove-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 24px;
    height: 24px;
    background: rgba(0, 0, 0, 0.7);
    border: none;
    border-radius: 50%;
    color: white;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    backdrop-filter: blur(4px);
}

.remove-btn:hover {
    background: var(--error);
    transform: scale(1.1);
}

/* ===== Prompt Section ===== */
.prompt-section {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.prompt-section .section-title {
    font-size: 18px;
}

.prompt-textarea {
    width: 100%;
    flex: 1;
    min-height: 160px;
    padding: 16px;
    background: var(--bg-input);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-lg);
    color: var(--text-primary);
    font-size: 15px;
    font-family: inherit;
    line-height: 1.7;
    resize: vertical;
    transition: all 0.2s ease;
}

.prompt-textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px var(--primary-light);
}

.prompt-textarea::placeholder {
    color: var(--text-muted);
}

.prompt-tips {
    padding: 12px 14px;
    margin-top: 12px;
    font-size: 13px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.prompt-tips strong {
    color: var(--primary);
}

.tips-inline {
    color: var(--text-muted);
}

.prompt-tips em {
    color: var(--primary);
    font-style: normal;
    font-weight: 600;
    background: var(--primary-light);
    padding: 1px 6px;
    border-radius: 4px;
}

.settings-row {
    display: flex;
    gap: 12px;
    margin-top: 16px;
}

.setting-item {
    flex: 1;
}

.setting-item label {
    display: block;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.select-input {
    width: 100%;
    padding: 12px 14px;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23a1a1aa' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}

.select-input:focus {
    outline: none;
    border-color: var(--primary);
}

/* ===== Result Section ===== */
.result-section {
    margin-top: 32px;
    animation: fadeInUp 0.4s ease-out;
}

.result-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.result-item {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    overflow: hidden;
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
}

.result-image {
    width: 100%;
    display: block;
}

.result-text {
    padding: 16px 20px;
    color: var(--text-secondary);
    font-size: 14px;
    line-height: 1.7;
    border-top: 1px solid var(--border-color);
    white-space: pre-wrap;
}

.result-actions {
    display: flex;
    gap: 12px;
    padding: 12px 16px;
    border-top: 1px solid var(--border-color);
    background: rgba(0, 0, 0, 0.2);
}

.btn-download {
    padding: 10px 16px;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    font-size: 13px;
    border-radius: var(--radius-md);
}

.btn-download:hover {
    background: var(--border-color);
    border-color: var(--border-light);
}

/* ===== Loading Overlay ===== */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(12px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.loading-content {
    text-align: center;
    padding: 40px 48px;
    border-radius: var(--radius-xl);
    max-width: 90%;
}

.loading-spinner {
    width: 52px;
    height: 52px;
    border: 3px solid var(--border-color);
    border-top-color: var(--primary);
    border-radius: 50%;
    margin: 0 auto 20px;
    animation: spin 0.8s linear infinite;
}

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

.loading-text {
    font-size: 17px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 6px;
}

.loading-hint {
    font-size: 13px;
    color: var(--text-muted);
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

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

/* ===== Tablet Responsive ===== */
@media (max-width: 1100px) {
    .main-content {
        grid-template-columns: 1fr 1fr;
        gap: 32px;
    }

    .app-main {
        padding: 24px;
    }
}

/* ===== Mobile Responsive ===== */
@media (max-width: 768px) {
    .main-content {
        grid-template-columns: 1fr;
        gap: 24px;
    }

    /* Mobile: Prompt first, Upload second */
    .prompt-section {
        order: 1;
    }

    .upload-section {
        order: 2;
        position: static;
    }

    .app-main {
        padding: 16px;
        padding-bottom: 100px;
    }

    .app-header {
        padding: 12px 16px;
    }

    .upload-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 8px;
    }

    .upload-placeholder svg {
        width: 20px;
        height: 20px;
    }

    .upload-placeholder span {
        font-size: 10px;
    }

    .prompt-textarea {
        min-height: 120px;
        font-size: 16px;
        /* Prevent iOS zoom */
    }
}

@media (max-width: 480px) {
    html {
        font-size: 15px;
    }

    .login-card {
        padding: 32px 24px;
        border-radius: var(--radius-lg);
    }

    .logo h1 {
        font-size: 22px;
    }

    .logo-icon {
        font-size: 36px;
    }

    .logo-icon-small {
        font-size: 24px;
    }

    .app-title {
        font-size: 15px;
    }

    .btn-ghost span {
        display: none;
    }

    .btn-ghost {
        padding: 8px;
    }

    .app-main {
        padding: 12px;
        padding-bottom: 120px;
    }

    .section-title {
        font-size: 14px;
        margin-bottom: 12px;
    }

    .prompt-section .section-title {
        font-size: 16px;
    }

    .section-title svg {
        width: 18px;
        height: 18px;
    }

    .upload-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 6px;
    }

    .image-badge {
        font-size: 9px;
        padding: 2px 5px;
        top: 4px;
        left: 4px;
    }

    .remove-btn {
        width: 20px;
        height: 20px;
        font-size: 12px;
        top: 4px;
        right: 4px;
    }

    .prompt-tips {
        padding: 10px 12px;
        font-size: 12px;
    }

    .prompt-textarea {
        min-height: 100px;
        padding: 14px;
    }

    .settings-row {
        gap: 10px;
    }

    .select-input {
        padding: 11px 12px;
        font-size: 14px;
    }

    .btn-large {
        padding: 14px 24px;
        font-size: 15px;
        margin-top: 16px;
    }

    .result-section {
        margin-top: 24px;
    }

    .result-actions {
        padding: 10px 12px;
    }

    .btn-download {
        padding: 8px 12px;
        font-size: 12px;
    }

    .loading-content {
        padding: 32px 40px;
    }

    .loading-spinner {
        width: 44px;
        height: 44px;
    }

    .loading-text {
        font-size: 15px;
    }
}

/* ===== Safe Area for iOS ===== */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .app-main {
        padding-bottom: calc(100px + env(safe-area-inset-bottom));
    }

    .loading-overlay {
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* ===== Animations ===== */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}