/**
 * UI 组件库样式
 * 包含：Toast 通知、骨架屏、页面过渡、空状态、无障碍增强
 * 与 style.css 配合使用，不覆盖原有样式
 */

/* ==================== CSS 变量（补充） ==================== */
:root {
    --toast-success: #10b981;
    --toast-error: #ef4444;
    --toast-warning: #f59e0b;
    --toast-info: #3b82f6;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
}

/* ==================== Toast 通知系统 ==================== */
#toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-width: 380px;
    width: calc(100% - 2rem);
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 0.875rem 1rem;
    background: #fff;
    border-radius: 10px;
    box-shadow: var(--shadow-lg);
    border-left: 4px solid var(--toast-info);
    transform: translateX(120%);
    opacity: 0;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s ease;
    pointer-events: auto;
    position: relative;
    overflow: hidden;
}

.toast-show {
    transform: translateX(0);
    opacity: 1;
}

.toast-hide {
    transform: translateX(120%);
    opacity: 0;
}

.toast-icon {
    font-size: 1.25rem;
    flex-shrink: 0;
    margin-top: 1px;
}

.toast-content {
    flex: 1;
    min-width: 0;
}

.toast-message {
    font-size: 0.9rem;
    line-height: 1.5;
    color: #1f2937;
    word-break: break-word;
}

.toast-close {
    flex-shrink: 0;
    background: none;
    border: none;
    color: #9ca3af;
    cursor: pointer;
    padding: 2px;
    font-size: 0.875rem;
    line-height: 1;
    transition: color 0.2s;
    margin-top: 1px;
}

.toast-close:hover {
    color: #4b5563;
}

/* Toast 类型 */
.toast-success { border-left-color: var(--toast-success); }
.toast-success .toast-icon { color: var(--toast-success); }

.toast-error { border-left-color: var(--toast-error); }
.toast-error .toast-icon { color: var(--toast-error); }

.toast-warning { border-left-color: var(--toast-warning); }
.toast-warning .toast-icon { color: var(--toast-warning); }

.toast-info { border-left-color: var(--toast-info); }
.toast-info .toast-icon { color: var(--toast-info); }

/* Toast 进度条 */
.toast-progress {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 3px;
    background: currentColor;
    opacity: 0.3;
    animation: toastProgress linear forwards;
    width: 100%;
    transform-origin: left;
}

@keyframes toastProgress {
    from { transform: scaleX(1); }
    to { transform: scaleX(0); }
}

/* Toast 移动端适配 */
@media (max-width: 768px) {
    #toast-container {
        top: auto;
        bottom: 5rem; /* 在底部导航上方 */
        right: 0.75rem;
        left: 0.75rem;
        max-width: none;
        width: auto;
    }

    .toast {
        transform: translateY(120%);
    }

    .toast-show {
        transform: translateY(0);
    }

    .toast-hide {
        transform: translateY(120%);
    }
}

/* ==================== 骨架屏 Shimmer ==================== */
.skeleton {
    background: #e5e7eb;
    border-radius: 6px;
    position: relative;
    overflow: hidden;
}

.skeleton::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(
        90deg,
        transparent 0%,
        rgba(255, 255, 255, 0.5) 50%,
        transparent 100%
    );
    animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
}

.skeleton-card {
    height: 120px;
    margin-bottom: 1rem;
    border-radius: var(--border-radius, 10px);
}

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

.skeleton-text.short {
    width: 60%;
}

.skeleton-text.medium {
    width: 80%;
}

.skeleton-circle {
    width: 48px;
    height: 48px;
    border-radius: 50%;
}

.skeleton-chart {
    height: 200px;
    border-radius: var(--border-radius, 10px);
}

/* 骨架屏容器（用于统计页概览卡片和图表卡片） */
.skeleton-box {
    position: relative;
    overflow: hidden;
}

/* 骨架状态：隐藏实际内容，仅显示 shimmer */
.skeleton-box .stat-value,
.skeleton-box .stat-label,
.skeleton-box .stat-icon,
.skeleton-box .card-title,
.skeleton-box canvas,
.skeleton-box .cal-stat-value,
.skeleton-box .cal-stat-label,
.skeleton-box .mastery-overview,
.skeleton-box .mastery-progress-bar-outer,
.skeleton-box .mastery-progress-text,
.skeleton-box .time-charts {
    visibility: hidden;
}

.skeleton-box .skeleton-shimmer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(
        90deg,
        transparent 0%,
        rgba(255, 255, 255, 0.6) 50%,
        transparent 100%
    );
    animation: shimmer 1.5s infinite;
    z-index: 1;
    pointer-events: none;
}

/* ==================== 页面过渡动画 ==================== */
@keyframes pageFadeIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

#main-content {
    animation: pageFadeIn 0.4s ease-out;
}

/* 交错动画 */
.stagger-item {
    opacity: 0;
    animation: staggerFadeIn 0.4s ease-out forwards;
}

@keyframes staggerFadeIn {
    from {
        opacity: 0;
        transform: translateY(12px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* 为每个 stagger-item 设置递增延迟 */
.stagger-item:nth-child(1) { animation-delay: 0.05s; }
.stagger-item:nth-child(2) { animation-delay: 0.1s; }
.stagger-item:nth-child(3) { animation-delay: 0.15s; }
.stagger-item:nth-child(4) { animation-delay: 0.2s; }
.stagger-item:nth-child(5) { animation-delay: 0.25s; }
.stagger-item:nth-child(6) { animation-delay: 0.3s; }
.stagger-item:nth-child(7) { animation-delay: 0.35s; }
.stagger-item:nth-child(8) { animation-delay: 0.4s; }
.stagger-item:nth-child(9) { animation-delay: 0.45s; }
.stagger-item:nth-child(10) { animation-delay: 0.5s; }

/* ==================== 空状态设计 v2 ==================== */
.empty-state-v2 {
    text-align: center;
    padding: 3rem 1.5rem;
}

.empty-state-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 1.25rem;
    font-size: 2rem;
}

.empty-state-icon.icon-info {
    background: linear-gradient(135deg, #dbeafe, #bfdbfe);
    color: #3b82f6;
}

.empty-state-icon.icon-success {
    background: linear-gradient(135deg, #d1fae5, #a7f3d0);
    color: #10b981;
}

.empty-state-icon.icon-warning {
    background: linear-gradient(135deg, #fef3c7, #fde68a);
    color: #f59e0b;
}

.empty-state-icon.icon-muted {
    background: linear-gradient(135deg, #f3f4f6, #e5e7eb);
    color: #9ca3af;
}

.empty-state-title {
    font-size: 1.1rem;
    font-weight: 600;
    color: #374151;
    margin-bottom: 0.5rem;
}

.empty-state-desc {
    font-size: 0.9rem;
    color: #6b7280;
    margin-bottom: 1.25rem;
    line-height: 1.6;
}

.empty-state-action .btn {
    min-width: 120px;
}

/* ==================== Flash 消息可关闭 ==================== */
.alert-dismissible {
    position: relative;
    padding-right: 2.5rem;
}

.alert-close {
    position: absolute;
    top: 50%;
    right: 0.75rem;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: inherit;
    opacity: 0.6;
    cursor: pointer;
    padding: 4px;
    font-size: 0.875rem;
    line-height: 1;
    transition: opacity 0.2s;
}

.alert-close:hover {
    opacity: 1;
}

.alert-fade-out {
    animation: alertFadeOut 0.3s ease forwards;
}

@keyframes alertFadeOut {
    from {
        opacity: 1;
        transform: translateY(0);
        max-height: 100px;
    }
    to {
        opacity: 0;
        transform: translateY(-10px);
        max-height: 0;
        margin: 0;
        padding: 0;
    }
}

/* ==================== 无障碍增强 ==================== */

/* Skip Navigation */
.skip-nav {
    position: absolute;
    top: -100%;
    left: 1rem;
    padding: 0.5rem 1rem;
    background: var(--primary-color, #4f46e5);
    color: #fff;
    border-radius: 0 0 6px 6px;
    font-size: 0.875rem;
    font-weight: 500;
    text-decoration: none;
    z-index: 10001;
    transition: top 0.2s;
}

.skip-nav:focus {
    top: 0;
    outline: 2px solid #fff;
    outline-offset: 2px;
}

/* 屏幕阅读器专用 */
.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 增强 */
*:focus-visible {
    outline: 2px solid var(--primary-color, #4f46e5);
    outline-offset: 2px;
    border-radius: 2px;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--primary-color, #4f46e5);
    outline-offset: 2px;
}

@media (prefers-color-scheme: dark) {
    .toast {
        background: #1e293b;
        border-left-color: #6366f1;
    }
    .toast-message { color: #e2e8f0; }
    .toast-close { color: #64748b; }
    .toast-close:hover { color: #94a3b8; }
    .skeleton { background: #334155; }
    .skeleton::after {
        background: linear-gradient(90deg, transparent 0%, rgba(30, 41, 59, 0.5) 50%, transparent 100%);
    }
    .skeleton-box .skeleton-shimmer {
        background: linear-gradient(90deg, transparent 0%, rgba(30, 41, 59, 0.5) 50%, transparent 100%);
    }
    .empty-state-title { color: #f1f5f9; }
    .empty-state-desc { color: #94a3b8; }
    .empty-state-icon.icon-info { background: linear-gradient(135deg, rgba(59, 130, 246, 0.2), rgba(59, 130, 246, 0.1)); color: #60a5fa; }
    .empty-state-icon.icon-success { background: linear-gradient(135deg, rgba(16, 185, 129, 0.2), rgba(16, 185, 129, 0.1)); color: #34d399; }
    .empty-state-icon.icon-warning { background: linear-gradient(135deg, rgba(245, 158, 11, 0.2), rgba(245, 158, 11, 0.1)); color: #fbbf24; }
    .empty-state-icon.icon-muted { background: linear-gradient(135deg, rgba(100, 116, 139, 0.2), rgba(100, 116, 139, 0.1)); color: #64748b; }
    .skip-nav:focus { outline-color: #1e293b; }
}

[data-theme="dark"] .toast {
    background: #1e293b;
    border-left-color: #6366f1;
}
[data-theme="dark"] .toast-message { color: #e2e8f0; }
[data-theme="dark"] .toast-close { color: #64748b; }
[data-theme="dark"] .toast-close:hover { color: #94a3b8; }
[data-theme="dark"] .skeleton { background: #334155; }
[data-theme="dark"] .skeleton::after {
    background: linear-gradient(90deg, transparent 0%, rgba(30, 41, 59, 0.5) 50%, transparent 100%);
}
[data-theme="dark"] .skeleton-box .skeleton-shimmer {
    background: linear-gradient(90deg, transparent 0%, rgba(30, 41, 59, 0.5) 50%, transparent 100%);
}
[data-theme="dark"] .empty-state-title { color: #f1f5f9; }
[data-theme="dark"] .empty-state-desc { color: #94a3b8; }
[data-theme="dark"] .empty-state-icon.icon-info { background: linear-gradient(135deg, rgba(59, 130, 246, 0.2), rgba(59, 130, 246, 0.1)); color: #60a5fa; }
[data-theme="dark"] .empty-state-icon.icon-success { background: linear-gradient(135deg, rgba(16, 185, 129, 0.2), rgba(16, 185, 129, 0.1)); color: #34d399; }
[data-theme="dark"] .empty-state-icon.icon-warning { background: linear-gradient(135deg, rgba(245, 158, 11, 0.2), rgba(245, 158, 11, 0.1)); color: #fbbf24; }
[data-theme="dark"] .empty-state-icon.icon-muted { background: linear-gradient(135deg, rgba(100, 116, 139, 0.2), rgba(100, 116, 139, 0.1)); color: #64748b; }

/* ==================== 减少动画（无障碍） ==================== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    .toast {
        transition: none;
    }

    .stagger-item {
        opacity: 1;
        animation: none;
    }

    #main-content {
        animation: none;
    }

    .skeleton::after {
        animation: none;
    }
}
