/* ============================================================
   SCR SINGLE-MODULE DIAGRAM
   Markup + script: includes/single-module.php
   Enqueued in functions.php, on the StackCR page template only.

   Over 960px : the stage is the full PNG width, edge to edge with
                the parent wrapper, cropped vertically from the top
                of the model (y 617) to the label rules (y 1565) of
                the 3841 × 2161 PNG. Labels sit below in a grid whose
                columns match the reference's rules. Headings, dots
                and lines scale with it (cqw units); body copy
                inherits the site's body size.
   960px down : dots become toggle buttons and one label at a time
                shows in a panel. Full image width and flow labels
                kept; leader lines hidden.
   640px down : additionally cropped to the module; flow labels hidden.

   The left and right edges of the image fade out (mask on the
   figure), on desktop and mobile.

   Dot positions (--x / --y on each dot, in the HTML) are
   percentages of the PNG itself, so they hold at every size.
   They were measured against single_module_alpharef@2x.png.
   If the PNG is re-rendered, keep the same framing or re-measure.
   ============================================================ */

.single-module {
    --single-module-max: var(--tecs-container-max); /* 1280px — raise for a bigger desktop diagram */
    --single-module-accent: var(--tecs-green-500);
    --single-module-fade: 5%; /* width of the fade at each side of the image */
    /* color: var(--tecs-white);  */
    overflow: hidden;
}

.single-module-container {
    max-width: var(--single-module-max);
    margin-inline: auto;
    container-type: inline-size;
}

/* Stage: PNG width × the band from model top to the label rules */
.single-module-stage {
    position: relative;
    aspect-ratio: 3841 / 948;
}

/* Figure: fills the stage on desktop, is the crop box on mobile.
   The mask fades the image's left and right edges to transparent. */
.single-module-figure {
    position: absolute;
    inset: 0;
    margin: 0;
    -webkit-mask-image: linear-gradient(to right, transparent, #000 var(--single-module-fade), #000 calc(100% - var(--single-module-fade)), transparent);
            mask-image: linear-gradient(to right, transparent, #000 var(--single-module-fade), #000 calc(100% - var(--single-module-fade)), transparent);
}

/* Exactly the PNG's box. Dots are children, so they move with it. */
.single-module-model {
    position: absolute;
    left: 0;
    top: -65.084%; /* -617 / 948 */
    width: 100%;
    aspect-ratio: 3841 / 2161;
}

.single-module-model img {
    display: block;
    pointer-events: none; /* transparent margins overhang other content */
    width: 100%;
    height: 100%;
}

/* Leader lines. Drawn in the PNG's 3841 × 2161 coordinates;
   the viewBox in the PHP crops that to the stage. */
.single-module-lines {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.single-module-lines path {
    fill: none;
    stroke: var(--single-module-accent);
    stroke-width: 2px;
    vector-effect: non-scaling-stroke;
}

/* Dots: the button is the hit area, ::before is the visible square */
.single-module-dot {
    --dot-size: max(12px, 0.9375cqw);
    position: absolute;
    left: var(--x);
    top: var(--y);
    width: var(--dot-size);
    height: var(--dot-size);
    padding: 0;
    border: 0;
    background: none;
    transform: translate(-50%, -50%);
}

.single-module-dot::before {
    content: "";
    position: absolute;
    left: 50%;
    top: 50%;
    width: var(--dot-size);
    height: var(--dot-size);
    background-color: var(--single-module-accent);
    transform: translate(-50%, -50%);
}

/* "Untreated / Treated exhaust", centred on the arrow band (y 1017).
   Dark text: the arrows behind are light whatever the section colour. */
.single-module-flow {
    position: absolute;
    top: 42.194%;
    margin: 0;
    /* font-size: 1.95cqw; */
    line-height: 1.05;
    text-transform: uppercase;
    transform: translateY(-50%);
}

.single-module-flow--in {
    left: 9.242%;
    color: var(--tecs-grey-800);
}

.single-module-flow--out {
    right: 9.685%;
    text-align: right;
    color: var(--tecs-green-900);
}

/* Labels: columns line up with the reference's rules
   (x 355–1043, 1201–1788, 1949–2843, 2982–3469). */
.single-module-labels {
    display: grid;
    grid-template-columns: 9.242% 17.912% 4.114% 15.282% 4.192% 23.275% 3.619% 12.679% 9.685%;
}

/* Column set via a variable, so the class-level mobile rules
   below can override placement without fighting ID specificity */
.single-module-label {
    grid-column: var(--col);
    min-width: 0;
    padding-top: 0.6cqw;
    border-top: 2px solid var(--single-module-accent);
}

#single-module-label-1 { --col: 2; }
#single-module-label-2 { --col: 4; }
#single-module-label-3 { --col: 6; }
#single-module-label-4 { --col: 8; }

.single-module-label .tecs-heading-xs {
    margin-bottom: 0.1em;
    /* font-size: 1.95cqw; */
    text-transform: uppercase;
    color: var(--tecs-grey-950);
}

.single-module-label sub {
    font-size: 0.65em;
    line-height: 0;
}


/* ============================================================
   960px AND BELOW — tappable dots
   ============================================================ */
@media (max-width: 960px) {

    .single-module-container {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: var(--tecs-sp-6);
    }

    /* 960–641px: same full-width stage as desktop (arrows and flow
       labels kept), just without the leader lines */
    .single-module-stage {
        width: 100%;
    }

    .single-module-lines {
        display: none;
    }

    .single-module-dot {
        --dot-size: 14px;
        --hit-size: 32px;
        width: var(--hit-size);
        height: var(--hit-size);
    }

    .single-module-labels {
        display: block;
        width: 100%;
        max-width: 34rem;
    }

    .single-module-label {
        grid-column: auto;
        padding-top: 0;
        border-top: 0;
    }

    .single-module-label + .single-module-label {
        margin-top: var(--tecs-sp-5);
    }

    .single-module-label .tecs-heading-xs {
        margin-bottom: 0.3em;
        font-size: 1.25rem;
    }

    .single-module-label .tecs-body {
        font-size: var(--tecs-fs-body);
        line-height: var(--tecs-lh-body);
    }

    /* --- With JS: one label at a time ---
       Everything shares one grid cell, so the panel is always as
       tall as its longest label and the page doesn't jump. */
    .single-module.is-enhanced .single-module-labels {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        padding-top: var(--tecs-sp-4);
        border-top: 2px solid var(--single-module-accent);
    }

    .single-module.is-enhanced .single-module-label {
        grid-area: 1 / 1;
        margin: 0;
    }

    .single-module.is-enhanced .single-module-label {
        visibility: hidden;
    }

    .single-module.is-enhanced .single-module-label.is-active {
        visibility: visible;
    }

    .single-module.is-enhanced .single-module-dot {
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .single-module.is-enhanced .single-module-dot::before {
        box-shadow: 0 0 0 2px var(--tecs-green-950);
        transition: transform 0.15s ease, background-color 0.15s ease;
    }

    .single-module.is-enhanced .single-module-dot[aria-expanded="true"] {
        z-index: 1;
    }

    .single-module.is-enhanced .single-module-dot[aria-expanded="true"]::before {
        background-color: var(--tecs-white);
        box-shadow: 0 0 0 2px var(--single-module-accent);
        transform: translate(-50%, -50%) scale(1.25);
    }

    .single-module.is-enhanced .single-module-dot:focus-visible {
        outline: 2px solid var(--tecs-white);
        outline-offset: -2px;
    }
}


/* ============================================================
   640px AND BELOW — zoom to the module
   ============================================================ */
@media (max-width: 640px) {

    .single-module-stage {
        aspect-ratio: auto;
    }

    .single-module-flow {
        display: none;
    }

    /* Crop box: the module plus a little of each arrow
       (x 1000–2830, y 617–1402 of the PNG) */
    .single-module-figure {
        position: relative;
        inset: auto;
        width: 100%;
        overflow: hidden;
        aspect-ratio: 1830 / 785;
    }

    /* Same PNG box, re-placed so the module fills the crop box */
    .single-module-model {
        left: -54.645%;
        top: -78.599%;
        width: 209.891%;
    }
}


/* ============================================================
   ANIMATION + PREV / NEXT
   Behaviour: includes/js/diagram-hotspots.js (shared).
   ============================================================ */

.single-module {
    --single-module-nav-hover: var(--tecs-green-700);
}

.single-module-nav {
    display: none;
}

/* Desktop: labels rise in one at a time the first time the diagram
   scrolls into view. --i (label order) is set by the script. */
@media (min-width: 961px) and (prefers-reduced-motion: no-preference) {
    .single-module.is-enhanced .single-module-label {
        transition:
            opacity 0.7s ease,
            transform 0.7s cubic-bezier(0.2, 0.7, 0.3, 1);
        transition-delay: calc(var(--i, 0) * 180ms);
    }

    .single-module.is-enhanced:not(.is-revealed) .single-module-label {
        opacity: 0;
        transform: translateY(1rem);
    }
}

@media (max-width: 960px) {

    /* --- Labels share one grid cell. Only the active label, and
       the one animating out, are visible. --- */
    .single-module.is-enhanced .single-module-labels > .is-active,
    .single-module.is-enhanced .single-module-labels > .is-leaving {
        visibility: visible;
    }

    /* Direction of travel comes from --dx on the root:
       next = 1, previous = -1. */
    .single-module.is-enhanced .single-module-labels > .is-active {
        animation: single-module-in 0.4s cubic-bezier(0.2, 0.7, 0.3, 1) both;
    }

    .single-module.is-enhanced .single-module-labels > .is-leaving {
        pointer-events: none;
        animation: single-module-out 0.25s ease-in both;
    }

    @keyframes single-module-in {
        from {
            opacity: 0;
            transform: translate(calc(var(--dx, 0) * 1.5rem), 0);
        }
    }

    @keyframes single-module-out {
        to {
            opacity: 0;
            transform: translate(calc(var(--dx, 0) * -1.5rem), 0);
        }
    }

    /* --- Prev / next, under the label --- */
    .single-module.is-enhanced .single-module-nav {
        position: relative;
        z-index: 1;
        display: flex;
        grid-row: 2;
        align-items: center;
        gap: var(--tecs-sp-3);
        margin-top: var(--tecs-sp-5);
    }

    .single-module-prev,
    .single-module-next {
        padding: 0;
        border: 0;
        border-radius: 50%;
        background: none;
        color: inherit;
        font-size: 2.5rem; /* icon is 1em */
        line-height: 0;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
        transition: color 0.2s ease;
    }

    .single-module-prev svg,
    .single-module-next svg {
        display: block;
    }

    .single-module-prev svg {
        transform: rotate(180deg);
    }

    .single-module-prev:hover { color: var(--single-module-nav-hover); }
    .single-module-next:hover { color: var(--single-module-nav-hover); }

    .single-module-prev:focus-visible,
    .single-module-next:focus-visible {
        outline: 2px solid currentColor;
        outline-offset: 2px;
    }

    .single-module-count {
        min-width: 3.5em;
        margin: 0;
        text-align: center;
        font-variant-numeric: tabular-nums;
        opacity: 0.7;
    }

    /* --- Dots: a slow pulse behind each one, staggered by --i.
       Stops on the open dot. --- */
    .single-module.is-enhanced .single-module-dot::after {
        content: "";
        position: absolute;
        left: 50%;
        top: 50%;
        z-index: -1;
        width: var(--dot-size);
        height: var(--dot-size);
        background-color: var(--single-module-accent);
        opacity: 0;
        filter: blur(1px);
        transform: translate(-50%, -50%);
        animation: single-module-pulse 2.4s ease-out infinite;
        animation-delay: calc(var(--i, 0) * 0.35s);
    }

    .single-module.is-enhanced .single-module-dot[aria-expanded="true"]::after {
        animation: none;
    }

    @keyframes single-module-pulse {
        0% {
            opacity: 0.6;
            transform: translate(-50%, -50%) scale(1);
        }
        70%,
        100% {
            opacity: 0;
            transform: translate(-50%, -50%) scale(2.6);
        }
    }
}

@media (prefers-reduced-motion: reduce) {
    .single-module.is-enhanced .single-module-dot::after {
        display: none;
    }

    .single-module.is-enhanced .single-module-labels > .is-active,
    .single-module.is-enhanced .single-module-labels > .is-leaving {
        animation: none;
    }

    .single-module.is-enhanced .single-module-labels > .is-leaving {
        visibility: hidden;
    }
}
