/* ============================================================================
 * Tooltip — the explain-bubble. ONE definition, product-wide.
 *
 * ⚠️ **THE COMPONENT IS FOUR FILES.** If you are fixing a tooltip, this is
 * the whole list — you should not have to go hunting for a fifth:
 *
 *   frontend/server_web/templates/components/info_tooltip.html
 *       The macro: markup and accessible name. The ONLY way to make a
 *       tooltip; ``test_tooltip_component.py`` fails on a hand-rolled one.
 *   frontend/server_web/static/css/design-system/tooltip.css
 *       This file. Everything visual.
 *   frontend/server_web/static/js/tooltip-escape.js
 *       Lifts the bubble out of a scrolling ancestor. Load-bearing — see
 *       below — and registered in base.html beside the other feature scripts.
 *   tests/unit/components/test_tooltip_component.py
 *       One guard for all of the above.
 *
 * ⚠️ **Why a CSS-only tooltip is not possible here.** Measured 2026-10-26: a
 * bubble inside a wide table is clipped by ``div.relative.overflow-x-auto``,
 * the horizontal scroller the table needs. A scroll container clips by
 * definition — that is the point of it — so no amount of ``overflow`` tidying
 * upstream can fix this. ``position: fixed`` is the only escape available
 * today, and only script can place a fixed box under its host. (CSS anchor
 * positioning would do it declaratively; it is Chrome-only.) Deleting the JS
 * and relying on the cascade has been tried and does not work.
 *
 * ⚠️ **A tooltip host must never create a stacking context.** The bubble is
 * an ``::after`` on the host, so ``z-index: 100`` ranks it only INSIDE the
 * host's own context. Give the host ``opacity < 1``, a ``transform``, a
 * ``filter`` or ``isolation: isolate`` and the bubble is sealed in, painting
 * at the host's place in document order — under whatever follows. This is not
 * theoretical: ``.info-help-icon`` carried ``opacity: 0.7`` to dim the glyph,
 * and it cut the gateway Radio bubble off mid-sentence. Dim with a lighter
 * COLOUR token instead. The guard enforces it for every host class.
 *
 * Moved out of ``compliance/`` on 2026-10-26: it was never compliance-only,
 * and living there is why it read as a framework helper rather than the
 * product-wide component it is. The matrix helpers that shared the file went
 * to ``compliance/matrix_help.css``.
 * ==========================================================================*/

/* ════════════════════════════════════════════════════════════════════
 * Reusable tooltip + info-icon helper (compliance-wide).
 *
 * Promoted from data/compliance/cisa/static/cisa.css so any compliance
 * surface — MITRE rail, AWIA narrative, future frameworks, generic
 * matrix headers — can drop a tooltip with no per-framework CSS.
 *
 * USAGE
 * -----
 * 1. Put ``data-tooltip="explainer text"`` on any element you want
 *    a bubble for.  Pair with ``aria-label="…"`` so screen readers
 *    stay in step with the visual.
 *
 *      <span class="fw-csf-chip"
 *            data-tooltip="Detect — surface security events…"
 *            aria-label="Detect — surface security events…">
 *        CSF · DE
 *        <span class="info-help-icon" aria-hidden="true">
 *          {{ ic.icon("info-circle", "w-3 h-3") }}
 *        </span>
 *      </span>
 *
 * 2. The element doing data-tooltip should be ``display: inline-flex``
 *    or similar non-table-cell so ``position: relative`` actually
 *    forms a containing block (``<th>`` cells DON'T — anchor on the
 *    inner span instead).
 *
 * 3. The info-circle icon is optional decoration via
 *    ``.info-help-icon`` — dimmed grey at rest, brand-green on
 *    hover.  Skip it if the host element already cues the tooltip
 *    (e.g. a column header reads as clickable on its own).
 *
 * POSITIONING NOTE
 * ----------------
 * Bubble renders BELOW the host (``top: calc(100% + 8px)``) on
 * purpose: many compliance surfaces nest the host inside an
 * ``overflow: hidden`` wrapper (the matrix table, for example),
 * which clips bubbles popping above.  Below pops into the parent's
 * body area where there's vertical room.  Override per-callsite
 * with ``data-tooltip-pos="above"`` if a specific surface wants the
 * other direction (rule below).
 *
 * z-index: 100 — sits above row content, badges, grade chips.
 * ════════════════════════════════════════════════════════════════════ */

[data-tooltip] {
    position: relative;
}
[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    top: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    /* Wide enough for two lines of compliance copy. */
    max-width: 280px;
    width: max-content;
    padding: 7px 10px;
    background: var(--fg-1);
    color: var(--fg-on-dark);
    font-family: ui-sans-serif, system-ui, sans-serif;
    font-size: 0.6875rem;
    font-weight: 500;
    line-height: 1.4;
    white-space: normal;
    text-align: left;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.18);
    /* Hidden until hover/focus, and OUT OF LAYOUT until then.
     *
     * ⚠️ ``display: none`` at rest, not just ``opacity: 0`` /
     * ``visibility: hidden``. An invisible absolute box still counts toward
     * its scroll container's scrollable area, so a 280px bubble centred on a
     * right-hand column header hung ~75px past the table and gave the AWIA
     * Tenants matrix a horizontal scrollbar onto nothing (2026-10-31; with
     * the pseudo-elements removed, scrollWidth fell from 1174 to exactly the
     * 1097 the table needs). Any tooltip near the edge of any scroller did
     * the same. ``@starting-style`` below keeps the fade-in. */
    display: none;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition:
        opacity 0.12s ease-out,
        visibility 0.12s ease-out;
    z-index: 100;
}
[data-tooltip]::before {
    /* Caret pointing up at the host. */
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-bottom-color: var(--fg-1);
    display: none;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition:
        opacity 0.12s ease-out,
        visibility 0.12s ease-out;
    z-index: 100;
}
[data-tooltip]:hover::after,
[data-tooltip]:focus-visible::after,
[data-tooltip]:hover::before,
[data-tooltip]:focus-visible::before {
    display: block;
    opacity: 1;
    visibility: visible;
}
/* The fade-in, from the out-of-layout rest state above. A browser without
   ``@starting-style`` shows the bubble without the fade. */
@starting-style {
    [data-tooltip]:hover::after,
    [data-tooltip]:focus-visible::after,
    [data-tooltip]:hover::before,
    [data-tooltip]:focus-visible::before {
        opacity: 0;
    }
}

/* ─────────────────────────────────────────────────────────────────────
   ESCAPE HATCH for a host inside a clipping ancestor.
 *
 * ⚠️ **Every `<table>` in this product clips.** `section-improvements.css`
 * declares `.patitur-table, table { overflow: hidden !important }`, so an
 * absolutely-positioned bubble inside any table is cut off at the table's
 * edge no matter what its z-index says. Measured 2026-10-26 on the gateway
 * Radio tab: the "Noise floor" bubble had FOUR clipping ancestors (the
 * table, its scroll wrapper, the panel, and `.dd-stage-main`) and ZERO
 * stacking contexts — it was never a z-index problem.
 *
 * `position: fixed` escapes overflow clipping; it is only trapped by an
 * ancestor with `transform`, `filter` or `contain: paint`, none of which
 * appear on these chains. The coordinates cannot come from CSS — a fixed box
 * has no relationship to its host — so `tooltip-escape.js` measures the host
 * and writes them here.
 *
 * ⚠️ Applied by that script ONLY when it finds a clipping ancestor, so every
 * surface where the plain absolute bubble already works is left exactly as it
 * was. The class is never written by hand.
 * ───────────────────────────────────────────────────────────────────── */
[data-tooltip].tt-escapes-clipping::after {
    position: fixed;
    top: var(--tt-y);
    left: var(--tt-x);
    transform: translateX(-50%);
    /* Never wider than the window less its two 8px edges, so on a screen
       narrower than the bubble (under 296px) it wraps instead of running
       off; ``tooltip-escape.js`` centres it on the window there. */
    max-width: min(280px, calc(100vw - 16px));
}
/* The caret is dropped rather than re-aimed: it points at a host the fixed
   bubble is no longer anchored to, and a caret aimed at nothing reads worse
   than none.
   ⚠️ This ties on specificity with the hover rule's ``display: block`` and
   wins by coming LATER. Keep it below that rule. */
[data-tooltip].tt-escapes-clipping::before {
    display: none;
}

/* Optional opt-in: pop above instead of below.  Use on hosts inside
 * containers WITHOUT ``overflow: hidden`` clipping (e.g. a stand-
 * alone widget). */
[data-tooltip][data-tooltip-pos="above"]::after {
    top: auto;
    bottom: calc(100% + 8px);
}
[data-tooltip][data-tooltip-pos="above"]::before {
    top: auto;
    bottom: 100%;
    border-bottom-color: transparent;
    border-top-color: var(--fg-1);
}

/* Info-circle glyph paired with [data-tooltip]. Sits dimmed at rest and goes
 * brand-green on hover/focus.  Two shapes use it: a decorative child of a
 * [data-tooltip] ancestor (aria-hidden, the explainer on the ancestor's
 * aria-label), and the glyph that IS the tooltip host, which is what
 * ``components/info_tooltip.html`` renders.
 *
 * ⚠️ **Never dim this with ``opacity``.**  It read ``opacity: 0.7`` until
 * 2026-10-26.  That was harmless while the glyph was only ever a child of the
 * host, and became a bug the moment the shared macro put ``data-tooltip`` on
 * the glyph itself: an element with ``opacity < 1`` creates a stacking
 * context, so the bubble's ``z-index: 100`` ranked only among this icon's own
 * descendants.  The icon paints at its place in document order, which put the
 * bubble UNDER the next card on the page — and no z-index on the bubble could
 * lift it out, because the trap is the host, not the bubble.  (It also drew
 * the bubble itself at 70% alpha, since opacity applies to the whole subtree.)
 * Dim with a lighter colour token instead: colour creates no stacking context,
 * so the glyph stays in its parent's. */
.info-help-icon {
    display: inline-flex;
    align-items: center;
    margin-left: 0.25rem;
    color: var(--fg-5);
    transition: color 0.12s;
}
/* Both shapes again.  The descendant combinator alone misses the second one —
 * ``[data-tooltip]:hover .info-help-icon`` cannot match the host itself, so a
 * macro-rendered icon never lit up on its own hover. */
[data-tooltip]:hover .info-help-icon,
[data-tooltip]:focus-visible .info-help-icon,
.info-help-icon[data-tooltip]:hover,
.info-help-icon[data-tooltip]:focus-visible {
    color: var(--color-brand-green);
}
