/**
 * SIRH IPS-CO — Responsive unifié
 * Fusion de responsive.css (v2.2) + responsive-improvements.css (v1.1)
 * + mobile-premium.css (v1.0) — source unique de vérité mobile/tablette/desktop.
 *
 * Breakpoints (alignés Bootstrap 5) :
 *   xs < 576px · sm ≥ 576px · md ≥ 768px · lg ≥ 992px · xl ≥ 1200px
 *
 * Changements volontaires vs fichiers d'origine :
 *   - cibles tactiles 44px scoppées aux contrôles (plus de min-height sur tous les <a>)
 *   - suppression du "animation-duration: 0s" global mobile (cassait la
 *     transition du sidebar) — prefers-reduced-motion reste respecté
 *   - fallback de scroll horizontal pour les tables sans wrapper .table-responsive
 *   - hauteur maîtrisée des conteneurs de graphiques sur mobile
 */

/* ═══════════════════════════════════════════
   1. VARIABLES
   ═══════════════════════════════════════════ */
:root {
  --mp-navy: #1A3E62;
  --mp-navy-deep: #132f4c;
  --mp-orange: #F58220;
  --mp-orange-soft: rgba(245, 130, 32, 0.12);
  --mp-radius: 14px;
  --mp-radius-sm: 10px;
  --mp-shadow: 0 8px 28px rgba(26, 62, 98, 0.12);
  --mp-safe-bottom: env(safe-area-inset-bottom, 0px);

  --mobile-padding: 0.75rem;
  --tablet-padding: 1rem;
  --desktop-padding: 1.25rem;
}

/* ═══════════════════════════════════════════
   2. ANTI-DÉBORDEMENT GLOBAL
   ═══════════════════════════════════════════ */
html, body {
  max-width: 100%;
  overflow-x: hidden;
}
img, svg, video, canvas {
  max-width: 100%;
  height: auto;
}
.table, .card, .modal-dialog, .dropdown-menu {
  max-width: 100%;
}
.img-responsive {
  width: 100%;
  height: auto;
  object-fit: cover;
}

/* ═══════════════════════════════════════════
   3. UTILITAIRES D'AFFICHAGE CONDITIONNEL
   ═══════════════════════════════════════════ */
.show-mobile  { display: block; }
.show-tablet  { display: none; }
.show-desktop { display: none; }

@media (min-width: 768px) {
  .show-mobile { display: none; }
  .show-tablet { display: block; }
  .mp-hide-xs  { display: table-cell !important; }
}
@media (min-width: 1024px) {
  .show-tablet  { display: none; }
  .show-desktop { display: block; }
}
@media (max-width: 767.98px) {
  .hide-mobile { display: none !important; }
  .mp-hide-xs  { display: none !important; }

  .py-mobile-2 { padding-top: var(--space-2); padding-bottom: var(--space-2); }
  .px-mobile-2 { padding-left: var(--space-2); padding-right: var(--space-2); }
}

/* Espacements / texte adaptatifs */
.p-responsive    { padding: var(--mobile-padding); }
.text-responsive { font-size: 0.875rem; }
.h1-responsive   { font-size: 1.75rem; }
@media (min-width: 768px) {
  .p-responsive    { padding: var(--tablet-padding); }
  .text-responsive { font-size: 1rem; }
  .h1-responsive   { font-size: 2.5rem; }
}
@media (min-width: 992px) {
  .p-responsive { padding: var(--desktop-padding); }
}

/* ═══════════════════════════════════════════
   4. TRÈS PETITS ÉCRANS (< 576px)
   ═══════════════════════════════════════════ */
@media (max-width: 575.98px) {
  .container,
  .container-fluid {
    padding-left: var(--mobile-padding);
    padding-right: var(--mobile-padding);
  }

  .navbar-toggler {
    padding: 0.5rem;
    font-size: 1.5rem;
    border: 2px solid rgba(255, 255, 255, 0.5);
  }
  .navbar-collapse {
    margin-top: 1rem;
    background: rgba(0, 0, 0, 0.05);
    padding: 1rem;
    border-radius: 8px;
  }

  /* Dropdowns intégrés au flux */
  .dropdown-menu {
    position: static !important;
    transform: none !important;
    border: none;
    box-shadow: none;
    background: transparent;
    padding: 0;
  }
  .dropdown-item {
    padding: 0.75rem 1rem;
    border-radius: 4px;
    margin-bottom: 0.25rem;
  }

  .stat-card { padding: 1rem; text-align: center; }
  .stat-card h3 { font-size: 1.5rem; }

  .btn-group-responsive {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }
  .btn-group-responsive .btn { width: 100%; }

  .breadcrumb {
    font-size: 0.875rem;
    padding: 0.5rem 0;
    background: transparent;
  }
  .breadcrumb-item {
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mobile-action-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
    padding: 1rem;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
    z-index: 1000;
  }
}

/* ═══════════════════════════════════════════
   5. MOBILE (< 768px) — SHELL PREMIUM
   ═══════════════════════════════════════════ */

/* Toggle + overlay du sidebar */
.mp-sidebar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  border: 1px solid rgba(26, 62, 98, 0.12);
  background: linear-gradient(180deg, #fff, #f7f9fc);
  color: var(--mp-navy);
  box-shadow: 0 2px 8px rgba(26, 62, 98, 0.08);
  margin-right: .5rem;
}
.mp-sidebar-toggle:hover,
.mp-sidebar-toggle:focus {
  color: var(--mp-orange);
  border-color: rgba(245, 130, 32, 0.35);
  outline: none;
}
.mp-sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(19, 47, 76, 0.45);
  backdrop-filter: blur(2px);
  z-index: 1040;
}
.mp-sidebar-overlay.show { display: block; }

@media (max-width: 767.98px) {
  .mp-sidebar-toggle { display: inline-flex; }

  #sidebarMenu {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: min(86vw, 300px) !important;
    max-width: 300px !important;
    height: 100vh !important;
    height: 100dvh !important;
    z-index: 1055 !important;
    transform: translateX(-105%);
    transition: transform .28s cubic-bezier(.22, 1, .36, 1);
    box-shadow: none;
    display: block !important; /* override Bootstrap .collapse display:none */
    padding-top: env(safe-area-inset-top, 0);
  }
  #sidebarMenu.show {
    transform: translateX(0);
    box-shadow: 12px 0 40px rgba(0, 0, 0, 0.35);
  }
  #sidebarMenu.collapse:not(.show) {
    display: block !important;
    transform: translateX(-105%);
  }
  #sidebarMenu.collapse.show { display: block !important; }

  body.mp-sidebar-open { overflow: hidden; }

  /* Navbar compacte */
  .navbar { padding-top: .55rem; padding-bottom: .55rem; }
  .navbar-brand span { font-size: .95rem !important; }

  /* Navigation repliée (fallback navbar-collapse hors sidebar) */
  .navbar-collapse {
    position: fixed;
    top: 60px;
    left: 0;
    right: 0;
    background-color: #fff;
    box-shadow: 0 8px 28px rgba(26, 62, 98, 0.18);
    max-height: calc(100vh - 60px);
    overflow-y: auto;
    padding: 1rem;
  }
  .navbar-nav { flex-direction: column; gap: .35rem; }

  /* Respiration du contenu principal */
  .container-fluid > .row > main,
  .container-fluid > .row > .col,
  .container-fluid > .row > [class*="col-"] {
    padding-left: .85rem !important;
    padding-right: .85rem !important;
  }

  /* Cartes premium */
  .card,
  .hub-card,
  .stat-card,
  .portal-card {
    border-radius: var(--mp-radius) !important;
    box-shadow: var(--mp-shadow) !important;
    border: 0 !important;
    margin-bottom: 1rem;
  }
  .card-header {
    border-radius: var(--mp-radius) var(--mp-radius) 0 0 !important;
    background: #fff !important;
  }
  .stat-card, .card .card-body { padding: 1rem 1.1rem; }

  /* Titres compacts */
  h1 { font-size: 1.5rem; }
  h2 { font-size: 1.3rem; }
  h3 { font-size: 1.15rem; }
  h1.h3, .h3, .hub-hero h1 {
    font-size: 1.25rem !important;
    line-height: 1.25;
  }

  /* Boutons "pouce" */
  .btn {
    min-height: 42px;
    border-radius: 10px;
    font-weight: 600;
  }
  .btn-sm { min-height: 34px; }
  .btn-group { flex-wrap: wrap; gap: .35rem; }
  .d-flex.gap-2, .d-flex.gap-3 { flex-wrap: wrap; }
  .btn-mobile-full {
    width: 100%;
    justify-content: center;
    margin-bottom: 0.5rem;
  }

  /* Formulaires */
  .form-row { flex-direction: column; }
  .form-group { width: 100%; margin-bottom: 1rem; }
  .form-control, .form-select {
    min-height: 44px;
    border-radius: 10px;
    font-size: 16px; /* anti-zoom iOS */
  }
  .form-label {
    font-weight: 600;
    color: var(--mp-navy);
    font-size: .85rem;
  }

  /* Modales plein-écran doux */
  .modal-dialog { margin: .75rem; }
  .modal-content {
    border-radius: var(--mp-radius);
    border: 0;
    box-shadow: var(--mp-shadow);
  }

  /* Bandeaux / heroes */
  .hub-hero,
  .reports-hero,
  .page-header {
    border-radius: var(--mp-radius) !important;
    padding: 1.15rem 1.25rem !important;
  }
  .hub-hero .btn,
  .reports-hero .btn {
    width: 100%;
    margin-top: .65rem;
  }

  /* Pills / pagination */
  .nav-pills { flex-wrap: wrap; gap: .4rem; }
  .nav-pills .nav-link {
    border-radius: 999px;
    padding: .45rem .9rem;
    font-size: .85rem;
    font-weight: 600;
  }
  .pagination {
    flex-wrap: wrap;
    justify-content: center;
    gap: .25rem;
  }

  /* Lisibilité des ventilations de devise */
  .small.text-success,
  .small.text-danger,
  .small.text-primary { font-weight: 600; }

  /* Barre d'actions collante des formulaires */
  .mp-sticky-actions {
    position: sticky;
    bottom: 0;
    bottom: var(--mp-safe-bottom);
    background: rgba(255,255,255,.92);
    backdrop-filter: blur(8px);
    padding: .75rem;
    margin: 0 -0.85rem;
    border-top: 1px solid rgba(26,62,98,.08);
    z-index: 20;
    display: flex;
    flex-wrap: wrap;
    gap: .5rem;
  }
  .mp-sticky-actions .btn { flex: 1 1 auto; }

  /* Les widgets flottants ne couvrent pas les CTA */
  #chatWidgetToggle,
  .chat-widget-toggle {
    bottom: calc(16px + var(--mp-safe-bottom)) !important;
  }

  /* Footer empilé */
  .footer { padding: 1.5rem 1rem; text-align: center; }
  .footer .row { flex-direction: column; gap: 1rem; }
  .footer .text-end { text-align: center !important; }
}

/* ═══════════════════════════════════════════
   6. TABLES
   ═══════════════════════════════════════════ */

/* Wrapper standard */
.table-responsive,
.table-responsive-custom {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 767.98px) {
  .table-responsive {
    border-radius: var(--mp-radius-sm);
    border: 1px solid rgba(26, 62, 98, 0.08);
    box-shadow: inset 0 0 0 1px rgba(255,255,255,.4);
    margin-bottom: 1rem;
  }
  .table {
    min-width: 560px;
    margin-bottom: 0;
    font-size: .86rem;
  }
  .table thead th {
    position: sticky;
    top: 0;
    background: #f4f7fb;
    color: var(--mp-navy);
    font-weight: 700;
    font-size: .75rem;
    text-transform: uppercase;
    letter-spacing: .03em;
    white-space: nowrap;
    z-index: 1;
  }
  .table td, .table th {
    vertical-align: middle;
    padding: .65rem .75rem;
  }

  /* Fallback : table .table SANS wrapper .table-responsive → scroll interne.
     (Inoffensif pour les tables déjà wrappées : le conteneur scrolle.) */
  .table:not(.table-stack) {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Pattern "cartes empilées" : ajouter .table-stack à la table
     et data-label="…" sur chaque <td>. */
  .table-stack, .table-stack tbody { display: block; }
  .table-stack { border: 0; min-width: 0; }
  .table-stack thead { display: none; }
  .table-stack tr {
    display: block;
    margin-bottom: 1rem;
    border: 1px solid #dee2e6;
    border-radius: var(--mp-radius-sm);
    padding: 1rem;
    background: #fff;
    box-shadow: 0 2px 10px rgba(26, 62, 98, 0.06);
  }
  .table-stack td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    padding: 0.45rem 0;
    border: none;
    border-bottom: 1px solid #f0f0f0;
    text-align: right;
  }
  .table-stack td:last-child { border-bottom: none; }
  .table-stack td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--mp-navy);
    text-align: left;
    flex-shrink: 0;
  }
  .table-stack td:not([data-label])::before { content: none; }
}

/* ═══════════════════════════════════════════
   7. TABLETTE (768px – 991px)
   ═══════════════════════════════════════════ */
@media (min-width: 768px) and (max-width: 991.98px) {
  .mp-sidebar-toggle { display: none; }
  .table { font-size: .9rem; }
  .card { border-radius: var(--mp-radius); }

  .container {
    padding-left: var(--tablet-padding);
    padding-right: var(--tablet-padding);
  }
  .col-tablet-6 { flex: 0 0 50%; max-width: 50%; }
  .grid-tablet-2 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
  }
  .sidebar { width: 250px; transition: width 0.3s; }
  .sidebar.collapsed { width: 60px; }
}

/* ═══════════════════════════════════════════
   8. DESKTOP (≥ 992px)
   ═══════════════════════════════════════════ */
@media (min-width: 992px) {
  .container {
    padding-left: var(--desktop-padding);
    padding-right: var(--desktop-padding);
  }

  .card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    transition: all 0.3s ease;
  }
  .btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  }
  [data-toggle="tooltip"] { cursor: help; }

  .grid-desktop-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1.5rem;
  }
  .grid-desktop-4 {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1.5rem;
  }
  .layout-sidebar {
    display: grid;
    grid-template-columns: 250px 1fr;
    gap: 1.5rem;
  }

  /* Pleine largeur applicative */
  .container-fluid,
  .main-content {
    max-width: none !important;
    width: 100% !important;
    padding-left: 0.5rem !important;
    padding-right: 0.5rem !important;
  }
  .container {
    max-width: none !important;
    width: 100% !important;
  }
}

@media (min-width: 1200px) {
  /* width/max-width en % et non en vw : 100vw ignore le padding-left de
     body.has-sidebar (240px) et inclut la scrollbar verticale, ce qui
     décalait le contenu d'environ 255px hors de l'écran à droite. */
  .container-fluid,
  .main-content,
  .content-wrapper {
    padding-left: 0.25rem !important;
    padding-right: 0.25rem !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
    max-width: 100% !important;
    width: 100% !important;
  }
  .row {
    margin-left: -0.25rem !important;
    margin-right: -0.25rem !important;
  }
  .col,
  .col-md-10,
  .col-lg-12 {
    padding-left: 0.25rem !important;
    padding-right: 0.25rem !important;
  }
}

/* ═══════════════════════════════════════════
   9. GRILLE & COMPOSANTS PARTAGÉS
   ═══════════════════════════════════════════ */
.grid-responsive {
  display: grid;
  gap: 1rem;
  grid-template-columns: 1fr;
}
@media (min-width: 576px)  { .grid-responsive { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 992px)  { .grid-responsive { grid-template-columns: repeat(3, 1fr); } }
@media (min-width: 1200px) { .grid-responsive { grid-template-columns: repeat(4, 1fr); } }

.card-equal-height {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.card-equal-height .card-body { flex: 1; }
.card-clickable {
  cursor: pointer;
  transition: all 0.3s ease;
}
.card-clickable:hover {
  transform: translateY(-4px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.navbar-sticky {
  position: sticky;
  top: 0;
  z-index: 1020;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* Formulaires multi-colonnes desktop */
@media (min-width: 768px) {
  .form-row-responsive { display: flex; gap: 1rem; }
  .form-row-responsive .form-group { flex: 1; }
}

/* Labels flottants */
.form-floating-custom { position: relative; }
.form-floating-custom input:focus + label,
.form-floating-custom input:not(:placeholder-shown) + label {
  transform: translateY(-1.5rem) scale(0.85);
  color: #667eea;
}
.form-floating-custom label {
  position: absolute;
  top: 0.75rem;
  left: 0.75rem;
  transition: all 0.2s;
  pointer-events: none;
}

/* Anti-zoom iOS partout (16px minimum sur les champs) */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="date"],
input[type="datetime-local"],
input[type="search"],
select,
textarea {
  font-size: max(16px, 1em);
}

/* Modales responsive opt-in */
@media (min-width: 576px) {
  .modal-dialog-responsive { max-width: 90%; margin: 1.75rem auto; }
}
@media (min-width: 992px) {
  .modal-dialog-responsive { max-width: 800px; }
}

/* Vidéos 16:9 */
.video-responsive {
  position: relative;
  padding-bottom: 56.25%;
  height: 0;
  overflow: hidden;
}
.video-responsive iframe,
.video-responsive object,
.video-responsive embed {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

/* ═══════════════════════════════════════════
   10. GRAPHIQUES (Chart.js & co)
   ═══════════════════════════════════════════ */
@media (max-width: 767.98px) {
  .chart-container,
  .chart-wrapper,
  .chart-area {
    height: 260px !important;
    max-height: 260px !important;
    position: relative;
  }
}

/* ═══════════════════════════════════════════
   11. ACCESSIBILITÉ & TOUCH
   ═══════════════════════════════════════════ */

/* Cibles tactiles ≥ 44px — scoppé aux contrôles (pas les liens inline) */
@media (max-width: 767.98px) {
  button,
  .btn,
  .nav-link,
  .dropdown-item,
  .page-link,
  .form-check-input,
  input[type="checkbox"],
  input[type="radio"] {
    min-height: 44px;
  }
  button, .btn {
    min-width: 44px;
  }
  .form-check-input,
  input[type="checkbox"],
  input[type="radio"] {
    min-height: 22px;
    min-width: 22px;
  }
  .nav-link, .dropdown-item, .page-link {
    display: inline-flex;
    align-items: center;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

@media (prefers-contrast: high) {
  :root {
    --color-primary: #0000ff;
    --color-gray-300: #000000;
  }
  .btn { border-width: 2px; }
}

/* ═══════════════════════════════════════════
   12. IMPRESSION
   ═══════════════════════════════════════════ */
@media print {
  .navbar,
  .sidebar,
  .header,
  .footer,
  .btn,
  .no-print,
  .mp-sidebar-toggle,
  .mp-sidebar-overlay,
  #sidebarMenu { display: none !important; }

  body {
    font-size: 12pt;
    color: black;
    background: white;
  }
  .container { width: 100%; max-width: none; }
  .card, table, .row { page-break-inside: avoid; }
  .card {
    box-shadow: none;
    border: 1px solid #ccc;
  }
  a[href]:after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #666;
  }
}

/* ═══════════════════════════════════════════
   13. EN-TÊTE MOBILE ET TABLETTE (< 992 px)
   style.css force `.navbar-collapse { display:flex !important }` pour la barre
   compacte du bureau : sans ce bloc, le menu principal ne se replie jamais et
   reste collé sous l'en-tête, par-dessus le contenu.
   ═══════════════════════════════════════════ */
@media (max-width: 991.98px) {
  .navbar.navbar-expand-lg > .container-fluid { flex-wrap: wrap !important; padding-left: .5rem !important; padding-right: .5rem !important; }

  .navbar .navbar-collapse:not(.show):not(.collapsing) { display: none !important; }

  .navbar .navbar-collapse.show,
  .navbar .navbar-collapse.collapsing {
    display: block !important;
    position: static !important;
    flex-basis: 100% !important;
    width: 100%;
    margin-top: .55rem !important;
    padding: .35rem 0 .5rem !important;
    max-height: calc(100dvh - 72px);
    overflow-y: auto;
    background: #fff;
    border-top: 1px solid rgba(26, 62, 98, 0.10);
    box-shadow: none;
    border-radius: 0;
  }

  .navbar .navbar-collapse .navbar-nav {
    flex-direction: column !important;
    align-items: stretch !important;
    margin: 0 !important;
  }
  .navbar .navbar-collapse .navbar-nav .nav-link {
    font-size: .95rem !important;
    font-weight: 600 !important;
    padding: .65rem .5rem !important;
    gap: .5rem;
    border-bottom: 1px solid rgba(26, 62, 98, 0.06);
  }
  .navbar .navbar-collapse .navbar-nav .nav-link i { width: 1.25rem; text-align: center; }
  .navbar .navbar-collapse .dropdown-menu {
    position: static !important;
    transform: none !important;
    float: none;
    border: 0;
    box-shadow: none;
    padding: 0 0 .35rem 1.75rem;
    background: transparent;
  }
  /* Pastilles de compteur remises dans le flux : en absolu, elles partent au bord du panneau */
  .navbar .navbar-collapse .notification-badge {
    position: static !important;
    transform: none !important;
    display: inline-block;
    margin-left: .4rem;
  }
  .navbar .navbar-collapse .notification-bell { display: inline-flex; align-items: center; padding: .65rem .5rem; }
  .navbar .navbar-collapse .navbar-nav.ms-auto {
    margin-top: .35rem !important;
    padding-top: .35rem !important;
    border-top: 2px solid rgba(26, 62, 98, 0.08);
  }

  /* Deux boutons de même facture : modules (tiroir latéral) et menu principal */
  .navbar .navbar-toggler {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    padding: 0;
    font-size: 1.05rem;
    border-radius: 12px;
    border: 1px solid rgba(26, 62, 98, 0.12) !important;
    background: linear-gradient(180deg, #fff, #f7f9fc);
    color: var(--mp-navy, #1a3e62);
    box-shadow: 0 2px 8px rgba(26, 62, 98, 0.08);
  }
  .navbar .navbar-toggler:focus { box-shadow: 0 0 0 3px rgba(245, 130, 32, 0.25); }
  .navbar .navbar-toggler[aria-expanded="true"] { color: var(--mp-orange, #f58220); }
  .navbar .mp-sidebar-toggle { margin-right: 0; }
}
