/* ============================================================
   VIDHA · MOTION
   Animaciones discretas que sirven a la psicología del lector.
   Cada efecto tiene un porqué: reducir ansiedad, ordenar atención,
   marcar jerarquía. Nunca decorar por decorar.
   
   Respeta prefers-reduced-motion en todo.
   ============================================================ */

/* ============================================================
   1 · VIEW TRANSITIONS — transición fluida entre páginas
   Reduce la "ruptura blanca" cognitiva al cambiar de URL.
   ============================================================ */

@view-transition {
  navigation: auto;
}

::view-transition-old(root) {
  animation: fade-out 280ms cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

::view-transition-new(root) {
  animation: fade-in 320ms cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

@keyframes fade-out {
  to { opacity: 0; }
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ============================================================
   2 · READING PROGRESS BAR
   Top fixed. Granate. Solo en páginas de lectura larga.
   Psicología: reduce ansiedad "cuánto me queda".
   ============================================================ */

.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: transparent;
  z-index: 100;
  pointer-events: none;
}

.reading-progress__fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 80ms linear;
  transform-origin: left;
}

/* Modern: scroll-driven animation cuando esté soportado */
@supports (animation-timeline: scroll()) {
  .reading-progress__fill {
    width: 100%;
    transform: scaleX(0);
    transition: none;
    animation: progress-grow linear;
    animation-timeline: scroll(root);
    animation-range: 0 100%;
  }
  
  @keyframes progress-grow {
    from { transform: scaleX(0); }
    to { transform: scaleX(1); }
  }
}

/* ============================================================
   3 · SCROLL REVEAL
   Bloques que aparecen con fade+lift al entrar en viewport.
   Psicología: ordena la atención visualmente sin forzar.
   Solo aplica donde se añade data-reveal.
   ============================================================ */

.js [data-reveal] {
  opacity: 0;
  transform: translateY(24px);
  transition: opacity 700ms cubic-bezier(0.16, 1, 0.3, 1),
              transform 700ms cubic-bezier(0.16, 1, 0.3, 1);
  transition-delay: var(--reveal-delay, 0ms);
  will-change: opacity, transform;
}

.js [data-reveal].is-revealed {
  opacity: 1;
  transform: translateY(0);
}

/* ============================================================
   4 · PULL-QUOTE EMPHASIS
   Las citas largas aparecen con énfasis suave al entrar.
   Psicología: invita a pausa de lectura sin gritar.
   ============================================================ */

.carta-quote-block,
.carta-emph {
  opacity: 0.55;
  transform: scale(0.985);
  transition: opacity 900ms cubic-bezier(0.16, 1, 0.3, 1),
              transform 900ms cubic-bezier(0.16, 1, 0.3, 1);
}

.carta-quote-block.is-emph,
.carta-emph.is-emph {
  opacity: 1;
  transform: scale(1);
}

/* ============================================================
   5 · TOC FLOTANTE (ensayos)
   Outline sidebar derecho que muestra sección actual.
   Psicología: orientación + agencia (ir a una sección concreta).
   Solo desktop ≥1100px.
   ============================================================ */

.essay-toc {
  display: none;
}

@media (min-width: 1100px) {
  .essay-toc {
    display: block;
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    right: max(2vw, calc((100vw - 720px) / 2 - 220px));
    width: 200px;
    max-height: 70vh;
    overflow-y: auto;
    padding: var(--space-5) 0;
    z-index: 50;
    opacity: 0;
    animation: toc-appear 500ms ease 400ms forwards;
  }
  
  @keyframes toc-appear {
    to { opacity: 1; }
  }
  
  .essay-toc__title {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--ink-faint);
    margin-bottom: var(--space-4);
    padding-left: var(--space-3);
  }
  
  .essay-toc__list {
    list-style: none;
    padding: 0;
    margin: 0;
    border-left: 1px solid var(--line);
  }
  
  .essay-toc__list li {
    margin: 0;
  }
  
  .essay-toc__list a {
    display: block;
    padding: var(--space-2) var(--space-4);
    margin-left: -1px;
    border-left: 1px solid transparent;
    font-family: var(--font-body);
    font-size: 0.82rem;
    line-height: 1.4;
    color: var(--ink-faint);
    text-decoration: none;
    transition: color 250ms ease, border-color 250ms ease;
  }
  
  .essay-toc__list a:hover {
    color: var(--ink);
  }
  
  .essay-toc__list a.is-active {
    color: var(--accent);
    border-left-color: var(--accent);
    font-weight: var(--weight-medium);
  }
}

/* ============================================================
   6 · BANDA DE CITAS (home)
   Marquee horizontal lento con frases ancla del libro.
   Psicología: abre apetito por el libro sin "vender".
   ============================================================ */

.quote-band {
  background: var(--paper-dark);
  color: var(--ink-inverse);
  padding: var(--space-6) 0;
  overflow: hidden;
  position: relative;
  border-top: 1px solid var(--line-strong);
  border-bottom: 1px solid var(--line-strong);
}

.quote-band__track {
  display: flex;
  gap: var(--space-9);
  white-space: nowrap;
  animation: marquee 80s linear infinite;
  will-change: transform;
}

.quote-band__item {
  font-family: var(--font-display);
  font-size: clamp(1.2rem, 2.2vw, 1.65rem);
  font-style: italic;
  font-weight: var(--weight-light);
  letter-spacing: -0.005em;
  color: var(--ink-inverse);
  opacity: 0.88;
  flex-shrink: 0;
}

.quote-band__item::after {
  content: " · ";
  color: var(--accent-soft);
  margin: 0 var(--space-3);
  opacity: 0.6;
}

@keyframes marquee {
  from { transform: translateX(0); }
  to { transform: translateX(-50%); }
}

/* Pausa al hover — pequeño gesto de respeto al lector */
.quote-band:hover .quote-band__track {
  animation-play-state: paused;
}

/* ============================================================
   7 · MAGNETIC HOVER en CTA primarias
   Microinteracción sutil para botones principales.
   Psicología: refuerza agencia, da respuesta táctil.
   ============================================================ */

.btn-primary {
  transition: transform 280ms cubic-bezier(0.34, 1.56, 0.64, 1),
              background 220ms ease,
              box-shadow 280ms ease !important;
}

.btn-primary:hover {
  transform: translateY(-2px) scale(1.015);
  box-shadow: 0 12px 28px rgba(122, 63, 56, 0.25);
}

.btn-primary:active {
  transform: translateY(0) scale(0.99);
  transition-duration: 80ms;
}

/* ============================================================
   8 · LINK UNDERLINE ANIMATION
   Subrayado que se expande al hover. Más elegante que el default.
   ============================================================ */

.carta-body p a,
.diario-section a,
article p a {
  background-image: linear-gradient(to right, var(--accent), var(--accent));
  background-size: 100% 1px;
  background-position: 0 100%;
  background-repeat: no-repeat;
  text-decoration: none;
  transition: background-size 280ms cubic-bezier(0.16, 1, 0.3, 1);
}

.carta-body p a:hover,
.diario-section a:hover,
article p a:hover {
  background-size: 100% 2px;
}

/* No tocar los enlaces de navegación */
.site-header a,
.site-footer a,
.btn,
.btn-link,
.home-notes-list a,
.diario-notes-list a,
.news-archive-item,
.carta-back,
.home-book {
  background-image: none !important;
}

/* ============================================================
   ACCESIBILIDAD — respetar prefers-reduced-motion
   ============================================================ */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  
  .js [data-reveal] {
    opacity: 1;
    transform: none;
  }
  
  .carta-quote-block,
  .carta-emph {
    opacity: 1;
    transform: none;
  }
  
  .quote-band__track {
    animation: none;
  }
  
  ::view-transition-old(root),
  ::view-transition-new(root) {
    animation: none;
  }
}


/* ============================================================
   9 · STICKY HEADER SCROLL-AWARE
   - Sticky en top (siempre accesible)
   - Backdrop-blur cuando hay scroll (capa cristal)
   - Se oculta al scrollear hacia abajo (más espacio para leer)
   - Reaparece al scrollear hacia arriba
   - Psicología: protagonismo al contenido sin perder control nav
   ============================================================ */

.site-header {
  position: sticky;
  top: 0;
  z-index: 90;
  transition: transform 320ms cubic-bezier(0.4, 0, 0.2, 1),
              background-color 280ms ease,
              backdrop-filter 280ms ease,
              border-bottom-color 280ms ease,
              box-shadow 280ms ease;
  will-change: transform;
}

/* Estado scrolleado: cristal blur con fondo translúcido */
.site-header.is-scrolled {
  background: rgba(250, 247, 241, 0.82);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom-color: var(--line-soft);
  box-shadow: 0 1px 0 rgba(26, 24, 21, 0.04);
}

/* Estado oculto: scrollear abajo lo recoge */
.site-header.is-hidden {
  transform: translateY(-100%);
}

/* Asegurar el body no añade espacio bajo el header sticky */
body { min-height: 100vh; }

@media (prefers-reduced-motion: reduce) {
  .site-header {
    transition: background-color 0ms, backdrop-filter 0ms;
  }
  .site-header.is-hidden {
    transform: none;
  }
}


/* ============================================================
   10 · HERO TITLE — LETTER (WORD) ANIMATION
   Las palabras del titular aparecen escalonadamente al cargar.
   Cada palabra con stagger ~70ms. La itálica granate llega después
   como un pequeño acento editorial.
   Psicología: marca un ritmo de lectura desde el primer instante.
   ============================================================ */

.hero-title--animated .hero-line {
  display: block;
}

.hero-title--animated .hero-word {
  display: inline-block;
  opacity: 0;
  transform: translateY(18px);
  animation: hero-word-in 800ms cubic-bezier(0.16, 1, 0.3, 1) forwards;
  /* Stagger: cada palabra con un retraso distinto via inline */
}

@keyframes hero-word-in {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Stagger por orden de aparición en el DOM
   Línea 1: 5 palabras */
.hero-title--animated .hero-line:nth-child(1) .hero-word:nth-child(1) { animation-delay: 80ms; }
.hero-title--animated .hero-line:nth-child(1) .hero-word:nth-child(2) { animation-delay: 150ms; }
.hero-title--animated .hero-line:nth-child(1) .hero-word:nth-child(3) { animation-delay: 220ms; }
.hero-title--animated .hero-line:nth-child(1) .hero-word:nth-child(4) { animation-delay: 290ms; }
.hero-title--animated .hero-line:nth-child(1) .hero-word:nth-child(5) { animation-delay: 360ms; }
/* Línea 2: 4 palabras + em */
.hero-title--animated .hero-line:nth-child(2) .hero-word:nth-child(1) { animation-delay: 460ms; }
.hero-title--animated .hero-line:nth-child(2) .hero-word:nth-child(2) { animation-delay: 530ms; }
.hero-title--animated .hero-line:nth-child(2) .hero-word:nth-child(3) { animation-delay: 600ms; }
.hero-title--animated .hero-line:nth-child(2) .hero-word:nth-child(4) { animation-delay: 670ms; }
/* La italica granate llega un poco más tarde, con énfasis */
.hero-title--animated .hero-line:nth-child(2) .hero-word--em { animation-delay: 880ms; }
/* Línea 3: 5 palabras */
.hero-title--animated .hero-line:nth-child(3) .hero-word:nth-child(1) { animation-delay: 1020ms; }
.hero-title--animated .hero-line:nth-child(3) .hero-word:nth-child(2) { animation-delay: 1090ms; }
.hero-title--animated .hero-line:nth-child(3) .hero-word:nth-child(3) { animation-delay: 1160ms; }
.hero-title--animated .hero-line:nth-child(3) .hero-word:nth-child(4) { animation-delay: 1230ms; }
.hero-title--animated .hero-line:nth-child(3) .hero-word:nth-child(5) { animation-delay: 1300ms; }

/* Resto del hero (presentación, location, botones, quote) entra
   después del titular completo (~1400ms) con fade simple */
.hero-presentation,
.hero-location,
.hero-actions,
.hero-quote {
  opacity: 0;
  animation: hero-rest-in 600ms cubic-bezier(0.4, 0, 0.2, 1) forwards;
  animation-delay: 1550ms;
}
.hero-location { animation-delay: 1650ms; }
.hero-actions { animation-delay: 1750ms; }
.hero-quote { animation-delay: 2000ms; }

@keyframes hero-rest-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .hero-title--animated .hero-word,
  .hero-presentation,
  .hero-location,
  .hero-actions,
  .hero-quote {
    opacity: 1;
    transform: none;
    animation: none;
  }
}


/* ============================================================
   11 · CLICK-TO-COPY en pull-quotes y citas importantes
   Selectores: .carta-quote-block, .carta-emph, .quote-band__item,
              blockquote dentro de error-quote
   Solo desktop (mobile tiene long-press nativo).
   ============================================================ */

@media (hover: hover) and (pointer: fine) {
  .carta-quote-block,
  .carta-emph,
  .quote-band__item,
  .error-quote blockquote,
  blockquote.copyable {
    cursor: pointer;
    transition: opacity 200ms ease, transform 200ms ease;
    position: relative;
  }
  
  /* Hint sutil al hover */
  .carta-quote-block:hover,
  .carta-emph:hover,
  .quote-band__item:hover,
  .error-quote blockquote:hover,
  blockquote.copyable:hover {
    opacity: 1 !important;
  }
  
  .carta-quote-block:hover::after,
  .carta-emph:hover::after,
  .error-quote blockquote:hover::after,
  blockquote.copyable:hover::after {
    content: "Copiar cita";
    position: absolute;
    top: -28px;
    right: 0;
    font-family: var(--font-mono);
    font-size: 0.7rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--ink-faint);
    opacity: 0;
    animation: copyhint-in 200ms 400ms forwards;
    pointer-events: none;
  }
  
  @keyframes copyhint-in {
    to { opacity: 1; }
  }
  
  /* Feedback de copia */
  .quote-copied {
    color: var(--accent) !important;
  }
  .quote-copied.carta-emph,
  .quote-copied .quote-band__item {
    color: var(--accent) !important;
  }
}

/* Toast notification — discreto, abajo a la derecha */
.copy-toast {
  position: fixed;
  bottom: var(--space-5);
  right: var(--space-5);
  background: var(--ink);
  color: var(--paper);
  padding: var(--space-3) var(--space-5);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  z-index: 200;
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 300ms cubic-bezier(0.16, 1, 0.3, 1),
              transform 300ms cubic-bezier(0.16, 1, 0.3, 1);
  pointer-events: none;
  box-shadow: 0 8px 24px rgba(26, 24, 21, 0.2);
}

.copy-toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.copy-toast::before {
  content: "✓ ";
  color: var(--green, #4a7c59);
  margin-right: 4px;
}

@media (max-width: 600px) {
  .copy-toast {
    left: var(--space-4);
    right: var(--space-4);
    text-align: center;
  }
}

@media (prefers-reduced-motion: reduce) {
  .copy-toast {
    transition: opacity 0ms;
    transform: none;
  }
}


/* ============================================================
   12 · HOVER PREVIEW de links a notas/cartas/ensayos
   Patrón editorial moderno (Notion, Stripe Press, Linear blog).
   Solo desktop (hover-aware).
   ============================================================ */

@media (hover: hover) and (pointer: fine) {
  
  /* Marca de "tiene preview" en links de piezas */
  a[data-preview-slug] {
    position: relative;
  }
  
  /* El tooltip — único, reposicionable con JS */
  .piece-preview {
    position: fixed;
    z-index: 200;
    min-width: 280px;
    max-width: 360px;
    padding: var(--space-4) var(--space-5);
    background: var(--paper);
    border: 1px solid var(--line);
    border-left: 3px solid var(--accent);
    border-radius: 4px;
    box-shadow: 0 12px 32px rgba(26, 24, 21, 0.12),
                0 2px 6px rgba(26, 24, 21, 0.06);
    pointer-events: none;
    opacity: 0;
    transform: translateY(6px);
    transition: opacity 180ms cubic-bezier(0.4, 0, 0.2, 1),
                transform 180ms cubic-bezier(0.4, 0, 0.2, 1);
    font-family: var(--font-body);
  }
  
  .piece-preview.is-visible {
    opacity: 1;
    transform: translateY(0);
  }
  
  /* Border-left forest cuando es ensayo */
  .piece-preview[data-kind="Ensayo"] {
    border-left-color: var(--forest);
  }
  
  .piece-preview__meta {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-family: var(--font-mono);
    font-size: 0.66rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--ink-faint);
    margin-bottom: var(--space-2);
  }
  
  .piece-preview__kind {
    color: var(--accent);
    font-weight: var(--weight-medium);
  }
  
  .piece-preview[data-kind="Ensayo"] .piece-preview__kind {
    color: var(--forest);
  }
  
  .piece-preview__sep {
    opacity: 0.5;
  }
  
  .piece-preview__title {
    font-family: var(--font-display);
    font-weight: var(--weight-regular);
    font-size: 1.05rem;
    line-height: 1.3;
    color: var(--ink);
    margin: 0 0 var(--space-3);
    letter-spacing: -0.005em;
  }
  
  .piece-preview__excerpt {
    font-size: 0.84rem;
    line-height: 1.5;
    color: var(--ink-soft);
    margin: 0 0 var(--space-3);
    /* Limita a 3 líneas */
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  
  .piece-preview__read {
    font-family: var(--font-mono);
    font-size: 0.66rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--ink-faint);
  }
  
  .piece-preview__read::before {
    content: "⏱  ";
    opacity: 0.7;
  }
}

@media (prefers-reduced-motion: reduce) {
  .piece-preview {
    transition: opacity 0ms;
    transform: none;
  }
}
