/**
 * ==========================================================================
 * LE41 PRO - RESPONSIVE DESIGN UNIVERSEL MULTI-APPAREILS
 * Prise en charge complète :
 * - Smartphones compacts (320px - 480px)
 * - Smartphones & Phablettes (480px - 768px)
 * - Tablettes verticales (768px - 900px)
 * - Tablettes horizontales & Laptops (900px - 1024px)
 * - Desktops & Écrans Larges (1024px - 1600px)
 * - Écrans Ultra-Wide / 4K (> 1600px)
 * - Mode Paysage Mobile & Optimisations Tactiles
 * - Impression Papier & Export PDF
 * ==========================================================================
 */

/* ==========================================================================
   1. VARIABLES FLUIDES & CONTENEURS UNIVERSELS
   ========================================================================== */

:root {
  --app-max-width: 1800px;
  --touch-target-min: 44px;
  --mobile-bottom-nav-height: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* Fluid Global Wrapper */
.main-wrapper {
  max-width: 100%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  flex: 1;
  width: 100%;
}

.content-body {
  width: 100%;
  max-width: var(--app-max-width);
  margin: 0 auto;
  box-sizing: border-box;
}

/* Universal Smooth Responsive Table Wrapper */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  position: relative;
  border-radius: var(--radius-md);
  margin-bottom: 1rem;
}

.table-responsive::-webkit-scrollbar {
  height: 6px;
}

.table-responsive::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.03);
  border-radius: 4px;
}

.table-responsive::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.18);
  border-radius: 4px;
}

.table-responsive::-webkit-scrollbar-thumb:hover {
  background: var(--color-primary);
}

/* Universal Top Navigation Ribbon Base */
.top-nav-bar {
  max-width: var(--app-max-width);
  width: 100%;
  box-sizing: border-box;
}

.top-nav-track {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

/* Prestataires (4 Emails, GPS Google Maps & Multi-Notes) Base Grid */
.providers-grid,
.provider-card {
  transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.provider-emails-grid,
.emails-four-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 0.5rem;
  width: 100%;
}

.provider-gps {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.provider-notes {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

/* ==========================================================================
   2. ÉCRANS ULTRA-WIDE & 4K (min-width: 1600px, 2560px, 3840px)
   ========================================================================== */

@media (min-width: 1600px) {
  .content-body {
    padding: 2.25rem 3rem;
  }

  .grid-dashboard {
    grid-template-columns: repeat(12, 1fr);
    gap: 1.5rem;
  }

  .kpi-card {
    grid-column: span 3;
    padding: 1.6rem;
  }

  .stats-kpi-grid {
    grid-template-columns: repeat(5, 1fr);
    gap: 1.25rem;
  }

  .stats-charts-grid {
    gap: 1.5rem;
  }

  .chart-canvas-wrap {
    min-height: 320px;
  }
}

@media (min-width: 2560px) {
  :root {
    --app-max-width: 2200px;
  }

  .content-body {
    max-width: var(--app-max-width);
    padding: 2.75rem 4rem;
  }

  .grid-dashboard {
    grid-template-columns: repeat(12, 1fr);
    gap: 2rem;
  }

  .kpi-card {
    grid-column: span 3;
    padding: 2rem;
  }

  .kpi-value {
    font-size: 2.2rem;
  }

  .stats-kpi-grid {
    grid-template-columns: repeat(5, 1fr);
    gap: 1.75rem;
  }

  .chart-canvas-wrap {
    min-height: 380px;
  }

  .top-nav-bar {
    padding: 0.6rem 2.5rem;
  }
}

@media (min-width: 3840px) {
  :root {
    --app-max-width: 2400px;
  }

  body {
    font-size: 17px;
  }

  .content-body {
    max-width: var(--app-max-width);
    padding: 3.5rem 6rem;
  }

  .topbar {
    height: 80px;
    padding: 0 3rem;
  }

  .top-nav-bar {
    padding: 0.75rem 3.5rem;
  }

  .top-nav-item {
    font-size: 0.95rem;
    padding: 0.55rem 1.15rem;
  }

  .kpi-value {
    font-size: 2.5rem;
  }

  .chart-canvas-wrap {
    min-height: 440px;
  }
}

/* ==========================================================================
   3. LAPTOPS & DESKTOPS INTERMÉDIAIRES (max-width: 1400px)
   ========================================================================== */

@media (max-width: 1400px) {
  .grid-dashboard {
    gap: 1rem;
  }

  .stats-kpi-grid {
    grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  }

  .kpi-value {
    font-size: 1.65rem;
  }

  .chart-box.col-8 {
    grid-column: span 12;
  }

  .chart-box.col-4 {
    grid-column: span 12;
  }

  .ai-studio-container {
    grid-template-columns: 1fr;
    gap: 1.25rem;
  }
}

/* ==========================================================================
   4. TABLETTES HORIZONTALES & PETITS LAPTOPS (max-width: 1024px)
   ========================================================================== */

@media (max-width: 1024px) {
  .sidebar {
    width: 240px;
  }

  .kpi-card {
    grid-column: span 6;
  }

  .chart-card-large, .chart-card-small {
    grid-column: span 12;
  }

  .dashboard-split-grid {
    grid-template-columns: 1fr;
    gap: 1.25rem;
  }

  .chart-box.col-6 {
    grid-column: span 12;
  }

  .stats-filters-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.85rem;
  }

  .stats-filters-pills {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.35rem;
    flex-wrap: nowrap;
    width: 100%;
  }

  .stats-filter-pill {
    flex-shrink: 0;
  }

  .correcteur-studio-container {
    grid-template-columns: 1fr;
  }

  .secours-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ==========================================================================
   5. TABLETTES VERTICALES (max-width: 900px)
   ========================================================================== */

@media (max-width: 900px) {
  /* Sidebar as Off-Canvas Drawer */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    height: 100vh;
    height: 100dvh;
    width: 280px;
    max-width: 82vw;
    z-index: 1200;
    transform: translateX(-100%);
    box-shadow: var(--shadow-lg);
    transition: transform 300ms cubic-bezier(0.16, 1, 0.3, 1);
    will-change: transform;
  }

  .sidebar.mobile-open {
    transform: translateX(0);
  }

  .mobile-sidebar-close {
    display: flex;
  }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 1150;
    opacity: 0;
    pointer-events: none;
    transition: opacity 280ms ease;
  }

  .sidebar-backdrop.active,
  .sidebar.mobile-open ~ .sidebar-backdrop,
  body.sidebar-open .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .topbar {
    padding: 0 1.25rem;
    height: 64px;
  }

  .topbar-left {
    gap: 0.75rem;
  }

  .role-switcher-container {
    display: none;
  }

  .accounting-journal-table,
  .copro-table,
  .ag-table {
    min-width: 600px;
  }

  .ag-resolution-card {
    padding: 1.25rem;
  }

  .suivi-card {
    padding: 1.15rem;
  }

  .secours-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
  }
}

/* ==========================================================================
   6. SMARTPHONES STANDARDS & PHABLETTES (max-width: 768px)
   ========================================================================== */

@media (max-width: 768px) {
  /* Layout Spacing with Mobile Safe Areas */
  .topbar {
    height: 58px;
    padding: var(--safe-top) 0.85rem 0 0.85rem;
    gap: 0.5rem;
    position: sticky;
    top: 0;
    z-index: 990;
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
  }

  .page-title-group h1 {
    font-size: 1.05rem;
    max-width: 150px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .page-subtitle {
    display: none;
  }

  /* Content area with bottom padding for thumb navigation */
  .content-body {
    padding: 0.85rem 0.85rem calc(var(--mobile-bottom-nav-height) + var(--safe-bottom) + 16px) 0.85rem;
  }

  /* Bottom Navigation Bar for Mobile */
  .mobile-bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(var(--mobile-bottom-nav-height) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: rgba(15, 23, 42, 0.95);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--border-color);
    z-index: 1000;
    justify-content: space-around;
    align-items: center;
  }

  .bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    height: 100%;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.7rem;
    font-weight: 500;
    gap: 0.2rem;
    transition: color 0.2s ease, transform 0.15s ease;
    min-height: var(--touch-target-min);
  }

  .bottom-nav-item i {
    width: 20px;
    height: 20px;
  }

  .bottom-nav-item.active {
    color: var(--color-primary);
    font-weight: 600;
  }

  .bottom-nav-item:active {
    transform: scale(0.92);
  }

  /* Dashboard & Bento KPIs */
  .grid-dashboard {
    gap: 0.85rem;
  }

  .kpi-card {
    grid-column: span 12;
    padding: 1rem;
    border-radius: var(--radius-md);
  }

  .kpi-value {
    font-size: 1.45rem;
  }

  .stats-kpi-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  .stats-kpi-card {
    padding: 1rem;
  }

  .stats-kpi-value {
    font-size: 1.35rem;
  }

  .chart-canvas-wrap {
    min-height: 250px;
  }

  /* Quick Action & Export Buttons Compact */
  #btn-quick-action span,
  .btn-export-text,
  .chevron-icon {
    display: none;
  }

  #btn-quick-action,
  .btn-export-toggle {
    padding: 0.45rem;
    min-width: var(--touch-target-min);
    height: var(--touch-target-min);
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* AI Assistant Mobile Experience */
  .ai-studio-container {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .ai-chat-live-container {
    height: calc(100vh - 220px);
    height: calc(100dvh - 220px);
  }

  .ai-chat-input-bar {
    position: sticky;
    bottom: 0;
    background: var(--bg-card);
    padding: 0.75rem 0.5rem;
    z-index: 10;
  }

  .ai-prompt-tools-grid {
    grid-template-columns: 1fr;
    gap: 0.5rem;
  }

  .ai-prompt-chip {
    width: 100%;
    justify-content: flex-start;
  }

  /* Correcteur Studio */
  .correcteur-studio-container {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .correcteur-metrics-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
  }

  .correcteur-actions-bar {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .correcteur-actions-bar .btn {
    flex: 1;
    min-width: 120px;
  }

  /* Secours 24/7 Grid */
  .secours-grid {
    grid-template-columns: 1fr;
    gap: 0.85rem;
  }

  .secours-card {
    padding: 1rem;
  }

  .secours-call-btn {
    width: 100%;
    min-height: var(--touch-target-min);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Suivi Dossiers & Mails */
  .suivi-channels-bar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    padding-bottom: 0.35rem;
    width: 100%;
  }

  .suivi-channel-chip {
    flex-shrink: 0;
  }

  .suivi-card {
    padding: 1rem;
  }

  .suivi-meta-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.4rem;
  }

  /* Assemblées Générales & Votes */
  .ag-resolution-card {
    padding: 1rem;
  }

  .ag-voting-box {
    flex-direction: column;
    gap: 0.5rem;
  }

  .ag-vote-btn {
    width: 100%;
    min-height: var(--touch-target-min);
  }

  /* Mains Courantes & OS */
  .events-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }

  .events-filter-tags {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    width: 100%;
    padding-bottom: 0.25rem;
  }

  /* Top Navigation Ribbon Mobile Adaptation */
  .top-nav-bar {
    top: 58px;
    padding: 0.3rem 0.5rem;
    gap: 0.35rem;
  }

  .top-nav-btn,
  .top-nav-arrow {
    width: 28px;
    height: 28px;
    min-width: 28px;
  }

  .top-nav-item {
    padding: 0.32rem 0.65rem;
    font-size: 0.75rem;
  }

  /* Prestataires (4 Emails, GPS Google Maps & Multi-Notes) Mobile Adaptations */
  .provider-card {
    padding: 1rem;
  }

  .provider-emails-grid,
  .emails-four-grid {
    grid-template-columns: 1fr;
    gap: 0.35rem;
  }

  .provider-gps {
    flex-direction: column;
    align-items: flex-start;
  }

  .provider-notes {
    width: 100%;
  }

  /* Modals as Responsive Bottom Sheets */
  .modal-backdrop {
    padding: 0;
    align-items: flex-end;
  }

  .modal-card,
  .calendar-rdv-modal,
  .reminder-modal {
    width: 100%;
    max-width: 100%;
    max-height: 88vh;
    max-height: 88dvh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    margin: 0;
    animation: modalSpringMobile 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
  }

  .modal-header {
    padding: 1rem 1.15rem;
  }

  .modal-body {
    padding: 1.15rem;
    max-height: calc(88dvh - 140px);
    overflow-y: auto;
  }

  .modal-footer,
  .rdv-form-footer,
  .reminder-form-footer {
    padding: 0.85rem 1.15rem calc(0.85rem + var(--safe-bottom)) 1.15rem;
    flex-direction: column-reverse;
    gap: 0.5rem;
    position: sticky;
    bottom: 0;
    background: var(--bg-card);
    z-index: 5;
  }

  .modal-footer .btn,
  .rdv-form-footer .btn,
  .reminder-form-footer .btn {
    width: 100%;
    min-height: var(--touch-target-min);
  }

  /* Toasts positioned above Mobile Bottom Nav */
  .toast-wrapper {
    left: 1rem;
    right: 1rem;
    bottom: calc(var(--mobile-bottom-nav-height) + var(--safe-bottom) + 12px);
    align-items: center;
  }

  .toast {
    min-width: unset;
    width: 100%;
    max-width: 440px;
    padding: 0.85rem 1rem;
  }

  /* Forms and Controls */
  .form-row {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  /* iOS Auto-Zoom Prevention (16px minimum font size) */
  input, select, textarea, .form-control, .auth-input {
    font-size: 16px !important;
  }
}

/* ==========================================================================
   7. SMARTPHONES COMPACTS (max-width: 480px)
   ========================================================================== */

@media (max-width: 480px) {
  .topbar {
    padding: var(--safe-top) 0.65rem 0 0.65rem;
  }

  .page-title-group h1 {
    font-size: 0.95rem;
    max-width: 120px;
  }

  .content-body {
    padding: 0.75rem 0.65rem calc(var(--mobile-bottom-nav-height) + var(--safe-bottom) + 12px) 0.65rem;
  }

  .card {
    padding: 0.85rem;
  }

  .kpi-value {
    font-size: 1.35rem;
  }

  .btn {
    padding: 0.5rem 0.85rem;
    font-size: 0.82rem;
    min-height: var(--touch-target-min);
  }

  .btn-sm {
    padding: 0.35rem 0.65rem;
    font-size: 0.76rem;
  }

  .auth-card {
    padding: 1.5rem 1.15rem;
    width: calc(100% - 1.25rem);
    margin: 0 auto;
  }

  .auth-demo-creds-grid {
    grid-template-columns: 1fr;
    gap: 0.4rem;
  }

  .stats-breakdown-card {
    padding: 1rem 0.85rem;
  }

  .chart-canvas-wrap {
    min-height: 220px;
  }
}

/* ==========================================================================
   8. TRÈS PETITS ÉCRANS & APPAREILS PLIABLES
   (max-width: 360px, 320px, 280px & dual-screen postures)
   ========================================================================== */

@media (max-width: 360px) {
  .page-title-group h1 {
    display: none;
  }

  .bottom-nav-item {
    font-size: 0.62rem;
  }

  .bottom-nav-item i {
    width: 18px;
    height: 18px;
  }

  .kpi-value {
    font-size: 1.2rem;
  }
}

/* Pliables fermés (Galaxy Z Fold cover screen ~280-320px) */
@media (max-width: 320px) {
  :root {
    --touch-target-min: 42px;
  }

  .content-body {
    padding: 0.5rem 0.4rem calc(var(--mobile-bottom-nav-height) + var(--safe-bottom) + 8px) 0.4rem;
  }

  .topbar {
    padding: var(--safe-top) 0.4rem 0 0.4rem;
  }

  .top-nav-arrow,
  .top-nav-btn {
    width: 26px;
    height: 26px;
    min-width: 26px;
  }

  .modal-card,
  .calendar-rdv-modal,
  .reminder-modal {
    max-height: 94dvh;
    border-radius: 12px 12px 0 0;
  }

  .kpi-value {
    font-size: 1.15rem;
  }
}

/* Écrans de couverture ultra-compacts (Galaxy Fold extérieur) */
@media (max-width: 280px) {
  .page-title-group {
    display: none;
  }

  .topbar-right {
    gap: 0.25rem;
  }

  .floating-clock-pill {
    font-size: 0.72rem;
    padding: 0.25rem 0.5rem;
  }

  .btn {
    font-size: 0.75rem;
    padding: 0.35rem 0.6rem;
  }
}

/* Posture dépliée à deux écrans côte à côte (Surface Duo, Galaxy Fold ouvert) */
@media (horizontal-viewport-segments: 2) or (screen-spanning: single-fold-vertical) {
  .dashboard-split-grid,
  .ai-studio-container,
  .correcteur-studio-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
  }
}

/* ==========================================================================
   9. MODE PAYSAGE MOBILE (max-height: 550px and orientation: landscape)
   ========================================================================== */

@media (max-height: 550px) and (orientation: landscape), (max-height: 500px) and (orientation: landscape) {
  .topbar {
    height: 48px;
  }

  .modal-card,
  .calendar-rdv-modal,
  .reminder-modal {
    max-height: 94vh;
    max-height: 94dvh;
  }

  .modal-body {
    max-height: calc(94dvh - 100px);
  }

  .chart-canvas-wrap {
    min-height: 180px;
  }

  .content-body {
    padding-bottom: 2rem;
  }
}

/* ==========================================================================
   10. OPTIMISATIONS ÉCRANS TACTILES (hover: none and pointer: coarse)
   ========================================================================== */

@media (hover: none) and (pointer: coarse) {
  /* Enforce smooth touch response & remove mouse hover locks */
  .btn,
  .nav-item,
  .bottom-nav-item,
  .icon-btn,
  .top-nav-btn,
  .top-nav-arrow,
  .stats-filter-pill,
  .suivi-channel-chip,
  .export-menu-item {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }

  .btn:active,
  .icon-btn:active,
  .top-nav-btn:active,
  .top-nav-arrow:active {
    transform: scale(0.95);
  }

  /* Simplify 3D tilts for battery & performance on mobile */
  .card:hover,
  .kpi-card:hover,
  .stats-kpi-card:hover {
    transform: none;
  }

  /* Actions rapides du calendrier (+ Rappel, + RDV) visibles et tactiles sans hover souris */
  .day-quick-actions {
    opacity: 1 !important;
    transform: none !important;
    pointer-events: auto !important;
  }

  .btn-day-action {
    min-height: 28px;
    padding: 0.2rem 0.45rem;
    font-size: 0.72rem;
    touch-action: manipulation;
  }

  .day-number-row {
    padding-bottom: 2px;
  }
}

/* ==========================================================================
   11. MODE IMPRESSION PAPIER & EXPORT PDF (@media print)
   ========================================================================== */

@media print {
  body {
    background: #ffffff !important;
    color: #000000 !important;
  }

  .sidebar,
  .topbar,
  .top-nav-bar,
  .mobile-bottom-nav,
  .sidebar-backdrop,
  .btn,
  .icon-btn,
  .export-dropdown-container,
  .toast-wrapper,
  .auth-overlay {
    display: none !important;
  }

  .main-wrapper {
    margin: 0 !important;
    padding: 0 !important;
    width: 100% !important;
  }

  .content-body {
    padding: 0 !important;
    max-width: 100% !important;
  }

  .card,
  .kpi-card,
  .stats-breakdown-card,
  .chart-box {
    box-shadow: none !important;
    border: 1px solid #ddd !important;
    break-inside: avoid;
    page-break-inside: avoid;
  }
}

/* Mobile Bottom Sheet Spring Animation */
@keyframes modalSpringMobile {
  from {
    transform: translateY(100%);
    opacity: 0.5;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
