/*
 * Styles for the media library: the upload fields, their gallery and "Choose from library" picker, the media library
 * page and its details slide-over. Buttons, badges, inputs, dropdowns, modals, tables and pagination are Filament's own
 * components; this file only lays them out and styles what Filament has no component for (tiles, the drop zone and a
 * list that looks like a Filament table). Filament's CSS variables are used so custom themes match.
 *
 * Sections:
 * 1. Field: variables, helpers, empty state, toolbar, uploads, thumbnails, alt text and the single file layout
 * 2. Gallery: the grid of tiles and the list of a field with several files
 * 3. Picker: the "Choose from library" dialog
 * 4. Library page: cards and list rows of the media library page
 * 5. Details: the slide-over with a media's details
 */

/* ==========================================================================================================================
 * 1. Field
 * ======================================================================================================================= */

.fml {
    --fml-border: var(--gray-200);
    --fml-surface: #fff;
    --fml-muted-surface: var(--gray-50);
    --fml-text: var(--gray-950);
    --fml-muted: var(--gray-500);
    --fml-ring: color-mix(in oklab, var(--gray-950) 10%, transparent);
    --fml-radius: var(--radius-lg, 0.5rem);
    --fml-tile-size: 11rem;

    position: relative;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    color: var(--fml-text);
    font-size: 0.875rem;
    line-height: 1.25rem;
}

.dark .fml {
    --fml-border: color-mix(in oklab, #fff 10%, transparent);
    --fml-surface: var(--gray-900);
    --fml-muted-surface: color-mix(in oklab, #fff 5%, transparent);
    --fml-text: #fff;
    --fml-muted: var(--gray-400);
    --fml-ring: color-mix(in oklab, #fff 10%, transparent);
}

/* Helpers */

.fml [x-cloak] {
    display: none !important;
}

.fml-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.fml-icon {
    width: 1rem;
    height: 1rem;
    flex: none;
}

.fml-muted {
    display: block;
    color: var(--fml-muted);
    font-size: 0.875rem;
    line-height: 1.25rem;
}

.fml-error-text {
    color: var(--danger-600);
    font-size: 0.875rem;
    line-height: 1.25rem;
}

.dark .fml-error-text {
    color: var(--danger-400);
}

.fml-help {
    margin: 0;
    color: var(--fml-muted);
    font-size: 0.875rem;
}

.fml-input-narrow {
    width: 6rem;
}

/* Nothing in the field is selectable as text while dragging: labels, badges and numbers are interface, not content. */

.fml-tile,
.fml-table-ctn tr,
.fml-toolbar,
.fml-dropzone,
.fml-drop-overlay,
.fml-hint,
.fml-limits {
    -webkit-user-select: none;
    user-select: none;
    font-size: 0.875rem;
    line-height: 1.25rem;
}

.fml input,
.fml textarea {
    -webkit-user-select: text;
    user-select: text;
}

body.sorting,
body.sorting * {
    -webkit-user-select: none !important;
    user-select: none !important;
}

body.sorting {
    cursor: grabbing;
}

/*
 * Read-only while a Livewire request of the form is on its way (see watchLivewireRequests() in the script). The field
 * itself keeps receiving dragged files, so a drop is never handed to the browser; it is added once the request is done.
 */

.fml.fml-is-busy {
    cursor: progress;
}

.fml.fml-is-busy > :not(.fml-drop-overlay) {
    pointer-events: none;
}

/* Empty state */

.fml-dropzone {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    min-height: 11rem;
    padding: 2rem 1rem;
    border: 2px dashed var(--fml-border);
    border-radius: var(--radius-xl, 0.75rem);
    background: var(--fml-muted-surface);
    text-align: center;
}

.fml-dropzone.fml-is-disabled {
    min-height: 5rem;
}

.fml-dropzone-icon-ctn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 3rem;
    height: 3rem;
    border-radius: 999px;
    background: var(--gray-100);
}

.dark .fml-dropzone-icon-ctn {
    background: color-mix(in oklab, var(--gray-500) 20%, transparent);
}

.fml-dropzone-icon {
    width: 1.5rem;
    height: 1.5rem;
    color: var(--fml-muted);
}

.fml-dropzone-text {
    color: var(--fml-muted);
}

/* Upload and "Choose from library" in the empty state of a library field. */
.fml-dropzone-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.75rem;
}

.fml-limits {
    margin: 0;
    color: var(--fml-muted);
    font-size: 0.75rem;
}

/* Dragging files over the field */

.fml-drop-overlay {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    border: 2px dashed var(--primary-500);
    border-radius: var(--radius-xl, 0.75rem);
    background: color-mix(in oklab, var(--primary-500) 12%, var(--fml-surface));
    color: var(--primary-700);
    font-size: 1rem;
    font-weight: 600;
    pointer-events: none;
}

.fml.fml-is-dragging > .fml-drop-overlay {
    display: flex;
}

.dark .fml-drop-overlay {
    color: var(--primary-300);
}

.fml-drop-overlay-icon {
    width: 2rem;
    height: 2rem;
}

/* Toolbar */

.fml-toolbar,
.fml-toolbar-start,
.fml-toolbar-end,
.fml-move,
.fml-more,
.fml-name-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
}

.fml-selection {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.75rem;
}

.fml-card-actions,
.fml-actions-group {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.125rem;
}

.fml-toolbar {
    justify-content: space-between;
    min-height: 2.25rem;
}

.fml-toolbar-end {
    margin-inline-start: auto;
}

.fml-count {
    color: var(--fml-muted);
}

/* Grid / list switch: a Filament button group; the current view is shown as pressed */
/* Icon-only buttons have no text line, so the padding is raised to match the height of the labelled buttons beside them */
.fml-view-toggle-btn.fi-btn.fi-size-sm {
    padding: 0.5rem 0.625rem;
}

.fml-view-toggle-btn.fi-btn[aria-pressed='true'] {
    background: var(--gray-100);
}

.fml-view-toggle-btn.fi-btn[aria-pressed='true'] .fi-icon {
    color: var(--primary-600);
}

.dark .fml-view-toggle-btn.fi-btn[aria-pressed='true'] {
    background: color-mix(in oklab, #fff 10%, transparent);
}

.dark .fml-view-toggle-btn.fi-btn[aria-pressed='true'] .fi-icon {
    color: var(--primary-400);
}

/* Progress and notices */

.fml-progress {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 500;
}

.fml-progress-bar {
    flex: 1;
    height: 0.375rem;
    border-radius: 999px;
    background: var(--gray-200);
    overflow: hidden;
}

.dark .fml-progress-bar {
    background: color-mix(in oklab, #fff 10%, transparent);
}

.fml-progress-bar > div {
    height: 100%;
    background: var(--primary-600);
    transition: width 0.2s;
}

.fml-progress-bar-light {
    flex: none;
    width: 70%;
    background: color-mix(in oklab, #fff 35%, transparent);
}

.fml-progress-bar-light > div {
    background: #fff;
}

.fml-hint {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.375rem;
    margin: 0;
    color: var(--fml-muted);
}

/* Filament callouts, a little tighter inside a field. */
.fml-callout.fi-callout {
    padding: 0.625rem 0.75rem;
}

/* Detailed upload list (optional) */

.fml-queue {
    border-radius: var(--radius-xl, 0.75rem);
    background: var(--fml-surface);
    box-shadow: 0 0 0 1px var(--fml-ring);
}

.fml-queue-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.625rem 0.75rem;
    border-bottom: 1px solid var(--fml-border);
}

.fml-queue-list {
    max-height: 12rem;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    list-style: none;
}

.fml-queue-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(6rem, auto) 8rem;
    align-items: center;
    gap: 0.75rem;
    padding: 0.375rem 0.75rem;
}

.fml-queue-item + .fml-queue-item {
    border-top: 1px solid var(--fml-border);
}

.fml-queue-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.fml-queue-size,
.fml-queue-status {
    color: var(--fml-muted);
    font-size: 0.75rem;
}

.fml-queue-item[data-status='failed'] .fml-queue-status {
    color: var(--danger-600);
}

.fml-queue-item[data-status='done'] .fml-progress-bar > div {
    background: var(--success-600);
}

@media (max-width: 40rem) {
    .fml-toolbar-end {
        flex: 1 1 100%;
    }

    .fml-add-btn {
        flex: 1;
    }

    .fml-queue-item {
        grid-template-columns: minmax(0, 1fr) auto;
    }

    .fml-queue-size,
    .fml-queue-item .fml-progress-bar {
        display: none;
    }
}

/* Thumbnails, with the upload state on top */

.fml-thumb {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    aspect-ratio: 1;
    border-radius: var(--radius-lg, 0.5rem);
    background: var(--fml-muted-surface);
    box-shadow: inset 0 0 0 1px var(--fml-ring);
    overflow: hidden;
}

.fml-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    -webkit-user-drag: none;
    user-select: none;
}

.fml-thumb-small {
    width: 3.5rem;
}

.fml-thumb-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    color: var(--fml-muted);
    font-size: 0.75rem;
    font-weight: 600;
}

.fml-thumb-placeholder-icon {
    width: 1.75rem;
    height: 1.75rem;
}

.fml-tile-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.5rem;
    background: color-mix(in oklab, #000 45%, transparent);
    color: #fff;
    text-align: center;
}

.fml-tile-overlay-error {
    background: color-mix(in oklab, var(--danger-700) 85%, transparent);
}

.fml-tile-status {
    font-size: 0.75rem;
    font-weight: 600;
    line-height: 1.2;
}

.fml-thumb-small .fml-tile-overlay .fml-tile-status,
.fml-thumb-small .fml-tile-overlay .fml-retry-btn,
.fml-thumb-small .fml-progress-bar {
    display: none;
}

.fml-spinner {
    width: 1.5rem;
    height: 1.5rem;
}

/* Alt text: the link under a file, the inputs per locale and the alt text dialog */

.fml-alt-link.fi-link {
    max-width: 100%;
    justify-content: flex-start;
}

.fml-alt-link .fi-link-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.fml-alt-locales {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.fml-alt-dialog {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.fml-alt-dialog-file {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* The option to also delete removed media from the library. */
.fml-also-delete {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    margin-top: 0.5rem;
    font-size: 0.875rem;
    line-height: 1.25rem;
    text-align: start;
    cursor: pointer;
}

.fml-also-delete input {
    margin-top: 0.125rem;
}

/* Single file: a chosen file and the empty field share one row: thumbnail, text, then quiet actions. */

.fml-single,
.fml-empty-single {
    display: grid;
    grid-template-columns: 4.5rem minmax(0, 1fr) auto;
    align-items: center;
    gap: 0.875rem;
    min-height: 4.375rem;
    padding: 0.5rem 0.875rem 0.5rem 0.5rem;
    border-radius: var(--radius-xl, 0.75rem);
}

.fml-single {
    background: var(--fml-surface);
    box-shadow: 0 1px 2px color-mix(in oklab, #000 5%, transparent), 0 0 0 1px var(--fml-ring);
}

.fml-empty-single {
    border: 1px dashed color-mix(in oklab, var(--gray-950) 18%, transparent);
}

.dark .fml-empty-single {
    border-color: color-mix(in oklab, #fff 18%, transparent);
}

.fml-thumb-single,
.fml-empty-single-icon {
    width: 4.5rem;
    aspect-ratio: 4 / 3;
}

.fml-empty-single-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-lg, 0.5rem);
    background: var(--fml-muted-surface);
    color: var(--gray-400);
}

.fml-empty-single-icon .fml-icon {
    width: 1.25rem;
    height: 1.25rem;
}

.fml-empty-single-text {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
    min-width: 0;
    font-size: 0.875rem;
    line-height: 1.25rem;
}

.fml-empty-single-text p {
    margin: 0;
}

.fml-empty-single-hint {
    color: var(--fml-muted);
}

p.fml-empty-single-hint {
    font-size: 0.875rem;
    line-height: 1.25rem;
}

.fml-single-body {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
    min-width: 0;
}

/* Size, other uses and missing alt text: one muted line, separated by dots. */
.fml-single-meta {
    display: flex;
    flex-wrap: wrap;
    margin: 0;
    color: var(--fml-muted);
    font-size: 0.875rem;
    line-height: 1.25rem;
    font-variant-numeric: tabular-nums;
}

.fml-single-meta > span:not(:first-child)::before {
    content: '·';
    margin-inline: 0.375rem;
}

.fml-single-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.fml-single-actions .fml-actions-group {
    gap: 0.25rem;
}

.fml-single-alt {
    margin-top: 0.25rem;
}

@media (max-width: 40rem) {
    .fml-single,
    .fml-empty-single {
        grid-template-columns: 4.5rem minmax(0, 1fr);
    }

    .fml-single .fml-card-actions,
    .fml-empty-single .fml-single-actions {
        grid-column: 2 / -1;
    }
}

/* ==========================================================================================================================
 * 2. Gallery
 * ======================================================================================================================= */

.fml-collection {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

/* Moving the selected files to a position, and loading more. */

.fml-move {
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius-lg, 0.5rem);
    background: var(--fml-muted-surface);
    box-shadow: 0 0 0 1px var(--primary-500);
}

.fml-more {
    justify-content: center;
}

/* Grid of tiles */

.fml-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(var(--fml-tile-size), 1fr));
    gap: 1rem;
    margin: 0;
    padding: 0;
    list-style: none;
}

.fml-tile {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
    min-width: 0;
}

.fml-tile.fml-is-draggable .fml-thumb {
    cursor: grab;
}

.fml-tile-top {
    position: absolute;
    top: 0.5rem;
    left: 0.5rem;
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 0.375rem;
}

.fml-tile-position {
    font-variant-numeric: tabular-nums;
}

.fml-tile-badges {
    position: absolute;
    left: 0.375rem;
    bottom: 0.375rem;
    z-index: 1;
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    pointer-events: none;
}

/* Badges sit on top of a photo, so they get a solid background instead of Filament's translucent one. */
.fml-tile-badge.fi-badge {
    background: color-mix(in oklab, var(--fml-surface) 92%, transparent);
    box-shadow: 0 1px 2px color-mix(in oklab, #000 15%, transparent);
}

/* Filament's gray icon button, on a solid background so it stays readable on top of a photo. */
.fml-tile-remove.fi-icon-btn {
    position: absolute;
    top: 0.375rem;
    right: 0.375rem;
    z-index: 2;
    margin: 0;
    background: color-mix(in oklab, var(--fml-surface) 92%, transparent);
    box-shadow: 0 1px 2px color-mix(in oklab, #000 15%, transparent);
    opacity: 0;
    transition: opacity 0.15s;
}

.fml-tile:hover .fml-tile-remove,
.fml-tile:focus-within .fml-tile-remove,
.fml-tile[data-state='failed'] .fml-tile-remove,
.fml-tile-remove:focus-visible {
    opacity: 1;
}

@media (hover: none), (max-width: 40rem) {
    .fml-tile-remove.fi-icon-btn {
        opacity: 1;
    }
}

/* The position sits in the bottom right corner of the photo, opposite the cover badge. */
.fml-tile-position.fi-badge {
    position: absolute;
    right: 0.375rem;
    bottom: 0.375rem;
    z-index: 1;
    pointer-events: none;
}

/* Keyboard focus anywhere in a card marks the whole card, like the picker's cards. */
.fml-grid > .fml-tile:has(:focus-visible) {
    outline: 2px solid var(--primary-600);
    outline-offset: 2px;
}

.fml-add-tile button:focus-visible {
    outline: 2px solid var(--primary-600);
    outline-offset: 2px;
}

.fml-tile-footer {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
    min-width: 0;
}

.fml-tile-footer-row {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    min-width: 0;
    min-height: 2rem;
}

/* A long name is cut off; the action icons always stay on one line at their full size */
.fml-tile-footer-row .fml-card-name {
    flex: 1 1 auto;
}

.fml-tile-footer-row .fml-card-actions,
.fml-tile-footer-row .fml-actions-group {
    flex: none;
    flex-wrap: nowrap;
}

.fml-tile-footer-row .fml-card-actions {
    margin-inline-start: auto;
}

.fml-card-name {
    display: block;
    min-width: 0;
    overflow: hidden;
    font-weight: 500;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Text actions under a tile: the alt text link (cut off when long) and "Details". */
.fml-tile-links {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
    margin-top: 0.125rem;
}

.fml-tile-alt {
    display: flex;
    flex: 0 1 auto;
    min-width: 0;
}

.fml-tile-details {
    flex: none;
}

.fml-tile-alt .fml-alt-link.fi-color-gray .fi-link-label {
    color: var(--fml-muted);
    font-weight: 400;
}

/* As tall as the cards in its row */
.fml-add-tile {
    display: flex;
    align-items: stretch;
    min-width: 0;
}

.fml-add-tile button {
    display: flex;
    flex: none;
    width: 100%;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
    min-height: 10rem;
    padding: 0.5rem;
    border: 2px dashed var(--fml-border);
    border-radius: var(--radius-xl, 0.75rem);
    background: transparent;
    color: var(--fml-muted);
    font-weight: 500;
    text-align: center;
    cursor: pointer;
}

.fml-add-tile button:hover {
    border-color: var(--primary-500);
    color: var(--primary-600);
}

.fml-add-tile-icon {
    width: 1.75rem;
    height: 1.75rem;
}

/* SortableJS: the tile left behind while dragging, and the copy that follows the pointer. */

.sortable-ghost {
    opacity: 0.35;
}

.sortable-fallback {
    opacity: 0.9;
    transform: rotate(2deg);
}

@media (max-width: 40rem) {
    .fml {
        --fml-tile-size: 7rem;
    }

    .fml-grid {
        gap: 0.5rem;
    }
}

/*
 * List (optional). Like a Filament table, with Filament's cell padding (px-3 py-4, ps-6/pe-6 on the outer cells), text
 * sizes and colours. Only the table is a card; the header and pagination sit outside it, as in the grid view.
 */

.fml-table-ctn {
    border-radius: var(--radius-xl, 0.75rem);
    background: var(--fml-surface);
    box-shadow:
        0 0 0 1px color-mix(in oklab, var(--gray-950) 5%, transparent),
        0 1px 3px 0 rgb(0 0 0 / 0.1),
        0 1px 2px -1px rgb(0 0 0 / 0.1);
    overflow-x: auto;
}

.dark .fml-table-ctn {
    box-shadow: 0 0 0 1px color-mix(in oklab, #fff 10%, transparent);
}

.fml-table {
    width: 100%;
    border-collapse: collapse;
    table-layout: auto;
    text-align: start;
    font-size: 0.875rem;
    line-height: 1.5rem;
}

.fml-table thead tr {
    background: var(--gray-50);
}

.dark .fml-table thead tr {
    background: color-mix(in oklab, #fff 5%, transparent);
}

.fml-table th {
    padding: 0.875rem 0.75rem;
    color: var(--gray-950);
    font-weight: 600;
    text-align: start;
    white-space: nowrap;
}

.dark .fml-table th {
    color: #fff;
}

.fml-table td {
    padding: 1rem 0.75rem;
    border-top: 1px solid var(--fml-border);
    vertical-align: middle;
    color: var(--gray-950);
}

.dark .fml-table td {
    border-top-color: color-mix(in oklab, #fff 5%, transparent);
    color: #fff;
}

.fml-table th:first-child,
.fml-table td:first-child {
    padding-inline-start: 1rem;
}

.fml-table th:last-child,
.fml-table td:last-child {
    padding-inline-end: 1rem;
}

@media (min-width: 40rem) {
    .fml-table th:first-child,
    .fml-table td:first-child {
        padding-inline-start: 1.5rem;
    }

    .fml-table th:last-child,
    .fml-table td:last-child {
        padding-inline-end: 1.5rem;
    }
}

.fml-table tr.fml-is-selected td {
    background: var(--gray-50);
}

.fml-table tr.fml-is-selected td:first-child {
    box-shadow: inset 2px 0 0 0 var(--primary-600);
}

.dark .fml-table tr.fml-is-selected td {
    background: color-mix(in oklab, #fff 5%, transparent);
}

.fml-table tr[data-state='failed'] td {
    background: color-mix(in oklab, var(--danger-500) 6%, var(--fml-surface));
}

.fml-table-cell-check {
    width: 1px;
    white-space: nowrap;
}

/*
 * Like Filament's table-auto tables: only the reorder handle and checkbox columns are narrow (w-1). Every other column
 * is sized by the browser from its content, text does not wrap, and the spare width is shared between the columns.
 */
.fml-table tbody td {
    white-space: nowrap;
}

/* Thumbnail and names in one cell, so the "File" header lines up with the content */
.fml-table-file {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.fml-table-file .fml-thumb-small {
    flex: none;
    width: 2.5rem;
    border-radius: var(--radius-md, 0.375rem);
}

.fml-table .fml-name-row {
    flex-wrap: nowrap;
}

/* Rows with details open them on click, like Filament's clickable table rows. */
.fml-table tbody tr.fml-is-clickable {
    cursor: pointer;
}

.fml-table tbody tr.fml-is-clickable:hover {
    background: var(--fml-muted-surface);
}

/* Secondary columns (details, upload date) are gray, like the library's list. */
.fml-table-muted {
    color: var(--fml-muted);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.fml-table tbody td.fml-table-cell-alt {
    min-width: 14rem;
    white-space: normal;
}

.fml-row-actions {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: flex-end;
    gap: 0.75rem;
}

/* Filament's reorder handle: a gray two-bar icon button in its own narrow column */
.fml-table-cell-handle {
    width: 1px;
}

.fml-table td.fml-table-cell-handle {
    padding-block: 0;
}

.fml-handle.fi-icon-btn {
    color: var(--gray-400);
    cursor: move;
}

.fml-handle.fi-icon-btn:hover {
    color: var(--gray-500);
}

.dark .fml-handle.fi-icon-btn {
    color: var(--gray-500);
}

/* ==========================================================================================================================
 * 3. Picker: the "Choose from library" dialog, teleported out of the field, so it sets its own variables
 * ======================================================================================================================= */

.fml-picker,
.fml-picker-footer {
    --fml-border: var(--gray-200);
    --fml-muted-surface: var(--gray-50);
    --fml-text: var(--gray-950);
    --fml-muted: var(--gray-500);
    --fml-faint: var(--gray-400);
    --fml-ring: color-mix(in oklab, var(--gray-950) 10%, transparent);
}

.dark .fml-picker,
.dark .fml-picker-footer {
    --fml-border: color-mix(in oklab, #fff 10%, transparent);
    --fml-muted-surface: color-mix(in oklab, #fff 5%, transparent);
    --fml-text: #fff;
    --fml-muted: var(--gray-400);
    --fml-faint: var(--gray-500);
    --fml-ring: color-mix(in oklab, #fff 10%, transparent);
}

/* The library and upload tabs. */
.fml-picker-tabs {
    margin-bottom: 1.5rem;
}

/* The grid with search on the left, the details of the clicked file on the right. */
.fml-picker {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 20rem;
    gap: 2rem;
    align-items: stretch;
    font-size: 0.875rem;
    line-height: 1.25rem;
}

.fml-picker-main {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    min-width: 0;
}

@media (max-width: 47.99rem) {
    .fml-picker {
        grid-template-columns: minmax(0, 1fr);
    }

    .fml-picker-side {
        display: none;
    }
}

/* Toolbar: filters on the left, Filament's search field on the right. */
.fml-picker-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.fml-picker-filters {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
}

.fml-picker-search {
    flex: 0 1 18rem;
    margin-inline-start: auto;
}

.fml-picker-status {
    display: flex;
    justify-content: center;
    margin: 0;
    padding: 2.5rem 1rem;
    color: var(--fml-muted);
    text-align: center;
}

/* Cards of media, loaded with infinite scroll. */

.fml-picker-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
    gap: 1rem;
    margin: 0;
    padding: 0;
    list-style: none;
}

.fml-picker-sentinel {
    height: 1px;
}

.fml-picker-item {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    padding: 0;
    overflow: hidden;
    border: 0;
    border-radius: var(--radius-xl, 0.75rem);
    background: var(--fml-surface, #fff);
    box-shadow: 0 0 0 1px var(--fml-ring), 0 1px 2px 0 rgb(0 0 0 / 0.05);
    color: inherit;
    text-align: start;
    cursor: pointer;
    transition: box-shadow 0.12s;
}

.dark .fml-picker-item {
    background: var(--gray-900);
}

.fml-picker-item:not([aria-disabled='true']):hover {
    box-shadow: 0 0 0 1px var(--gray-300), 0 4px 14px -6px rgb(0 0 0 / 0.25);
}

.fml-picker-item.fml-is-selected,
.fml-picker-item.fml-is-selected:hover {
    box-shadow: 0 0 0 2px var(--primary-600);
}

.fml-picker-item.fml-is-focused:not(.fml-is-selected) {
    box-shadow: 0 0 0 1px var(--gray-400), 0 1px 2px 0 rgb(0 0 0 / 0.05);
}

.fml-picker-item:focus-visible {
    outline: 2px solid var(--primary-600);
    outline-offset: 2px;
}

.fml-picker-item[aria-disabled='true'] {
    cursor: default;
    opacity: 0.5;
}

.fml-picker-thumb {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    aspect-ratio: 4 / 3;
    border-bottom: 1px solid var(--fml-border);
    background: var(--fml-muted-surface);
    overflow: hidden;
}

.fml-picker-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.fml-picker-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    color: var(--fml-faint);
    font-size: 0.75rem;
    font-weight: 600;
}

.fml-picker-placeholder-icon {
    width: 2rem;
    height: 2rem;
}

.fml-picker-check {
    position: absolute;
    top: 0.5rem;
    inset-inline-start: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.375rem;
    height: 1.375rem;
    border: 1.5px solid rgb(255 255 255 / 0.9);
    border-radius: 0.375rem;
    background: rgb(17 17 17 / 0.25);
    color: #fff;
    opacity: 0;
    transition: opacity 0.1s;
}

.fml-picker-meta {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 0;
    padding: 0.75rem;
}

.fml-picker-name {
    overflow: hidden;
    font-weight: 500;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.fml-picker-details {
    margin: 0;
    color: var(--fml-muted);
    font-size: 0.75rem;
    line-height: 1rem;
    font-variant-numeric: tabular-nums;
}

/* Details of the clicked file. */

/* The divider runs the full height of the grid; the panel's content stays in view while the grid scrolls. */
.fml-picker-side {
    min-height: 12rem;
    padding-inline-start: 2rem;
    border-inline-start: 1px solid var(--fml-border);
}

.fml-picker-side > * {
    position: sticky;
    top: 0;
}

/* One line for the file name; the full name is in the tooltip. */
.fml-picker-side .fi-section-header,
.fml-picker-side .fi-section-header > div {
    min-width: 0;
}

.fml-picker-side .fi-section-header-heading {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.fml-picker-side-content {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.fml-picker-side-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    margin: 0;
    padding: 2rem 0.5rem;
    color: var(--fml-muted);
    text-align: center;
}

/* Label and value rows, like Filament's inline text entries, for details shown with Alpine. */
.fml-entries {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 0.75rem 1rem;
    margin: 0;
    font-size: 0.875rem;
    line-height: 1.25rem;
}

.fml-entries > div {
    display: contents;
}

.fml-entries dt {
    color: var(--gray-950);
    font-weight: 500;
}

.fml-entries dd {
    margin: 0;
    color: var(--gray-950);
    overflow-wrap: anywhere;
    font-variant-numeric: tabular-nums;
}

.dark .fml-entries dt,
.dark .fml-entries dd {
    color: #fff;
}

/* Footer: the selected files on the left, the buttons on the right. */

.fml-picker-footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    width: 100%;
}

.fml-picker-tray {
    display: flex;
    flex: 1 1 12rem;
    align-items: center;
    justify-content: flex-end;
    gap: 0.375rem;
    min-width: 0;
    overflow: hidden;
    font-size: 0.875rem;
}

.fml-picker-tray .fml-muted {
    white-space: nowrap;
}

.fml-picker-mini {
    display: flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    overflow: hidden;
    border: 1px solid var(--fml-border);
    border-radius: 0.375rem;
    background: var(--fml-muted-surface);
    color: var(--fml-faint);
}

.fml-picker-mini img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* The upload tab: the field's drop zone, taller. */

.fml-picker-upload {
    --fml-border: var(--gray-200);
    --fml-muted-surface: var(--gray-50);
    --fml-muted: var(--gray-500);

    font-size: 0.875rem;
    line-height: 1.25rem;
}

.dark .fml-picker-upload {
    --fml-border: color-mix(in oklab, #fff 10%, transparent);
    --fml-muted-surface: color-mix(in oklab, #fff 5%, transparent);
    --fml-muted: var(--gray-400);
}

.fml-picker-upload .fml-dropzone {
    min-height: 16rem;
}

/* ==========================================================================================================================
 * 4. Library page: Filament's table as cards (grid) or rows (list). The variables also cover the details slide-over.
 * ======================================================================================================================= */

.fml-library-table,
.fml-library-list,
.fml-library-preview,
.fml-library-usage,
.fml-merge-options {
    --fml-border: var(--gray-200);
    --fml-muted-surface: var(--gray-50);
    --fml-text: var(--gray-950);
    --fml-muted: var(--gray-500);
    --fml-faint: var(--gray-400);
}

.dark .fml-library-table,
.dark .fml-library-list,
.dark .fml-library-preview,
.dark .fml-library-usage,
.dark .fml-merge-options {
    --fml-border: color-mix(in oklab, #fff 10%, transparent);
    --fml-muted-surface: color-mix(in oklab, #fff 5%, transparent);
    --fml-text: #fff;
    --fml-muted: var(--gray-400);
    --fml-faint: var(--gray-500);
}

/* Cards: the thumbnail runs to the card's edges and the selection checkbox sits on top of it. */

.fml-library-table .fi-ta-content-grid {
    gap: 1rem;
}

.fml-library-table .fi-ta-record {
    position: relative;
    padding: 0;
    overflow: hidden;
    transition: box-shadow 0.12s, transform 0.12s;
}

.fml-library-table .fi-ta-record-checkbox {
    position: absolute;
    top: 0.5rem;
    left: 0.5rem;
    z-index: 1;
    margin: 0;
    opacity: 0;
    transition: opacity 0.1s;
}

.fml-library-table .fi-ta-record-content-ctn,
.fml-library-table .fi-ta-record-content-ctn > div,
.fml-library-table .fi-ta-record-content {
    flex: 1 1 0%;
    width: 100%;
    min-width: 0;
    padding: 0;
    margin: 0;
}

.fml-library-table .fi-ta-record-content .fi-ta-col {
    display: block;
    width: 100%;
    padding: 0;
}

/* The whole card opens the slide-over, which holds the actions. */
.fml-library-table .fi-ta-record .fi-ta-actions {
    display: none;
}

.fml-library-thumb {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    aspect-ratio: 4 / 3;
    background: var(--fml-muted-surface);
    border-bottom: 1px solid var(--fml-border);
    overflow: hidden;
}

.fml-library-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.fml-library-thumb-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--fml-faint);
}

.fml-library-thumb-icon {
    width: 2.25rem;
    height: 2.25rem;
}

.fml-library-tile-badge {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
}

/* Name, details, date and usage below the thumbnail: Filament text columns, padded inside the card. */
.fml-library-table .fml-library-tile-meta {
    gap: 0.125rem;
    padding: 0.5rem 0.75rem 0.75rem;
    min-width: 0;
}

.fml-library-table .fml-library-tile-meta .fi-ta-col {
    min-width: 0;
    font-variant-numeric: tabular-nums;
}

/* List view: a small square thumbnail in the first column. */
.fml-library-thumb-compact {
    width: 2.5rem;
    border: 1px solid var(--fml-border);
    border-radius: var(--radius-md, 0.375rem);
    aspect-ratio: 1;
}

.fml-library-thumb-compact .fml-library-thumb-icon {
    width: 1.25rem;
    height: 1.25rem;
}

/* List view: preview, name and file name in one cell, like the upload field's list. */
/* The same padding as Filament's text columns, so the cell lines up with its header. */
.fml-library-file {
    padding: 0.75rem;
}

.fml-library-file .fml-library-thumb-compact {
    flex: none;
}

.fml-library-file-text {
    min-width: 0;
}

.fml-library-file-name {
    display: block;
    overflow: hidden;
    color: var(--fml-text);
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.25rem;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ==========================================================================================================================
 * 5. Details: the slide-over with a media's preview, actions and usage
 * ======================================================================================================================= */

.fml-library-preview {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 8rem;
    border-radius: var(--radius-lg, 0.5rem);
    border: 1px solid var(--fml-border);
    background: repeating-conic-gradient(var(--fml-muted-surface) 0 25%, transparent 0 50%) 0 0 / 16px 16px;
    overflow: hidden;
}

.fml-library-preview img,
.fml-library-preview video {
    display: block;
    max-width: 100%;
    max-height: 18rem;
    object-fit: contain;
}

.fml-library-preview .fml-library-thumb-placeholder {
    flex-direction: column;
    gap: 0.25rem;
    padding: 2rem;
    font-size: 0.75rem;
    font-weight: 600;
}

.fml-library-actions .fi-ac {
    flex-wrap: wrap;
}

.fml-library-actions .fml-library-delete {
    margin-inline-start: auto;
}

.fml-library-usage {
    margin: -0.25rem 0;
    padding: 0;
    list-style: none;
    font-size: 0.875rem;
}

.fml-library-usage li {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem 0;
}

.fml-library-usage li + li {
    border-top: 1px solid var(--fml-border);
}

.fml-library-usage-icon {
    display: grid;
    flex: none;
    place-items: center;
    width: 2rem;
    height: 2rem;
    border-radius: 0.5rem;
    background: var(--fml-muted-surface);
    color: var(--fml-muted);
}

.fml-library-usage-icon svg {
    width: 1rem;
    height: 1rem;
}

.fml-library-usage-text {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-width: 0;
}

.fml-library-usage-title {
    overflow: hidden;
    font-weight: 500;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.fml-library-usage-type {
    color: var(--fml-muted);
    font-size: 0.875rem;
    line-height: 1.25rem;
}

/* Merge dialog: the choice of the main file, one row per file like the usage list. */
.fml-merge-options {
    display: flex;
    flex-direction: column;
}

.fml-merge-option {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem 0;
    cursor: pointer;
}

.fml-merge-option + .fml-merge-option {
    border-top: 1px solid var(--fml-border);
}

.fml-merge-option .fi-radio-input {
    flex: none;
}

.fml-merge-option .fml-library-thumb-compact {
    flex: none;
}

/* ==========================================================================================================================
 * 6. Cards: one look for every grid of files, the picker, the library page and the field's gallery
 * ======================================================================================================================= */

.fml-picker-item,
.fml-library-table .fi-ta-record,
.fml-grid > .fml-tile {
    gap: 0;
    overflow: hidden;
    border-radius: var(--radius-xl, 0.75rem);
    background: #fff;
    box-shadow: 0 0 0 1px color-mix(in oklab, var(--gray-950) 10%, transparent), 0 1px 2px 0 rgb(0 0 0 / 0.05);
    transition: box-shadow 0.12s;
}

.dark .fml-picker-item,
.dark .fml-library-table .fi-ta-record,
.dark .fml-grid > .fml-tile {
    background: var(--gray-900);
    box-shadow: 0 0 0 1px color-mix(in oklab, #fff 10%, transparent);
}

.fml-picker-item:not([aria-disabled='true']):hover,
.fml-library-table .fi-ta-record:hover,
.fml-grid > .fml-tile:hover {
    box-shadow: 0 0 0 1px var(--gray-300), 0 4px 14px -6px rgb(0 0 0 / 0.25);
}

.dark .fml-picker-item:not([aria-disabled='true']):hover,
.dark .fml-library-table .fi-ta-record:hover,
.dark .fml-grid > .fml-tile:hover {
    box-shadow: 0 0 0 1px var(--gray-600), 0 4px 14px -6px rgb(0 0 0 / 0.5);
}

.fml-picker-item.fml-is-selected,
.fml-picker-item.fml-is-selected:hover,
.fml-library-table .fi-ta-record.fi-selected,
.fml-library-table .fi-ta-record.fi-selected:hover,
.fml-grid > .fml-tile.fml-is-selected,
.fml-grid > .fml-tile.fml-is-selected:hover {
    box-shadow: 0 0 0 2px var(--primary-600);
}

/* The thumbnail runs to the card's edges, 4:3, above a border. */
.fml-picker-thumb,
.fml-library-table .fml-library-thumb,
.fml-grid > .fml-tile > .fml-thumb {
    aspect-ratio: 4 / 3;
    border-radius: 0;
    border-bottom: 1px solid var(--fml-border, var(--gray-200));
    background: var(--fml-muted-surface, var(--gray-50));
    box-shadow: none;
}

/* Name and details under the thumbnail. */
.fml-picker-meta,
.fml-library-table .fml-library-tile-meta,
.fml-grid > .fml-tile > .fml-tile-footer {
    gap: 0.25rem;
    padding: 0.75rem;
}

.fml-picker-name,
.fml-library-card-name > span,
.fml-grid > .fml-tile .fml-card-name {
    color: var(--gray-950);
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.25rem;
}

.dark .fml-picker-name,
.dark .fml-library-card-name > span,
.dark .fml-grid > .fml-tile .fml-card-name {
    color: #fff;
}

.fml-picker-details,
.fml-grid > .fml-tile .fml-card-details,
.fml-library-table .fml-library-tile-meta .fi-ta-text-item {
    font-weight: 400;
    font-variant-numeric: tabular-nums;
    color: var(--gray-500);
    font-size: 0.75rem;
    line-height: 1rem;
}

.dark .fml-picker-details,
.dark .fml-grid > .fml-tile .fml-card-details,
.dark .fml-library-table .fml-library-tile-meta .fi-ta-text-item {
    color: var(--gray-400);
}

/* A file without a thumbnail: its icon and type. */
.fml-picker-placeholder,
.fml-library-table .fml-library-thumb-placeholder,
.fml-thumb-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    color: var(--gray-400);
    font-size: 0.75rem;
    font-weight: 600;
    line-height: 1rem;
}

.fml-picker-placeholder-icon,
.fml-library-table .fml-library-thumb-icon,
.fml-thumb-placeholder-icon {
    width: 2rem;
    height: 2rem;
}

/* The selection check in the top corner: shown on hover, when selected, or once anything in the grid is selected. */
.fml-picker-check,
.fml-library-table .fi-ta-record .fi-ta-record-checkbox,
.fml-grid .fml-tile-check .fi-checkbox-input {
    display: block;
    flex: none;
    width: 1.375rem;
    height: 1.375rem;
    margin: 0;
    border: 1.5px solid rgb(255 255 255 / 0.9);
    border-radius: 0.375rem;
    background-color: rgb(17 17 17 / 0.25);
    background-image: none;
    box-shadow: none;
    cursor: pointer;
    appearance: none;
    opacity: 0;
    transition: opacity 0.1s;
}

.fml-grid .fml-tile-check {
    display: flex;
}

.fml-picker-item.fml-is-selected .fml-picker-check,
.fml-library-table .fi-ta-record .fi-ta-record-checkbox:checked,
.fml-grid .fml-tile-check .fi-checkbox-input:checked {
    border-color: var(--primary-600);
    background-color: var(--primary-600);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='white'%3E%3Cpath fill-rule='evenodd' d='M16.704 4.153a.75.75 0 0 1 .143 1.052l-8 10.5a.75.75 0 0 1-1.127.075l-4.5-4.5a.75.75 0 0 1 1.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 0 1 1.05-.143Z' clip-rule='evenodd'/%3E%3C/svg%3E");
    background-position: center;
    background-repeat: no-repeat;
    background-size: 0.875rem;
}

.fml-picker-item:not([aria-disabled='true']):hover .fml-picker-check,
.fml-picker-item.fml-is-selected .fml-picker-check,
.fml-picker-grid:has(.fml-is-selected) .fml-picker-item:not([aria-disabled='true']) .fml-picker-check,
.fml-library-table .fi-ta-record:hover .fi-ta-record-checkbox,
.fml-library-table .fi-ta-record .fi-ta-record-checkbox:checked,
.fml-library-table .fi-ta-record .fi-ta-record-checkbox:focus-visible,
.fml-library-table .fi-ta-content-grid:has(.fi-ta-record-checkbox:checked) .fi-ta-record-checkbox,
.fml-grid > .fml-tile:hover .fml-tile-check .fi-checkbox-input,
.fml-grid .fml-tile-check .fi-checkbox-input:checked,
.fml-grid .fml-tile-check .fi-checkbox-input:focus-visible,
.fml-grid:has(.fml-tile-check :checked) .fml-tile-check .fi-checkbox-input {
    opacity: 1;
}

.fml-library-table .fi-ta-record .fi-ta-record-checkbox:focus-visible,
.fml-grid .fml-tile-check .fi-checkbox-input:focus-visible {
    outline: 2px solid var(--primary-600);
    outline-offset: 2px;
}

/* A column the library's cards only need for sorting. */
.fml-library-table .fi-ta-col:has(> .fml-library-tile-hidden) {
    display: none;
}

/* The name on one line, cut off, with the Details icon after it: the same row as the field's cards. */
.fml-library-card-name {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    min-height: 2rem;
}

.fml-library-card-name > span {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.fml-library-card-name-icon {
    flex: none;
    width: 1.25rem;
    height: 1.25rem;
    color: var(--primary-600);
}

.dark .fml-library-card-name-icon {
    color: var(--primary-400);
}

/* Cards fill each row at the same minimum width as the field's grid. */
.fml-library-table .fi-ta-content-grid {
    grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
}

/* Names in rows are medium, like the library's list and the single file field. */
.fml-table-cell-file .fml-table-text {
    font-weight: 500;
}

/* Library list cells use the same line height as the field's list rows. */
.fml-library-list .fi-ta-text-item {
    line-height: 1.25rem;
}

/* On phones every grid of files uses the field's smaller cards and gaps, so they show two or more columns alike. */
@media (max-width: 40rem) {
    .fml-picker-grid,
    .fml-library-table .fi-ta-content-grid {
        grid-template-columns: repeat(auto-fill, minmax(7rem, 1fr));
        gap: 0.5rem;
    }
}
