/* ============================================
   СЕТКА КАРТОЧЕК С МНОЖЕСТВОМ ССЫЛОК
   ID: multi-link-grid
   ============================================ */

#multi-link-grid {
    display: block !important;
    width: 100% !important;
    margin: 20px 0 !important;
    border: 1px solid #c2d1e5 !important;
    border-radius: 12px !important;
    overflow: hidden !important;
    background: #fff !important;
}

#multi-link-grid thead th {
    background: #0070c0 !important;
    color: #fff !important;
    padding: 12px 20px !important;
    font-size: 20px !important;
    text-align: left !important;
    font-weight: bold !important;
    display: block !important;
}

#multi-link-grid tbody {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 0 !important;
}

#multi-link-grid tr {
    display: contents !important;
}

#multi-link-grid td {
    display: flex !important;
    flex-direction: column !important;
    padding: 20px !important;
    border-right: 1px solid #eee !important;
    align-items: center !important;
}

#multi-link-grid td:last-child {
    border-right: none !important;
}

#multi-link-grid .img-box {
    width: 100% !important;
    height: 250px !important;
    background: #f8f8f8 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    margin-bottom: 20px !important;
}

#multi-link-grid .img-box img {
    max-height: 90% !important;
    max-width: 90% !important;
    object-fit: contain !important;
}

#multi-link-grid .links-group {
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    margin-bottom: 15px !important;
}

#multi-link-grid .links-group a {
    display: block !important;
    text-decoration: none !important;
    color: #555 !important;
    font-weight: 700 !important;
    font-size: 15px !important;
    text-transform: uppercase !important;
    text-align: center !important;
    padding: 5px 0 !important;
    transition: color 0.2s !important;
}

#multi-link-grid .links-group a:hover {
    color: #0070c0 !important;
}

#multi-link-grid .type-title {
    font-size: 18px !important;
    font-weight: 900 !important;
    color: #000 !important;
    margin: 10px 0 !important;
    text-align: center !important;
}

#multi-link-grid .description {
    font-size: 13px !important;
    color: #333 !important;
    line-height: 1.4 !important;
    text-align: left !important;
    margin-top: auto !important;
}

@media (max-width: 768px) {
    #multi-link-grid tbody {
        grid-template-columns: 1fr !important;
    }
    #multi-link-grid td {
        border-right: none !important;
        border-bottom: 1px solid #eee !important;
    }
}

/* ============================================
   УНИВЕРСАЛЬНАЯ СЕТКА КАРТОЧЕК
   ID: multi-link-grid-flex
   ============================================ */

#multi-link-grid-flex {
    display: block !important;
    width: 100% !important;
    margin: 20px 0 !important;
    border: 1px solid #c2d1e5 !important;
    border-radius: 12px !important;
    overflow: hidden !important;
    background: #fff !important;
}

#multi-link-grid-flex thead th {
    background: #0070c0 !important;
    color: #fff !important;
    padding: 12px 20px !important;
    font-size: 20px !important;
    text-align: left !important;
    font-weight: bold !important;
    display: block !important;
}

/* Базовая авто-раскладка (сработает, если количество карточек не совпадает с правилами ниже) */
#multi-link-grid-flex tbody {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)) !important;
    gap: 0 !important;
}

#multi-link-grid-flex tr {
    display: contents !important;
}

#multi-link-grid-flex td {
    display: flex !important;
    flex-direction: column !important;
    padding: 20px !important;
    align-items: center !important;
    border-right: 1px solid #eee !important;
    border-bottom: 1px solid #eee !important;
    box-sizing: border-box !important;
    min-width: 0 !important;
}

#multi-link-grid-flex td:first-child {
    border-left: none !important;
}
#multi-link-grid-flex td:last-child {
    border-right: none !important;
}

#multi-link-grid-flex .img-box {
    width: 100% !important;
    height: 250px !important;
    background: #f8f8f8 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    margin-bottom: 20px !important;
    border-radius: 8px !important;
    overflow: hidden !important;
}

#multi-link-grid-flex .img-box img {
    max-height: 90% !important;
    max-width: 90% !important;
    object-fit: contain !important;
    display: block !important;
}

#multi-link-grid-flex .links-group {
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    margin-bottom: 15px !important;
}

#multi-link-grid-flex .links-group a {
    display: block !important;
    text-decoration: none !important;
    color: #555 !important;
    font-weight: 700 !important;
    font-size: 15px !important;
    text-transform: uppercase !important;
    text-align: center !important;
    padding: 5px 0 !important;
    transition: color 0.2s !important;
    word-wrap: break-word !important;
    overflow-wrap: anywhere !important;
}

#multi-link-grid-flex .links-group a:hover {
    color: #0070c0 !important;
}

#multi-link-grid-flex .type-title {
    font-size: 18px !important;
    font-weight: 900 !important;
    color: #000 !important;
    margin: 10px 0 !important;
    text-align: center !important;
}

#multi-link-grid-flex .description {
    font-size: 13px !important;
    color: #333 !important;
    line-height: 1.4 !important;
    text-align: left !important;
    margin-top: auto !important;
}

/* ============================================
   ЛОГИКА БЕЗ JS: КРАТНО 3 И КРАТНО 2
   ============================================ */

/* 
   Селектор :has() проверяет наличие конкретного ребенка.
   td:nth-child(3):last-child означает: "третий ребенок, который является последним".
   То есть ровно 3 карточки.
*/

/* --- ЕСЛИ КРАТНО 3 (Ровно 3, 6, 9 карточек) --- */
#multi-link-grid-flex:has(td:nth-child(3):last-child) tbody,
#multi-link-grid-flex:has(td:nth-child(6):last-child) tbody,
#multi-link-grid-flex:has(td:nth-child(9):last-child) tbody,
#multi-link-grid-flex:has(td:nth-child(12):last-child) tbody {
    grid-template-columns: repeat(3, 1fr) !important;
}

/* --- ЕСЛИ КРАТНО 2 (Ровно 2, 4, 8 карточек) --- */
#multi-link-grid-flex:has(td:nth-child(2):last-child) tbody,
#multi-link-grid-flex:has(td:nth-child(4):last-child) tbody,
#multi-link-grid-flex:has(td:nth-child(8):last-child) tbody,
#multi-link-grid-flex:has(td:nth-child(10):last-child) tbody {
    grid-template-columns: repeat(2, 1fr) !important;
}

/* ============================================
   ИСПРАВЛЕНИЕ: Одна ссылка — убираем пустоту и выравниваем
   ============================================ */

#multi-link-grid-flex td:has(.links-group a:only-child) .description {
    margin-top: 10px !important;
    text-align: center !important;
}

/* ============================================
   АДАПТИВ (перекрывает жёсткие правила на маленьких экранах)
   ============================================ */

/* Планшет: максимум 2 колонки, даже если сработало правило для 3 */
@media (max-width: 992px) {
    #multi-link-grid-flex tbody,
    #multi-link-grid-flex:has(td:nth-child(3):last-child) tbody,
    #multi-link-grid-flex:has(td:nth-child(6):last-child) tbody,
    #multi-link-grid-flex:has(td:nth-child(9):last-child) tbody {
        grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)) !important;
    }
}

/* Мобильный: строго 1 колонка */
@media (max-width: 600px) {
    #multi-link-grid-flex tbody,
    #multi-link-grid-flex:has(td:nth-child(3):last-child) tbody,
    #multi-link-grid-flex:has(td:nth-child(6):last-child) tbody,
    #multi-link-grid-flex:has(td:nth-child(2):last-child) tbody,
    #multi-link-grid-flex:has(td:nth-child(4):last-child) tbody {
        grid-template-columns: 1fr !important;
    }
    #multi-link-grid-flex td {
        border-right: none !important;
    }
    #multi-link-grid-flex .img-box {
        height: 220px !important;
    }
}