:root {
  --bg: #ffffff;
  --ink: #1d1d1f;
  --muted: #6e6e73;
  --line: #e6e6e9;
  --btn-bg: #f4f4f6;
  --btn-bg-hover: #e9e9ed;
  --page-placeholder: #f2f2f4;
  --gutter-left: linear-gradient(to left, rgba(0, 0, 0, 0.22) 0%, rgba(0, 0, 0, 0.08) 3%, rgba(0, 0, 0, 0) 9%);
  --gutter-right: linear-gradient(to right, rgba(0, 0, 0, 0.22) 0%, rgba(0, 0, 0, 0.08) 3%, rgba(0, 0, 0, 0) 9%);
  --topbar-h: 56px;
  --controls-h: 72px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button { font: inherit; }

.app {
  display: grid;
  grid-template-rows: var(--topbar-h) 1fr var(--controls-h);
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- Barre du haut ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
}

.topbar__title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__actions { display: flex; gap: 8px; flex-shrink: 0; }

.icon-btn, .nav-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--btn-bg);
  color: var(--ink);
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s ease, transform 0.15s ease, opacity 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover, .nav-btn:hover { background: var(--btn-bg-hover); }
.icon-btn:active, .nav-btn:active { transform: scale(0.94); }
.icon-btn:focus-visible, .nav-btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.icon-btn[hidden] { display: none; }

.icon-btn svg, .nav-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Zone du livre ---------- */
.stage {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  padding: 8px 16px;
  overflow: hidden;
}

.book {
  position: relative;
  transition: transform 0.6s cubic-bezier(0.22, 0.61, 0.36, 1);
  will-change: transform;
}

/* Corrections du CSS de la librairie : on pilote la taille nous-mêmes */
.book.stf__parent { min-width: 0 !important; max-width: none !important; }
.book .stf__wrapper { position: relative; width: 100%; height: 100%; }

.page {
  background: var(--page-placeholder);
  overflow: hidden;
  box-shadow: 0 10px 24px -8px rgba(0, 0, 0, 0.28);
}

.page img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.page img.is-loaded { opacity: 1; }

/* Ombre de reliure au centre (effet magazine) */
.page::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.book.is-landscape .page--left:not(.page--hard)::after { background: var(--gutter-left); }
.book.is-landscape .page--right:not(.page--hard)::after { background: var(--gutter-right); }

/* Léger reflet sur la couverture rigide */
.page--hard::after {
  background: linear-gradient(105deg, rgba(255, 255, 255, 0.12) 0%, rgba(255, 255, 255, 0) 40%);
}

/* ---------- Navigation du bas ---------- */
.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.counter {
  min-width: 96px;
  text-align: center;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.nav-btn { width: 44px; height: 44px; }
.nav-btn:disabled { opacity: 0.35; cursor: default; }
.nav-btn:disabled:hover { background: var(--btn-bg); }

/* ---------- Chargement / erreur ---------- */
.loader {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  transition: opacity 0.3s ease;
}
.loader.is-hidden { opacity: 0; }
.loader span {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--ink);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error {
  margin: 0;
  max-width: 420px;
  text-align: center;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.5;
}

/* ---------- Zoom ---------- */
.zoom {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.98);
}
.zoom[hidden] { display: none; }

.zoom__bar {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
}

.zoom__scroll {
  flex: 1;
  overflow: auto;
  cursor: grab;
  touch-action: pan-x pan-y pinch-zoom;
  -webkit-overflow-scrolling: touch;
}
.zoom__scroll.is-dragging { cursor: grabbing; }

.zoom__pages {
  display: flex;
  justify-content: center;
  margin: 0 auto;
  padding: 0 16px 24px;
  width: max-content;
  min-width: 100%;
}

.zoom__pages img {
  display: block;
  height: auto;
  box-shadow: 0 8px 24px -10px rgba(0, 0, 0, 0.3);
  user-select: none;
  -webkit-user-drag: none;
}

/* ---------- Petits écrans ---------- */
@media (max-width: 600px) {
  :root { --topbar-h: 52px; --controls-h: 64px; }
  .topbar { padding: 0 12px; }
  .topbar__title { font-size: 14px; }
  .icon-btn { width: 36px; height: 36px; }
  .stage { padding: 4px 8px; }
}

/* Paysage très bas (téléphone couché) : on réduit les barres */
@media (max-height: 480px) and (orientation: landscape) {
  :root { --topbar-h: 44px; --controls-h: 52px; }
  .icon-btn { width: 34px; height: 34px; }
  .nav-btn { width: 38px; height: 38px; }
}

@media (prefers-reduced-motion: reduce) {
  .book, .page img, .loader { transition: none; }
}
