/* =========================
   Fenêtres modales style macOS
   (desktop uniquement — drag, resize, traffic lights)
   ========================= */

@media (min-width: 769px) {
  /* Overlay visible uniquement — ne jamais forcer les overlays fermés */
  .modal.active,
  .modal-overlay.active,
  .modal-actualite.active,
  .modal-confirm.active,
  .modal-sondage.active,
  .modal-invite-desktop.active,
  .modal-calendrier.active,
  .modal-confirm-sondages[style*="display: flex"],
  .modal-confirm-sondages[style*="display:flex"],
  .modal-confirm-annonces[style*="display: flex"],
  .modal-confirm-annonces[style*="display:flex"],
  .modal[style*="display: flex"],
  .modal[style*="display:flex"],
  .sql-modal-overlay[style*="display: flex"],
  .sql-modal-overlay[style*="display:flex"],
  #doc-ocr-text-modal.open,
  #doc-qa-history-modal.open,
  #add-document-modal.active {
    background: rgba(15, 23, 42, 0.45) !important;
    backdrop-filter: blur(8px) saturate(1.1);
    -webkit-backdrop-filter: blur(8px) saturate(1.1);
    align-items: center !important;
    justify-content: center !important;
    padding: 24px !important;
  }

  .mac-window {
    position: fixed !important;
    display: flex !important;
    flex-direction: column !important;
    border-radius: 14px !important;
    /* visible : sinon les poignées de resize sont cliquées/coupées */
    overflow: visible !important;
    box-shadow:
      0 0 0 1px rgba(255, 255, 255, 0.35) inset,
      0 28px 80px rgba(0, 0, 0, 0.35),
      0 8px 24px rgba(0, 0, 0, 0.18) !important;
    background: linear-gradient(180deg, #fbfbfd 0%, #f3f4f8 100%) !important;
    max-width: none;
    animation: macWindowIn 0.22s ease-out;
    touch-action: none;
    margin: 0 !important;
    z-index: 100001 !important;
    box-sizing: border-box !important;
  }

  /* Dialogs courts : collés au contenu (pas de grand vide sous les boutons) */
  .mac-window.modal-container-small,
  .mac-window.modal-confirm-content {
    height: auto !important;
    min-height: 0 !important;
    max-height: calc(100vh - 48px) !important;
  }
  .mac-window.modal-container-small > .modal-dialog-body,
  .mac-window.modal-confirm-content > .modal-confirm-body {
    flex: 0 0 auto !important;
    overflow: visible !important;
  }

  /* Contenu arrondi / scroll sans couper les poignées */
  .mac-window > .mac-window-chrome {
    border-radius: 14px 14px 0 0;
    overflow: hidden;
  }
  .mac-window > *:not(.mac-window-chrome):not(.mac-window-resize):not(.mac-window-grip) {
    border-radius: 0 0 14px 14px;
  }

  .mac-window.mac-window-maximized {
    border-radius: 10px !important;
    top: 16px !important;
    left: 16px !important;
    right: 16px !important;
    bottom: 16px !important;
    width: auto !important;
    height: auto !important;
  }

  .mac-window.mac-window-minimized {
    height: 44px !important;
    min-height: 44px !important;
    max-height: 44px !important;
    overflow: hidden !important;
  }

  .mac-window.mac-window-minimized > *:not(.mac-window-chrome):not(.mac-window-resize) {
    display: none !important;
  }

  @keyframes macWindowIn {
    from { opacity: 0; transform: translateY(12px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
  }

  .mac-window-chrome {
    flex: 0 0 auto;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 0 14px 0 16px;
    background: linear-gradient(180deg, #eceef3 0%, #dde1ea 100%);
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    cursor: grab;
    user-select: none;
    z-index: 5;
  }

  .mac-window-chrome:active { cursor: grabbing; }

  .mac-window-chrome::before {
    content: '';
    width: 62px;
    flex-shrink: 0;
  }

  .mac-window-title {
    flex: 1;
    min-width: 0;
    font-size: 13px;
    font-weight: 650;
    color: #2c3340;
    letter-spacing: 0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    text-align: center;
  }

  .mac-window-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
  }

  .mac-btn {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.12);
    padding: 0;
    cursor: pointer;
    position: relative;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.35);
    transition: filter 0.12s ease, transform 0.12s ease;
  }

  .mac-btn:hover { filter: brightness(1.05); }
  .mac-btn:active { transform: scale(0.92); }

  .mac-btn-close { background: radial-gradient(circle at 30% 30%, #ff8e8a, #ff5f57); }
  .mac-btn-min { background: radial-gradient(circle at 30% 30%, #ffd56a, #febc2e); }
  .mac-btn-max { background: radial-gradient(circle at 30% 30%, #61d870, #28c840); }

  .mac-window-controls:hover .mac-btn::after {
    content: '';
    position: absolute;
    inset: 0;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 8px 8px;
    opacity: 0.85;
  }

  .mac-window-controls:hover .mac-btn-close::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpath stroke='%234a0002' stroke-width='1.6' stroke-linecap='round' d='M2.5 2.5l5 5M7.5 2.5l-5 5'/%3E%3C/svg%3E");
  }

  .mac-window-controls:hover .mac-btn-min::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpath stroke='%235c4000' stroke-width='1.8' stroke-linecap='round' d='M2.2 5h5.6'/%3E%3C/svg%3E");
  }

  .mac-window-controls:hover .mac-btn-max::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpath fill='none' stroke='%23004614' stroke-width='1.4' d='M2.2 5.8V2.2h3.6M7.8 4.2v3.6H4.2'/%3E%3C/svg%3E");
  }

  .mac-window .mac-window-hide-close,
  .mac-window > .modal-close,
  .mac-window .modal-actualite-close,
  .mac-window .modal-discussion-close,
  .mac-window .modal-sondage-close,
  .mac-window .modal-invite-desktop-close,
  .mac-window .modal-calendrier-close,
  .mac-window .sql-modal-close,
  .mac-window .doc-ocr-text-close {
    display: none !important;
  }

  /* Titre déjà dans la chrome → masquer les anciens bandeaux */
  .mac-window > .modal-header,
  .mac-window > .modal-actualite-header,
  .mac-window > .modal-discussion-header,
  .mac-window > .modal-confirm-header,
  .mac-window > .modal-sondage-header,
  .mac-window > .modal-invite-desktop-header,
  .mac-window > .modal-calendrier-header,
  .mac-window > .sql-modal-header,
  .mac-window > .doc-ocr-text-header,
  .mac-window > .doc-qa-history-header,
  .mac-window > h2:first-of-type {
    display: none !important;
  }

  .mac-window > .modal-body,
  .mac-window > .modal-actualite-body,
  .mac-window > .modal-discussion-body,
  .mac-window > .modal-confirm-body,
  .mac-window > .modal-messages,
  .mac-window > .modal-dialog-body,
  .mac-window > form,
  .mac-window > .sql-renew-body,
  .mac-window > .sql-renew-success-body,
  .mac-window > .modal-import-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
  }

  .mac-window-resize {
    position: absolute;
    z-index: 20;
    background: transparent;
    touch-action: none;
  }
  /* Poignées entièrement à l’intérieur (fiables même si un parent clip) */
  .mac-window-resize.n { top: 0; left: 16px; right: 16px; height: 8px; cursor: n-resize; }
  .mac-window-resize.s { bottom: 0; left: 16px; right: 16px; height: 10px; cursor: s-resize; }
  .mac-window-resize.e { top: 16px; right: 0; bottom: 16px; width: 10px; cursor: e-resize; }
  .mac-window-resize.w { top: 16px; left: 0; bottom: 16px; width: 10px; cursor: w-resize; }
  .mac-window-resize.ne { top: 0; right: 0; width: 18px; height: 18px; cursor: ne-resize; }
  .mac-window-resize.nw { top: 0; left: 0; width: 18px; height: 18px; cursor: nw-resize; }
  .mac-window-resize.se { bottom: 0; right: 0; width: 28px; height: 28px; cursor: se-resize; }
  .mac-window-resize.sw { bottom: 0; left: 0; width: 18px; height: 18px; cursor: sw-resize; }

  /* Indicateur visuel coin bas-droit (au-dessus de la zone .se) */
  .mac-window-grip {
    position: absolute;
    right: 6px;
    bottom: 6px;
    width: 14px;
    height: 14px;
    z-index: 21;
    cursor: se-resize;
    touch-action: none;
    background:
      linear-gradient(135deg, transparent 0 45%, #9aa3b2 45% 55%, transparent 55% 70%, #9aa3b2 70% 80%, transparent 80%);
    opacity: 0.8;
    pointer-events: auto;
  }
  .mac-window-grip:hover { opacity: 1; }

  .mac-window.mac-window-maximized .mac-window-resize,
  .mac-window.mac-window-minimized .mac-window-resize,
  .mac-window.mac-window-maximized .mac-window-grip,
  .mac-window.mac-window-minimized .mac-window-grip {
    display: none !important;
  }

  body.mac-window-dragging,
  body.mac-window-dragging * {
    user-select: none !important;
  }
  body.mac-window-resizing {
    cursor: se-resize !important;
  }
}

@media (max-width: 768px) {
  .mac-window-chrome,
  .mac-window-resize {
    display: none !important;
  }
}
