/*
 * Kwalee BI — Loading States, Error Boundaries & Data Freshness
 *
 * Skeleton animations, error tile styles, retry button,
 * degradation banner, and freshness indicator.
 *
 * Story 2.7: Loading States, Error Boundaries & Data Freshness
 */

/* ═══════════════════════════════════════════════════════════════════════
   SKELETON ANIMATIONS
   ═══════════════════════════════════════════════════════════════════════ */

@keyframes skeleton-pulse {
  0% {
    opacity: 0.4;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0.4;
  }
}

/* ─── Base skeleton tile ─── */
.tile-skeleton {
  pointer-events: none;
}

/* ═══════════════════════════════════════════════════════════════════════
   PER-TILE REFRESHING STATE (KWBI-167)
   Applies a skeleton shimmer overlay to an individual tile without
   replacing its content.  Used for filter-triggered refreshes so that
   only in-flight tiles animate — static tiles remain fully visible.
   ═══════════════════════════════════════════════════════════════════════ */

/* Marker class applied to skeleton tiles that are actively refreshing.
   Combined with .tile-skeleton the existing skeleton-bar animations
   provide the visual feedback.  When applied to a real tile (not a
   skeleton) the ::after overlay adds the shimmer. */
.dashboard-tile--refreshing {
  position: relative;
  overflow: hidden;
}

/* Shimmer overlay for non-skeleton tiles (e.g. future per-tile callbacks) */
.dashboard-tile--refreshing:not(.tile-skeleton)::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--color-skeleton-bg);
  animation: skeleton-pulse 1.5s ease-in-out infinite;
  pointer-events: none;
  z-index: 1;
}

@media (prefers-reduced-motion: reduce) {
  .dashboard-tile--refreshing:not(.tile-skeleton)::after {
    animation: none;
    opacity: 0.7;
  }
}

/* KPI section dim during a filter-triggered refresh.
   The KPI tiles are not replaced with skeletons (they stay readable so the
   previous numbers remain glanceable) but a subtle dim + disabled hit-testing
   signals that the values are being recomputed. */
.dashboard-kpi-section--refreshing {
  opacity: 0.55;
  pointer-events: none;
  transition: opacity 120ms ease-in;
}

@media (prefers-reduced-motion: reduce) {
  .dashboard-kpi-section--refreshing {
    transition: none;
  }
}

/* ─── Skeleton bar (generic animated rectangle) ─── */
.skeleton-bar {
  background: var(--color-skeleton-bg);
  border-radius: var(--radius-sm, 4px);
  animation: skeleton-pulse 1.5s ease-in-out infinite;
}

/* ─── KPI skeleton bars ─── */
.skeleton-bar--label {
  width: 45%;
  height: 0.75rem;
  margin-bottom: var(--spacing-sm, 8px);
}

.skeleton-bar--value {
  width: 65%;
  height: 2rem;
  margin-bottom: var(--spacing-sm, 8px);
}

.skeleton-bar--trend {
  width: 35%;
  height: 0.8125rem;
  margin-bottom: var(--spacing-xs, 4px);
}

.skeleton-bar--period {
  width: 50%;
  height: 0.6875rem;
  margin-top: auto;
}

/* ─── Chart skeleton ─── */
.skeleton-bar--chart-title {
  width: 40%;
  height: 1rem;
  margin-bottom: var(--spacing-md, 16px);
}

.skeleton-chart-area {
  display: flex;
  align-items: flex-end;
  gap: var(--spacing-sm, 8px);
  height: 160px;
  padding-top: var(--spacing-md, 16px);
}

.skeleton-chart-bar {
  flex: 1;
  background: var(--color-skeleton-bg);
  border-radius: var(--radius-sm, 4px) var(--radius-sm, 4px) 0 0;
  animation: skeleton-pulse 1.5s ease-in-out infinite;
}

/* Stagger animation for chart bars */
.skeleton-chart-bar:nth-child(2) {
  animation-delay: 0.1s;
}
.skeleton-chart-bar:nth-child(3) {
  animation-delay: 0.2s;
}
.skeleton-chart-bar:nth-child(4) {
  animation-delay: 0.3s;
}
.skeleton-chart-bar:nth-child(5) {
  animation-delay: 0.4s;
}
.skeleton-chart-bar:nth-child(6) {
  animation-delay: 0.5s;
}
.skeleton-chart-bar:nth-child(7) {
  animation-delay: 0.6s;
}

/* ─── Table skeleton ─── */
.tile-skeleton--table {
  min-height: 280px;
}

.skeleton-table {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.skeleton-table-row {
  display: flex;
  gap: var(--spacing-sm, 8px);
  padding: var(--spacing-xs, 4px) 0;
}

.skeleton-table-row--header {
  margin-bottom: var(--spacing-xs, 4px);
}

.skeleton-table-cell {
  flex: 1;
  height: 1rem;
  background: var(--color-skeleton-bg);
  border-radius: var(--radius-sm, 4px);
  animation: skeleton-pulse 1.5s ease-in-out infinite;
}

.skeleton-table-cell--header {
  height: 0.875rem;
  background: var(--color-skeleton-bg-strong);
}

/* ═══════════════════════════════════════════════════════════════════════
   ERROR STATES
   ═══════════════════════════════════════════════════════════════════════ */

.tile-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-sm, 8px);
  text-align: center;
}

.tile-error__icon {
  font-size: 2rem;
  line-height: 1;
  opacity: 0.6;
}

.tile-error__message {
  font-size: 0.8125rem;
  margin: 0;
  max-width: 280px;
}

[data-theme="dark"] .tile-error__message {
  color: var(--color-neutral-400);
}

[data-theme="light"] .tile-error__message {
  color: var(--color-neutral-500);
}

.tile-error__retry-btn {
  font-family: var(--font-body, system-ui);
  font-size: 0.8125rem;
  font-weight: 500;
  padding: 6px 16px;
  border-radius: var(--radius-md, 8px);
  border: 1px solid transparent;
  cursor: pointer;
  transition:
    background 0.15s ease,
    opacity 0.15s ease;
}

[data-theme="dark"] .tile-error__retry-btn {
  background: var(--color-neutral-700);
  color: var(--color-neutral-100);
  border-color: var(--color-neutral-600);
}

[data-theme="light"] .tile-error__retry-btn {
  background: var(--color-neutral-100);
  color: var(--color-neutral-700);
  border-color: var(--color-neutral-300);
}

.tile-error__retry-btn:hover {
  opacity: 0.85;
}

.tile-error__retry-btn:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

/* Spinner inside retry button during re-fetch */
.tile-error__retry-btn--loading {
  pointer-events: none;
  opacity: 0.6;
}

.tile-error__retry-btn--loading::before {
  content: "";
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  margin-right: 6px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: retry-spin 0.6s linear infinite;
  vertical-align: -0.1em;
}

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

/* ═══════════════════════════════════════════════════════════════════════
   DEGRADATION BANNER (page-level outage)
   ═══════════════════════════════════════════════════════════════════════ */

.degradation-banner {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm, 8px);
  padding: var(--spacing-md, 16px) var(--spacing-lg, 24px);
  border-radius: var(--radius-md, 8px);
  margin-bottom: var(--spacing-lg, 24px);
  border: 1px solid var(--color-error);
}

[data-theme="dark"] .degradation-banner {
  background: var(--color-error-bg);
  color: var(--color-neutral-200);
}

[data-theme="light"] .degradation-banner {
  background: var(--color-error-bg);
  color: var(--color-neutral-800);
}

.degradation-banner__icon {
  font-size: 1.25rem;
  flex-shrink: 0;
}

.degradation-banner__text {
  font-size: 0.875rem;
  font-weight: 500;
}

/* ═══════════════════════════════════════════════════════════════════════
   DATA FRESHNESS INDICATOR
   ═══════════════════════════════════════════════════════════════════════ */

/* ═══════════════════════════════════════════════════════════════════════
   FRESHNESS INDICATOR (Story 2.7 + Story 7.3 dual encoding)
   ═══════════════════════════════════════════════════════════════════════ */

.freshness-indicator {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs, 4px);
  font-size: 0.75rem;
  padding: var(--spacing-xs, 4px) 0;
}

.freshness-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.freshness-dot--green {
  background: var(--color-success);
}

.freshness-dot--yellow {
  background: var(--color-warning);
}

.freshness-dot--red {
  background: var(--color-error);
}

.freshness-dot--unknown {
  background: var(--color-warning);
}

/* ─── Status icon (dual encoding — supplements colour dot) ─── */
.freshness-status-icon {
  font-size: 0.75rem;
  font-weight: 700;
  flex-shrink: 0;
}

[data-theme="dark"] .freshness-status-icon--green {
  color: var(--color-success-text);
}
[data-theme="dark"] .freshness-status-icon--yellow {
  color: var(--color-warning-text);
}
[data-theme="dark"] .freshness-status-icon--red {
  color: var(--color-error-text);
}
[data-theme="dark"] .freshness-status-icon--unknown {
  color: var(--color-warning-text);
}

[data-theme="light"] .freshness-status-icon--green {
  color: var(--color-success-text);
}
[data-theme="light"] .freshness-status-icon--yellow {
  color: var(--color-warning-text);
}
[data-theme="light"] .freshness-status-icon--red {
  color: var(--color-error-text);
}
[data-theme="light"] .freshness-status-icon--unknown {
  color: var(--color-warning-text);
}

/* ─── Status text label (dual encoding — supplements colour dot) ─── */
.freshness-status-label {
  font-weight: 600;
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

[data-theme="dark"] .freshness-status-label--green {
  color: var(--color-success-text);
}
[data-theme="dark"] .freshness-status-label--yellow {
  color: var(--color-warning-text);
}
[data-theme="dark"] .freshness-status-label--red {
  color: var(--color-error-text);
}
[data-theme="dark"] .freshness-status-label--unknown {
  color: var(--color-warning-text);
}

[data-theme="light"] .freshness-status-label--green {
  color: var(--color-success-text);
}
[data-theme="light"] .freshness-status-label--yellow {
  color: var(--color-warning-text);
}
[data-theme="light"] .freshness-status-label--red {
  color: var(--color-error-text);
}
[data-theme="light"] .freshness-status-label--unknown {
  color: var(--color-warning-text);
}

.freshness-label {
  white-space: nowrap;
}

[data-theme="dark"] .freshness-label {
  color: var(--color-neutral-400);
}

[data-theme="light"] .freshness-label {
  color: var(--color-neutral-500);
}

.freshness-label--unknown {
  font-style: italic;
}

[data-theme="dark"] .freshness-label--unknown {
  color: var(--color-warning-text);
}

[data-theme="light"] .freshness-label--unknown {
  color: var(--color-warning-text);
}

.freshness-age {
  white-space: nowrap;
}

[data-theme="dark"] .freshness-age {
  color: var(--color-neutral-500);
}

[data-theme="light"] .freshness-age {
  color: var(--color-neutral-400);
}

/* ═══════════════════════════════════════════════════════════════════════
   TILE STATE TRANSITIONS
   ═══════════════════════════════════════════════════════════════════════ */

/* Container for tile content that supports state swapping */
.tile-state-container {
  position: relative;
  min-height: inherit;
}

/* Fade transition between loading/data/error states */
.tile-state-enter {
  animation: tile-fade-in 0.25s ease-out;
}

@keyframes tile-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* ═══════════════════════════════════════════════════════════════════════
   REDUCED MOTION — disable skeleton animations
   ═══════════════════════════════════════════════════════════════════════ */

@media (prefers-reduced-motion: reduce) {
  .skeleton-bar,
  .skeleton-chart-bar,
  .skeleton-table-cell {
    animation: none;
    opacity: 0.7;
  }

  .tile-state-enter {
    animation: none;
  }

  .tile-error__retry-btn--loading::before {
    animation: none;
  }

}
