/*
 * CFO HUB structural table layouts.
 * Palette remains owned by table-themes.css; this file owns density, hierarchy,
 * table geometry and the authoritative alignment contract shared with Viewer.
 */
html[data-cfo-table-layout] {
    --cfo-layout-row-gap: 6px;
    --cfo-layout-radius: 7px;
    --cfo-layout-border-width: 1px;
    --cfo-layout-header-transform: uppercase;
    --cfo-layout-header-letter: .025em;
    --cfo-layout-shadow: var(--cfo-t-shadow);
    --cfo-layout-row-py: 10px;
    --cfo-layout-row-px: 12px;
    --cfo-layout-summary-min: 54px;
    --cfo-layout-detail-min: 30px;
    --cfo-layout-font-scale: 1;
}

html[data-cfo-table-density="compact"] {
    --cfo-layout-row-gap: 2px;
    --cfo-layout-row-py: 6px;
    --cfo-layout-row-px: 9px;
    --cfo-layout-summary-min: 42px;
    --cfo-layout-detail-min: 24px;
}

html[data-cfo-table-density="comfortable"] {
    --cfo-layout-row-gap: 10px;
    --cfo-layout-row-py: 14px;
    --cfo-layout-row-px: 16px;
    --cfo-layout-summary-min: 66px;
    --cfo-layout-detail-min: 36px;
}

html[data-cfo-table-text="large"] {
    --cfo-layout-font-scale: 1.11;
}

html[data-cfo-table-layout] body :is(
    .table, .table-kpi, .pluri-matrix-scroll, .gesteco-year-table,
    .matrix-table-scroll, .financial-preview-scroll, .financial-preview-wrap,
    .ce-table, .ce-table-wrap, .theme-sample, .viewer-dto-table
) {
    font-size: calc(.84rem * var(--cfo-layout-font-scale));
    font-variant-numeric: tabular-nums lining-nums;
}

html[data-cfo-table-layout] body .table > :not(caption) > * > *,
html[data-cfo-table-layout] body .table-kpi > :not(caption) > * > * {
    padding: var(--cfo-layout-row-py) var(--cfo-layout-row-px) !important;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-grid, .gesteco-list, .macro-list, .financial-preview-list,
    .theme-sample-body
) {
    gap: var(--cfo-layout-row-gap) !important;
}

/*
 * Standard Budget V Actual: the financial annual view is the canonical
 * surface for every comparable monthly/annual matrix.
 */
html[data-cfo-table-layout] body .cfo-bva-standard :is(
    .financial-preview-wrap,
    .gesteco-year-table,
    .pluri-matrix-scroll
) {
    padding: 0 0 16px !important;
    background: var(--cfo-t-surface-alt, #f8fafc) !important;
}

html[data-cfo-table-layout] body .cfo-bva-standard :is(
    .financial-preview-header,
    .gesteco-year-header,
    .pluri-matrix-header
) {
    border: 1px solid var(--cfo-t-border, #dde3ee) !important;
    border-radius: 8px 8px 0 0 !important;
}

html[data-cfo-table-layout] body .cfo-bva-standard :is(
    .financial-preview-list,
    .gesteco-list
) {
    padding: 10px 0 0 !important;
    background: transparent !important;
}

/* The pluriennale header is the first child of its grid. Top padding on that
   grid would create a window where scrolled values remain visible above the
   sticky month row. Spacing after the header is provided by the grid gap. */
html[data-cfo-table-layout] body .cfo-bva-standard .pluri-matrix-grid {
    padding: 0 !important;
    background: transparent !important;
}

html[data-cfo-table-layout] body .cfo-bva-standard .cfo-measure-check,
html[data-cfo-table-layout] body .cfo-bva-standard.cfo-bva-toolbar .cfo-measure-check {
    min-height: 30px !important;
    padding: 5px 10px !important;
    border: 1px solid #c7d7f2 !important;
    border-radius: 999px !important;
    background: #fff !important;
    color: #122044 !important;
    font-size: .76rem !important;
    font-weight: 850 !important;
}

html[data-cfo-table-layout] body .cfo-bva-standard .cfo-measure-check:has(input:checked),
html[data-cfo-table-layout] body .cfo-bva-standard.cfo-bva-toolbar .cfo-measure-check:has(input:checked) {
    border-color: #2459ad !important;
    background: #eef5ff !important;
    color: #16479a !important;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-card, .gesteco-card, .gesteco-year-card, .macro-block,
    .financial-preview-card, .ce-card, .theme-sample-row
) {
    border-width: var(--cfo-layout-border-width) !important;
    border-radius: var(--cfo-layout-radius) !important;
    box-shadow: var(--cfo-layout-shadow) !important;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-card, .gesteco-card, .gesteco-year-card, .macro-block,
    .financial-preview-card, .ce-card
) > summary,
html[data-cfo-table-layout] body .theme-sample-row {
    min-height: var(--cfo-layout-summary-min) !important;
    padding: var(--cfo-layout-row-py) var(--cfo-layout-row-px) !important;
}

/*
 * Authoritative financial alignment contract.
 * The descriptive field is left aligned; every period, budget, actual,
 * variance and total column is centered below its own heading.
 */
html[data-cfo-table-layout] body :is(.table, .table-kpi, .viewer-dto-table) :is(th, td):not(:first-child),
html[data-cfo-table-layout] body :is(
    .pluri-matrix-header, .pluri-matrix-card > summary, .pluri-matrix-detail-row,
    .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,
    .ce-table-head, .ce-row, .ce-card > summary, .ce-detail-row,
    .theme-sample-header, .theme-sample-row
) > :not(:first-child) {
    justify-content: center !important;
    align-items: center !important;
    text-align: center !important;
}

/*
 * Bilanci & KPI is a conventional historical data table, not a multi-line
 * planning matrix. Numeric values stay right aligned for rapid comparison.
 * This exception must follow the shared centering contract above.
 */
html[data-cfo-table-layout] body .table-kpi tbody td.metric-value {
    justify-content: flex-end !important;
    text-align: right !important;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-cell, .gesteco-month-cell, .financial-preview-month,
    .macro-value, .ce-value
),
html[data-cfo-table-layout] body :is(
    .pluri-matrix-cell, .gesteco-month-cell, .financial-preview-month,
    .macro-value, .ce-value
) :is(span, b, strong, small, .pluri-cell-line, .gesteco-cell-line),
html[data-cfo-table-layout] body .theme-sample :is(.theme-sample-header, .theme-sample-row) > span:not(:first-child) {
    justify-content: center !important;
    align-items: center !important;
    text-align: center !important;
}

html[data-cfo-table-layout] body :is(.table, .table-kpi, .viewer-dto-table) :is(th, td):first-child,
html[data-cfo-table-layout] body :is(
    .pluri-matrix-header, .pluri-matrix-card > summary, .pluri-matrix-detail-row,
    .pluri-matrix-total-row, .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,
    .ce-table-head, .ce-row, .ce-card > summary, .ce-detail-row,
    .theme-sample-header, .theme-sample-row
) > :first-child {
    justify-content: flex-start !important;
    text-align: left !important;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-header, .gesteco-year-header, .financial-preview-header,
    .ce-table-head, .theme-sample-header
) {
    letter-spacing: var(--cfo-layout-header-letter) !important;
    text-transform: var(--cfo-layout-header-transform) !important;
}

/*
 * Single-surface row contract.
 * Frozen descriptive columns remain opaque while periods scroll underneath,
 * but they must not look like a second card inside the table row.
 */
html[data-cfo-table-layout] body :is(
    .pluri-matrix-title, .pluri-matrix-total-title, .gesteco-name,
    .macro-title, .financial-preview-title, .financial-preview-row-label,
    .ce-title
) {
    border-radius: 0 !important;
    box-shadow: none !important;
    outline: 0 !important;
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-header, .pluri-matrix-row, .pluri-matrix-detail-row,
    .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, .ce-table-head, .ce-row, .ce-detail-row
) > :first-child,
html[data-cfo-table-layout] body .cfo-sticky-table > table > :is(thead, tbody, tfoot) > tr > :first-child {
    box-shadow: none !important;
}

/* Conventional HTML tables share the same spreadsheet navigation contract as
   the planning matrices. The class is applied centrally and in the Viewer
   mirror; short tables keep their natural height. */
@media screen {
    html[data-cfo-table-layout] body .cfo-sticky-table {
        position: relative !important;
        max-width: 100% !important;
        max-height: min(72dvh, 760px) !important;
        padding-inline: 0 !important;
        overflow: auto !important;
        overscroll-behavior-inline: contain;
        overscroll-behavior-block: auto;
        -webkit-overflow-scrolling: touch;
    }

    html[data-cfo-table-layout] body .cfo-sticky-table > table {
        border-collapse: separate !important;
        border-spacing: 0 !important;
    }

    html[data-cfo-table-layout] body .cfo-sticky-table > table > thead > tr > th {
        position: sticky !important;
        top: 0 !important;
        z-index: 40 !important;
        background: var(--cfo-t-header, var(--bs-table-bg, #edf2f8)) !important;
    }

    html[data-cfo-table-layout] body .cfo-sticky-table > table > :is(thead, tbody, tfoot) > tr > :first-child {
        position: sticky !important;
        left: 0 !important;
        z-index: 30 !important;
        background: var(--cfo-t-surface, var(--bs-table-bg, #fff)) !important;
    }

    html[data-cfo-table-layout][data-cfo-table-theme] body .cfo-sticky-table > table > tbody > tr:nth-child(even) > :first-child {
        background: var(--cfo-t-zebra, var(--cfo-t-surface, var(--bs-table-bg, #fff))) !important;
    }

    html[data-cfo-table-layout][data-cfo-table-theme] body .cfo-sticky-table > table > :is(tbody, tfoot) > tr:is(.total, .totale, .subtotal, .subtotale, .metric-margin, .metric-profit) > :first-child,
    html[data-cfo-table-layout][data-cfo-table-theme] body .cfo-sticky-table > table > tfoot > tr > :first-child {
        background: var(--cfo-t-balance-soft, var(--cfo-t-surface, var(--bs-table-bg, #fff))) !important;
    }

    /* The sticky first cell paints its own surface above the row. Keep it in
       the same hover/focus state as every other cell in the row. */
    html[data-cfo-table-layout][data-cfo-table-theme] body .cfo-sticky-table > table > tbody > tr:is(:hover, :focus-within) > :first-child {
        background: var(--cfo-t-hover, var(--cfo-t-surface, var(--bs-table-bg, #fff))) !important;
    }

    html[data-cfo-table-layout] body .cfo-sticky-table > table > thead > tr > :first-child {
        z-index: 50 !important;
        background: var(--cfo-t-header, #edf2f8) !important;
    }
}

@media screen and (max-width: 780px) {
    html[data-cfo-table-layout] body .cfo-sticky-table {
        max-height: min(68dvh, 680px) !important;
    }
}

/* When the viewport is tall enough, let the page own vertical scrolling and
   show the complete table instead of creating a second scrollbar inside it. */
@media screen and (min-height: 900px) {
    html[data-cfo-table-layout] body .cfo-sticky-table {
        max-height: none !important;
        overflow-x: auto !important;
        overflow-y: visible !important;
        scrollbar-gutter: auto;
    }
}

html[data-cfo-table-layout] body :is(
    .pluri-matrix-card.balance, .pluri-matrix-card.neutral,
    .gesteco-year-card.total, .financial-preview-card.balance,
    .ce-card.balance
) > summary > :first-child {
    background: var(--cfo-t-balance-soft) !important;
}

/* 1. CFO Clean */
html[data-cfo-table-layout="cfo_clean"] {
    --cfo-layout-radius: 7px;
    --cfo-layout-row-gap: 6px;
    --cfo-layout-shadow: 0 2px 8px rgba(18, 32, 68, .045);
}
html[data-cfo-table-layout="cfo_clean"] body :is(.table, .table-kpi) tbody tr > * {
    border-bottom-color: var(--cfo-t-grid) !important;
}

/* 2. Executive Board */
html[data-cfo-table-layout="executive_board"] {
    --cfo-layout-radius: 4px;
    --cfo-layout-row-gap: 5px;
    --cfo-layout-header-letter: .055em;
    --cfo-layout-shadow: 0 3px 10px rgba(18, 32, 68, .08);
}
html[data-cfo-table-layout="executive_board"] body :is(
    .table thead th, .table-kpi thead th, .pluri-matrix-header,
    .gesteco-year-header, .financial-preview-header, .ce-table-head, .theme-sample-header
) {
    background: var(--cfo-t-text) !important;
    color: #fff !important;
}
html[data-cfo-table-layout="executive_board"] body :is(
    .matrix-total-row, .gesteco-year-card.total > summary, .pluri-matrix-card.balance > summary,
    .financial-preview-card.balance > summary, .ce-card.balance > summary, .theme-sample-row.total
) {
    border-top: 0 !important;
    background: var(--cfo-t-balance-soft) !important;
    font-weight: 800 !important;
}

/* 3. FP&A Variance Focus */
html[data-cfo-table-layout="fpa_variance"] {
    --cfo-layout-radius: 5px;
    --cfo-layout-row-gap: 4px;
}
html[data-cfo-table-layout="fpa_variance"] body :is(
    .delta-ok, .delta-ko, .eco-good, .eco-bad, .value-delta,
    .pluri-cell-delta, .gesteco-cell-delta, .financial-preview-cell-delta
) {
    display: inline-flex !important;
    min-width: 72px;
    min-height: 25px;
    align-items: center !important;
    justify-content: center !important;
    padding: 3px 7px;
    border-radius: 999px;
    font-weight: 800 !important;
}
html[data-cfo-table-layout="fpa_variance"] body :is(.delta-ok, .eco-good) {
    background: var(--cfo-t-entry-soft) !important;
}
html[data-cfo-table-layout="fpa_variance"] body :is(.delta-ko, .eco-bad) {
    background: var(--cfo-t-tax-soft) !important;
}

/* 4. Compact Ledger */
html[data-cfo-table-layout="compact_ledger"] {
    --cfo-layout-radius: 0;
    --cfo-layout-row-gap: 0px;
    --cfo-layout-border-width: 1px;
    --cfo-layout-row-py: 6px;
    --cfo-layout-row-px: 8px;
    --cfo-layout-summary-min: 40px;
    --cfo-layout-detail-min: 23px;
    --cfo-layout-shadow: none;
}
html[data-cfo-table-layout="compact_ledger"] body :is(
    .pluri-matrix-card, .gesteco-card, .gesteco-year-card, .macro-block,
    .financial-preview-card, .ce-card, .theme-sample-row
) {
    border-bottom: 0 !important;
}
html[data-cfo-table-layout="compact_ledger"] body :is(.table, .table-kpi) {
    border: 1px solid var(--cfo-t-border);
}
html[data-cfo-table-layout="compact_ledger"] body :is(.table, .table-kpi) :is(th, td) {
    border: 1px solid var(--cfo-t-grid) !important;
}

/* 5. Business Cards */
html[data-cfo-table-layout="business_cards"] {
    --cfo-layout-radius: 11px;
    --cfo-layout-row-gap: 12px;
    --cfo-layout-row-py: 14px;
    --cfo-layout-summary-min: 64px;
    --cfo-layout-shadow: 0 7px 20px rgba(18, 32, 68, .075);
}
html[data-cfo-table-layout="business_cards"] body :is(
    .pluri-matrix-detail-row, .gesteco-year-detail-row,
    .financial-preview-detail-row, .ce-detail-row
) {
    margin-inline: 12px;
    background: var(--cfo-t-surface-alt) !important;
}

/* 6. Monthly Performance Bands */
html[data-cfo-table-layout="monthly_bands"] body :is(
    .pluri-matrix-header, .pluri-matrix-card > summary, .pluri-matrix-detail-row,
    .matrix-total-row, .gesteco-year-header, .gesteco-year-card > summary,
    .gesteco-year-detail-row, .financial-preview-header,
    .financial-preview-card > summary, .financial-preview-detail-row,
    .theme-sample-header, .theme-sample-row
) > :nth-child(even):not(:first-child) {
    background: color-mix(in srgb, var(--cfo-t-balance-soft) 55%, transparent) !important;
    box-shadow: inset 1px 0 var(--cfo-t-grid), inset -1px 0 var(--cfo-t-grid);
}

/* 7. Traffic Light Control */
html[data-cfo-table-layout="traffic_light"] body :is(.delta-ok, .eco-good, .delta-ko, .eco-bad) {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 6px;
    font-weight: 800 !important;
}
html[data-cfo-table-layout="traffic_light"] body :is(.delta-ok, .eco-good, .delta-ko, .eco-bad)::before {
    width: 9px;
    height: 9px;
    flex: 0 0 9px;
    border-radius: 50%;
    background: currentColor;
    content: "";
    box-shadow: 0 0 0 3px color-mix(in srgb, currentColor 14%, transparent);
}

/* 8. Inline Performance Bars */
html[data-cfo-table-layout="inline_bars"] body :is(
    .delta-ok, .eco-good, .delta-ko, .eco-bad,
    .value-budget, .value-actual, .pluri-cell-budget, .pluri-cell-actual
) {
    min-width: 84px;
    padding: 4px 7px;
    border-radius: 4px;
    background-repeat: no-repeat !important;
    background-size: 100% 100% !important;
}
html[data-cfo-table-layout="inline_bars"] body :is(.delta-ok, .eco-good, .value-actual) {
    background-image: linear-gradient(90deg, color-mix(in srgb, var(--cfo-t-good) 16%, transparent) 0 68%, transparent 68%) !important;
}
html[data-cfo-table-layout="inline_bars"] body :is(.delta-ko, .eco-bad) {
    background-image: linear-gradient(90deg, color-mix(in srgb, var(--cfo-t-bad) 14%, transparent) 0 68%, transparent 68%) !important;
}

/* 9. Management Zebra */
html[data-cfo-table-layout="management_zebra"] {
    --cfo-layout-radius: 3px;
    --cfo-layout-row-gap: 1px;
    --cfo-layout-shadow: none;
}
html[data-cfo-table-layout="management_zebra"] body :is(.table, .table-kpi) tbody tr:nth-child(even) > *,
html[data-cfo-table-layout="management_zebra"] body :is(
    .pluri-matrix-card, .gesteco-year-card, .financial-preview-card, .ce-card
):nth-child(even) > summary {
    background: var(--cfo-t-surface-alt) !important;
}

/* 10. Audit & Compliance */
html[data-cfo-table-layout="audit_compliance"] {
    --cfo-layout-radius: 1px;
    --cfo-layout-row-gap: 0px;
    --cfo-layout-border-width: 1px;
    --cfo-layout-shadow: none;
}
html[data-cfo-table-layout="audit_compliance"] body :is(.table, .table-kpi) :is(th, td) {
    border: 1px solid var(--cfo-t-border) !important;
}
html[data-cfo-table-layout="audit_compliance"] body :is(.table-responsive, .pluri-matrix-scroll, .gesteco-year-table, .financial-preview-wrap) thead th,
html[data-cfo-table-layout="audit_compliance"] body :is(.pluri-matrix-header, .gesteco-year-header, .financial-preview-header, .ce-table-head) {
    position: sticky;
    top: 0;
    z-index: 5;
}
html[data-cfo-table-layout="audit_compliance"] body .table-responsive :is(th, td):first-child {
    position: sticky;
    left: 0;
    z-index: 4;
    background: var(--cfo-t-surface);
    box-shadow: 1px 0 var(--cfo-t-border);
}

/* 11. Presentation Wide */
html[data-cfo-table-layout="presentation_wide"] {
    --cfo-layout-radius: 9px;
    --cfo-layout-row-gap: 12px;
    --cfo-layout-row-py: 16px;
    --cfo-layout-row-px: 18px;
    --cfo-layout-summary-min: 72px;
    --cfo-layout-detail-min: 38px;
    --cfo-layout-font-scale: 1.12;
    --cfo-layout-shadow: 0 8px 24px rgba(18, 32, 68, .075);
}
html[data-cfo-table-layout="presentation_wide"] body :is(
    .matrix-total-row, .gesteco-year-card.total > summary, .pluri-matrix-card.balance > summary,
    .financial-preview-card.balance > summary, .ce-card.balance > summary
) {
    font-size: 1.08em !important;
    font-weight: 800 !important;
}

/* 12. Print & PDF Professional */
html[data-cfo-table-layout="print_pdf"] {
    --cfo-layout-radius: 0;
    --cfo-layout-row-gap: 0px;
    --cfo-layout-shadow: none;
    --cfo-layout-header-letter: .04em;
}
html[data-cfo-table-layout="print_pdf"] body :is(
    .pluri-matrix-card, .gesteco-card, .gesteco-year-card, .macro-block,
    .financial-preview-card, .ce-card, .table, .table-kpi
) {
    box-shadow: none !important;
}
html[data-cfo-table-layout="print_pdf"] body :is(.delta-ok, .eco-good, .delta-ko, .eco-bad) {
    color: var(--cfo-t-text) !important;
    text-decoration: underline;
    text-decoration-style: dotted;
}

@media print {
    html[data-cfo-table-layout] {
        --cfo-layout-radius: 0;
        --cfo-layout-row-gap: 0px;
        --cfo-layout-shadow: none;
        --cfo-layout-row-py: 5px;
        --cfo-layout-row-px: 7px;
    }
    html[data-cfo-table-layout] body :is(.table-responsive, .pluri-matrix-scroll, .gesteco-year-table, .financial-preview-wrap) {
        overflow: visible !important;
    }
    html[data-cfo-table-layout] body :is(.table, .table-kpi, .pluri-matrix-scroll, .gesteco-year-table) {
        break-inside: avoid;
    }
}
