// ============================================
// ADMIN ANALYTICS - Mobile-First Compact Design
// ============================================
@import '../admin-shared.scss';

// ============================================
// LOADING & ERROR STATES
// ============================================
.loading-state,
.error-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: $admin-space-2xl;
    background: $glass-bg;
    border: 1px solid $glass-border;
    border-radius: $radius-md;
    text-align: center;
    animation: fadeIn 0.3s ease-out;

    .loading-spinner {
        width: 40px;
        height: 40px;
        border: 3px solid rgba($color-brand-primary, 0.2);
        border-top-color: $color-brand-primary;
        border-radius: 50%;
        animation: spin 1s linear infinite;
        margin-bottom: $admin-space-md;
    }

    .material-symbols-outlined {
        font-size: 2.5rem;
        color: $accent-red;
        margin-bottom: $admin-space-sm;
    }

    h3 {
        margin: 0 0 $admin-space-2xs;
        font-size: 1rem;
        font-weight: 700;
        color: $color-text-primary;
    }

    p {
        color: $color-text-tertiary;
        font-size: 0.875rem;
        margin: 0 0 $admin-space-md;
    }
}

// ============================================
// AUTO-RELOAD & COUNTDOWN
// ============================================
.auto-reload-container {
    display: flex;
    align-items: center;
    gap: $admin-space-sm;
}

.auto-reload-toggle {
    .toggle-label {
        display: flex;
        align-items: center;
        gap: $admin-space-xs;
        cursor: pointer;
        font-size: 0.8125rem;
        color: $color-text-secondary;
    }

    .toggle-input {
        position: absolute;
        opacity: 0;
        pointer-events: none;

        &:checked + .toggle-slider {
            background: $color-brand-primary;

            &::before {
                transform: translateX(16px);
            }
        }
    }

    .toggle-slider {
        position: relative;
        width: 36px;
        height: 20px;
        background: $color-gray-300;
        border-radius: $radius-full;
        transition: background $transition-fast;

        &::before {
            content: '';
            position: absolute;
            top: 2px;
            left: 2px;
            width: 16px;
            height: 16px;
            background: white;
            border-radius: 50%;
            transition: transform $transition-fast;
            box-shadow: $shadow-sm;
        }
    }

    .toggle-text {
        font-weight: 500;
        white-space: nowrap;
    }
}

.countdown-timer {
    display: flex;
    align-items: center;
    gap: $admin-space-xs;
    padding: $admin-space-2xs $admin-space-sm;
    background: rgba($color-brand-primary, 0.1);
    border-radius: $radius-full;
}

.countdown-text {
    font-size: 0.75rem;
    font-weight: 600;
    color: $color-brand-primary;
    min-width: 24px;
}

.countdown-progress {
    width: 40px;
    height: 4px;
    background: rgba($color-brand-primary, 0.2);
    border-radius: 2px;
    overflow: hidden;
}

.countdown-bar {
    height: 100%;
    background: $color-brand-primary;
    border-radius: 2px;
    transition: width 1s linear;
}

// ============================================
// PERIOD SELECTOR
// ============================================
.period-selector {
    display: flex;
    gap: $admin-space-2xs;
    margin-bottom: $admin-space-md;
    overflow-x: auto;
    padding-bottom: $admin-space-2xs;
    scrollbar-width: none;
    animation: fadeIn 0.3s ease-out;

    &::-webkit-scrollbar {
        display: none;
    }
}

.period-btn {
    padding: $admin-space-xs $admin-space-sm;
    background: $glass-bg;
    border: 1px solid $glass-border;
    border-radius: $radius-sm;
    font-size: 0.75rem;
    font-weight: 600;
    color: $color-text-secondary;
    cursor: pointer;
    transition: all $transition-fast;
    white-space: nowrap;

    &:hover {
        border-color: $color-brand-primary;
        color: $color-brand-primary;
    }

    &.active {
        background: $gradient-primary;
        border-color: transparent;
        color: white;
    }
}

// ============================================
// STAT CARDS
// ============================================
.stat-cards {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: $admin-space-xs;
    margin-bottom: $admin-space-md;
    animation: fadeIn 0.3s ease-out 0.05s backwards;

    @media (min-width: 640px) {
        grid-template-columns: repeat(4, 1fr);
    }
}

.stat-card {
    display: flex;
    align-items: center;
    gap: $admin-space-sm;
    padding: $admin-space-sm;
    background: $glass-bg;
    border: 1px solid $glass-border;
    border-radius: $radius-md;
    transition: all $transition-fast;
    animation: staggerFade 0.3s ease-out backwards;

    @for $i from 1 through 4 {
        &:nth-child(#{$i}) {
            animation-delay: #{$i * 50}ms;
        }
    }

    &:hover {
        box-shadow: $shadow-sm;
        transform: translateY(-2px);
        border-color: rgba($color-brand-primary, 0.3);
    }
}

.stat-icon {
    width: 40px;
    height: 40px;
    border-radius: $radius-sm;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;

    .material-symbols-outlined {
        font-size: 1.25rem;
        color: white;
    }

    &--blue { background: linear-gradient(135deg, $accent-blue, darken($accent-blue, 10%)); }
    &--purple { background: linear-gradient(135deg, $accent-purple, darken($accent-purple, 10%)); }
    &--green { background: linear-gradient(135deg, $accent-green, darken($accent-green, 10%)); }
    &--orange { background: linear-gradient(135deg, $accent-orange, darken($accent-orange, 10%)); }

    @media (min-width: 768px) {
        width: 44px;
        height: 44px;

        .material-symbols-outlined {
            font-size: 1.375rem;
        }
    }
}

.stat-content {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.stat-value {
    font-size: 1.25rem;
    font-weight: 800;
    color: $color-text-primary;
    line-height: 1.2;

    @media (min-width: 768px) {
        font-size: 1.5rem;
    }
}

.stat-label {
    font-size: 0.625rem;
    font-weight: 600;
    color: $color-text-tertiary;
    text-transform: uppercase;
    letter-spacing: 0.03em;

    @media (min-width: 768px) {
        font-size: 0.6875rem;
    }
}

// ============================================
// ANALYTICS GRID
// ============================================
.analytics-grid {
    display: grid;
    gap: $admin-space-sm;
    grid-template-columns: 1fr;
    animation: fadeIn 0.3s ease-out 0.1s backwards;

    @media (min-width: 768px) {
        grid-template-columns: repeat(2, 1fr);
    }

    @media (min-width: 1024px) {
        grid-template-columns: repeat(3, 1fr);
    }
}

// ============================================
// ANALYTICS CARD
// ============================================
.analytics-card {
    background: $glass-bg;
    border: 1px solid $glass-border;
    border-radius: $radius-md;
    overflow: hidden;
    animation: staggerFade 0.3s ease-out backwards;
    transition: all $transition-fast;

    &:hover {
        border-color: rgba($color-brand-primary, 0.2);
        box-shadow: $shadow-sm;
    }

    @for $i from 1 through 6 {
        &:nth-child(#{$i}) {
            animation-delay: #{100 + ($i * 50)}ms;
        }
    }

    // Card variants for grid placement
    &.chart-card {
        @media (min-width: 768px) {
            grid-column: span 2;
        }
    }

    &.events-card {
        @media (min-width: 1024px) {
            grid-row: span 2;
        }
    }

    // Card header (h3 direct child)
    > h3 {
        display: flex;
        align-items: center;
        gap: $admin-space-xs;
        padding: $admin-space-sm $admin-space-md;
        margin: 0;
        font-size: 0.875rem;
        font-weight: 700;
        color: $color-text-primary;
        border-bottom: 1px solid $border-default;
        background: rgba($color-gray-50, 0.5);

        .material-symbols-outlined {
            font-size: 1.125rem;
            color: $color-brand-primary;
        }
    }
}

// ============================================
// SIMPLE CHART (Bar Chart)
// ============================================
.simple-chart {
    display: flex;
    align-items: flex-end;
    gap: 2px;
    height: 120px;
    padding: $admin-space-md;
    padding-bottom: 0;

    @media (min-width: 768px) {
        height: 160px;
    }
}

.chart-bar {
    flex: 1;
    min-width: 4px;
    background: linear-gradient(180deg, $color-brand-primary, rgba($color-brand-primary, 0.6));
    border-radius: 2px 2px 0 0;
    transition: all $transition-fast;
    position: relative;
    cursor: pointer;

    &:hover {
        background: linear-gradient(180deg, $accent-purple, rgba($accent-purple, 0.6));
        transform: scaleY(1.02);
    }

    &:hover .chart-tooltip {
        opacity: 1;
        visibility: visible;
        transform: translateX(-50%) translateY(-4px);
    }
}

.chart-tooltip {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%) translateY(0);
    padding: $admin-space-2xs $admin-space-xs;
    background: $color-gray-900;
    color: white;
    font-size: 0.625rem;
    font-weight: 600;
    border-radius: $radius-sm;
    white-space: nowrap;
    opacity: 0;
    visibility: hidden;
    transition: all $transition-fast;
    pointer-events: none;
    z-index: 10;

    &::after {
        content: '';
        position: absolute;
        top: 100%;
        left: 50%;
        transform: translateX(-50%);
        border: 4px solid transparent;
        border-top-color: $color-gray-900;
    }
}

.chart-labels {
    display: flex;
    justify-content: space-between;
    padding: $admin-space-xs $admin-space-md $admin-space-md;
    font-size: 0.625rem;
    color: $color-text-tertiary;
}

// ============================================
// LIST CONTENT (Top Pages, Referrers)
// ============================================
.list-content {
    display: flex;
    flex-direction: column;
    gap: $admin-space-2xs;
    padding: $admin-space-sm;
    max-height: 200px;
    overflow-y: auto;

    &::-webkit-scrollbar {
        width: 4px;
    }

    &::-webkit-scrollbar-track {
        background: transparent;
    }

    &::-webkit-scrollbar-thumb {
        background: $color-gray-300;
        border-radius: 2px;
    }
}

.list-item {
    display: flex;
    align-items: center;
    gap: $admin-space-xs;
    padding: $admin-space-xs;
    background: $color-gray-50;
    border-radius: $radius-sm;
    transition: all $transition-fast;

    &:hover {
        background: $color-gray-100;
    }
}

.list-rank {
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: $color-brand-primary;
    color: white;
    font-size: 0.625rem;
    font-weight: 700;
    border-radius: $radius-sm;
    flex-shrink: 0;
}

.list-name {
    flex: 1;
    font-size: 0.75rem;
    color: $color-text-primary;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.list-value {
    font-size: 0.75rem;
    font-weight: 700;
    color: $color-brand-primary;
    padding: $admin-space-2xs $admin-space-xs;
    background: rgba($color-brand-primary, 0.1);
    border-radius: $radius-sm;
}

// ============================================
// EMPTY STATE
// ============================================
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: $admin-space-xl;
    text-align: center;
    color: $color-text-tertiary;

    .material-symbols-outlined {
        font-size: 2rem;
        margin-bottom: $admin-space-xs;
        opacity: 0.4;
    }

    p {
        font-size: 0.8125rem;
        margin: 0;
    }
}

// ============================================
// DEVICES SECTION
// ============================================
.devices-content {
    display: flex;
    flex-direction: column;
    gap: $admin-space-sm;
    padding: $admin-space-sm;
}

.device-row {
    display: flex;
    align-items: center;
    gap: $admin-space-xs;
}

.device-icon {
    font-size: 1.25rem;
    width: 32px;
    color: $color-text-tertiary;
}

.device-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: $color-text-secondary;
    width: 60px;
}

.device-bar {
    flex: 1;
    height: 8px;
    background: $color-gray-200;
    border-radius: $radius-full;
    overflow: hidden;
}

.device-fill {
    height: 100%;
    border-radius: $radius-full;
    transition: width 0.5s ease-out;

    &.desktop {
        background: linear-gradient(90deg, $accent-blue, lighten($accent-blue, 10%));
    }

    &.mobile {
        background: linear-gradient(90deg, $accent-green, lighten($accent-green, 10%));
    }

    &.tablet {
        background: linear-gradient(90deg, $accent-purple, lighten($accent-purple, 10%));
    }
}

.device-percent {
    font-size: 0.75rem;
    font-weight: 700;
    color: $color-text-primary;
    width: 40px;
    text-align: right;
}

// ============================================
// EVENTS SECTION
// ============================================
.events-list {
    display: flex;
    flex-direction: column;
    gap: $admin-space-2xs;
    padding: $admin-space-sm;
    max-height: 280px;
    overflow-y: auto;

    @media (min-width: 1024px) {
        max-height: 400px;
    }

    &::-webkit-scrollbar {
        width: 4px;
    }

    &::-webkit-scrollbar-track {
        background: transparent;
    }

    &::-webkit-scrollbar-thumb {
        background: $color-gray-300;
        border-radius: 2px;
    }
}

.event-item {
    display: flex;
    gap: $admin-space-xs;
    padding: $admin-space-xs;
    background: $color-gray-50;
    border-radius: $radius-sm;
    transition: all $transition-fast;

    &:hover {
        background: $color-gray-100;
    }

    &.conversion-event {
        background: rgba($accent-green, 0.08);
        border-left: 3px solid $accent-green;

        &:hover {
            background: rgba($accent-green, 0.12);
        }
    }
}

.event-icon {
    width: 28px;
    height: 28px;
    border-radius: $radius-sm;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;

    .material-symbols-outlined {
        font-size: 0.875rem;
        color: white;
    }

    &[data-type="pageview"] { background: $accent-blue; }
    &[data-type="conversion"] { background: $accent-green; }
    &[data-type="click"] { background: $accent-purple; }
    &[data-type="scroll"] { background: $accent-orange; }
    &[data-type="error"] { background: $accent-red; }
}

.event-content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.event-header {
    display: flex;
    align-items: center;
    gap: $admin-space-xs;
}

.event-type {
    font-size: 0.6875rem;
    font-weight: 700;
    color: $color-text-primary;
    text-transform: uppercase;
    letter-spacing: 0.02em;
}

.event-device {
    .material-symbols-outlined {
        font-size: 0.875rem;
        color: $color-text-tertiary;
    }
}

.event-page {
    font-size: 0.75rem;
    color: $color-text-secondary;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.event-details {
    display: flex;
    flex-wrap: wrap;
    gap: $admin-space-2xs;
    margin-top: 2px;
}

.event-metadata {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    padding: 2px $admin-space-2xs;
    background: rgba($color-brand-primary, 0.1);
    border-radius: $radius-sm;
    font-size: 0.625rem;
    color: $color-brand-primary;

    .material-symbols-outlined {
        font-size: 0.75rem;
    }
}

.event-session {
    display: flex;
    align-items: center;
    gap: 2px;
    font-size: 0.625rem;
    color: $color-text-tertiary;

    .material-symbols-outlined {
        font-size: 0.75rem;
    }
}

.event-time {
    font-size: 0.625rem;
    font-weight: 500;
    color: $color-text-tertiary;
    white-space: nowrap;
    flex-shrink: 0;
}

// ============================================
// PRIVACY INFO
// ============================================
.privacy-info {
    display: flex;
    align-items: center;
    gap: $admin-space-sm;
    margin-top: $admin-space-md;
    padding: $admin-space-sm $admin-space-md;
    background: rgba($accent-green, 0.08);
    border: 1px solid rgba($accent-green, 0.2);
    border-radius: $radius-md;
    animation: fadeIn 0.3s ease-out 0.3s backwards;

    .material-symbols-outlined {
        font-size: 1.25rem;
        color: $accent-green;
        flex-shrink: 0;
    }

    p {
        margin: 0;
        font-size: 0.75rem;
        color: $color-text-secondary;
        line-height: 1.4;
    }
}

// ============================================
// RESPONSIVE ADJUSTMENTS
// ============================================
@media (max-width: 640px) {
    .admin-header-actions {
        flex-direction: column;
        align-items: stretch;
        gap: $admin-space-xs;
    }

    .auto-reload-container {
        justify-content: space-between;
        flex-wrap: wrap;
    }

    .stat-cards {
        gap: $admin-space-2xs;
    }

    .stat-card {
        padding: $admin-space-xs;
        gap: $admin-space-xs;
    }

    .stat-icon {
        width: 32px;
        height: 32px;

        .material-symbols-outlined {
            font-size: 1rem;
        }
    }

    .stat-value {
        font-size: 1rem;
    }

    .simple-chart {
        height: 100px;
    }

    .privacy-info {
        flex-direction: column;
        text-align: center;
    }
}

// ============================================
// ANIMATIONS
// ============================================
@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
