/**
 * 堆排序可视化样式
 * @author changyadai
 * 
 * 算法特有样式 - 堆树视图、节点和动画
 */

/* ==================== 树形视图 ==================== */
.heap-tree-container {
    position: relative;
    width: 100%;
    height: 320px;
    margin-bottom: 30px;
    background: rgba(255, 255, 255, 0.02);
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    overflow: hidden;
}

.tree-lines {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.tree-line {
    stroke: rgba(168, 85, 247, 0.4);
    stroke-width: 2;
    transition: stroke 0.3s ease, stroke-width 0.3s ease;
}

.tree-line.active {
    stroke: #a855f7;
    stroke-width: 3;
    filter: drop-shadow(0 0 6px rgba(168, 85, 247, 0.8));
}

.tree-nodes {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

/* 树节点样式 */
.tree-node {
    position: absolute;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: linear-gradient(135deg, #a855f7 0%, #6366f1 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 16px;
    font-weight: bold;
    color: #fff;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    transform: translate(-50%, -50%);
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(168, 85, 247, 0.3);
    cursor: default;
}

.tree-node::before {
    content: '';
    position: absolute;
    top: 3px;
    left: 10%;
    width: 80%;
    height: 40%;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.3) 0%, transparent 100%);
    border-radius: 50% 50% 0 0;
}

/* 节点索引标签 */
.tree-node::after {
    content: attr(data-index);
    position: absolute;
    bottom: -20px;
    font-size: 11px;
    color: rgba(255, 255, 255, 0.5);
    font-weight: normal;
}

/* 比较中的节点 */
.tree-node.comparing {
    transform: translate(-50%, -50%) scale(1.2);
    box-shadow: 
        0 0 20px rgba(0, 217, 255, 0.8),
        0 0 40px rgba(0, 217, 255, 0.4);
    background: linear-gradient(135deg, #00d9ff 0%, #00ff88 100%);
    z-index: 10;
}

/* 交换中的节点 */
.tree-node.swapping {
    box-shadow: 
        0 0 25px rgba(255, 100, 200, 0.9),
        0 0 50px rgba(255, 100, 200, 0.5);
    background: linear-gradient(135deg, #ff6b9d 0%, #ff00aa 100%);
}

/* 已排序的节点（已移出堆） */
.tree-node.sorted {
    background: linear-gradient(135deg, #00ff88 0%, #00cc6a 100%);
    box-shadow: 0 0 15px rgba(0, 255, 136, 0.5);
    opacity: 0.6;
}

/* 根节点特殊样式 */
.tree-node.root {
    width: 56px;
    height: 56px;
    font-size: 18px;
}

/* 节点移动动画 */
.tree-node.moving {
    transition: left 0.4s ease-in-out, top 0.4s ease-in-out;
}

/* ==================== 数组视图 ==================== */
.array-section {
    margin-top: 20px;
}

.section-label {
    font-size: 0.9rem;
    color: rgba(255, 255, 255, 0.5);
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 2px;
}

.array-container {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 8px;
    flex-wrap: wrap;
    padding-top: 22px; /* 为索引标签留出空间 */
    min-height: 67px; /* 45px (item height) + 22px (padding-top) */
    max-height: 85px; /* 限制最大高度 */
    overflow: visible;
}

/* 数组元素样式 */
.array-item {
    width: 45px;
    height: 45px;
    border-radius: 10px;
    background: linear-gradient(135deg, #a855f7 0%, #6366f1 100%);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    font-weight: bold;
    color: #fff;
    transition: all 0.3s ease;
    position: relative;
}

.array-item .index {
    position: absolute;
    top: -18px;
    font-size: 10px;
    color: rgba(255, 255, 255, 0.4);
    font-weight: normal;
    white-space: nowrap;
}

/* 比较中的数组元素 */
.array-item.comparing {
    transform: scale(1.15);
    box-shadow: 
        0 0 15px rgba(0, 217, 255, 0.8),
        0 0 30px rgba(0, 217, 255, 0.4);
    background: linear-gradient(135deg, #00d9ff 0%, #00ff88 100%);
    z-index: 10;
}

/* 交换中的数组元素 */
.array-item.swapping {
    box-shadow: 
        0 0 20px rgba(255, 100, 200, 0.9),
        0 0 40px rgba(255, 100, 200, 0.5);
    background: linear-gradient(135deg, #ff6b9d 0%, #ff00aa 100%);
}

/* 已排序的数组元素 */
.array-item.sorted {
    background: linear-gradient(135deg, #00ff88 0%, #00cc6a 100%);
    box-shadow: 0 0 10px rgba(0, 255, 136, 0.4);
}

/* 堆范围指示器 */
.array-item.out-of-heap {
    opacity: 0.4;
}

/* ==================== 堆调整动画 ==================== */
@keyframes nodeSwap {
    0% {
        transform: translate(-50%, -50%) scale(1);
    }
    50% {
        transform: translate(-50%, -50%) scale(1.3);
    }
    100% {
        transform: translate(-50%, -50%) scale(1);
    }
}

.tree-node.swap-animation {
    animation: nodeSwap 0.4s ease-in-out;
}

/* 下沉效果 */
@keyframes sinkDown {
    0% { transform: translate(-50%, -50%); }
    25% { transform: translate(-50%, calc(-50% + 10px)); }
    50% { transform: translate(-50%, -50%); }
    75% { transform: translate(-50%, calc(-50% + 5px)); }
    100% { transform: translate(-50%, -50%); }
}

/* 上浮效果 */
@keyframes floatUp {
    0% { transform: translate(-50%, -50%); }
    25% { transform: translate(-50%, calc(-50% - 10px)); }
    50% { transform: translate(-50%, -50%); }
    75% { transform: translate(-50%, calc(-50% - 5px)); }
    100% { transform: translate(-50%, -50%); }
}

/* ==================== 粒子效果 ==================== */
.heap-particle {
    position: absolute;
    width: 8px;
    height: 8px;
    background: radial-gradient(circle, rgba(168, 85, 247, 0.8) 0%, transparent 70%);
    border-radius: 50%;
    pointer-events: none;
    animation: particleFade 0.8s ease-out forwards;
}

@keyframes particleFade {
    0% {
        opacity: 1;
        transform: scale(1);
    }
    100% {
        opacity: 0;
        transform: scale(2) translateY(-30px);
    }
}

/* ==================== 完成动画 ==================== */
.tree-node.celebrate {
    animation: celebrateNode 0.5s ease-in-out;
}

.array-item.celebrate {
    animation: celebrateItem 0.5s ease-in-out;
}

@keyframes celebrateNode {
    0%, 100% { transform: translate(-50%, -50%) scale(1); }
    50% { transform: translate(-50%, -50%) scale(1.2); }
}

@keyframes celebrateItem {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.15) translateY(-5px); }
}

/* ==================== 响应式 ==================== */
@media (max-width: 768px) {
    .heap-tree-container {
        height: 280px;
    }
    
    .tree-node {
        width: 40px;
        height: 40px;
        font-size: 14px;
    }
    
    .tree-node.root {
        width: 46px;
        height: 46px;
    }
    
    .array-item {
        width: 38px;
        height: 38px;
        font-size: 12px;
    }
}
