/*
 * Django's stock related-field controls, laid out the Unfold way.
 *
 * Unfold renders the edit/add/delete/view links beside a foreign key from its own
 * ``unfold/widgets/related_widget_wrapper.html`` — a flex row that collapses into a "⋮" menu. It
 * installs that template by wrapping the field, which only its ``ModelAdmin`` does. An admin that
 * cannot inherit from it falls back to Django's stock wrapper, and Unfold ships none of the
 * floating rules Django's own stylesheet uses to line those links up — so all four dropped onto
 * separate rows underneath the field as loose coloured icons.
 *
 * The obvious fix is to put the theme's ModelAdmin in the MRO, and that is what
 * config/admin_filer.py does for django-filer. It is not available everywhere: PageAdmin — the
 * screen editors spend the most time in — is documented at pages/admin.py as the one admin that
 * *cannot* take it, because Unfold's nested-inline support probes every formset with
 * ``hasattr(formset, "empty_form")`` and django-polymorphic's formset raises RuntimeError from
 * that property, which ``hasattr`` propagates into a 500 on the add form.
 *
 * So this sheet restores the layout for whatever is left on the stock widget. It is presentation
 * only — no Python, no template — which is what makes it safe to apply broadly.
 */

/*
 * ``:not(.flex)`` is the guard: Unfold's own wrapper is emitted as
 * ``class="flex flex-row related-widget-wrapper"``, so anything the theme already styles is left
 * alone and only the stock markup is touched. A wrapper that ever gains the class on its own is
 * already a flex row, so the exclusion degrades safely either way.
 */
.related-widget-wrapper:not(.flex) {
    align-items: center;
    display: flex;
    flex-direction: row;
    gap: 6px;
}

/* The widget itself — a select, an autocomplete, or filer's picker — takes the room; the links are
   a fixed-size group at the end. ``min-width: 0`` lets a long selected value truncate instead of
   pushing the buttons off the row. */
.related-widget-wrapper:not(.flex) > *:first-child {
    flex: 1 1 auto;
    min-width: 0;
}

/* Sized to Unfold's own controls: 38px square, 6px radius, the theme's hairline border — so the
   group reads as a row of icon buttons rather than four bare images. */
.related-widget-wrapper:not(.flex) > .related-widget-wrapper-link {
    align-items: center;
    background: #fff;
    border: 1px solid var(--color-base-200);
    border-radius: 6px;
    display: flex;
    flex: none;
    height: 38px;
    justify-content: center;
    width: 38px;
}

.related-widget-wrapper:not(.flex) > .related-widget-wrapper-link:hover {
    border-color: var(--color-base-300);
}

/*
 * Django's four icons are saturated and mutually unrelated — #b48c08 mustard, #5fa225 green,
 * #dd4646 red, #2b70bf blue — and none of them is in this palette; four hues in a row beside one
 * field is most of why they read as belonging to another admin. Muted to grey at rest, where they
 * are secondary controls sitting next to the value that matters, and returned to full colour on
 * hover and keyboard focus, so the destructive one is unmistakable at the moment it can be
 * clicked. Deferring the semantics rather than repainting them.
 */
.related-widget-wrapper:not(.flex) > .related-widget-wrapper-link img {
    filter: grayscale(1);
    height: 16px;
    opacity: 0.55;
    transition: filter 120ms ease, opacity 120ms ease;
    width: 16px;
}

.related-widget-wrapper:not(.flex) > .related-widget-wrapper-link:hover img,
.related-widget-wrapper:not(.flex) > .related-widget-wrapper-link:focus-visible img {
    filter: none;
    opacity: 1;
}
