/* =====================================================================================================
   Article images and the zoom overlay (board Components › 19; artboards ArticleImages, ImageZoom, MobileImageZoom,
   ArabicImageZoom). Loaded by content/article only when the body has an image.
   Markup: App\Content\Images\FigureRenderer writes the figure; content/partials/image-zoom holds the overlay and the
   failed-image templates; public/js/image-zoom.js opens the overlay and swaps a failed image for its alt text.

   .kb-figure (+ --screenshot | --diagram | --tall | --rtl-capture) · __open (the link: frame, crop, cursor)
   · __badge (+ __badge-text) · __fade · __more (the "See the whole image" pill) · __caption
   · __failed (__failed-body, __failed-icon, __failed-title, __failed-alt, __retry)
   .kb-zoom (the <dialog>) · __bar · __tools · __btn (+ --icon) · __level · __sep · __end · __close (__close-word)
   · __stage · __canvas · __img · __foot · __pill · __caption · __more · __keys · __desk / __phone (which bar)
   No new colours: every value is a help.css token or the board's on-navy control values.
   ===================================================================================================== */

/* ------------------------------------------------------------------------------------- The figure */
.prose .kb-figure { margin-inline: 0; margin-bottom: 0; max-width: 100%; }
.prose li > .kb-figure { margin-top: var(--sp-12); }

.kb-figure__open {
    position: relative; display: block; width: fit-content; max-width: 100%;
    border-radius: var(--r-card); color: inherit; text-decoration: none; cursor: zoom-in;
}
.prose .kb-figure img {
    /* No CSS width: the width and height attributes size it before it loads (no shift), never above real size. */
    display: block; max-width: 100%; height: auto;
    border: 0; border-radius: 0; background: var(--c-subtle); /* behind the image while it loads */
}
/* Screenshots: the image fills a 1px frame edge to edge, r12, shadow 1. */
.kb-figure--screenshot .kb-figure__open {
    overflow: hidden; border: 1px solid var(--c-border); background: var(--c-surface); box-shadow: var(--shadow-1);
}
/* Diagrams (the file name says "diagram") draw their own boxes on white: no frame; r12 is for the focus ring. */
.prose .kb-figure--diagram img { background: none; }

@media (any-hover: hover) {
    .kb-figure--screenshot .kb-figure__open:hover { border-color: var(--c-control); }
    .kb-figure__open:hover .kb-figure__badge { opacity: 1; }
}
.kb-figure__open:focus-visible .kb-figure__badge { opacity: 1; }
/* Back from an overlay opened with the mouse: focus returns without the ring (image-zoom.js, until the next blur). */
.kb-figure__open.is-quiet:focus-visible { outline: none; }
@media (hover: hover) { .kb-figure__open.is-quiet:focus-visible:not(:hover) .kb-figure__badge { opacity: 0; } }

/* "Enlarge": top right of the image, because captures are LTR (also on right-to-left pages); an .ar.png top left. */
.kb-figure__badge {
    position: absolute; top: var(--sp-8); right: var(--sp-8); z-index: 2;
    display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 10px 0 8px;
    background: var(--c-surface); border: 1px solid var(--c-control); border-radius: var(--r-control); box-shadow: var(--shadow-2);
    color: var(--c-primary); font: 500 var(--fs-13)/1 var(--font-body); white-space: nowrap;
    opacity: 0; pointer-events: none;
}
.kb-figure--rtl-capture .kb-figure__badge { right: auto; left: var(--sp-8); }
/* Touch screens have no hover: the badge is always there, icon only. */
@media (hover: none) {
    .kb-figure__badge { opacity: 1; width: 32px; padding: 0; justify-content: center; }
    .kb-figure__badge-text { display: none; }
}

/* Tall (over 900px at the column width): the top 560px, a 120px white fade and the pill, all in the one link. */
.kb-figure--tall .kb-figure__open { max-height: 560px; overflow: hidden; }
.kb-figure__fade {
    position: absolute; inset: auto 0 0 0; height: 120px; pointer-events: none;
    background: linear-gradient(rgb(255 255 255 / 0), #ffffff);
}
.kb-figure__more {
    position: absolute; bottom: var(--sp-16); left: 50%; transform: translateX(-50%); z-index: 2;
    display: inline-flex; align-items: center; gap: var(--sp-8); height: 36px; padding: 0 var(--sp-16);
    background: var(--c-navy); color: #ffffff; border-radius: var(--r-pill); box-shadow: var(--shadow-2);
    font: 500 var(--fs-14)/1 var(--font-body); white-space: nowrap;
}

/* An optional caption, only from a Markdown title. */
.kb-figure__caption { margin-top: var(--sp-8); max-width: var(--measure); font-size: var(--fs-14); line-height: var(--lh-ui); color: var(--c-muted); }

/* Failed: the alt text and Try again in a dashed box; no link, so nothing opens. */
.kb-figure__failed {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--sp-12);
    min-height: 200px; padding: var(--sp-24); text-align: center;
    background: var(--c-subtle); border: 1px dashed var(--c-control); border-radius: var(--r-card);
}
.kb-figure__failed-body { display: flex; flex-direction: column; align-items: center; gap: var(--sp-4); max-width: var(--measure); }
.kb-figure__failed-icon { color: var(--c-muted); margin-bottom: var(--sp-4); }
.prose .kb-figure__failed-title { font-size: var(--fs-15); line-height: var(--lh-ui); font-weight: 500; color: var(--c-strong); }
.prose .kb-figure__failed-alt { font-size: var(--fs-14); line-height: var(--lh-ui); color: var(--c-muted); }
.kb-figure__retry { min-height: 44px; }

/* ------------------------------------------------------------------------------------ The overlay */
/* The page is locked while the overlay is open; scrollbar-gutter keeps its width, so nothing behind moves. The kept
   gutter is outside the viewport the backdrop covers: paint it the backdrop's colour over the page (navy at 92%). */
html.kb-zoom-open { overflow: hidden; scrollbar-gutter: stable; background: #1d3153; }

.kb-zoom {
    position: fixed; inset: 0; width: 100%; height: 100%; max-width: none; max-height: none;
    margin: 0; padding: 0; border: 0; overflow: hidden; background: transparent; color: #ffffff;
    flex-direction: column;
}
.kb-zoom[open] { display: flex; }
.kb-zoom::backdrop { background: rgb(10 31 68 / .92); }
.kb-zoom[open]::backdrop { animation: kb-zoom-fade 160ms ease-out; }
@keyframes kb-zoom-fade { from { opacity: 0; } }

.kb-zoom__bar {
    flex: none; height: 64px; padding: 0 var(--sp-24);
    display: flex; align-items: center; justify-content: space-between; gap: var(--sp-16);
}
.kb-zoom__tools, .kb-zoom__end { display: flex; align-items: center; gap: var(--sp-8); }
.kb-zoom__btn {
    height: 44px; min-width: 44px; padding: 0 var(--sp-12);
    display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-8);
    background: transparent; border: 1px solid var(--c-navy-border); border-radius: var(--r-control);
    color: #ffffff; font: 500 var(--fs-14)/1 var(--font-body); white-space: nowrap; text-decoration: none; cursor: pointer;
}
.kb-zoom__btn--icon { width: 44px; padding: 0; }
.kb-zoom__btn:hover { background: var(--c-navy-field); color: #ffffff; }
.kb-zoom__btn[aria-pressed="true"] { background: #ffffff; border-color: #ffffff; color: var(--c-primary); }
.kb-zoom__btn[aria-disabled="true"] { background: var(--c-navy-divider); border-color: transparent; color: #606d85; cursor: default; }
.kb-zoom__level { min-width: 56px; text-align: center; font: 500 var(--fs-14)/1 var(--font-body); font-variant-numeric: tabular-nums; }
.kb-zoom__sep { width: 1px; height: 24px; margin: 0 var(--sp-4); background: var(--c-navy-divider); }
.kb-zoom__phone { display: none; }

/* The stage: a focused scroll area (LTR always: the screenshot never mirrors). */
.kb-zoom__stage {
    position: relative; flex: 1 1 auto; min-height: 0; overflow: auto; overscroll-behavior: contain;
    border-radius: var(--r-card); outline: none;
    scrollbar-width: thin; scrollbar-color: var(--c-navy-border) transparent;
}
.kb-zoom__stage:focus-visible { outline: 2px solid #ffffff; outline-offset: -6px; }
.kb-zoom__stage::-webkit-scrollbar { width: 6px; height: 6px; }
.kb-zoom__stage::-webkit-scrollbar-thumb { background: var(--c-navy-border); border-radius: var(--r-pill); }
.kb-zoom__canvas { display: grid; box-sizing: border-box; width: max-content; min-width: 100%; min-height: 100%; padding: var(--sp-24); }
.kb-zoom__img {
    display: block; margin: auto; max-width: none; border-radius: var(--r-control); box-shadow: var(--shadow-3);
    background: #ffffff; /* behind transparent PNGs */
    touch-action: none; user-select: none; -webkit-user-select: none; -webkit-user-drag: none;
}
.kb-zoom.is-pannable .kb-zoom__img { cursor: grab; }
.kb-zoom.is-dragging .kb-zoom__img { cursor: grabbing; }

.kb-zoom__foot {
    flex: none; padding: var(--sp-12) var(--sp-24) var(--sp-20);
    display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center;
}
.kb-zoom__caption { margin: 0; max-width: 90ch; font-size: var(--fs-15); line-height: 1.6; color: #ffffff; }
.kb-zoom__keys { margin: 0; font-size: var(--fs-13); line-height: var(--lh-ui); color: var(--c-on-navy); }
.kb-zoom__keys kbd {
    font: 12px/1.5 var(--font-mono); color: #ffffff; background: none;
    border: 1px solid var(--c-navy-border); border-radius: var(--r-code); padding: 0 6px;
}
@media not (pointer: fine) { .kb-zoom__keys { display: none; } }
.kb-zoom__pill {
    display: inline-flex; align-items: center; gap: var(--sp-8); height: 36px; margin: 0 0 var(--sp-8); padding: 0 var(--sp-16);
    background: var(--c-navy-field); border: 1px solid var(--c-navy-border); border-radius: var(--r-pill);
    font: 500 var(--fs-13)/1 var(--font-body); color: #ffffff;
}
.kb-zoom__pill[hidden] { display: none; }
.kb-zoom__more {
    min-height: 44px; padding: 0; background: none; border: 0; color: #ffffff;
    font: 500 var(--fs-14)/1 var(--font-body); text-decoration: underline; text-underline-offset: 3px; cursor: pointer;
}
.kb-zoom__more[hidden] { display: none; }

/* Phones: a 56px bar with the Fit / Actual size toggle, the level and an icon-only Close; zoom by gesture;
   the caption at two lines, then More. */
@media (max-width: 640px) {
    .kb-zoom__bar { height: 56px; padding: 0 var(--sp-16); }
    .kb-zoom__desk { display: none; }
    .kb-zoom__phone { display: inline-flex; }
    .kb-zoom__close { width: 44px; padding: 0; }
    .kb-zoom__close-word { display: none; }
    .kb-zoom__canvas { padding: var(--sp-16); }
    .kb-zoom__foot {
        align-items: stretch; text-align: start; gap: 0;
        padding: var(--sp-12) max(var(--sp-16), env(safe-area-inset-right)) max(var(--sp-12), env(safe-area-inset-bottom)) max(var(--sp-16), env(safe-area-inset-left));
    }
    .kb-zoom__pill { align-self: center; }
    .kb-zoom__caption { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; }
    .kb-zoom.is-described .kb-zoom__caption { display: block; max-height: 45vh; overflow-y: auto; -webkit-line-clamp: none; line-clamp: none; }
    .kb-zoom__more { align-self: flex-start; }
}

@media (prefers-reduced-motion: reduce) {
    .kb-zoom[open]::backdrop { animation: none; }
}

/* ---------------------------------------------------------------------------------------- Print */
/* No overlay, badge, crop or fade: each image at the column width (a tall one fitted to one page), whole on its
   page, in a thin rule, with its caption if it has one. A failed image prints its alt text. */
@media print {
    .kb-zoom, .kb-figure__badge, .kb-figure__fade, .kb-figure__more, .kb-figure__retry { display: none !important; }
    .kb-figure { break-inside: avoid; page-break-inside: avoid; }
    .kb-figure__open, .kb-figure--tall .kb-figure__open, .kb-figure--screenshot .kb-figure__open {
        max-height: none; overflow: visible; border: 1px solid #dbdfea; border-radius: 0; box-shadow: none; background: none;
    }
    .prose .kb-figure img { max-width: 100%; max-height: 230mm; width: auto; height: auto; background: none; }
    .kb-figure__caption { max-width: none; font-size: 9.5pt; color: #364a63; }
    .kb-figure__failed { min-height: 0; padding: 8pt; background: none; break-inside: avoid; }
    .kb-figure__failed-icon { display: none; }
}
