/* =====================================================================
   Medicore Loader v1 — unified loading design language
   ---------------------------------------------------------------------
   Single source of truth for every loader across the product.
   Modes: overlay · bar · inline · button
   Tokens: consumes existing --color-primary, --color-secondary,
           --bg-card-solid, --border-color, --text-primary, --text-muted,
           --bg-overlay from theme_tokens.html. Falls back if absent.
   Dark mode: automatic via the same tokens + explicit html.dark rules.
   RTL:    safe (no inline-specific margins, uses logical properties
           where it matters).
   A11y:   respects prefers-reduced-motion; carries role/aria-busy.
   ===================================================================== */

/* --- Spinner primitive ----------------------------------------------- */
.ml-spinner {
  display: inline-block;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  border: 3px solid color-mix(in srgb, var(--color-primary, #0ea5e9) 22%, transparent);
  border-top-color: var(--color-primary, #0ea5e9);
  animation: ml-spin 0.78s linear infinite;
  flex: none;
  vertical-align: middle;
  box-sizing: border-box;
}
.ml-spinner--sm { width: 1rem;    height: 1rem;    border-width: 2px; }
.ml-spinner--md { width: 1.5rem;  height: 1.5rem;  border-width: 2.5px; }
.ml-spinner--lg { width: 3.25rem; height: 3.25rem; border-width: 3.5px; }

@keyframes ml-spin { to { transform: rotate(360deg); } }

/* --- Overlay mode (full-screen modal) ------------------------------- */
.ml-overlay {
  position: fixed;
  inset: 0;
  z-index: 9998;              /* below toasts (10000), above modals (~100) */
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(15, 23, 42, 0.42);
  -webkit-backdrop-filter: blur(4px);
          backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity 0.2s ease-out;
  font-family: inherit;
  pointer-events: auto;
}
html.dark .ml-overlay { background: rgba(2, 6, 23, 0.62); }
.ml-overlay[data-visible="true"] { display: flex; opacity: 1; }

.ml-overlay__card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.95rem;
  padding: 1.75rem 2rem;
  min-width: 14rem;
  max-width: min(22rem, 88vw);
  border-radius: 1rem;
  background: var(--bg-card-solid, #ffffff);
  border: 1px solid var(--border-color, rgba(148, 163, 184, 0.28));
  box-shadow:
    0 30px 80px -20px rgba(15, 23, 42, 0.35),
    0 10px 30px -12px rgba(15, 23, 42, 0.18);
  transform: translateY(8px) scale(0.98);
  transition: transform 0.22s ease-out;
  text-align: center;
  color: var(--text-primary, #0f172a);
}
html.dark .ml-overlay__card {
  box-shadow:
    0 30px 80px -20px rgba(2, 6, 23, 0.72),
    0 10px 30px -12px rgba(2, 6, 23, 0.5);
}
.ml-overlay[data-visible="true"] .ml-overlay__card { transform: translateY(0) scale(1); }

.ml-overlay__message {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  line-height: 1.5;
  color: var(--text-primary, #0f172a);
}
.ml-overlay__sublabel {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-muted, #64748b);
}

/* --- Bar mode (top progress bar) ------------------------------------ */
.ml-bar {
  position: fixed;
  top: 0;
  inset-inline: 0;
  height: 3px;
  z-index: 9999;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease-out;
  background: transparent;
  overflow: hidden;
}
.ml-bar[data-visible="true"] { opacity: 1; }
.ml-bar__fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(
    90deg,
    var(--color-primary, #0ea5e9) 0%,
    var(--color-secondary, #14b8a6) 100%
  );
  box-shadow: 0 0 10px color-mix(in srgb, var(--color-primary, #0ea5e9) 55%, transparent);
  transition: width 0.3s ease-out;
  will-change: width, transform;
}
.ml-bar__fill--indeterminate {
  width: 42%;
  animation: ml-bar-slide 1.15s cubic-bezier(0.65, 0.05, 0.36, 1) infinite;
}
@keyframes ml-bar-slide {
  0%   { transform: translateX(-110%); }
  100% { transform: translateX(260%); }
}
/* RTL: translate in the opposite direction */
[dir="rtl"] .ml-bar__fill--indeterminate {
  animation-name: ml-bar-slide-rtl;
}
@keyframes ml-bar-slide-rtl {
  0%   { transform: translateX(110%); }
  100% { transform: translateX(-260%); }
}

/* --- Inline / scoped mode ------------------------------------------- */
.ml-inline {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  padding: 1rem;
  background: color-mix(in srgb, var(--bg-card-solid, #ffffff) 72%, transparent);
  -webkit-backdrop-filter: blur(2px);
          backdrop-filter: blur(2px);
  border-radius: inherit;
  opacity: 0;
  transition: opacity 0.18s ease-out;
  pointer-events: auto;
  color: var(--text-secondary, #334155);
}
html.dark .ml-inline {
  background: color-mix(in srgb, var(--bg-card-solid, #0f172a) 78%, transparent);
}
.ml-inline[data-visible="true"] { opacity: 1; }
.ml-inline__message {
  font-size: 0.85rem;
  font-weight: 600;
  color: inherit;
}

/* --- Button mode (JS injects markup; CSS just styles the marker) --- */
[data-ml-busy="1"] {
  position: relative;
  cursor: wait !important;
  user-select: none;
}
.ml-btn-spinner-wrap {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  pointer-events: none;
  white-space: nowrap;
}
.ml-btn-spinner {
  width: 1em;
  height: 1em;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-top-color: transparent;
  animation: ml-spin 0.7s linear infinite;
  opacity: 0.85;
  flex: none;
  box-sizing: border-box;
}

/* --- Screen-reader only --------------------------------------------- */
.ml-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* --- Reduced motion -------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .ml-spinner,
  .ml-btn-spinner {
    animation-duration: 2.4s;
  }
  .ml-bar__fill--indeterminate {
    animation: none;
    width: 55%;
    transform: translateX(0);
  }
  .ml-overlay,
  .ml-overlay__card,
  .ml-inline {
    transition: none;
  }
}

/* --- Mobile adjustments --------------------------------------------- */
@media (max-width: 640px) {
  .ml-overlay__card {
    min-width: min(80vw, 14rem);
    padding: 1.5rem 1.5rem;
  }
  .ml-spinner--lg { width: 2.75rem; height: 2.75rem; }
  .ml-bar { height: 2px; }
}

/* --- Print: never show a loader on paper ---------------------------- */
@media print {
  .ml-overlay,
  .ml-bar,
  .ml-inline { display: none !important; }
}
