/* Lecteur de magazine — Arthur Loyd Bretagne
   Palette sobre, accent = rouge de la charte (#E40521). Thèmes sombre (défaut) et clair. */

:root {
  --al-red: #e40521;
  --al-gray: #444153;

  --bg: #0d0d11;
  --bg-glow: #1b1b22;
  --surface: rgba(24, 24, 30, 0.72);
  --surface-solid: #16161c;
  --border: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.18);
  --text: #f4f4f5;
  --text-dim: #a1a1aa;
  --shadow-page: 0 24px 60px -12px rgba(0, 0, 0, 0.75), 0 4px 14px rgba(0, 0, 0, 0.5);
  --paper: #20202a;

  --radius: 10px;
  --dur-flip: 620ms;
  --dur-ui: 180ms;
  --topbar-h: 56px;
  --toolbar-h: 56px;
}

:root[data-theme='light'] {
  --bg: #e9e9ed;
  --bg-glow: #f7f7f9;
  --surface: rgba(255, 255, 255, 0.82);
  --surface-solid: #ffffff;
  --border: rgba(0, 0, 0, 0.1);
  --border-strong: rgba(0, 0, 0, 0.2);
  --text: #1b1b22;
  --text-dim: #5d5d6b;
  --shadow-page: 0 20px 50px -14px rgba(0, 0, 0, 0.35), 0 3px 10px rgba(0, 0, 0, 0.14);
  --paper: #eeece7;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background:
    radial-gradient(120% 90% at 50% 0%, var(--bg-glow) 0%, var(--bg) 62%),
    var(--bg);
  color: var(--text);
  font: 400 15px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: 12px; top: -60px; z-index: 100;
  background: var(--surface-solid); color: var(--text);
  padding: 10px 14px; border-radius: var(--radius); border: 1px solid var(--border-strong);
  transition: top var(--dur-ui);
}
.skip-link:focus { top: 12px; }

/* ---------- ossature ---------- */
.app {
  display: grid;
  /* minmax(0,1fr) : sans cela le min-content des barres élargit la colonne au-delà
     de la fenêtre, et la scène (donc la page) déborde. */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: var(--topbar-h) 1fr var(--toolbar-h);
  height: 100dvh;
  overflow: hidden;
}

.topbar, .toolbar {
  display: flex; align-items: center; gap: 10px;
  padding: 0 12px;
  min-width: 0; overflow: hidden;
  background: var(--surface);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  z-index: 5;
}
.topbar { border-bottom: 1px solid var(--border); justify-content: space-between; }
.toolbar { border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom); }

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand__mark { width: 4px; height: 26px; border-radius: 2px; background: var(--al-red); flex: none; }
.brand__text { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.brand__text strong { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand__text small { font-size: 12px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.topbar__actions { display: flex; align-items: center; gap: 4px; }

/* ---------- boutons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 12px;
  font: inherit; font-size: 14px; color: var(--text);
  background: transparent; border: 1px solid transparent; border-radius: 8px;
  cursor: pointer; text-decoration: none;
  transition: background var(--dur-ui), border-color var(--dur-ui), color var(--dur-ui), transform var(--dur-ui);
}
.btn--icon { width: 38px; min-width: 38px; padding: 0; }
.btn:hover { background: color-mix(in srgb, var(--text) 10%, transparent); }
.btn:active { transform: scale(0.96); }
.btn--ghost { border-color: var(--border-strong); }
.btn[aria-pressed='true'], .btn.is-active { background: color-mix(in srgb, var(--al-red) 18%, transparent); color: var(--text); }
.btn:focus-visible, input:focus-visible, a:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--al-red); outline-offset: 2px;
}
.btn[disabled] { opacity: 0.35; cursor: not-allowed; }

:root[data-theme='dark'] .icon-sun, :root[data-theme='light'] .icon-moon { display: none; }

/* ---------- scène ---------- */
.stage {
  position: relative;
  display: grid;
  place-items: center;
  padding: clamp(10px, 2.2vh, 28px) clamp(48px, 6vw, 96px);
  min-height: 0;
  touch-action: pan-y;
}

/* Les dimensions du cadre sont calculées en JS (voir fitFrame) : ni `aspect-ratio` ni
   max-width/max-height ne savent contraindre les deux axes à la fois sans déformer. */
.frame {
  position: relative;
  width: var(--fw, 100%);
  height: var(--fh, 100%);
  /* Perspective très lointaine : la page se soulève vers le lecteur presque à plat, sans
     grossir. Une valeur plus courte donnait un mouvement spectaculaire mais fatigant. */
  perspective: 7000px;
  perspective-origin: 50% 50%;
  /* le livre fait deux pages de large et se translate : on ne montre que la page en cours,
     la découpe se fait à la reliure */
  overflow: hidden;
  border-radius: 2px;
  box-shadow: var(--shadow-page);
}

.book {
  position: absolute; inset: 0;
  width: 200%; height: 100%;
  transform: translate3d(var(--tx, -50%), 0, 0);
  transform-style: preserve-3d;
  will-change: transform;
}

.slot {
  position: absolute; top: 0; height: 100%; width: 50%;
  background: var(--surface-solid);
  overflow: hidden;
}
.slot--left { left: 0; }
.slot--right { left: 50%; }
/* Emplacement libéré par la feuille en mouvement : fond papier, pas de contenu. Le dégradé
   évoque l'épaisseur du magazine, sinon la zone se lit comme un vide. */
.slot.is-blank {
  background: var(--paper);
  background-image: linear-gradient(to right, rgba(0, 0, 0, 0.13), rgba(0, 0, 0, 0) 22%);
}
.slot--left.is-blank {
  background-image: linear-gradient(to left, rgba(0, 0, 0, 0.13), rgba(0, 0, 0, 0) 22%);
}
.slot.is-blank > img { visibility: hidden; }
.slot.is-blank::after { opacity: 0.5; }
.slot img, .sheet img, .zoomview img {
  display: block; width: 100%; height: 100%; object-fit: contain;
  user-select: none; -webkit-user-drag: none;
}
/* pliure côté reliure : à peine suggérée */
.slot--right::before {
  content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 16px;
  background: linear-gradient(to right, rgba(0, 0, 0, 0.08), rgba(0, 0, 0, 0));
  pointer-events: none; z-index: 2;
}

/* Feuille en rotation. Elle n'est pas un plan rigide : `flip.js` la compose de bandes
   verticales articulées (positionnées en 3D) pour lui donner la courbure d'une vraie page. */
.sheet {
  position: absolute; top: 0; left: 50%; width: 50%; height: 100%;
  transform-style: preserve-3d;
  pointer-events: none;
  visibility: hidden;
}
.sheet.is-active { visibility: visible; }
/* les <img> de la feuille ne servent qu'au préchargement : le rendu passe par les bandes */
.sheet > img { display: none; }

.strip {
  position: absolute; top: 0; left: 0;
  /* léger chevauchement : sans lui, l'antialiasing laisse des lignes claires aux jointures */
  width: calc(var(--sw, 8%) + 0.6px);
  height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
}
.strip__face {
  position: absolute; inset: 0;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background-color: var(--surface-solid);
  background-repeat: no-repeat;
  background-size: var(--pw, 100%) 100%;
}
/* chaque bande n'affiche que sa tranche de la page */
.strip__face--front {
  background-position: calc(-1 * var(--i) * var(--sw)) 0;
}
/* le verso est vu retourné : on prend la tranche symétrique pour compenser le miroir */
.strip__face--back {
  transform: rotateY(180deg);
  background-position: calc((var(--i) + 1) * var(--sw) - var(--pw)) 0;
}
/* ombrage porté par l'inclinaison de la bande, piloté depuis flip.js */
.strip__face::after {
  content: ''; position: absolute; inset: 0;
  background: #000; opacity: var(--shade, 0);
}
/* hors flux : sert seulement à garder les pages de la feuille en cache */
.sheet-preload { position: absolute; width: 0; height: 0; overflow: hidden; }
/* ombre projetée par la feuille sur la page de destination : la lumière vient de la reliure,
   au bord droit de l'emplacement de gauche */
.cast {
  position: absolute; top: 0; left: 0; width: 50%; height: 100%;
  pointer-events: none; z-index: 3; opacity: var(--cast, 0);
  background: linear-gradient(to right, rgba(0, 0, 0, 0) 46%, rgba(0, 0, 0, 0.34) 100%);
}
/* symétrique : la feuille encore proche de l'horizontale assombrit la page de droite */
.slot--right::after {
  content: ''; position: absolute; inset: 0; z-index: 2; pointer-events: none;
  opacity: var(--cast2, 0);
  background: linear-gradient(to right, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0) 42%);
}

/* ---------- zones cliquables ---------- */
.hs { position: absolute; inset: 0; z-index: 4; }
.hs a, .hs button {
  position: absolute;
  display: block; padding: 0; margin: 0;
  border: 1.5px solid transparent; border-radius: 5px;
  background: transparent; cursor: pointer;
  font: inherit; color: transparent;
  transition: background var(--dur-ui), border-color var(--dur-ui), box-shadow var(--dur-ui);
}
.hs a:hover, .hs button:hover, .hs a:focus-visible, .hs button:focus-visible {
  background: color-mix(in srgb, var(--al-red) 16%, transparent);
  border-color: color-mix(in srgb, var(--al-red) 65%, transparent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--al-red) 16%, transparent);
}
.book.is-flipping .hs, .frame.is-busy .hs { pointer-events: none; opacity: 0; }

/* pastilles d'aide au repérage des liens, à la première visite d'une page */
.marks { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
.marks span {
  position: absolute;
  border-radius: 6px;
  border: 1.5px solid color-mix(in srgb, var(--al-red) 70%, transparent);
  background: color-mix(in srgb, var(--al-red) 12%, transparent);
  animation: markPulse 2.4s ease-out 1 both;
}
@keyframes markPulse {
  0%, 62% { opacity: 1; }
  100% { opacity: 0; }
}

/* surlignage des résultats de recherche */
.hs mark.hit {
  position: absolute; display: block; background: rgba(255, 214, 0, 0.42);
  border-radius: 3px; mix-blend-mode: multiply; animation: hitFade 4s ease-out both;
}
:root[data-theme='dark'] .hs mark.hit { mix-blend-mode: screen; background: rgba(255, 214, 0, 0.3); }
@keyframes hitFade { 0%, 70% { opacity: 1; } 100% { opacity: 0; } }

/* ---------- navigation latérale ---------- */
.nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 44px; height: 88px;
  display: grid; place-items: center;
  color: var(--text); background: var(--surface); backdrop-filter: blur(10px);
  border: 1px solid var(--border); border-radius: 12px;
  cursor: pointer; z-index: 6;
  transition: background var(--dur-ui), opacity var(--dur-ui), transform var(--dur-ui);
}
.nav svg { width: 26px; height: 26px; }
.nav--prev { left: 6px; }
.nav--next { right: 6px; }
.nav:hover { background: color-mix(in srgb, var(--text) 14%, var(--surface)); }
.nav[disabled] { opacity: 0.25; cursor: default; }

.hint {
  position: absolute; left: 50%; bottom: 8px; transform: translateX(-50%);
  margin: 0; padding: 6px 12px; z-index: 7;
  font-size: 12.5px; color: var(--text-dim);
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px;
  backdrop-filter: blur(10px);
  animation: hintFade 6s ease-out both;
}
@keyframes hintFade { 0%, 70% { opacity: 1; } 100% { opacity: 0; visibility: hidden; } }

/* ---------- zoom ---------- */
.zoomview {
  position: absolute; inset: 0; z-index: 8;
  overflow: hidden; background: var(--surface-solid);
  border-radius: 2px; box-shadow: var(--shadow-page);
  cursor: grab;
}
.zoomview.is-panning { cursor: grabbing; }
.zoomview > img, .zoomview > .hs {
  transform-origin: 0 0;
  transform: translate3d(var(--zx, 0px), var(--zy, 0px), 0) scale(var(--zs, 1));
}
.zoomview > .hs { position: absolute; inset: 0; }

/* ---------- barre du bas ---------- */
.toolbar__group { display: flex; align-items: center; gap: 6px; flex: none; min-width: 0; }
.toolbar__group--grow { flex: 1 1 auto; min-width: 0; gap: 12px; }

.counter { display: flex; align-items: center; gap: 4px; font-variant-numeric: tabular-nums; color: var(--text-dim); }
.counter input {
  width: 46px; padding: 6px 4px; text-align: center;
  font: inherit; font-variant-numeric: tabular-nums; color: var(--text);
  background: color-mix(in srgb, var(--text) 8%, transparent);
  border: 1px solid var(--border); border-radius: 7px;
  -moz-appearance: textfield;
}
.counter input::-webkit-outer-spin-button, .counter input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.slider { flex: 1; min-width: 80px; display: flex; }
.slider input[type='range'] {
  width: 100%; height: 22px; margin: 0; background: transparent; -webkit-appearance: none; appearance: none; cursor: pointer;
}
.slider input[type='range']::-webkit-slider-runnable-track {
  height: 4px; border-radius: 999px;
  background: color-mix(in srgb, var(--text) 18%, transparent);
}
.slider input[type='range']::-moz-range-track { height: 4px; border-radius: 999px; background: color-mix(in srgb, var(--text) 18%, transparent); }
.slider input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none; width: 14px; height: 14px; margin-top: -5px;
  border-radius: 50%; background: var(--al-red); border: 2px solid var(--surface-solid);
}
.slider input[type='range']::-moz-range-thumb { width: 14px; height: 14px; border: 2px solid var(--surface-solid); border-radius: 50%; background: var(--al-red); }

.page-name {
  flex: none; max-width: 34ch; font-size: 12.5px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.zoom-level { min-width: 52px; text-align: center; font-size: 12.5px; color: var(--text-dim); font-variant-numeric: tabular-nums; }

/* ---------- miniatures ---------- */
.thumbs {
  position: fixed; left: 0; right: 0; bottom: var(--toolbar-h); z-index: 20;
  background: var(--surface); backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  animation: slideUp var(--dur-ui) ease-out;
}
@keyframes slideUp { from { transform: translateY(12px); opacity: 0; } }
.thumbs__head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; font-size: 13px; }
.thumbs__list {
  display: flex; gap: 10px; margin: 0; padding: 0 0 6px; list-style: none;
  overflow-x: auto; scroll-snap-type: x proximity; scrollbar-width: thin;
}
.thumbs__list li { scroll-snap-align: center; }
.thumbs__list button {
  display: block; padding: 0; border: 2px solid transparent; border-radius: 6px;
  background: none; cursor: pointer; line-height: 0;
  transition: border-color var(--dur-ui), transform var(--dur-ui);
}
.thumbs__list img { display: block; width: 118px; height: auto; border-radius: 4px; }
.thumbs__list button:hover { transform: translateY(-2px); }
.thumbs__list button[aria-current='true'] { border-color: var(--al-red); }
.thumbs__list .n { display: block; font-size: 11px; color: var(--text-dim); text-align: center; padding-top: 3px; line-height: 1.2; }

/* ---------- panneaux ---------- */
.panel {
  border: none; padding: 0; color: var(--text);
  background: var(--surface-solid);
  box-shadow: 0 20px 70px rgba(0, 0, 0, 0.5);
  max-height: 100dvh;
}
.panel::backdrop { background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(3px); }
.panel--left {
  margin: 0; height: 100dvh; width: min(420px, 92vw);
  border-right: 1px solid var(--border);
  animation: panelIn 220ms cubic-bezier(0.22, 1, 0.36, 1);
}
@keyframes panelIn { from { transform: translateX(-24px); opacity: 0; } }
.panel--center {
  margin: auto; width: min(560px, 92vw); border-radius: 14px; border: 1px solid var(--border);
  animation: dlgIn 200ms cubic-bezier(0.22, 1, 0.36, 1);
}
@keyframes dlgIn { from { transform: translateY(10px) scale(0.98); opacity: 0; } }

.panel__head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 12px 14px 18px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface-solid); z-index: 2;
}
.panel__head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.panel__body { padding: 14px 18px 22px; overflow-y: auto; max-height: calc(100dvh - 60px); }

.muted { color: var(--text-dim); font-size: 13px; }

/* sommaire */
.toc { list-style: none; margin: 0; padding: 0; }
.toc li + li { margin-top: 2px; }
.toc button {
  display: flex; align-items: baseline; gap: 10px; width: 100%;
  padding: 10px 12px; text-align: left;
  font: inherit; color: var(--text); background: none;
  border: 1px solid transparent; border-radius: 8px; cursor: pointer;
  transition: background var(--dur-ui), border-color var(--dur-ui);
}
.toc button:hover { background: color-mix(in srgb, var(--text) 7%, transparent); border-color: var(--border); }
.toc .t { flex: 1; font-size: 13.5px; font-weight: 600; letter-spacing: 0.01em; }
.toc .d { display: block; font-weight: 400; font-size: 12.5px; color: var(--text-dim); margin-top: 2px; }
.toc .p { flex: none; font-size: 12px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.toc button[aria-current='true'] { background: color-mix(in srgb, var(--al-red) 12%, transparent); border-color: color-mix(in srgb, var(--al-red) 40%, transparent); }

/* recherche */
.searchbox { display: flex; align-items: center; gap: 8px; padding: 0 12px; border: 1px solid var(--border-strong); border-radius: 10px; }
.searchbox svg { flex: none; color: var(--text-dim); }
.searchbox input { flex: 1; padding: 11px 0; font: inherit; color: var(--text); background: none; border: none; outline: none; }
.searchbox input::-webkit-search-cancel-button { filter: invert(0.5); }

.results { list-style: none; margin: 12px 0 0; padding: 0; }
.results li + li { margin-top: 4px; }
.results button {
  display: block; width: 100%; padding: 9px 12px; text-align: left;
  font: inherit; color: var(--text); background: none;
  border: 1px solid transparent; border-radius: 8px; cursor: pointer;
}
.results button:hover { background: color-mix(in srgb, var(--text) 7%, transparent); border-color: var(--border); }
.results .p { display: block; font-size: 11.5px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.results .x { display: block; font-size: 13px; margin-top: 2px; }
.results mark { background: color-mix(in srgb, var(--al-red) 30%, transparent); color: inherit; border-radius: 2px; padding: 0 1px; }

/* partage */
.field { margin-top: 16px; }
.field label { display: block; font-size: 12.5px; color: var(--text-dim); margin-bottom: 6px; }
.copyrow { display: flex; gap: 8px; }
.field input, .field textarea {
  flex: 1; width: 100%; padding: 9px 11px;
  font: inherit; font-size: 13px; color: var(--text);
  background: color-mix(in srgb, var(--text) 6%, transparent);
  border: 1px solid var(--border); border-radius: 8px; resize: vertical;
}
.field textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; margin-bottom: 8px; }
.field .btn { border-color: var(--border-strong); }
.sharelinks { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }

/* ---------- version texte (a11y / indexation) ---------- */
.pagetext {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0);
}
.pagetext:focus-within, .pagetext:target {
  position: static; width: auto; height: auto; clip: auto;
  max-width: 70ch; margin: 0 auto; padding: 32px 20px 64px;
}

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  :root { --topbar-h: 52px; --toolbar-h: 52px; }
  .stage { padding: 8px 8px 4px; }
  .nav { display: none; }
  .brand__text small { display: none; }
  .page-name { display: none; }
  #btnZoomIn, #btnZoomOut, .zoom-level, #btnTheme { display: none; }
  .thumbs__list img { width: 96px; }
}

@media (max-width: 560px) {
  .counter input { width: 40px; }
  .topbar__actions .btn { width: 34px; min-width: 34px; }
}

/* Écran bas (téléphone en paysage) : barres compactes pour laisser la page respirer. */
@media (max-height: 520px) {
  :root { --topbar-h: 42px; --toolbar-h: 44px; }
  .stage { padding: 4px 6px; }
  .brand__text small { display: none; }
  .hint { display: none; }
}

/* Le magazine est à l'italienne : en portrait étroit, on invite à tourner l'appareil. */
.rotate-hint {
  display: none;
  position: absolute; left: 50%; bottom: 8px; transform: translateX(-50%);
  max-width: calc(100% - 24px);
  align-items: center; gap: 8px;
  margin: 0; padding: 8px 12px; z-index: 7;
  font-size: 12.5px; color: var(--text);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: 999px;
  backdrop-filter: blur(10px);
}
.rotate-hint svg { width: 16px; height: 16px; }
.rotate-hint button {
  margin-left: 2px; padding: 0 2px; border: 0; background: none; color: var(--text-dim);
  font: inherit; cursor: pointer; line-height: 1;
}
@media (max-width: 760px) and (orientation: portrait) {
  .rotate-hint:not([hidden]) { display: inline-flex; }
}

/* ---------- préférences système ---------- */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
  .sheet { display: none !important; }
  .slot img { transition: opacity 200ms linear; }
}

@media print {
  .topbar, .toolbar, .nav, .thumbs, .hint { display: none !important; }
  .pagetext { position: static; width: auto; height: auto; clip: auto; }
}
