/* ===== Card View (responsive mobile table) ===== */
@media (max-width: 599.98px) {

    /*
     * Layout cells — cells with their own display/order/layout.
     * When adding a new layout cell type, add it here so generic
     * data-cell rules (background, tabular-nums, order, text-align) skip it.
     *
     * ALL_LAYOUT = .cell-name, .cell-image, .cell-actions, .cell-balance,
     *              .cell-meta, .cell-description
     */

    /* ── Container & table reset ── */

    .card-view .mud-table-container,
    .card-view .mud-table-container * {
        box-sizing: border-box;
    }

    /* Hide thead (keep in DOM for sort clicks) */
    .card-view .mud-table-container > table > thead {
        position: absolute !important;
        width: 1px !important;
        height: 1px !important;
        overflow: hidden !important;
        clip: rect(0, 0, 0, 0) !important;
        white-space: nowrap !important;
        border: 0 !important;
        padding: 0 !important;
        margin: -1px !important;
    }

    /* Hide standard tfoot on mobile to prefer top-totals cards */
    .card-view .mud-table-container > table > tfoot {
        display: none !important;
    }

    .card-view .mud-table-container {
        overflow-x: hidden;
        padding: 0 8px;
        width: 100%;
        max-width: 100%;
    }

    .card-view .mud-table-container > table {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        table-layout: auto !important;
        display: block;
        border: none;
        margin: 0 !important;
        padding: 0 !important;
    }

    .card-view .mud-table-container > table > tbody {
        display: block;
        width: 100%;
    }

    /* ── Card row: 2-column grid ── */

    .card-view .mud-table-container > table > tbody > tr {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 4px 12px;
        padding: 8px 10px;
        margin-bottom: 12px;
        width: 100%;
        max-width: 100%;
        background-color: var(--mud-palette-surface);
        border: 1px solid rgba(var(--mud-palette-primary-rgb), 0.35);
        border-radius: 6px;
        box-shadow: 0 2px 6px rgba(0,0,0,0.08);
    }

    /* ── Base cell: label:value grid ── */
    /* !important overrides MudBlazor's default table-cell display */

    .card-view .mud-table-container > table > tbody > tr > td {
        display: grid !important;
        grid-template-columns: auto minmax(0, 1fr);
        column-gap: 8px;
        align-items: center;
        justify-content: space-between;
        text-align: left !important;
        padding: 6px 8px;
        white-space: normal !important;
        min-width: 0;
        border-bottom: none !important;
        border-radius: 4px;
        font-size: 0.95rem;
    }

    .card-view .mud-table-container > table > tbody > tr > td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.85rem;
        color: var(--mud-palette-text-secondary);
        min-width: 0;
        display: block;
        white-space: normal;
        margin-right: 4px;
    }

    .card-view .mud-table-container > table > tbody > tr > td[data-label=""]::before {
        display: none !important;
    }

    /* hide-label: icon-based label cells — flex layout matching cell-meta */
    .card-view .mud-table-container > table > tbody > tr > td.hide-label {
        display: flex !important;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        padding: 4px 8px !important;
    }
    .card-view .mud-table-container > table > tbody > tr > td.hide-label::before {
        display: none !important;
    }

    .card-view .mud-table-container > table > tbody > tr > td > * {
        min-width: 0;
    }

    /* ── Data cells: background, numeric, right-align, default order ── */
    /* Uses ONE exclusion list (ALL_LAYOUT) so new layout cells only need one update */

    .card-view .mud-table-container > table > tbody > tr > td:not(:is(.cell-name, .cell-image, .cell-actions, .cell-balance, .cell-meta, .cell-description)) {
        background-color: rgba(var(--mud-palette-primary-rgb), 0.12) !important;
        font-variant-numeric: tabular-nums;
        order: 0;
    }

    .card-view .mud-table-container > table > tbody > tr > td:not(:is(.cell-name, .cell-image, .cell-actions, .cell-balance, .cell-meta, .cell-description)) > * {
        justify-self: end;
        text-align: right;
    }

    /* ── Layout cells: flex instead of grid ── */

    .card-view .mud-table-container > table > tbody > tr > td.cell-name,
    .card-view .mud-table-container > table > tbody > tr > td.cell-image,
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions {
        display: flex !important;
    }

    /* ── Date cell (composable — use with cell-meta) ── */

    .card-view .mud-table-container > table > tbody > tr > td.cell-date,
    .card-view .mud-table-container > table > tbody > tr > td.cell-date * {
        white-space: nowrap !important;
        font-size: 0.8rem;
        font-weight: 700;
    }

    /* ── Full-width utility ── */

    .card-view .mud-table-container > table > tbody > tr > td.card-full {
        grid-column: 1 / -1;
    }

    /* ── Description cell: full width, appears last ── */

    .card-view .mud-table-container > table > tbody > tr > td.cell-description {
        grid-column: 1 / -1;
        order: 100;
        display: flex !important;
        flex-direction: row;
        align-items: center;
        justify-content: flex-start;
        text-align: left !important;
        background-color: rgba(var(--mud-palette-primary-rgb), 0.12) !important;
        gap: 0;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-description::before {
        margin-bottom: 0;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-description > * {
        justify-self: start !important;
        text-align: left !important;
    }

    /* ── Name cell: prominent header at top ── */

    .card-view .mud-table-container > table > tbody > tr > td.cell-name {
        grid-column: 1 / -1;
        order: -20;
        padding: 4px 6px 6px;
        margin-bottom: 4px;
        border-bottom: 1px solid var(--mud-palette-lines-default) !important;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-name,
    .card-view .mud-table-container > table > tbody > tr > td.cell-name * {
        font-size: 1.0rem !important;
        font-weight: 700 !important;
    }

    /* Allow flex children of name cell to participate in the cell's flex layout */
    .card-view .mud-table-container > table > tbody > tr > td.cell-name > .name-cell-wrapper {
        display: flex;
        width: 100%;
        align-items: center;
        justify-content: space-between;
        
    }

    /* ── Image cell: right of name ── */

    .card-view .mud-table-container > table > tbody > tr > td.cell-image {
        order: -20;
        justify-content: flex-end !important;
        align-items: center;
        padding: 6px 0 6px 6px;
    }

    /* When image exists: name left, image right on first row */
    .card-view .mud-table-container > table > tbody > tr:has(> td.cell-image) > td.cell-name {
        grid-column: 1;
        order: -21;
        align-self: center;
        justify-content: flex-start !important;
        text-align: left !important;
        border-bottom: none !important;
        margin-bottom: 0;
        padding-bottom: 10px;
    }
    .card-view .mud-table-container > table > tbody > tr:has(> td.cell-image) > td.cell-image {
        grid-column: 2;
        padding-bottom: 10px;
        margin-bottom: 0;
        border-bottom: none !important;
    }
    .card-view .mud-table-container > table > tbody > tr:has(> td.cell-image) {
        position: relative;
    }
    .card-view .mud-table-container > table > tbody > tr:has(> td.cell-image)::before {
        content: '';
        grid-column: 1 / -1;
        order: -19;
        height: 0;
        border-top: 1px solid var(--mud-palette-lines-default);
        margin-bottom: 6px;
    }

    /* ── Meta cell: near top (composable) ── */

    .card-view .mud-table-container > table > tbody > tr > td.cell-meta {
        order: -18;
        grid-column: span 1;
        display: flex !important;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        padding: 4px 8px;
        border-radius: 6px;
        background: rgba(var(--mud-palette-primary-rgb), 0.12);
        white-space: nowrap !important;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-meta > * {
        justify-self: start !important;
        text-align: left !important;
    }

    /* ── Balance cell: full-width badge below name ── */

    .card-view .mud-table-container > table > tbody > tr > td.cell-balance {
        grid-column: 1 / -1;
        order: -18;
        display: flex !important;
        align-items: center;
        justify-content: space-between;
        padding: 8px 10px;
        margin: 2px 0 6px;
        border-radius: 6px;
        background: rgba(var(--mud-palette-primary-rgb), 0.07);
        font-size: 1.1rem;
        font-weight: 700;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-balance::before {
        font-weight: 600;
        font-size: 0.8rem;
        color: var(--mud-palette-text-secondary);
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-balance .mud-typography {
        font-size: 1.15rem;
        font-weight: 700;
    }

    /* ── Actions cell: bottom row, full-width ── */

    .card-view .mud-table-container > table > tbody > tr > td.cell-actions {
        grid-column: 1 / -1;
        order: 100;
        display: block !important;
        width: 100%;
        padding: 8px 0 4px 0 !important;
        border-top: 1px solid var(--mud-palette-lines-default) !important;
        margin-top: 4px;
        white-space: nowrap !important;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions > div,
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions .mud-button-group-root {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 6px;
        flex-wrap: wrap;
        margin: 0 !important;
        padding: 0 !important;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions > div > .card-action-btn,
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions .card-action-menu {
        flex: 1 1 0%;
        min-width: 0;
        margin: 0 !important;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions .card-action-btn,
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions .card-action-menu .mud-button-root {
        width: 100%;
        margin: 0;
        border-radius: 8px;
        padding: 6px 8px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 4px;
        text-transform: none;
        font-size: 0.85rem;
        font-weight: 700;
        line-height: 1;
        text-align: center;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions .card-action-btn .mud-button-label,
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions .card-action-menu .mud-button-root .mud-button-label {
        width: 100%;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 4px;
        text-align: center;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions .mud-button-icon-start {
        margin-right: 2px !important;
        margin-left: 0 !important;
    }
    .card-view .mud-table-container > table > tbody > tr > td.cell-actions .mud-menu .mud-icon-button {
        padding: 4px !important;
        width: auto;
        height: auto;
    }

    /* Hide parent-row unit columns in mobile card view; keep them in expanded child rows */
    .card-view .mud-table-container > table > tbody > tr:not(.child-row) > td.mobile-hide {
        display: none !important;
    }

    /* ── Child rows: compact table-like mobile layout matching desktop ── */

    .card-view .mud-table-container > table > tbody > tr.child-row {
        display: flex !important;
        align-items: center;
        position: relative;
        gap: 0;
        border: none;
        border-radius: 0;
        box-shadow: none;
        margin-bottom: 0;
        padding: 4px 0 4px 8px;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row::before {
        content: "";
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: 4px;
        background: var(--mud-palette-primary);
    }

    /* Hide empty placeholder cells in child rows */
    .card-view .mud-table-container > table > tbody > tr.child-row > td:empty {
        display: none !important;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td {
        display: flex !important;
        align-items: center;
        padding: 4px 6px;
        font-size: 0.85rem;
        line-height: 1.2;
        background: transparent !important;
        border-radius: 0 !important;
        box-shadow: none !important;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td:not(:is(.cell-name, .cell-image, .cell-actions, .cell-balance, .cell-meta, .cell-description)) {
        background: transparent !important;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td::before {
        display: none !important;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) {
        flex: 1 1 auto;
        min-width: 0;
        padding-left: 0;
        justify-content: flex-start;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) > .d-flex {
        display: flex !important;
        align-items: center;
        justify-content: flex-start;
        gap: 6px;
        width: 100%;
        min-width: 0;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) > .d-flex > img {
        display: block !important;
        flex: 0 0 28px !important;
        overflow: hidden !important;
        text-indent: 100% !important;
        white-space: nowrap !important;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) > .d-flex > .mud-icon-button {
        flex: 0 0 28px !important;
        width: 28px !important;
        min-width: 28px !important;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) > .d-flex > span,
    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) > .d-flex > div,
    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) > .d-flex > .mud-typography {
        min-width: 0;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) ~ td {
        flex: 0 0 54px;
        min-width: 54px;
        justify-content: flex-end;
        text-align: right;
        white-space: nowrap;
        padding-left: 3px;
        padding-right: 5px;
    }

    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) ~ td b,
    .card-view .mud-table-container > table > tbody > tr.child-row > td:has(> .d-flex) ~ td .mud-typography {
        white-space: nowrap;
        text-align: right;
    }
}

/* ===== Sort-menu header ===== */
.sort-menu-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 16px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    min-height: 32px;
}

/* ===== Mobile menu normalization (sort, kebab, currency) ===== */

.card-view-menu .mud-menu-item {
    height: 44px !important;
    padding: 6px 12px !important;
    font-size: 0.875rem !important;
    align-items: center !important;
    overflow: hidden;
}

.card-view-menu .mud-menu-item .mud-menu-item-icon {
    min-width: 0 !important;
    margin-inline-end: 8px !important;
}

.card-view-menu .mud-menu-item .mud-icon-root {
    font-size: 1.2rem !important;
}

.card-view-menu .mud-menu-item .mud-checkbox {
    margin: 0 !important;
}

.card-view-menu .mud-menu-item .mud-input-control {
    margin: 0 !important;
    padding: 0 !important;
}

.card-view-menu .mud-menu-item .mud-input-control .mud-input-control-input-container {
    padding: 0 !important;
}

/* Override MudBlazor's auto icon-gutter alignment (36px left margin on items without icons) */
.card-view-menu .mud-menu-item .mud-menu-item-text {
    margin-inline-start: 0 !important;
    margin-inline-end: 0 !important;
}
