/*
 * The browser lays out and paints only the blocks on the screen. With many
 * blocks, every layout the page needs (opening a side panel: its editor
 * measures itself several times) went through all of them: Edit took 10 s
 * on a page of 36 blocks, 2.9 s with this. A block under the pointer or
 * holding the focus is drawn whole, so nothing it opens is cut at its edge.
 * Only blocks drawn as bars: a block drawn as its form (the visual builder)
 * holds editors that measure themselves whenever they are drawn again, and
 * would keep the page busy.
 */
.cms-blocks-as-bars .fi-fo-builder-item:not(:hover, :focus-within) {
    content-visibility: auto;
    contain-intrinsic-size: auto 7rem;
}

/*
 * A block in the block builder (cms-blocks::block-preview): a dashed
 * bar with a hint, Edit and Settings (the header's pencil and gear).
 */
.cms-block-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: calc(var(--spacing) * 3);
    margin: calc(var(--spacing) * 4);
    padding: calc(var(--spacing) * 3) calc(var(--spacing) * 4);
    border: 1px dashed var(--gray-300);
    border-radius: var(--radius-lg);
    cursor: pointer;
    transition: background-color 150ms, border-color 150ms;
}

.cms-block-bar:hover {
    border-color: var(--gray-400);
    background-color: var(--gray-50);
}

.dark .cms-block-bar {
    border-color: color-mix(in oklab, var(--gray-500) 40%, transparent);
}

.dark .cms-block-bar:hover {
    border-color: var(--gray-500);
    background-color: color-mix(in oklab, var(--gray-500) 10%, transparent);
}

/* The bar's button waiting for its panel: its spinner in the place of its icon. */
.cms-block-bar-spinner {
    display: none;
    width: 1rem;
    height: 1rem;
}

.cms-block-bar-busy .cms-block-bar-spinner {
    display: inline-block;
}

.cms-block-bar-busy > svg:not(.cms-block-bar-spinner),
.cms-block-bar-busy > .fi-icon:not(.cms-block-bar-spinner) {
    display: none;
}

.cms-block-bar[aria-busy='true'] {
    cursor: progress;
}

/* Narrow (a phone, a side panel): the hint takes the row, the buttons go under it. */
.cms-block-bar-text {
    flex: 1 1 16rem;
    font-size: 0.875rem;
    line-height: 1.25rem;
    color: var(--gray-600);
}

.dark .cms-block-bar-text {
    color: var(--gray-400);
}

/* A border side's name at the start of its row (BlockStyling). */
.cms-styling-side {
    align-self: center;
    font-size: 0.875rem;
    font-weight: 500;
}

.cms-block-bar-readonly {
    cursor: default;
}

@media (max-width: 639px) {
    /* A block's bar on a phone: closer buttons, so its name ("Video 1") keeps room beside them, in a grid's cell too. */
    .cms-blocks-builder .fi-fo-builder-item-header {
        column-gap: calc(var(--spacing) * 1.5);
        padding-inline: calc(var(--spacing) * 2.5);
    }

    .cms-blocks-builder .fi-fo-builder-item-header-start-actions,
    .cms-blocks-builder .fi-fo-builder-item-header-end-actions {
        column-gap: calc(var(--spacing) * 1.5);
    }

    .cms-blocks-builder .fi-fo-builder-item-header-label {
        flex: 1 1 auto;
        min-width: 3.5rem;
    }

    .cms-block-bar {
        margin: calc(var(--spacing) * 3);
        padding: calc(var(--spacing) * 3);
    }

    /* A block's side panels on a phone: less space around the tabs, more for the editor. */
    .cms-block-panel .fi-modal-content {
        padding-inline: calc(var(--spacing) * 3);
    }

    .cms-block-panel .fi-sc-tabs.fi-contained .fi-sc-tabs-tab.fi-active {
        padding: calc(var(--spacing) * 3);
    }
}

.cms-block-bar-actions {
    display: flex;
    flex-shrink: 0;
    gap: calc(var(--spacing) * 2);
}

/* In the header, the pencil (edit) first, then the gear (style and settings), then Filament's own. */
.cms-blocks-builder .fi-fo-builder-item-header-end-actions > li {
    order: 2;
}

.cms-blocks-builder .fi-fo-builder-item-header-end-actions > li:has(.cms-block-edit) {
    order: 0;
}

.cms-blocks-builder .fi-fo-builder-item-header-end-actions > li:has(.cms-block-settings) {
    order: 1;
}

/*
 * The blocks' label row: the label never squeezed into two lines; the links
 * beside it (Edit in the visual builder, Snapshots, Copy blocks) go to the
 * next line, wrapping among themselves, when the row is too narrow (a phone).
 */
.cms-blocks-field .fi-fo-field-label-ctn {
    flex-wrap: wrap;
    row-gap: 0.25rem;
}

.cms-blocks-field .fi-fo-field-label {
    white-space: nowrap;
}

.cms-blocks-field .fi-fo-field-label-ctn > :not(.fi-fo-field-label) {
    margin-inline-start: auto;
}

.cms-blocks-field .fi-fo-field-label-ctn .fi-sc-actions,
.cms-blocks-field .fi-fo-field-label-ctn .fi-ac {
    flex-wrap: wrap;
    row-gap: 0.25rem;
}

/* Phones: the blocks' hint links as icon buttons, their label in a tooltip (ContentForm::iconOnPhones). */
@media (max-width: 39.99rem) {
    .cms-phone-icon-button.fi-link {
        justify-content: center;
        inline-size: 2.25rem;
        block-size: 2.25rem;
        padding: 0;
        border: 1px solid var(--gray-200);
        border-radius: var(--radius-lg, 0.5rem);
        background-color: var(--color-white, #fff);
        box-shadow: 0 1px 2px rgb(0 0 0 / 0.05);
    }

    .dark .cms-phone-icon-button.fi-link {
        border-color: rgb(255 255 255 / 0.1);
        background-color: rgb(255 255 255 / 0.05);
    }

    .cms-phone-icon-button.fi-link .fi-link-label {
        position: absolute;
        inline-size: 1px;
        block-size: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    .cms-blocks-field .fi-fo-field-label-ctn .fi-ac {
        gap: 0.5rem;
    }
}

/* The up and down buttons of a block's bar: on phones only, beside dragging (BlocksBuilder). */
@media (min-width: 40rem) {
    .cms-blocks-builder .cms-block-move {
        display: none;
    }
}

/* Phones: the up and down buttons instead of the grip, so the block's title keeps its room. */
@media (max-width: 39.99rem) {
    .cms-blocks-builder .cms-block-drag {
        display: none;
    }
}

/*
 * A block's side panel slides away without its form. Filament's input
 * wrapper has a rule with :has() (.fi-input-wrp:not(:has(…))) that makes the
 * browser compute the styles of everything around a panel again whenever
 * the panel moves: with a block's form of 1,000-4,000 elements (editors,
 * tables, pickers) its closing stalled 60-150 ms before it moved, then
 * jumped. A small panel (the featured image) never showed it. The form is
 * thrown away once the panel is closed anyway.
 */
.fi-modal:not(.fi-modal-open) .cms-block-panel > .fi-modal-content {
    display: none;
}

/*
 * "Insert between blocks": Filament shows a gap's button while a block beside
 * it is under the pointer, but the button sits in the space between the
 * blocks, where there is no block: moving the pointer to it hid it on the way,
 * and the buttons blinked while the pointer crossed the list. While shown, the
 * gap holds that space (its margins, 1rem above and below), so the pointer
 * keeps it open until it reaches the button.
 */
.cms-blocks-builder .fi-fo-builder-add-between-items-ctn::before {
    content: '';
    position: absolute;
    inset-inline: 0;
    top: calc(var(--spacing) * -4);
    height: calc(var(--spacing) * 8);
}

/* "Add block": a group of blocks under its heading, never split across the columns (block-picker-groups.js). */
.cms-block-picker-group {
    break-inside: avoid;
}

.cms-block-picker-group[hidden] {
    display: none;
}

.cms-block-picker-group-heading {
    padding: 0.5rem 0.5rem 0.25rem;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--gray-500);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.dark .cms-block-picker-group-heading {
    color: var(--gray-400);
}
