/**
 * @package         Joomla.Site
 * @subpackage      com_nextdb
 *
 * @copyright       (C) 2026 NextSoftware Team <https://nextsoftware.dev/>
 * @license         GNU General Public License version 2 or later; see LICENSE.txt
 *
 * Frontend Data View styles. The table chrome (layout, sort icons, paging, search box, info text,
 * processing/empty states) now comes from DataTables' Bootstrap 5 integration + Cassiopeia's `.table`.
 * Only the app-specific cell chrome (chips, booleans, images, the detail panel) and print rules live
 * here, plus the per-column range-filter layout.
 */

.nextdb-dv {
    width: 100%;
}

.nextdb-dv-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.nextdb-dv-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.nextdb-dv .dt-container > .row:first-child .nextdb-dv-toolbar {
    margin-bottom: 0;
}

.nextdb-dv-status:empty {
    display: none;
}

/* Toolbar dropdowns: native <details>, so they work with any template's scripts. */
.nextdb-dv-menu {
    position: relative;
}

.nextdb-dv-menu > summary {
    list-style: none;
}

.nextdb-dv-menu > summary::-webkit-details-marker {
    display: none;
}

.nextdb-dv-menu > summary::after {
    content: " \25BE";
}

.nextdb-dv-menu-panel {
    position: absolute;
    right: 0;
    z-index: 20;
    min-width: 12rem;
    max-height: 60vh;
    margin-top: 0.25rem;
    padding: 0.25rem 0;
    overflow-y: auto;
    background: var(--bs-body-bg, #fff);
    border: 1px solid var(--bs-border-color, #dee2e6);
    border-radius: 0.375rem;
    box-shadow: 0 0.25rem 0.75rem rgba(0, 0, 0, 0.15);
}

/* Styled here because not every template loads Bootstrap's dropdown styles. */
.nextdb-dv-menu-panel .dropdown-item {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 0.4rem 1rem;
    color: inherit;
    text-align: left;
    white-space: nowrap;
    background: transparent;
    border: 0;
    cursor: pointer;
}

.nextdb-dv-menu-panel .dropdown-item:hover,
.nextdb-dv-menu-panel .dropdown-item:focus-visible {
    background: var(--bs-tertiary-bg, #f1f3f5);
}

.nextdb-dv-column-option {
    gap: 0.5rem;
}

.nextdb-dv-column-option .form-check-input {
    margin: 0;
}

/* Fullscreen: real fullscreen where allowed, otherwise an overlay filling the window. */
.nextdb-dv-is-fullscreen {
    position: fixed;
    inset: 0;
    z-index: 1050;
    padding: 1rem;
    overflow: auto;
    background: var(--bs-body-bg, #fff);
}

.nextdb-dv-is-fullscreen .nextdb-dv-heading,
.nextdb-dv-is-fullscreen .nextdb-dv-description {
    display: none;
}

.nextdb-dv-no-scroll {
    overflow: hidden;
}

/* A wide table scrolls inside its own area instead of widening the page. */
.nextdb-dv .dt-layout-table > div {
    overflow-x: auto;
}

/* Per-column "from / to" range filters (numeric + date). */
.nextdb-dv-range {
    display: flex;
    gap: 0.25rem;
}

/* Detail child-row panel (rendered by ViewRenderer::renderDetail). */
.nextdb-dv-detail {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 0.25rem 1rem;
    margin: 0;
}

.nextdb-dv-detail dt {
    font-weight: 600;
}

.nextdb-dv-detail dd {
    margin: 0;
}

/* App-specific cell renderings emitted by ViewRenderer. */
.nextdb-dv-chip {
    display: inline-block;
    padding: 0.1rem 0.5rem;
    border-radius: 999px;
    font-size: 0.85em;
    line-height: 1.6;
    background: rgba(0, 0, 0, 0.08);
}

.nextdb-dv-bool.is-true {
    color: #2e7d32;
}

.nextdb-dv-bool.is-false {
    color: #b0b0b0;
}

.nextdb-dv-img {
    max-width: 100%;
    height: auto;
    object-fit: cover;
    border-radius: 4px;
}

.nextdb-dv-noscript {
    padding: 1rem;
    text-align: center;
    opacity: 0.7;
}

/* Auto-refresh: changed rows or cells fade from the view's colour. */
tr.nextdb-dv-flash > td,
td.nextdb-dv-flash {
    animation: nextdb-dv-flash var(--nextdb-dv-flash-duration, 2000ms) ease-out;
}

@keyframes nextdb-dv-flash {
    from {
        background-color: var(--nextdb-dv-flash-color, #fff3cd);
    }
}

@media (prefers-reduced-motion: reduce) {
    tr.nextdb-dv-flash > td,
    td.nextdb-dv-flash {
        animation: none;
    }
}

/* Sort and filter controls for the phone layout; the table header does this job on larger screens. */
.nextdb-dv-phonebar {
    display: none;
}

/*
 * Phones: each row becomes a card with the column label beside its value, since a wide table can't fit.
 * Keep this breakpoint in step with PHONE_QUERY in dataview.js.
 */
@media screen and (max-width: 767.98px) {
    .nextdb-dv .dt-container > .row {
        --bs-gutter-x: 0;
        margin-right: 0;
        margin-left: 0;
    }

    .nextdb-dv .dt-container > .row > div {
        padding-right: 0;
        padding-left: 0;
    }

    /* Toolbar */
    .nextdb-dv .dt-container > .row:first-child {
        gap: 0.5rem;
    }

    .nextdb-dv .dt-container > .row:first-child > div {
        width: 100%;
        padding: 0;
        margin: 0;
    }

    .nextdb-dv .dt-search {
        width: 100%;
        margin: 0;
        text-align: left;
    }

    .nextdb-dv .dt-search label {
        display: none;
    }

    /* 16px stops iOS zooming the page when a field gets focus. */
    .nextdb-dv .dt-search input,
    .nextdb-dv-phonebar select,
    .nextdb-dv-filter-row input,
    .nextdb-dv-filter-row select {
        display: block;
        width: 100% !important;
        min-height: 44px;
        margin: 0 !important;
        font-size: 16px;
    }

    .nextdb-dv-toolbar {
        margin: 0;
    }

    .nextdb-dv-actions {
        width: 100%;
    }

    .nextdb-dv-actions > * {
        flex: 1 1 0;
    }

    .nextdb-dv-actions .btn {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        min-height: 44px;
        white-space: nowrap;
    }

    .nextdb-dv-fullscreen {
        display: none !important;
    }

    .nextdb-dv-phonebar {
        display: flex;
        gap: 0.5rem;
        margin-top: 0.5rem;
    }

    .nextdb-dv-phonebar select {
        flex: 1 1 auto;
        min-width: 0;
    }

    .nextdb-dv-phonebar .btn {
        flex: 0 0 auto;
        min-height: 44px;
    }

    /* Toolbar menus open as a bottom sheet. */
    .nextdb-dv-menu[open] > summary::before {
        position: fixed;
        inset: 0;
        z-index: 1059;
        content: "";
        background: rgba(0, 0, 0, 0.4);
    }

    .nextdb-dv-menu-panel {
        position: fixed;
        top: auto;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: 1060;
        min-width: 0;
        max-height: 70vh;
        margin: 0;
        padding: 0.5rem 0 calc(0.5rem + env(safe-area-inset-bottom));
        border-width: 1px 0 0;
        border-radius: 1rem 1rem 0 0;
    }

    .nextdb-dv-menu-panel .dropdown-item {
        display: flex;
        align-items: center;
        min-height: 48px;
        padding: 0.5rem 1.25rem;
        white-space: normal;
    }

    /* Filters collapse behind a button. */
    .nextdb-dv table.nextdb-dv-table,
    .nextdb-dv table.nextdb-dv-table > thead,
    .nextdb-dv table.nextdb-dv-table > tbody {
        display: block;
        width: 100% !important;
        border: 0;
    }

    .nextdb-dv table.nextdb-dv-table > colgroup,
    .nextdb-dv table.nextdb-dv-table > thead > tr:not(.nextdb-dv-filter-row),
    .nextdb-dv table.nextdb-dv-table > thead > tr.nextdb-dv-filter-row,
    .nextdb-dv table.nextdb-dv-table > thead th.is-empty {
        display: none;
    }

    .nextdb-dv.nextdb-dv-filters-open table.nextdb-dv-table > thead > tr.nextdb-dv-filter-row {
        display: block;
        margin-top: 0.5rem;
        padding: 0.75rem;
        background: var(--bs-tertiary-bg, #f8f9fa);
        border-radius: 0.5rem;
    }

    .nextdb-dv table.nextdb-dv-table .nextdb-dv-filter-row th {
        display: block;
        width: 100% !important;
        padding: 0 0 0.75rem;
        text-align: left;
        border: 0;
    }

    .nextdb-dv-filter-row th[data-label]::before {
        display: block;
        margin-bottom: 0.25rem;
        font-size: 0.875rem;
        content: attr(data-label);
    }

    /* Row cards */
    .nextdb-dv table.nextdb-dv-table > tbody > tr {
        display: block;
        margin-top: 0.75rem;
        padding: 0.25rem 0.75rem;
        background: var(--bs-body-bg, #fff);
        border: 1px solid var(--bs-border-color, #dee2e6);
        border-radius: 0.5rem;
    }

    .nextdb-dv table.nextdb-dv-table > tbody > tr > td {
        display: flex;
        gap: 1rem;
        align-items: baseline;
        justify-content: space-between;
        padding: 0.5rem 0;
        text-align: right;
        overflow-wrap: anywhere;
        background: transparent !important;
        border-width: 0 0 1px;
        border-style: solid;
        border-color: var(--bs-border-color-translucent, rgba(0, 0, 0, 0.1));
        box-shadow: none !important;
    }

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

    .nextdb-dv table.nextdb-dv-table > tbody > tr > td[data-label]::before {
        flex: 0 0 35%;
        font-weight: 600;
        text-align: left;
        content: attr(data-label);
    }

    /* The details toggle always sits at the bottom of its card. */
    .nextdb-dv table.nextdb-dv-table > tbody > tr > td.dt-control {
        order: 99;
        border-bottom: 0;
        justify-content: flex-start;
        gap: 0.5rem;
        min-height: 44px;
        align-items: center;
        color: var(--bs-link-color, #0d6efd);
        cursor: pointer;
    }

    .nextdb-dv table.nextdb-dv-table > tbody > tr > td.dt-control::after {
        content: attr(data-details);
    }

    .nextdb-dv table.nextdb-dv-table > tbody > tr:has(> td.dt-control) {
        display: flex;
        flex-direction: column;
    }

    /* With the toggle moved to the bottom, the last column still needs its divider. */
    .nextdb-dv table.nextdb-dv-table > tbody > tr:has(> td.dt-control) > td:last-child {
        border-bottom: 1px solid var(--bs-border-color-translucent, rgba(0, 0, 0, 0.1));
    }

    /* The open detail panel joins the card above it. */
    .nextdb-dv table.nextdb-dv-table > tbody > tr.dt-hasChild {
        margin-bottom: 0;
        border-bottom: 0;
        border-radius: 0.5rem 0.5rem 0 0;
    }

    .nextdb-dv table.nextdb-dv-table > tbody > tr.dt-hasChild + tr {
        margin-top: 0;
        border-top: 0;
        border-radius: 0 0 0.5rem 0.5rem;
    }

    .nextdb-dv table.nextdb-dv-table > tbody > tr > td[colspan] {
        display: block;
        text-align: left;
    }

    .nextdb-dv-detail {
        grid-template-columns: 40% 1fr;
    }

    .nextdb-dv-detail dd {
        overflow-wrap: anywhere;
    }

    /* Rows-per-page, count and paging stack in the centre. */
    .nextdb-dv .dt-container > .row:last-child > div {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        align-items: center;
        width: 100%;
        margin-top: 0.75rem;
        padding: 0;
        text-align: center;
    }

    .nextdb-dv .dt-paging .pagination {
        flex-wrap: wrap;
        justify-content: center;
    }

    .nextdb-dv .dt-paging .page-link {
        min-width: 44px;
        min-height: 44px;
        line-height: 1.75;
        text-align: center;
    }
}

.nextdb-dv-print-sheet {
    display: none;
}

@media print {
    .nextdb-dv-toolbar,
    .nextdb-dv-filter-row,
    .nextdb-dv .dt-search,
    .nextdb-dv .dt-info,
    .nextdb-dv .dt-paging,
    .nextdb-dv .dt-length,
    td.dt-control {
        display: none !important;
    }

    /* The Print button shows only its own sheet, without the site template. */
    body.nextdb-dv-printing > :not(.nextdb-dv-print-sheet) {
        display: none !important;
    }

    body.nextdb-dv-printing > .nextdb-dv-print-sheet {
        display: block;
    }

    .nextdb-dv-print-title {
        margin: 0 0 0.5rem;
        font-size: 1.5rem;
    }

    .nextdb-dv-print-description,
    .nextdb-dv-print-meta,
    .nextdb-dv-print-note {
        margin: 0 0 0.25rem;
        font-size: 0.85rem;
    }

    .nextdb-dv-print-note {
        font-weight: 600;
    }

    .nextdb-dv-print-sheet table {
        width: 100%;
        margin-top: 0.75rem;
        border-collapse: collapse;
        font-size: 0.8rem;
    }

    .nextdb-dv-print-sheet thead {
        display: table-header-group;
    }

    .nextdb-dv-print-sheet tr {
        break-inside: avoid;
    }

    .nextdb-dv-print-sheet th,
    .nextdb-dv-print-sheet td {
        padding: 0.25rem 0.4rem;
        border: 1px solid #999;
        text-align: left;
        vertical-align: top;
    }
}
