/**
 * Gestioparc - Optimisations Responsive
 * Mobile-first enhancements pour une expérience optimale sur tous les écrans
 */

/* === Variables responsive === */
@media (max-width: 575.98px) {
  :root {
    --responsive-padding: 0.75rem;
    --card-padding: 1rem;
  }
}

/* === Safe area pour appareils à encoche === */
@supports (padding: env(safe-area-inset-bottom)) {
  body {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  .theme-customization__button {
    margin-bottom: calc(1.5rem + env(safe-area-inset-bottom));
    margin-right: calc(1.5rem + env(safe-area-inset-right));
  }
}

/* === Base & Typography === */
@media (max-width: 767.98px) {
  html {
    font-size: 15px;
  }
  .dashboard-main-body {
    padding: 0.75rem !important;
  }
  .breadcrumb {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 0.5rem !important;
  }
  .breadcrumb h6 {
    font-size: 1rem;
  }
  .breadcrumb p {
    font-size: 0.8125rem;
  }
}

/* === Navbar header mobile === */
@media (max-width: 991.98px) {
  .navbar-header {
    padding: 0.75rem 1rem;
    height: auto;
    min-height: 3.5rem;
  }
  .navbar-header .row {
    --bs-gutter-x: 0.5rem;
  }
  .navbar-header .gap-4 {
    gap: 0.5rem !important;
  }
}

@media (max-width: 575.98px) {
  .navbar-header {
    padding: 0.5rem 0.75rem;
  }
  .navbar-search input {
    width: 100%;
    min-width: 0;
  }
}

/* === Bouton thème : position mobile === */
@media (max-width: 767.98px) {
  .theme-customization__button {
    width: 40px !important;
    height: 40px !important;
    margin-bottom: 1rem !important;
    margin-right: 1rem !important;
    font-size: 1.25rem !important;
    bottom: 0;
    right: 0;
  }
}

/* === Sidebar mobile optimisations === */
@media (max-width: 1199.98px) {
  .sidebar {
    width: min(85vw, 320px);
    max-width: 320px;
  }
  .sidebar-menu-area {
    max-height: calc(100vh - 12rem);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .profile-dropdown__contents span:first-child {
    max-width: 150px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* === Dashboard main content === */
@media (max-width: 767.98px) {
  .dashboard-main-body {
    padding: 0.5rem 0.75rem !important;
  }
  .d-footer {
    padding: 1rem 0.75rem;
    font-size: 0.8125rem;
    text-align: center;
  }
  .d-footer p {
    line-height: 1.4;
  }
}

/* === Cards responsive === */
@media (max-width: 767.98px) {
  .card-body {
    padding: 1rem !important;
  }
  .card-body.p-24 {
    padding: 1rem !important;
  }
  .card-body.p-20 {
    padding: 0.875rem !important;
  }
  .card-header {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 0.75rem;
    padding: 1rem !important;
  }
  .card-header.d-flex.justify-content-between {
    flex-wrap: wrap;
  }
  .card-header h5 {
    font-size: 0.9375rem;
  }
  .card-header .btn-sm {
    align-self: flex-start;
  }
}

/* === Tables : responsive optimisé === */

/* Envelope scroll avec indicateur visuel */
.table-responsive {
  -webkit-overflow-scrolling: touch;
  overflow-x: auto;
  position: relative;
  border-radius: 0.5rem;
}

/* Légère ombre à droite pour indiquer le scroll */
.table-responsive::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 24px;
  background: linear-gradient(to left, rgba(0,0,0,0.06), transparent);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}

@media (max-width: 991.98px) {
  .table-responsive.scrollable::after {
    opacity: 1;
  }
}

/* Table : largeur minimale pour scroll fluide */
.table-responsive .table {
  min-width: 600px;
}

@media (max-width: 767.98px) {
  .table-responsive .table {
    min-width: 520px;
    font-size: 0.8125rem;
  }
  .table th,
  .table td {
    padding: 0.5rem 0.5rem;
    vertical-align: middle;
  }
  .table th:first-child,
  .table td:first-child {
    padding-left: 0.75rem;
  }
  .table th:last-child,
  .table td:last-child {
    padding-right: 0.75rem;
  }
  .table .btn-sm {
    padding: 0.35rem 0.5rem;
    font-size: 0.75rem;
  }
  .table .d-flex.gap-2 {
    flex-wrap: wrap;
    gap: 0.25rem !important;
  }
  .table .badge {
    font-size: 0.6875rem;
  }
}

/* Première colonne fixe lors du scroll horizontal (mobile/tablette) */
@media (max-width: 991.98px) {
  .table-responsive .table th:first-child,
  .table-responsive .table td:first-child {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--white);
    box-shadow: 2px 0 6px -2px rgba(0,0,0,0.08);
  }
  html[data-theme="dark"] .table-responsive .table th:first-child,
  html[data-theme="dark"] .table-responsive .table td:first-child {
    background: var(--neutral-900);
  }
}

/* Mode carte : chaque ligne devient une carte sur très petit écran */
@media (max-width: 575.98px) {
  .table-responsive-card {
    overflow-x: visible;
  }
  .table-responsive-card::after {
    display: none;
  }
  .table-responsive-card .table {
    min-width: 100%;
    display: block;
  }
  .table-responsive-card .table thead {
    display: none;
  }
  .table-responsive-card .table tbody {
    display: block;
  }
  .table-responsive-card .table tr {
    display: block;
    border: 1px solid var(--neutral-200);
    border-radius: 0.5rem;
    margin-bottom: 0.75rem;
    padding: 0.75rem;
  }
  .table-responsive-card .table td {
    display: block;
    padding: 0.35rem 0;
    border: none;
  }
  .table-responsive-card .table td::before {
    content: attr(data-label);
    display: block;
    font-weight: 600;
    font-size: 0.6875rem;
    color: var(--neutral-500);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 0.15rem;
  }
  .table-responsive-card .table td:last-child:has(.btn) {
    border-top: 1px solid var(--neutral-200);
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    padding-bottom: 0;
  }
  .table-responsive-card .table td:last-child:has(.btn)::before {
    display: none;
  }
  .table-responsive-card .table .d-flex.gap-2 {
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  .table-responsive-card .table .btn-sm {
    flex: 0 0 auto;
  }
}

/* === Layout liste : titre + bouton === */
@media (max-width: 575.98px) {
  .d-flex.justify-content-between.align-items-center.flex-wrap {
    flex-direction: column;
    align-items: stretch !important;
    gap: 0.75rem;
  }
  .d-flex.justify-content-between.align-items-center.flex-wrap .btn,
  .d-flex.justify-content-between.align-items-center.flex-wrap a.btn {
    width: 100%;
    justify-content: center;
  }
}

/* === Forms responsive === */
@media (max-width: 767.98px) {
  .col-lg-8 {
    max-width: 100%;
    flex: 0 0 100%;
  }
  .row.g-4 {
    --bs-gutter-x: 1rem;
    --bs-gutter-y: 1rem;
  }
  .form-label {
    font-size: 0.875rem;
  }
  .d-flex.gap-2 .btn {
    flex: 1;
    min-width: 0;
  }
}

@media (max-width: 575.98px) {
  .col-12.d-flex.gap-2 button[type="submit"],
  .col-12.d-flex.gap-2 .btn {
    width: 100%;
  }
  .col-12.d-flex.gap-2 {
    flex-direction: column;
  }
}

/* === Boutons : touch-friendly (min 44px) === */
@media (pointer: coarse) {
  .btn-sm {
    min-height: 36px;
    padding: 0.375rem 0.75rem;
  }
  .btn {
    min-height: 44px;
  }
  .sidebar-menu li a {
    min-height: 44px;
    padding: 0.75rem 1rem;
    display: flex;
    align-items: center;
  }
  .form-control,
  .form-select {
    min-height: 44px;
  }
}

/* === Dashboard spécifique === */
@media (max-width: 767.98px) {
  .row.gy-4 .col-lg-3 {
    flex: 0 0 100%;
    max-width: 100%;
  }
  .row.gy-4 .col-md-4 {
    flex: 0 0 100%;
    max-width: 100%;
  }
  .card h4 {
    font-size: 1.25rem;
  }
  #chartVehiculesEtat,
  #chartCouts,
  #chartEvolution {
    min-height: 220px !important;
  }
}

/* === Alertes === */
@media (max-width: 575.98px) {
  .alert.d-flex.align-items-center.justify-content-between {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 0.75rem;
  }
  .alert .btn {
    width: 100%;
  }
}

/* === Role permissions table === */
@media (max-width: 767.98px) {
  .table th:nth-child(n+2),
  .table td:nth-child(n+2) {
    padding: 0.5rem 0.25rem;
  }
  .table input.form-check-input {
    margin: 0 auto;
  }
}

/* === Dropdown menu mobile === */
@media (max-width: 1199.98px) {
  .dropdown-menu.dropdown-menu-lg-end {
    max-width: min(90vw, 280px);
    margin-top: 0.25rem;
  }
}

/* === DataTables responsive === */
@media (max-width: 767.98px) {
  .dataTables_wrapper .dataTables_length,
  .dataTables_wrapper .dataTables_filter {
    margin-bottom: 0.75rem;
  }
  .dataTables_wrapper .dataTables_length select {
    padding: 0.25rem 1.5rem;
  }
  .dataTables_wrapper .dataTables_paginate {
    margin-top: 0.75rem;
  }
  .dataTables_wrapper .dataTables_paginate .paginate_button {
    padding: 0.25rem 0.5rem;
    margin: 2px;
  }
}

/* === Utilitaires responsive === */
@media (max-width: 575.98px) {
  .gap-3 {
    gap: 0.5rem !important;
  }
  .gap-4 {
    gap: 0.75rem !important;
  }
  .mb-24 {
    margin-bottom: 1rem !important;
  }
  .mt-24 {
    margin-top: 1rem !important;
  }
}

/* === Fix overflow horizontal === */
html,
body {
  overflow-x: hidden;
}

.dashboard-main {
  overflow-x: hidden;
}

/* === Theme customization mobile === */
@media (max-width: 575.98px) {
  .theme-customization-sidebar .d-grid.grid-cols-3 {
    grid-template-columns: repeat(2, 1fr);
  }
  .theme-customization-sidebar .color-picker-btn__box {
    height: 32px !important;
  }
}

/* === Amélioration scroll === */
.sidebar-menu-area,
.theme-customization-sidebar,
.overflow-y-auto {
  scrollbar-width: thin;
}

.sidebar-menu-area::-webkit-scrollbar,
.theme-customization-sidebar::-webkit-scrollbar {
  width: 4px;
}

/* === ApexCharts responsive === */
@media (max-width: 767.98px) {
  .apexcharts-legend {
    flex-wrap: wrap;
    justify-content: center;
  }
}
