/*
 * Ритм секций конструктора (<x-builder::content>) — единый зазор между блоками.
 *
 * Включается классом .builder-rhythm на предке (тема default ставит его на <body>).
 * Темы со своим ритмом секций (nature, arena: поля секций, блоки вплотную) класс
 * не ставят — для них файл ничего не меняет. Разметку шаблонов не трогаем: темы
 * используют её классы (section.py-5, .py-3, …) в своих селекторах.
 *
 * Зазор — --builder-block-gap (по умолчанию 2rem, с 768px — 3rem): сверху и снизу каждой секции,
 * между соседними — один зазор (поля схлопываются). Блоки не должны
 * давать внешних вертикальных отступов: собственные py-5 шаблонов без фона гасятся,
 * поля по «краю» блока (первый/последний потомок до 8 уровней) обнуляются. Ряды
 * Bootstrap (.row и колонки) не трогаем: отрицательные поля гаттера компенсируют
 * поля колонок. Внутренние отступы блоков с фоном (.bg-*, слайдер, CTA, hero) остаются.
 */
.builder-rhythm {
    --builder-block-gap-current: var(--builder-block-gap-sm, 2rem);
}

@media (min-width: 768px) {
    .builder-rhythm {
        --builder-block-gap-current: var(--builder-block-gap-lg, 3rem);
    }
}

/* flow-root: поля потомков (в т. ч. отрицательный гаттер .row) не схлопываются наружу блока. */
.builder-rhythm .builder-block {
    display: flow-root;
}

/* Отступ и сверху, и снизу у каждой секции (в т. ч. первой и последней, слайдеров). Вертикальные поля
   соседних блоков схлопываются — между секциями тот же один зазор, а не двойной. */
.builder-rhythm .builder-block {
    margin-block: var(--builder-block-gap, var(--builder-block-gap-current));
}

/* Контейнер страницы уже даёт нижний отступ (<main class="mb-5">) — у последней секции его не удваиваем:
   <body> темы — flex, поэтому поля <main> и секции не схлопываются. */
.builder-rhythm :is(.mb-5, .my-5) > .builder-block:last-child {
    margin-bottom: 0;
}

/* Блок, который ничего не вывел, не должен давать двойной зазор. */
.builder-rhythm .builder-block:not(:has(*)) {
    display: none;
}

/* Собственные внешние отступы шаблонов без фона (особенности, статистика, YouTube, статьи). */
.builder-rhythm .builder-block > :is(section, div):is(.py-5, .py-3):not([class*="bg-"]) {
    padding-top: 0 !important;
    padding-bottom: 0 !important;
}

/* Обёртка фото в «особенности» (text-top / text-bottom): поле только между фото и текстом. */
.builder-rhythm .builder-block > section > .container > .py-3:first-child {
    padding-top: 0 !important;
}

.builder-rhythm .builder-block > section > .container > .py-3:last-child {
    padding-bottom: 0 !important;
}

/* Кнопка под списком статей: отступ только сверху. */
.builder-rhythm .builder-block .col-lg-12.text-center.p-5 {
    padding: 1.5rem 0 0 !important;
}

/* Сетки, где колонки разделены mb-* (статистика, галерея): шаг рядов — row-gap, без хвоста снизу. */
.builder-rhythm .builder-block .row:has(> [class*="col"] > .counter-item),
.builder-rhythm .builder-block .row:has(> [class*="col"] > img.img-fluid) {
    row-gap: 1rem;
}

.builder-rhythm .builder-block .row > [class*="col"]:has(> .counter-item),
.builder-rhythm .builder-block .row > [class*="col"]:has(> img.img-fluid) {
    margin-bottom: 0 !important;
}

/* Последний абзац карточки в колонке (feature-list) не раздувает низ блока. «Последний» — без непустых
   соседей после него: <p> из редактора внутри <p> шаблона браузер разбивает, оставляя пустые хвосты. */
.builder-rhythm .builder-block .row > * :is(p, ul, ol, h1, h2, h3, h4, h5, h6):not(:has(~ :not(:empty))) {
    margin-bottom: 0 !important;
}

.builder-rhythm .builder-block > :first-child:not(.row, .row > *),
.builder-rhythm .builder-block > :first-child > :first-child:not(.row, .row > *),
.builder-rhythm .builder-block > :first-child > :first-child > :first-child:not(.row, .row > *),
.builder-rhythm .builder-block > :first-child > :first-child > :first-child > :first-child:not(.row, .row > *),
.builder-rhythm .builder-block > :first-child > :first-child > :first-child > :first-child > :first-child:not(.row, .row > *),
.builder-rhythm .builder-block > :first-child > :first-child > :first-child > :first-child > :first-child > :first-child:not(.row, .row > *),
.builder-rhythm .builder-block > :first-child > :first-child > :first-child > :first-child > :first-child > :first-child > :first-child:not(.row, .row > *),
.builder-rhythm .builder-block > :first-child > :first-child > :first-child > :first-child > :first-child > :first-child > :first-child > :first-child:not(.row, .row > *) {
    margin-top: 0 !important;
}

.builder-rhythm .builder-block > :last-child:not(.row, .row > *),
.builder-rhythm .builder-block > :last-child > :last-child:not(.row, .row > *),
.builder-rhythm .builder-block > :last-child > :last-child > :last-child:not(.row, .row > *),
.builder-rhythm .builder-block > :last-child > :last-child > :last-child > :last-child:not(.row, .row > *),
.builder-rhythm .builder-block > :last-child > :last-child > :last-child > :last-child > :last-child:not(.row, .row > *),
.builder-rhythm .builder-block > :last-child > :last-child > :last-child > :last-child > :last-child > :last-child:not(.row, .row > *),
.builder-rhythm .builder-block > :last-child > :last-child > :last-child > :last-child > :last-child > :last-child > :last-child:not(.row, .row > *),
.builder-rhythm .builder-block > :last-child > :last-child > :last-child > :last-child > :last-child > :last-child > :last-child > :last-child:not(.row, .row > *) {
    margin-bottom: 0 !important;
}
