/**
 * Dynasty Mobile — Android · iPhone · iPad · Tablette
 * Navigation basse · Safe area · Gestes · PWA
 */

/* ==========================================================================
   Variables mobile
   ========================================================================== */
:root {
  --mobile-nav-height: 64px;
  --mobile-fab-size: 52px;
  --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);
}

/* Hauteur viewport iOS (barre d'adresse) */
@supports (height: 100dvh) {
  .dynasty-app .wrapper,
  .dynasty-app .content-wrapper {
    min-height: 100dvh;
  }
}

/* Safe area — iPhone encoche / barre home */
.dynasty-app .main-header.navbar {
  padding-top: max(0.25rem, var(--safe-top));
  padding-left: max(0.5rem, var(--safe-left));
  padding-right: max(0.5rem, var(--safe-right));
}

.dynasty-app .main-sidebar {
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

/* ==========================================================================
   Barre de navigation inférieure (smartphone)
   ========================================================================== */
.mobile-bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1045;
  height: calc(var(--mobile-nav-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 24px rgba(15, 23, 42, 0.08);
  justify-content: space-around;
  align-items: stretch;
}

@media (max-width: 991.98px) {
  .mobile-bottom-nav {
    display: flex;
  }

  .dynasty-app .content-wrapper {
    padding-bottom: calc(var(--mobile-nav-height) + var(--safe-bottom) + 0.5rem) !important;
  }

  .dynasty-app .main-footer {
    margin-bottom: calc(var(--mobile-nav-height) + var(--safe-bottom));
  }

  .back-to-top {
    bottom: calc(var(--mobile-nav-height) + var(--safe-bottom) + 1rem) !important;
  }

  /* Masquer footer texte sur très petit écran */
  .main-footer .float-right {
    display: none !important;
  }
}

.mobile-nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  padding: 0.375rem 0.25rem;
  border: none;
  background: transparent;
  color: var(--text-muted);
  text-decoration: none !important;
  font-size: 0.625rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  min-height: var(--mobile-nav-height);
  transition: color 0.15s ease, transform 0.1s ease;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}

.mobile-nav-item i {
  font-size: 1.25rem;
  line-height: 1;
}

.mobile-nav-item.active {
  color: var(--primary);
}

.mobile-nav-item.active i {
  transform: scale(1.05);
}

.mobile-nav-item:active {
  transform: scale(0.92);
}

/* Bouton central surélevé — Nouveau colis */
.mobile-nav-fab {
  flex: 0 0 auto;
  width: var(--mobile-fab-size);
  height: var(--mobile-fab-size);
  margin-top: -18px;
  border-radius: 50%;
  background: var(--accent-gradient);
  color: #fff !important;
  box-shadow: 0 4px 16px rgba(37, 99, 235, 0.45);
  font-size: 0.5625rem;
  min-height: auto;
  align-self: center;
}

.mobile-nav-fab i {
  font-size: 1.375rem;
}

.mobile-nav-fab.active,
.mobile-nav-fab:active {
  color: #fff !important;
}

[data-theme="dark"] .mobile-bottom-nav {
  background: var(--bg-surface);
  border-color: var(--border-strong);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.35);
}

/* ==========================================================================
   Tablette (iPad, Android tablet) — 768px à 991px
   ========================================================================== */
@media (min-width: 768px) and (max-width: 991.98px) {
  .content-header h1 {
    font-size: 1.5rem;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .table-scroll-container .table,
  .table-scroll-container table.dataTable {
    min-width: 100%;
  }

  .mobile-bottom-nav {
    max-width: 480px;
    left: 50%;
    transform: translateX(-50%);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }

  .global-search-trigger span {
    display: inline !important;
  }
}

/* ==========================================================================
   Split view tablette — formulaire gauche · liste droite
   ========================================================================== */
@media (min-width: 768px) and (max-width: 991.98px) {
  .tablet-split {
    display: flex !important;
    flex-wrap: nowrap !important;
    align-items: flex-start !important;
    gap: 0;
    margin-left: -0.375rem !important;
    margin-right: -0.375rem !important;
  }

  .tablet-split > .col-lg-4,
  .tablet-split > .col-lg-8 {
    order: unset !important;
    min-width: 0;
    padding-left: 0.375rem;
    padding-right: 0.375rem;
    margin-bottom: 0 !important;
  }

  .tablet-split > .col-lg-4 {
    flex: 0 0 38% !important;
    max-width: 38% !important;
    width: 38% !important;
    position: relative;
  }

  .tablet-split > .col-lg-8 {
    flex: 1 1 62% !important;
    max-width: 62% !important;
    width: 62% !important;
  }

  /* Séparateur vertical entre les panneaux */
  .tablet-split > .col-lg-4::after {
    content: '';
    position: absolute;
    top: 0.5rem;
    bottom: 0.5rem;
    right: -1px;
    width: 2px;
    background: linear-gradient(180deg, transparent, var(--border) 15%, var(--border) 85%, transparent);
    pointer-events: none;
    z-index: 2;
  }

  /* Panneau formulaire — scroll indépendant + sticky */
  .tablet-split > .col-lg-4 .card {
    position: sticky;
    top: calc(var(--header-height) + 0.5rem);
    max-height: calc(100dvh - var(--header-height) - 1.25rem);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    border-radius: var(--radius-lg) !important;
    box-shadow: var(--shadow-md) !important;
  }

  .tablet-split > .col-lg-4 .card-body {
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    flex: 1 1 auto;
    padding: 1rem !important;
  }

  /* Champs internes du formulaire empilés dans le panneau étroit */
  .tablet-split > .col-lg-4 .dynasty-form .col-md-6,
  .tablet-split > .col-lg-4 form .col-md-6,
  .tablet-split > .col-lg-4 .row > [class*="col-md-"] {
    flex: 0 0 100% !important;
    max-width: 100% !important;
  }

  .tablet-split > .col-lg-4 .border-right {
    border-right: none !important;
    border-bottom: none !important;
    padding-bottom: 0 !important;
    margin-bottom: 0 !important;
  }

  /* Panneau liste */
  .tablet-split > .col-lg-8 .card {
    border-radius: var(--radius-lg) !important;
    min-height: calc(100dvh - var(--header-height) - 6rem);
    display: flex;
    flex-direction: column;
  }

  .tablet-split > .col-lg-8 .card-body {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 0.75rem !important;
  }

  .tablet-split > .col-lg-8 .table-scroll-container {
    flex: 1 1 auto;
    max-height: calc(100dvh - var(--header-height) - 11rem);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .tablet-split > .col-lg-8 .dataTables_wrapper {
    flex: 1 1 auto;
    min-height: 0;
  }

  /* Labels panneaux */
  .tablet-split-panel-tag {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    font-size: 0.625rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    padding: 0.5rem 0.75rem 0.25rem;
  }

  .tablet-split-panel-tag i {
    color: var(--primary);
    font-size: 0.6875rem;
  }

  .tablet-split > .col-lg-4 .card-form-header,
  .tablet-split > .col-lg-8 .card-list-header {
    padding: 0.625rem 0.875rem !important;
  }

  .tablet-split > .col-lg-4 .card-form-header h3,
  .tablet-split > .col-lg-8 .card-list-header h3 {
    font-size: 0.875rem !important;
  }

  /* DataTables compact dans panneau droit */
  .tablet-split .dataTables_length,
  .tablet-split .dataTables_filter {
    font-size: 0.75rem !important;
  }

  .tablet-split .dataTables_filter input {
    max-width: 140px !important;
    min-width: 80px !important;
  }

  /* Masquer fil d'Ariane long sur split pour gagner de l'espace */
  .has-tablet-split .dynasty-breadcrumb {
    display: none;
  }

  .has-tablet-split .content-header {
    padding: 0.75rem 1rem 0.625rem;
  }

  .has-tablet-split section.content {
    padding: 0.625rem 0.75rem 1rem;
  }
}

/* Split view — paysage tablette : ratio 42 / 58 */
@media (min-width: 768px) and (max-width: 991.98px) and (orientation: landscape) {
  .tablet-split > .col-lg-4 {
    flex: 0 0 42% !important;
    max-width: 42% !important;
    width: 42% !important;
  }

  .tablet-split > .col-lg-8 {
    flex: 1 1 58% !important;
    max-width: 58% !important;
    width: 58% !important;
  }

  .tablet-split > .col-lg-4 .card {
    max-height: calc(100dvh - var(--header-height) - 0.75rem);
  }
}

[data-theme="dark"] .tablet-split > .col-lg-4::after {
  background: linear-gradient(180deg, transparent, var(--border-strong) 15%, var(--border-strong) 85%, transparent);
}

/* Tablette landscape */
@media (min-width: 768px) and (max-width: 991.98px) and (orientation: landscape) {
  .mobile-bottom-nav {
    height: calc(56px + var(--safe-bottom));
  }

  .mobile-nav-item {
    flex-direction: row;
    gap: 0.375rem;
    font-size: 0.6875rem;
    text-transform: none;
  }

  .mobile-nav-fab {
    margin-top: -12px;
    width: 46px;
    height: 46px;
  }
}

/* ==========================================================================
   Smartphone paysage
   ========================================================================== */
@media (max-width: 767.98px) and (orientation: landscape) {
  .content-header {
    padding: 0.625rem 1rem !important;
  }

  .page-icon-badge {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }

  .page-header-text h1 {
    font-size: 1.125rem !important;
  }

  .page-greeting,
  .dynasty-breadcrumb {
    display: none !important;
  }

  .mobile-bottom-nav {
    height: calc(52px + var(--safe-bottom));
  }

  .mobile-nav-item span {
    display: none;
  }

  .mobile-nav-fab span {
    display: none;
  }
}

/* ==========================================================================
   Touch & interactions tactiles
   ========================================================================== */
@media (hover: none) and (pointer: coarse) {
  .btn,
  .nav-link,
  .dropdown-item,
  .card,
  .global-search-item {
    -webkit-tap-highlight-color: transparent;
  }

  .btn:active,
  .nav-link:active:not(.nav-is-tree) {
    transform: scale(0.97);
    opacity: 0.9;
  }

  .card:hover {
    transform: none;
  }

  .kpi-card:hover {
    transform: none;
  }

  /* Zones cliquables minimum 44px */
  .dropdown-item,
  .paginate_button,
  .dataTables_wrapper .dataTables_paginate .paginate_button {
    min-height: 44px !important;
    display: inline-flex !important;
    align-items: center !important;
  }
}

/* ==========================================================================
   Modales plein écran sur mobile
   ========================================================================== */
@media (max-width: 575.98px) {
  .modal-dialog {
    margin: 0 !important;
    max-width: 100% !important;
    min-height: 100%;
  }

  .modal-dialog-centered {
    min-height: 100% !important;
    align-items: flex-end !important;
  }

  .modal-content {
    border-radius: var(--radius-xl) var(--radius-xl) 0 0 !important;
    min-height: 50vh;
    max-height: 92vh;
    max-height: 92dvh;
    overflow-y: auto;
  }

  .modal-dialog-centered .modal-content {
    margin-top: auto;
  }

  .modal-footer {
    position: sticky;
    bottom: 0;
    z-index: 2;
    padding-bottom: calc(1rem + var(--safe-bottom)) !important;
  }

  .modal-footer .btn {
    min-height: 48px;
    flex: 1;
  }

  .modal-footer {
    display: flex;
    gap: 0.5rem;
  }
}

/* ==========================================================================
   iOS — éviter zoom auto sur focus input
   ========================================================================== */
@media (max-width: 991.98px) {
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="tel"],
  input[type="search"],
  input[type="date"],
  input[type="time"],
  select,
  textarea,
  .form-control {
    font-size: 16px !important;
  }
}

/* ==========================================================================
   Bandeau orientation (optionnel visuel)
   ========================================================================== */
.mobile-scroll-hint-bar {
  display: none;
  position: fixed;
  bottom: calc(var(--mobile-nav-height) + var(--safe-bottom) + 0.5rem);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.85);
  color: #fff;
  font-size: 0.6875rem;
  padding: 0.375rem 0.75rem;
  border-radius: var(--radius-full);
  z-index: 1040;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
  white-space: nowrap;
}

.mobile-scroll-hint-bar.show {
  opacity: 1;
}

/* PWA standalone */
@media (display-mode: standalone) {
  .dynasty-app .main-header.navbar {
    padding-top: max(0.5rem, var(--safe-top));
  }
}

/* Android Chrome — barre d'adresse */
@supports (-webkit-touch-callout: none) {
  .dynasty-app.fixed-layout .content-wrapper {
    -webkit-overflow-scrolling: touch;
  }
}

/* Réduire animations sur mobile faible perf */
@media (max-width: 767.98px) and (prefers-reduced-motion: no-preference) {
  .dynasty-app section.content .container-fluid {
    animation-duration: 0.25s;
  }
}

[data-theme="dark"] .mobile-scroll-hint-bar {
  background: rgba(30, 41, 59, 0.95);
}
