:root {
  /* Retro modern: paper de polaroid, blanc i negre, amb accents mostassa i rovell */
  --ink: #1d1d1b;        /* negre càlid: text, vores, botons actius */
  --ink-soft: #6b6862;   /* text secundari */
  --paper: #f3f1ec;      /* fons general: blanc trencat de marc de polaroid */
  --panel: #eeece7;      /* barra lateral i panell */
  --card: #fbfaf7;       /* camps, botons, miniatures, barra superior */
  --rule: #e1dfd9;       /* vores suaus */
  --muted: #a19e97;
  --stage: #e4e2dd;      /* fons de l'àlbum: gris neutre clar */
  --line: #cecbc4;       /* vores de camps i botons */
  --accent: var(--ink);  /* sense color: les seleccions i el botó principal van en negre */
  --rust: #a0472a;       /* rovell: eliminar, avisos */
  --radius: 8px;
  --shadow: 0 1px 2px rgba(20, 20, 18, .06), 0 4px 14px rgba(20, 20, 18, .08);
  --ratio: 0.7071; /* mig A4 apaïsat (A5): 148,5 / 210 */
  --ui: "Instrument Sans", system-ui, sans-serif;     /* text de la interfície */
  --type: "Courier Prime", "Courier New", monospace;  /* etiquetes petites, com escrites a màquina */
  --display: "Shrikhand", Georgia, serif;             /* logo i títols */
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
/* Mòbil: sense ampliar la pàgina amb dos dits ni amb doble toc (els panells es poden seguir desplaçant) */
html { touch-action: pan-x pan-y; }
body {
  display: grid;
  grid-template-rows: 56px 1fr 40px;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--ui);
  font-size: 13px;
  overflow: hidden;
}
button { font: inherit; cursor: pointer; color: inherit; }

/* ---------- Topbar ---------- */
.topbar {
  background: #fff;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px 0 0;
  border-bottom: 1px solid var(--rule);
}
/* El logo ocupa la mateixa columna que la barra lateral (160px, marge de 8px), centrat en alçada */
.brand { position: relative; width: 160px; height: 100%; padding: 0 8px; flex: none; display: flex; align-items: center; justify-content: center; }

/* Botó per amagar/mostrar la barra lateral, a l'esquerra del logo (fora del flux: el logo queda centrat) */
.brand #pagesToggle { position: absolute; left: 6px; top: 50%; transform: translateY(-50%); }
.top-icon {
  display: flex; align-items: center; justify-content: center; flex: none;
  width: 34px; height: 34px; padding: 0;
  border: 1px solid transparent; border-radius: var(--radius);
  background: transparent; color: var(--ink);
}
.top-icon:hover { border-color: var(--line); background: var(--paper); }
/* La fletxa apunta cap on anirà la barra: ‹ amagar, › mostrar */
.top-icon .chev { transition: transform .2s; transform-origin: 14.5px 12px; }
#pagesToggle[aria-expanded="false"] .chev { transform: scaleX(-1); }

/* Ordinador: barra lateral plegada, l'àlbum ocupa tot l'ample.
   La columna s'encongeix i la barra llisca a l'esquerra amb el mateix ritme, així van sincronitzades. */
.sidebar-collapsed .app { grid-template-columns: 0 minmax(0, 1fr); }
.sidebar-collapsed .sidebar { transform: translateX(-100%); visibility: hidden; }
.brand-book { display: block; height: 49px; width: auto; aspect-ratio: 107 / 75; } /* llibret A5 obert (46px) + les pàgines de sota */
.top-actions { display: flex; gap: 10px; align-items: center; }

.btn {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  padding: 7px 14px;
  letter-spacing: 1.5px;
  font-size: 12px;
  border-radius: var(--radius);
  white-space: nowrap;
  font-weight: 600;
}
.btn:hover { border-color: var(--ink); }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--card);
  font-weight: 700;
  box-shadow: 3px 3px 0 var(--line);
}
.btn-primary:hover { transform: translate(-1px, -1px); box-shadow: 4px 4px 0 var(--line); }
/* Previsualitza: ombra cap a l'esquerra, mirall de "Descarrega l'àlbum". Junts semblen un llibre obert */
#previewBtn, #closePrint { background: #fff; border-color: var(--ink); box-shadow: -3px 3px 0 var(--line); }
#previewBtn:hover, #closePrint:hover { transform: translate(1px, -1px); box-shadow: -4px 4px 0 var(--line); }


/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 160px minmax(0, 1fr); min-height: 0; overflow: hidden; transition: grid-template-columns .35s cubic-bezier(.4, 0, .2, 1); }
.workspace { display: grid; grid-template-rows: 1fr auto; min-height: 0; min-width: 0; background: var(--stage); } /* el que es veu quan les opcions baixen és del color de sota l'àlbum */

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--rule);
  overflow-y: auto;
  padding: 10px 8px;
  scrollbar-color: var(--muted) var(--panel);
  width: 160px; /* amplada fixa: no s'aixafa mentre la columna s'encongeix */
  transition: transform .35s cubic-bezier(.4, 0, .2, 1), visibility .35s;
}
#thumbs { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.thumb {
  position: relative;
  background: var(--card);
  border: 1px solid var(--rule); border-radius: var(--radius);
  padding: 7px 7px 5px;
  cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.thumb:hover { border-color: var(--ink); }
.thumb.active { border-color: var(--ink); box-shadow: 3px 3px 0 var(--line); }
.thumb .sheet { width: 50%; pointer-events: none; }
.thumb .sheet.spread { width: 100%; }
/* Miniatures: els forats buits es marquen perquè es vegi la distribució de la pàgina */
.thumb .slot.empty { background: #efede8; box-shadow: inset 0 0 0 1px #d9d6cf; }
.thumb-label { font-family: var(--type); font-size: 10px; letter-spacing: 1.5px; color: var(--ink-soft); }

/* Paperera: apareix en passar per sobre d'una doble pàgina */
.thumb-del {
  position: absolute; top: 6px; right: 6px;
  width: 26px; height: 26px; padding: 0; border: 0; border-radius: 50%;
  background: var(--ink); color: var(--card);
  display: none; align-items: center; justify-content: center;
}
.thumb:hover .thumb-del { display: flex; }
.thumb-del:hover { background: var(--rust); }
.thumb.will-delete { border-color: var(--rust); outline: 2px solid var(--rust); }
.thumb.will-delete .sheet { opacity: .45; }

/* "+" entre miniatures per afegir dobles pàgines en aquell punt */
.insert { height: 22px; }
/* Sense botó "+" (àlbum al màxim): mateix espai entre miniatures */
.thumb + .thumb { margin-top: 22px; }
.insert button {
  position: relative; width: 100%; height: 100%; padding: 0;
  border: 0; background: transparent;
  display: flex; align-items: center; justify-content: center;
}
.insert button::before {
  content: ""; position: absolute; left: 4px; right: 4px; top: 50%; height: 2px; margin-top: -1px;
  background: var(--accent); opacity: 0;
}
.insert span {
  position: relative; width: 18px; height: 18px; border-radius: 50%;
  background: var(--panel); border: 1px solid var(--muted); color: var(--ink-soft);
  font-size: 13px; line-height: 16px; text-align: center;
}
.insert button:hover::before { opacity: 1; }
.insert button:hover span { background: var(--accent); border-color: var(--accent); color: var(--card); }
.add-spread:disabled { opacity: .5; cursor: default; border-color: var(--rule); color: var(--muted); }
.add-spread {
  width: 100%; margin-top: 10px; padding: 10px 0; border-radius: var(--radius);
  background: transparent; border: 1px dashed var(--muted);
  font-size: 11px; letter-spacing: 1.5px; color: var(--ink-soft);
}
.add-spread:hover { border-color: var(--ink); color: var(--ink); }
.reset-album { margin-top: 6px; border-style: solid; border-color: transparent; color: var(--muted); }
.reset-album:hover { border-color: var(--rust); color: var(--rust); }

/* ---------- Stage ---------- */
.stage {
  background: var(--stage);
  display: flex; align-items: center; justify-content: center;
  padding: 28px;
  min-height: 0;
  overflow: visible; /* l'ombra del llibre continua per sota de les opcions, sense tall recte */
}
/* Pàgina muntada + fulls de sota (falques que surten del llom, com al logo) */
.mount { position: relative; flex: none; isolation: isolate; perspective: 2400px; }

/* Canvi a/des de portada o contraportada: esvair suau */
.mount.page-fade { animation: page-fade .4s ease both; }
@keyframes page-fade { from { opacity: 0; transform: translateY(8px) scale(.985); } }

/* Mode revista: el full gira pel llom */
.flip { position: absolute; inset: 0; z-index: 8; pointer-events: none; }
.flip-still, .flip-leaf { position: absolute; top: 0; bottom: 0; width: 50%; }
.flip.next .flip-still { left: 0; }
.flip.prev .flip-still { right: 0; }
.flip-leaf { transform-style: preserve-3d; animation: .75s cubic-bezier(.45, .05, .3, 1) forwards; }
.flip.next .flip-leaf { left: 50%; transform-origin: left center; animation-name: flip-next; }
.flip.prev .flip-leaf { left: 0; transform-origin: right center; animation-name: flip-prev; }
@keyframes flip-next { from { transform: rotateY(0); } to { transform: rotateY(-180deg); } }
@keyframes flip-prev { from { transform: rotateY(0); } to { transform: rotateY(180deg); } }
.flip-face { position: absolute; inset: 0; backface-visibility: hidden; background: #fff; overflow: hidden; }
.flip-face.back { transform: rotateY(180deg); }
.flip .sheet { width: 100%; height: 100%; aspect-ratio: auto; }
.flip .sheet.single .page { height: 100%; }
/* Obrir i tancar el llibre: la meitat on aterra el full queda amagada fins que hi arriba,
   i el llibre llisca entre "centrat tancat" (±25%) i "centrat pel llom" */
.mount.hide-left .sheet.spread > .page:first-child, .mount.hide-left .under.left,
.mount.hide-right .sheet.spread > .page:last-child, .mount.hide-right .under.right { visibility: hidden; }
.mount.hide-left .sheet.spread, .mount.hide-right .sheet.spread { background: transparent; box-shadow: none; }
.mount.hide-left .sheet.spread::after, .mount.hide-right .sheet.spread::after { display: none; }
.mount.shift-from-left { animation: book-from-left .75s cubic-bezier(.45, .05, .3, 1) both; }
.mount.shift-from-right { animation: book-from-right .75s cubic-bezier(.45, .05, .3, 1) both; }
.mount.shift-to-left { animation: book-to-left .75s cubic-bezier(.45, .05, .3, 1) forwards; }
.mount.shift-to-right { animation: book-to-right .75s cubic-bezier(.45, .05, .3, 1) forwards; }
@keyframes book-from-left { from { transform: translateX(-25%); } }
@keyframes book-from-right { from { transform: translateX(25%); } }
@keyframes book-to-left { to { transform: translateX(-25%); } }
@keyframes book-to-right { to { transform: translateX(25%); } }

/* A l'editor el full que gira no porta ombra de llom (només la previsualització en llibre en té) */
.under {
  position: absolute; top: 0; z-index: -1;
  --dx: calc(var(--o) * 4px); --dy: calc(var(--o) * 5px);
  width: calc(50% + var(--dx)); height: calc(100% + var(--dy));
}
.under.o1 { --o: 1; background: #dcd9d2; }
.under.o2 { --o: 2; background: #cfccc4; }
.under.o3 { --o: 3; background: #c3bfb7; }
.under.left { left: calc(-1 * var(--dx)); clip-path: polygon(100% 0, 100% calc(100% - var(--dy)), 0 100%, 0 var(--dy)); }
.under.right { right: calc(-1 * var(--dx)); clip-path: polygon(0 0, 0 calc(100% - var(--dy)), 100% 100%, 100% var(--dy)); }
/* Portada i contraportada: la falca ocupa tota la pàgina (el llom és a la vora) */
.under.full { width: calc(100% + var(--dx)); }

/* Botons rodons per passar de pàgina: mateix llenguatge que "Descarrega àlbum" (ombra desplaçada) */
.nav-btn {
  width: 46px; height: 46px; padding: 0; flex: none;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--ink); border-radius: 50%;
  background: #fff; color: var(--ink);
  box-shadow: 3px 3px 0 var(--line);
  transition: transform .15s ease, box-shadow .15s ease, background-color .15s ease, color .15s ease, opacity .15s;
}
.nav-btn:hover:not(:disabled) { background: var(--ink); color: #fff; transform: translate(-1px, -1px); box-shadow: 4px 4px 0 var(--line); }
.nav-btn:active:not(:disabled) { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--line); }
.nav-btn:disabled { opacity: .25; cursor: default; box-shadow: none; }
/* Botó d'anterior: ombra en mirall, cap a l'esquerra (com "Previsualitza") */
.nav-btn.prev, #prevPage { box-shadow: -3px 3px 0 var(--line); }
.nav-btn.prev:hover:not(:disabled), #prevPage:hover:not(:disabled) { transform: translate(1px, -1px); box-shadow: -4px 4px 0 var(--line); }
.nav-btn.prev:active:not(:disabled), #prevPage:active:not(:disabled) { transform: translate(-2px, 2px); box-shadow: -1px 1px 0 var(--line); }
.nav-btn.prev:disabled, #prevPage:disabled { box-shadow: none; }
/* Els botons van just al costat de l'àlbum (fora de les falques de sota), no a les vores */
.stage { position: relative; gap: 22px; }
.stage-nav { z-index: 7; }

.stage .sheet { border-radius: 2px; box-shadow: 0 2px 4px rgba(20, 20, 18, .08), 0 18px 40px rgba(20, 20, 18, .18); }

/* ---------- Sheets (pàgines) ---------- */
.sheet {
  position: relative;
  background: #fff;
  aspect-ratio: var(--ratio);
  container-type: size;
  overflow: hidden;
  flex: none;
}
.sheet.spread { aspect-ratio: calc(var(--ratio) * 2); display: grid; grid-template-columns: 1fr 1fr; }
.sheet.spread::after {
  content: ""; position: absolute; left: 50%; top: 0; bottom: 0; width: 1px;
  background: linear-gradient(rgba(0,0,0,.25), rgba(0,0,0,.08), rgba(0,0,0,.25));
}
.page { position: relative; container-type: size; display: flex; flex-direction: column; min-width: 0; }
.slots { flex: 1; display: grid; gap: 0; background: #fff; min-height: 0; } /* sense marges: fotos enganxades, sense cap píxel blanc */
.slots.l-1 { grid-template-rows: 1fr; }
.slots.l-2 { grid-template-rows: 1fr 1fr; }
.slots.l-3 { grid-template-rows: 1fr 1fr; grid-template-columns: 1fr 1fr; }
.slots.l-3 .cell:first-child { grid-column: 1 / -1; }
.slots.l-3b { grid-template-rows: 1fr 1fr; grid-template-columns: 1fr 1fr; }
.slots.l-3b .cell:last-child { grid-column: 1 / -1; }
.slots.l-3r { grid-template-rows: 1fr 1fr 1fr; }
/* Amb marges: marge --m igual a tot arreu. L'espai sota cada foto el posa la cel·la:
   sense descripció = --m (la foto ocupa la resta); amb descripció = caixa polaroid
   proporcional a l'alçada de la foto (una foto sola té més caixa que tres). */
.slots.margins { --m: 5cqw; padding: var(--m) var(--m) 0; column-gap: var(--m); row-gap: 0; }
.slots.margins .cell { padding-bottom: var(--m); }
.slots.margins .cell.has-cap { padding-bottom: 0; }

/* Cada foto + la seva descripció a sota */
.cell { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: #fff; }
.cell .slot { flex: 1; }
.slots.margins .caption {
  flex: 0 0 18%;
  padding: 0 1cqw; display: flex; align-items: center; background: transparent;
}
/* Alineació de les descripcions (per pàgina) */
.slots.al-center .caption { text-align: center; justify-content: center; }
.slots.al-right .caption { text-align: right; justify-content: flex-end; }

.slot { position: relative; overflow: hidden; background: #ece8e1; min-height: 0; }
.slot img { width: 100%; height: 100%; display: block; }
/* Forat buit: en blanc. Només a l'editor es marca on clicar (vora suau i "+ FOTO") */
.slot.empty {
  display: flex; align-items: center; justify-content: center;
  background: #fff;
  color: transparent; font-size: 6cqmin; letter-spacing: .1em;
}
.stage .slot { cursor: pointer; }
.stage .slot.empty { color: var(--muted); outline: 1px dashed var(--line); outline-offset: -6px; }
.stage .slot.filled { cursor: grab; touch-action: none; user-select: none; }
.stage .slot.panning { cursor: grabbing; }
/* Botons sobre la foto (només a l'editor, en passar-hi el ratolí) */
.slot-tools { display: none; }
.stage .slot.filled:hover .slot-tools { display: flex; }
.stage .slot.panning .slot-tools { display: none; }
.slot-tools {
  position: absolute; top: 10px; right: 10px; gap: 4px; /* fora de les cantonades */
  z-index: 5; /* per sobre del vel i el títol de la portada */
}
.slot-tools button {
  width: 28px; height: 28px; padding: 0;
  border: 0; border-radius: 50%;
  background: rgba(20, 20, 18, .8); color: var(--card); font-size: 15px; line-height: 1;
}
.slot-tools button:hover { background: var(--card); color: var(--ink); }
.slot-tools [data-tool="reset"] { margin-right: 6px; }
.stage .slot:hover { outline: 3px solid var(--accent); outline-offset: -3px; }
.stage .slot.dragover { outline: 3px dashed var(--accent); outline-offset: -6px; }

.caption {
  padding: 2.2cqw 4cqw; /* +10% d'espai vertical */
  font-family: "Courier Prime", monospace;
  font-size: 3.4cqw;
  line-height: 1.43; /* +10% */
  background: #fff;
  overflow-wrap: anywhere;
  overflow: hidden;
}

/* Portada / contraportada */
.cover .slot { position: absolute; inset: 0; }
.overlay { position: absolute; inset: 0; pointer-events: none; }
.cover.margins .slot, .cover.margins .overlay { inset: 5cqw; } /* sense descripció: marge igual a tot arreu */
.cover-text {
  position: absolute; inset: 0;
  padding: 10cqh 8cqw;
  display: flex; flex-direction: column; gap: 2cqh;
  pointer-events: none;
}
.cover-text h1 { margin: 0; line-height: 1; font-weight: normal; }
.cover-text p { margin: 0; font-size: 4cqw; }
.cover-text small { font-size: 3cqw; letter-spacing: .05em; } /* es respecten majúscules i minúscules tal com s'escriuen */
.size-S h1 { font-size: 8cqw; }
.size-M h1 { font-size: 11cqw; }
.size-L h1 { font-size: 15cqw; }
.h-left { align-items: flex-start; text-align: left; }
.h-center { align-items: center; text-align: center; }
.h-right { align-items: flex-end; text-align: right; }
.v-top { justify-content: flex-start; }
.v-middle { justify-content: center; }
.v-bottom { justify-content: flex-end; }
/* Repartit: títol i subtítol junts a dalt, autor a baix de tot */
.v-spread { justify-content: flex-start; }
.v-spread small { margin-top: auto; }
.chip svg { display: block; margin: 0 auto; }

/* ---------- Panel inferior ---------- */
/* Les opcions sempre per sobre de l'àlbum: tapen l'ombra amb la seva vora */
.panel {
  position: relative; z-index: 3;
  box-shadow: 0 -6px 18px rgba(20, 20, 18, .05);
  background: var(--panel);
  border-top: 1px solid var(--rule);
  padding: 16px 20px;
  display: flex; gap: 24px;
  /* Alçada fixa: en canviar de pàgina l'àlbum no canvia de mida (si no hi cap, es desplaça per dins) */
  height: clamp(220px, 36vh, 360px);
  overflow: auto;
  transition: transform .3s cubic-bezier(.2, .8, .2, 1), opacity .3s ease;
}
/* Canvi de pàgina: les opcions baixen i s'amaguen; després les noves pugen des de baix */
.panel.panel-out {
  transform: translateY(100%); opacity: 0;
  transition-duration: .2s; transition-timing-function: ease-in;
}
.panel-col { flex: 1; display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.panel-divider { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; border-left: 1px dashed var(--muted); border-right: 1px dashed var(--muted); padding: 0 18px; }
.row { display: flex; gap: 20px; flex-wrap: wrap; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 5px; flex: 1; min-width: 120px; }
.field.auto { flex: none; min-width: 0; }
.label { font-family: var(--type); font-size: 11px; letter-spacing: 1.5px; color: var(--ink-soft); text-transform: uppercase; font-weight: 700; }
.field input[type=text], .field textarea {
  font: inherit; font-size: 14px;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  padding: 8px 10px; border-radius: var(--radius); width: 100%;
}
.field textarea { resize: none; height: 46px; }
.field input:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: -2px; }

.chips { display: flex; gap: 5px; flex-wrap: wrap; }
.chip {
  background: var(--card); border: 1px solid var(--line); border-radius: 6px;
  padding: 5px 10px; font-size: 13px; min-width: 34px;
}
.chip.active { background: var(--ink); color: var(--card); }
/* Botons amb icona (distribució i marges) */
/* Sense padding: el negre arriba fins a la vora del botó (com una pàgina sense marges) */
.icon-chip { padding: 0; min-width: 0; line-height: 0; border: 1px solid var(--ink); border-radius: 3px; overflow: hidden; }
/* Les icones són SVG dibuixats en píxels reals de pantalla (iconSVG a app.js) */
.icon-chip .ico { display: block; }
/* Seleccionat: icona sempre fosca, marcada amb contorn mostassa */
.icon-chip.active { background: var(--card); color: var(--ink); box-shadow: none; outline: 2px solid var(--accent); outline-offset: 1px; }
.swatch { width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--muted); padding: 0; }
.swatch.active { outline: 2px solid var(--accent); outline-offset: 2px; }
/* Selector de color lliure: cercle amb l'arc de Sant Martí (o el color triat) i l'input invisible a sobre */
.swatch.picker {
  position: relative; display: inline-block; cursor: pointer; overflow: hidden;
  background: conic-gradient(#e74c3c, #f1c40f, #2ecc71, #1abc9c, #3498db, #9b59b6, #e74c3c);
}
.swatch.picker input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; border: 0; padding: 0; }
.swatch.picker:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon-btn { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); width: 38px; height: 34px; font-size: 16px; }
.icon-btn:hover { background: var(--ink); color: var(--card); }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; }
.danger-icon { color: var(--rust); border-color: var(--rust); }
.danger-icon:hover { background: var(--rust); border-color: var(--rust); color: #fff; }
.danger { color: var(--rust); border-color: var(--rust); background: var(--card); border-radius: var(--radius); padding: 6px 10px; font-size: 11px; letter-spacing: 1px; border: 1px solid; }

.photo-mini {
  width: 110px; height: 140px; flex: none; position: relative;
  border: 1px dashed var(--muted); border-radius: 6px; overflow: hidden; background: var(--card); cursor: pointer;
  display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 11px;
}
.photo-mini img { width: 100%; height: 100%; object-fit: cover; }
.photo-mini .remove {
  position: absolute; top: 4px; right: 4px; width: 22px; height: 22px;
  border-radius: 50%; border: 0; background: var(--ink); color: var(--card); font-size: 12px; line-height: 1;
}
.photo-caps { display: flex; gap: 10px; flex-wrap: wrap; }
.photo-cap { display: flex; gap: 6px; flex: 1 1 180px; min-width: 0; align-items: flex-start; }
.photo-cap .photo-mini.small { width: 60px; height: 60px; font-size: 9px; text-align: center; }
/* Botons d'enquadrar al panell: només al mòbil / pantalles tàctils */
.cap-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.mini-col { display: flex; flex-direction: column; gap: 8px; flex: none; }
.crop-tools { display: none; gap: 6px; }
.crop-tools .icon-btn { width: 36px; height: 32px; font-size: 15px; }
@media (max-width: 760px), (hover: none) { .crop-tools { display: flex; } }
/* Mòbil: al calaix, les fotos sempre una sota l'altra i els botons a la dreta de la descripció */
@media (max-width: 760px) {
  .photo-caps { flex-direction: column; }
  .photo-cap { flex: none; width: 100%; }
  .cap-body { flex-direction: row; align-items: flex-start; gap: 8px; }
  .cap-body textarea { flex: 1; min-width: 0; }
  .cap-body .crop-tools { flex: none; }
}
.photo-cap textarea {
  flex: 1; min-width: 0; height: 60px; resize: none;
  font: inherit; font-size: 12px; padding: 6px 8px;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); color: var(--ink);
}
.photo-cap textarea:focus { outline: 2px solid var(--accent); outline-offset: -2px; }
.side-title.to-spine-right { align-self: flex-end; }
.side-title { font-family: var(--display); font-weight: normal; font-size: 16px; letter-spacing: .5px; }
input[type=range] { accent-color: var(--ink); width: 160px; }

/* ---------- Footer ---------- */
.footer {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0 20px; font-size: 11px; letter-spacing: 1px; color: var(--ink-soft);
  border-top: 1px solid var(--rule); background: #fff;
}
.footer small { color: var(--muted); }

/* ---------- Previsualització ---------- */
.preview {
  position: fixed; inset: 0; z-index: 14; /* per sobre del botó flotant de l'editor (mòbil) */
  background: #d9d4cc;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px;
}
.preview[hidden] { display: none; }
.preview:not([hidden]) { animation: preview-in .25s ease both; }
@keyframes preview-in { from { opacity: 0; } }
.preview-close { position: fixed; top: 14px; right: 18px; z-index: 11; }

.book-wrap { perspective: 2600px; }
.book {
  position: relative;
  width: calc(var(--page-w) * 2);
  height: var(--page-h);
  transition: transform .8s ease;
  cursor: pointer;
}
/* Tancat: només es veu una cara, la centrem */
.book.closed-front { transform: translateX(-25%); }
.book.closed-back { transform: translateX(25%); }

.leaf {
  position: absolute; top: 0; left: 50%;
  width: 50%; height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform .8s cubic-bezier(.45, .05, .3, 1);
}
.leaf.flipped { transform: rotateY(-180deg); }
.face {
  position: absolute; inset: 0;
  backface-visibility: hidden;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 10px 30px rgba(20, 20, 18, .25);
}
.face.back { transform: rotateY(180deg); }
.face .sheet { width: 100%; height: 100%; aspect-ratio: auto; }
.sheet.single .page { height: 100%; }
/* Ombra del llom */
.face::after { content: ""; position: absolute; inset: 0; pointer-events: none; }
.face.front::after { background: linear-gradient(to right, rgba(0,0,0,.22), rgba(0,0,0,0) 8%); }
.face.back::after { background: linear-gradient(to left, rgba(0,0,0,.22), rgba(0,0,0,0) 8%); }

/* ---------- Popup de confirmació ---------- */
.modal {
  position: fixed; inset: 0; z-index: 30;
  background: rgba(20, 20, 18, .35); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal[hidden] { display: none; }
.modal-box {
  width: min(440px, 100%);
  background: var(--card); color: var(--ink);
  border: 1px solid var(--ink); border-radius: 12px; box-shadow: 5px 5px 0 var(--ink);
  padding: 20px 22px;
}
.modal-box h3 { margin: 0 0 10px; font-family: var(--display); font-weight: normal; font-size: 22px; }
.modal-box p { margin: 0 0 12px; line-height: 1.5; }
.modal-detail { color: var(--rust); font-weight: 700; }
.modal-preview { display: flex; gap: 12px; margin-bottom: 12px; }
.modal-preview figure { margin: 0; flex: 1; display: flex; flex-direction: column; align-items: center; gap: 5px; min-width: 0; }
.modal-preview .sheet { width: 100%; outline: 2px solid var(--rust); pointer-events: none; }
.modal-preview figcaption { font-size: 10px; letter-spacing: 1.5px; color: var(--ink-soft); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.btn-modal {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--card);
  padding: 9px 16px; font-size: 13px; font-weight: 600;
}
.btn-modal:hover { background: var(--ink); color: var(--card); }
.btn-modal:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.danger-fill { background: var(--rust); border-color: var(--rust); color: var(--card); font-weight: 700; }
.danger-fill:hover { background: #83391f; border-color: #83391f; }

/* ---------- Impressió ---------- */
.print-view {
  position: fixed; inset: 0; z-index: 20; overflow-y: auto;
  background: var(--stage); color: var(--ink);
}
.print-view[hidden] { display: none; }
.print-head {
  position: sticky; top: 0; z-index: 1;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 20px;
  padding: 16px 24px; background: var(--card); border-bottom: 1px solid var(--rule);
}
.print-head h2 { margin: 0 0 6px; font-family: var(--display); font-size: 22px; font-weight: normal; letter-spacing: .5px; }
.print-head p { margin: 0 0 8px; color: var(--ink-soft); }
.print-steps { margin: 0; padding-left: 18px; color: var(--ink-soft); font-size: 12px; line-height: 1.6; }
.print-steps b, .print-head p b { color: var(--ink); }
.print-actions { display: flex; gap: 10px; flex: none; }
.print-sheets { display: flex; flex-direction: column; gap: 34px; padding: 30px 24px 60px; align-items: center; }
.print-sheet { display: flex; gap: 28px; flex-wrap: wrap; justify-content: center; }
.print-item { margin: 0; display: flex; flex-direction: column; gap: 8px; }
.print-label { font-family: var(--type); font-size: 11px; letter-spacing: 1.5px; color: var(--ink-soft); display: flex; justify-content: space-between; gap: 12px; }
.print-label span { color: var(--rust); }
.print-side {
  --w: 460px;
  position: relative;
  width: var(--w);
  aspect-ratio: calc(var(--ratio) * 2);
  display: grid; grid-template-columns: 1fr 1fr;
  background: #fff;
  box-shadow: var(--shadow);
}
.print-side .sheet { width: 100%; height: 100%; aspect-ratio: auto; }
.print-side .sheet.single .page { height: 100%; }
.sheet.blank { background: #fff; }

@page { size: 297mm 210mm; margin: 0; } /* A4 apaïsat */

@media print {
  html, body { height: auto; overflow: visible; background: #fff; display: block; }
  body > *:not(#printView) { display: none !important; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .print-view { position: static; overflow: visible; background: none; }
  .print-head, .print-label { display: none; }
  .print-sheets, .print-sheet { display: block; padding: 0; gap: 0; }
  .print-item { display: block; }
  .print-side {
    width: 297mm; height: 210mm; aspect-ratio: auto;
    box-shadow: none; break-after: page; page-break-after: always; overflow: hidden;
  }
  .print-sheet:last-child .print-item:last-child .print-side { break-after: auto; page-break-after: auto; }
}

.book-nav { display: flex; align-items: center; gap: 18px; color: var(--ink); font-family: var(--type); font-size: 12px; letter-spacing: 2px; }
.book-nav span { min-width: 230px; text-align: center; }
.book-nav { gap: 22px; }
/* Botó "✎ Editar": el mateix a la previsualització i al mòbil (botó flotant) */
.pill-btn {
  padding: 12px 24px;
  border: 2px solid var(--ink); border-radius: 999px;
  background: var(--ink); color: var(--card);
  font-size: 13px; font-weight: 700; letter-spacing: 1.5px; white-space: nowrap;
  box-shadow: 3px 3px 0 var(--line);
  transition: transform .15s ease, box-shadow .15s ease;
}
.pill-btn:hover { transform: translate(-1px, -1px); box-shadow: 4px 4px 0 var(--line); }
.book-edit { margin-top: 4px; }

/* ---------- Mòbil ---------- */
/* A l'ordinador: el calaix no existeix (el panell queda sota l'àlbum) i els controls de mòbil s'amaguen */
.panel-drawer { display: contents; }
.drawer-head, .edit-fab, .scrim, .btn .sm { display: none; }

/* Pantalles tàctils (sense hover): els botons que surten en passar el ratolí, sempre visibles */
@media (hover: none) {
  /* Sense ratolí: els botons sempre a sobre de cada foto, una mica més petits */
  /* Sense ratolí no hi ha botons a sobre de les fotos: s'enquadra amb els dits (un dit mou,
     dos dits fan zoom i mouen) i els botons són al calaix d'edició */
  .stage .slot.filled .slot-tools { display: none; }
  .thumb .thumb-del { display: flex; }
}

@media (max-width: 760px) {
  body { grid-template-rows: 52px 1fr; }
  .footer { display: none; }

  /* Barra superior compacta */
  .topbar { padding: 0 10px 0 4px; gap: 6px; }
  .top-icon { width: 38px; height: 38px; }
  /* Al mòbil la barra lateral no es plega: és un calaix */
  .sidebar-collapsed .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar-collapsed .sidebar { visibility: visible; padding: 10px 8px; border-right: 1px solid var(--rule); }
  .brand { width: auto; padding: 0 0 0 4px; margin-right: auto; gap: 6px; }
  .brand #pagesToggle { position: static; transform: none; order: -1; }
  .brand-book { height: 45px; }
  .top-actions { gap: 6px; }
  .btn { padding: 7px 10px; font-size: 11px; letter-spacing: 1px; }
  .btn .lg { display: none; }
  .btn .sm { display: inline; }
  .btn-primary { box-shadow: 2px 2px 0 var(--line); }
  #previewBtn, #closePrint { box-shadow: -2px 2px 0 var(--line); }

  /* L'àlbum ocupa tota la pantalla i no es mou: res li treu espai */
  .app { grid-template-columns: minmax(0, 1fr); }
  .workspace { grid-template-rows: 1fr; }
  .stage { padding: 16px; transition: padding-bottom .25s ease; }
  /* Opcions obertes: el llibre s'encongeix a l'espai lliure de dalt per veure el que edites */
  .panel-open .stage { padding-bottom: calc(var(--drawer-h, 55vh) + 12px); }

  /* Barra lateral: calaix que surt de l'esquerra, per sobre */
  .sidebar {
    position: fixed; top: 52px; bottom: 0; left: 0; z-index: 16;
    width: 180px;
    transform: translateX(-100%); transition: transform .25s ease;
    box-shadow: 6px 0 24px rgba(20, 20, 18, .18);
  }
  .sidebar-open .sidebar { transform: none; }

  /* Opcions: calaix que surt de baix, per sobre */
  .panel-drawer {
    display: flex; flex-direction: column;
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 16;
    max-height: 55vh;
    background: var(--panel);
    border-top: 1px solid var(--rule); border-radius: 16px 16px 0 0; overflow: hidden;
    box-shadow: 0 -8px 30px rgba(20, 20, 18, .18);
    transform: translateY(100%); transition: transform .25s ease;
  }
  .panel-open .panel-drawer { transform: none; }
  .drawer-head {
    display: flex; align-items: center; justify-content: space-between; flex: none;
    padding: 4px 4px 4px 16px;
    background: var(--card); color: var(--ink); border-bottom: 1px solid var(--rule);
    font-family: var(--type); font-size: 11px; letter-spacing: 2px; font-weight: 700;
  }
  .drawer-actions { display: flex; align-items: center; gap: 2px; }
  /* Fletxes una mica separades del ✕ */
  .drawer-nav + .top-icon:not(.drawer-nav) { margin-left: 12px; }
  .drawer-nav:disabled { opacity: .25; cursor: default; background: transparent; border-color: transparent; }
  .panel {
    flex-direction: column; gap: 18px;
    height: auto; min-height: 0; max-height: none; overflow-y: auto;
    padding: 16px; border-top: 0;
  }
  .panel-divider {
    flex-direction: row; justify-content: center; gap: 12px; /* intercanviar i paperera, centrats */
    border: 0; border-top: 1px dashed var(--muted); border-bottom: 1px dashed var(--muted);
    padding: 10px 0;
  }
  .side-title.to-spine-right { align-self: flex-start; } /* al mòbil, una pàgina sota l'altra: tot a l'esquerra */
  .row { gap: 14px; }
  .field { min-width: 100%; }
  /* L'iPhone amplia la pàgina en tocar un camp amb lletra de menys de 16px */
  .field input[type=text], .field textarea, .photo-cap textarea { font-size: 16px; }
  .field.auto { min-width: 0; }

  /* Botó flotant per obrir les opcions */
  .edit-fab {
    display: block;
    position: fixed; left: 50%; bottom: 18px; z-index: 12;
    transform: translateX(-50%);
  }
  .edit-fab:hover { transform: translateX(-50%) translateY(-1px); }
  .panel-open .edit-fab { display: none; }
  /* Botons de passar pàgina: a baix, a banda i banda del botó Edita */
  .stage-nav { position: absolute; bottom: 18px; width: 44px; height: 44px; }
  .stage-nav.prev { left: 16px; }
  .stage-nav.next { right: 16px; }
  .panel-open .stage-nav { display: none; }

  /* Fons fosc darrere de la barra lateral: tocar-lo la tanca.
     Amb les opcions obertes no n'hi ha: així es veu l'àlbum canviar mentre edites. */
  .scrim {
    display: block;
    position: fixed; inset: 52px 0 0 0; z-index: 15;
    background: rgba(20, 20, 18, .25);
    opacity: 0; pointer-events: none; transition: opacity .25s;
  }
  .sidebar-open .scrim { opacity: 1; pointer-events: auto; }

  /* Pantalles d'impressió i previsualització */
  .print-head { flex-direction: column; padding: 14px 16px; }
  .print-actions { width: 100%; }
  .print-actions .btn { flex: 1; }
  .print-side { --w: calc(100vw - 32px); }
}

/* Focus de teclat visible */
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
