/* Manifest Edit */

@layer base, components, utilities;

/* Rules are :where()-wrapped and keyed off data-edit-* attributes, so authors
   override without specificity battles. Visuals are on for every area;
   x-edit.bare (data-edit-area="bare") keeps the behaviour and draws none of them. */

@layer utilities {

    /* ---- Pieces ---- */
    :where([data-edit-piece]) {
        cursor: grab;
        touch-action: none;
        transition: opacity 0.12s ease, outline-color 0.12s ease;
    }

    :where([data-edit-piece]):where(:hover):where(:not([data-edit-area="bare"], [data-edit-area="bare"] *, [data-edit-dragging], [data-edit-ghost], :has([data-edit-dragging]) *)) {
        outline: 1px solid color-mix(in oklch, var(--edit-accent, var(--color-brand-content)) 45%, transparent);
        outline-offset: 3px;
    }

    :where([data-edit-piece]):focus-visible:where(:not([data-edit-area="bare"], [data-edit-area="bare"] *)) {
        outline: 2px solid var(--edit-accent, var(--color-brand-content));
        outline-offset: 3px;
    }

    /* A piece with its own grab handle drags only from it */
    :where([data-edit-piece][data-edit-grabbable]) {
        cursor: auto;
    }

    :where([x-edit\.grab]) {
        cursor: grab;
        touch-action: none;
        user-select: none;
    }

    /* Lifted, following the pointer */
    :where([data-edit-dragging=""]) {
        z-index: 1000;
        cursor: grabbing;
    }

    :where([data-edit-dragging=""]):where(:not([data-edit-area="bare"], [data-edit-area="bare"] *)) {
        box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);
    }

    /* Grabbed from the keyboard */
    :where([data-edit-dragging="keyboard"]):where(:not([data-edit-area="bare"], [data-edit-area="bare"] *)) {
        outline: 2px dashed var(--edit-accent, var(--color-brand-content));
        outline-offset: 3px;
    }

    /* The stand-in holding the slot the drop would land in */
    :where([data-edit-ghost]) {
        opacity: var(--edit-ghost-opacity, 0.4);
        pointer-events: none;
    }

    :where([data-edit-area="bare"] [data-edit-ghost]) {
        opacity: 0;
    }

    /* The container the drop would land in */
    :where(:has(> [data-edit-ghost])):where(:not([data-edit-area="bare"], [data-edit-area="bare"] *)) {
        outline: 1px dashed color-mix(in oklch, var(--edit-accent, var(--color-brand-content)) 55%, transparent);
        outline-offset: 4px;
    }

    /* Other areas a drag can land in; an empty one still has a surface */
    :where([data-edit-drop-target]):where(:not([data-edit-area="bare"])) {
        min-height: 2.5rem;
        outline: 1px dashed color-mix(in oklch, var(--edit-accent, var(--color-brand-content)) 55%, transparent);
        outline-offset: 4px;
    }

    /* While dragging: empty containers can be dropped into, nothing else reacts */
    :where([data-edit-area]:not([data-edit-area="bare"]):has([data-edit-dragging=""])) :where(:empty:not(img, input, br, hr, svg, video, canvas, iframe, textarea, [data-edit-ghost], [data-edit-handle], [data-edit-area="bare"], [data-edit-area="bare"] *)) {
        min-height: 2.5rem;
    }

    :where([data-edit-area]:has([data-edit-dragging=""])) {
        cursor: grabbing;
        user-select: none;

        & :where([data-edit-piece]:not([data-edit-dragging])) {
            cursor: grabbing;
        }
    }

    /* ---- Inline text ---- */
    :where([data-edit-area]:not([data-edit-area="bare"])) :where([contenteditable="true"], [data-edit-text]):where(:not([data-edit-area="bare"] *)),
    :where([data-edit-area]:is([contenteditable="true"], [data-edit-text]):not([data-edit-area="bare"])) {
        cursor: text;
        outline: 1px solid transparent;
        outline-offset: 2px;
        border-radius: 3px;
        transition: outline-color 0.12s ease;

        &:hover {
            outline-color: color-mix(in oklch, var(--edit-accent, var(--color-brand-content)) 45%, transparent);
        }

        &:focus {
            outline-color: var(--edit-accent, var(--color-brand-content));
        }
    }

    /* ---- Size handles ---- */
    :where(:has(> [data-edit-handle])) {
        position: relative;
    }

    :where([data-edit-handle])::before {
        opacity: 0;
        transition: opacity 0.12s ease, background-color 0.15s ease;
    }

    :where(:has(> [data-edit-handle])):is(:hover, :focus-within):where(:not([data-edit-area="bare"], [data-edit-area="bare"] *)) > :where([data-edit-handle])::before {
        opacity: 1;
    }

    :where([data-edit-handle]) {
        position: absolute;
        z-index: 100;
        background: transparent;
        touch-action: none;
        --h: var(--edit-size-handle, 1rem);

        &::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 1px;
            height: 1px;
            background: transparent;
            transition: background-color 0.15s ease;
            pointer-events: none;
        }

        &:hover::before,
        &:active::before {
            background-color: var(--edit-accent, var(--color-brand-content));
        }
    }

    /* While resizing, the edge stays drawn; at a snap point it thickens */
    :where([data-edit-handle][data-edit-resizing]):where(:not([data-edit-area="bare"] *))::before {
        opacity: 1;
        background-color: var(--edit-accent, var(--color-brand-content));
    }

    :where([data-edit-handle="top"][data-edit-snapped], [data-edit-handle="bottom"][data-edit-snapped]):where(:not([data-edit-area="bare"] *))::before {
        height: 3px;
    }

    :where([data-edit-handle="left"][data-edit-snapped], [data-edit-handle="right"][data-edit-snapped], [data-edit-handle="start"][data-edit-snapped], [data-edit-handle="end"][data-edit-snapped]):where(:not([data-edit-area="bare"] *))::before {
        width: 3px;
    }

    :where([data-edit-handle*="-"][data-edit-snapped]):where(:not([data-edit-area="bare"] *))::before {
        width: 0.5rem;
        height: 0.5rem;
        border-radius: 50%;
    }

    :where([data-edit-handle]):focus-visible:where(:not([data-edit-area="bare"], [data-edit-area="bare"] *)) {
        outline: 2px solid var(--edit-accent, var(--color-brand-content));
    }

    :where([data-edit-handle="top"], [data-edit-handle="bottom"]) {
        left: 0;
        width: 100%;
        height: var(--h);
        cursor: ns-resize;

        &::before {
            width: 100%;
        }
    }

    :where([data-edit-handle="left"], [data-edit-handle="right"], [data-edit-handle="start"], [data-edit-handle="end"]) {
        top: 0;
        width: var(--h);
        height: 100%;
        cursor: ew-resize;

        &::before {
            height: 100%;
        }
    }

    :where([data-edit-handle*="-"]) {
        width: var(--h);
        height: var(--h);
    }

    :where([data-edit-handle^="top"]) {
        top: calc(var(--h) * -0.5);
    }

    :where([data-edit-handle^="bottom"]) {
        bottom: calc(var(--h) * -0.5);
    }

    :where([data-edit-handle$="left"]) {
        left: calc(var(--h) * -0.5);
    }

    :where([data-edit-handle$="right"]) {
        right: calc(var(--h) * -0.5);
    }

    :where([data-edit-handle$="start"]) {
        inset-inline-start: calc(var(--h) * -0.5);
    }

    :where([data-edit-handle$="end"]) {
        inset-inline-end: calc(var(--h) * -0.5);
    }

    :where([data-edit-handle="top-left"], [data-edit-handle="bottom-right"], [data-edit-handle="top-start"], [data-edit-handle="bottom-end"]) {
        cursor: nwse-resize;
    }

    :where([data-edit-handle="top-right"], [data-edit-handle="bottom-left"], [data-edit-handle="top-end"], [data-edit-handle="bottom-start"]) {
        cursor: nesw-resize;
    }

    [dir=rtl] :where([data-edit-handle="top-start"], [data-edit-handle="bottom-end"]) {
        cursor: nesw-resize;
    }

    [dir=rtl] :where([data-edit-handle="top-end"], [data-edit-handle="bottom-start"]) {
        cursor: nwse-resize;
    }

    /* Bare: handles keep their hit areas and cursors, draw nothing */
    :where([data-edit-area="bare"] [data-edit-handle])::before {
        content: none;
    }

    /* Pointer capture layer during a resize */
    :where([data-edit-overlay]) {
        position: fixed;
        inset: 0;
        z-index: 9999;
        background: transparent;
    }

    /* Screen-reader announcements */
    :where([data-edit-live]) {
        position: absolute;
        width: 1px;
        height: 1px;
        margin: -1px;
        padding: 0;
        border: 0;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    @media (prefers-reduced-motion: reduce) {

        :where([data-edit-piece]),
        :where([data-edit-handle])::before {
            transition: none;
        }
    }
}
