/* ==========================================================================
   BizFlow — responsive.css
   Points de rupture :
   - ≤ 1280px : grand écran réduit (KPI 2 colonnes)
   - ≤ 1024px : tablette (sidebar en tiroir, grilles empilées)
   - ≤ 768px  : mobile (recherche en plein écran, tableaux en cartes)
   - ≤ 480px  : petit mobile
   ========================================================================== */

/* ---------- ≤ 1280px ---------- */
@media (max-width: 1280px) {
  :root {
    --page-pad: 24px;
  }

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

  /* Bandeau des chiffres sur deux lignes : filets entre les cellules */
  .stat-card:nth-child(odd) {
    border-left: none;
  }

  .stat-card:nth-child(n + 3) {
    border-top: 1px solid var(--border);
  }

  .dashboard-grid--charts {
    grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr);
  }
}

/* Opérations du jour : sur téléphone, « Payé » se déduit du montant et du reste dû */
@media (max-width: 640px) {
  .ops-table [data-cell="paid"] {
    display: none;
  }
}

/* ---------- ≤ 1024px : sidebar en tiroir ---------- */
@media (max-width: 1024px) {
  .main-wrapper,
  .sidebar-collapsed .main-wrapper {
    margin-left: 0;
  }

  /* Le tiroir ignore l'état « réduit » du desktop */
  /* Fermé, le tiroir est aussi retiré du parcours clavier et des lecteurs
     d'écran (visibility) — masqué seulement après l'animation de sortie */
  .sidebar,
  .sidebar-collapsed .sidebar {
    width: min(300px, 86vw);
    transform: translateX(-100%);
    box-shadow: none;
    visibility: hidden;
    transition: transform var(--dur-slow) var(--ease-out), visibility 0s linear var(--dur-slow);
  }

  .sidebar-open .sidebar {
    transform: none;
    box-shadow: var(--shadow-lg);
    visibility: visible;
    transition: transform var(--dur-slow) var(--ease-out), visibility 0s;
  }

  .sidebar-collapsed .brand__name,
  .sidebar-collapsed .nav__label,
  .sidebar-collapsed .nav__badge,
  .sidebar-collapsed .workspace__info {
    opacity: 1;
    pointer-events: auto;
    width: auto;
  }

  .sidebar-collapsed .sidebar__brand { padding: 0 18px; }
  .sidebar-collapsed .sidebar__workspace {
    justify-content: flex-start;
    padding: 8px 10px;
    border-color: var(--border);
    background: var(--surface-2);
  }
  .sidebar-collapsed .nav__section {
    height: auto;
    margin: 0;
    padding: 16px 10px 6px;
    color: var(--text-3);
    background: none;
  }
  .sidebar-collapsed .nav__link {
    justify-content: flex-start;
    gap: 12px;
    padding: 0 10px;
  }
  .sidebar-collapsed .nav__link[data-tooltip]:hover::before { content: none; }
  .sidebar-collapsed .nav__link:has(.nav__badge:not([hidden]))::after { content: none; }

  .icon-btn.sidebar__close {
    display: inline-grid;
  }

  .sidebar__footer {
    display: none;
  }

  .sidebar-overlay {
    position: fixed;
    inset: 0;
    z-index: 55;
    background: rgba(10, 10, 20, 0.45);
    backdrop-filter: blur(2px);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--dur-slow) var(--ease), visibility var(--dur-slow);
  }

  .sidebar-open .sidebar-overlay {
    opacity: 1;
    visibility: visible;
  }

  .sidebar-open {
    overflow: hidden;
  }

  .icon-btn.header__menu {
    display: inline-grid;
    margin-left: -8px;
  }

  .header__search {
    max-width: 320px;
  }

  .breadcrumb__root,
  .breadcrumb__sep {
    display: none;
  }

  .dashboard-grid--charts,
  .dashboard-grid--main {
    grid-template-columns: minmax(0, 1fr);
  }

  /* Caisse du jour : « À traiter » passe sous le montant */
  .till {
    grid-template-columns: minmax(0, 1fr);
  }


  /* Le donut passe à l'horizontale quand il occupe toute la largeur */
  .donut {
    flex-direction: row;
    align-items: center;
    gap: var(--space-8);
  }

  .chart-container--donut {
    margin: 0;
  }

  .donut__legend {
    flex: 1;
  }
}

/* ---------- ≤ 768px : mobile ---------- */
@media (max-width: 768px) {
  :root {
    --page-pad: 16px;
    --header-h: 60px;
  }

  .main {
    padding-top: var(--space-6);
  }

  .breadcrumb__root,
  .breadcrumb__sep,
  .profile__info,
  .profile__chevron,
  .header__divider {
    display: none;
  }

  .header {
    gap: var(--space-2);
  }

  .header__right {
    flex: 0 0 auto;
  }

  .profile {
    padding: 2px;
    margin-left: 4px;
  }

  /* Recherche : masquée, puis superposée au header quand on l'ouvre */
  .header__search {
    display: none;
  }

  .icon-btn.header__search-toggle {
    display: inline-grid;
  }

  .search-open .header__search {
    display: block;
    position: absolute;
    inset: 0;
    max-width: none;
    padding: 11px var(--page-pad);
    background: var(--surface);
    z-index: 5;
    animation: view-in var(--dur) var(--ease-out);
  }

  .search-open .search-results {
    top: calc(100% - 2px);
    left: var(--page-pad);
    right: var(--page-pad);
  }

  .search-field__kbd {
    display: none;
  }

  /* 16px minimum : évite le zoom automatique d'iOS au focus */
  .search-field input {
    font-size: 16px;
  }

  /* Panneaux déroulants : pleine largeur */
  .dropdown {
    position: static;
  }

  .dropdown-panel--right {
    left: var(--page-pad);
    right: var(--page-pad);
    top: calc(var(--header-h) - 4px);
    width: auto;
  }

  .page-head {
    flex-direction: column;
    align-items: stretch;
  }

  .page-head__title {
    font-size: var(--fs-xl);
  }

  .page-head__actions .btn {
    flex: 1;
  }

  .stats-grid,
  .dashboard-grid,
  .till {
    gap: var(--space-3);
    margin-bottom: var(--space-3);
  }

  .stats-grid {
    gap: 0; /* un seul bandeau, séparé par des filets */
  }


  .till__main,
  .till__todo {
    padding-left: 20px;
    padding-right: 20px;
  }

  .till__main {
    padding-top: 22px;
  }

  .till__amount {
    font-size: 3.75rem;
  }

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

  .dashboard-grid--half {
    grid-template-columns: minmax(0, 1fr);
  }

  .card {
    padding: var(--space-4);
  }

  .card__head--padded {
    padding: var(--space-4) var(--space-4) 0;
  }

  .chart-card .card__head {
    flex-direction: column;
  }

  .chart-container--lg {
    height: 240px;
    min-height: 240px;
  }

  .donut {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-5);
  }

  .chart-container--donut {
    margin: 0 auto;
  }

  /* Tableau → liste de cartes compactes */

  .toast-region {
    left: 16px;
    right: 16px;
    bottom: 16px;
  }

  .toast {
    width: 100%;
  }
}

/* ---------- ≤ 480px ---------- */
@media (max-width: 480px) {
  .stats-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .stat-card + .stat-card {
    border-left: none;
    border-top: 1px solid var(--border);
  }

  .segmented {
    width: 100%;
  }

  .segmented__item {
    flex: 1;
  }

  .chart-summary {
    gap: var(--space-2) var(--space-5);
  }
}

/* Écrans tactiles : cibles d'au moins 44 × 44 px (WCAG 2.5.5) */
@media (pointer: coarse) {
  .icon-btn {
    width: 44px;
    height: 44px;
  }

  .header__right {
    gap: 0;
  }

  .btn {
    height: 44px;
  }

  .segmented__item {
    height: 38px;
  }

  .nav__link {
    height: 44px;
  }

  /* Zone de clic agrandie sans changer le visuel */
  .link-btn,
  .toast__close {
    position: relative;
  }

  .link-btn::after,
  .toast__close::after {
    content: '';
    position: absolute;
    inset: -12px;
  }
}

/* ==========================================================================
   Pages (phase 2)
   ========================================================================== */
@media (max-width: 1024px) {
  .settings {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-4);
  }

  /* Le sommaire des paramètres devient une rangée défilante */
  .settings__nav {
    position: static;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .settings__nav ul {
    flex-direction: row;
  }

  .settings__link {
    white-space: nowrap;
  }
}

@media (max-width: 768px) {
  /* Bandeau de chiffres : 2 colonnes */
  .stat-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stat-strip__item {
    padding: 14px 16px;
  }

  .stat-strip__item:nth-child(odd) {
    border-left: none;
  }

  .stat-strip__item:nth-child(n + 3) {
    border-top: 1px solid var(--border);
  }

  .stat-strip__value {
    font-size: var(--fs-lg);
  }

  .toolbar {
    padding: var(--space-3) var(--space-4);
  }

  .toolbar__search {
    flex-basis: 100%;
    max-width: none;
  }

  .toolbar__end {
    width: 100%;
    margin-left: 0;
    justify-content: space-between;
  }

  .form-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  /* Modale : pleine largeur, ancrée en bas de l'écran */
  .modal-backdrop {
    place-items: end stretch;
    padding: 0;
  }

  .modal {
    max-height: 92dvh;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    animation-name: sheet-in;
  }

  .modal__head,
  .modal__body,
  .modal__foot {
    padding-left: 16px;
    padding-right: 16px;
  }

  .modal__foot {
    border-radius: 0;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  .modal__foot .btn {
    flex: 1;
  }

  @keyframes sheet-in {
    from { transform: translateY(24px); opacity: 0; }
    to { transform: none; opacity: 1; }
  }

  /* Lignes d'articles : l'article sur sa propre ligne */
  .line-items__head {
    display: none;
  }

  .line-items__row {
    grid-template-columns: 88px minmax(0, 1fr) 32px;
    padding-top: 10px;
    border-bottom: 1px solid var(--border);
  }

  .line-items__row > .field:first-child {
    grid-column: 1 / -1;
  }

  .line-items__add {
    margin-top: 10px;
  }

  .detail-list li {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .detail-list li .badge {
    grid-row: 2;
  }

  .invoice-doc {
    padding: 18px;
  }

  .invoice-doc__head {
    flex-direction: column;
  }

  .invoice-doc__meta {
    text-align: left;
  }

  .choice-group {
    grid-template-columns: minmax(0, 1fr);
  }

  .settings-danger {
    flex-direction: column;
    align-items: flex-start;
  }

  /* 16px minimum : pas de zoom automatique sur iOS */
  .input {
    font-size: 16px;
  }
}

/* Tableaux de données → fiches empilées sur tablette et mobile (chaque
   cellule affiche son libellé grâce à data-label) */
@media (max-width: 1024px) {
  .data-table thead {
    display: none;
  }

  .data-table,
  .data-table tbody,
  .data-table tr,
  .data-table td {
    display: block;
  }

  .data-table tr {
    padding: 12px var(--space-4);
    border-bottom: 1px solid var(--border);
  }

  .data-table tbody tr:last-child {
    border-bottom: none;
  }

  .data-table td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    padding: 4px 0;
    border: none;
    white-space: normal;
    text-align: right;
  }

  .data-table td::before {
    content: attr(data-label);
    flex-shrink: 0;
    font-size: var(--fs-sm);
    color: var(--text-3);
    text-align: left;
  }

  /* Première cellule : titre de la fiche, sans libellé */
  .data-table td:first-child {
    padding-bottom: 8px;
    text-align: left;
  }

  .data-table td:first-child::before,
  .data-table td:not([data-label])::before {
    content: none;
  }

  .data-table td:not([data-label]) {
    justify-content: flex-end;
  }

  .data-table .cell-stock {
    min-width: 0;
  }
}

@media (max-width: 768px) {
  .data-table .td-value {
    min-width: 0;
  }
}

/* Dépenses : la répartition passe sous la liste quand la place manque */
@media (max-width: 1280px) {
  .page-grid {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* Ventes : la colonne « Articles » s'efface quand la largeur est juste */
@media (min-width: 1025px) and (max-width: 1200px) {
  .data-table .col-items {
    display: none;
  }
}

/* Fenêtres modales : jamais plus larges que l'écran */
.modal,
.modal__form,
.modal__body {
  min-width: 0;
}

.lines-table td:first-child {
  white-space: normal;
}

@media (max-width: 768px) {
  .lines-table .col-unit {
    display: none;
  }

  .modal__foot {
    flex-wrap: wrap;
  }

  /* L'action destructrice passe sur sa propre ligne, à l'écart */
  .modal__foot .btn--ghost-danger {
    order: 2;
    flex-basis: 100%;
    margin-right: 0;
  }
}

/* Pied de fenêtre : les boutons passent à la ligne s'il en a beaucoup */
.modal__foot {
  flex-wrap: wrap;
}

/* En-tête de page : les boutons passent à la ligne quand ils sont nombreux */
@media (max-width: 768px) {
  .page-head__actions {
    flex-wrap: wrap;
  }

  .page-head__actions .btn {
    flex: 1 1 auto;
  }
}
