/* ============================================
   🎨 LOADING STATES - HTMX Spinners & Indicators
   ============================================ */

/* ========== SPINNER ANIMAÇÃO ========== */
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

/* ========== SPINNER BÁSICO ========== */
.spinner {
  display: inline-block;
  width: 1rem;
  height: 1rem;
  border: 2px solid rgba(0, 0, 0, 0.1);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

.spinner-lg {
  width: 2rem;
  height: 2rem;
  border-width: 3px;
}

/* ========== HTMX LOADING INDICATORS ========== */

/* Indicador inline (aparece ao lado do botão/texto) */
.htmx-indicator {
  display: none;
  opacity: 0;
  transition: opacity 200ms ease-in;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
  display: inline-block;
  opacity: 1;
}

/* Desabilitar elemento durante request */
.htmx-request.htmx-disabled {
  pointer-events: none;
  opacity: 0.6;
  cursor: not-allowed;
}

/* ========== LOADING OVERLAY ========== */
.loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(2px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.htmx-request .loading-overlay {
  display: flex;
}

/* ========== SKELETON LOADING ========== */
.skeleton {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s ease-in-out infinite;
  border-radius: 4px;
}

@keyframes skeleton-loading {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

.skeleton-text {
  height: 1em;
  margin-bottom: 0.5em;
}

.skeleton-title {
  height: 1.5em;
  width: 60%;
  margin-bottom: 1em;
}

/* ========== BUTTON LOADING STATES ========== */
button.loading,
.btn.loading {
  position: relative;
  color: transparent !important;
  pointer-events: none;
}

button.loading::after,
.btn.loading::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 1rem;
  height: 1rem;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

/* Botão com spinner inline */
.btn-with-spinner {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-with-spinner .spinner {
  display: none;
}

.btn-with-spinner.loading .spinner {
  display: inline-block;
}

.btn-with-spinner.loading .btn-text {
  opacity: 0.7;
}

/* ========== FORM LOADING STATES ========== */
form.htmx-request fieldset {
  opacity: 0.6;
  pointer-events: none;
}

form.htmx-request input,
form.htmx-request select,
form.htmx-request textarea {
  background-color: #f5f5f5;
}

/* ========== TABLE/LIST LOADING ========== */
.table-loading {
  position: relative;
  min-height: 200px;
}

.table-loading .loading-overlay {
  position: absolute;
}

/* Pulsar linhas durante loading */
.htmx-request tbody tr {
  animation: pulse 1.5s ease-in-out infinite;
}

/* ========== PROGRESS BAR ========== */
.progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #4f46e5, #06b6d4);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 300ms ease-out;
  z-index: 9999;
}

.htmx-request .progress-bar {
  animation: progress 2s ease-in-out infinite;
}

@keyframes progress {
  0% {
    transform: scaleX(0);
  }
  50% {
    transform: scaleX(0.7);
  }
  100% {
    transform: scaleX(1);
  }
}

/* ========== TOAST LOADING ========== */
.toast.loading {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.toast.loading .spinner {
  flex-shrink: 0;
}

/* ========== SPECIFIC COMPONENTS ========== */

/* Modal/Dialog loading */
dialog.htmx-request::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 2rem;
  height: 2rem;
  border: 3px solid rgba(0, 0, 0, 0.1);
  border-top-color: #4f46e5;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

/* Card loading */
.card.loading {
  position: relative;
  pointer-events: none;
}

.card.loading::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(1px);
  z-index: 1;
}

.card.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 2rem;
  height: 2rem;
  border: 3px solid rgba(0, 0, 0, 0.1);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  z-index: 2;
}

/* ========== UTILITY CLASSES ========== */
.loading-text {
  animation: pulse 1.5s ease-in-out infinite;
}

.blur-loading {
  filter: blur(2px);
  pointer-events: none;
  transition: filter 200ms;
}

.htmx-request .blur-loading {
  filter: blur(0);
}

/* Hide during loading */
.hide-while-loading {
  transition: opacity 200ms;
}

.htmx-request .hide-while-loading {
  opacity: 0;
}

/* Show during loading */
.show-while-loading {
  display: none;
}

.htmx-request .show-while-loading {
  display: block;
}

/* ========== RESPONSIVE ========== */
@media (max-width: 640px) {
  .spinner-lg {
    width: 1.5rem;
    height: 1.5rem;
  }

  .loading-overlay {
    backdrop-filter: blur(1px);
  }
}

/* ========== ACCESSIBILITY ========== */
@media (prefers-reduced-motion: reduce) {
  .spinner,
  .progress-bar,
  button.loading::after,
  .skeleton {
    animation: none;
  }

  .htmx-request tbody tr {
    animation: none;
  }
}

/* ========== DARK MODE SUPPORT ========== */
@media (prefers-color-scheme: dark) {
  .loading-overlay {
    background: rgba(0, 0, 0, 0.6);
  }

  .skeleton {
    background: linear-gradient(90deg, #2a2a2a 25%, #1a1a1a 50%, #2a2a2a 75%);
  }

  form.htmx-request input,
  form.htmx-request select,
  form.htmx-request textarea {
    background-color: #1a1a1a;
  }
}
