/* ============================================
   Loading States & Skeletons CSS
   US-078: Loading States & Skeletons
   ============================================ */

/* ============================================
   Skeleton Base Styles
   ============================================ */

.skeleton {
    background-color: var(--skeleton-base-color, var(--dp-surface-hover));
    border-radius: 4px;
    display: block;
}

/* Dark mode skeleton colors — subtle dark shapes, not bright white */
[data-bs-theme="dark"] .skeleton,
.dark-mode .skeleton {
    --skeleton-base-color: var(--dp-surface-secondary);
    --skeleton-highlight-color: var(--dp-surface-hover);
}

/* ============================================
   Skeleton Animations
   ============================================ */

/* Shimmer Effect - Gradient sweep animation */
.skeleton-shimmer {
    background: linear-gradient(
        90deg,
        var(--skeleton-base-color, var(--dp-surface-hover)) 0%,
        var(--skeleton-highlight-color, var(--dp-surface-primary)) 50%,
        var(--skeleton-base-color, var(--dp-surface-hover)) 100%
    );
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.5s ease-in-out infinite;
}

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

/* Pulse Effect - Opacity animation */
.skeleton-pulse {
    animation: skeleton-pulse 1.5s ease-in-out infinite;
}

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

/* Wave Effect - Combined shimmer + scale */
.skeleton-wave {
    background: linear-gradient(
        90deg,
        var(--skeleton-base-color, var(--dp-surface-hover)) 0%,
        var(--skeleton-highlight-color, var(--dp-surface-primary)) 50%,
        var(--skeleton-base-color, var(--dp-surface-hover)) 100%
    );
    background-size: 200% 100%;
    animation: skeleton-wave 1.8s ease-in-out infinite;
}

@keyframes skeleton-wave {
    0% {
        background-position: 200% 0;
        transform: scaleX(0.98);
    }
    50% {
        transform: scaleX(1);
    }
    100% {
        background-position: -200% 0;
        transform: scaleX(0.98);
    }
}

/* Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    .skeleton-shimmer,
    .skeleton-pulse,
    .skeleton-wave {
        animation: none;
    }
    
    .skeleton-pulse {
        opacity: 0.7;
    }
}

/* ============================================
   Skeleton Element Types
   ============================================ */

.skeleton-text {
    height: 16px;
    margin-bottom: 8px;
    border-radius: 4px;
}

.skeleton-text:last-child {
    margin-bottom: 0;
}

.skeleton-title {
    height: 24px;
    border-radius: 4px;
}

.skeleton-avatar,
.skeleton-circle {
    border-radius: 50%;
    flex-shrink: 0;
}

.skeleton-image {
    border-radius: 8px;
}

.skeleton-button {
    height: 38px;
    border-radius: 6px;
}

.skeleton-badge {
    height: 22px;
    border-radius: 12px;
    min-width: 50px;
}

/* ============================================
   Skeleton Containers
   ============================================ */

.skeleton-container {
    width: 100%;
}

.skeleton-card {
    overflow: hidden;
}

.skeleton-card-image {
    border-radius: 0;
}

.skeleton-table {
    width: 100%;
}

.skeleton-table th,
.skeleton-table td {
    padding: 0.75rem;
    vertical-align: middle;
}

/* Comfortable density tier for TableSkeleton — the counterpart of `.dp-grid` (compact) that the
   same component emits, reading the same --ds-table-* tokens as DsDataTable so the placeholder and
   the real table share one source of geometry (ADR-046 §5.4). Without it a DsDataTable page fell
   back to the 34px compact skeleton and every row shifted ~10px when the data landed.
   Same tier as `.dp-grid`: ≥768px only, so the existing mobile padding below is untouched.
   Child-combinator specificity (0,1,3) for the same reason data-grid.css uses it — it has to beat
   Bootstrap's `.table > :not(caption) > * > *` and `.skeleton-table td` above. */
@media (min-width: 768px) {
    .skeleton-table--comfortable > thead > tr > th {
        padding: var(--ds-table-head-py-comfortable) var(--ds-table-cell-px-comfortable);
    }

    .skeleton-table--comfortable > tbody > tr > td {
        padding: var(--ds-table-cell-py-comfortable) var(--ds-table-cell-px-comfortable);
        /* height:auto IS load-bearing here (unlike in .ds-table): this table carries `.table`, and
           apple-design-tokens.css pins `.table td { height: var(--table-row-height) }` = 52px. On a
           cell `height` is a minimum, so padding alone cannot bring the row down to the comfortable
           ~41px. */
        height: auto;
    }
}

.skeleton-list-item {
    transition: background-color 0.15s ease;
}

/* ============================================
   Loading Button Styles
   ============================================ */

.btn .spinner-border-sm {
    width: 1rem;
    height: 1rem;
    border-width: 0.15em;
}

.btn-lg .spinner-border-sm {
    width: 1.25rem;
    height: 1.25rem;
}

.btn-sm .spinner-border-sm {
    width: 0.875rem;
    height: 0.875rem;
}

/* ============================================
   Progress Overlay Styles
   ============================================ */

.progress-overlay {
    position: fixed;
    z-index: 1060;
    display: flex;
    align-items: center;
    justify-content: center;
}

.progress-overlay-fullscreen {
    inset: 0;
}

.progress-overlay-container {
    position: absolute;
    inset: 0;
}

.progress-overlay-backdrop {
    position: absolute;
    inset: 0;
    background-color: var(--dp-surface-overlay);
    transition: background-color 0.2s ease;
}

[data-bs-theme="dark"] .progress-overlay-backdrop,
.dark-mode .progress-overlay-backdrop {
    background-color: var(--dp-surface-overlay);
}

.progress-overlay-blur {
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.progress-overlay-content {
    position: relative;
    z-index: 1;
}

.progress-overlay-card {
    background: var(--dp-surface-primary);
    border-radius: 12px;
    padding: 2rem;
    text-align: center;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    max-width: 400px;
    width: 90vw;
}

[data-bs-theme="dark"] .progress-overlay-card,
.dark-mode .progress-overlay-card {
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.progress-overlay-spinner {
    display: flex;
    justify-content: center;
}

.progress-overlay-title {
    font-weight: 600;
    color: var(--bs-body-color);
    margin: 0;
}

.progress-overlay-description {
    font-size: 0.9375rem;
    margin: 0;
}

.progress-overlay-step {
    margin: 0;
}

.progress-overlay-time {
    margin: 0;
}

.progress-overlay-bar .progress {
    border-radius: 4px;
    overflow: hidden;
}

/* Progress dots animation */
.progress-dots {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.progress-dots span {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background-color: var(--bs-primary);
    animation: progress-dots-bounce 1.4s ease-in-out infinite both;
}

.progress-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.progress-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes progress-dots-bounce {
    0%, 80%, 100% {
        transform: scale(0.6);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Progress bars animation */
.progress-bars {
    display: flex;
    gap: 4px;
    justify-content: center;
    align-items: flex-end;
    height: 40px;
}

.progress-bars span {
    width: 6px;
    background-color: var(--bs-primary);
    border-radius: 3px;
    animation: progress-bars-stretch 1.2s ease-in-out infinite;
}

.progress-bars span:nth-child(1) { animation-delay: 0s; }
.progress-bars span:nth-child(2) { animation-delay: 0.1s; }
.progress-bars span:nth-child(3) { animation-delay: 0.2s; }
.progress-bars span:nth-child(4) { animation-delay: 0.3s; }
.progress-bars span:nth-child(5) { animation-delay: 0.4s; }

@keyframes progress-bars-stretch {
    0%, 40%, 100% {
        height: 20px;
    }
    20% {
        height: 40px;
    }
}

/* ============================================
   Inline Loading Styles
   ============================================ */

.inline-loading {
    display: inline-flex;
    align-items: center;
    vertical-align: middle;
}

.inline-loading-xs .spinner-border-sm {
    width: 0.75rem;
    height: 0.75rem;
}

.inline-loading-sm .spinner-border-sm {
    width: 1rem;
    height: 1rem;
}

.inline-loading-md .spinner-border {
    width: 1.5rem;
    height: 1.5rem;
}

.inline-loading-lg .spinner-border {
    width: 2rem;
    height: 2rem;
}

.inline-loading-text {
    font-size: inherit;
    color: var(--bs-secondary-color);
}

/* Inline dots animation */
.inline-loading-dots {
    display: inline-flex;
    gap: 4px;
}

.inline-loading-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background-color: currentColor;
    animation: inline-dots-fade 1.4s ease-in-out infinite;
}

.inline-loading-dots span:nth-child(1) { animation-delay: 0s; }
.inline-loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.inline-loading-dots span:nth-child(3) { animation-delay: 0.4s; }

.inline-loading-xs .inline-loading-dots span {
    width: 4px;
    height: 4px;
}

.inline-loading-lg .inline-loading-dots span {
    width: 8px;
    height: 8px;
    gap: 6px;
}

@keyframes inline-dots-fade {
    0%, 80%, 100% {
        opacity: 0.3;
    }
    40% {
        opacity: 1;
    }
}

/* Inline pulse animation */
.inline-loading-pulse {
    width: 1rem;
    height: 1rem;
    border-radius: 50%;
    background-color: var(--bs-primary);
    animation: inline-pulse 1.5s ease-in-out infinite;
}

.inline-loading-xs .inline-loading-pulse {
    width: 0.75rem;
    height: 0.75rem;
}

.inline-loading-lg .inline-loading-pulse {
    width: 1.5rem;
    height: 1.5rem;
}

@keyframes inline-pulse {
    0%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
    }
    50% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ============================================
   Page Loading Styles
   ============================================ */

.page-loading {
    min-height: 400px;
}

/* ============================================
   Content Loading States
   ============================================ */

/* Loading state for containers */
.is-loading {
    position: relative;
    pointer-events: none;
}

.is-loading::after {
    content: '';
    position: absolute;
    inset: 0;
    background: var(--dp-surface-overlay);
    z-index: 10;
}

[data-bs-theme="dark"] .is-loading::after,
.dark-mode .is-loading::after {
    background: var(--dp-surface-overlay);
}

/* Loading state for cards */
.card.is-loading .card-body {
    opacity: 0.5;
}

/* Loading state for buttons */
.btn.is-loading {
    position: relative;
    color: transparent !important;
}

.btn.is-loading::after {
    content: '';
    position: absolute;
    width: 1rem;
    height: 1rem;
    top: 50%;
    left: 50%;
    margin-left: -0.5rem;
    margin-top: -0.5rem;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    /* spinner-enter: fade+scale in on first appearance, then spin continuously */
    animation:
        spinner-enter var(--motion-fast, 150ms) var(--ease-out, cubic-bezier(0, 0, 0.2, 1)) both,
        btn-loading-spin 0.75s linear 150ms infinite;
}

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

/* ============================================
   Accessibility
   ============================================ */

/* Screen reader only loading text */
.loading-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;
}

/* Focus visible for loading elements */
.skeleton:focus-visible,
.progress-overlay-card:focus-visible {
    outline: 2px solid var(--bs-primary);
    outline-offset: 2px;
}

/* ============================================
   Responsive Adjustments
   ============================================ */

@media (max-width: 768px) {
    .progress-overlay-card {
        padding: 1.5rem;
        border-radius: 8px;
    }
    
    .progress-overlay-bar {
        width: 100% !important;
    }
    
    .skeleton-table th,
    .skeleton-table td {
        padding: 0.5rem;
    }
}

/* =============================================
   AUTH CHECKING STATE — Phased visibility
   ============================================= */
.auth-checking-state {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 400px;
}

.auth-checking-state__indicator {
    text-align: center;
}

/* Phase 1: hidden (0-200ms) */
.auth-checking-state__indicator .progress-dots {
    opacity: 0;
}

/* Phase 2: dots visible (200ms+) */
.auth-checking-state__indicator.is-visible .progress-dots {
    opacity: 1;
    animation: fade-in var(--motion-fast, 150ms) var(--ease-out, cubic-bezier(0, 0, 0.2, 1)) both;
}

/* Phase 3: text visible (2000ms+) */
.auth-checking-state__text {
    opacity: 0;
    font-size: 0.8125rem;
    color: var(--dp-text-muted);
    margin-top: 12px;
    transition: opacity 200ms ease-out;
}

.auth-checking-state__text.is-visible {
    opacity: 1;
}

/* Phase 4: recovery (10000ms+) */
.auth-checking-state__recovery {
    text-align: center;
    animation: fade-in 200ms ease-out both;
}

.auth-checking-state__recovery-icon {
    font-size: 1.5rem;
    color: var(--dp-status-warning);
    margin-bottom: 12px;
}

.auth-checking-state__recovery-text {
    font-size: 0.9375rem;
    color: var(--dp-text-secondary);
    margin-bottom: 16px;
    max-width: 320px;
    margin-left: auto;
    margin-right: auto;
}

/* Auth state cards */
.auth-state-card {
    background-color: var(--dp-surface-primary);
    border: 1px solid var(--dp-border-default);
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    max-width: 420px;
    margin: 0 auto;
}

.auth-state-card--denied {
    border-color: var(--dp-status-danger-border);
}

.auth-state-card__icon {
    font-size: 2.5rem;
    line-height: 1;
}

.auth-state-card__title {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--dp-text-primary);
    margin-bottom: 8px;
}

.auth-state-card__description {
    font-size: 0.9375rem;
    color: var(--dp-text-muted);
    margin-bottom: 24px;
}

/* Responsive */
@media (max-width: 767.98px) {
    .auth-state-card .d-flex.gap-2 {
        flex-direction: column;
    }
    .auth-state-card .d-flex.gap-2 .btn {
        width: 100%;
    }
    .auth-checking-state {
        min-height: 250px;
    }
}

@media (min-width: 768px) and (max-width: 1279.98px) {
    .auth-checking-state {
        min-height: 300px;
    }
}

/* ============================================
   Utility Classes
   ============================================ */

/* Quick skeleton helpers */
.skeleton-w-25 { width: 25% !important; }
.skeleton-w-50 { width: 50% !important; }
.skeleton-w-75 { width: 75% !important; }
.skeleton-w-100 { width: 100% !important; }

.skeleton-h-sm { height: 12px !important; }
.skeleton-h-md { height: 16px !important; }
.skeleton-h-lg { height: 24px !important; }
.skeleton-h-xl { height: 32px !important; }

/* Disable animations utility */
.skeleton-no-animation,
.skeleton-no-animation .skeleton-shimmer,
.skeleton-no-animation .skeleton-pulse,
.skeleton-no-animation .skeleton-wave {
    animation: none !important;
}
