/**
 * AGRISOFT ERP — Responsive (admin)
 * Complète layout.css / erp-modules.css pour mobile, tablette et grands écrans.
 */

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html,
body {
  max-width: 100%;
  overflow-x: clip;
}

img,
video,
canvas,
svg {
  max-width: 100%;
  height: auto;
}

#main-content,
.page-body,
#topbar,
.module-tab-bar,
.erp-table-shell {
  max-width: 100%;
}

.module-tab-bar,
.module-tab-bar-inner,
.module-tab-bar-scroll {
  box-sizing: border-box;
}

/* Overlay sidebar : ne jamais bloquer le contenu quand le menu est fermé */
.sidebar-overlay.d-none {
  display: none !important;
  pointer-events: none !important;
  visibility: hidden !important;
}

/* Contenu principal toujours accessible au toucher */
#main-content,
.page-body,
.module-tab-panels,
.erp-table-scroll {
  position: relative;
  z-index: 1;
}

.erp-help-overlay:not(.is-open) {
  pointer-events: none !important;
}

/* ── Tablette paysage ── */
@media (max-width: 1200px) {
  .page-body { padding: 22px; }
  .chart-card { padding: 16px; }
}

@media (max-width: 1100px) {
  #topbar { padding: 0 16px; gap: 12px; }
  .topbar-search { max-width: 280px; min-width: 0; }
  .hero-card { padding: 26px 28px; }
}

/* ── Tablette / sidebar repliée ── */
@media (max-width: 992px) {
  #topbar { padding: 0 12px; gap: 10px; }
  .topbar-pagetitle { min-width: 0; flex-shrink: 1; }
  .topbar-page-name {
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .topbar-search { max-width: 220px; }
  .topbar-user-dropdown {
    right: 0;
    left: auto;
    width: min(300px, calc(100vw - 24px));
  }
  .dash-section-head { align-items: flex-start; }
  .erp-mini-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .module-zones-row { grid-template-columns: 1fr; }

  .module-tab-bar {
    width: 100%;
    left: 0;
    right: 0;
  }
  .module-tab-bar-inner {
    width: 100%;
    max-width: none;
    padding: 0 10px;
  }
  .module-tab-bar-scroll {
    width: 100%;
    justify-content: flex-start;
  }
  .module-tab-bar .module-menu-tabs {
    width: 100%;
    min-width: 100%;
    justify-content: flex-start;
    margin: 0;
  }
}

/* ── Mobile ── */
@media (max-width: 768px) {
  :root {
    --topbar-h: 60px;
    --module-tabbar-h: 50px;
  }

  #topbar {
    padding: 0 10px;
    gap: 8px;
  }

  .topbar-page-name {
    font-size: 15px;
    max-width: 130px;
  }

  .live-indicator {
    padding: 3px 8px;
    font-size: 10px;
  }

  .topbar-search input {
    padding: 8px 12px 8px 34px;
    font-size: 13px;
  }

  .topbar-search { max-width: none; flex: 1; min-width: 0; }
  .topbar-actions { gap: 5px; flex-shrink: 0; margin-left: auto; }
  .farm-selector { padding: 5px 8px; font-size: 11.5px; max-width: 140px; }
  .farm-selector span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .topbar-profile-info--desktop { display: none; }
  .topbar-profile-trigger { max-width: 44px; padding: 4px 6px; }
  .topbar-profile-chevron { display: none; }

  .page-body { padding: 14px 12px; }
  body.has-module-tabs .page-body { padding-top: 14px; }

  .hero-card { padding: 18px 16px; border-radius: 16px; margin-bottom: 18px; }
  .hero-title { font-size: 20px; }
  .hero-sub { font-size: 13px; margin-bottom: 14px; }
  .hero-stats { gap: 14px; }
  .hero-stat-val { font-size: 18px; }

  .kpi-card { padding: 14px; }
  .kpi-value { font-size: 22px; }

  .erp-list-toolbar {
    padding: 10px 12px;
    gap: 10px;
    justify-content: flex-start;
    align-items: flex-start;
    flex-direction: column;
  }
  .erp-list-toolbar .erp-btn-create {
    order: -1;
    align-self: flex-start;
  }
  .erp-list-toolbar .erp-list-meta {
    align-self: flex-start;
  }

  .erp-quick-actions {
    justify-content: flex-start;
  }

  .erp-table { font-size: 12.5px; }
  .erp-table thead th,
  .erp-table tbody td {
    padding: 9px 10px;
  }
  .erp-table-scroll {
    margin: 0 -1px;
    scrollbar-width: thin;
  }

  .erp-module-grid { gap: 14px; }
  .erp-chat-ia { padding: 14px; }
  .erp-chat-ia .chat-prompt { flex-direction: column; }
  .erp-chat-ia .chat-prompt .btn { width: 100%; }

  .chart-card,
  .erp-table-shell {
    border-radius: 12px;
  }

  .modal-dialog {
    margin: 0.65rem auto;
    max-width: calc(100vw - 1.25rem);
  }
  .modal-dialog.modal-lg,
  .modal-dialog.modal-xl {
    max-width: calc(100vw - 1.25rem);
  }
  .cms-modal-form .modal-body,
  .erp-delete-modal-body {
    padding-left: 1rem !important;
    padding-right: 1rem !important;
  }
  .row.g-3 > [class*="col-"],
  .row.g-2 > [class*="col-"] {
    margin-bottom: 0;
  }

  .erp-toast-container {
    left: 12px;
    right: 12px;
    top: calc(var(--topbar-h) + 8px);
  }
  .erp-toast {
    min-width: 0;
    width: 100%;
  }

  .apexcharts-canvas { max-width: 100% !important; }
  .apexcharts-toolbar { transform: scale(0.9); transform-origin: top right; }
}

/* ── Topbar deux lignes (petits smartphones) ── */
@media (max-width: 640px) {
  :root {
    --topbar-h: 96px;
  }

  #topbar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    align-items: center;
    align-content: center;
    min-height: var(--topbar-h);
    height: auto;
    padding: 8px 10px 10px;
    gap: 6px 8px;
    margin-left: 0;
  }

  #topbar .btn-sidebar-toggle {
    grid-column: 1;
    grid-row: 1;
    align-self: center;
  }

  .topbar-pagetitle {
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
    overflow: hidden;
  }

  .topbar-actions {
    grid-column: 3;
    grid-row: 1;
    margin-left: 0;
    flex-wrap: nowrap;
    gap: 4px;
    justify-self: end;
    max-width: 100%;
  }

  .topbar-search {
    grid-column: 1 / -1;
    grid-row: 2;
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  .topbar-page-name {
    max-width: 100%;
    font-size: 14px;
  }

  .farm-selector {
    padding: 5px 7px;
    max-width: 36px;
    overflow: hidden;
    justify-content: center;
  }

  .farm-selector span {
    display: none;
  }

  .topbar-profile-info--desktop {
    display: none;
  }

  .topbar-profile-trigger {
    max-width: 44px;
    padding: 4px;
  }

  .topbar-profile-chevron {
    display: none;
  }

  .erp-help-btn {
    min-width: 36px;
    padding: 0 8px;
  }
}

/* ── Très petits écrans ── */
@media (max-width: 480px) {
  :root {
    --topbar-h: 92px;
    --module-tabbar-h: 46px;
  }

  .module-tab-bar-inner {
    padding: 0 8px;
  }

  .live-indicator { display: none; }
  .topbar-page-name { font-size: 13px; }
  .page-body { padding: 12px 10px; }

  .erp-mini-kpis { grid-template-columns: 1fr; }
  .kpi-grid { gap: 10px; }

  .module-tab-bar .module-menu-tab {
    min-width: 44px;
    justify-content: flex-start;
    padding: 0 12px;
  }

  .topbar-user-dropdown {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-width: none;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 32px rgba(15, 23, 42, 0.18);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .erp-help-panel {
    width: 100vw;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .dewebot-toggle {
    bottom: calc(16px + env(safe-area-inset-bottom, 0));
    right: 14px;
  }
  .dewebot-window {
    bottom: calc(84px + env(safe-area-inset-bottom, 0));
  }
}

/* ── Safe areas (encoches) ── */
@supports (padding: env(safe-area-inset-top)) {
  #topbar {
    padding-left: max(10px, env(safe-area-inset-left));
    padding-right: max(10px, env(safe-area-inset-right));
  }
  .page-body {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
}

/* ── Grands écrans ── */
@media (min-width: 1600px) {
  .page-body {
    max-width: 1680px;
    margin-left: auto;
    margin-right: auto;
  }
}

/* ── Préférence mouvement réduit ── */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
