/* ==========================================================================
   ANUNCIOS: PILA DE FLYERS + VISTA EN GRANDE
   Usa las variables y el header de style.css (--bg-color, --primary-color,
   --card-bg, --font-display, --font-body). Cargar DESPUÉS de style.css.
   ========================================================================== */

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;          /* el celular no agranda el texto por su cuenta */
}

.sr-only {
    position: absolute;
    width: 1px; height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
}

/* Alto máximo de la imagen del flyer en la pila (dvh = alto real con barras del navegador móvil) */
body.anuncios-page { --img-max: min(46vh, 340px); }
@supports (height: 1dvh) {
    body.anuncios-page { --img-max: min(46dvh, 340px); }
}
@media screen and (max-height: 720px) {
    body.anuncios-page { --img-max: min(36vh, 300px); }
}
@supports (height: 1dvh) {
    @media screen and (max-height: 720px) {
        body.anuncios-page { --img-max: min(36dvh, 300px); }
    }
}

body.anuncios-page {
    overflow-x: hidden;
    overflow-y: auto;                /* si el teléfono está de lado y no cabe, se puede bajar */
    overscroll-behavior: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

body.anuncios-page.viewer-open {
    overflow: hidden;                /* mientras el visor está abierto no se mueve la página de atrás */
}

/* Sin el destello azul de selección/toque en móvil */
body.anuncios-page,
body.anuncios-page * {
    -webkit-tap-highlight-color: transparent;
}

body.anuncios-page ::selection {
    background: transparent;
}

.anuncios-main {
    min-height: 100vh;
    padding: 130px 1rem 2rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
@supports (height: 1dvh) {
    .anuncios-main { min-height: 100dvh; }
}

.stack-hint {
    font-family: 'Courier New', Courier, monospace;
    font-weight: 700;
    font-size: 0.8rem;
    color: #cbd5e0;
    text-align: center;
    max-width: 38ch;
    margin-bottom: 1.8rem;
}

/* ---------- PILA ---------- */
.flyer-stack {
    position: relative;
    width: min(72vw, 300px);
    /* alto de la imagen más grande + barra de abajo + margen */
    height: calc(var(--img-max) + 4.8rem);
    display: flex;
    align-items: center;
    justify-content: center;
}

/*
  La tarjeta mide EXACTAMENTE lo que mide el flyer (--ar la calcula el JS
  con la proporción real de la imagen). Mínimo 210px para que quepa la barra.
*/
.flyer {
    position: absolute;
    display: block;
    width: max(210px, min(min(72vw, 300px), calc(var(--img-max) * var(--ar, 0.75))));
    cursor: grab;
    touch-action: none;
    --dx: 0px;
    --dy: 0px;
    --tilt: 0deg;
    transform:
        translate(calc(var(--ox, 0px) + var(--dx)), calc(var(--oy, 0px) + var(--dy)))
        rotate(calc(var(--rot, 0deg) + var(--tilt)));
    transition: transform 0.22s cubic-bezier(0.2, 0.9, 0.3, 1);
}

.flyer.is-dragging {
    cursor: grabbing;
    transition: none;
    will-change: transform;
}

/* El marco rodea solo la imagen: nada de relleno a los lados */
.flyer-cover {
    width: fit-content;
    max-width: 100%;
    margin: 0 auto;
    line-height: 0;
    border: 2px solid var(--primary-color);
    box-shadow: 6px 6px 0 rgba(0, 0, 0, 0.4);
    overflow: hidden;
}

.flyer.is-top .flyer-cover {
    border-color: #ffffff;
    box-shadow: 8px 8px 0 var(--primary-color);
}

.flyer.is-dragging .flyer-cover {
    box-shadow: 14px 16px 0 rgba(0, 0, 0, 0.5);
}

.flyer-cover img {
    display: block;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: var(--img-max);      /* imagen completa, sin recortar */
    pointer-events: none;
    -webkit-user-drag: none;
}

/* Si la imagen no existe o no carga */
.flyer-cover.is-missing {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 260px;
    padding: 1rem;
    line-height: 1.2;
    text-align: center;
    white-space: pre-line;
    background: var(--card-bg);
    font-family: var(--font-display);
    font-size: 1.5rem;
    letter-spacing: 1px;
    color: #ff003c;
}

.flyer-meta {
    margin-top: 0.7rem;
    background: var(--bg-color);
    border: 2px solid #4a5568;
    padding: 0.4rem 0.6rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.6rem;
    font-family: 'Courier New', Courier, monospace;
    font-weight: 700;
    font-size: 0.78rem;
    text-transform: uppercase;
    color: #ffffff;
}

.flyer-meta > span {
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.flyer-open {
    flex: 0 0 auto;
    background: var(--primary-color);
    color: #ffffff;
    border: none;
    padding: 0.3rem 0.8rem;
    font-family: var(--font-display);
    font-size: 0.85rem;
    letter-spacing: 1px;
    cursor: pointer;
    touch-action: manipulation;
}

/* Un solo parpadeo agresivo: la etiqueta del flyer de arriba */
@media (prefers-reduced-motion: no-preference) {
    .flyer.is-top:not(.is-dragging) .flyer-meta {
        animation: flyerGlitch 4s infinite;
    }
}

@keyframes flyerGlitch {
    0%, 85%, 100% { background-color: var(--bg-color); color: #ffffff; }
    88% { background-color: var(--primary-color); color: var(--bg-color); }
    92% { background-color: var(--bg-color); color: #ffffff; }
    95% { background-color: #ff003c; color: #ffffff; }
}

/* ---------- CONTROLES ---------- */
.stack-controls {
    margin-top: 1.6rem;
    display: flex;
    gap: 0.8rem;
    flex-wrap: wrap;
    justify-content: center;
    position: relative;
    z-index: 30;
}

.stack-btn {
    background: var(--card-bg);
    color: #ffffff;
    border: 2px solid var(--primary-color);
    padding: 0.5rem 1.1rem;
    font-family: var(--font-display);
    font-size: 0.95rem;
    letter-spacing: 1px;
    cursor: pointer;
    touch-action: manipulation;
    box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.4);
}

.stack-btn:active {
    background: #ffffff;
    color: var(--bg-color);
    border-color: #ffffff;
    transform: translate(2px, 2px);
    box-shadow: 2px 2px 0 rgba(0, 0, 0, 0.4);
}

/* ---------- VISTA EN GRANDE ---------- */
.flyer-viewer {
    position: fixed;
    inset: 0;
    z-index: 10000; /* sobre el header (9999) */
    background: #081221; /* sólido y sin blur: no se transparenta la página y es más ligero en teléfonos */
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding:
        max(1rem, env(safe-area-inset-top))
        max(1rem, env(safe-area-inset-right))
        max(1rem, env(safe-area-inset-bottom))
        max(1rem, env(safe-area-inset-left));
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.15s ease, visibility 0.15s ease;
    cursor: pointer;
    touch-action: manipulation;
}
@supports (height: 1dvh) {
    .flyer-viewer { height: 100dvh; }
}

.flyer-viewer.is-open {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

.viewer-top {
    flex: 0 0 auto;
    display: flex;
    flex-wrap: nowrap;               /* siempre en un solo renglón */
    justify-content: space-between;
    align-items: center;
    gap: 0.8rem;
    padding-bottom: 0.7rem;
    border-bottom: 2px dashed #4a5568;
    cursor: default;
}

.viewer-title {
    flex: 1 1 auto;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-family: var(--font-display);
    font-size: clamp(1rem, 2.4vw, 1.6rem);
    letter-spacing: 1px;
    text-transform: uppercase;
}

.viewer-counter {
    flex: 0 0 auto;
    white-space: nowrap;
    font-family: 'Courier New', Courier, monospace;
    font-weight: 700;
    color: #cbd5e0;
    font-variant-numeric: tabular-nums;
}

.viewer-close {
    flex: 0 0 auto;
    white-space: nowrap;
    background: var(--primary-color);
    color: #ffffff;
    border: none;
    padding: 0.45rem 1.1rem;
    font-family: var(--font-display);
    font-size: 0.95rem;
    letter-spacing: 1px;
    cursor: pointer;
    touch-action: manipulation;
    box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.4);
}

.viewer-close:active {
    background: #ff003c;
}

/*
  Escritorio:  [←]  imagen  [→]
  Móvil:       imagen arriba, flechas abajo (nunca encima de la imagen)
*/
.viewer-stage {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-areas: "prev frame next";
    align-items: center;
    gap: 1.2rem;
}

.viewer-frame {
    grid-area: frame;
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 0;
    margin: 0;
}

/* La imagen se ajusta dentro del espacio libre; no se agranda ni se rellena */
#viewer-img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    cursor: default;
    -webkit-user-drag: none;
    filter: drop-shadow(8px 8px 0 var(--primary-color));
}

.arrow-left  { grid-area: prev; }
.arrow-right { grid-area: next; }

.nav-arrow {
    width: 48px;
    height: 48px;
    background: var(--card-bg);
    color: #ffffff;
    border: 2px solid var(--primary-color);
    font-family: var(--font-display);
    font-size: 1.4rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.4);
}

.nav-arrow:active {
    background: #ffffff;
    color: var(--bg-color);
    border-color: #ffffff;
}

/* Hover solo en dispositivos con mouse (en el teléfono se quedaba "pegado" tras tocar) */
@media (hover: hover) {
    .flyer-open:hover { background: #ffffff; color: var(--bg-color); }
    .stack-btn:hover {
        background: #ffffff;
        color: var(--bg-color);
        border-color: #ffffff;
        box-shadow: 4px 4px 0 var(--primary-color);
    }
    .viewer-close:hover { background: #ff003c; }
    .nav-arrow:hover {
        background: #ffffff;
        color: var(--bg-color);
        border-color: #ffffff;
    }
}

.stack-btn:focus-visible,
.flyer-open:focus-visible,
.viewer-close:focus-visible,
.nav-arrow:focus-visible {
    outline: 3px solid #ff003c;
    outline-offset: 3px;
}

/* Botones cómodos de tocar con el dedo */
@media (pointer: coarse) {
    .flyer-open { min-height: 36px; padding: 0.3rem 1rem; }
    .stack-btn  { min-height: 44px; }
    .viewer-close { min-height: 40px; }
}

/* ---------- MÓVIL / TABLET ---------- */
@media screen and (max-width: 1200px) {
    /* padding-bottom extra: el botón de WhatsApp flota abajo a la derecha */
    .anuncios-main { padding-top: 140px; padding-bottom: 5.5rem; }
}

@media screen and (max-width: 680px) {
    .viewer-top { gap: 0.5rem; }
    .viewer-title { font-size: 0.95rem; letter-spacing: 0.5px; }
    .viewer-counter { font-size: 0.8rem; }
    .viewer-close { padding: 0.4rem 0.8rem; font-size: 0.85rem; }

    .viewer-stage {
        grid-template-columns: 1fr 1fr;
        grid-template-rows: minmax(0, 1fr) auto;
        grid-template-areas:
            "frame frame"
            "prev  next";
        gap: 0.9rem 1rem;
    }

    .arrow-left  { justify-self: end; }
    .arrow-right { justify-self: start; }

    .nav-arrow { width: 54px; height: 44px; }

    .flyer-cover.is-missing { height: 220px; }
}

@media screen and (max-width: 400px) {
    .viewer-title { font-size: 0.85rem; letter-spacing: 0; }
    .viewer-counter { font-size: 0.75rem; }
    .viewer-close { padding: 0.4rem 0.65rem; }
}

@media (prefers-reduced-motion: reduce) {
    .flyer, .flyer-viewer { transition: none; }
}