:root {
    --bg: #07080c;
    --panel: rgba(16, 18, 26, 0.86);
    --panel-solid: #12141c;
    --border: rgba(255, 255, 255, 0.09);
    --text: #e9ecf3;
    --muted: #8b93a7;
    --accent: #4fd1ff;
    --accent-2: #a78bfa;
    --danger: #ff6b7a;
    --radius: 10px;
    --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 14px;
}

body.mode-edit { overflow: hidden; height: 100vh; height: 100dvh; }
/* Lecture : pas de « tirer pour rafraîchir » ni de rebond qui ferait sauter la caméra */
html:has(body.mode-play) { overscroll-behavior-y: none; }
body.mode-play { overscroll-behavior-y: none; }

#canvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    height: 100lvh; /* hauteur max de l'écran mobile : ne bouge pas avec la barre d'adresse */
    display: block;
    outline: none;
}

#scroll-track { position: relative; width: 1px; pointer-events: none; }
body.mode-edit #scroll-track { display: none; }

/* ---------- Top bar ---------- */
#topbar {
    position: fixed;
    top: 0; left: 0; right: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 18px;
    z-index: 20;
    pointer-events: none;
    background: linear-gradient(to bottom, rgba(0,0,0,0.55), transparent);
}
#topbar > * { pointer-events: auto; }
.brand { display: flex; gap: 10px; align-items: center; font-weight: 600; letter-spacing: 0.02em; }
.brand em { font-style: normal; color: var(--accent); font-weight: 500; }
.logo { color: var(--accent); }

.mode-switch {
    display: flex;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 3px;
    backdrop-filter: blur(10px);
}
.mode-switch button {
    border: none;
    background: transparent;
    color: var(--muted);
    border-radius: 999px;
    padding: 6px 14px;
}
.mode-switch button.active { background: var(--accent); color: #041018; font-weight: 600; }

/* ---------- Buttons & inputs ---------- */
button, .button {
    font: inherit;
    color: var(--text);
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 7px 11px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
button:hover, .button:hover { background: rgba(255, 255, 255, 0.12); }
button.primary { background: var(--accent); color: #041018; border-color: transparent; font-weight: 600; width: 100%; justify-content: center; }
button.primary:hover { filter: brightness(1.1); }
button.danger { color: var(--danger); }
button:disabled { opacity: 0.4; cursor: default; }

input[type="text"], textarea, select {
    font: inherit;
    width: 100%;
    color: var(--text);
    background: rgba(0, 0, 0, 0.35);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 8px;
}
textarea { resize: vertical; }
input[type="range"] { accent-color: var(--accent); width: 100%; }
kbd {
    font-family: ui-monospace, monospace;
    font-size: 11px;
    padding: 1px 5px;
    border: 1px solid var(--border);
    border-bottom-width: 2px;
    border-radius: 4px;
    background: rgba(255,255,255,0.06);
    margin: 0 1px;
}
code { font-size: 12px; color: var(--accent); }

/* ---------- Editor panel ---------- */
#editor {
    position: fixed;
    top: 64px; right: 14px; bottom: 14px;
    width: 330px;
    overflow-y: auto;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 14px;
    backdrop-filter: blur(14px);
    z-index: 15;
    padding: 4px 16px 16px;
}
body.mode-play #editor, body.mode-play #fly-help { display: none; }

#editor section { padding: 12px 0; border-bottom: 1px solid var(--border); }
#editor section:last-child { border-bottom: none; }
#editor h3 {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
    margin: 4px 0 10px;
    display: flex; align-items: center; gap: 8px;
}
.badge { background: var(--accent-2); color: #120a26; border-radius: 999px; padding: 0 7px; font-size: 11px; }
.hint { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 4px 0 10px; }
.status { color: var(--muted); font-size: 12px; margin: 6px 0; }

.row { display: grid; grid-template-columns: 110px 1fr; gap: 8px; align-items: center; margin: 8px 0; }
.row.slider { grid-template-columns: 110px 1fr 42px; }
.row.slider output { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.row.check { grid-template-columns: auto 1fr; cursor: pointer; }
.row.stack { grid-template-columns: 1fr; gap: 4px; }
.row > span { color: var(--muted); }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; }

.file-drop {
    display: block;
    border: 1px dashed rgba(255,255,255,0.2);
    border-radius: 8px;
    padding: 10px;
    text-align: center;
    cursor: pointer;
    color: var(--muted);
}
.file-drop:hover { border-color: var(--accent); color: var(--text); }
.file-drop input { display: none; }

#kf-list { list-style: none; padding: 0; margin: 10px 0 0; display: flex; flex-direction: column; gap: 4px; }
#kf-list li {
    display: flex; align-items: center; gap: 8px;
    padding: 7px 9px;
    border-radius: 8px;
    border: 1px solid transparent;
    background: rgba(255,255,255,0.04);
    cursor: pointer;
}
#kf-list li:hover { background: rgba(255,255,255,0.08); }
#kf-list li.selected { border-color: var(--accent); background: rgba(79, 209, 255, 0.1); }
#kf-list li.current .num { background: var(--accent-2); color: #120a26; }
#kf-list .num {
    flex: none; width: 22px; height: 22px; border-radius: 50%;
    display: grid; place-items: center;
    font-size: 11px; font-weight: 700;
    background: rgba(255,255,255,0.1);
}
#kf-list .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#kf-list .label.empty { color: var(--muted); font-style: italic; }
#kf-list .dur { color: var(--muted); font-size: 11px; }

.timeline { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.timeline button { flex: none; width: 36px; justify-content: center; }

.hidden { display: none !important; }

#fly-help {
    position: fixed;
    left: 14px; bottom: 14px;
    max-width: calc(100vw - 380px);
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px 12px;
    color: var(--muted);
    font-size: 12px;
    z-index: 15;
    backdrop-filter: blur(10px);
}
#fly-help b { color: var(--text); }

body.viewer-only .mode-switch { display: none; }

.domain-input { display: flex; align-items: center; gap: 4px; }
.domain-input input { flex: 1; min-width: 0; }
#pub-domain { color: var(--muted); font-size: 12px; white-space: nowrap; }
#pub-status a { color: var(--accent); word-break: break-all; }
#publish { margin-top: 4px; }

/* ---------- Story (play mode) ---------- */
#story { display: none; }
body.mode-play #story { display: block; }

.caption {
    position: fixed;
    left: 6vw;
    bottom: 12vh;
    max-width: min(460px, 86vw);
    padding: 22px 26px;
    background: rgba(8, 10, 16, 0.62);
    border: 1px solid var(--border);
    border-radius: 16px;
    backdrop-filter: blur(12px);
    z-index: 10;
    opacity: 0;
    transform: translateY(16px);
    pointer-events: none;
    will-change: opacity, transform;
}
.caption h2 { margin: 0 0 8px; font-size: clamp(22px, 3vw, 34px); line-height: 1.15; }
.caption p { margin: 0; color: #c7cddb; line-height: 1.6; font-size: 15px; white-space: pre-line; }
.caption p:empty { display: none; }
.caption h2:empty { display: none; }

#scroll-hint {
    position: fixed;
    left: 50%; bottom: 28px;
    transform: translateX(-50%);
    display: flex; flex-direction: column; align-items: center; gap: 10px;
    color: var(--muted);
    font-size: 12px; letter-spacing: 0.12em; text-transform: uppercase;
    z-index: 10;
    transition: opacity 0.4s;
    pointer-events: none;
}
#scroll-hint.gone { opacity: 0; }
.mouse { width: 22px; height: 34px; border: 2px solid var(--muted); border-radius: 12px; position: relative; }
.wheel { width: 3px; height: 7px; background: var(--muted); border-radius: 2px; position: absolute; left: 50%; top: 6px; transform: translateX(-50%); animation: wheel 1.6s infinite; }
@keyframes wheel { 0% { opacity: 1; top: 6px; } 100% { opacity: 0; top: 18px; } }

#progress-rail {
    position: fixed;
    right: 22px; top: 50%;
    transform: translateY(-50%);
    width: 3px; height: 40vh;
    background: rgba(255,255,255,0.12);
    border-radius: 3px;
    z-index: 10;
}
#progress-fill { width: 100%; height: 0; background: linear-gradient(var(--accent), var(--accent-2)); border-radius: 3px; }
#progress-dots .dot {
    position: absolute; left: 50%;
    width: 9px; height: 9px;
    border-radius: 50%;
    transform: translate(-50%, -50%);
    background: #2a2f3d;
    border: 2px solid rgba(255,255,255,0.35);
    pointer-events: auto;
    cursor: pointer;
}
#progress-dots .dot.passed { background: var(--accent); border-color: var(--accent); }

/* ---------- Loading / toast / drop ---------- */
#loading {
    position: fixed; inset: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
    background: rgba(5, 6, 10, 0.7);
    z-index: 50;
}
.spinner { width: 42px; height: 42px; border: 3px solid rgba(255,255,255,0.15); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#loading .bar { width: 220px; height: 3px; background: rgba(255,255,255,0.12); border-radius: 3px; overflow: hidden; }
#loading-bar { height: 100%; width: 0; background: var(--accent); transition: width 0.1s; }
#loading-bar.indeterminate { width: 35%; animation: indeterminate 1.2s ease-in-out infinite; }
@keyframes indeterminate { from { transform: translateX(-100%); } to { transform: translateX(290%); } }

/* Pendant le chargement de l'expérience : écran opaque, interface de lecture masquée */
body.viewer-only #loading { background: var(--bg); }
body.is-loading #story { visibility: hidden; }

#toast {
    position: fixed;
    top: 64px; left: 50%;
    transform: translate(-50%, -10px);
    background: var(--panel-solid);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 9px 16px;
    opacity: 0;
    transition: opacity 0.25s, transform 0.25s;
    z-index: 60;
    pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { border-color: var(--danger); color: var(--danger); }

#drop-overlay {
    position: fixed; inset: 0;
    display: none; align-items: center; justify-content: center;
    font-size: 22px;
    background: rgba(79, 209, 255, 0.12);
    border: 3px dashed var(--accent);
    z-index: 70;
    pointer-events: none;
}
body.dragging #drop-overlay { display: flex; }

@media (max-width: 760px) {
    #editor { top: auto; left: 8px; right: 8px; bottom: 8px; width: auto; max-height: 48vh; }
    #fly-help { display: none; }
    .brand span:last-child { display: none; }
}

/* Écrans tactiles : indice « glisser » au lieu de la souris */
@media (pointer: coarse) {
    #scroll-hint .mouse { display: none; }
    #scroll-hint::after {
        content: "";
        width: 14px; height: 14px;
        border-left: 2px solid var(--muted);
        border-top: 2px solid var(--muted);
        transform: rotate(45deg);
        animation: swipe-up 1.6s ease-in-out infinite;
    }
    @keyframes swipe-up { 0%, 100% { translate: 0 6px; opacity: 0.4; } 50% { translate: 0 -4px; opacity: 1; } }
    #progress-rail { right: 12px; }
    .caption { left: 16px; right: 16px; bottom: 9vh; max-width: none; padding: 16px 18px; }
    .caption p { font-size: 14px; }
}

/* ---------- Démarrage : rôle choisi avant le script principal ---------- */
html.role-viewer #editor,
html.role-viewer #fly-help,
html.role-viewer .mode-switch { display: none !important; }
/* Expérience : écran de chargement visible dès la première image */
html.role-viewer:not(.app-started) #loading.hidden { display: flex !important; background: var(--bg); }
html.role-viewer:not(.app-started) #story { display: none; }

#boot-error {
    display: none;
    position: fixed;
    left: 16px; right: 16px; top: 16px;
    z-index: 100;
    background: #2a0f14;
    border: 1px solid var(--danger);
    color: #ffd7dc;
    border-radius: 12px;
    padding: 14px 16px;
    font-size: 13px;
}
#boot-error pre { white-space: pre-wrap; word-break: break-word; font-size: 12px; margin: 8px 0; }
#boot-error small { color: #c99; word-break: break-all; }
