/* ============================================
   RSL KPI Cards Component
   Version: 1.0.0
   Description: Premium dashboard KPI cards with
   trend indicators, sparklines, and animations.
   A flagship RSL component for data visualization.
   ============================================ */

/* ============================================
   CSS Custom Properties
   ============================================ */
:root {
    /* Base colors */
    --rsl-kpi-bg: #ffffff;
    --rsl-kpi-border: #e2e8f0;
    --rsl-kpi-text: #1e293b;
    --rsl-kpi-text-muted: #64748b;
    --rsl-kpi-shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
    --rsl-kpi-shadow-hover: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);

    /* Trend colors */
    --rsl-kpi-trend-up: #10b981;
    --rsl-kpi-trend-up-bg: rgba(16, 185, 129, 0.1);
    --rsl-kpi-trend-down: #ef4444;
    --rsl-kpi-trend-down-bg: rgba(239, 68, 68, 0.1);
    --rsl-kpi-trend-neutral: #6b7280;
    --rsl-kpi-trend-neutral-bg: rgba(107, 114, 128, 0.1);

    /* Color schemes */
    --rsl-kpi-primary: #6E7BFF;
    --rsl-kpi-primary-light: rgba(110, 123, 255, 0.1);
    --rsl-kpi-success: #10b981;
    --rsl-kpi-success-light: rgba(16, 185, 129, 0.1);
    --rsl-kpi-warning: #f59e0b;
    --rsl-kpi-warning-light: rgba(245, 158, 11, 0.1);
    --rsl-kpi-danger: #ef4444;
    --rsl-kpi-danger-light: rgba(239, 68, 68, 0.1);
    --rsl-kpi-info: #06b6d4;
    --rsl-kpi-info-light: rgba(6, 182, 212, 0.1);

    /* Animation timing */
    --rsl-kpi-transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ============================================
   Base KPI Card
   ============================================ */
.rsl-kpi-card {
    background: var(--rsl-kpi-bg);
    border: 1px solid var(--rsl-kpi-border);
    border-radius: 12px;
    padding: 1.5rem;
    box-shadow: var(--rsl-kpi-shadow);
    transition: all var(--rsl-kpi-transition);
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 140px;
}

.rsl-kpi-card:hover {
    box-shadow: var(--rsl-kpi-shadow-hover);
    transform: translateY(-2px);
}

/* Focus states for keyboard accessibility */
.rsl-kpi-card:focus {
    outline: 2px solid var(--rsl-kpi-scheme-color, var(--rsl-kpi-primary));
    outline-offset: 2px;
}

.rsl-kpi-card:focus:not(:focus-visible) {
    outline: none;
}

.rsl-kpi-card:focus-visible {
    outline: 2px solid var(--rsl-kpi-scheme-color, var(--rsl-kpi-primary));
    outline-offset: 2px;
    box-shadow: var(--rsl-kpi-shadow-hover), 0 0 0 4px var(--rsl-kpi-scheme-light, var(--rsl-kpi-primary-light));
}

/* ============================================
   Card Header (Icon + Label)
   ============================================ */
.rsl-kpi-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

.rsl-kpi-label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--rsl-kpi-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.025em;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.rsl-kpi-icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.125rem;
    background: var(--rsl-kpi-primary-light);
    color: var(--rsl-kpi-primary);
    flex-shrink: 0;
}

/* ============================================
   Main Value Display
   ============================================ */
.rsl-kpi-value-container {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-bottom: 0.5rem;
}

.rsl-kpi-value {
    font-size: 2.25rem;
    font-weight: 700;
    color: var(--rsl-kpi-text);
    line-height: 1.2;
    letter-spacing: -0.025em;
}

.rsl-kpi-value-prefix,
.rsl-kpi-value-suffix {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--rsl-kpi-text-muted);
}

/* Animated value counter */
.rsl-kpi-value[data-animate] {
    opacity: 0;
    transform: translateY(10px);
}

.rsl-kpi-value[data-animate].animated {
    opacity: 1;
    transform: translateY(0);
    transition: opacity 0.5s ease, transform 0.5s ease;
}

/* ============================================
   Trend Indicator
   ============================================ */
.rsl-kpi-trend {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem 0.625rem;
    border-radius: 9999px;
    font-size: 0.8125rem;
    font-weight: 600;
    transition: all var(--rsl-kpi-transition);
}

/* Trend Up */
.rsl-kpi-trend[data-direction="up"],
.rsl-kpi-trend.trend-up {
    background: var(--rsl-kpi-trend-up-bg);
    color: var(--rsl-kpi-trend-up);
}

.rsl-kpi-trend[data-direction="up"] .rsl-kpi-trend-icon::before,
.rsl-kpi-trend.trend-up .rsl-kpi-trend-icon::before {
    content: "↑";
    font-weight: 700;
}

/* Trend Down */
.rsl-kpi-trend[data-direction="down"],
.rsl-kpi-trend.trend-down {
    background: var(--rsl-kpi-trend-down-bg);
    color: var(--rsl-kpi-trend-down);
}

.rsl-kpi-trend[data-direction="down"] .rsl-kpi-trend-icon::before,
.rsl-kpi-trend.trend-down .rsl-kpi-trend-icon::before {
    content: "↓";
    font-weight: 700;
}

/* Trend Neutral */
.rsl-kpi-trend[data-direction="neutral"],
.rsl-kpi-trend.trend-neutral {
    background: var(--rsl-kpi-trend-neutral-bg);
    color: var(--rsl-kpi-trend-neutral);
}

.rsl-kpi-trend[data-direction="neutral"] .rsl-kpi-trend-icon::before,
.rsl-kpi-trend.trend-neutral .rsl-kpi-trend-icon::before {
    content: "→";
    font-weight: 700;
}

.rsl-kpi-trend-icon {
    display: inline-flex;
    font-size: 0.75rem;
}

/* FontAwesome icon support */
.rsl-kpi-trend .fa-arrow-up,
.rsl-kpi-trend .fa-arrow-trend-up {
    color: inherit;
}

.rsl-kpi-trend .fa-arrow-down,
.rsl-kpi-trend .fa-arrow-trend-down {
    color: inherit;
}

/* Animated trend entrance */
.rsl-kpi-trend[data-animate] {
    opacity: 0;
    transform: scale(0.8);
}

.rsl-kpi-trend[data-animate].animated {
    opacity: 1;
    transform: scale(1);
    transition: opacity 0.4s ease 0.2s, transform 0.4s ease 0.2s;
}

/* ============================================
   Comparison Text
   ============================================ */
.rsl-kpi-comparison {
    font-size: 0.8125rem;
    color: var(--rsl-kpi-text-muted);
    margin-top: 0.25rem;
}

/* ============================================
   Footer (Trend + Comparison)
   ============================================ */
.rsl-kpi-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: auto;
    padding-top: 0.75rem;
}

/* ============================================
   Sparkline Container
   ============================================ */
.rsl-kpi-sparkline {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    opacity: 0.15;
    pointer-events: none;
}

.rsl-kpi-sparkline svg {
    width: 100%;
    height: 100%;
}

/* Sparkline within card body (visible) */
.rsl-kpi-sparkline-inline {
    margin-top: 1rem;
    height: 50px;
    opacity: 1;
}

/* ============================================
   Progress Indicator
   ============================================ */
.rsl-kpi-progress {
    margin-top: 1rem;
}

.rsl-kpi-progress-bar {
    height: 8px;
    background: var(--rsl-kpi-border);
    border-radius: 9999px;
    overflow: hidden;
}

.rsl-kpi-progress-fill {
    height: 100%;
    background: var(--rsl-kpi-primary);
    border-radius: 9999px;
    transition: width 1s cubic-bezier(0.4, 0, 0.2, 1);
}

.rsl-kpi-progress-label {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: var(--rsl-kpi-text-muted);
    margin-top: 0.375rem;
}

/* Animated progress */
.rsl-kpi-progress-fill[data-animate] {
    width: 0 !important;
}

.rsl-kpi-progress-fill[data-animate].animated {
    width: var(--progress-width) !important;
}

/* ============================================
   Secondary Value
   ============================================ */
.rsl-kpi-secondary {
    display: flex;
    align-items: baseline;
    gap: 0.375rem;
    padding-top: 0.75rem;
    margin-top: 0.75rem;
    border-top: 1px solid var(--rsl-kpi-border);
}

.rsl-kpi-secondary-value {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--rsl-kpi-text);
}

.rsl-kpi-secondary-label {
    font-size: 0.8125rem;
    color: var(--rsl-kpi-text-muted);
}

/* ============================================
   Size Variants
   ============================================ */

/* Compact */
.rsl-kpi-card[data-size="compact"],
.rsl-kpi-card.rsl-kpi-compact {
    padding: 1rem;
    min-height: 100px;
}

.rsl-kpi-card[data-size="compact"] .rsl-kpi-value,
.rsl-kpi-card.rsl-kpi-compact .rsl-kpi-value {
    font-size: 1.5rem;
}

.rsl-kpi-card[data-size="compact"] .rsl-kpi-icon,
.rsl-kpi-card.rsl-kpi-compact .rsl-kpi-icon {
    width: 32px;
    height: 32px;
    font-size: 0.875rem;
}

/* Large / Hero */
.rsl-kpi-card[data-size="large"],
.rsl-kpi-card[data-size="hero"],
.rsl-kpi-card.rsl-kpi-hero {
    padding: 2rem;
    min-height: 200px;
}

.rsl-kpi-card[data-size="large"] .rsl-kpi-value,
.rsl-kpi-card[data-size="hero"] .rsl-kpi-value,
.rsl-kpi-card.rsl-kpi-hero .rsl-kpi-value {
    font-size: 3.5rem;
}

.rsl-kpi-card[data-size="large"] .rsl-kpi-icon,
.rsl-kpi-card[data-size="hero"] .rsl-kpi-icon,
.rsl-kpi-card.rsl-kpi-hero .rsl-kpi-icon {
    width: 56px;
    height: 56px;
    font-size: 1.5rem;
}

.rsl-kpi-card[data-size="large"] .rsl-kpi-label,
.rsl-kpi-card[data-size="hero"] .rsl-kpi-label,
.rsl-kpi-card.rsl-kpi-hero .rsl-kpi-label {
    font-size: 1rem;
}

/* ============================================
   Color Schemes
   ============================================ */

/* Primary (Default) */
.rsl-kpi-card[data-color-scheme="primary"],
.rsl-kpi-card.rsl-kpi-primary {
    --rsl-kpi-scheme-color: var(--rsl-kpi-primary);
    --rsl-kpi-scheme-light: var(--rsl-kpi-primary-light);
}

/* Success */
.rsl-kpi-card[data-color-scheme="success"],
.rsl-kpi-card.rsl-kpi-success {
    --rsl-kpi-scheme-color: var(--rsl-kpi-success);
    --rsl-kpi-scheme-light: var(--rsl-kpi-success-light);
}

/* Warning */
.rsl-kpi-card[data-color-scheme="warning"],
.rsl-kpi-card.rsl-kpi-warning {
    --rsl-kpi-scheme-color: var(--rsl-kpi-warning);
    --rsl-kpi-scheme-light: var(--rsl-kpi-warning-light);
}

/* Danger */
.rsl-kpi-card[data-color-scheme="danger"],
.rsl-kpi-card.rsl-kpi-danger {
    --rsl-kpi-scheme-color: var(--rsl-kpi-danger);
    --rsl-kpi-scheme-light: var(--rsl-kpi-danger-light);
}

/* Info */
.rsl-kpi-card[data-color-scheme="info"],
.rsl-kpi-card.rsl-kpi-info {
    --rsl-kpi-scheme-color: var(--rsl-kpi-info);
    --rsl-kpi-scheme-light: var(--rsl-kpi-info-light);
}

/* Apply color scheme to icon */
.rsl-kpi-card[data-color-scheme] .rsl-kpi-icon,
.rsl-kpi-card[class*="rsl-kpi-"] .rsl-kpi-icon {
    background: var(--rsl-kpi-scheme-light, var(--rsl-kpi-primary-light));
    color: var(--rsl-kpi-scheme-color, var(--rsl-kpi-primary));
}

/* Apply color scheme to progress */
.rsl-kpi-card[data-color-scheme] .rsl-kpi-progress-fill,
.rsl-kpi-card[class*="rsl-kpi-"] .rsl-kpi-progress-fill {
    background: var(--rsl-kpi-scheme-color, var(--rsl-kpi-primary));
}

/* Apply color scheme to value (optional accent) */
.rsl-kpi-card[data-color-scheme][data-accent-value] .rsl-kpi-value,
.rsl-kpi-card[class*="rsl-kpi-"][data-accent-value] .rsl-kpi-value {
    color: var(--rsl-kpi-scheme-color, var(--rsl-kpi-primary));
}

/* ============================================
   Gradient Accent Border (Premium)
   ============================================ */
.rsl-kpi-card[data-accent="gradient"],
.rsl-kpi-card.rsl-kpi-gradient-accent {
    border-top: 3px solid transparent;
    border-image: linear-gradient(90deg, var(--rsl-kpi-scheme-color, var(--rsl-kpi-primary)), var(--rsl-kpi-info)) 1;
}

/* Left accent line */
.rsl-kpi-card[data-accent="left"],
.rsl-kpi-card.rsl-kpi-accent-left {
    border-left: 4px solid var(--rsl-kpi-scheme-color, var(--rsl-kpi-primary));
    border-radius: 0 12px 12px 0;
}

/* ============================================
   Glow Effect (Premium)
   ============================================ */
.rsl-kpi-card[data-glow="true"],
.rsl-kpi-card.rsl-kpi-glow {
    box-shadow: var(--rsl-kpi-shadow), 0 0 20px var(--rsl-kpi-scheme-light, var(--rsl-kpi-primary-light));
}

.rsl-kpi-card[data-glow="true"]:hover,
.rsl-kpi-card.rsl-kpi-glow:hover {
    box-shadow: var(--rsl-kpi-shadow-hover), 0 0 30px var(--rsl-kpi-scheme-light, var(--rsl-kpi-primary-light));
}

/* ============================================
   Clickable/Interactive Cards
   ============================================ */
.rsl-kpi-card[data-clickable="true"],
.rsl-kpi-card.rsl-kpi-clickable {
    cursor: pointer;
}

.rsl-kpi-card[data-clickable="true"]:active,
.rsl-kpi-card.rsl-kpi-clickable:active {
    transform: translateY(0);
    box-shadow: var(--rsl-kpi-shadow);
}

/* ============================================
   Loading State / Skeleton
   ============================================ */
.rsl-kpi-card[data-loading="true"],
.rsl-kpi-card.rsl-kpi-loading {
    pointer-events: none;
}

/* Skeleton base styles */
.rsl-kpi-skeleton {
    background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
    background-size: 200% 100%;
    animation: rsl-kpi-shimmer 1.5s infinite;
    color: transparent !important;
    border-radius: 4px;
}

.rsl-kpi-card[data-loading="true"] .rsl-kpi-label,
.rsl-kpi-card.rsl-kpi-loading .rsl-kpi-label,
.rsl-kpi-label.rsl-kpi-skeleton {
    width: 60%;
    height: 1rem;
    min-width: 80px;
}

.rsl-kpi-card[data-loading="true"] .rsl-kpi-icon,
.rsl-kpi-card.rsl-kpi-loading .rsl-kpi-icon,
.rsl-kpi-icon.rsl-kpi-skeleton {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
    background-size: 200% 100%;
    animation: rsl-kpi-shimmer 1.5s infinite;
}

.rsl-kpi-card[data-loading="true"] .rsl-kpi-value,
.rsl-kpi-card.rsl-kpi-loading .rsl-kpi-value,
.rsl-kpi-value.rsl-kpi-skeleton {
    background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
    background-size: 200% 100%;
    animation: rsl-kpi-shimmer 1.5s infinite;
    color: transparent;
    border-radius: 4px;
    min-width: 120px;
    height: 2.5rem;
}

.rsl-kpi-card[data-loading="true"] .rsl-kpi-trend,
.rsl-kpi-card.rsl-kpi-loading .rsl-kpi-trend,
.rsl-kpi-trend.rsl-kpi-skeleton {
    background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
    background-size: 200% 100%;
    animation: rsl-kpi-shimmer 1.5s infinite;
    color: transparent;
    border-radius: 4px;
    min-width: 60px;
    height: 1.25rem;
}

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

/* ============================================
   Dark Mode Support
   ============================================ */
[data-theme="dark"] .rsl-kpi-card {
    --rsl-kpi-bg: #1e293b;
    --rsl-kpi-border: #334155;
    --rsl-kpi-text: #f1f5f9;
    --rsl-kpi-text-muted: #94a3b8;
    --rsl-kpi-shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2);
    --rsl-kpi-shadow-hover: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -2px rgba(0, 0, 0, 0.2);

    /* Adjusted scheme lights for dark mode */
    --rsl-kpi-primary-light: rgba(110, 123, 255, 0.2);
    --rsl-kpi-success-light: rgba(16, 185, 129, 0.2);
    --rsl-kpi-warning-light: rgba(245, 158, 11, 0.2);
    --rsl-kpi-danger-light: rgba(239, 68, 68, 0.2);
    --rsl-kpi-info-light: rgba(6, 182, 212, 0.2);

    /* Trend backgrounds */
    --rsl-kpi-trend-up-bg: rgba(16, 185, 129, 0.2);
    --rsl-kpi-trend-down-bg: rgba(239, 68, 68, 0.2);
    --rsl-kpi-trend-neutral-bg: rgba(107, 114, 128, 0.2);
}

/* Dark mode skeleton styles */
[data-theme="dark"] .rsl-kpi-skeleton,
[data-theme="dark"] .rsl-kpi-card[data-loading="true"] .rsl-kpi-label,
[data-theme="dark"] .rsl-kpi-card[data-loading="true"] .rsl-kpi-icon,
[data-theme="dark"] .rsl-kpi-card[data-loading="true"] .rsl-kpi-value,
[data-theme="dark"] .rsl-kpi-card[data-loading="true"] .rsl-kpi-trend,
[data-theme="dark"] .rsl-kpi-card.rsl-kpi-loading .rsl-kpi-label,
[data-theme="dark"] .rsl-kpi-card.rsl-kpi-loading .rsl-kpi-icon,
[data-theme="dark"] .rsl-kpi-card.rsl-kpi-loading .rsl-kpi-value,
[data-theme="dark"] .rsl-kpi-card.rsl-kpi-loading .rsl-kpi-trend {
    background: linear-gradient(90deg, #334155 25%, #475569 50%, #334155 75%);
    background-size: 200% 100%;
}

/* ============================================
   Responsive Design
   ============================================ */
@media (max-width: 768px) {
    .rsl-kpi-card {
        padding: 1.25rem;
        min-height: 120px;
    }

    .rsl-kpi-value {
        font-size: 1.875rem;
    }

    .rsl-kpi-card[data-size="large"] .rsl-kpi-value,
    .rsl-kpi-card[data-size="hero"] .rsl-kpi-value,
    .rsl-kpi-card.rsl-kpi-hero .rsl-kpi-value {
        font-size: 2.5rem;
    }

    .rsl-kpi-icon {
        width: 36px;
        height: 36px;
        font-size: 1rem;
    }

    .rsl-kpi-footer {
        flex-direction: column;
        align-items: flex-start;
    }
}

@media (max-width: 480px) {
    .rsl-kpi-value {
        font-size: 1.5rem;
    }

    .rsl-kpi-card[data-size="large"] .rsl-kpi-value,
    .rsl-kpi-card[data-size="hero"] .rsl-kpi-value,
    .rsl-kpi-card.rsl-kpi-hero .rsl-kpi-value {
        font-size: 2rem;
    }

    .rsl-kpi-header {
        flex-direction: column-reverse;
        align-items: flex-start;
        gap: 0.75rem;
    }
}

/* ============================================
   Reduced Motion Support
   ============================================ */
@media (prefers-reduced-motion: reduce) {
    .rsl-kpi-card {
        transition: none;
    }

    .rsl-kpi-card:hover {
        transform: none;
    }

    .rsl-kpi-value[data-animate],
    .rsl-kpi-trend[data-animate],
    .rsl-kpi-progress-fill[data-animate] {
        opacity: 1;
        transform: none;
        transition: none;
    }

    .rsl-kpi-progress-fill {
        transition: none;
    }

    @keyframes rsl-kpi-shimmer {
        0%, 100% { background-position: 0 0; }
    }
}

/* ============================================
   High Contrast Mode
   ============================================ */
@media (prefers-contrast: high) {
    .rsl-kpi-card {
        border-width: 2px;
    }

    .rsl-kpi-trend {
        border: 1px solid currentColor;
    }

    .rsl-kpi-progress-bar {
        border: 1px solid var(--rsl-kpi-text-muted);
    }
}

/* ============================================
   Print Styles
   ============================================ */
@media print {
    .rsl-kpi-card {
        box-shadow: none;
        border: 1px solid #000;
        break-inside: avoid;
    }

    .rsl-kpi-sparkline {
        display: none;
    }
}

/* ============================================
   FilterBus Visibility
   Show/hide KPI cards based on filter state
   ============================================ */

/* Only apply transitions AFTER initialization to prevent flicker on page load */
.rsl-kpi-card.rsl-kpi-initialized {
    transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s ease;
}

.rsl-kpi-card.rsl-kpi-hidden {
    opacity: 0;
    transform: scale(0.95);
    visibility: hidden;
    pointer-events: none;
    position: absolute;
    /* Keep in flow but invisible for smooth grid reflow */
}

/* For grid layouts - hide the slot-item container when card is hidden */
.slot-item:has(.rsl-kpi-card.rsl-kpi-hidden) {
    display: none;
}

/* Alternative: fade only without collapsing */
.rsl-kpi-card[data-filter-fade-only].rsl-kpi-hidden {
    opacity: 0.3;
    transform: none;
    visibility: visible;
    pointer-events: auto;
    position: relative;
    height: auto;
    min-height: auto;
}

/* Respect reduced motion preferences */
@media (prefers-reduced-motion: reduce) {
    .rsl-kpi-card {
        transition: none;
    }

    .rsl-kpi-card.rsl-kpi-hidden {
        transition: none;
    }
}
