/* ==========================================================================
   Ameliorations mobile / PWA.
   TOUT est limite a max-width: 767.98px : le rendu bureau n'est pas modifie.
   ========================================================================== */

/* Elements ajoutes pour le mobile : invisibles sur grand ecran */
.sidebar-backdrop,
.drawer-close {
    display: none;
}

@media (max-width: 767.98px) {

    /* ---------- Structure : une colonne pleine hauteur, seul .content defile ---------- */
    html body .page {
        height: 100vh;
        height: 100dvh;
    }

    html body .page .main {
        display: flex !important;
        flex-direction: column;
        width: 100% !important;
        min-width: 0;
        height: 100vh;
        height: 100dvh;
        margin-left: 0 !important;
    }

    /* Laisse la place a la barre du bas quand elle est affichee */
    body:has(.bottom-nav) .page .main {
        padding-bottom: calc(var(--bottom-nav-h, 64px) + env(safe-area-inset-bottom));
    }

    html body .page .main > .content {
        flex: 1 1 auto;
        height: auto !important;
        min-height: 0;
        padding-left: .75rem !important;
        padding-right: .75rem !important;
    }

    /* ---------- Barre du haut : en-tete d'application (logo + titre a gauche, profil a droite) ---------- */
    html body .page .main > .top-row {
        display: flex !important;
        flex: 0 0 auto;
        flex-wrap: nowrap !important;
        align-items: center;
        height: auto !important;
        min-height: 3.4rem;
        padding: .3rem .85rem !important;
        gap: 10px;
        background: linear-gradient(135deg, #5f9ea0 0%, #3e7f88 100%) !important;
        border-bottom: 0 !important;
        box-shadow: 0 2px 8px rgba(20, 42, 53, .18);
        position: relative;
        z-index: 5;
    }

    /* Logo */
    html body .page .main > .top-row::before {
        content: "";
        order: 0;
        flex: 0 0 auto;
        width: 36px;
        height: 36px;
        border-radius: 10px;
        background: #fff url('../images/tmc.png') center / 78% no-repeat;
        box-shadow: 0 1px 4px rgba(0, 0, 0, .2);
    }

    /* Titre */
    html body .page .main > .top-row::after {
        content: "TMC Transports";
        order: 1;
        flex: 1 1 auto;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        color: #fff;
        font-size: 1.05rem;
        font-weight: 700;
        letter-spacing: .2px;
    }

    html body .page .main > .top-row .navbar-toggle-button {
        display: none !important;
    }

    /* La navigation est assuree par le menu lateral + la barre du bas */
    html body .page .main > .top-row .top-menu-inline {
        display: none !important;
    }

    html body .page .main > .top-row .header-controls {
        order: 2 !important;
        flex: 0 0 auto;
        width: auto !important;
        margin-left: 0 !important;
        min-width: 0;
        gap: 0 !important;
    }

    html body .page .main > .top-row .profile-section {
        padding-left: 0 !important;
        border-left: 0 !important;
        min-width: 0;
    }

    html body .page .main > .top-row .profile-card {
        position: static !important;
        padding: 4px 10px 4px 6px;
        gap: 6px;
        background: rgba(255, 255, 255, .18);
        border-radius: 999px;
    }

    html body .page .main > .top-row .profile-card::before {
        font-family: "bootstrap-icons";
        content: "\f4e1";
        font-size: 1.45rem;
        line-height: 1;
        color: #fff;
    }

    html body .page .main > .top-row .profile-info {
        font-size: .82rem;
        gap: 4px;
        max-width: 38vw;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    html body .page .main > .top-row .username,
    html body .page .main > .top-row .lastname {
        color: #fff !important;
        font-weight: 600;
    }

    /* On garde prenom + nom ; le role (et son tiret) sont dans le profil */
    html body .page .main > .top-row .profile-info > span:nth-child(n+3) {
        display: none;
    }

    /* Pages de connexion / inscription / mot de passe : aucun en-tete (ni nom, ni logo) pour un visiteur non connecte */
    html body .page:has(.signin-card) .main > .top-row {
        display: none !important;
    }

    /* ---------- Menu lateral = tiroir ---------- */
    html body .page > .sidebar {
        position: fixed !important;
        top: 0;
        left: 0;
        bottom: 0;
        height: auto !important;
        width: min(290px, 86vw) !important;
        max-width: none;
        margin-left: 0 !important;
        z-index: 1035;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        box-shadow: 4px 0 16px rgba(0, 0, 0, .3);
    }

    body:has(.bottom-nav) .page > .sidebar {
        bottom: calc(var(--bottom-nav-h, 64px) + env(safe-area-inset-bottom));
    }

    /* Le .sidebar interne (NavMenu) est dans le flux du tiroir */
    html body .page > .sidebar .sidebar {
        position: static !important;
        width: 100% !important;
        height: auto !important;
        margin-left: 0 !important;
        overflow: visible !important;
        box-shadow: none;
        transform: none;
    }

    /* Liste toujours depliee dans le tiroir (le hamburger interne n'a plus lieu d'etre) */
    html body .page > .sidebar .collapse {
        display: block !important;
    }

    html body .page > .sidebar .navbar-toggler {
        display: none !important;
    }

    html body .page > .sidebar .drawer-close {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        margin-left: auto;
        background: rgba(255, 255, 255, .15);
        border: 0;
        border-radius: 6px;
        color: #fff;
        font-size: 1.1rem;
    }

    /* Clic en dehors du tiroir = fermeture (gere dans MainLayout.razor) */
    .sidebar-backdrop {
        display: block;
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: 1030;
        background: rgba(0, 0, 0, .45);
    }

    /* ---------- Formulaires : evite le zoom automatique d'iOS ---------- */
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
    select,
    textarea {
        font-size: 16px !important;
    }

    /* ---------- Modales ---------- */
    .modal-dialog {
        max-width: calc(100vw - 1rem) !important;
        margin: .5rem auto !important;
    }

    .modal-dialog .modal-content {
        width: 100% !important;
        margin: 0 !important;
    }

    .modal-dialog .modal-body {
        max-height: calc(100dvh - 11rem);
        overflow-y: auto;
    }

    .modal-content {
        max-width: calc(100vw - 1rem);
    }

    /* Petites fenetres "card" centrees a 50% de largeur (Clients, Chauffeurs, Companies...) */
    .card[style*="width: 50%"] {
        width: 92% !important;
        max-width: none !important;
    }

    /* Dialogues Radzen */
    .rz-dialog {
        max-width: 96vw !important;
        max-height: 92dvh;
    }

    .rz-dialog-content {
        overflow: auto;
    }

    /* ---------- Planning des tournees : barre d'outils compacte sur mobile ----------
       Ligne 1 : Nouveau planning + ; ligne 2 : cases a cocher sur 2 colonnes ;
       puis bouton filtres pleine largeur ; puis Alertes + Date. */
    .tournees-toolbar {
        flex-wrap: wrap !important;
        padding: .6rem .75rem;
        gap: .5rem;
    }

    .tournees-toolbar > .btn-success:not(.rounded-circle) {
        flex: 1 1 auto;
    }

    .tournees-toolbar > .d-flex {
        flex: 1 1 100%;
        flex-wrap: wrap !important;
        align-items: center;
        gap: .5rem .75rem;
        width: 100%;
    }

    /* Cases a cocher : 2 par ligne, libelles non decoupes mot a mot */
    .tournees-toolbar > .d-flex > .form-check {
        flex: 1 1 calc(50% - .75rem);
        min-width: 0;
        margin: 0 !important;
        font-size: .9rem;
    }

    .tournees-toolbar > .d-flex > .form-check .form-check-label {
        line-height: 1.25;
    }

    /* Bouton de filtres avances : pleine largeur sur une ligne */
    .tournees-toolbar > .d-flex > .form-check:has(> .btn) {
        flex: 1 1 100%;
        padding-left: 0;
    }

    .tournees-toolbar > .d-flex > .form-check > .btn {
        width: 100%;
        white-space: nowrap;
    }

    /* Alertes + Date : sur une ligne, Date a droite */
    .tournees-toolbar > .d-flex > .d-flex {
        flex: 1 1 100%;
        flex-wrap: nowrap !important;
        justify-content: space-between;
        margin-left: 0 !important;
    }

    .tournees-toolbar > .d-flex > .d-flex > .d-flex {
        flex: 0 1 auto;
        min-width: 0;
    }

    .tournees-toolbar #toolbarSelectedDatePicker {
        width: 150px !important;
        max-width: 100%;
    }

    /* Selecteur de date / entreprise (quand les filtres avances sont masques) */
    .tournees-toolbar [class*="col-sm-"] {
        flex: 1 1 100%;
        max-width: 100%;
        padding-left: 0;
        padding-right: 0;
    }

    /* ---------- Tableaux en "cartes" (opt-in : <table class="... mobile-cards"> + data-label sur les <td>) ----------
       Evite de ne voir que 2 colonnes sur un tableau qui defile horizontalement.
       1re cellule = bandeau de titre, cellules suivantes = lignes libelle/valeur, derniere = actions. */
    .table-container:has(.mobile-cards),
    .responsive-table:has(.mobile-cards),
    div:has(> table.mobile-cards) {
        overflow-x: hidden;
        background: transparent !important;
        box-shadow: none !important;
        border: 0 !important;
        padding: .25rem !important;
    }

    table.mobile-cards,
    table.mobile-cards tbody,
    table.mobile-cards tr,
    table.mobile-cards td {
        display: block;
        width: 100%;
    }

    table.mobile-cards {
        margin: 0 !important;
        border: 0 !important;
        background: transparent !important;
        box-shadow: none !important;
    }

    table.mobile-cards thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0 0 0 0);
    }

    table.mobile-cards tbody tr {
        margin: 0 0 1rem;
        padding: 0;
        overflow: hidden;
        background: #fff;
        border: 1px solid #d5e0e6 !important;
        border-radius: 14px;
        box-shadow: 0 2px 8px rgba(20, 42, 53, .10);
    }

    table.mobile-cards tbody tr.driver-row-inactive {
        background: #f5efea;
    }

    table.mobile-cards tbody tr:hover {
        transform: none;
        box-shadow: 0 2px 8px rgba(20, 42, 53, .10);
    }

    table.mobile-cards td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: .75rem;
        min-width: 0 !important;
        padding: .5rem .9rem !important;
        background: transparent !important;
        border: 0 !important;
        border-bottom: 1px solid #eef2f5 !important;
        font-size: .92rem;
        font-weight: 500;
        color: #1f2d3a;
        text-align: right;
        word-break: break-word;
    }

    table.mobile-cards td::before {
        content: attr(data-label);
        flex: 0 0 42%;
        text-align: left;
        font-size: .7rem;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: .4px;
        color: #7a8794;
    }

    /* Bandeau de titre */
    table.mobile-cards td:first-child {
        padding: .7rem .9rem !important;
        background: linear-gradient(135deg, #2a5f75 0%, #1a4456 100%) !important;
        border-bottom: 0 !important;
        color: #fff;
        font-size: 1.05rem;
        font-weight: 700;
    }

    table.mobile-cards td:first-child::before {
        color: rgba(255, 255, 255, .7);
    }

    table.mobile-cards td:first-child a,
    table.mobile-cards td:first-child a:hover {
        color: #fff !important;
    }

    /* Actions */
    table.mobile-cards td:last-child:not(:first-child) {
        justify-content: flex-end;
        gap: .5rem;
        padding: .65rem .9rem !important;
        background: #f6f9fb !important;
        border-top: 1px solid #e3eaef !important;
        border-bottom: 0 !important;
    }

    table.mobile-cards td:last-child:not(:first-child)::before {
        display: none;
    }

    table.mobile-cards td:last-child .btn,
    table.mobile-cards td:last-child .btn-action {
        min-width: 40px;
        min-height: 38px;
    }

    /* Avant-derniere cellule : pas de trait si c'est la derniere avant les actions */
    table.mobile-cards td:nth-last-child(2) {
        border-bottom: 0 !important;
    }

    /* Cellules vides : inutile de les afficher */
    table.mobile-cards td:empty {
        display: none;
    }

    /* ---------- En-tetes de page : recherche + bouton ne debordent plus ---------- */
    .search-box {
        min-width: 0 !important;
        max-width: 100% !important;
        width: 100%;
    }

    .header-content,
    .header-actions {
        width: 100% !important;
        max-width: 100%;
    }

    .header-actions {
        flex-direction: column;
        align-items: stretch !important;
    }

    .header-actions > * {
        width: 100% !important;
        max-width: 100% !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
    }

    /* ---------- Modales plein ecran (alertes, fenetres "modern") : ne plus etre rognees ---------- */
    .alert-modal-overlay,
    .modern-modal-overlay {
        height: 100vh;
        height: 100dvh;
        bottom: auto !important;
        padding: .5rem !important;
        padding-top: calc(.5rem + env(safe-area-inset-top)) !important;
        padding-bottom: calc(.5rem + env(safe-area-inset-bottom)) !important;
        z-index: 1060 !important;
    }

    .alert-modal-card {
        width: 100% !important;
        max-height: calc(100dvh - 1rem - env(safe-area-inset-top) - env(safe-area-inset-bottom)) !important;
    }

    .alert-modal-header {
        padding: .8rem 1rem !important;
    }

    .alert-modal-body {
        padding: .75rem !important;
        min-height: 0;
        overflow: auto;
    }

    .alert-modal-footer {
        padding: .75rem 1rem !important;
    }

    .alert-modal-footer .btn {
        min-width: 120px !important;
    }

    /* ---------- Page d'accueil / Rapports : tuiles compactes en grille (au lieu d'une tuile geante par ligne) ---------- */
    html body .dashboard-container {
        margin-top: .5rem !important;
        margin-bottom: 1rem !important;
        padding: 0 .25rem !important;
        min-height: 0 !important;
    }

    html body .dashboard-container .dashboard-title {
        font-size: 1.3rem !important;
        line-height: 1.25;
        margin-bottom: 1rem !important;
    }

    html body .dashboard-container > .row[class*="row-cols"] {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(145px, 1fr));
        gap: .75rem;
        margin: 0;
    }

    html body .dashboard-container > .row[class*="row-cols"] > .col {
        width: auto !important;
        max-width: none !important;
        flex: none;
        margin: 0 !important;
        padding: 0 !important;
    }

    html body .dashboard-container .dashboard-tile {
        height: auto !important;
        min-height: 116px;
        border-radius: 16px;
        box-shadow: 0 3px 10px rgba(20, 42, 53, .12);
        transition: transform .12s ease, box-shadow .12s ease;
    }

    html body .dashboard-container .dashboard-tile:hover {
        transform: none;
    }

    html body .dashboard-container .dashboard-tile:active {
        transform: scale(.97);
        box-shadow: 0 1px 4px rgba(20, 42, 53, .15);
    }

    html body .dashboard-container .dashboard-tile .card-body {
        padding: .85rem .5rem;
    }

    html body .dashboard-container .dashboard-icon {
        font-size: 2.1rem;
        line-height: 1;
    }

    html body .dashboard-container .dashboard-tile .card-title {
        font-size: .98rem;
        line-height: 1.2;
        margin: .5rem 0 0 !important;
    }

    /* Les titres suffisent : on masque les descriptions pour garder des tuiles compactes */
    html body .dashboard-container .dashboard-tile .card-text {
        display: none;
    }

    /* ---------- Champs date (iOS : evite le debordement et le texte centre) ---------- */
    :not(.input-group) > input[type="date"],
    :not(.input-group) > input[type="datetime-local"],
    :not(.input-group) > input[type="time"] {
        -webkit-appearance: none;
        appearance: none;
        display: block;
        width: 100%;
        min-width: 0;
        max-width: 100%;
        box-sizing: border-box;
        text-align: left;
        min-height: 40px;
    }

    input[type="date"]::-webkit-date-and-time-value {
        text-align: left;
    }

    /* ---------- Filtres des tournees : boutons d'action en grille ----------
       Appliquer (pleine largeur), Exporter | Exporter PDF, Reinitialiser (discret) */
    .tournees-filters-card .row.mt-2.text-center > .col {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: .6rem;
        padding-top: .5rem;
    }

    .tournees-filters-card .row.mt-2.text-center > .col > .btn {
        margin: 0 !important;
        min-height: 46px;
        border-radius: 12px;
        font-weight: 600;
        font-size: .95rem;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: .4rem;
        white-space: nowrap;
        box-shadow: 0 1px 3px rgba(20, 42, 53, .12);
    }

    .tournees-filters-card .row.mt-2.text-center > .col > .btn:disabled {
        opacity: .55;
    }

    /* Appliquer */
    .tournees-filters-card .row.mt-2.text-center > .col > .btn-primary {
        order: 1;
        grid-column: 1 / -1;
        min-height: 50px;
        font-size: 1.02rem;
        color: #fff;
        border: 0;
        background: linear-gradient(135deg, #2a7f82 0%, #2a5f75 100%);
        box-shadow: 0 3px 10px rgba(42, 95, 117, .35);
    }

    /* Exporter (Excel) */
    .tournees-filters-card .row.mt-2.text-center > .col > .btn-info {
        order: 2;
        color: #1e7e4f;
        background: #e8f5ee;
        border: 1px solid #bfe3cf;
    }

    /* Exporter PDF */
    .tournees-filters-card .row.mt-2.text-center > .col > .btn-danger {
        order: 3;
        color: #b3261e;
        background: #fdecea;
        border: 1px solid #f5c6c2;
    }

    /* Reinitialiser */
    .tournees-filters-card .row.mt-2.text-center > .col > .btn-secondary {
        order: 4;
        grid-column: 1 / -1;
        min-height: 40px;
        color: #4a5560;
        background: #f1f4f6;
        border: 1px solid #dde3e8;
        box-shadow: none;
        font-weight: 500;
    }

    .tournees-filters-card .row.mt-2.text-center > .col > .progress {
        order: 5;
        grid-column: 1 / -1;
        margin-top: .25rem !important;
    }

    /* ---------- Titres de pages : taille adaptee, graisse homogene, petit trait d'accent ----------
       (on ne touche pas a la couleur : certains titres sont blancs sur un bandeau fonce) */
    html body :is(.page-title, .tournees-title, .dashboard-title):not(.modern-header-card *) {
        position: relative;
        font-size: clamp(1.25rem, 5.6vw, 1.65rem) !important;
        font-weight: 700;
        line-height: 1.2;
        letter-spacing: -.01em;
        padding-bottom: .6rem;
        text-wrap: balance;
        overflow-wrap: anywhere;
    }

    html body :is(.page-title, .tournees-title, .dashboard-title):not(.modern-header-card *)::after {
        content: "";
        position: absolute;
        left: 0;
        bottom: 0;
        width: 44px;
        height: 4px;
        border-radius: 4px;
        background: linear-gradient(90deg, #2a7f82, #2a5f75);
    }

    html body .text-center:is(.page-title, .tournees-title, .dashboard-title)::after {
        left: 50%;
        margin-left: -22px;
    }

    /* ---------- Divers ---------- */
    img, canvas {
        max-width: 100%;
    }
}
