.matrix-table {
    border: 1px solid var(--dashboard-border);
    height: 100%;
    background: #ffffff;
    font-family: var(--ems-chart-font-family, "Segoe UI", Tahoma, Geneva, Verdana, sans-serif);
    position: relative;
}

.matrix-table .card-body {
    position: relative;
    min-height: 280px;
}

.matrix-table--pending .matrix-table__scroll,
.matrix-table--loading .matrix-table__scroll {
    opacity: 0.35;
    pointer-events: none;
}

.matrix-table--pending .card-body,
.matrix-table--loading .card-body {
    min-height: 320px;
}

.matrix-table__loading-overlay {
    position: absolute;
    inset: 0;
    z-index: 3;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    background: rgba(255, 255, 255, 0.72);
    pointer-events: none;
}

.matrix-table__loading-overlay .spinner-border {
    width: 2rem;
    height: 2rem;
    color: var(--dashboard-primary);
    border-width: 0.2em;
}

.matrix-table__loading-text {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--dashboard-muted);
}

.matrix-table__header {
    background: #ffffff;
    border-bottom: 1px solid var(--dashboard-border);
    padding: 0.5rem 0.6rem;
    gap: 0.5rem;
}

.matrix-table__title {
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    color: var(--dashboard-primary);
    text-transform: uppercase;
    line-height: 1.25;
    white-space: nowrap;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1 1 auto;
}

.matrix-table__tools {
    flex: 0 0 auto;
    margin-left: auto;
}

.matrix-table__unit {
    font-size: 0.62rem;
    font-weight: 600;
    color: var(--dashboard-muted);
    text-transform: none;
    letter-spacing: 0;
}

.matrix-table__scroll {
    max-height: 420px;
    overflow-x: auto;
    overflow-y: scroll;
    scrollbar-gutter: stable;
    position: relative;
}

.matrix-table__scroll.table-responsive {
    overflow-x: auto;
    overflow-y: scroll;
    padding: 0;
    margin: 0;
}

.matrix-table__grid {
    --matrix-stt-width: 2.35rem;
    --matrix-name-width: 11rem;
    --matrix-metric-width: 3.45rem;
    --matrix-freeze-last-width: 3.45rem;
    --matrix-indent-step: 0.5rem;
    width: 100%;
    min-width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    table-layout: fixed;
}

.matrix-table__grid col.matrix-table__col-stt {
    width: var(--matrix-stt-width);
}

.matrix-table__grid col.matrix-table__col-name {
    width: var(--matrix-name-width);
}

.matrix-table__grid col.matrix-table__col-metric {
    width: var(--matrix-metric-width);
}

.matrix-table__grid--wide {
    --matrix-stt-width: 2.5rem;
    --matrix-name-width: 13rem;
    --matrix-metric-width: 4.65rem;
    --matrix-freeze-last-width: 4.65rem;
}

.matrix-table__grid--fill {
    width: 100% !important;
    min-width: 100% !important;
    table-layout: fixed;
}

.matrix-table__grid--fill .matrix-table__col-metric,
.matrix-table__grid--fill .matrix-table__col-stt,
.matrix-table__grid--fill .matrix-table__col-name {
    max-width: none !important;
}

.matrix-table__grid--fill col.matrix-table__col-stt,
.matrix-table__grid--fill col.matrix-table__col-name,
.matrix-table__grid--fill col.matrix-table__col-metric {
    width: var(--matrix-metric-width);
}

.matrix-table__grid--fill col.matrix-table__col-stt {
    width: var(--matrix-stt-width);
}

.matrix-table__grid--fill col.matrix-table__col-name {
    width: var(--matrix-name-width);
}

.matrix-table__grid--compact {
    --matrix-stt-width: 3.5rem;
    --matrix-name-width: 18rem;
    --matrix-metric-width: 7rem;
    --matrix-freeze-last-width: 7rem;
    --matrix-indent-step: 0.45rem;
}

.matrix-table__grid thead th,
.matrix-table__grid tbody td {
    border: 1px solid var(--dashboard-border);
}

/* Đồng nhất mép header → body: cột TT/Dịch vụ và cột chỉ số không còn đường kẻ lệch nhau. */
.matrix-table__grid thead tr:first-child th.matrix-table__col-stt,
.matrix-table__grid thead tr:first-child th.matrix-table__col-name,
.matrix-table__grid thead tr:nth-child(2) th {
    border-bottom: 0;
}

.matrix-table__grid tbody td {
    border-top: 0;
}

.matrix-table__grid thead th {
    background: #f8fafc;
    font-size: 0.62rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.01em;
    color: var(--dashboard-muted);
    white-space: normal;
    position: sticky;
    top: 0;
    z-index: 4;
    padding: 0.35rem 0.25rem;
    line-height: 1.15;
    vertical-align: middle;
    text-align: center;
}

.matrix-table__grid thead tr:nth-child(2) th {
    top: 1.85rem;
    z-index: 5;
}

.matrix-table__grid tbody td {
    font-size: 0.72rem;
    font-weight: 500;
    vertical-align: middle;
    white-space: nowrap;
    background-color: #ffffff;
    padding: 0.28rem 0.2rem;
    line-height: 1.2;
    font-variant-numeric: tabular-nums;
}

.matrix-table__grid tbody td.matrix-table__col-name {
    white-space: normal;
    vertical-align: top;
    padding-top: 0.35rem;
    padding-bottom: 0.35rem;
}

.matrix-table__col-stt {
    width: var(--matrix-stt-width) !important;
    min-width: var(--matrix-stt-width);
    max-width: none;
    font-weight: 700;
    font-size: 0.72rem;
    color: var(--dashboard-muted);
    text-align: center;
    padding-left: 0.05rem;
    padding-right: 0.05rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    box-sizing: border-box;
}

.matrix-table__grid .matrix-table__col-name {
    width: var(--matrix-name-width);
    min-width: var(--matrix-name-width);
    max-width: none;
    text-align: left;
    font-size: 0.68rem;
    line-height: 1.25;
    white-space: normal;
    padding-left: 0.2rem;
    padding-right: 0.25rem;
    overflow: visible;
}

.matrix-table__col-metric {
    width: var(--matrix-metric-width);
    min-width: var(--matrix-metric-width);
    max-width: var(--matrix-metric-width);
    overflow: hidden;
    box-sizing: border-box;
}

.matrix-table__grid thead th.matrix-table__col-group {
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    text-align: center;
    vertical-align: middle;
    white-space: nowrap;
    padding: 0.35rem 0.25rem;
    border-bottom: 1px solid var(--dashboard-border);
}

.matrix-table__grid thead th.matrix-table__col-group--qty {
    background: #dbeafe;
    color: #1e3a8a;
}

.matrix-table__grid thead th.matrix-table__col-group--weight {
    background: #ffedd5;
    color: #9a3412;
}

.matrix-table__grid thead th.matrix-table__col-group--revenue {
    background: #dcfce7;
    color: #166534;
}

.matrix-table__grid thead th.matrix-table__col-sub {
    font-size: 0.6rem;
    font-weight: 600;
    line-height: 1.2;
    white-space: normal;
    vertical-align: middle;
    padding: 0.28rem 0.15rem;
    height: auto;
    min-height: 2.6rem;
    text-transform: none;
    letter-spacing: 0;
}

.matrix-table__grid thead th.matrix-table__col-sub--qty {
    background: #eff6ff;
    color: #1e40af;
}

.matrix-table__grid thead th.matrix-table__col-sub--weight {
    background: #fff7ed;
    color: #9a3412;
}

.matrix-table__grid thead th.matrix-table__col-sub--revenue {
    background: #f0fdf4;
    color: #166534;
}

.matrix-table__grid thead tr:first-child th.matrix-table__col-stt,
.matrix-table__grid thead tr:first-child th.matrix-table__col-name {
    vertical-align: middle;
}

.matrix-table__grid--wide .matrix-table__col-metric {
    padding-left: 0.35rem;
    padding-right: 0.35rem;
}

.matrix-table__grid--wide .matrix-table__trend {
    gap: 0.2rem;
}

.matrix-table__grid thead th.matrix-table__col-name {
    text-align: left;
}

.matrix-table__grid thead th.matrix-table__col-stt {
    width: var(--matrix-stt-width) !important;
    min-width: var(--matrix-stt-width);
    max-width: none;
    text-align: center;
    overflow: hidden;
    box-sizing: border-box;
}

.matrix-table__col-freeze-left {
    position: sticky;
    z-index: 3;
}

.matrix-table__grid thead th.matrix-table__col-freeze-left {
    z-index: 6;
    background: #f8fafc;
}

.matrix-table__grid .matrix-table__col-stt.matrix-table__col-freeze-left {
    left: 0;
    box-shadow: 2px 0 4px rgba(15, 23, 42, 0.06);
}

.matrix-table__grid .matrix-table__col-name.matrix-table__col-freeze-left {
    left: var(--matrix-stt-width);
    box-shadow: 2px 0 4px rgba(15, 23, 42, 0.06);
}

.matrix-table__col-freeze-right {
    position: sticky;
    z-index: 3;
    overflow: hidden;
    background-clip: padding-box;
}

.matrix-table__col-freeze-right--prev4 {
    right: calc(var(--matrix-metric-width) * 4);
    z-index: 3;
}

.matrix-table__col-freeze-right--prev3 {
    right: calc(var(--matrix-metric-width) * 3);
    z-index: 3;
}

.matrix-table__col-freeze-right--prev2 {
    right: calc(var(--matrix-metric-width) * 2);
    z-index: 4;
}

.matrix-table__col-freeze-right--prev {
    right: var(--matrix-metric-width);
    z-index: 5;
}

.matrix-table__col-freeze-right--last {
    right: 0;
    z-index: 6;
}

.matrix-table__col-freeze-right--edge {
    box-shadow: -3px 0 6px rgba(15, 23, 42, 0.1);
}

.matrix-table__col-freeze-right-group {
    position: sticky;
    right: 0;
    z-index: 8;
    box-shadow: -3px 0 6px rgba(15, 23, 42, 0.1);
}

.matrix-table__grid thead th.matrix-table__col-freeze-right {
    z-index: 6;
    background: #f8fafc;
}

.matrix-table__grid thead th.matrix-table__col-freeze-left.matrix-table__col-stt {
    z-index: 7;
}

.matrix-table__grid thead th.matrix-table__col-freeze-left.matrix-table__col-name {
    z-index: 7;
}

.matrix-table__grid thead th.matrix-table__col-freeze-right--prev4,
.matrix-table__grid thead th.matrix-table__col-freeze-right--prev3 {
    z-index: 7;
}

.matrix-table__grid thead th.matrix-table__col-freeze-right--prev2 {
    z-index: 8;
}

.matrix-table__grid thead th.matrix-table__col-freeze-right--prev {
    z-index: 9;
}

.matrix-table__grid thead th.matrix-table__col-freeze-right--last {
    z-index: 10;
}

.matrix-table__grid thead th.matrix-table__col-freeze-right-group {
    z-index: 9;
}

.matrix-table__grid thead th.matrix-table__col-sub--revenue.matrix-table__col-freeze-right {
    background: #f0fdf4;
    color: #166534;
}

.matrix-table__grid thead th.matrix-table__col-group--revenue.matrix-table__col-freeze-right-group {
    background: #dcfce7;
    color: #166534;
}

.matrix-table__grid tbody tr:not(.matrix-table__row--group) .matrix-table__col-freeze-left {
    background-color: #ffffff;
}

.matrix-table__grid tbody tr.matrix-table__row--group .matrix-table__col-freeze-left {
    background-color: #f7f9fc;
}

.matrix-table__grid tbody tr .matrix-table__col-freeze-left.matrix-table__cell--good,
.matrix-table__grid tbody tr .matrix-table__col-stt.matrix-table__cell--good {
    background-color: rgba(92, 184, 92, 0.18);
}

.matrix-table__grid tbody tr .matrix-table__col-freeze-left.matrix-table__cell--warning,
.matrix-table__grid tbody tr .matrix-table__col-stt.matrix-table__cell--warning {
    background-color: rgba(240, 173, 78, 0.22);
}

.matrix-table__grid tbody tr .matrix-table__col-freeze-left.matrix-table__cell--danger,
.matrix-table__grid tbody tr .matrix-table__col-stt.matrix-table__cell--danger {
    background-color: rgba(217, 83, 79, 0.15);
}

.matrix-table__name-cell {
    display: flex;
    align-items: flex-start;
    gap: 0.2rem;
    min-width: 0;
    padding-left: calc(var(--row-level, 0) * var(--matrix-indent-step, 0.5rem));
}

.matrix-table__toggle {
    width: 0.95rem;
    height: 0.95rem;
    border: 1px solid var(--dashboard-border);
    background: #ffffff;
    border-radius: 0.15rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.65rem;
    font-weight: 700;
    color: var(--dashboard-primary);
    cursor: pointer;
    flex-shrink: 0;
    padding: 0;
    line-height: 1;
}

.matrix-table__toggle--placeholder {
    visibility: hidden;
    display: inline-flex;
    pointer-events: none;
}

.matrix-table__name {
    font-weight: 600;
    font-size: 0.72rem;
    line-height: 1.25;
    word-break: break-word;
    flex: 1 1 auto;
    min-width: 0;
}

.matrix-table__name--level-1 {
    font-weight: 500;
    color: #4a5568;
    font-size: 0.7rem;
}

.matrix-table__name--level-2 {
    font-weight: 500;
    color: #4a5568;
    font-size: 0.68rem;
}

.matrix-table__grid tbody tr:not(.matrix-table__row--group) .matrix-table__col-freeze-right {
    background-color: #ffffff;
}

.matrix-table__grid tbody tr.matrix-table__row--group .matrix-table__col-freeze-right {
    background-color: #f7f9fc;
}

/* Cột cố định phải: nền trắng như DT LK năm BC, chỉ giữ màu trên trend */
.matrix-table__grid tbody tr:not(.matrix-table__row--group) .matrix-table__col-freeze-right.matrix-table__cell--good,
.matrix-table__grid tbody tr:not(.matrix-table__row--group) .matrix-table__col-freeze-right.matrix-table__cell--warning,
.matrix-table__grid tbody tr:not(.matrix-table__row--group) .matrix-table__col-freeze-right.matrix-table__cell--danger,
.matrix-table__grid tbody tr:not(.matrix-table__row--group) .matrix-table__col-freeze-right.matrix-table__cell--neutral {
    background-color: #ffffff;
    color: var(--dashboard-text);
}

.matrix-table__grid tbody tr.matrix-table__row--group .matrix-table__col-freeze-right.matrix-table__cell--good,
.matrix-table__grid tbody tr.matrix-table__row--group .matrix-table__col-freeze-right.matrix-table__cell--warning,
.matrix-table__grid tbody tr.matrix-table__row--group .matrix-table__col-freeze-right.matrix-table__cell--danger,
.matrix-table__grid tbody tr.matrix-table__row--group .matrix-table__col-freeze-right.matrix-table__cell--neutral {
    background-color: #f7f9fc;
    color: var(--dashboard-text);
}

.matrix-table__grid--compact .matrix-table__col-metric {
    font-size: 0.66rem;
    padding-left: 0.28rem;
    padding-right: 0.28rem;
}

.matrix-table__grid--kdtn-dv.matrix-table__grid--compact .matrix-table__col-metric {
    font-size: 0.66rem;
    padding-left: 0.12rem;
    padding-right: 0.12rem;
}

.matrix-table__grid--compact .matrix-table__trend {
    gap: 0.12rem;
    font-size: 0.66rem;
    max-width: 100%;
}

.matrix-table__grid--compact .matrix-table__trend-value {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.matrix-table__cell {
    text-align: right;
    font-variant-numeric: tabular-nums;
    cursor: pointer;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.matrix-table__cell-inner {
    display: block;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.matrix-table__col-freeze-right .matrix-table__cell-inner {
    position: relative;
    z-index: 1;
}

.matrix-table__trend {
    display: inline-flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.25rem;
    font-weight: 700;
    font-size: 0.72rem;
    line-height: 1.2;
    letter-spacing: -0.01em;
    max-width: 100%;
    vertical-align: top;
}

.matrix-table__trend-icon {
    font-size: 0.55rem;
    line-height: 1;
    flex-shrink: 0;
}

.matrix-table__trend-value {
    font-variant-numeric: tabular-nums;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.matrix-table__trend--up {
    color: #2d6a2d;
}

.matrix-table__trend--up .matrix-table__trend-icon {
    color: #3d9a50;
}

.matrix-table__trend--down {
    color: #922020;
}

.matrix-table__trend--down .matrix-table__trend-icon {
    color: #DC2626;
}

.matrix-table__trend--warn {
    color: #9a6700;
    font-weight: 700;
}

.matrix-table__trend--flat {
    color: var(--dashboard-muted);
    font-weight: 600;
}

.matrix-table__cell--good {
    background-color: var(--success-bg);
    color: var(--success);
    font-weight: 700;
}

.matrix-table__cell--warning {
    background-color: var(--warning-bg);
    color: var(--target-dark);
    font-weight: 700;
}

.matrix-table__cell--danger {
    background-color: var(--danger-bg);
    color: var(--danger-dark);
    font-weight: 700;
}

.matrix-table__cell--neutral {
    background-color: var(--card-bg);
    color: var(--text-main);
}

.matrix-table__cell--active {
    outline: 2px solid var(--dashboard-primary);
    outline-offset: -2px;
}

.matrix-table__row--group td {
    background: #f7f9fc;
}

.matrix-table__row--group .matrix-table__name,
.matrix-table__name--group {
    font-weight: 700;
    color: #1a365d;
}

.matrix-table__row--theme-blue td:first-child { color: #0B5CAD; }
.matrix-table__row--theme-green td:first-child { color: #2d6a2d; }
.matrix-table__row--theme-orange td:first-child { color: #b45309; }
.matrix-table__row--theme-purple td:first-child { color: #6b21a8; }

.matrix-table__row--hidden,
.matrix-table__grid tbody tr[hidden] {
    display: none !important;
}

/* Sticky freeze cells stay painted if only the <tr> is hidden. */
.matrix-table__row--hidden > td,
.matrix-table__row--hidden > th,
.matrix-table__grid tbody tr[hidden] > td,
.matrix-table__grid tbody tr[hidden] > th {
    display: none !important;
    position: static !important;
}

.matrix-table__row--section-hidden td {
    background: #f8fafc;
    border-top: 1px dashed #d7e0ea;
}

.matrix-table__name-cell--section-hidden {
    padding-left: 0.15rem;
}

.matrix-table__section-open {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    border: 0;
    background: transparent;
    color: #0B5CAD;
    font-size: 0.68rem;
    font-weight: 700;
    padding: 0.15rem 0;
    cursor: pointer;
}

.matrix-table__section-open-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1rem;
    height: 1rem;
    border: 1px solid #9db3cf;
    border-radius: 2px;
    line-height: 1;
    font-size: 0.75rem;
    background: #fff;
}

.matrix-table__section-open-label {
    text-align: left;
}

@media (max-width: 1199.98px) {
    .matrix-table__scroll {
        max-height: 300px;
    }
}

@media (max-width: 767.98px) {
    .matrix-table__scroll {
        max-height: none;
    }

    .matrix-table__grid {
        --matrix-stt-width: 2.4rem;
        --matrix-name-width: 8.5rem;
        --matrix-metric-width: 3.35rem;
    }
}
