/* ========================================
   网盘按钮响应式布局 - 完整规则实现
   版本: 3.0.0
   ======================================== */

/* 
 * 响应式按钮布局规则：
 * 1. 1个按钮：左对齐
 * 2. 2个按钮：同一行平均分布，第一个按钮左对齐
 * 3. 3个按钮：同一行平均分布
 * 4. 4个按钮：同一行平均分布
 * 5. 5个按钮：两行排列（3+2），第二行第一个与第一行第一个左对齐
 * 6. 6个按钮：两行排列（3+3），第二行第一个与第一行第一个左对齐
 */

/* ========================================
   基础容器样式 - 统一设置
   ======================================== */

.zib-netdisk-download-shortcode,
.zib-netdisk-download-section,
div[id^="zib-netdisk-buttons-"] {
    position: relative !important;
    margin: 30px 0 !important;
    padding: 25px 20px 20px 20px !important;
    background: #ffffff !important;
    border: 1px dashed var(--netdisk-theme-color, #28a745) !important;
    border-radius: 8px !important;
    box-sizing: border-box !important;
    overflow: visible !important;
    min-height: auto !important;
    width: 100% !important;
}

/* 表格基础样式 */
.zib-netdisk-download-shortcode table,
.zib-netdisk-download-section table,
div[id^="zib-netdisk-buttons-"] table {
    width: 100% !important;
    border-collapse: separate !important;
    border-spacing: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    background: transparent !important;
}

.zib-netdisk-download-shortcode tbody,
.zib-netdisk-download-section tbody,
div[id^="zib-netdisk-buttons-"] tbody {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap !important;
    justify-content: flex-start !important;
    align-items: center !important;
    gap: 16px !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
}

.zib-netdisk-download-shortcode tr,
.zib-netdisk-download-section tr,
div[id^="zib-netdisk-buttons-"] tr {
    display: contents !important;
    margin: 0 !important;
    padding: 0 !important;
}

.zib-netdisk-download-shortcode td,
.zib-netdisk-download-section td,
div[id^="zib-netdisk-buttons-"] td {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    flex: 0 0 auto !important;
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
    padding: 0 !important;
    margin: 0 !important;
    border: none !important;
    background: transparent !important;
    text-align: center !important;
    vertical-align: middle !important;
    box-sizing: border-box !important;
}

/* 按钮基础样式 */
.zib-netdisk-btn {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 6px !important;
    padding: 12px 24px !important;
    background: #1578f8 !important;
    background-color: #1578f8 !important;
    color: white !important;
    text-decoration: none !important;
    border-radius: 6px !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    transition: all 0.3s ease !important;
    box-shadow: 0 2px 8px rgba(21, 120, 248, 0.3) !important;
    border: 1px solid #0d6efd !important;
    cursor: pointer !important;
    white-space: nowrap !important;
    min-width: 160px !important;
    width: auto !important;
    flex-shrink: 0 !important;
    vertical-align: middle !important;
    text-align: center !important;
    margin: 0 !important;
}

/* ========================================
   规则1：1个按钮 - 左对齐
   ======================================== */

/* 当只有1个按钮时 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(1):last-child),
.zib-netdisk-download-section tbody:has(td:nth-child(1):last-child),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(1):last-child) {
    justify-content: flex-start !important;
    max-width: 400px !important;
}

.zib-netdisk-download-shortcode tbody:has(td:nth-child(1):last-child) td,
.zib-netdisk-download-section tbody:has(td:nth-child(1):last-child) td,
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(1):last-child) td {
    justify-content: flex-start !important;
    margin-right: 0 !important;
}

/* ========================================
   规则2：2个按钮 - 平均分布，第一个左对齐
   ======================================== */

/* 当只有2个按钮时 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(2):last-child),
.zib-netdisk-download-section tbody:has(td:nth-child(2):last-child),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(2):last-child) {
    justify-content: space-between !important;
    max-width: 500px !important;
}

.zib-netdisk-download-shortcode tbody:has(td:nth-child(2):last-child) td:nth-child(1),
.zib-netdisk-download-section tbody:has(td:nth-child(2):last-child) td:nth-child(1),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(2):last-child) td:nth-child(1) {
    justify-content: flex-start !important;
    margin-right: auto !important;
}

.zib-netdisk-download-shortcode tbody:has(td:nth-child(2):last-child) td:nth-child(2),
.zib-netdisk-download-section tbody:has(td:nth-child(2):last-child) td:nth-child(2),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(2):last-child) td:nth-child(2) {
    justify-content: flex-end !important;
    margin-left: auto !important;
}

/* ========================================
   规则3：3个按钮 - 平均分布
   ======================================== */

/* 当只有3个按钮时 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))),
.zib-netdisk-download-section tbody:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) {
    justify-content: space-between !important;
    max-width: 600px !important;
}

.zib-netdisk-download-shortcode tbody:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) td:nth-child(1),
.zib-netdisk-download-section tbody:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) td:nth-child(1),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) td:nth-child(1) {
    justify-content: flex-start !important;
}

.zib-netdisk-download-shortcode tbody:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) td:nth-child(2),
.zib-netdisk-download-section tbody:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) td:nth-child(2),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) td:nth-child(2) {
    justify-content: center !important;
}

.zib-netdisk-download-shortcode tbody:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) td:nth-child(3),
.zib-netdisk-download-section tbody:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) td:nth-child(3),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(3):last-child):not(:has(td:nth-child(4))) td:nth-child(3) {
    justify-content: flex-end !important;
}

/* ========================================
   规则4：4个按钮 - 平均分布
   ======================================== */

/* 当只有4个按钮时 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(4):last-child):not(:has(td:nth-child(5))),
.zib-netdisk-download-section tbody:has(td:nth-child(4):last-child):not(:has(td:nth-child(5))),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(4):last-child):not(:has(td:nth-child(5))) {
    justify-content: space-between !important;
    max-width: 800px !important;
}

.zib-netdisk-download-shortcode tbody:has(td:nth-child(4):last-child):not(:has(td:nth-child(5))) td,
.zib-netdisk-download-section tbody:has(td:nth-child(4):last-child):not(:has(td:nth-child(5))) td,
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(4):last-child):not(:has(td:nth-child(5))) td {
    flex: 1 !important;
    justify-content: center !important;
}

/* ========================================
   规则5：5个按钮 - 两行排列（3+2）
   ======================================== */

/* 当只有5个按钮时 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))),
.zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 16px !important;
    justify-content: center !important;
    align-items: center !important;
    max-width: 600px !important;
    margin: 0 auto !important;
}

/* 第一行3个按钮 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(1),
.zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(2),
.zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(3),
.zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(1),
.zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(2),
.zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(3),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(1),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(2),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(3) {
    grid-row: 1 !important;
    justify-content: center !important;
}

/* 第二行2个按钮 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(4),
.zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(5),
.zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(4),
.zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(5),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(4),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(5) {
    grid-row: 2 !important;
    justify-content: center !important;
}

/* 第二行第一个按钮（第4个）左对齐 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(4),
.zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(4),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(4) {
    grid-column: 1 !important;
    justify-content: flex-start !important;
}

/* 第二行第二个按钮（第5个） */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(5),
.zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(5),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td:nth-child(5) {
    grid-column: 2 !important;
    justify-content: flex-start !important;
}

/* ========================================
   规则6：6个按钮 - 两行排列（3+3）
   ======================================== */

/* 当只有6个按钮时 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child),
.zib-netdisk-download-section tbody:has(td:nth-child(6):last-child),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child) {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 16px !important;
    justify-content: center !important;
    align-items: center !important;
    max-width: 600px !important;
    margin: 0 auto !important;
}

/* 第一行3个按钮 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child) td:nth-child(1),
.zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child) td:nth-child(2),
.zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child) td:nth-child(3),
.zib-netdisk-download-section tbody:has(td:nth-child(6):last-child) td:nth-child(1),
.zib-netdisk-download-section tbody:has(td:nth-child(6):last-child) td:nth-child(2),
.zib-netdisk-download-section tbody:has(td:nth-child(6):last-child) td:nth-child(3),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child) td:nth-child(1),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child) td:nth-child(2),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child) td:nth-child(3) {
    grid-row: 1 !important;
    justify-content: center !important;
}

/* 第二行3个按钮 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child) td:nth-child(4),
.zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child) td:nth-child(5),
.zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child) td:nth-child(6),
.zib-netdisk-download-section tbody:has(td:nth-child(6):last-child) td:nth-child(4),
.zib-netdisk-download-section tbody:has(td:nth-child(6):last-child) td:nth-child(5),
.zib-netdisk-download-section tbody:has(td:nth-child(6):last-child) td:nth-child(6),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child) td:nth-child(4),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child) td:nth-child(5),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child) td:nth-child(6) {
    grid-row: 2 !important;
    justify-content: center !important;
}

/* 第二行第一个按钮（第4个）左对齐 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child) td:nth-child(4),
.zib-netdisk-download-section tbody:has(td:nth-child(6):last-child) td:nth-child(4),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child) td:nth-child(4) {
    grid-column: 1 !important;
    justify-content: flex-start !important;
}

/* ========================================
   超过6个按钮 - 自动换行布局
   ======================================== */

/* 当超过6个按钮时 */
.zib-netdisk-download-shortcode tbody:has(td:nth-child(n+7)),
.zib-netdisk-download-section tbody:has(td:nth-child(n+7)),
div[id^="zib-netdisk-buttons-"]:has(td:nth-child(n+7)) {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)) !important;
    gap: 16px !important;
    justify-content: center !important;
    align-items: center !important;
    max-width: 1000px !important;
    margin: 0 auto !important;
}

.zib-netdisk-download-shortcode tbody:has(td:nth-child(n+7)) td,
.zib-netdisk-download-section tbody:has(td:nth-child(n+7)) td,
div[id^="zib-netdisk-buttons-"] tbody:has(td:nth-child(n+7)) td {
    justify-content: center !important;
}

/* ========================================
   响应式适配 - 移动端优化
   ======================================== */

@media (max-width: 768px) {
    /* 移动端统一单列显示 */
    .zib-netdisk-download-shortcode tbody,
    .zib-netdisk-download-section tbody,
    div[id^="zib-netdisk-buttons-"] tbody {
        flex-direction: column !important;
        align-items: center !important;
        gap: 12px !important;
        max-width: 350px !important;
    }
    
    /* 移动端td样式 */
    .zib-netdisk-download-shortcode td,
    .zib-netdisk-download-section td,
    div[id^="zib-netdisk-buttons-"] td {
        width: 100% !important;
        max-width: 300px !important;
        margin: 0 !important;
        padding: 0 !important;
        justify-content: center !important;
    }
    
    /* 移动端按钮样式 */
    .zib-netdisk-btn {
        width: 100% !important;
        min-width: 0 !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 14px 20px !important;
        font-size: 15px !important;
    }
    
    /* 移动端网格布局适配 */
    .zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))),
    .zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))),
    div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))),
    .zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child),
    .zib-netdisk-download-section tbody:has(td:nth-child(6):last-child),
    div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child),
    .zib-netdisk-download-shortcode tbody:has(td:nth-child(n+7)),
    .zib-netdisk-download-section tbody:has(td:nth-child(n+7)),
    div[id^="zib-netdisk-buttons-"] tbody:has(td:nth-child(n+7)) {
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        gap: 12px !important;
        max-width: 350px !important;
        grid-template-columns: none !important;
    }
    
    /* 移动端特定按钮数量适配 */
    .zib-netdisk-download-shortcode tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td,
    .zib-netdisk-download-section tbody:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td,
    div[id^="zib-netdisk-buttons-"]:has(td:nth-child(5):last-child):not(:has(td:nth-child(6))) td,
    .zib-netdisk-download-shortcode tbody:has(td:nth-child(6):last-child) td,
    .zib-netdisk-download-section tbody:has(td:nth-child(6):last-child) td,
    div[id^="zib-netdisk-buttons-"]:has(td:nth-child(6):last-child) td,
    .zib-netdisk-download-shortcode tbody:has(td:nth-child(n+7)) td,
    .zib-netdisk-download-section tbody:has(td:nth-child(n+7)) td,
    div[id^="zib-netdisk-buttons-"] tbody:has(td:nth-child(n+7)) td {
        grid-row: auto !important;
        grid-column: auto !important;
        justify-content: center !important;
    }
}

@media (max-width: 480px) {
    /* 小屏手机优化 */
    .zib-netdisk-download-shortcode,
    .zib-netdisk-download-section,
    div[id^="zib-netdisk-buttons-"] {
        padding: 20px 15px 15px 15px !important;
        margin: 20px 0 !important;
    }
    
    .zib-netdisk-btn {
        padding: 12px 16px !important;
        font-size: 14px !important;
        min-width: 0 !important;
    }
}

/* ========================================
   调试工具 - 可视化调试
   ======================================== */

.debug-layout .zib-netdisk-download-shortcode tbody,
.debug-layout .zib-netdisk-download-section tbody,
.debug-layout div[id^="zib-netdisk-buttons-"] tbody {
    outline: 3px solid red !important;
    outline-offset: 3px !important;
    background-color: rgba(255, 0, 0, 0.05) !important;
}

.debug-layout .zib-netdisk-download-shortcode td,
.debug-layout .zib-netdisk-download-section td,
.debug-layout div[id^="zib-netdisk-buttons-"] td {
    outline: 2px solid blue !important;
    outline-offset: 2px !important;
    background-color: rgba(0, 0, 255, 0.05) !important;
}

.debug-layout .zib-netdisk-btn {
    outline: 2px solid green !important;
    outline-offset: 1px !important;
    background-color: rgba(0, 255, 0, 0.1) !important;
}

/* ========================================
   性能优化 - 减少重排重绘
   ======================================== */

.zib-netdisk-download-shortcode tbody,
.zib-netdisk-download-section tbody,
div[id^="zib-netdisk-buttons-"] tbody {
    contain: layout style !important;
}

.zib-netdisk-btn {
    contain: layout style paint !important;
    will-change: transform !important;
}

/* ========================================
   可访问性优化
   ======================================== */

@media (prefers-reduced-motion: reduce) {
    .zib-netdisk-btn {
        transition: none !important;
    }
}

/* 高对比度模式支持 */
@media (prefers-contrast: high) {
    .zib-netdisk-btn {
        border-width: 2px !important;
        box-shadow: 0 0 0 2px currentColor !important;
    }
}