/*
 * Dashboard tables: containment, then presentation.
 *
 * Two problems, one file. Only 21 of the 99 table views were wrapped in
 * anything scrollable, so the other tables pushed the page sideways -- the
 * widest carries 44 columns -- and the whole layout moved with them. And the
 * tables were unstyled beyond Bootstrap's borders, so a dense screen of medical
 * and financial data read as a wall.
 *
 * Applied from the layout, not per view, so it reaches every table without 99
 * edits and cannot drift between them. Written direction-agnostic: the
 * dashboard renders RTL for Arabic and LTR for English off <html dir>.
 */

:root {
    --mq-table-border: #ebedf3;
    --mq-table-head-bg: #f8f9fc;
    --mq-table-head-fg: #7e8299;
    --mq-table-row-hover: #f4f6fa;
    --mq-table-stripe: #fcfdfe;
    --mq-table-fg: #3f4254;
    --mq-table-radius: 12px;
    --mq-table-accent: #3699ff;
}

/* ── The scroll container ──────────────────────────────────────────────────
 * The table scrolls inside this, so a wide table never widens the page. */
.mq-table-wrap {
    position: relative;
    width: 100%;
    max-width: 100%;
    overflow: auto;
    /* A height bound is what makes the sticky header below actually stick:
     * sticky positions against the nearest scrolling ancestor, so without one
     * the header has nothing to stick to. Only applies once a table is long
     * enough to exceed it, so short tables are untouched. */
    max-height: 72vh;
    border: 1px solid var(--mq-table-border);
    border-radius: var(--mq-table-radius);
    background: #fff;
    -webkit-overflow-scrolling: touch;
}

.mq-table-wrap::-webkit-scrollbar {
    height: 9px;
}

.mq-table-wrap::-webkit-scrollbar-track {
    background: transparent;
}

.mq-table-wrap::-webkit-scrollbar-thumb {
    background: #d6dae3;
    border-radius: 999px;
}

.mq-table-wrap::-webkit-scrollbar-thumb:hover {
    background: #b9bfcc;
}

/* There is more beyond the edge than fits: say so, rather than leaving the
 * reader to find out by accident.
 *
 * An inset shadow rather than an absolutely positioned overlay, because this
 * container scrolls in both directions -- an overlay would scroll away with the
 * content, while an inset shadow is painted on the box and stays put. */
.mq-table-wrap.mq-has-overflow {
    box-shadow: inset -14px 0 12px -12px rgba(24, 28, 50, 0.14);
}

[dir='rtl'] .mq-table-wrap.mq-has-overflow {
    box-shadow: inset 14px 0 12px -12px rgba(24, 28, 50, 0.14);
}

.mq-table-wrap.mq-has-overflow.mq-scrolled-end,
[dir='rtl'] .mq-table-wrap.mq-has-overflow.mq-scrolled-end {
    box-shadow: none;
}

/* ── The table ───────────────────────────────────────────────────────────── */
.mq-table-wrap > table {
    width: 100%;
    margin: 0;
    border: 0;
    border-collapse: separate;
    border-spacing: 0;
    color: var(--mq-table-fg);
    font-size: 0.925rem;
}

.mq-table-wrap > table > thead > tr > th {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--mq-table-head-bg);
    color: var(--mq-table-head-fg);
    font-weight: 600;
    font-size: 0.8rem;
    letter-spacing: 0.02em;
    text-transform: none;
    white-space: nowrap;
    padding: 0.9rem 1rem;
    border: 0;
    border-bottom: 1px solid var(--mq-table-border);
    vertical-align: middle;
}

.mq-table-wrap > table > tbody > tr > td {
    padding: 0.85rem 1rem;
    border: 0;
    border-bottom: 1px solid var(--mq-table-border);
    vertical-align: middle;
    white-space: nowrap;
}

.mq-table-wrap > table > tbody > tr:last-child > td {
    border-bottom: 0;
}

.mq-table-wrap > table > tbody > tr:nth-child(even) {
    background: var(--mq-table-stripe);
}

.mq-table-wrap > table > tbody > tr:hover {
    background: var(--mq-table-row-hover);
}

/* Long free text is the exception to nowrap -- a bio would otherwise make the
 * table thousands of pixels wide on its own. */
.mq-table-wrap > table td.mq-wrap,
.mq-table-wrap > table td[data-wrap] {
    white-space: normal;
    min-width: 220px;
    max-width: 420px;
}

/* Bootstrap's own borders fight the ones above; the wrapper draws the frame. */
.mq-table-wrap > table.table-bordered,
.mq-table-wrap > table.table-bordered > thead > tr > th,
.mq-table-wrap > table.table-bordered > tbody > tr > td {
    border-left: 0;
    border-right: 0;
}

.mq-table-wrap > table.table-striped > tbody > tr:nth-of-type(odd) {
    background: transparent;
}

/* ── Toolbar above the table ─────────────────────────────────────────────── */
.mq-table-toolbar {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.mq-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    height: 36px;
    padding: 0 0.85rem;
    border: 1px solid var(--mq-table-border);
    border-radius: 8px;
    background: #fff;
    color: #5e6278;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.mq-btn:hover {
    background: var(--mq-table-head-bg);
    border-color: #d6dae3;
    color: var(--mq-table-accent);
}

.mq-btn svg {
    width: 16px;
    height: 16px;
    flex: none;
}

.mq-btn .mq-count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--mq-table-accent);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 600;
}

/* ── Column picker ───────────────────────────────────────────────────────── */
.mq-columns {
    position: relative;
}

.mq-columns-panel {
    position: absolute;
    top: calc(100% + 6px);
    z-index: 40;
    width: 260px;
    max-height: 380px;
    overflow-y: auto;
    padding: 0.5rem 0;
    border: 1px solid var(--mq-table-border);
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 10px 30px rgba(24, 28, 50, 0.12);
    display: none;
}

[dir='rtl'] .mq-columns-panel {
    left: 0;
}

[dir='ltr'] .mq-columns-panel,
html:not([dir='rtl']) .mq-columns-panel {
    right: 0;
}

.mq-columns.mq-open .mq-columns-panel {
    display: block;
}

.mq-columns-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.35rem 0.9rem 0.6rem;
    border-bottom: 1px solid var(--mq-table-border);
    margin-bottom: 0.35rem;
}

.mq-columns-head strong {
    font-size: 0.85rem;
    color: var(--mq-table-fg);
}

.mq-columns-reset {
    border: 0;
    background: none;
    padding: 0;
    font-size: 0.78rem;
    color: #f1416c;
    cursor: pointer;
}

.mq-columns-reset:hover {
    text-decoration: underline;
}

.mq-columns-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.4rem 0.9rem;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--mq-table-fg);
}

.mq-columns-item:hover {
    background: var(--mq-table-head-bg);
}

.mq-columns-item input {
    width: 16px;
    height: 16px;
    accent-color: var(--mq-table-accent);
    cursor: pointer;
    flex: none;
}

.mq-columns-item span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* A column switched off is hidden, not removed: the cells stay in the DOM so
 * nothing that counts them (DataTables, row scripts) is thrown off. */
.mq-col-hidden {
    display: none !important;
}

@media print {
    .mq-table-toolbar {
        display: none;
    }

    .mq-table-wrap {
        overflow: visible;
        border: 0;
    }
}
