:root {
  --bg: #14121a;
  --bg-2: #1d1a25;
  --bg-3: #29253a;
  --line: #ffffff14;
  --text: #f3f0f7;
  --muted: #a39db3;
  --accent: #ff5a5f;
  --accent-2: #ffb347;
  --danger: #ff6b6b;
  --gb: #8bac0f;
  --gbc: #7b5cff;
  --gba: #5a6cff;
  --nds: #c9ccd6;
  --radius: 16px;
  --controls-opacity: 0.75;
  --safe-t: env(safe-area-inset-top);
  --safe-r: env(safe-area-inset-right);
  --safe-b: env(safe-area-inset-bottom);
  --safe-l: env(safe-area-inset-left);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2 { margin: 0; letter-spacing: -0.01em; }

/* ---------- Boutons communs ---------- */
.primary, .ghost, .danger {
  border: 0; border-radius: 12px; padding: 13px 16px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  transition: transform .08s ease, background .15s ease;
}
.primary { background: var(--accent); color: #fff; }
.primary:active, .ghost:active { transform: scale(.97); }
.ghost { background: var(--bg-3); color: var(--text); }
.ghost:disabled { opacity: .4; }
.danger, .ghost.danger { background: #ff6b6b1f; color: var(--danger); }
.link { background: none; border: 0; color: var(--accent-2); padding: 4px 0; font-weight: 600; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--bg-3);
  display: grid; place-items: center; color: var(--text);
}

/* ---------- Bibliothèque ---------- */
body.library {
  min-height: 100vh;
  padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 32px) calc(var(--safe-l) + 16px);
  background:
    radial-gradient(1200px 500px at 85% -10%, #ff5a5f22, transparent 60%),
    radial-gradient(900px 500px at -10% 10%, #5a6cff1f, transparent 60%),
    var(--bg);
}
.lib-header { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 20px; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand img { border-radius: 10px; }
.brand h1 { font-size: 26px; font-weight: 800; }
.brand p { margin: 0; color: var(--muted); font-size: 12px; letter-spacing: .12em; font-weight: 600; }
main { max-width: 980px; margin: 0 auto; }
.primary.big { width: 100%; font-size: 17px; padding: 16px; border-radius: 14px; box-shadow: 0 8px 24px #ff5a5f40; }
.hint { color: var(--muted); font-size: 13px; margin: 10px 2px 20px; }
.legal { color: var(--muted); font-size: 12px; text-align: center; margin-top: 36px; }

.install-banner {
  display: grid; gap: 4px; padding: 14px 16px; border-radius: var(--radius);
  background: linear-gradient(135deg, #ffb34722, #ff5a5f1a); border: 1px solid #ffb34740; margin-bottom: 22px; font-size: 14px;
}
.install-banner[hidden] { display: none; }
.install-banner span { color: #e9e2f0; }
.install-banner .link { justify-self: start; }

section { margin-top: 26px; }
section[hidden] { display: none; }
section h2 { font-size: 20px; font-weight: 750; margin-bottom: 12px; }
.sec-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.sec-head h2 { margin: 0; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
.rail { display: grid; grid-auto-flow: column; grid-auto-columns: 128px; gap: 12px; overflow-x: auto; scroll-snap-type: x mandatory; padding-bottom: 6px; margin: 0 -16px; padding-left: 16px; padding-right: 16px; scrollbar-width: none; }
.rail::-webkit-scrollbar { display: none; }
.rail .card { scroll-snap-align: start; }

.card { position: relative; }
.card-hit { display: block; width: 100%; padding: 0; border: 0; background: none; text-align: left; border-radius: var(--radius); }
.card-hit:active .cover { transform: scale(.97); }
.card.missing .cover { filter: saturate(.35) brightness(.7); }
.card.missing .cover::after {
  content: "+"; position: absolute; right: 10px; bottom: 8px; width: 30px; height: 30px; border-radius: 50%;
  background: #ffffffe6; color: #14121a; display: grid; place-items: center; font-size: 22px; font-weight: 700;
}
.cover {
  position: relative; aspect-ratio: 3 / 4; border-radius: var(--radius); overflow: hidden;
  background: linear-gradient(150deg, var(--c1), var(--c2));
  box-shadow: inset 0 0 0 1px #ffffff1f, 0 10px 24px #00000059;
  padding: 12px; display: flex; flex-direction: column; justify-content: flex-end;
  transition: transform .12s ease;
}
/* encoche façon cartouche */
.cover::before {
  content: ""; position: absolute; top: 0; left: 50%; transform: translateX(-50%);
  width: 46%; height: 9px; border-radius: 0 0 8px 8px; background: #0000003d;
}
.cover-title { font-weight: 800; font-size: 17px; line-height: 1.12; text-shadow: 0 2px 10px #0000005c; color: #fff; overflow-wrap: anywhere; }
.cover-year { font-size: 12px; color: #ffffffc0; margin-top: 4px; font-weight: 600; }
.small .cover-title { font-size: 14px; }
.badge {
  position: absolute; top: 16px; left: 12px; font-size: 11px; font-weight: 800; letter-spacing: .08em;
  padding: 3px 7px; border-radius: 6px; background: #00000052; color: #fff;
}
.card-meta { display: flex; align-items: center; justify-content: space-between; padding: 8px 2px 0; min-height: 34px; }
.card-status { font-size: 13px; color: var(--muted); }
.card.ready .card-status { color: #bdf5c9; }
.small .card-status { color: var(--muted) !important; font-size: 12px; }
.more { border: 0; background: none; color: var(--muted); font-size: 22px; line-height: 1; width: 36px; height: 32px; border-radius: 8px; }
.more:active { background: var(--bg-3); }

.chips { display: flex; gap: 6px; }
.chip { border: 1px solid var(--line); background: var(--bg-2); color: var(--muted); border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 600; }
.chip.on { background: var(--text); color: var(--bg); border-color: transparent; }

.list { list-style: none; margin: 0; padding: 0; border-radius: var(--radius); overflow: hidden; background: var(--bg-2); border: 1px solid var(--line); }
.row { display: flex; align-items: center; border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: 0; }
.row-hit { flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px; padding: 12px 14px; border: 0; background: none; text-align: left; }
.row-hit:active { background: var(--bg-3); }
.row-badge { flex: none; width: 42px; height: 42px; border-radius: 10px; display: grid; place-items: center; font-size: 12px; font-weight: 800; color: #14121a; }
.row-badge.sys-gb { background: var(--gb); }
.row-badge.sys-gbc { background: var(--gbc); color: #fff; }
.row-badge.sys-gba { background: var(--gba); color: #fff; }
.row-badge.sys-nds { background: var(--nds); }
.row-text { min-width: 0; display: grid; }
.row-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 13px; color: var(--muted); }

/* ---------- Feuilles (dialog) ---------- */
dialog.sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 560px; max-height: 92vh;
  background: var(--bg-2); color: var(--text); border-radius: 22px 22px 0 0;
  box-shadow: 0 -10px 40px #00000080;
}
dialog.sheet[open] { animation: sheet-in .22s cubic-bezier(.2, .9, .3, 1); }
dialog.sheet::backdrop { background: #0000008c; backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
@keyframes sheet-in { from { transform: translateY(30px); opacity: .4; } to { transform: none; opacity: 1; } }
@media (min-width: 700px) { dialog.sheet { margin: auto; border-radius: 22px; } }
.sheet-body { padding: 18px 18px calc(var(--safe-b) + 18px); display: grid; gap: 12px; overflow-y: auto; max-height: 92vh; }
.sheet-title { font-size: 20px; font-weight: 750; }
.sheet-sub { margin: -6px 0 0; color: var(--muted); font-size: 14px; }
.tips { margin: 0; font-size: 14px; color: #ddd6e8; background: var(--bg-3); border-radius: 12px; padding: 10px 12px; }
.tips[hidden] { display: none; }
.sheet-actions { display: grid; gap: 8px; }
.sheet-actions.grid2 { grid-template-columns: 1fr 1fr; }
.field { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.field select, .field input[type=range] { width: 100%; }
.field select {
  appearance: none; -webkit-appearance: none; background: var(--bg-3); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; font: inherit; font-size: 16px;
}
input[type=range] { accent-color: var(--accent); }
.toggle { display: flex; align-items: center; gap: 10px; font-size: 15px; }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }
.storage { font-size: 13px; color: var(--muted); }
.muted { color: var(--muted); font-weight: 400; }

.slots { display: grid; gap: 6px; }
.slots-head { font-weight: 650; font-size: 15px; }
.slot { display: grid; grid-template-columns: 28px 1fr auto auto; gap: 8px; align-items: center; background: var(--bg-3); border-radius: 12px; padding: 6px 6px 6px 12px; }
.slot-name { font-weight: 800; color: var(--accent-2); }
.slot-date { color: var(--muted); font-size: 13px; }
.ghost.sm { padding: 8px 12px; font-size: 14px; background: #ffffff12; }
.ghost.wide { width: 100%; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 24px); transform: translate(-50%, 20px);
  background: #f3f0f7; color: #14121a; padding: 10px 12px 10px 16px; border-radius: 14px; font-size: 14px; font-weight: 600;
  display: flex; align-items: center; gap: 12px; width: max-content; max-width: calc(100vw - 32px);
  box-shadow: 0 10px 30px #00000080; opacity: 0; pointer-events: none; transition: .2s ease; z-index: 100;
  margin: 0; border: 0; inset: auto auto calc(var(--safe-b) + 24px) 50%; overflow: visible;
}
#toast:popover-open { display: flex; }
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast button { border: 0; background: var(--accent); color: #fff; font-weight: 700; padding: 7px 12px; border-radius: 10px; white-space: nowrap; }
body.player #toast { inset: calc(var(--safe-t) + 56px) auto auto 50%; }

.drop-overlay {
  position: fixed; inset: 12px; border: 2px dashed var(--accent); border-radius: 24px; background: #14121ae6;
  display: grid; place-items: center; font-size: 22px; font-weight: 700; opacity: 0; pointer-events: none; transition: .15s; z-index: 50;
}
.drop-overlay.show { opacity: 1; }

/* ---------- Écran de jeu ---------- */
body.player {
  position: fixed; inset: 0; overflow: hidden; background: #000;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none;
}
/* z-index + isolation : les z-index internes d'EmulatorJS restent sous la manette et la barre */
#screen-wrap { position: absolute; background: #000; overflow: hidden; z-index: 1; isolation: isolate; }
#game { width: 100%; height: 100%; }
/* masque l'interface intégrée d'EmulatorJS : nous avons la nôtre */
#game .ejs_menu_bar, #game .ejs_menu_bar_hidden, #game .ejs_virtualGamepad_parent, #game .ejs_start_button,
#game .ejs_context_menu, #game .ejs_menu_button { display: none !important; }
#game .ejs_parent, #game .ejs_game, #game .ejs_canvas_parent { width: 100% !important; height: 100% !important; background: #000 !important; }
#game canvas { touch-action: none; }
.sys-gb #game canvas, .sys-gbc #game canvas, .sys-gba #game canvas { image-rendering: pixelated; }

#loading {
  position: fixed; inset: 0; display: grid; place-content: center; justify-items: center; gap: 16px; padding: 24px;
  background: radial-gradient(600px 400px at 50% 40%, #29253a, #000); z-index: 30; text-align: center; transition: opacity .3s;
}
#loading.hide { opacity: 0; pointer-events: none; }
#loading p { margin: 0; color: var(--muted); max-width: 320px; }
#loading.error p { color: var(--danger); }
#loading.error .spinner { display: none; }
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 3px solid #ffffff22; border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.hud {
  position: fixed; top: calc(var(--safe-t) + 4px); left: calc(var(--safe-l) + 8px); right: calc(var(--safe-r) + 8px);
  height: 40px; display: flex; align-items: center; gap: 8px; z-index: 40; pointer-events: none;
}
.hud > * { pointer-events: auto; }
.hud-title { flex: 1; min-width: 0; text-align: center; font-size: 14px; font-weight: 600; color: #ffffffb3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; pointer-events: none; }
.hud-btn {
  height: 38px; min-width: 38px; padding: 0 10px; border: 0; border-radius: 19px; background: #ffffff1a; color: #fff;
  display: inline-flex; align-items: center; justify-content: center; gap: 4px; font-size: 13px; font-weight: 700;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.hud-btn:active { background: #ffffff33; }
.hud-btn.on { background: var(--accent); }
#btn-ff { order: 3; }
#btn-menu { order: 4; }
body.landscape .hud-title { visibility: hidden; }

/* Manette */
#controls { position: fixed; z-index: 20; opacity: var(--controls-opacity); touch-action: none; }
body.controls-hidden #controls { display: none; }
body.portrait #controls {
  top: var(--ctl-top, 60%); left: 0; right: 0; bottom: 0;
  padding: 4px calc(var(--safe-r) + 14px) calc(var(--safe-b) + 6px) calc(var(--safe-l) + 14px);
  display: flex; justify-content: space-between; align-items: stretch;
}
body.landscape #controls { inset: 0; pointer-events: none; }
body.landscape .pad-group { pointer-events: auto; position: absolute; top: 0; bottom: 0; width: var(--side); padding: calc(var(--safe-t) + 50px) 8px calc(var(--safe-b) + 10px); }
body.landscape .pad-group.left { left: var(--safe-l); }
body.landscape .pad-group.right { right: var(--safe-r); }

.pad-group { display: flex; flex-direction: column; align-items: center; justify-content: space-between; gap: 6px; width: 48%; }
.pad-group.left .shoulder { align-self: flex-start; }
.pad-group.right .shoulder { align-self: flex-end; }
body.landscape .pad-group .shoulder { align-self: stretch; }

#controls button { border: 0; padding: 0; margin: 0; touch-action: none; user-select: none; -webkit-user-select: none; }
.shoulder {
  width: 96px; height: 38px; border-radius: 12px 12px 8px 8px; background: #3a3646; color: #e7e3ef;
  font-weight: 800; font-size: 15px; box-shadow: inset 0 -3px 0 #0000004d;
}
.sys-gb .shoulder, .sys-gbc .shoulder { visibility: hidden; }

.dpad { --s: var(--pad-s, min(40vw, 150px)); position: relative; width: var(--s); height: var(--s); flex: none; touch-action: none; }
.dpad .d, .dpad .d-center { position: absolute; background: #3a3646; box-shadow: inset 0 -3px 0 #0000004d; }
.dpad .d { width: 34%; height: 34%; }
.dpad .up { left: 33%; top: 0; border-radius: 8px 8px 0 0; }
.dpad .down { left: 33%; bottom: 0; border-radius: 0 0 8px 8px; }
.dpad .left { left: 0; top: 33%; border-radius: 8px 0 0 8px; }
.dpad .right { right: 0; top: 33%; border-radius: 0 8px 8px 0; }
.dpad .d-center { left: 33%; top: 33%; width: 34%; height: 34%; box-shadow: none; }
.dpad .d::after { content: ""; position: absolute; inset: 35%; border-radius: 50%; background: #ffffff1f; }
.dpad .d.pressed, .shoulder.pressed, .fb.pressed, .pill.pressed { filter: brightness(1.6); }
.dpad .d.pressed { background: #5a5470; }

.face { --s: var(--pad-s, min(40vw, 150px)); position: relative; width: var(--s); height: var(--s); flex: none; }
.fb {
  position: absolute; width: 40%; height: 40%; border-radius: 50%; font-weight: 800; font-size: 18px; color: #fff;
  box-shadow: inset 0 -4px 0 #00000059, 0 2px 8px #00000059;
}
/* GB / GBA : A en haut à droite, B en bas à gauche, comme sur la console */
.fb.a { right: 2%; top: 14%; background: #c8324a; }
.fb.b { left: 2%; bottom: 14%; background: #c8324a; }
/* DS : losange X / A / B / Y */
.sys-nds .fb.x { left: 30%; top: 0; background: #4a6fd1; }
.sys-nds .fb.a { right: 0; top: 30%; background: #c8324a; }
.sys-nds .fb.b { left: 30%; bottom: 0; background: #d1a33a; }
.sys-nds .fb.y { left: 0; top: 30%; background: #3a9d5d; }
.sys-nds .fb { width: 38%; height: 38%; }

.meta-row { display: flex; gap: 8px; justify-content: center; min-height: 30px; }
.pill { height: 28px; padding: 0 12px !important; border-radius: 14px; background: #3a3646; color: #cfc9da; font-size: 11px; font-weight: 800; letter-spacing: .06em; }
.pill.unsupported { display: none; }
.ds-only, .ds-only-block { display: none; }
.sys-nds .ds-only { display: inline-flex; align-items: center; }
.sys-nds .ds-only.fb { display: block; }
.sys-nds .ds-only-block { display: block; }
.sys-nds .pill { padding: 0 10px !important; }

/* Petits écrans (iPhone SE) */
@media (max-height: 700px) and (orientation: portrait) {
  .shoulder { height: 32px; width: 84px; }
}

/* ---------- Plein écran : manette transparente posée sur le jeu ---------- */
[hidden] { display: none !important; }
body.overlay #controls { pointer-events: none; }
body.overlay .pad-group { pointer-events: none !important; }
body.overlay #controls button, body.overlay .dpad { pointer-events: auto; }
body.overlay.portrait #controls { background: none; }

/* Touches en verre : fond très léger, contour blanc, lettres lisibles sur tous les jeux */
body.overlay .shoulder,
body.overlay .pill,
body.overlay .fb,
body.overlay .dpad .d,
body.overlay .dpad .d-center {
  background: rgb(255 255 255 / 0.16) !important;
  color: #fff;
  box-shadow: none;
  text-shadow: 0 1px 3px rgb(0 0 0 / 0.8);
  filter: none;
}
body.overlay .shoulder, body.overlay .pill, body.overlay .fb { border: 2px solid rgb(255 255 255 / 0.7) !important; }
body.overlay .dpad .d-center { background: rgb(255 255 255 / 0.16) !important; }
body.overlay .dpad .up { box-shadow: inset 2px 2px 0 rgb(255 255 255 / .7), inset -2px 0 0 rgb(255 255 255 / .7); }
body.overlay .dpad .down { box-shadow: inset 2px -2px 0 rgb(255 255 255 / .7), inset -2px 0 0 rgb(255 255 255 / .7); }
body.overlay .dpad .left { box-shadow: inset 2px 2px 0 rgb(255 255 255 / .7), inset 0 -2px 0 rgb(255 255 255 / .7); }
body.overlay .dpad .right { box-shadow: inset -2px 2px 0 rgb(255 255 255 / .7), inset 0 -2px 0 rgb(255 255 255 / .7); }
body.overlay .dpad .d::after { background: rgb(255 255 255 / 0.45); }
/* Une touche enfoncée s'illumine pour qu'on la voie même très transparente */
body.overlay .shoulder.pressed,
body.overlay .pill.pressed,
body.overlay .fb.pressed,
body.overlay .dpad .d.pressed { background: rgb(255 255 255 / 0.55) !important; }

/* La barre du haut se fait discrète sur l'image */
body.overlay .hud-title { visibility: hidden; }
body.overlay .hud-btn { background: rgb(0 0 0 / 0.25); opacity: .7; }
body.overlay .hud-btn.on { background: var(--accent); opacity: 1; }

/* ---------- Pochettes ---------- */
.cover { aspect-ratio: 1 / 1; }
.cover .art-bg, .cover .art { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; transition: opacity .25s ease; }
.cover .art-bg { object-fit: cover; filter: blur(14px) saturate(1.3) brightness(.75); transform: scale(1.2); }
.cover .art { object-fit: contain; }
.cover.has-art.art-ready .art-bg, .cover.has-art.art-ready .art { opacity: 1; }
/* Avec la pochette, le titre et l'année sont déjà sur l'image : on les cache */
.cover.has-art.art-ready .cover-title, .cover.has-art.art-ready .cover-year { display: none; }
.cover.has-art.art-ready::before { display: none; }
.cover.has-art .badge { z-index: 1; background: #000000a6; top: 8px; left: 8px; }
.card.missing .cover.art-ready { filter: saturate(.25) brightness(.6); }
.card.missing .cover::after { z-index: 2; }
.row-badge { position: relative; overflow: hidden; }
.row-badge img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

/* ---------- Réglages : section sauvegardes ---------- */
.sheet-section { display: grid; gap: 8px; padding-top: 4px; border-top: 1px solid var(--line); }
.section-title { font-weight: 700; font-size: 16px; padding-top: 10px; }
.section-hint { margin: 0; color: var(--muted); font-size: 13px; }

/* ---------- Progression d'import ---------- */
.progress {
  position: fixed; inset: auto auto calc(var(--safe-b) + 24px) 50%; transform: translateX(-50%);
  width: min(420px, calc(100vw - 32px)); margin: 0; border: 0; z-index: 110;
  background: #f3f0f7; color: #14121a; border-radius: 14px; padding: 12px 14px;
  display: grid; gap: 8px; font-size: 14px; font-weight: 600; box-shadow: 0 10px 30px #00000080; overflow: hidden;
}
.progress[hidden] { display: none !important; }
.progress-bar { height: 6px; border-radius: 3px; background: #14121a1f; overflow: hidden; }
.progress-bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 3px; transition: width .15s linear; }
.progress.indeterminate .progress-bar i { width: 35% !important; animation: progress-slide 1.1s ease-in-out infinite; }
@keyframes progress-slide { from { transform: translateX(-100%); } to { transform: translateX(290%); } }

/* ---------- Retour en arrière ---------- */
.sys-nds #btn-rewind { display: none; }
#btn-rewind { order: 2; touch-action: none; -webkit-user-select: none; user-select: none; }
#btn-overlay { order: 2; }

.engine-tag { font-size: 14px; font-weight: 500; }
