/*
 * Global loading states (LADE-AUDIT Teil A, Quick Win 3).
 *
 * - Turbo progress bar in the brand colour. `--turbo-progress-bar-color` is
 *   copied from the agency's `--agency-primary` onto <html> in application.js,
 *   because the bar sits outside <body>.
 * - `turbo-frame[busy]`: Turbo sets `busy` while a frame loads. The content
 *   stays in place and fades, so the user sees that a refresh is running.
 *   Lazy frames on their first load keep their placeholder untouched.
 * - `form[aria-busy]`: set by Turbo during a submission and by the
 *   `submit-disable` controller for `data-turbo=false` forms. Every submit
 *   control is locked, the pressed (disabled) button shows a spinner.
 */

.turbo-progress-bar {
  height: 3px;
  background: var(--turbo-progress-bar-color, #003A3A);
}

turbo-frame[busy]:not([loading="lazy"]:not([complete])) {
  opacity: 0.55;
  transition: opacity 150ms ease-in-out;
  pointer-events: none;
}

form[aria-busy] [type="submit"] {
  cursor: progress;
  pointer-events: none;
  opacity: 0.7;
}

form[aria-busy] button[type="submit"]:disabled::before {
  content: "";
  display: inline-block;
  flex-shrink: 0;
  width: 0.9em;
  height: 0.9em;
  margin-right: 0.45em;
  vertical-align: -0.125em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 9999px;
  animation: sh-busy-spin 0.7s linear infinite;
}

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

@media (prefers-reduced-motion: reduce) {
  form[aria-busy] button[type="submit"]:disabled::before {
    animation-duration: 2s;
  }
}

/*
 * Busy buttons and links (busy.js / busy_controller.js, W03): spinner in
 * front of the label, width kept via inline min-width, colours unchanged.
 * Class names follow the V4 system so the reskin can take them over.
 */
a[aria-busy="true"],
button[aria-busy="true"],
input[aria-busy="true"] {
  cursor: progress;
  pointer-events: none;
}

.is-busy {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45em;
}

.busy-spinner {
  display: inline-block;
  flex-shrink: 0;
  width: 0.9em;
  height: 0.9em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 9999px;
  opacity: 0.85;
  animation: sh-busy-spin 0.7s linear infinite;
}

/* Spinner + text for running background work (role="status"). */
.busy-status {
  display: inline-flex;
  align-items: center;
  gap: 0.5em;
}

/*
 * Skeleton placeholders (shared/_skeleton.html.erb) for the first build of a
 * lazy Turbo-Frame: rows (lists), cards (grids), lines (text), stats (tiles).
 */
.skeleton {
  display: block;
  border-radius: 8px;
  background: linear-gradient(90deg, #f3f4f6 0%, #e5e7eb 50%, #f3f4f6 100%);
  background-size: 200% 100%;
  animation: sh-skeleton-shimmer 1.4s ease infinite;
}

.dark .skeleton {
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.05) 0%, rgba(255, 255, 255, 0.1) 50%, rgba(255, 255, 255, 0.05) 100%);
  background-size: 200% 100%;
}

.skel { display: flex; flex-direction: column; }
.skel-row {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) 96px;
  gap: 14px;
  align-items: center;
  padding: 16px 20px;
  border-top: 1px solid #f3f4f6;
}
.dark .skel-row { border-top-color: rgba(255, 255, 255, 0.06); }
.skel-row:first-child { border-top: 0; }
.skel-row > div { display: flex; flex-direction: column; gap: 8px; }
.skel-dot { width: 36px; height: 36px; border-radius: 10px; }
.skel-l { height: 12px; width: 100%; }
.skel-w40 { width: 40%; }
.skel-w60 { width: 60%; }
.skel-w80 { width: 80%; }
.skel-btn { height: 32px; border-radius: 999px; }
.skel--cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 280px), 1fr));
  gap: 20px;
}
.skel-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 20px;
  border-radius: 16px;
  border: 1px solid #e5e7eb;
  background: #fff;
}
.dark .skel-card { border-color: rgba(255, 255, 255, 0.08); background: rgba(255, 255, 255, 0.02); }
.skel-card .skel-btn { margin-top: 8px; width: 50%; }
.skel--lines { gap: 10px; }
.skel--lines .skel-l:last-child { width: 60%; }
.skel-stat-value { height: 28px; width: 48px; margin-top: 6px; }

/* A frame that still shows its skeleton is loading for the first time: no fade. */
turbo-frame[busy]:has(> .skel) {
  opacity: 1 !important;
}

/* Background polling (frame_reload_controller) refreshes silently, no fade. */
turbo-frame[busy]:has([data-controller~="frame-reload"]) {
  opacity: 1 !important;
  pointer-events: auto !important;
}

/* Reports page: the stat tiles outside the list frame fade with it on a filter change. */
body:has(#proposals_list[busy]) #proposals_stats:not([aria-busy]) {
  opacity: 0.55;
  transition: opacity 150ms ease-in-out;
}

@keyframes sh-skeleton-shimmer {
  0% { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .busy-spinner { animation-duration: 2s; }
  .skeleton { animation: none; }
}
