/* Phones and small tablets (below Bootstrap's md breakpoint).
 *
 * Only targeted rules: Bootstrap's grid, buttons and inputs are already
 * responsive, so nothing here overrides .row, .col-*, .btn or .form-control
 * for every page. Tables with three or more columns become labelled cards
 * (static/js/responsive-tables.js marks them .table-stack); other tables keep
 * their table layout and scroll sideways inside .table-responsive.
 */

@media (max-width: 767.98px) {
    /* ---- Type ---------------------------------------------------------- */
    h1, .h1 { font-size: 1.6rem; }
    h2, .h2 { font-size: 1.4rem; }
    h3, .h3 { font-size: 1.25rem; }
    h1, h2, h3, h4, h5, h6 { overflow-wrap: break-word; }

    /* 16px inputs: iOS Safari zooms the page on focus below that. */
    .form-control,
    .form-select,
    textarea { font-size: 16px; }

    /* Comfortable tap targets without changing button widths. */
    .btn { min-height: 40px; }
    .btn-sm { min-height: 36px; }
    .btn-sm,
    .btn:has(> i:only-child) { min-width: 36px; }

    /* ---- Headers: title on its own line, actions underneath ------------ */
    .d-flex.justify-content-between:has(> .btn, > .btn-group, > div .btn, > form .btn) {
        flex-wrap: wrap;
        gap: 0.5rem 0.75rem;
    }

    .d-flex.justify-content-between:has(> .btn, > .btn-group, > div .btn, > form .btn)
        > :first-child:not(.btn):not(.btn-group):not(form) {
        flex: 1 1 100%;
        min-width: 0;
    }

    .d-flex.justify-content-between:has(> .btn, > .btn-group, > div .btn, > form .btn)
        > div:last-child:not(:first-child) {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .d-flex.justify-content-between:has(> .btn, > .btn-group, > div .btn, > form .btn)
        > div:last-child:not(:first-child) > .btn {
        margin: 0 !important;
    }

    /* Long identifiers in headings and badges wrap instead of widening the page. */
    .badge { white-space: normal; text-align: left; }

    /* ---- Detail pages: tighter label/value lists ---------------------- */
    dl.row > dt { margin-bottom: 0.1rem; }
    dl.row > dd { margin-bottom: 0.85rem; }

    /* ---- Cards and containers ----------------------------------------- */
    .card-body { padding: 1rem; }
    .card-header { padding: 0.75rem 1rem; }
    .modal-dialog { margin: 0.5rem; }
    .pagination { flex-wrap: wrap; }
    .nav-tabs { flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
    .nav-tabs .nav-link { white-space: nowrap; }
    pre, code { white-space: pre-wrap; word-break: break-word; }
    img, canvas, video { max-width: 100%; }

    /* ---- Tables as cards ---------------------------------------------- */
    .table-responsive:has(> table.table-stack) { overflow: visible; }

    table.table-stack,
    table.table-stack > tbody { display: block; width: 100%; }

    table.table-stack { border: 0; margin-bottom: 0.5rem; }

    table.table-stack > thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    table.table-stack > tbody > tr {
        display: block;
        position: relative;
        margin-bottom: 0.75rem;
        padding: 0.65rem 0.9rem;
        background: var(--neutral-white, #fff);
        border: 1px solid var(--border-color, #dee2e6);
        border-radius: 10px;
        box-shadow: var(--shadow-sm, 0 1px 2px rgba(0, 0, 0, 0.06));
    }

    table.table-stack > tbody > tr.has-select { padding-right: 2.75rem; }

    table.table-stack > tbody > tr > td {
        display: grid;
        grid-template-columns: minmax(6rem, 38%) minmax(0, 1fr);
        column-gap: 0.75rem;
        align-items: baseline;
        justify-items: start;
        padding: 0.3rem 0;
        border: 0;
        background: none;
        box-shadow: none;
        text-align: left !important;
        overflow-wrap: anywhere;
    }

    table.table-stack > tbody > tr > td::before {
        content: attr(data-label);
        color: var(--text-secondary, #6c757d);
        font-size: 0.8rem;
        font-weight: 600;
    }

    table.table-stack > tbody > tr > td:not([data-label])::before,
    table.table-stack > tbody > tr > td[data-label=""]::before { content: none; }

    table.table-stack > tbody > tr > td:not([data-label]),
    table.table-stack > tbody > tr > td[data-label=""] { display: block; }

    table.table-stack > tbody > tr > td.stack-empty { display: none; }

    table.table-stack > tbody > tr > td.stack-title { font-weight: 600; font-size: 1rem; }

    table.table-stack > tbody > tr > td.stack-full {
        display: block;
        text-align: center !important;
    }

    table.table-stack > tbody > tr > td.stack-select {
        position: absolute;
        top: 0.6rem;
        right: 0.75rem;
        display: block;
        padding: 0;
        width: auto;
    }

    table.table-stack > tbody > tr > td.stack-select::before { content: none; }

    table.table-stack > tbody > tr > td.stack-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        margin-top: 0.4rem;
        padding-top: 0.6rem;
        border-top: 1px solid var(--border-color, #dee2e6);
    }

    table.table-stack > tbody > tr > td.stack-actions::before { content: none; }

    table.table-stack > tbody > tr > td.stack-actions > *,
    table.table-stack > tbody > tr > td.stack-actions .btn-group,
    table.table-stack > tbody > tr > td.stack-actions .d-flex {
        display: inline-flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        margin: 0 !important;
    }

    table.table-stack > tbody > tr > td.stack-actions .btn { width: auto; margin: 0 !important; border-radius: 6px !important; }

    /* ---- Cookie notice: compact, never covering the page --------------- */
    #cookieConsentBanner { padding: 0.5rem !important; }
    #cookieConsentBanner .card-body { padding: 0.75rem; }
    #cookieConsentBanner .card-body > div:first-child { margin-bottom: 0.6rem !important; }
    #cookieConsentBanner .btn { flex: 1 1 0; }
}

@media print {
    table.table-stack > thead { position: static; width: auto; height: auto; clip-path: none; }
}

/* Dashboard quick actions: two per row on small phones, long words wrap. */
@media (max-width: 400px) {
    .dashboard-action-icon + div { min-width: 0; hyphens: auto; overflow-wrap: break-word; }
}

/* Touch screens cannot hover, so row actions are always fully visible. */
@media (hover: none) {
    tr .row-actions { opacity: 1; }
}
