/* ==========================================================================
   agentView Data Table
   Shared table surface for admin.html and dashboard.html.
   ========================================================================== */

.av-data-table-shell {
    min-width: 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    overflow: hidden;
}

.av-data-table-scroll,
.av-scroll-x:has(.av-data-table),
.av-import-table-scroll:has(.av-data-table) {
    width: 100%;
    min-width: 0;
    overflow-x: auto;
    overflow-y: hidden;
    background: var(--panel);
}

/* Force a clearly visible horizontal scrollbar on every table scroll
   container. The global rule in design-system.css uses a transparent track
   plus a near-invisible 8px thumb (var(--line) ≈ oklch 0.90), which is
   indistinguishable from the panel surface — users have no affordance that
   the table can scroll horizontally when columns get clipped. We override
   the scrollbar styling here so the horizontal rail always reads as a
   scrollable surface when the table overflows. */
.av-data-table-scroll,
.av-scroll-x:has(.av-data-table),
.av-import-table-scroll:has(.av-data-table),
.av-table-scroll {
    /* scrollbar-width: auto keeps ::-webkit-scrollbar styling active in
       Chromium (Chrome/Edge); 'thin' would suppress the explicit 12px height
       below and fall back to the OS overlay style (~7px, near-invisible). */
    scrollbar-width: auto;
    scrollbar-color: var(--ink-2) var(--bg-warm);   /* Firefox: thumb track */
}

.av-data-table-scroll::-webkit-scrollbar,
.av-scroll-x:has(.av-data-table)::-webkit-scrollbar,
.av-import-table-scroll:has(.av-data-table)::-webkit-scrollbar,
.av-table-scroll::-webkit-scrollbar {
    height: 12px;
    width: 12px;
}

.av-data-table-scroll::-webkit-scrollbar-track,
.av-scroll-x:has(.av-data-table)::-webkit-scrollbar-track,
.av-import-table-scroll:has(.av-data-table)::-webkit-scrollbar-track,
.av-table-scroll::-webkit-scrollbar-track {
    background: var(--bg-warm);
    border-top: 1px solid var(--line);
}

.av-data-table-scroll::-webkit-scrollbar-thumb,
.av-scroll-x:has(.av-data-table)::-webkit-scrollbar-thumb,
.av-import-table-scroll:has(.av-data-table)::-webkit-scrollbar-thumb,
.av-table-scroll::-webkit-scrollbar-thumb {
    background: var(--ink-2);
    border-radius: 999px;
    border: 2px solid var(--bg-warm);
}

.av-data-table-scroll::-webkit-scrollbar-thumb:hover,
.av-scroll-x:has(.av-data-table)::-webkit-scrollbar-thumb:hover,
.av-import-table-scroll:has(.av-data-table)::-webkit-scrollbar-thumb:hover,
.av-table-scroll::-webkit-scrollbar-thumb:hover {
    background: var(--ink);
}

.av-data-table {
    width: 100%;
    min-width: 680px;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--panel);
    color: var(--ink);
    font-size: 13px;
    line-height: 1.45;
    table-layout: auto;
}

.av-data-table.av-table thead,
.av-data-table.av-table tbody,
.av-data-table.av-table tr,
.av-data-table.av-table th,
.av-data-table.av-table td {
    display: revert;
}

.av-data-table.av-table tr {
    background: transparent;
    border: 0;
    border-radius: 0;
    padding: 0;
}

.av-data-table.av-table td::before {
    display: none;
}

.av-data-table caption {
    padding: 10px 14px;
    color: var(--muted);
    font-size: 12px;
    text-align: left;
}

.av-data-table th,
.av-data-table td {
    border: 0;
    border-bottom: 1px solid var(--line-2);
    text-align: left;
    vertical-align: middle;
}

.av-data-table thead th {
    height: 42px;
    padding: 0 14px;
    background: var(--bg-warm);
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10.5px;
    font-weight: 600;
    letter-spacing: 0;
    text-transform: uppercase;
    white-space: nowrap;
    user-select: none;
}

.av-data-table thead tr:first-child th {
    border-bottom-color: var(--line);
}

.av-data-table th.is-sortable,
.av-data-table th[data-sort-col] {
    cursor: pointer;
}

.av-data-table th.is-sortable:hover,
.av-data-table th[data-sort-col]:hover {
    color: var(--ink);
}

.av-data-table tbody td {
    padding: 12px 14px;
    color: var(--ink);
    min-height: 44px;
}

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

.av-data-table tbody tr:hover > td {
    background: var(--line-3);
}

.av-data-table tbody tr.is-muted > td,
.av-data-table tbody tr[style*="opacity"] > td {
    color: var(--muted);
}

.av-data-table .av-row-actions,
.av-data-table .av-display-row-actions,
.av-data-table .av-security-audit-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
    flex-wrap: wrap;
}

.av-data-table .av-badge {
    font-size: 10.5px;
    letter-spacing: 0;
}

/* ── Custom checkboxes inside av-data-table ──
   Replaces the browser default with a design-system-aligned control that
   has consistent border, hover/focus/checked/indeterminate states and a
   crisp SVG checkmark/dash. Falls back to native checkbox styling only
   if the user agent does not support `appearance: none`. */
.av-data-table .av-table-check,
.av-data-table input[type="checkbox"] {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    width: 18px;
    height: 18px;
    margin: 0;
    padding: 0;
    flex-shrink: 0;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
    position: relative;
    accent-color: var(--accent);
}

.av-data-table .av-table-check:hover,
.av-data-table input[type="checkbox"]:hover {
    border-color: var(--ink-2);
}

.av-data-table .av-table-check:focus-visible,
.av-data-table input[type="checkbox"]:focus-visible {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.av-data-table .av-table-check:checked,
.av-data-table input[type="checkbox"]:checked,
.av-data-table .av-table-check:indeterminate,
.av-data-table input[type="checkbox"]:indeterminate {
    background-color: var(--accent);
    border-color: var(--accent);
}

/* SVG checkmark via masked background — paints in --panel so it follows
   the surface colour. Mask URLs are encoded inline so no extra request. */
.av-data-table .av-table-check:checked::after,
.av-data-table input[type="checkbox"]:checked::after {
    content: "";
    position: absolute;
    inset: 0;
    background: var(--panel);
    -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round' d='M3.5 8.5l3 3 6-7'/></svg>") center / 14px 14px no-repeat;
    mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round' d='M3.5 8.5l3 3 6-7'/></svg>") center / 14px 14px no-repeat;
}

.av-data-table .av-table-check:indeterminate::after,
.av-data-table input[type="checkbox"]:indeterminate::after {
    content: "";
    position: absolute;
    inset: 0;
    background: var(--panel);
    -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' d='M4 8h8'/></svg>") center / 14px 14px no-repeat;
    mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' d='M4 8h8'/></svg>") center / 14px 14px no-repeat;
}

.av-data-table .av-table-check:disabled,
.av-data-table input[type="checkbox"]:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

/* Center checkbox cells in both header and body so the box sits centred
   in the column rather than floating against the left padding. */
.av-data-table thead th:has(> input[type="checkbox"]),
.av-data-table tbody td:has(> input[type="checkbox"]) {
    text-align: center;
    padding-left: 12px;
    padding-right: 12px;
}

.av-data-table-filter-row th,
.av-data-table .filter-row-th th {
    height: 38px;
    padding: 6px 14px;
    background: var(--panel);
    cursor: default;
    user-select: auto;
}

.av-data-table-filter-wrap,
.av-data-table .th-filter-wrap {
    display: flex;
    align-items: center;
    gap: 6px;
    width: 100%;
    min-width: 0;
    height: 28px;
    padding: 0 8px;
    border-bottom: 1px solid var(--line);
    color: var(--muted);
}

.av-data-table .th-filter,
.av-data-table-filter {
    width: 100%;
    min-width: 0;
    height: 28px;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: var(--ink);
    font: 500 12px var(--font-text);
    outline: none;
}

.av-data-table select.th-filter,
.av-data-table select.av-data-table-filter {
    padding: 0 18px 0 0;
    border-bottom: 1px solid var(--line);
    color: var(--ink-2);
}

.av-data-table .th-filter::placeholder,
.av-data-table-filter::placeholder {
    color: var(--muted);
}

.av-data-table .th-filter:focus-visible,
.av-data-table-filter:focus-visible,
.av-data-table .th-filter-wrap:focus-within,
.av-data-table-filter-wrap:focus-within {
    border-color: var(--accent);
}

.av-data-table-empty-row td,
.av-admin-grid-empty-row td,
.av-table-empty-row td {
    padding: 34px 16px;
    color: var(--muted);
    text-align: center;
}

.av-data-table-empty-state.av-empty-state {
    padding: 0;
    background: transparent;
    border: 0;
}

.av-data-table-empty-title,
.av-admin-grid-empty-title {
    margin-bottom: 4px;
    color: var(--ink);
    font-weight: 650;
}

.av-data-table-empty-hint,
.av-admin-grid-empty-hint {
    color: var(--muted);
}

.av-data-table-toolbar,
.av-admin-grid-toolbar {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 12px;
}

.av-api-key-top-filter.av-data-table-toolbar {
    margin-bottom: 0;
    align-items: center;
}

.av-data-table-toolbar .av-search,
.av-data-table-toolbar .av-input,
.av-admin-grid-search {
    min-width: 220px;
    max-width: 420px;
    flex: 1 1 260px;
}

.av-data-table-footer,
.av-admin-grid-footer,
.av-api-key-pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 14px;
    border-top: 1px solid var(--line-2);
    color: var(--muted);
    font-size: 12px;
}

.av-data-table--compact tbody td,
.av-table-compact.av-data-table tbody td {
    padding-top: 8px;
    padding-bottom: 8px;
}

.av-data-table--wide {
    min-width: 920px;
}

.av-data-table--dense {
    min-width: 760px;
}

.av-data-table--import {
    min-width: 780px;
}

.av-data-table--capabilities {
    min-width: 620px;
}

.av-data-table--clickable tbody tr {
    cursor: pointer;
}

.av-data-table-group-row td {
    padding: 8px 14px;
    background: var(--bg-warm);
    color: var(--ink-2);
    font-size: 12px;
    font-weight: 650;
}

.av-data-table-cell-muted,
.av-data-table .meta-cell,
.av-data-table .av-td-muted {
    color: var(--muted);
    font-size: 12px;
}

.av-data-table-cell-mono,
.av-data-table .key-id,
.av-data-table .av-td-id,
.av-data-table code {
    font-family: var(--font-mono);
    font-size: 11.5px;
    letter-spacing: 0;
}

.av-data-grid {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    overflow: hidden;
}

.av-data-grid-header {
    background: var(--bg-warm);
    border-bottom: 1px solid var(--line);
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10.5px;
    font-weight: 600;
    letter-spacing: 0;
    text-transform: uppercase;
}

.av-data-grid-row {
    border-bottom: 1px solid var(--line-2);
}

.av-data-grid-row:last-child {
    border-bottom: 0;
}

.av-log-data-table-shell .av-log-table {
    border: 0;
    border-radius: 0;
}

.av-log-data-table-shell .av-log-table-header {
    border-bottom-color: var(--line);
}

@media (max-width: 760px) {
    .av-data-table-shell {
        border-radius: 8px;
    }

    .av-data-table {
        min-width: 720px;
    }

    .av-data-table-toolbar,
    .av-admin-grid-toolbar {
        align-items: stretch;
    }

    .av-data-table-toolbar .av-search,
    .av-data-table-toolbar .av-input,
    .av-admin-grid-search,
    .av-data-table-footer,
    .av-admin-grid-footer,
    .av-api-key-pagination {
        flex: 1 1 100%;
        width: 100%;
    }

    .av-data-table-footer,
    .av-admin-grid-footer,
    .av-api-key-pagination {
        align-items: stretch;
        flex-direction: column;
    }
}

/* ────────────────────────────────────────────────────────────────────────────
   Responsive (stacked-card) mode — opt-in via .av-data-table--responsive.
   Each row collapses to a vertical card, every <td> shows its column header
   (from `data-label`) above the cell value. Filter row turns into a vertical
   toolbar above the cards. Header row is hidden because labels are duplicated
   per cell.

   Used by every renderDataTable consumer by default; pass `responsive: false`
   in the config to opt out (e.g. log streams that need to stay tabular).
*/
@media (max-width: 760px) {
    .av-data-table.av-data-table--responsive {
        min-width: 0;
        width: 100%;
        display: block;
    }

    .av-data-table.av-data-table--responsive thead {
        display: block;
        background: transparent;
    }

    /* Hide the column-title row on mobile — labels are repeated per cell. */
    .av-data-table.av-data-table--responsive thead tr:first-child {
        display: none;
    }

    /* Filter row becomes a vertical stack of inputs above the card list.
       Sticky so users can refine filters while scrolling through long lists. */
    .av-data-table.av-data-table--responsive thead tr.filter-row-th {
        display: flex;
        flex-direction: column;
        gap: 8px;
        padding: 10px 12px;
        background: var(--bg-warm);
        border-bottom: 1px solid var(--line);
        position: sticky;
        top: 0;
        z-index: 5;
        backdrop-filter: saturate(140%) blur(8px);
        -webkit-backdrop-filter: saturate(140%) blur(8px);
    }

    .av-data-table.av-data-table--responsive thead tr.filter-row-th th {
        display: block;
        height: auto;
        padding: 0;
        background: transparent;
    }

    /* Filter inputs span the full width on mobile — easier to tap. */
    .av-data-table.av-data-table--responsive .av-data-table-filter-wrap,
    .av-data-table.av-data-table--responsive .th-filter-wrap {
        height: 40px;
        background: var(--panel);
        border: 1px solid var(--line);
        border-radius: 8px;
        padding: 0 10px;
    }

    .av-data-table.av-data-table--responsive .th-filter,
    .av-data-table.av-data-table--responsive .av-data-table-filter {
        height: 38px;
        font-size: 14px;
    }

    .av-data-table.av-data-table--responsive select.th-filter,
    .av-data-table.av-data-table--responsive select.av-data-table-filter {
        width: 100%;
        height: 40px;
        padding: 0 12px;
        background: var(--panel);
        border: 1px solid var(--line);
        border-radius: 8px;
        color: var(--ink);
    }

    /* Each row becomes a card with proper surface treatment so individual items
       feel like discrete tap targets rather than rows of a spreadsheet. */
    .av-data-table.av-data-table--responsive tbody {
        display: block;
        padding: 12px;
        background: var(--bg-warm);
    }

    .av-data-table.av-data-table--responsive tbody tr {
        display: grid;
        grid-template-columns: 1fr;
        gap: 6px;
        padding: 14px 16px;
        margin-bottom: 10px;
        background: var(--panel);
        border: 1px solid var(--line);
        border-radius: var(--r-md, 10px);
        box-shadow: 0 1px 2px oklch(0.18 0.012 255 / 0.04);
    }

    .av-data-table.av-data-table--responsive tbody tr:last-child {
        margin-bottom: 0;
    }

    .av-data-table.av-data-table--responsive tbody tr:hover > td {
        background: transparent;
    }

    /* Cell renders as a label-above-value pair on mobile. The label sits in
       the left column (mono small caps), the value fills the right column. */
    .av-data-table.av-data-table--responsive tbody td {
        display: grid;
        grid-template-columns: minmax(72px, 25%) 1fr;
        align-items: center;
        gap: 12px;
        padding: 4px 0;
        border-bottom: 0;
        text-align: left !important;
        min-height: 0;
    }

    .av-data-table.av-data-table--responsive tbody td::before {
        content: attr(data-label);
        font-family: var(--font-mono);
        font-size: 10px;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: var(--muted);
        font-weight: 600;
    }

    /* Columns with empty labels (icon / avatar columns) drop entirely on
       mobile — the card identity is carried by the first labeled column. */
    .av-data-table.av-data-table--responsive tbody td[data-label=""] {
        display: none;
    }

    /* Cells without any data-label attribute (legacy inline tables that have
       not been retrofitted yet) span the full card width with no label
       column. Backward-compatible fallback so existing tables don't break. */
    .av-data-table.av-data-table--responsive tbody td:not([data-label]) {
        grid-template-columns: 1fr;
    }

    .av-data-table.av-data-table--responsive tbody td:not([data-label])::before {
        display: none;
    }

    /* Self-explanatory cells don't need a label. A standalone badge already
       carries meaning ("ONLINE", "OFFLINE", "LIZENZ AUTO ZUGEWIESEN"); same
       for cells whose only content is action buttons. Hide the ::before
       label and let the value span the full width. */
    .av-data-table.av-data-table--responsive tbody td:has(> .av-badge:only-child),
    .av-data-table.av-data-table--responsive tbody td:has(> .av-row-actions:only-child),
    .av-data-table.av-data-table--responsive tbody td:has(> .av-member-role-badge:only-child) {
        grid-template-columns: 1fr;
    }

    .av-data-table.av-data-table--responsive tbody td:has(> .av-badge:only-child)::before,
    .av-data-table.av-data-table--responsive tbody td:has(> .av-row-actions:only-child)::before,
    .av-data-table.av-data-table--responsive tbody td:has(> .av-member-role-badge:only-child)::before {
        display: none;
    }

    /* The "name" / first labeled column gets emphasis treatment so the card
       has a clear primary line. */
    .av-data-table.av-data-table--responsive tbody td.name-cell {
        grid-template-columns: 1fr;
        padding-bottom: 10px;
        margin-bottom: 6px;
        border-bottom: 1px solid var(--line-2);
    }

    .av-data-table.av-data-table--responsive tbody td.name-cell::before {
        display: none;
    }

    /* Action buttons get bigger tap targets on mobile to clear the 44 px
       Apple HIG / Material guideline. Buttons inside a row's action cell
       align right by default; on very narrow screens they wrap. */
    .av-data-table.av-data-table--responsive tbody .av-row-actions {
        gap: 8px;
        flex-wrap: wrap;
        justify-content: flex-end;
    }

    .av-data-table.av-data-table--responsive tbody .av-row-actions .av-btn {
        min-height: 40px;
        padding-left: 14px;
        padding-right: 14px;
        font-size: 13px;
    }

    /* Empty-state row (the "no matches" inline state) doesn't try to mimic
       a row layout — it just centres its message inside the card surface. */
    .av-data-table.av-data-table--responsive tbody tr.av-data-table-empty-row {
        display: block;
        padding: 0;
    }

    .av-data-table.av-data-table--responsive tbody tr.av-data-table-empty-row td {
        display: block;
        padding: 24px 16px;
    }

    .av-data-table.av-data-table--responsive tbody tr.av-data-table-empty-row td::before {
        display: none;
    }

    /* On mobile the cards already carry the surface (background + border +
       radius), so the outer shell becomes a transparent canvas. */
    .av-data-table-shell:has(.av-data-table--responsive) {
        border: 0;
        border-radius: 0;
        background: transparent;
        overflow: visible;
    }

    .av-data-table-shell:has(.av-data-table--responsive) .av-data-table-scroll {
        background: transparent;
        overflow-x: visible;
    }
}
