/* ============================================================
   SD Turbo — WebNN · Interface
   Single stylesheet · Dark precision theme
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300;400;500;600;700&family=Space+Mono:wght@400;700&display=swap');

/* ── Variables ── */
:root {
    --bg:           #09090f;
    --bg-1:         #111119;
    --bg-2:         #18181f;
    --bg-3:         #202028;
    --bg-4:         #2a2a36;

    --border:       rgba(255,255,255,0.07);
    --border-hi:    rgba(99,102,241,0.4);

    --accent:       #6366f1;
    --accent-dim:   rgba(99,102,241,0.12);
    --accent-glow:  rgba(99,102,241,0.22);
    --accent-2:     #06b6d4;

    --green:        #22d3ee;
    --green-dim:    rgba(34,211,238,0.12);
    --pink:         #f43f5e;
    --pink-dim:     rgba(244,63,94,0.12);

    --text:         #e2e4f0;
    --text-2:       #8b8fa8;
    --text-3:       #42455a;

    --font-ui:      'Space Grotesk', system-ui, sans-serif;
    --font-mono:    'Space Mono', 'Courier New', monospace;

    --radius:       12px;
    --radius-sm:    6px;
    --radius-pill:  100px;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 15px; }

body {
    font-family: var(--font-ui);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    background-image: radial-gradient(ellipse 70% 40% at 50% -5%,
        rgba(99,102,241,0.07) 0%, transparent 60%);
    scrollbar-width: thin;
    scrollbar-color: var(--bg-3) transparent;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--green); text-decoration: underline; }

/* ── Header ── */
header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 32px;
    height: 60px;
    border-bottom: 1px solid var(--border);
    background: rgba(9,9,15,0.9);
    backdrop-filter: blur(16px);
    position: sticky;
    top: 0;
    z-index: 100;
    gap: 16px;
    flex-shrink: 0;
}

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

.logo-mark {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    display: grid;
    place-items: center;
    font-family: var(--font-mono);
    font-size: 1.2rem;
    font-weight: 600;
    color: #fff;
    letter-spacing: 0.04em;
    flex-shrink: 0;
}

.header-titles {
    display: flex;
    flex-direction: column;
    line-height: 0;
}

.header-title {
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--text);
}

.header-title span {
    background: linear-gradient(90deg, var(--accent), var(--accent-2));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header-sub {
    font-size: 0.68rem;
    color: var(--text-3);
    font-family: var(--font-mono);
    letter-spacing: 0.04em;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 14px;
}

/* ── WebNN status ── */
#webnnstatus {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 0.75rem;
    font-family: var(--font-mono);
    color: var(--text-2);
}

#webnnstatus #circle {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-3);
    flex-shrink: 0;
    transition: background 0.3s, box-shadow 0.3s;
}

#webnnstatus.green #circle {
    background: var(--green);
    box-shadow: 0 0 8px var(--green);
}
#webnnstatus.red #circle {
    background: var(--pink);
    box-shadow: 0 0 8px var(--pink);
}
#webnnstatus.red { color: var(--pink); }

#backend-links { display: inline-flex; gap: 4px; font-size: 0.72rem; }
#backend-links a { color: var(--text-3); }
#backend-links a:hover { color: var(--accent); }

/* ── Device badge ── */
#badge {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 4px 12px;
    border-radius: var(--radius-pill);
    border: 1px solid var(--border-hi);
    background: var(--accent-dim);
    font-family: var(--font-mono);
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--accent);
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

#badge .badge-ep { font-weight: 400; color: var(--text-3); margin-left: 2px; }
#badge.npu { border-color: rgba(167,139,250,0.35); background: rgba(167,139,250,0.1); color: #a78bfa; }
#badge.cpu { border-color: rgba(34,211,238,0.3); background: rgba(34,211,238,0.08); color: var(--green); }

/* ── Main layout ── */
main {
    flex: 1;
    padding: 24px 32px 32px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-width: 95%;
    width: 100%;
    margin: 0 auto;
}

/* ── Image grid ── */
#image_area {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
}

/* ── Frame ── */
.frame {
    position: relative;
    aspect-ratio: 1;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--bg-1);
    overflow: hidden;
    transition: border-color 0.3s, box-shadow 0.3s;
}

.frame:hover { border-color: rgba(99,102,241,0.2); }

.frame canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: none;
    border-radius: var(--radius);
}

.frame-placeholder {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.placeholder-num {
    font-family: var(--font-mono);
    font-size: 2.2rem;
    font-weight: 700;
    color: var(--bg-3);
    user-select: none;
}

.frame-progress {
    position: absolute;
    bottom: 0; left: 0; right: 0;
    padding: 12px 14px 14px;
    display: none;
    flex-direction: column;
    gap: 5px;
    background: linear-gradient(to top, rgba(9,9,15,0.92) 70%, transparent);
}

.progress-bar {
    height: 2px;
    border-radius: 2px;
    background: var(--bg-4);
    overflow: hidden;
}

.progress-bar::after {
    content: '';
    display: block;
    height: 100%;
    width: var(--progress, 0%);
    background: linear-gradient(90deg, var(--accent), var(--accent-2));
    border-radius: 2px;
    transition: width 0.4s ease;
}

.progress-label {
    font-size: 0.65rem;
    font-family: var(--font-mono);
    color: var(--text-2);
    text-align: right;
}

/* Frame states */
.frame .frame-placeholder { display: flex; }
.frame .frame-progress    { display: none; }
.frame canvas             { display: none; }

.frame.loadwave .frame-placeholder  { display: none; }
.frame.loadwave .frame-progress     { display: flex; }
.frame.loadwave { animation: pulse-border 2.5s ease-in-out infinite; }

.frame.inferencing .frame-placeholder { display: none; }
.frame.inferencing .frame-progress    { display: flex; }
.frame.inferencing { animation: pulse-border 1.4s ease-in-out infinite; }

.frame.ready .frame-placeholder { display: flex; }
.frame.ready .frame-progress    { display: none; }
.frame.ready .placeholder-num   { color: var(--bg-4); }
.frame.ready { border-color: rgba(99,102,241,0.15); }

.frame.done .frame-placeholder { display: none; }
.frame.done .frame-progress    { display: none; }
.frame.done canvas             { display: block; }

.frame.done.nsfw::after {
    content: 'NSFW';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(244,63,94,0.6);
    backdrop-filter: blur(10px);
    font-family: var(--font-mono);
    font-weight: 700;
    letter-spacing: 0.12em;
    color: #fff;
    border-radius: var(--radius);
}

/* ── Download button ── */
.btn-download {
    position: absolute;
    bottom: 10px;
    right: 10px;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 1px solid rgba(255,255,255,0.15);
    background: rgba(9,9,15,0.7);
    backdrop-filter: blur(8px);
    color: #fff;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background 0.18s, border-color 0.18s, transform 0.18s;
    z-index: 10;
}

.btn-download svg {
    width: 15px;
    height: 15px;
    flex-shrink: 0;
}

.btn-download:hover {
    background: var(--accent);
    border-color: var(--accent);
    transform: translateY(-2px);
    box-shadow: 0 4px 14px rgba(99,102,241,0.5);
}

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

/* Visible uniquement quand l'image est générée */
.frame.done .btn-download { display: flex; }

@keyframes pulse-border {
    0%, 100% { border-color: var(--border); box-shadow: none; }
    50% { border-color: var(--border-hi); box-shadow: 0 0 24px var(--accent-glow); }
}

/* ── Hidden utility ── */
.hidden { display: none; }

/* ── Data area ── */
#data_area {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    min-height: 30px;
}

#data_area div {
    display: none;
    align-items: center;
    justify-content: center;
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    background: var(--accent-dim);
    border: 1px solid var(--border-hi);
    font-family: var(--font-mono);
    font-size: 0.72rem;
    color: var(--accent);
    font-weight: 700;
    text-align: center;
}

#data_area div.show  { display: flex; }
#data_area div.nsfw  {
    background: var(--pink-dim);
    border-color: rgba(244,63,94,0.3);
    color: var(--pink);
}

/* ── Action zone ── */
.action-zone {
    display: flex;
    flex-direction: column;
    gap: 10px;
    /*max-width: 760px;*/
    margin: 0 auto;
    width: 100%;
}

/* ── Model loader bar ── */
.model-loader {
    display: none;          /* shown via #buttons.loading ~ rule */
    flex-direction: column;
    gap: 8px;
    padding: 14px 18px;
    background: var(--bg-1);
    border: 1px solid var(--border-hi);
    border-radius: var(--radius);
    animation: fade-in 0.3s ease;
}

@keyframes fade-in {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(0); }
}

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

.loader-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-2);
    letter-spacing: 0.02em;
}

.loader-pct {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--accent);
    min-width: 40px;
    text-align: right;
}

.loader-track {
    height: 4px;
    border-radius: 4px;
    background: var(--bg-3);
    overflow: hidden;
}

.loader-fill {
    height: 100%;
    width: 0%;
    border-radius: 4px;
    background: linear-gradient(90deg, var(--accent), var(--accent-2));
    transition: width 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    /* shimmer animation */
}

.loader-fill::after {
    content: '';
    position: absolute;
    top: 0; right: 0; bottom: 0;
    width: 60px;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.25), transparent);
    animation: shimmer 1.5s ease-in-out infinite;
}

@keyframes shimmer {
    0%   { transform: translateX(-60px); }
    100% { transform: translateX(60px); }
}

.loader-steps {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.loader-step {
    font-family: var(--font-mono);
    font-size: 0.62rem;
    color: var(--text-3);
    padding: 2px 8px;
    border-radius: 4px;
    border: 1px solid var(--border);
    background: var(--bg-2);
    transition: all 0.3s;
    letter-spacing: 0.03em;
}

.loader-step.active {
    color: var(--accent-2);
    border-color: rgba(6,182,212,0.3);
    background: rgba(6,182,212,0.08);
}

.loader-step.done {
    color: var(--green);
    border-color: rgba(34,211,238,0.2);
    background: var(--green-dim);
}

/* Show loader when #buttons has class "loading" */
#buttons.loading ~ .model-loader,
.action-zone:has(#buttons.loading) .model-loader {
    display: flex;
}

/* ── Prompt wrap ── */
.prompt-wrap {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    transition: border-color 0.2s, box-shadow 0.2s;
    padding: 2px;
}

.prompt-wrap:focus-within {
    border-color: var(--border-hi);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

#user-input {
    display: block;
    width: 100%;
    background: transparent;
    border: none;
    outline: none;
    font-family: var(--font-ui);
    font-size: 0.92rem;
    color: var(--text-2);
    resize: none;
    line-height: 1.5;
    caret-color: var(--accent);
    padding: 12px 16px;
    min-height: 60px;
}

#user-input.enabled,
#user-input:focus { color: var(--text); }
#user-input::placeholder { color: var(--text-3); }

/* ── Button group ── */
.button-group {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* Boutons */
.button {
    border: none;
    border-radius: var(--radius-pill);
    font-family: var(--font-ui);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    padding: 10px 22px;
    display: inline-flex;
    align-items: center;
    gap: 7px;
    white-space: nowrap;
    transition: all 0.18s ease;
    letter-spacing: 0.01em;
    line-height: 1;
}

.btn-load {
    background: var(--bg-3);
    color: var(--text-2);
    border: 1px solid var(--border);
}

.btn-load:hover:not(:disabled) {
    background: var(--bg-4);
    color: var(--text);
    border-color: rgba(255,255,255,0.14);
}

.btn-generate {
    background: linear-gradient(135deg, var(--accent) 0%, #4f46e5 100%);
    color: #fff;
    box-shadow: 0 2px 14px rgba(99,102,241,0.35);
}

.btn-generate:hover:not(:disabled) {
    background: linear-gradient(135deg, #7c7ff5 0%, var(--accent) 100%);
    box-shadow: 0 4px 22px rgba(99,102,241,0.55);
    transform: translateY(-1px);
}

.btn-generate:active { transform: translateY(0); box-shadow: 0 1px 6px rgba(99,102,241,0.3); }

.button:disabled {
    opacity: 0.3;
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
}

.btn-spark { font-size: 0.9rem; }

.shortcut-hint {
    font-size: 0.72rem;
    font-family: var(--font-mono);
    color: var(--text-3);
    margin-left: 4px;
}

/* Button visibility driven by #buttons class (set by index.js) */
#buttons #load           { display: inline-flex; }
#buttons #generate       { display: none; }
#buttons .shortcut-hint  { display: none; }

#buttons.loaded #load          { display: none; }
#buttons.loaded #generate      { display: inline-flex; }
#buttons.loaded .shortcut-hint { display: inline; }

#buttons.loading #load         { display: none; }
#buttons.loading #generate     { display: none; }
#buttons.loading .shortcut-hint{ display: none; }

/* ── Model loader visibility: use sibling trick ──
   The loader sits AFTER #buttons in .action-zone.
   When #buttons.loading, show the loader.               */
#buttons.loading + .prompt-wrap + .button-group + .model-loader,
#buttons ~ .model-loader { display: none; }

/* Simpler: target via the parent */
.action-zone .model-loader { display: none; }
.action-zone:has(#buttons.loading) .model-loader { display: flex; }

/* ── Bottom half: controls + perf ── */
.grid-2 {
    display: grid;
    grid-template-columns: 1fr 3fr;
    gap: 16px;
    align-items: start;
}

.mt-1 { margin-top: 4px; }

.footerinfo { font-size: 0.8rem; color: var(--text-2); }

.controls-col {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px;
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

/* Toggle */
#release {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-3);
    font-size: 0.78rem;
}

#release input[type=checkbox] {
    height: 0; width: 0; visibility: hidden; position: absolute;
}

#release label {
    cursor: pointer;
    width: 34px; height: 18px;
    background: var(--bg-3);
    border: 1px solid var(--border);
    border-radius: 18px;
    position: relative;
    transition: all 0.2s;
    flex-shrink: 0;
}

#release label::after {
    content: '';
    position: absolute;
    top: 2px; left: 2px;
    width: 12px; height: 12px;
    background: var(--text-3);
    border-radius: 50%;
    transition: 0.2s;
}

#release input:checked + label { background: var(--accent-dim); border-color: var(--border-hi); }
#release input:checked + label::after {
    background: var(--accent);
    left: calc(100% - 14px);
    box-shadow: 0 0 6px var(--accent-glow);
}

#versions {
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: var(--text-3);
    line-height: 1.7;
}

#versions a { color: var(--text-3); text-decoration: underline; }
#versions a:hover { color: var(--green); }

/* ── Perf table ── */
#data {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

#data table { width: 100%; border-collapse: collapse; font-size: 0.7rem; }

#data thead th,
#data thead td {
    background: var(--bg-2);
    color: var(--text-2);
    font-weight: 600;
    font-family: var(--font-ui);
    padding: 6px 8px;
    border-bottom: 1px solid var(--border);
    border-right: 1px solid var(--border);
    text-align: center;
    letter-spacing: 0.02em;
}

#data tbody td {
    padding: 5px 8px;
    border-bottom: 1px solid var(--border);
    border-right: 1px solid var(--border);
    text-align: center;
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: var(--text-3);
    background: var(--bg-1);
    transition: background 0.12s;
}

#data tbody td:first-child {
    font-family: var(--font-ui);
    font-size: 0.7rem;
    color: var(--text-2);
    text-align: left;
    font-weight: 500;
}

#data tbody tr:hover td {
    background: var(--accent-dim);
    color: var(--text);
}

.table-note {
    padding: 6px 12px;
    font-size: 0.62rem;
    color: var(--text-3);
    font-family: var(--font-mono);
    border-top: 1px solid var(--border);
    text-align: right;
}

/* ── Footer ── */
footer {
    padding: 14px 32px;
    border-top: 1px solid var(--border);
    text-align: center;
    font-size: 0.65rem;
    font-family: var(--font-mono);
    color: var(--text-3);
    letter-spacing: 0.04em;
    flex-shrink: 0;
}

/* ── Scrollbars ── */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-3); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--bg-4); }

/* ── Responsive ── */
@media (max-width: 1024px) {
    #image_area { grid-template-columns: repeat(2, 1fr); }
    #data_area  { grid-template-columns: repeat(2, 1fr); }
    .grid-2     { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
    header { padding: 0 16px; }
    main   { padding: 16px; gap: 12px; }
    .header-sub { display: none; }
    #image_area { grid-template-columns: repeat(2, 1fr); gap: 6px; }
    #data_area  { grid-template-columns: repeat(2, 1fr); gap: 6px; }
    .shortcut-hint { display: none !important; }
    footer { padding: 12px 16px; }
}
