/*
 * HiBargh B2B Portal - Premium Micro-interactions
 * Apple Human Interface Guidelines (HIG) Inspired
 */

/* =============================================================================
   View Transitions API — SPA page-change animations (Bug Fix #3)
   Only applies in browsers that support startViewTransition (Chrome 111+, Safari 18+).
   Falls back to instant swap in older browsers — no regression.
   ============================================================================= */
@keyframes spa-content-exit {
    from { opacity: 1; transform: translateY(0) scale(1); }
    to   { opacity: 0; transform: translateY(-6px) scale(0.995); }
}

@keyframes spa-content-enter {
    from { opacity: 0; transform: translateY(12px) scale(0.998); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* Persistent shell elements (sidebar, header) don't transition */
::view-transition-group(root) {
    animation-duration: 0.35s;
}

::view-transition-old(root) {
    animation: spa-content-exit 0.25s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

::view-transition-new(root) {
    animation: spa-content-enter 0.35s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

@media (prefers-reduced-motion: reduce) {
    ::view-transition-old(root),
    ::view-transition-new(root) {
        animation: none;
    }
}

:root {
    --apple-ease: cubic-bezier(0.4, 0, 0.2, 1);
    --shimmer-bg: #e5e5e7;
    --shimmer-highlight: #f5f5f7;
}

[data-theme="dark"] {
    --shimmer-bg: #2c2c2e;
    --shimmer-highlight: #3a3a3c;
}

/* Dark body background for non-SPA page load */
[data-theme="dark"] body {
    background-color: #1c1c1e;
}

/* --- Premium Button Overhaul --- */
.btn-apple-primary,
.btn-apple-dark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 2rem;
    height: 48px;
    border-radius: 999px;
    /* Capsule shape */
    font-weight: 600;
    letter-spacing: -0.01em;
    color: white;
    transition: all 0.3s var(--apple-ease);
    position: relative;
    overflow: hidden;
    cursor: pointer;
    border: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.btn-apple-primary {
    background: var(--apple-blue);
}

.btn-apple-dark {
    background: var(--apple-blue);
}

/* Inner Glow / 3D Glass Effect */
.btn-apple-primary::before,
.btn-apple-dark::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 50%;
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0.15), transparent);
    border-radius: 999px 999px 0 0;
    pointer-events: none;
}

/* Interaction: The "Squish" Effect */
.btn-apple-primary:active,
.btn-apple-dark:active {
    transform: scale(0.95);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.btn-apple-primary:hover,
.btn-apple-dark:hover {
    filter: brightness(1.1);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

/* --- Morphing Button Logic --- */
.morph-button {
    transition: all 0.4s var(--apple-ease);
    position: relative;
    overflow: hidden;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.morph-button.loading {
    width: 48px !important;
    /* Matches height for perfect circle */
    /* Forces a perfect circle/square based on min-height */
    padding: 0 !important;
    border-radius: 50% !important;
    scale: 0.95;
    flex: 0 0 48px !important;
    /* Override flex-1 on parent so the morph stays a circle, not stretched oval */
    min-width: 0 !important;
}

.morph-button.success {
    background: #34c759 !important;
    /* Apple Success Green */
    width: 48px !important;
    padding: 0 !important;
    border-radius: 50% !important;
    flex: 0 0 48px !important;
    min-width: 0 !important;
}

.morph-button .button-content {
    transition: opacity 0.2s var(--apple-ease);
    width: max-content;
}

.morph-button.loading .button-content,
.morph-button.success .button-content {
    opacity: 0;
    pointer-events: none;
}

/* --- Premium Spinner & Success Mark --- */
.premium-spinner,
.success-checkmark {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: all 0.3s var(--apple-ease);
}

.morph-button.loading .premium-spinner {
    opacity: 1;
}

.morph-button.success .success-checkmark {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.1);
}

.morph-button.success .premium-spinner {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.5);
}

.spinner-icon {
    width: 100%;
    height: 100%;
    animation: spin 1s linear infinite;
    stroke: currentColor;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

/* --- Skeleton Screens --- */
.skeleton {
    background: var(--shimmer-bg);
    background-image: linear-gradient(90deg,
            var(--shimmer-bg) 0px,
            var(--shimmer-highlight) 40px,
            var(--shimmer-bg) 80px);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite linear;
    border-radius: 1rem;
}

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

    100% {
        background-position: 200% 0;
    }
}

/* Skeleton Shape Classes */
.skeleton-title {
    height: 1.5rem;
    width: 70%;
    margin-bottom: 1rem;
}

.skeleton-text {
    height: 1rem;
    width: 40%;
    margin-bottom: 0.5rem;
}

.skeleton-badge {
    height: 1.5rem;
    width: 60px;
    border-radius: 999px;
}

.skeleton-button {
    height: 44px;
    width: 100%;
    border-radius: 1.5rem;
}

/* --- Page Transitions --- */
.page-transition-entry {
    animation: fadeSlideUp 0.4s var(--apple-ease) forwards;
    will-change: transform, opacity;
}

@keyframes fadeSlideUp {
    0% {
        opacity: 0;
        transform: translateY(20px);
    }

    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Non-SPA Page Load smoother */
body {
    opacity: 0;
    background-color: #f5f5f7;
}

body.loaded {
    opacity: 1;
    transition: opacity 0.4s var(--apple-ease);
}

/* Reduced Motion Override */
@media (prefers-reduced-motion: reduce) {
    .morph-button {
        transition: none;
    }

    .spinner-icon {
        animation: none;
    }

    .skeleton {
        animation: none;
        background-image: none;
    }

    .page-transition-entry {
        animation: none;
        opacity: 1;
    }

    body {
        opacity: 1;
    }
}
