.matrix {
    display: inline-block;
    vertical-align: middle;
    font-size: 18px;
    line-height: 1;
    white-space: nowrap;
    padding: 10px;
    position: relative;
    user-select: none;
}
.matrix:before, .matrix:after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    width: 2px;
    background-color: black;
}
.matrix:before { left: 0; }
.matrix:after { right: 0; }
.matrix-row {
    display: flex;
    justify-content: flex-start;
}
.matrix-element {
    width: 3em;
    height: 1.5em;
    text-align: center;
    margin: 0.2em;
    cursor: ew-resize;
    color: #0066cc;
    display: flex;
    align-items: center;
    justify-content: center;
}

.matrix-element::selection {
    background-color: transparent;
}

.matrix-wrapper {
    display: inline-block;
    font-family: monospace;
    font-size: 14px;
}

.matrix-grid {
    display: flex;
    align-items: flex-start;
}

.matrix-column {
    display: flex;
    flex-direction: column;
}

.matrix-label-column {
    margin-right: 5px;
}

.matrix-data-wrapper {
    display: flex;
    position: relative;
    padding: 0 15px;
}

/* Matrix brackets */
.matrix-data-wrapper::before,
.matrix-data-wrapper::after {
    content: '';
    position: absolute;
    width: 10px;
    height: 100%;
    pointer-events: none;
}

.matrix-data-wrapper::before {
    left: 3px;
    border-left: 2px solid black;
    border-top: 2px solid black;
    border-bottom: 2px solid black;
}

.matrix-data-wrapper::after {
    right: 3px;
    border-right: 2px solid black;
    border-top: 2px solid black;
    border-bottom: 2px solid black;
}

.matrix-data-column {
    margin: 0;
}

.matrix-cell {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 2em;
    box-sizing: border-box;
}

.matrix-element {
    width: 3em;
    margin: 0 0.3em;
    cursor: ew-resize;
    color: #0066cc;
}

.matrix-element-static {
    cursor: default;
    color: #666;
}

.matrix-row-label {
    width: auto;
    min-width: 5em;
    justify-content: flex-end;
    padding-right: 0.5em;
    font-weight: bold;
    color: #333;
    white-space: nowrap;
}

.matrix-col-label {
    font-weight: bold;
    color: #333;
    margin-bottom: 5px;
    width: 3.6em; /* Slightly wider than matrix elements to account for margins */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Flexible columns mode */
.matrix-grid.flexible-columns .matrix-element {
    width: auto;
    min-width: 3em;
    padding: 0 0.5em;
}

.matrix-grid.flexible-columns .matrix-col-label {
    width: auto;
    min-width: 3.6em;
    padding: 0 0.5em;
}

.matrix-grid.flexible-columns .matrix-data-column {
    flex-shrink: 0;
}

.matrix-corner-cell {
    visibility: hidden;
    height: 2em;
    margin-bottom: 5px;
}

/* Adjust bracket positioning to exclude headers */
.matrix-grid:has(.matrix-col-label) .matrix-data-wrapper::before,
.matrix-grid:has(.matrix-col-label) .matrix-data-wrapper::after {
    top: calc(2em + 5px); /* Height of header cell + margin */
    height: calc(100% - 2em - 5px); /* Full height minus header */
}

/* If there are no column names, brackets should span full height */
.matrix-grid:not(:has(.matrix-col-label)) .matrix-data-wrapper::before,
.matrix-grid:not(:has(.matrix-col-label)) .matrix-data-wrapper::after {
    top: 0;
    height: 100%;
}