/* Shared phone and tablet rules (Mobile-Friendly Plan, phase 2). Loaded after Bootstrap, Telerik
   and app.css, on every layout. Desktop with a mouse looks the same as before: the tap-size rules
   only apply to touch screens, and the rest only below the widths named. */

/* ---- Page headers: title + actions ------------------------------------------------------------
   Most pages use .page-header > .container(-fluid) > .d-flex.justify-content-between with the title
   block first and the action buttons after. On a phone the actions drop under the title and share
   the full width, so they never squeeze the title onto two lines. PageHeaderComp does the same. */
@media (max-width: 575.98px) {
    .page-header .d-flex.justify-content-between,
    .page-header-comp {
        flex-wrap: wrap;
        align-items: stretch !important;
        gap: 0.75rem;
    }

    .page-header .d-flex.justify-content-between > :first-child,
    .page-header-comp > .page-header-title {
        flex: 1 1 100%;
        min-width: 0;
    }

    .page-header .d-flex.justify-content-between > :not(:first-child),
    .page-header-comp > .page-header-actions {
        display: flex;
        flex: 1 1 100%;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .page-header .d-flex.justify-content-between > :not(:first-child) > *,
    .page-header-comp > .page-header-actions > * {
        flex: 1 1 auto;
        justify-content: center;
    }

    /* A button group laid out above keeps its children's rounded corners. */
    .page-header .btn-group > .btn,
    .page-header-comp .btn-group > .btn {
        border-radius: var(--bs-btn-border-radius) !important;
    }
}

/* ---- Wide content scrolls in its own box ------------------------------------------------------
   Tables sit in .table-responsive; this gives the scroll a visible edge so it's clear there is more. */
.table-responsive {
    background:
        linear-gradient(to right, #fff 30%, rgba(255, 255, 255, 0)) left / 2rem 100% no-repeat local,
        linear-gradient(to left, #fff 30%, rgba(255, 255, 255, 0)) right / 2rem 100% no-repeat local,
        radial-gradient(farthest-side at 0 50%, rgba(0, 0, 0, 0.18), transparent) left / 0.75rem 100% no-repeat scroll,
        radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, 0.18), transparent) right / 0.75rem 100% no-repeat scroll;
}

.k-grid {
    max-width: 100%;
}

/* ---- Tap targets: about 44 px on touch screens -------------------------------------------------- */
@media (pointer: coarse) {
    .btn,
    .k-button,
    .page-link,
    .dropdown-item,
    .list-group-item-action,
    .nav-tabs .nav-link,
    .k-tabstrip-item .k-link {
        min-height: 44px;
    }

    .btn:not(.d-block):not(.btn-link) {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.25rem;
    }

    /* Icon-only buttons (card menus, move up/down) get a square 44 px target. */
    .k-button.k-icon-button,
    .btn.btn-icon {
        min-width: 44px;
    }

    .form-control,
    .form-select,
    .k-input,
    .k-picker {
        min-height: 44px;
    }

    .form-check-input {
        width: 1.35em;
        height: 1.35em;
    }

    .form-check {
        min-height: 44px;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .form-check .form-check-input {
        float: none;
        margin: 0;
        flex-shrink: 0;
    }

    .form-check.form-check-inline {
        display: inline-flex;
    }
}

/* ---- Forms ------------------------------------------------------------------------------------ */
@media (max-width: 575.98px) {
    /* A card inside a card loses the inner side borders and padding, so the margins aren't paid twice. */
    .card .card {
        border-left: 0;
        border-right: 0;
        border-radius: 0;
    }

    .card .card > .card-body,
    .card .card > .card-header,
    .card .card > .card-footer {
        padding-left: 0;
        padding-right: 0;
    }

    .card > .card-body {
        padding: 1rem 0.75rem;
    }
}

/* A Save bar for long forms: stays at the bottom of the screen, above the shortcuts bar on phones
   and tablets. Put the form's main buttons in <div class="sticky-save-bar">. */
.sticky-save-bar {
    position: sticky;
    bottom: 0;
    z-index: 1020;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 0.5rem;
    padding: 0.75rem 0;
    background: #fff;
    border-top: 1px solid #dee2e6;
}

@media (max-width: 991.98px) {
    .sticky-save-bar {
        bottom: calc(var(--bottom-nav-height, 0px) + env(safe-area-inset-bottom, 0px));
    }

    .sticky-save-bar > .btn,
    .sticky-save-bar > .k-button {
        flex: 1 1 auto;
    }
}

/* ---- Card lists: one column on phones, two from 768 px, three from 1200 px ---------------------- */
.card-list {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 1rem;
}

@media (min-width: 768px) {
    .card-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (min-width: 1200px) {
    .card-list {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

/* ---- Maps: full width, never taller than the screen allows -------------------------------------- */
.leaflet-container {
    max-width: 100%;
}

@media (max-width: 991.98px) {
    .map-view-container {
        max-height: 55vh;
        min-height: 240px;
    }
}

/* Leaflet's own controls (zoom, layer switcher) get a 44 px target on touch screens. */
@media (pointer: coarse) {
    .leaflet-touch .leaflet-bar a {
        width: 44px;
        height: 44px;
        line-height: 44px;
    }

    .leaflet-touch .leaflet-control-layers-toggle {
        width: 44px;
        height: 44px;
    }

    .leaflet-control-layers-base label {
        min-height: 36px;
        display: flex;
        align-items: center;
    }
}

/* ---- Page side margins on phones ------------------------------------------------------------------
   Pages use .container-fluid.px-4 (24 px each side); on a 390 px phone that's an eighth of the width. */
@media (max-width: 575.98px) {
    .container-fluid.px-4 {
        padding-left: 0.75rem !important;
        padding-right: 0.75rem !important;
    }
}

/* ---- Stacked tables: below 768 px each row of a <table class="table-stack"> becomes a card and each
   cell a "Label  value" line from its data-label. For tables whose columns all matter (loads,
   incoming check-ins), where a sideways-scrolling table would hide most of them. */
@media (max-width: 767.98px) {
    .table-stack thead {
        display: none;
    }

    .table-stack,
    .table-stack tbody,
    .table-stack tr,
    .table-stack td {
        display: block;
        width: 100%;
    }

    .table-stack tr {
        width: auto;
        margin: 0.75rem;
        border: 1px solid #dee2e6;
        border-radius: 0.5rem;
        overflow: hidden;
    }

    .table-stack td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1rem;
        text-align: right;
        white-space: normal !important;
        border-bottom: 1px solid #f1f3f5;
        padding: 0.4rem 0.75rem;
    }

    .table-stack td:last-child {
        border-bottom: 0;
    }

    .table-stack td::before {
        content: attr(data-label);
        flex: 0 0 auto;
        text-align: left;
        font-weight: 600;
        color: #6c757d;
    }

    .table-stack td[data-label=""] {
        justify-content: flex-end;
    }

    .table-stack td[data-label=""]::before {
        content: none;
    }
}
