/* Property of Sistema THEAD. All rights reserved. */
/* =====================================================================
   NATIVE DIALOGS (tj-dialog.js) — the host and the popup's own chrome
   =====================================================================
   Every popup is a native <dialog> opened with showModal(), so the browser
   makes the page behind inert. This file is the only place the window's
   look is declared:

     dialog.tj-dialog-host          the invisible full-screen host
     ::backdrop                     the dimming, painted by the browser
     .tj-dialog                     the white card (also .swal2-popup)
     .tj-dialog__header             icon / image / title / close
     .tj-dialog__body               prose, controls; the only scroll area
     .tj-dialog__footer             actions, footer, timer bar
     .tj-dialog__close              the one close control

   The caller-specific contents still live with their pages (home.css,
   story.css, gallery.css, …). The generic .swal2-* rules that used to be
   copied into popups.css, index/70-popups.css, index/80-responsive.css and
   settings/modal.css moved here, so one file now decides the frame. The
   .swal2-* class names stay on the elements: callers and tests read them.

   Look: opaque white, the app's 28px radius, a hairline instead of a
   shadow, and the native backdrop instead of a mascot scrim layer.
   ===================================================================== */
:root {
    --popup-radius: 28px;
    --tj-dialog-inset: clamp(10px, 2.4vmin, 24px);
    --tj-dialog-gap: clamp(0.5rem, 1.1vmin, 0.8rem);
    /* Sizes of the close disc, declared on the card so the title can
       reserve the same gutter on both sides and stay centred. */
    --tj-close-size: clamp(44px, 7vmin, 52px);
}

/* ---- The host ---------------------------------------------------------
   Invisible and full screen. The card inside paints every visible pixel;
   the native ::backdrop paints the dimming. */
dialog.tj-dialog-host {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    overflow: visible;
}

/* ---- Native backdrop --------------------------------------------------
   One colour from tokens.css, no blur and no extra full-screen layer.
   Open fades in with @starting-style and allow-discrete; close fades out
   while the host carries .tj-dialog-host--closing (the engine holds the
   dialog in the top layer for those 140ms). */
dialog.tj-dialog-host::backdrop {
    background: rgba(var(--brand-ink-rgb), 0.45);
    transition: background-color 0.18s ease, display 0.18s;
    transition-behavior: allow-discrete;
}

@starting-style {
    dialog.tj-dialog-host[open]::backdrop {
        background: rgba(var(--brand-ink-rgb), 0);
    }
}

/* SweetAlert's container is the positioning grid, never a surface. */
.swal2-container.swal2-backdrop-show,
.swal2-container.swal2-backdrop-hide {
    background: transparent;
}

/* The old persistent scrim element (swalMascot.js keeps chaining working)
   no longer paints: the native backdrop is the only dark layer. */
.tactile-popup-scrim {
    background: transparent;
}

/* ---- The card --------------------------------------------------------
   Three rows: header and footer keep their natural height, the body takes
   the rest and scrolls. So the actions are always reachable on a short
   phone even when the content is taller than the screen. */
.tj-dialog {
    position: relative;
    grid-template-rows: auto minmax(0, 1fr) auto;
    max-height: calc(100dvh - (2 * var(--tj-dialog-inset)));
    border: 0;
    border-radius: var(--popup-radius);
    background: var(--brand-white);
    color: var(--brand-ink);
    font-family: var(--app-font);
    /* Opened by showModal(): fade and lift a few pixels, never bounce. */
    opacity: 1;
    translate: 0 0;
    scale: 1;
    transition:
        opacity 0.2s ease,
        translate 0.2s cubic-bezier(0.22, 1, 0.36, 1),
        scale 0.2s cubic-bezier(0.22, 1, 0.36, 1);
}

@starting-style {
    dialog.tj-dialog-host[open] .tj-dialog {
        opacity: 0;
        translate: 0 8px;
        scale: 0.985;
    }
}

/* Close: the engine adds this class for 140ms before it closes and removes
   the host. Same properties, slightly quicker, no bounce. */
dialog.tj-dialog-host--closing {
    pointer-events: none;
}

dialog.tj-dialog-host--closing .tj-dialog {
    opacity: 0;
    translate: 0 4px;
    scale: 0.99;
    transition-duration: 0.14s;
}

dialog.tj-dialog-host--closing::backdrop {
    background: rgba(var(--brand-ink-rgb), 0);
}

/* Flat surface, one radius. The !important outranks the per-page gradients
   and drop shadows that used to be added on top of SweetAlert's box
   (home.css, gallery.css) without moving those files. */
.swal2-popup {
    border: 0 !important;
    border-radius: var(--popup-radius) !important;
    background: var(--brand-white) !important;
    box-shadow: none !important;
    padding: 1.35em !important;
    font-family: var(--app-font) !important;
}

/* ---- Header ---------------------------------------------------------- */
.tj-dialog__header {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.45rem;
    min-width: 0;
}

.tj-dialog__header > .swal2-title {
    margin: 0;
    padding: 0.1rem clamp(1.2rem, 4.5vmin, 2.6rem);
    color: var(--brand-ink);
    font-size: clamp(1.15rem, 1.1vw + 1rem, 1.45rem);
    font-weight: 800;
    line-height: 1.2;
    letter-spacing: -0.01em;
    text-wrap: balance;
}

.tj-dialog__header > .swal2-image {
    margin: 0 auto 0.2rem;
    border-radius: 12px;
}

.tj-dialog__header > .swal2-icon {
    margin: 0 auto;
}

/* ---- Body ------------------------------------------------------------
   The only scroll area. overscroll-behavior stops a swipe at the end of
   the list from scrolling the (inert) page behind. */
.tj-dialog__body {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    gap: var(--tj-dialog-gap);
    min-width: 0;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.tj-dialog__body > .swal2-html-container {
    margin: 0;
    padding: 0;
    font-size: 1rem;
    line-height: 1.5;
}

.tj-dialog__body > .swal2-input,
.tj-dialog__body > .swal2-textarea,
.tj-dialog__body > .swal2-select {
    margin: 0;
}

.tj-dialog__body > .swal2-input-label {
    margin: 0;
    text-align: start;
}

/* ---- Footer ---------------------------------------------------------- */
.tj-dialog__footer {
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    min-width: 0;
}

.tj-dialog--with-footer .tj-dialog__footer {
    margin-top: 0.9rem;
    padding-top: 0.85rem;
    border-top: 1px solid rgba(var(--brand-ink-rgb), 0.1);
}

.tj-dialog__footer > .swal2-actions {
    margin: 0;
    padding: 0;
    gap: 0.9rem;
    width: 100%;
    justify-content: center;
    flex-wrap: wrap;
}

.tj-dialog__footer > .swal2-footer {
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: rgba(var(--brand-ink-rgb), 0.78);
    font-size: 0.9rem;
}

/* ---- Buttons ---------------------------------------------------------
   BIG RECTANGLES (boss, 23 Nov: "todos los botones de popups ... altos y
   largos, como un rectángulo grande, para un niño de 4 años o una persona
   mayor de 80"). Tall and wide, sharing the row evenly and wrapping to
   their own row when the window is narrow. Only a teacher's small controls
   (?, i, the rename pencil) stay small; they are not dialog actions.
   Colours unchanged; focus stays focus.css's dashed orange. */
.tj-dialog .swal2-styled {
    min-height: 64px;
    min-width: min(220px, 100%);
    flex: 1 1 220px;
    max-width: 360px;
    margin: 0;
    padding: 0.9rem 1.6rem;
    border: 0;
    border-radius: 18px;
    font-family: var(--app-font);
    font-size: 1.2rem;
    font-weight: 700;
    line-height: 1.1;
    box-shadow: none;
    transition: background-color 0.16s ease, transform 0.12s ease;
}

.tj-dialog .swal2-styled.swal2-confirm {
    background-color: var(--accent-blue);
}

.tj-dialog .swal2-styled.swal2-cancel {
    background-color: var(--accent-orange);
}


.tj-dialog .swal2-styled:active {
    transform: scale(0.98);
}

/* ---- Close -----------------------------------------------------------
   One close control for the app: a brand-blue disc with a white ring and a
   drawn X, copied from the page-level back/close controls. It lives in the
   header now, so it stays put while the body scrolls. */
.tj-dialog__close {
    box-sizing: border-box;
    width: var(--tj-close-size);
    height: var(--tj-close-size);
    min-width: var(--tj-close-size);
    min-height: var(--tj-close-size);
    margin: 0;
    padding: 0;
    border: 3px solid var(--brand-white);
    border-radius: 50%;
    background: var(--brand-blue);
    color: transparent;
    font-size: 0;
    line-height: 0;
    overflow: hidden;
    /* Not !important: the engine writes inline display:none on popups that
       never asked for a close button. */
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: var(--tj-dialog-inset);
    right: var(--tj-dialog-inset);
    left: auto;
    inset-inline-start: auto;
    inset-inline-end: var(--tj-dialog-inset);
    transition: background-color 0.16s ease, transform 0.16s ease;
}

.tj-dialog__close::before,
.tj-dialog__close::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 42%;
    height: 3px;
    border-radius: 3px;
    background: var(--brand-white);
}

.tj-dialog__close::before {
    transform: translate(-50%, -50%) rotate(45deg);
}

.tj-dialog__close::after {
    transform: translate(-50%, -50%) rotate(-45deg);
}

/* Hover is pointer-only: iOS pins it to the last-tapped element. */
@media (hover: hover) {
    .tj-dialog__close:hover {
        background: var(--brand-orange);
    }
}

.tj-dialog__close:active {
    background: var(--brand-orange);
    transform: scale(0.96);
}

/* ---- Small phones --------------------------------------------------- */
@media (max-width: 560px) {
    .tj-dialog {
        padding: 1.1em !important;
    }

    .tj-dialog__header > .swal2-title {
        font-size: 1.2em;
    }
}

/* ---- Short viewport (phone landscape) -------------------------------
   Scoped to max-height, not max-width: it is the short axis that hurts. */
@media (orientation: landscape) and (max-height: 500px) {
    .tj-dialog {
        padding: 0.75rem 1rem 1rem !important;
        margin: 0.35rem auto;
        --tj-dialog-inset: 8px;
    }

    .tj-dialog__header > .swal2-title {
        padding-top: 0.1rem;
        margin-bottom: 0.35rem;
        font-size: clamp(1rem, 3.2vw, 1.3rem);
    }

    .tj-dialog__footer > .swal2-actions {
        margin-top: 0.6rem;
    }

    /* Short screens: still big, a little less tall. */
    .tj-dialog .swal2-styled {
        min-height: 52px;
        font-size: 1.05rem;
    }
}

/* ---- Native app (html.tj-native-app, set by tj-dialog.js) -----------
   The page runs under the status bar and over the home indicator, so the
   card stays inside the safe area. In a browser this never applies. */
html.tj-native-app dialog.tj-dialog-host .swal2-container {
    padding-top: max(0.625em, env(safe-area-inset-top));
    padding-bottom: max(0.625em, env(safe-area-inset-bottom));
    padding-left: max(0.625em, env(safe-area-inset-left));
    padding-right: max(0.625em, env(safe-area-inset-right));
}

html.tj-native-app dialog.tj-dialog-host .swal2-popup {
    max-height: min(760px, calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 1.25em)) !important;
}

/* ---- Reduce motion ---------------------------------------------------
   The device setting and the Settings switch (data-motion="reduce") both
   turn the fade and lift off entirely: the popup appears at its final
   place, instantly. */
@media (prefers-reduced-motion: reduce) {
    dialog.tj-dialog-host::backdrop,
    .tj-dialog,
    .tj-dialog__close,
    .tj-dialog .swal2-styled {
        transition: none !important;
    }
}

:root[data-motion="reduce"] dialog.tj-dialog-host::backdrop,
:root[data-motion="reduce"] .tj-dialog,
:root[data-motion="reduce"] .tj-dialog__close,
:root[data-motion="reduce"] .tj-dialog .swal2-styled {
    transition: none !important;
}
