/* ── Theme Variables ──────────────────────────────── */
:root { color-scheme: dark light; }

[data-theme="dark"] {
    --bg: #0a0a0c; --bg-secondary: #111114;
    --card: #16161a; --card-hover: #1c1c21;
    --border: #27272c; --border-hover: #3a3a42;
    --text-primary: #f0f0f2; --text-secondary: #b0b0b6;
    --text-muted: #6e6e76; --text-faint: #4a4a52;
    --input-bg: #111114; --input-border: #27272c;
    --nav-bg: rgba(10,10,12,0.85); --overlay: rgba(0,0,0,0.6);
    --shadow: 0 4px 24px rgba(0,0,0,0.4);
}

[data-theme="light"] {
    --bg: #f8f9fc; --bg-secondary: #ffffff;
    --card: #ffffff; --card-hover: #f3f3f5;
    --border: #e4e4e9; --border-hover: #c8c8d0;
    --text-primary: #18181b; --text-secondary: #52525b;
    --text-muted: #8e8e96; --text-faint: #b0b0b8;
    --input-bg: #ffffff; --input-border: #d4d4d8;
    --nav-bg: rgba(248,249,252,0.92); --overlay: rgba(0,0,0,0.3);
    --shadow: 0 4px 24px rgba(0,0,0,0.06);
}

/* ── Base ────────────────────────────────────────── */
* { scrollbar-width: thin; scrollbar-color: var(--border) var(--bg); }
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::selection { background: rgba(124,77,255,0.25); }

html, body { overflow-x: hidden; max-width: 100%; }
/* touch-action does NOT inherit — apply to every element so pinch/double-tap on
   any child also can't zoom the browser. The image viewers below use the more
   specific id selector to opt back into native control (touch-action: none) so
   their own pinch handlers can take over. */
* { touch-action: pan-x pan-y; }
body { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
#pf-image-viewer, #pf-image-viewer *,
#lightbox, #lightbox * { touch-action: none; }
img, video, canvas { max-width: 100%; height: auto; }
button, [role="button"], a.btn-primary, a.btn-ghost { touch-action: manipulation; }
input, textarea, select { font-size: 16px; } /* prevent iOS zoom-on-focus */
@media (min-width: 768px) {
    input, textarea, select { font-size: 14px; }
}

/* iOS safe-area for fixed bottom bars / notched devices */
.safe-bottom { padding-bottom: env(safe-area-inset-bottom, 0); }
.safe-top { padding-top: env(safe-area-inset-top, 0); }

/* ── Theme Utility Classes ───────────────────────── */
.theme-bg { background: var(--bg); color: var(--text-secondary); }
.theme-text { color: var(--text-secondary); }
.theme-text-primary { color: var(--text-primary); }
.theme-text-muted { color: var(--text-muted); }
.theme-text-faint { color: var(--text-faint); }
.theme-card { background: var(--card); border: 1px solid var(--border); }
.theme-card-bg { background: var(--card); }
.theme-border { border-color: var(--border); }
.theme-border-t { border-top: 1px solid var(--border); }
.theme-hover:hover { background: var(--card-hover); }
.theme-nav { background: var(--nav-bg); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px); border-color: var(--border); }

/* ── Nav Buttons ─────────────────────────────────── */
.nav-btn {
    display: inline-flex; align-items: center; justify-content: center;
    padding: 8px 18px; border-radius: 10px; font-size: 14px; font-weight: 500;
    color: var(--text-secondary); background: var(--card);
    border: 1px solid var(--border); transition: all 0.2s;
    text-decoration: none; gap: 6px; white-space: nowrap;
}
.nav-btn:hover {
    color: var(--text-primary); border-color: var(--border-hover);
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.nav-btn.active {
    color: #7c4dff; border-color: rgba(124,77,255,0.4);
    background: rgba(124,77,255,0.06);
}

/* ── Theme Toggle ────────────────────────────────── */
.theme-toggle-btn {
    display: flex; align-items: center; justify-content: center;
    width: 38px; height: 38px; border-radius: 10px;
    background: var(--card); border: 1px solid var(--border);
    color: var(--text-muted); transition: all 0.2s; cursor: pointer;
}
.theme-toggle-btn:hover { color: #7c4dff; border-color: rgba(124,77,255,0.4); }

/* ── Buttons ─────────────────────────────────────── */
.btn-primary {
    display: inline-flex; align-items: center; justify-content: center;
    padding: 10px 20px; border-radius: 12px;
    background: linear-gradient(135deg, #6d28ff, #7c4dff);
    color: #fff; font-weight: 500; font-size: 14px;
    transition: all 0.2s; box-shadow: 0 4px 14px rgba(124,77,255,0.25);
    border: none; cursor: pointer; text-decoration: none;
}
.btn-primary:hover { background: linear-gradient(135deg, #7c4dff, #8b71ff); transform: translateY(-1px); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.btn-ghost {
    display: inline-flex; align-items: center; justify-content: center;
    padding: 10px 20px; border-radius: 12px;
    border: 1px solid var(--border); color: var(--text-secondary);
    font-size: 14px; font-weight: 500; background: transparent;
    transition: all 0.2s; cursor: pointer; text-decoration: none;
}
.btn-ghost:hover { border-color: var(--border-hover); color: var(--text-primary); background: var(--card-hover); }

/* ── Input ───────────────────────────────────────── */
.input-field {
    padding: 10px 16px; border-radius: 12px;
    background: var(--input-bg); border: 1px solid var(--input-border);
    color: var(--text-primary); font-size: 14px;
    transition: all 0.2s; outline: none;
}
.input-field::placeholder { color: var(--text-faint); }
.input-field:focus { border-color: rgba(124,77,255,0.5); box-shadow: 0 0 0 3px rgba(124,77,255,0.1); }
select.input-field {
    appearance: none; -webkit-appearance: none; -moz-appearance: none;
    padding-right: 36px; cursor: pointer;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%237c4dff' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
    background-repeat: no-repeat;
    background-position: right 14px center;
    background-size: 12px;
}
select.input-field option {
    background: var(--bg-secondary); color: var(--text-primary); padding: 8px;
}

/* ── Cards ───────────────────────────────────────── */
.glass-card {
    background: var(--card); border: 1px solid var(--border);
    box-shadow: var(--shadow);
}
.feature-card {
    background: var(--card); border: 1px solid var(--border);
    border-radius: 16px; padding: 24px;
    transition: all 0.3s; box-shadow: var(--shadow);
}
.feature-card:hover { border-color: rgba(124,77,255,0.3); transform: translateY(-2px); }

/* ── Template Cards ──────────────────────────────── */
.template-card {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 8px; padding: 16px 12px; border-radius: 14px;
    background: var(--card); border: 1px solid var(--border);
    cursor: pointer; transition: all 0.25s; text-align: center; min-height: 80px;
}
.template-card:hover {
    border-color: rgba(124,77,255,0.5); background: var(--card-hover);
    transform: translateY(-2px); box-shadow: 0 6px 20px rgba(124,77,255,0.12);
}
.template-card .template-emoji { font-size: 24px; line-height: 1; }
.template-card .template-title { font-size: 13px; font-weight: 500; color: var(--text-primary); }

/* ── Batch Count Stepper (− N +) ──────────────────── */
.count-btn {
    width: 32px; height: 36px; line-height: 1;
    border: 1px solid var(--border);
    background: var(--bg-secondary);
    border-radius: 8px;
    color: var(--text-muted);
    font-size: 18px; font-weight: 600;
    cursor: pointer; transition: all 0.12s;
    user-select: none; display: inline-flex;
    align-items: center; justify-content: center;
}
.count-btn:hover {
    background: var(--card-hover);
    color: #a78bff;
    border-color: rgba(124,77,255,0.4);
}
.count-btn:active { transform: scale(0.95); }
/* Hide native number-input spinners; we use our own ± buttons. */
#genCountInput::-webkit-outer-spin-button,
#genCountInput::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
#genCountInput { -moz-appearance: textfield; }

/* ── Mode Tabs (text2img / img2img) ──────────────── */
.mode-tab {
    display: flex; align-items: center; gap: 14px;
    padding: 16px 20px; border-radius: 14px;
    background: var(--card); border: 1px solid var(--border);
    color: var(--text-secondary); cursor: pointer; transition: all 0.2s;
    text-align: left;
}
.mode-tab:hover { border-color: rgba(124,77,255,0.4); transform: translateY(-1px); }
.mode-tab.active {
    background: linear-gradient(135deg, rgba(124,77,255,0.12), rgba(68,138,255,0.06));
    border-color: #7c4dff; color: #8b71ff;
    box-shadow: 0 6px 20px rgba(124,77,255,0.15);
}
.mode-tab .mode-tab-title { font-size: 15px; font-weight: 600; line-height: 1.2; }
.mode-tab .mode-tab-sub { font-size: 11px; opacity: 0.75; margin-top: 2px; line-height: 1.2; }
.mode-tab svg { flex-shrink: 0; }

/* ── Feature Option Cards (img2img) ──────────────── */
.feat-card {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 4px; padding: 10px 6px; border-radius: 10px;
    background: var(--card); border: 1px solid var(--border);
    cursor: pointer; transition: all 0.2s; text-align: center;
    font-size: 12px; color: var(--text-secondary); line-height: 1.3;
}
.feat-card:hover { border-color: rgba(124,77,255,0.4); }
.feat-card.active {
    border-color: #7c4dff; background: rgba(124,77,255,0.08); color: #8b71ff;
    box-shadow: 0 0 0 1px rgba(124,77,255,0.25);
}
.feat-card .feat-emoji { font-size: 18px; line-height: 1; }

/* ── Brush Swatches (inpaint color picker) ──────── */
.brush-swatch {
    width: 22px; height: 22px; border-radius: 50%;
    border: 2px solid transparent; cursor: pointer;
    box-shadow: 0 0 0 1px var(--border);
    transition: transform 0.15s, box-shadow 0.15s;
    padding: 0;
}
.brush-swatch:hover { transform: scale(1.12); }
.brush-swatch.active {
    border-color: var(--text-primary);
    box-shadow: 0 0 0 2px #7c4dff, 0 0 10px rgba(124,77,255,0.45);
    transform: scale(1.12);
}

/* ── Brush Range Slider (themed) ──────────────────── */
.brush-range {
    -webkit-appearance: none;
    appearance: none;
    height: 6px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 3px;
    outline: none;
    cursor: pointer;
}
.brush-range::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px; height: 16px; border-radius: 50%;
    background: #8b71ff;
    cursor: pointer; border: 2px solid var(--bg);
}
.brush-range::-moz-range-thumb {
    width: 14px; height: 14px; border-radius: 50%;
    background: #8b71ff;
    cursor: pointer; border: 2px solid var(--bg);
}
.brush-range::-moz-range-track {
    background: var(--bg-secondary);
    height: 6px; border-radius: 3px;
    border: 1px solid var(--border);
}

/* ── Action Buttons ──────────────────────────────── */
.action-btn {
    width: 40px; height: 40px; border-radius: 12px;
    background: var(--card); border: 1px solid var(--border);
    display: flex; align-items: center; justify-content: center;
    color: var(--text-muted); transition: all 0.2s; cursor: pointer;
}
.action-btn:hover { border-color: rgba(124,77,255,0.5); color: #8b71ff; }

/* ── Hero Orbs (floating gradient blobs) ─────────── */
.hero-section { position: relative; overflow: hidden; }

.orb {
    position: absolute; border-radius: 50%;
    filter: blur(80px); opacity: 0.5;
    animation: orb-float 8s ease-in-out infinite;
    pointer-events: none;
}
.orb-1 {
    width: 400px; height: 400px; top: -100px; left: 10%;
    background: radial-gradient(circle, rgba(124,77,255,0.5) 0%, transparent 70%);
    animation-delay: 0s;
}
.orb-2 {
    width: 350px; height: 350px; top: 50px; right: 5%;
    background: radial-gradient(circle, rgba(255,64,129,0.4) 0%, transparent 70%);
    animation-delay: -2s;
}
.orb-3 {
    width: 300px; height: 300px; bottom: -50px; left: 35%;
    background: radial-gradient(circle, rgba(24,255,255,0.3) 0%, transparent 70%);
    animation-delay: -4s;
}
.orb-4 {
    width: 250px; height: 250px; top: 30%; left: -5%;
    background: radial-gradient(circle, rgba(68,138,255,0.35) 0%, transparent 70%);
    animation-delay: -6s;
}

[data-theme="light"] .orb { opacity: 0.55; filter: blur(80px); }

@keyframes orb-float {
    0%, 100% { transform: translate(0, 0) scale(1); }
    25% { transform: translate(30px, -20px) scale(1.05); }
    50% { transform: translate(-10px, 20px) scale(0.95); }
    75% { transform: translate(-25px, -10px) scale(1.02); }
}

/* ── Page background gradient mesh ───────────────── */
.page-bg-mesh {
    position: fixed; inset: 0; z-index: -1; pointer-events: none;
    background:
        radial-gradient(ellipse 50% 40% at 85% 15%, rgba(124,77,255,0.06) 0%, transparent 100%),
        radial-gradient(ellipse 40% 50% at 10% 80%, rgba(255,64,129,0.04) 0%, transparent 100%),
        radial-gradient(ellipse 60% 30% at 50% 50%, rgba(68,138,255,0.03) 0%, transparent 100%);
}
[data-theme="dark"] .page-bg-mesh {
    background:
        radial-gradient(ellipse 50% 40% at 85% 15%, rgba(124,77,255,0.04) 0%, transparent 100%),
        radial-gradient(ellipse 40% 50% at 10% 80%, rgba(255,64,129,0.03) 0%, transparent 100%),
        radial-gradient(ellipse 60% 30% at 50% 50%, rgba(68,138,255,0.02) 0%, transparent 100%);
}

/* ── Animations ───────────────────────────────────── */
@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-6px); }
}
.animate-float { animation: float 3s ease-in-out infinite; }

@keyframes modal-in {
    from { opacity: 0; transform: scale(0.95) translateY(10px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}
.animate-modal { animation: modal-in 0.2s ease-out; }

/* ── Loading Ring ─────────────────────────────────── */
.loading-ring {
    width: 48px; height: 48px;
    border: 3px solid rgba(124,77,255,0.15);
    border-top-color: #7c4dff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Showcase Poster Pile ────────────────────────── */
.showcase-pile-wrap {
    position: relative;
    height: 860px;
    display: flex; align-items: center; justify-content: center;
    user-select: none; cursor: grab;
}
.showcase-pile-inner {
    position: relative;
    width: 580px; height: 780px;
}
.showcase-card {
    position: absolute; inset: 0;
    border-radius: 16px; overflow: hidden;
    box-shadow: 0 8px 40px rgba(0,0,0,0.35), 0 2px 8px rgba(0,0,0,0.2);
    transform: rotate(var(--rot,0deg)) translate(var(--tx,0),var(--ty,0));
    transition: transform 0.5s cubic-bezier(.4,.0,.2,1), opacity 0.5s ease;
    pointer-events: none;
}
/* Only promote the active layers to the GPU. Promoting all 16 cards causes
   memory pressure and scroll jank on phones. */
.showcase-card.is-top, .showcase-card.is-gone { will-change: transform, opacity; }
.showcase-card.is-gone {
    transform: rotate(calc(var(--rot,0deg) - 15deg)) translate(-120%,-30%) scale(0.7);
    opacity: 0;
    pointer-events: none;
}
.showcase-card.is-top {
    z-index: 10;
    box-shadow: 0 12px 50px rgba(124,77,255,0.15), 0 8px 30px rgba(0,0,0,0.3);
}
.showcase-card img {
    width: 100%; height: 100%; object-fit: cover;
    display: block;
}
.showcase-label {
    position: absolute; bottom: 0; left: 0; right: 0;
    padding: 16px 20px 14px;
    background: linear-gradient(to top, rgba(0,0,0,0.75), transparent);
    color: #fff; font-size: 15px; font-weight: 500;
}
.showcase-counter {
    position: absolute; bottom: 16px; right: 24px;
    font-size: 13px; font-weight: 600; color: var(--text-muted);
    background: var(--card); border: 1px solid var(--border);
    padding: 4px 14px; border-radius: 20px;
    z-index: 20;
}
.showcase-hint {
    position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%);
    display: flex; align-items: center; gap: 6px;
    font-size: 12px; z-index: 20;
}

@media (max-width: 640px) {
    .showcase-pile-wrap { height: 480px; touch-action: pan-y; }
    .showcase-pile-inner { width: 85vw; height: 60vh; max-height: 420px; }
}
@media (min-width: 641px) and (max-width: 1024px) {
    .showcase-pile-inner { width: 500px; height: 670px; }
}

/* ── Gallery ──────────────────────────────────────── */
.gallery-item { transition: transform 0.2s ease; }
.gallery-item:hover { transform: translateY(-2px); }

/* ── Category Header ─────────────────────────────── */
.category-header {
    font-size: 12px; font-weight: 600; letter-spacing: 0.05em;
    text-transform: uppercase; color: var(--text-faint);
    padding-bottom: 8px; margin-bottom: 12px;
    border-bottom: 1px solid var(--border);
}

/* ── Custom Select ──────────────────────────────── */
.custom-select { position: relative; }
.custom-select-trigger {
    display: flex; align-items: center; justify-content: space-between;
    padding: 8px 12px; border-radius: 12px; width: 100%;
    background: var(--input-bg); border: 1px solid var(--input-border);
    color: var(--text-primary); font-size: 13px;
    cursor: pointer; transition: all 0.2s; outline: none;
    user-select: none; white-space: nowrap;
}
.custom-select-sm .custom-select-trigger { font-size: 12px; }
.custom-select-sm .custom-select-option { font-size: 12px; }
.custom-select-trigger:hover { border-color: var(--border-hover); }
.custom-select.open .custom-select-trigger {
    border-color: rgba(124,77,255,0.5);
    box-shadow: 0 0 0 3px rgba(124,77,255,0.1);
}
.custom-select-arrow {
    width: 14px; height: 14px; color: var(--text-faint);
    transition: transform 0.2s; flex-shrink: 0; margin-left: 6px;
}
.custom-select.open .custom-select-arrow { transform: rotate(180deg); }
.custom-select-dropdown {
    position: absolute; top: calc(100% + 4px); left: 0; right: 0;
    background: var(--card); border: 1px solid var(--border);
    border-radius: 12px; padding: 4px; z-index: 50;
    box-shadow: var(--shadow);
    opacity: 0; visibility: hidden; transform: translateY(-4px);
    transition: all 0.15s ease;
}
.custom-select.open .custom-select-dropdown {
    opacity: 1; visibility: visible; transform: translateY(0);
}
.custom-select-option {
    padding: 8px 12px; border-radius: 8px; font-size: 13px;
    color: var(--text-secondary); cursor: pointer; transition: all 0.15s;
}
.custom-select-option:hover {
    background: var(--card-hover); color: var(--text-primary);
}
.custom-select-option.selected {
    color: #7c4dff; background: rgba(124,77,255,0.08);
}

/* ── Responsive ───────────────────────────────────── */
@media (max-width: 1024px) {
    .glass-card { box-shadow: 0 2px 12px rgba(0,0,0,0.12); }
    .feature-card { padding: 18px; }
    .template-card { min-height: 70px; padding: 12px 8px; }
    .template-card .template-emoji { font-size: 20px; }
    .template-card .template-title { font-size: 12px; }
}
@media (max-width: 640px) {
    .orb { filter: blur(60px); }
    .orb-1 { width: 250px; height: 250px; }
    .orb-2 { width: 200px; height: 200px; }
    .orb-3 { width: 180px; height: 180px; }
    .orb-4 { display: none; }
    .nav-btn { padding: 6px 12px; font-size: 13px; }
    .btn-primary, .btn-ghost { padding: 10px 16px; font-size: 13px; }
    .mode-tab { padding: 12px 14px; gap: 10px; }
    .mode-tab .mode-tab-title { font-size: 14px; }
    .mode-tab .mode-tab-sub { font-size: 10px; }
    .feat-card { padding: 8px 4px; font-size: 11px; }
    .feat-card .feat-emoji { font-size: 16px; }
    .action-btn { width: 36px; height: 36px; }

    /* Touch-friendly tap targets: ensure interactive controls hit ~40px */
    .nav-btn, .btn-ghost, .btn-primary { min-height: 40px; }

    /* Tighter section padding on phones */
    section.max-w-7xl, section.max-w-5xl, section.max-w-4xl, section.max-w-3xl { padding-top: 16px; padding-bottom: 16px; }
}

/* Image viewer always full-screen on touch */
@media (hover: none) and (pointer: coarse) {
    #pf-image-viewer img { max-width: 100vw !important; max-height: 100vh !important; }
}

/* ── Generate page result/preview heights ───────── */
.result-area-h { min-height: 340px; max-height: 560px; }
.upload-zone-h { min-height: 360px; }
.viewport-h { height: 460px; }
.ref-img-preview { max-height: 440px; }
@media (max-width: 1024px) {
    .result-area-h { min-height: 300px; max-height: 480px; }
    .upload-zone-h { min-height: 300px; }
    .viewport-h { height: 360px; }
    .ref-img-preview { max-height: 340px; }
}
@media (max-width: 640px) {
    .result-area-h { min-height: 240px; max-height: 70vh; }
    .upload-zone-h { min-height: 220px; }
    .viewport-h { height: 260px; }
    .ref-img-preview { max-height: 240px; }
}

/* Hide scrollbar on horizontally scrollable strips while keeping the scroll */
.scroll-strip { -ms-overflow-style: none; scrollbar-width: none; }
.scroll-strip::-webkit-scrollbar { display: none; }

/* ── Progress Steps ──────────────────────────────── */
.progress-step {
    display: flex; align-items: center; gap: 8px;
    font-size: 11px; color: var(--text-faint);
    transition: color 0.3s; line-height: 1.2;
}
.progress-dot {
    width: 8px; height: 8px; border-radius: 50%;
    border: 1.5px solid var(--border);
    background: transparent;
    transition: all 0.3s; flex-shrink: 0;
    position: relative;
}
.progress-step.active { color: #8b71ff; font-weight: 500; }
.progress-step.active .progress-dot {
    border-color: #8b71ff; background: #8b71ff;
    box-shadow: 0 0 8px rgba(124,77,255,0.5);
}
.progress-step.active .progress-dot::after {
    content: ''; position: absolute; inset: -4px;
    border-radius: 50%; border: 2px solid rgba(124,77,255,0.3);
    animation: progress-ping 1.2s ease-in-out infinite;
}
.progress-step.done { color: #22c55e; }
.progress-step.done .progress-dot {
    border-color: #22c55e; background: #22c55e;
}
@keyframes progress-ping {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.4); opacity: 0; }
}
