/*
 * Lightbox for the drawings (assets/lightbox.js; markup: templates/_lightbox.html.twig). Loaded on the pages that have
 * diagrams only. Two parts: the «Увеличить» button in the corner of a figure, and the full-screen overlay the script
 * clones from <template id="lb-template">. Colours come from the engine tokens, so the dark scheme follows by itself;
 * the overlay's own backdrop is a neutral dark in both schemes, the drawing lies on a sheet in the page's surface colour
 * (the label halos of .diagram text use --c-surface, so the sheet has to be that colour).
 * Without JS nothing here shows: the button stays [hidden] and the figures are as before.
 */

/* ---------- The figure and its button ---------- */
/* The button sits in the row under the drawing (the caption row, or a row of its own), never over it: a drawing has
   labels up to its very corner. */
[data-lightbox]:focus:not(:focus-visible) { outline: none; }
.lb-js [data-lightbox] { cursor: zoom-in; }
.lb-js [data-lightbox] figcaption { cursor: auto; }
.lb-js [data-lightbox] a { cursor: pointer; }
.lb-row { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); }
.lb-row:not(figcaption) { justify-content: flex-end; }
.lb-actions { display: inline-flex; align-items: center; gap: var(--s-3); }

.lb-open {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid var(--c-border-strong);
    border-radius: var(--radius);
    background: var(--c-surface);
    color: var(--c-text);
    box-shadow: var(--shadow);
    cursor: zoom-in;
    /* A figure that scrolls sideways (phones) keeps the button in view: lightbox.js sets --lb-shift. */
    transform: translateX(var(--lb-shift, 0px));
}
.lb-open[hidden] { display: none; }
.lb-open:hover { border-color: var(--c-accent); background: var(--c-accent-soft); color: var(--c-link); filter: none; }
.lb-open:focus-visible { outline: 2px solid var(--c-focus); outline-offset: 2px; }
.lb-open .icon { width: 22px; height: 22px; }
[data-lightbox][aria-busy="true"] .lb-open { cursor: progress; opacity: .6; }

/* ---------- The overlay ---------- */
html.lb-locked, html.lb-locked body { overflow: hidden; }

.lb {
    --lb-backdrop: #1a1f23;
    --lb-fg: #f2f4f6;
    --lb-fg-muted: #b6bfc8;
    --lb-line: rgba(255, 255, 255, .28);
    --lb-hover: rgba(255, 255, 255, .14);
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    background: var(--lb-backdrop);
    color: var(--lb-fg);
    overscroll-behavior: contain;
}
.lb[hidden] { display: none; }

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .lb { --lb-backdrop: #0b0e10; }
}
@media not print {
    :root[data-theme="dark"] .lb { --lb-backdrop: #0b0e10; }
}

.lb-bar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    grid-template-areas: "caption tools close";
    align-items: center;
    gap: var(--s-2) var(--s-3);
    padding: var(--s-2) var(--s-3);
    padding-left: max(var(--s-3), env(safe-area-inset-left));
    padding-right: max(var(--s-3), env(safe-area-inset-right));
    border-bottom: 1px solid var(--lb-line);
}
.lb-caption { grid-area: caption; min-width: 0; }
.lb-title { margin: 0; font-family: var(--font); font-size: var(--fs-base); font-weight: 600; line-height: 1.3; color: var(--lb-fg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-hint { margin: 2px 0 0; font-size: var(--fs-xs); color: var(--lb-fg-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-hint__coarse { display: none; }
@media (pointer: coarse) {
    .lb-hint__fine { display: none; }
    .lb-hint__coarse { display: inline; }
}
.lb-tools { grid-area: tools; display: flex; align-items: center; gap: var(--s-1); }
.lb-close { grid-area: close; }

.lb-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    padding: 0 var(--s-2);
    border: 1px solid var(--lb-line);
    border-radius: var(--radius);
    background: transparent;
    color: var(--lb-fg);
    font-size: var(--fs-sm);
    font-weight: 600;
    cursor: pointer;
}
.lb-btn:hover { border-color: var(--lb-fg); background: var(--lb-hover); filter: none; }
.lb-btn:focus-visible, .lb-stage:focus-visible { outline: 2px solid var(--lb-fg); outline-offset: 2px; }
.lb-btn[aria-disabled="true"] { opacity: .4; cursor: default; }
.lb-btn[aria-disabled="true"]:hover { border-color: var(--lb-line); background: transparent; }
.lb-btn--text { padding: 0 var(--s-3); }
.lb-level { min-width: 3.4em; text-align: center; font-size: var(--fs-sm); font-variant-numeric: tabular-nums; color: var(--lb-fg-muted); }

/* The stage takes the rest of the screen; the pane inside it is moved by lightbox.js (translate) and the drawing is
   resized, not scaled by a transform, so every zoom level is re-rendered from the vector and stays sharp. */
.lb-stage { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; touch-action: none; cursor: grab; outline-offset: -4px; }
.lb-stage.is-dragging { cursor: grabbing; user-select: none; }
.lb-pane { position: absolute; top: 0; left: 0; transform-origin: 0 0; }
.lb-pane svg.diagram {
    display: block;
    min-width: 0;
    max-width: none;
    margin: 0;
    border-radius: var(--radius-sm);
    background: var(--c-surface);
    box-shadow: 0 2px 18px rgba(0, 0, 0, .45);
}

/* Phones: the bar is two rows (the name and ×, then the tools) and the overlay is the whole screen already. */
@media (max-width: 599px) {
    .lb-bar { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "caption close" "tools tools"; padding-block: var(--s-2); }
    .lb-tools { justify-content: space-between; }
    .lb-title { font-size: var(--fs-sm); white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
    .lb-hint { white-space: normal; }
    .lb-level { flex: 1 1 auto; }
}
@media (max-height: 420px) {
    .lb-hint { display: none; }
}

@media (prefers-reduced-motion: no-preference) {
    .lb:not([hidden]) { animation: lb-in .12s ease-out; }
    @keyframes lb-in { from { opacity: 0; } to { opacity: 1; } }
}

/* ---------- Print: none of it ---------- */
@media print {
    .lb, .lb-open, #lb-template { display: none !important; }
    html.lb-locked, html.lb-locked body { overflow: visible; }
}
