/* Folia — ambiance « encre & papier » : noir d'encre chaud, texte couleur papier,
   accent vermillon (comme un sceau d'encre), trame de points façon manga en fond. */
@font-face { font-family: 'Fraunces'; font-style: normal; font-weight: 300 900; font-display: swap; src: url(lib/fonts/fraunces.woff2) format('woff2'); }
@font-face { font-family: 'Fraunces'; font-style: italic; font-weight: 300 900; font-display: swap; src: url(lib/fonts/fraunces-italic.woff2) format('woff2'); }
@font-face { font-family: 'Bricolage Grotesque'; font-style: normal; font-weight: 300 800; font-display: swap; src: url(lib/fonts/bricolage.woff2) format('woff2'); }
@font-face { font-family: 'JetBrains Mono'; font-style: normal; font-weight: 400 600; font-display: swap; src: url(lib/fonts/jetbrains-mono.woff2) format('woff2'); }

:root {
  --bg: #121110;
  --bg2: #1b1a18;
  --bg3: #252320;
  --line: #34312c;
  --text: #f1ebdf;
  --muted: #9b9488;
  --accent: #ff5a36;
  --accent-soft: rgba(255,90,54,.14);
  --accent-ink: #1a0c08;
  --danger: #ff6b6b;
  --radius: 10px;
  --display: 'Fraunces', Georgia, serif;
  --ui: 'Bricolage Grotesque', 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --ease: cubic-bezier(.2,.8,.2,1);
  font-family: var(--ui);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { overflow-x: hidden; -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
a { color: inherit; }
::selection { background: var(--accent); color: var(--accent-ink); }

/* ---------- fond : lueur vermillon + trame de points (screentone) ---------- */
#library { min-height: 100%; position: relative; isolation: isolate; }
#library::before {
  content: ""; position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background:
    radial-gradient(ellipse 60% 45% at 100% 0%, rgba(255,90,54,.16), transparent 70%),
    radial-gradient(ellipse 50% 40% at 0% 100%, rgba(241,235,223,.05), transparent 70%);
}
#library::after {
  content: ""; position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background-image: radial-gradient(rgba(241,235,223,.09) 1.1px, transparent 1.4px);
  background-size: 9px 9px;
  -webkit-mask-image: radial-gradient(ellipse 70% 60% at 100% 0%, #000 10%, transparent 75%);
          mask-image: radial-gradient(ellipse 70% 60% at 100% 0%, #000 10%, transparent 75%);
}

/* ---------- boutons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--bg3); color: var(--text); border: 1px solid var(--line);
  border-radius: 999px; padding: 8px 16px; font: inherit; font-size: 14px; font-weight: 500;
  cursor: pointer; white-space: nowrap; user-select: none; text-decoration: none;
  transition: border-color .15s, background .15s, transform .15s var(--ease);
}
.btn:hover { border-color: #57524a; }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.btn.primary:hover { background: #ff7150; }
.btn.danger { background: transparent; color: var(--danger); border-color: #5c2a26; }
.btn.ghost { background: rgba(18,17,16,.72); border-color: rgba(241,235,223,.14); backdrop-filter: blur(8px); }
.btn.icon { padding: 8px 12px; font-size: 16px; }
.btn.warn { border-color: var(--accent); color: var(--accent); }
.folder-row { margin-top: -6px !important; margin-bottom: 6px; }
#folderName { font-family: var(--display); font-weight: 700; font-size: 18px; }
.btn.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* ---------- bibliothèque ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  padding: 14px 32px; background: rgba(18,17,16,.82); backdrop-filter: blur(12px) saturate(1.2);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--accent); }
.brand svg { width: 30px; height: 30px; transition: transform .4s var(--ease); }
.brand:hover svg { transform: rotate(-12deg); }
.brand span { font-family: var(--display); font-style: italic; font-weight: 700; font-size: 28px; letter-spacing: -.02em; color: var(--text); font-variation-settings: "opsz" 144; }
#search {
  flex: 1; min-width: 160px; max-width: 420px;
  background: var(--bg2); border: 1px solid var(--line); color: var(--text);
  border-radius: 999px; padding: 10px 16px; font: inherit; font-size: 14px;
  transition: border-color .15s, box-shadow .15s;
}
#search:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.actions { display: flex; gap: 8px; margin-left: auto; flex-wrap: wrap; }
.btn .sm { display: none; }

#libMain { padding: 40px 32px 80px; max-width: 1500px; margin: 0 auto; }
#breadcrumb { margin-bottom: 34px; display: flex; align-items: baseline; gap: 18px; flex-wrap: wrap; }
#breadcrumb h1 {
  font-family: var(--display); font-weight: 800; font-size: clamp(40px, 6vw, 72px); line-height: .95;
  letter-spacing: -.035em; margin: 0; font-variation-settings: "opsz" 144;
}
#breadcrumb h1 em { font-style: italic; font-weight: 300; color: var(--accent); }
#breadcrumb .meta { font-family: var(--mono); color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .1em; }
#breadcrumb .btn { align-self: center; }
.section-title {
  display: flex; align-items: center; gap: 12px;
  font-family: var(--mono); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .16em; color: var(--muted); margin: 6px 0 18px;
}
.section-title::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.section-title::after { content: ""; flex: 1; height: 1px; background: var(--line); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(156px, 1fr)); gap: 34px 22px; }
.row-scroll { display: grid; grid-auto-flow: column; grid-auto-columns: 156px; gap: 22px; overflow-x: auto; padding: 6px 0 14px; margin-bottom: 40px; scrollbar-width: thin; }

.card { cursor: pointer; position: relative; }
.animate .card { animation: rise .55s var(--ease) both; animation-delay: calc(min(var(--i, 0), 18) * 35ms); }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } }
.card .cover {
  aspect-ratio: 2 / 3; border-radius: 3px 6px 6px 3px; overflow: hidden; background: var(--bg2);
  position: relative;
  box-shadow: 0 1px 0 rgba(255,255,255,.06) inset, 0 10px 24px -8px rgba(0,0,0,.7), 0 2px 4px rgba(0,0,0,.4);
  transition: transform .35s var(--ease), box-shadow .35s var(--ease);
}
/* reliure : ombre et reflet sur le bord gauche, comme un vrai livre */
.card .cover::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(90deg, rgba(0,0,0,.45) 0, rgba(255,255,255,.12) 2.5%, rgba(0,0,0,.18) 5%, transparent 9%);
}
.card:hover .cover { transform: translateY(-6px) rotate(-1deg); box-shadow: 0 1px 0 rgba(255,255,255,.06) inset, 0 22px 36px -12px rgba(0,0,0,.85), 0 0 0 1px var(--accent); }
.card .cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .stack::before, .card .stack::after {
  content: ""; position: absolute; inset: 0; border-radius: 3px 6px 6px 3px; background: var(--bg3); border: 1px solid var(--line); z-index: -1;
  transition: transform .35s var(--ease);
}
.card .stack { position: relative; z-index: 0; }
.card .stack::before { transform: translate(5px, -5px); }
.card .stack::after { transform: translate(10px, -10px); opacity: .55; }
.card:hover .stack::before { transform: translate(8px, -9px) rotate(1.5deg); }
.card:hover .stack::after { transform: translate(15px, -16px) rotate(3deg); }
.card .name { margin-top: 12px; font-family: var(--display); font-size: 17px; font-weight: 600; line-height: 1.15; letter-spacing: -.01em; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; font-variation-settings: "opsz" 24; }
.card .sub { font-family: var(--mono); font-size: 11px; color: var(--muted); margin-top: 5px; letter-spacing: .02em; }
.badge {
  position: absolute; top: 8px; left: 8px; z-index: 1; font-family: var(--mono); font-size: 11px; font-weight: 600;
  background: rgba(18,17,16,.82); color: var(--text); padding: 3px 7px; border-radius: 4px; backdrop-filter: blur(4px);
}
/* le sens de lecture façon tampon */
.badge.dir {
  left: auto; right: 8px; background: var(--accent); color: var(--accent-ink);
  text-transform: uppercase; letter-spacing: .08em; font-size: 10px; transform: rotate(3deg);
}
.progress { position: absolute; left: 0; right: 0; bottom: 0; height: 4px; background: rgba(0,0,0,.55); z-index: 1; }
.progress > i { display: block; height: 100%; background: var(--accent); box-shadow: 0 0 10px var(--accent); }
.card .more {
  position: absolute; top: 8px; right: 8px; z-index: 2; opacity: 0;
  background: rgba(18,17,16,.85); color: var(--text); border: 1px solid rgba(241,235,223,.15); border-radius: 50%; width: 30px; height: 30px; cursor: pointer; font-size: 15px;
  transition: opacity .15s;
}
.card:hover .more, .card .more:focus { opacity: 1; }
.card.volume .badge.dir { display: none; }
@media (hover: none) { .card .more { opacity: 1; } }

.empty { text-align: center; padding: 60px 20px; color: var(--muted); max-width: 520px; margin: 0 auto; }
.empty h2 { color: var(--text); font-family: var(--display); font-size: 40px; font-weight: 800; letter-spacing: -.03em; margin: 44px 0 12px; line-height: 1; }
.empty h2 em { font-style: italic; font-weight: 300; color: var(--accent); }
.empty p { line-height: 1.6; }
.empty-art {
  display: grid; grid-template-columns: 1.4fr 1fr; grid-template-rows: 1fr 1.2fr; gap: 7px; width: 132px; height: 170px; margin: 0 auto;
  padding: 9px; background: var(--text); border-radius: 3px; transform: rotate(-4deg);
  box-shadow: 14px 14px 0 var(--accent);
}
.empty-art span { border: 2.5px solid var(--bg); border-radius: 1px; background-image: radial-gradient(rgba(18,17,16,.35) 1px, transparent 1.3px); background-size: 5px 5px; }
.empty-art span:nth-child(1) { background-image: none; }
.empty-art span:nth-child(3) { grid-column: span 2; }
.empty-art span:nth-child(4) { display: none; }

#dropOverlay { position: fixed; inset: 0; z-index: 50; background: rgba(255,90,54,.10); border: 3px dashed var(--accent); display: grid; place-items: center; pointer-events: none; }
#dropOverlay div { background: var(--bg2); padding: 20px 32px; border-radius: 14px; font-family: var(--display); font-style: italic; font-size: 26px; font-weight: 700; }

/* ---------- lecteur ---------- */
#reader { position: fixed; inset: 0; background: #0a0a09; overflow: hidden; user-select: none; }
#stage { position: absolute; inset: 0; overflow: hidden; cursor: pointer; touch-action: none; }
#stage.dragging { cursor: grabbing; }
#pageWrap { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
#pageWrap.anim { transition: transform .38s cubic-bezier(.3,.7,.25,1); }
#pageCanvas { display: block; }
#panelOverlay { position: absolute; left: 0; top: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
#panelOverlay rect { fill: rgba(255,90,54,.10); stroke: var(--accent); stroke-width: 3; vector-effect: non-scaling-stroke; }
#panelOverlay rect.current { fill: rgba(255,90,54,.25); stroke: #fff; }
#panelOverlay text { fill: #fff; stroke: #000; stroke-width: 4px; paint-order: stroke; font-weight: 800; font-family: var(--mono); }
/* le masque assombrit ce qui est autour de la case affichée */
#mask { position: absolute; pointer-events: none; box-shadow: 0 0 0 200vmax rgba(10,10,9,.93); border-radius: 2px; transition: all .38s cubic-bezier(.3,.7,.25,1); opacity: 0; }
#mask.show { opacity: 1; }
#loading .btn { pointer-events: auto; font-family: var(--ui); font-style: normal; font-size: 15px; }
#loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); pointer-events: none; font-family: var(--display); font-style: italic; font-size: 20px; }

#hud { position: absolute; inset: 0; pointer-events: none; opacity: 0; transition: opacity .2s; }
#hud.show { opacity: 1; }
#hud.show .hud-top, #hud.show .hud-bottom { pointer-events: auto; }
.hud-top { display: flex; align-items: center; gap: 14px; padding: 14px 18px; background: linear-gradient(rgba(0,0,0,.82), transparent); flex-wrap: wrap; }
#bookTitle { font-family: var(--display); font-weight: 700; font-size: 19px; letter-spacing: -.01em; flex: 1; min-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hud-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.hud-bottom { position: absolute; left: 0; right: 0; bottom: 0; display: flex; align-items: center; gap: 16px; padding: 20px 22px; background: linear-gradient(transparent, rgba(0,0,0,.86)); font-family: var(--mono); font-size: 12px; color: var(--muted); }
#pageSlider { flex: 1; accent-color: var(--accent); }
#pageSlider.rtl { direction: rtl; }

/* ---------- fenêtres ---------- */
dialog { background: var(--bg2); color: var(--text); border: 1px solid var(--line); border-radius: 18px; padding: 26px; width: min(460px, calc(100vw - 32px)); box-shadow: 0 30px 80px rgba(0,0,0,.6); }
dialog[open] { animation: pop .3s var(--ease); }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.98); } }
dialog::backdrop { background: rgba(8,8,7,.7); backdrop-filter: blur(3px); }
dialog h3 { margin: 0 0 18px; font-family: var(--display); font-size: 28px; font-weight: 800; letter-spacing: -.02em; }
dialog label { display: block; font-family: var(--mono); font-size: 11px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin-bottom: 14px; }
dialog label.check { display: flex; gap: 10px; align-items: flex-start; color: var(--text); font-family: var(--ui); font-size: 14px; text-transform: none; letter-spacing: 0; }
dialog input[type=checkbox] { accent-color: var(--accent); width: 16px; height: 16px; margin: 1px 0 0; }
dialog input:not([type=checkbox]), dialog select {
  display: block; width: 100%; margin-top: 6px; background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font: inherit; font-family: var(--ui); font-size: 15px; text-transform: none; letter-spacing: 0;
}
dialog input:focus, dialog select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
dialog .row { display: flex; gap: 8px; margin-top: 22px; align-items: center; }
.spacer { flex: 1; }
.hint { font-size: 12px; color: var(--muted); }

.toast { position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 60; background: var(--text); color: var(--bg); padding: 11px 20px; border-radius: 999px; font-size: 14px; font-weight: 600; box-shadow: 0 12px 30px rgba(0,0,0,.5); max-width: calc(100vw - 32px); animation: toastIn .3s var(--ease); }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } }
#toastReader { position: absolute; bottom: 80px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
@media (max-width: 640px) {
  .topbar { padding: 10px 16px; gap: 12px; }
  .brand span { font-size: 24px; }
  .btn .lg { display: none; } .btn .sm { display: inline; }
  .actions label:has(#folderInput) { display: none; } /* choisir un dossier n'a pas de sens sur téléphone */
  .actions { margin-left: 0; width: 100%; } .actions .btn:not(.icon) { flex: 1; justify-content: center; }
  #search { max-width: none; }
  #libMain { padding: 24px 16px 60px; }
  #breadcrumb { margin-bottom: 22px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 26px 14px; }
  .row-scroll { grid-auto-columns: 124px; }
}
.card .sub .miss { color: var(--accent); }

/* ---------- profils ---------- */
.avatar {
  --c: var(--accent);
  display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 50%;
  background: var(--c); color: #120c0a; font-family: var(--display); font-weight: 800; font-size: 14px; line-height: 1;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.18);
}
.profile-btn { padding: 4px; border-radius: 50%; }
#profiles {
  position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 24px; overflow: auto;
  background: radial-gradient(ellipse 60% 50% at 50% 0%, rgba(255,90,54,.14), transparent 70%), var(--bg);
}
.profiles-inner { text-align: center; max-width: 900px; }
#profiles h1 { font-family: var(--display); font-weight: 800; font-size: clamp(40px, 7vw, 76px); letter-spacing: -.035em; margin: 0 0 40px; font-variation-settings: "opsz" 144; }
#profiles h1 em { font-style: italic; font-weight: 300; color: var(--accent); }
#profileGrid { display: flex; flex-wrap: wrap; justify-content: center; gap: 28px; margin-bottom: 44px; }
.profile { background: none; border: 0; color: var(--text); cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 12px; position: relative; font: inherit; animation: rise .5s var(--ease) both; }
.profile:nth-child(2) { animation-delay: .05s; } .profile:nth-child(3) { animation-delay: .1s; } .profile:nth-child(4) { animation-delay: .15s; } .profile:nth-child(n+5) { animation-delay: .2s; }
.avatar.big { width: 112px; height: 112px; font-size: 52px; border-radius: 28px; transition: transform .25s var(--ease), box-shadow .25s var(--ease); box-shadow: inset 0 -8px 0 rgba(0,0,0,.18), 0 12px 30px -10px rgba(0,0,0,.7); }
.profile:hover .avatar.big, .profile:focus-visible .avatar.big { transform: translateY(-4px) rotate(-3deg); box-shadow: inset 0 -8px 0 rgba(0,0,0,.18), 0 0 0 3px var(--bg), 0 0 0 5px var(--text); }
.avatar.plus { background: transparent; color: var(--muted); border: 2px dashed var(--line); box-shadow: none; font-family: var(--ui); font-weight: 300; }
.profile.edit .avatar.big { opacity: .55; }
.profile .pen { position: absolute; top: 40px; font-size: 28px; pointer-events: none; }
.pname { font-family: var(--display); font-size: 19px; font-weight: 600; color: var(--muted); }
.profile:hover .pname { color: var(--text); }
#profileColors { display: flex; gap: 10px; flex-wrap: wrap; margin: -6px 0 8px; }
.swatch { width: 32px; height: 32px; border-radius: 50%; background: var(--c); border: 0; cursor: pointer; }
.swatch.on { box-shadow: 0 0 0 3px var(--bg2), 0 0 0 5px var(--text); }
@media (max-width: 640px) { .avatar.big { width: 88px; height: 88px; font-size: 40px; border-radius: 22px; } .profile .pen { top: 28px; } #profileGrid { gap: 20px; } }
