/*
 * CFO HUB matrix geometry — authoritative source.
 * Contract: contracts/cfo-matrix-contract.json (20260829.1).
 *
 * Do not redefine grid tracks, widths, overflow or domain-column positioning
 * in page CSS, themes, profiles, sidebar or Viewer. Visual styling belongs in
 * table-layouts.css/table-themes.css; geometry belongs only here.
 */
html[data-cfo-table-layout] {
    --cfo-matrix-label-min: 220px;
    --cfo-matrix-period-min: 108px;
    --cfo-matrix-total-min: 160px;
    --cfo-matrix-control-width: 24px;
    --cfo-matrix-column-gap: 4px;
    --cfo-matrix-surface-min: 1800px;
    --cfo-matrix-inline-padding: 0px;
    --cfo-matrix-scroll-max: min(72dvh, 760px);
    --cfo-matrix-columns:
        minmax(var(--cfo-matrix-label-min), 1.55fr)
        repeat(12, minmax(var(--cfo-matrix-period-min), 1fr))
        minmax(var(--cfo-matrix-total-min), 1.2fr)
        var(--cfo-matrix-control-width);
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-scroll,
    .gesteco-year-table,
    .financial-preview-wrap
) {
    position: relative !important;
    container-name: cfo-matrix;
    container-type: inline-size;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    height: auto !important;
    max-height: var(--cfo-matrix-scroll-max) !important;
    padding-inline: 0 !important;
    overflow: auto !important;
    overscroll-behavior-inline: contain;
    overscroll-behavior-block: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-gutter: stable;
}

/*
 * The financial twelve-month report is part of the document narrative, like
 * the other executive panels. It therefore keeps only the horizontal matrix
 * overflow and lets the page own vertical scrolling. The opt-in modifier
 * avoids changing the compact analytical workspaces that still need a fixed
 * two-axis viewport.
 */
@media screen and (min-width: 781px) {
    html[data-cfo-table-layout] body .cfo-bva-flowing-matrix .pluri-matrix-scroll {
        max-height: none !important;
        overflow-x: auto !important;
        overflow-y: visible !important;
        overscroll-behavior-block: auto;
        scrollbar-gutter: auto;
    }
}

html[data-cfo-table-layout] body .pluri-matrix-cell {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 3px;
    font-variant-numeric: tabular-nums lining-nums;
}

html[data-cfo-table-layout] body .pluri-matrix-cell .pluri-cell-line {
    display: block !important;
    width: 100%;
    min-height: 18px;
    font-size: calc(.78rem * var(--cfo-layout-font-scale)) !important;
    line-height: 1.25;
    white-space: nowrap;
}

/*
 * Financial values use one semantic line per measure. The short key is
 * visually separated from the right-aligned number, so B/C/Δ cannot be read
 * as part of the amount. This rule is shared by HUB, preview and Viewer.
 */
html[data-cfo-table-layout] body :is(
    .pluri-matrix-cell,
    .gesteco-month-cell,
    .financial-preview-month
) {
    gap: 3px !important;
    min-height: 62px;
    font-variant-numeric: tabular-nums lining-nums;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-cell,
    .gesteco-month-cell,
    .financial-preview-month
) > .cfo-metric-line {
    display: grid !important;
    box-sizing: border-box !important;
    grid-template-columns: 22px minmax(0, 1fr) !important;
    align-items: baseline !important;
    gap: 5px !important;
    width: 100% !important;
    min-width: 0 !important;
    min-height: 18px;
    margin: 0 !important;
    padding: 0 !important;
    border-radius: 0 !important;
    line-height: 1.25 !important;
    white-space: nowrap !important;
    text-align: initial !important;
}

/*
 * A key is useful only when two or more measures coexist in the same cell.
 * With one selected measure the amount receives the whole track, without an
 * empty 22px key column, and can use the same visual scale as the row label.
 */
html[data-cfo-table-layout] body .pluri-matrix-cell-single {
    min-height: 44px;
}

html[data-cfo-table-layout] body .pluri-matrix-cell-single > .cfo-metric-line {
    grid-template-columns: minmax(0, 1fr) !important;
    font-size: calc(.84rem * var(--cfo-layout-font-scale)) !important;
}

/* Layout profiles may decorate legacy delta nodes with pseudo-elements or
   padding. Semantic metric lines own their 22px key and must remain immune. */
html[data-cfo-table-layout] body .cfo-metric-line::before {
    display: none !important;
    content: none !important;
}

html[data-cfo-table-layout] body .cfo-metric-line > .cfo-metric-key {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 22px !important;
    min-width: 22px !important;
    height: 16px;
    margin: 0 !important;
    border: 1px solid #d6deea;
    border-radius: 4px;
    background: #f3f6fa;
    color: #52627a !important;
    font-size: .58rem !important;
    font-weight: 800 !important;
    line-height: 1 !important;
    letter-spacing: 0 !important;
    text-align: center !important;
}

html[data-cfo-table-layout] body .cfo-metric-line > .cfo-metric-value {
    display: block !important;
    min-width: 0 !important;
    margin: 0 !important;
    overflow: visible !important;
    color: inherit;
    font-size: inherit !important;
    font-weight: inherit !important;
    line-height: inherit !important;
    letter-spacing: 0 !important;
    text-align: right !important;
    white-space: nowrap !important;
}

html[data-cfo-table-layout] body .cfo-metric-line.is-zero > .cfo-metric-value {
    color: #7b8798 !important;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-annual-cell,
    .gesteco-year-total,
    .financial-preview-total
) {
    padding-left: 10px !important;
    border-left: 2px solid #c9d8ed;
    background: color-mix(in srgb, #eef5ff 58%, transparent);
}

html[data-cfo-table-layout] body .pluri-matrix-scroll .pluri-matrix-grid,
html[data-cfo-table-layout] body .gesteco-year-table :is(.gesteco-list, .gesteco-year-header),
html[data-cfo-table-layout] body .financial-preview-wrap :is(
    .financial-preview-header,
    .financial-preview-list
) {
    box-sizing: border-box !important;
    width: max(100%, var(--cfo-matrix-surface-min)) !important;
    min-width: var(--cfo-matrix-surface-min) !important;
    max-width: none !important;
    padding-inline: 0 !important;
}

html[data-cfo-table-layout][data-cfo-table-theme] body :is(
    .pluri-matrix-card,
    .gesteco-year-card,
    .financial-preview-card
) {
    box-sizing: border-box !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    overflow: visible !important;
    margin-inline: 0 !important;
    padding-inline: 0 !important;
    border-inline: 0 solid transparent !important;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-detail,
    .gesteco-detail,
    .financial-preview-detail
) {
    box-sizing: border-box !important;
    padding-inline: 0 !important;
}

html[data-cfo-table-layout][data-cfo-table-theme] body :is(
    .pluri-matrix-header,
    .pluri-matrix-row,
    .pluri-matrix-detail-row,
    .pluri-matrix-total-row,
    .gesteco-year-header,
    .gesteco-year-card > summary,
    .gesteco-year-static-row,
    .gesteco-year-detail-row,
    .financial-preview-header,
    .financial-preview-card > summary,
    .financial-preview-detail-row
) {
    display: grid !important;
    box-sizing: border-box !important;
    grid-template-columns: var(--cfo-matrix-columns) !important;
    column-gap: var(--cfo-matrix-column-gap) !important;
    row-gap: 0 !important;
    align-items: center !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    height: auto !important;
    padding-inline: var(--cfo-matrix-inline-padding) !important;
}

/*
 * The total row is a peer of headers and data rows. Page-level decoration
 * must not inset it, consume track space or draw a separator around it.
 * Theme background and typography provide the hierarchy without a dark rule.
 */
html[data-cfo-table-layout][data-cfo-table-theme] body .pluri-matrix-total-row {
    margin-inline: 0 !important;
    border: 0 !important;
    outline: 0 !important;
}

html[data-cfo-table-layout][data-cfo-table-theme] body
.gesteco-year-table > .gesteco-year-header,
html[data-cfo-table-layout][data-cfo-table-theme] body
.financial-preview-wrap > .financial-preview-header {
    width: max(100%, var(--cfo-matrix-surface-min)) !important;
    min-width: var(--cfo-matrix-surface-min) !important;
}

/* Compatibility for signed snapshots produced before the canonical summary
   row was flattened. The wrapper must not introduce a second grid or padding. */
html[data-cfo-table-layout][data-cfo-table-theme] body
.pluri-matrix-card > summary:has(> .pluri-matrix-row) {
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    padding: 0 !important;
}

/*
 * Domain columns remain in their canonical grid tracks. The descriptive track
 * is sticky but still consumes its original grid space; the annual total must
 * never become an overlay over November or December.
 */
/* Commercial grids are also embedded in already-published Viewer payloads.
 * Supply their missing neutral surface here so the shared Shadow DOM adapter
 * repairs legacy payload CSS too, without changing snapshots or calculations.
 * Low specificity preserves current/target/area colors and hover states.
 */
:where(.agenti-analysis-row) {
    background-color: var(--bs-body-bg, #fff);
}

:where(.agenti-kpi-row) > :where(span:first-child) {
    background: inherit;
}

/*
 * Single-surface sticky column. The descriptive track remains opaque while
 * horizontal content passes underneath, but it is visually part of its row:
 * no inset card, outline, radius or separator shadow.
 */
html[data-cfo-table-layout][data-cfo-table-theme] body :is(
    .pluri-matrix-header,
    .pluri-matrix-row,
    .pluri-matrix-detail-row,
    .pluri-matrix-total-row,
    .gesteco-year-header,
    .gesteco-year-card > summary,
    .gesteco-year-static-row,
    .gesteco-year-detail-row,
    .financial-preview-header,
    .financial-preview-card > summary,
    .financial-preview-detail-row
) > * {
    box-sizing: border-box;
    min-width: 0;
    position: static !important;
    inset: auto !important;
    transform: none !important;
}

/*
 * Spreadsheet navigation contract. The month header remains visible during
 * vertical scrolling and the descriptive track remains visible during
 * horizontal scrolling. The intersection receives the highest stacking level.
 */
html[data-cfo-table-layout][data-cfo-table-theme] body :is(
    .pluri-matrix-header,
    .gesteco-year-header,
    .financial-preview-header
) {
    position: sticky !important;
    top: 0 !important;
    z-index: 40 !important;
    background: var(--cfo-t-header, #edf2f8) !important;
    box-shadow: 0 1px 0 var(--cfo-t-border, #d9e1ec) !important;
}

html[data-cfo-table-layout][data-cfo-table-theme] body :is(
    .pluri-matrix-header,
    .pluri-matrix-row,
    .pluri-matrix-detail-row,
    .pluri-matrix-total-row,
    .gesteco-year-header,
    .gesteco-year-card > summary,
    .gesteco-year-static-row,
    .gesteco-year-detail-row,
    .financial-preview-header,
    .financial-preview-card > summary,
    .financial-preview-detail-row
) > :first-child {
    position: sticky !important;
    left: 0 !important;
    z-index: 30 !important;
    align-self: stretch !important;
    isolation: isolate;
    padding-inline: 12px !important;
    background-clip: border-box !important;
    background: var(--cfo-row-surface, var(--cfo-t-surface, #fff)) !important;
    border: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    outline: 0 !important;
}

html[data-cfo-table-layout][data-cfo-table-theme] body :is(
    .pluri-matrix-header,
    .gesteco-year-header,
    .financial-preview-header
) > :first-child:not(.cfo-matrix-control) {
    z-index: 50 !important;
    background: var(--cfo-t-header, #edf2f8) !important;
}

/* Subtle column rails keep the key of the next month from being perceived as
   part of the preceding amount. They do not alter the canonical grid. */
html[data-cfo-table-layout][data-cfo-table-theme] body :is(
    .pluri-matrix-header,
    .pluri-matrix-row,
    .pluri-matrix-detail-row,
    .pluri-matrix-total-row,
    .gesteco-year-header,
    .gesteco-year-card > summary,
    .gesteco-year-static-row,
    .gesteco-year-detail-row,
    .financial-preview-header,
    .financial-preview-card > summary,
    .financial-preview-detail-row
) > :not(:first-child):not(:last-child) {
    padding-inline: 6px !important;
    border-left: 1px solid #e3eaf3;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-annual-cell,
    .gesteco-year-total,
    .financial-preview-total
) {
    min-width: 0 !important;
}

html[data-cfo-table-layout][data-cfo-table-theme] body .cfo-matrix-control {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: auto !important;
    min-width: 0 !important;
}

/* Backward-compatible control slot for already signed Viewer snapshots. */
html[data-cfo-table-layout][data-cfo-table-theme] body :is(
    .pluri-matrix-header,
    .pluri-matrix-row,
    .pluri-matrix-detail-row,
    .pluri-matrix-total-row,
    .gesteco-year-header,
    .gesteco-year-card > summary,
    .gesteco-year-static-row,
    .gesteco-year-detail-row,
    .financial-preview-header,
    .financial-preview-card > summary,
    .financial-preview-detail-row
) > :last-child {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

/*
 * Adaptive full-width mode.
 * A twelve-period matrix does not need an 1800px canvas when the available
 * surface can keep every amount readable with slightly tighter tracks. The
 * compact geometry is enabled only when the container is already wide enough;
 * narrower surfaces retain the spreadsheet-style horizontal fallback.
 */
@container cfo-matrix (min-width: 1580px) {
    html[data-cfo-table-layout] body .pluri-matrix-scroll .pluri-matrix-grid,
    html[data-cfo-table-layout] body .gesteco-year-table :is(.gesteco-list, .gesteco-year-header),
    html[data-cfo-table-layout] body .financial-preview-wrap :is(
        .financial-preview-header,
        .financial-preview-list
    ) {
        --cfo-matrix-label-min: 200px;
        --cfo-matrix-period-min: 96px;
        --cfo-matrix-total-min: 132px;
        --cfo-matrix-surface-min: 100%;
    }

    html[data-cfo-table-layout] body :is(
        .pluri-matrix-cell,
        .gesteco-month-cell,
        .financial-preview-month
    ) > .cfo-metric-line {
        grid-template-columns: 18px minmax(0, 1fr) !important;
        gap: 3px !important;
        font-size: calc(.72rem * var(--cfo-layout-font-scale)) !important;
    }

    html[data-cfo-table-layout] body .cfo-metric-line > .cfo-metric-key {
        width: 18px !important;
        min-width: 18px !important;
    }
}

/* Tall pages can present the complete surface in the document flow. Internal
 * vertical scroll remains the fallback only when the viewport is constrained. */
@media screen and (min-height: 900px) {
    html[data-cfo-table-layout] body :is(
        .pluri-matrix-scroll,
        .gesteco-year-table,
        .financial-preview-wrap
    ) {
        max-height: none !important;
        overflow-x: auto !important;
        overflow-y: visible !important;
        scrollbar-gutter: auto;
    }
}

@container cfo-matrix (max-width: 780px) {
    html[data-cfo-table-layout] body .pluri-matrix-scroll .pluri-matrix-grid,
    html[data-cfo-table-layout] body .gesteco-year-table :is(.gesteco-list, .gesteco-year-header),
    html[data-cfo-table-layout] body .financial-preview-wrap :is(
        .financial-preview-header,
        .financial-preview-list
    ) {
        --cfo-matrix-label-min: 200px;
        --cfo-matrix-period-min: 108px;
        --cfo-matrix-total-min: 160px;
        --cfo-matrix-surface-min: 1780px;
        --cfo-matrix-scroll-max: min(68dvh, 680px);
    }
}

@media print {
    html[data-cfo-table-layout] {
        --cfo-matrix-label-min: 118px;
        --cfo-matrix-period-min: 50px;
        --cfo-matrix-total-min: 72px;
        --cfo-matrix-control-width: 0px;
        --cfo-matrix-column-gap: 3px;
        --cfo-matrix-surface-min: 0px;
        --cfo-matrix-inline-padding: 7px;
    }

    html[data-cfo-table-layout] body :is(
        .pluri-matrix-scroll,
        .gesteco-year-table,
        .financial-preview-wrap
    ) {
        max-height: none !important;
        overflow: visible !important;
    }

    html[data-cfo-table-layout][data-cfo-table-theme] body :is(
        .pluri-matrix-header,
        .pluri-matrix-row,
        .pluri-matrix-detail-row,
        .pluri-matrix-total-row,
        .gesteco-year-header,
        .gesteco-year-card > summary,
        .gesteco-year-static-row,
        .gesteco-year-detail-row,
        .financial-preview-header,
        .financial-preview-card > summary,
        .financial-preview-detail-row
    ),
    html[data-cfo-table-layout][data-cfo-table-theme] body :is(
        .pluri-matrix-header,
        .pluri-matrix-row,
        .pluri-matrix-detail-row,
        .pluri-matrix-total-row,
        .gesteco-year-header,
        .gesteco-year-card > summary,
        .gesteco-year-static-row,
        .gesteco-year-detail-row,
        .financial-preview-header,
        .financial-preview-card > summary,
        .financial-preview-detail-row
    ) > :first-child {
        position: static !important;
        inset: auto !important;
        padding-inline: 0 !important;
        box-shadow: none !important;
    }

    html[data-cfo-table-layout] body .pluri-matrix-scroll .pluri-matrix-grid,
    html[data-cfo-table-layout] body .gesteco-year-table :is(.gesteco-list, .gesteco-year-header),
    html[data-cfo-table-layout] body .financial-preview-wrap :is(
        .financial-preview-header,
        .financial-preview-list
    ) {
        width: 100% !important;
        min-width: 0 !important;
    }
}
