/* MeMori — la finestra «Aggiungi un libro» nello stile «Ritagli» (27/10/2026, prova in
 * tools/prova-aggiungi): un foglio di carta con l'ombra d'inchiostro; sul telefono sale dal basso e
 * prende lo schermo, e la scansione del codice a barre diventa il grande ritaglio senape. Ricerca a
 * pillola, risultati come riquadri con la copertina e il + tondo (che diventa ✓), conferma in un
 * bollino salvia. Vale solo dentro #add-dialog (i risultati e le edizioni si usano anche altrove).
 * Caratteri e colori di base: ritagli.css. Animazioni: motion.css (D5). */

#add-dialog {
  width: min(760px, calc(100vw - 64px));
  max-height: calc(100vh - 64px);
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: 34px 28px 36px 30px;
  background: var(--rt-paper);
  color: var(--rt-ink);
  font-family: var(--rt-body);
  box-shadow: 8px 8px 0 var(--rt-ink);
}

/* Sul telefono il fuoco va alla finestra (per non aprire la tastiera): nessun contorno. */
#add-dialog:focus {
  outline: none;
}

#add-dialog::backdrop {
  background: color-mix(in srgb, var(--rt-ink) 38%, transparent);
  backdrop-filter: blur(2px);
}

/* Intestazione fissa, corpo che scorre, fondo fisso. */
#add-form {
  display: flex;
  flex-direction: column;
  gap: 0;
  max-height: calc(100vh - 64px);
}

.ad-head {
  position: relative;
  flex: none;
  padding: 26px 30px 14px;
}

#add-dialog h2 {
  margin: 0;
  padding-right: 3rem;
  font: 800 2.3rem/1 var(--rt-display);
  letter-spacing: -0.04em;
  text-transform: none;
}

.ad-close {
  position: absolute;
  top: 22px;
  right: 22px;
  width: 42px;
  height: 42px;
  border: 2px solid var(--rt-ink);
  border-radius: 50%;
  background: var(--rt-paper);
  color: var(--rt-ink);
  font: 700 1.1rem var(--rt-display);
  cursor: pointer;
}

/* Dove va: due pillole in una pillola (i pulsanti di scelta restano, nascosti, per la tastiera). */
#add-dialog .ad-target {
  display: inline-flex;
  margin: 14px 0 0;
  padding: 3px;
  border: 2px solid var(--rt-ink);
  border-radius: 999px;
}

#add-dialog .ad-target label {
  position: relative;
  margin: 0;
  cursor: pointer;
}

#add-dialog .ad-target input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

#add-dialog .ad-target span {
  display: block;
  padding: 0.45rem 1rem;
  border-radius: 999px;
  color: var(--muted);
  font: 600 0.92rem var(--rt-body);
  letter-spacing: 0;
  text-transform: none;
}

#add-dialog .ad-target input:checked + span {
  background: var(--rt-ink);
  color: var(--rt-paper);
}

#add-dialog .ad-target input:focus-visible + span {
  outline: 3px solid var(--rt-accent);
  outline-offset: 2px;
}

.ad-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  padding: 4px 30px 18px;
}

/* --- Ricerca: pillola grande, Cerca, e il bottone tondo per scansionare ---------------- */
.ad-search {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

/* Un'etichetta: la regola dei moduli la metterebbe in colonna (lente sopra il campo). */
#add-dialog .ad-field {
  flex: 1;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
  margin: 0;
  padding: 0.8rem 1.1rem;
  border: 2px solid var(--rt-ink);
  border-radius: 999px;
  background: var(--bg);
}

#add-dialog .ad-field:focus-within {
  box-shadow: 4px 4px 0 var(--rt-accent);
}

.ad-field .icon {
  flex: none;
  width: 22px;
  height: 22px;
}

#add-dialog .ad-field input {
  flex: 1;
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  color: var(--rt-ink);
  font: 500 1.05rem var(--rt-body);
  outline: none;
  box-shadow: none;
}

#add-dialog .ad-go {
  flex: none;
  padding: 0.8rem 1.3rem;
  border: 0;
  border-radius: 999px;
  background: var(--rt-ink);
  color: var(--rt-paper);
  font: 700 0.95rem var(--rt-body);
  letter-spacing: 0;
  text-transform: none;
  cursor: pointer;
}

#add-dialog .ad-scan {
  flex: none;
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  padding: 0;
  border: 0;
  border-radius: 55% 45% 50% 50% / 50% 55% 45% 50%;
  background: var(--rt-accent);
  color: var(--rt-accent-ink);
  box-shadow: 3px 3px 0 var(--rt-ink);
  font-family: var(--rt-body);
  cursor: pointer;
}

.ad-scan .icon {
  width: 26px;
  height: 26px;
}

/* Sul PC solo l'icona (il testo resta per i lettori di schermo). */
.ad-scan-text {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.ad-scan-text small {
  display: block;
}

#add-dialog .ad-close:focus-visible,
#add-dialog .ad-go:focus-visible,
#add-dialog .ad-scan:focus-visible {
  outline: 3px solid var(--rt-accent);
  outline-offset: 2px;
}

/* --- Finestra vuota: un piccolo disegno e un esempio ----------------------------------- */
.ad-empty {
  display: none;
  justify-items: center;
  gap: 0.6rem;
  padding: 1.6rem 1rem 0.6rem;
  color: var(--muted);
  text-align: center;
}

#add-form:has(#add-results:empty):has(#add-scanner[hidden]) .ad-empty {
  display: grid;
}

#add-form:has(#add-scan[hidden]) .ad-empty-scan {
  display: none;
}

/* Con la fotocamera accesa il pulsante «Scansiona» non serve (c'è «Annulla»): si toglie, e il riquadro
   della fotocamera sale al suo posto. */
#add-form:has(#add-scanner:not([hidden])) .ad-scan {
  display: none;
}

.ad-empty p {
  margin: 0;
  max-width: 36ch;
}

.ad-stack {
  position: relative;
  width: 120px;
  height: 92px;
}

.ad-stack i {
  position: absolute;
  left: 50%;
  height: 18px;
  border-radius: 4px;
  box-shadow: 3px 3px 0 var(--rt-ink);
}

.ad-stack i:nth-child(1) {
  bottom: 0;
  width: 110px;
  translate: -50% 0;
  background: var(--rt-coral);
}

.ad-stack i:nth-child(2) {
  bottom: 22px;
  width: 94px;
  translate: -46% 0;
  rotate: -4deg;
  background: #8e5fd0;
}

.ad-stack i:nth-child(3) {
  bottom: 44px;
  width: 100px;
  translate: -52% 0;
  rotate: 3deg;
  background: var(--rt-accent);
}

.ad-stack b {
  position: absolute;
  right: -6px;
  top: -8px;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--rt-ink);
  color: var(--rt-paper);
  font: 800 1.4rem var(--rt-display);
}

/* --- Stato e conferma ----------------------------------------------------------------- */
#add-dialog .lookup-status {
  color: var(--muted);
  font-size: 0.92rem;
}

/* Aggiunto: un bollino salvia con la ✓ in un cerchio, dove è finito il libro, e la sua copertina
   piccola un po' storta; il campo resta pronto per il libro successivo. I coriandoli (dentro la ✓)
   si vedono solo con l'animazione (motion.css, D5). */
.ad-done {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  padding: 0.8rem 1rem;
  border-radius: 20px 26px 22px 24px;
  background: var(--rt-sage);
  color: var(--rt-ink);
}

.ad-ok {
  position: relative;
  flex: none;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--rt-paper);
  color: var(--rt-ink);
  font: 800 1.2rem var(--rt-display);
  box-shadow: 2px 2px 0 var(--rt-ink);
}

.ad-done-text {
  min-width: 0;
}

.ad-done-text b {
  display: block;
  font: 700 1rem/1.2 var(--rt-display);
  overflow-wrap: anywhere;
}

.ad-done-text small {
  font-size: 0.82rem;
  opacity: 0.8;
}

.ad-done-cover {
  flex: none;
  width: 34px;
  height: auto;
  aspect-ratio: 2 / 3;
  margin-left: auto;
  overflow: hidden;
  border-radius: 3px;
  box-shadow: 2px 2px 0 var(--rt-ink);
  rotate: 4deg;
}

.ad-done-cover img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.ad-confetti {
  display: none;
}

/* --- Risultati: riquadri con la copertina e il + tondo -------------------------------- */
#add-dialog .add-results {
  gap: 0.8rem;
  max-height: none;
  overflow: visible;
}

#add-dialog .lookup-item {
  display: grid;
  grid-template-columns: 58px minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.9rem;
  padding: 0.8rem 0.9rem;
  border: 0;
  border-radius: 22px 26px 24px 20px;
  background: var(--bg);
  color: var(--rt-ink);
  box-shadow: none;
  transition: box-shadow 0.15s ease;
}

#add-dialog .lookup-item:hover {
  box-shadow: 0 0 0 2px var(--rt-ink);
}

#add-dialog .lookup-item:focus-visible {
  outline: 3px solid var(--rt-accent);
  outline-offset: 2px;
}

#add-dialog .lookup-thumb {
  width: 58px;
  height: auto;
  aspect-ratio: 2 / 3;
  border-radius: 4px;
  box-shadow: 4px 4px 0 var(--rt-ink);
}

#add-dialog .lookup-text {
  font-size: 0.85rem;
  color: var(--muted);
}

#add-dialog .lookup-text strong {
  color: var(--rt-ink);
  font: 700 1.05rem/1.2 var(--rt-display);
}

#add-dialog .lookup-author {
  color: var(--rt-ink);
  font-size: 0.9rem;
  font-style: normal;
}

/* Il «+ Aggiungi» è un + tondo senape (la parola resta nel codice per le altre finestre). */
#add-dialog .lookup-add {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 55% 45% 50% 50% / 50% 55% 45% 50%;
  background: var(--rt-accent);
  color: var(--rt-accent-ink);
  box-shadow: 3px 3px 0 var(--rt-ink);
  font-size: 0;
}

#add-dialog .lookup-add::before {
  content: "+";
  font: 800 1.5rem var(--rt-display);
}

/* Appena aggiunto: diventa una spunta salvia. */
#add-dialog .lookup-add.is-added {
  background: var(--rt-sage);
  color: #161616;
}

#add-dialog .lookup-add.is-added::before {
  content: "✓";
}

/* «9 edizioni Bompiani →»: una pillola sotto il risultato. */
#add-dialog .lookup-editions {
  align-self: flex-start;
  margin: 0.4rem 0 0 calc(58px + 1.8rem);
  padding: 0.15rem 0.7rem;
  border: 1.5px solid var(--rt-ink);
  border-radius: 999px;
  color: var(--rt-ink);
  font: 600 0.78rem var(--rt-body);
}

#add-dialog .editions-open {
  align-self: center;
  padding: 0.55rem 1.2rem;
  border: 2px solid var(--rt-ink);
  border-radius: 999px;
  background: none;
  color: var(--rt-ink);
  font: 600 0.92rem var(--rt-body);
  letter-spacing: 0;
  text-transform: none;
}

/* --- Scansione: inquadratura con gli angoli a ritaglio e la linea che scorre ----------- */
/* Il riquadro ha subito la sua forma (niente salto quando arriva il video) e un fondo scuro; mentre la
   fotocamera si accende (is-starting) c'è la scritta, poi il video entra in dissolvenza. È anche il
   contenitore delle misure della linea che scorre (cqh). */
#add-dialog .scanner-view {
  aspect-ratio: 4 / 3;
  border-radius: 26px;
  background: #1c1b1a;
  container-type: size;
}

#add-dialog .scanner video {
  width: 100%;
  height: 100%;
  max-height: none;
  object-fit: cover;
  border-radius: 26px;
  transition: opacity 0.3s ease;
}

.ad-cam-wait {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  color: #f1ece2;
  font: 600 0.95rem var(--rt-body);
}

#add-scanner.is-starting .ad-cam-wait {
  display: grid;
}

#add-scanner.is-starting :is(video, .ad-laser, .scanner-guide) {
  opacity: 0;
}

#add-dialog .scanner-guide {
  inset: 30% 8%;
  border: 0;
  border-radius: 18px;
  box-shadow: 0 0 0 100vmax rgb(0 0 0 / 0.35);
}

#add-dialog .scanner-guide::before,
#add-dialog .scanner-guide::after {
  content: "";
  position: absolute;
  width: 34px;
  height: 34px;
  border: 5px solid var(--rt-accent);
}

#add-dialog .scanner-guide::before {
  left: -3px;
  top: -3px;
  border-right: 0;
  border-bottom: 0;
  border-radius: 14px 0 0 0;
}

#add-dialog .scanner-guide::after {
  right: -3px;
  bottom: -3px;
  border-left: 0;
  border-top: 0;
  border-radius: 0 0 14px 0;
}

.ad-laser {
  position: absolute;
  left: 10%;
  right: 10%;
  top: 36%;
  will-change: translate;
  height: 3px;
  background: var(--rt-coral);
  box-shadow: 0 0 12px var(--rt-coral);
  pointer-events: none;
}

#add-dialog .scanner-actions .btn {
  border: 2px solid var(--rt-ink);
  border-radius: 999px;
  background: none;
  color: var(--rt-ink);
  font: 600 0.9rem var(--rt-body);
  letter-spacing: 0;
  text-transform: none;
}

/* --- Fondo: «Non lo trovi? Inseriscilo a mano» ------------------------------------------ */
.ad-foot {
  flex: none;
  padding: 14px 30px 20px;
  border-top: 2px dashed color-mix(in srgb, var(--muted) 50%, transparent);
}

#add-dialog .ad-foot .link {
  color: var(--rt-ink);
  font: 600 0.95rem var(--rt-body);
  text-decoration: underline;
  text-underline-offset: 3px;
}

#add-dialog .form-error:empty {
  display: none;
}

/* --- Telefono: un foglio che sale dal basso e prende lo schermo ------------------------- */
@media (max-width: 760px) {
  /* Il foglio copre già lo schermo: niente sfocatura dietro (pesa mentre gira il video). */
  #add-dialog::backdrop {
    backdrop-filter: none;
  }

  #add-dialog .scanner-view {
    aspect-ratio: 3 / 4;
    max-height: 52vh;
    margin-inline: auto;
  }

  #add-dialog {
    width: 100vw;
    max-width: 100vw;
    height: calc(100dvh - 38px);
    max-height: none;
    margin: auto 0 0;
    border-radius: 28px 28px 0 0;
    box-shadow: 0 -4px 0 var(--rt-ink);
  }

  #add-form {
    height: 100%;
    max-height: none;
  }

  /* La linguetta in alto, come i fogli che si trascinano. */
  .ad-head::before {
    content: "";
    display: block;
    width: 46px;
    height: 6px;
    margin: -4px auto 10px;
    border-radius: 999px;
    background: color-mix(in srgb, var(--muted) 55%, transparent);
  }

  .ad-head {
    padding: 14px 18px 10px;
  }

  #add-dialog h2 {
    font-size: 1.8rem;
  }

  .ad-close {
    top: 26px;
    right: 14px;
    width: 38px;
    height: 38px;
  }

  .ad-body {
    padding: 4px 18px 18px;
  }

  .ad-search {
    flex-wrap: wrap;
  }

  /* Cerca col tasto Invio della tastiera; la scansione è il grande ritaglio senape. */
  #add-dialog .ad-go {
    display: none;
  }

  #add-dialog .ad-scan {
    display: flex;
    align-items: center;
    gap: 0.9rem;
    width: 100%;
    height: auto;
    padding: 0.9rem 1.1rem;
    border-radius: 26px 32px 28px 24px;
    box-shadow: 4px 4px 0 var(--rt-ink);
    text-align: left;
  }

  #add-dialog .ad-scan[hidden] {
    display: none;
  }

  .ad-scan-icon {
    display: grid;
    place-items: center;
    width: 52px;
    height: 52px;
    flex: none;
    border-radius: 50%;
    background: var(--rt-paper);
    color: var(--rt-ink);
  }

  .ad-scan-text {
    position: static;
    width: auto;
    height: auto;
    overflow: visible;
    clip-path: none;
    white-space: normal;
  }

  .ad-scan-text b {
    display: block;
    font: 800 1.15rem/1.1 var(--rt-display);
  }

  .ad-scan-text small {
    font-size: 0.82rem;
    opacity: 0.8;
  }

  #add-dialog .lookup-item {
    grid-template-columns: 48px minmax(0, 1fr) auto;
    gap: 0.7rem;
    padding: 0.7rem;
  }

  #add-dialog .lookup-thumb {
    width: 48px;
  }

  #add-dialog .lookup-add {
    width: 42px;
    height: 42px;
  }

  #add-dialog .lookup-editions {
    margin-left: calc(48px + 1.4rem);
  }

  .ad-foot {
    padding: 12px 18px calc(18px + env(safe-area-inset-bottom));
  }
}
