/* -------------------------------------------------------------------------- */
/* Grid                                                                       */
/* -------------------------------------------------------------------------- */

.card-grid {
    position: relative;
    display: grid;
    grid-template-columns: repeat(12, minmax(0, 1fr));
    gap: 1rem;
    margin-block: 1rem;
    align-items: stretch;
    padding: 0.25rem;
}

/* The `.fi-prose` "stack" rule adds `margin-top` to adjacent siblings, which
   makes cells 2..N shorter than cell 1. Reset it on the grid and cells. */
.card-grid,
.card-grid .card-grid-cell {
    margin-top: 0;
}

/* Editor-only: dashed outline so the grid structure is visible while editing
   without obscuring the content. The content div inside holds the cells. */
.fi-fo-rich-editor .card-grid,
.ProseMirror .card-grid {
    border: 1px dashed rgb(148 163 184);
    border-radius: 0.5rem;
}

.dark .fi-fo-rich-editor .card-grid,
.dark .ProseMirror .card-grid {
    border-color: rgb(71 85 105);
}

.card-grid-content {
    display: contents;
}

/* -------------------------------------------------------------------------- */
/* Handle toolbar (top-left, absolutely positioned)                            */
/* -------------------------------------------------------------------------- */

.card-grid-handles {
    position: absolute;
    top: -0.625rem;
    left: -0.625rem;
    display: none;
    align-items: center;
    gap: 0.375rem;
    z-index: 3;
    user-select: none;
}

/* Cell handles dock to the top-right so they are not obscured by the grid's
   own handle group in the top-left corner. */
.card-grid-cell > .card-grid-handles {
    left: auto;
    right: -0.625rem;
}

/* Show the toolbar when hovering the grid or the cell. */
.card-grid:hover > .card-grid-handles,
.card-grid-cell:hover > .card-grid-handles {
    display: flex;
}

/* Every button in the toolbar shares this base look. The `.fi-prose` stack
   rule adds `margin-top` to adjacent siblings; reset it here and on the
   surrounding toolbar/group so the handles stay on one line. */
.card-grid-handle,
.card-grid-handles,
.card-grid-handle-group {
    margin-top: 0;
}

.card-grid-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.5rem;
    height: 1.5rem;
    padding: 0;
    border-radius: 0.375rem;
    background-color: white;
    color: rgb(55 65 81);
    cursor: pointer;
    border: 1px solid rgb(209 213 219);
    box-shadow: 0 1px 3px rgb(0 0 0 / 0.1), 0 1px 2px rgb(0 0 0 / 0.06);
    line-height: 1;
    font-size: 0;
}

.card-grid-handle:hover {
    background-color: rgb(243 244 246);
    color: rgb(17 24 39);
    border-color: rgb(156 163 175);
}

.card-grid-handle--delete:hover {
    background-color: rgb(254 226 226);
    color: rgb(185 28 28);
    border-color: rgb(252 165 165);
}

/* A handle group joins its buttons into one visual unit: no gap, shared
   borders, and rounded corners only on the outer edges. */
.card-grid-handle-group {
    display: flex;
    align-items: center;
    border-radius: 0.375rem;
    box-shadow: 0 1px 3px rgb(0 0 0 / 0.1), 0 1px 2px rgb(0 0 0 / 0.06);
    overflow: hidden;
}

.card-grid-handle-group .card-grid-handle {
    border-radius: 0;
    box-shadow: none;
    border-right-width: 0;
}

.card-grid-handle-group .card-grid-handle:first-child {
    border-radius: 0.375rem 0 0 0.375rem;
}

.card-grid-handle-group .card-grid-handle:last-child {
    border-radius: 0 0.375rem 0.375rem 0;
    border-right-width: 1px;
}

/* In RTL the group mirrors its rounding. */
[dir="rtl"] .card-grid-handle-group .card-grid-handle:first-child {
    border-radius: 0 0.375rem 0.375rem 0;
}

[dir="rtl"] .card-grid-handle-group .card-grid-handle:last-child {
    border-radius: 0.375rem 0 0 0.375rem;
    border-right-width: 0;
}

[dir="rtl"] .card-grid-handle-group .card-grid-handle {
    border-right-width: 1px;
    border-left-width: 0;
}

[dir="rtl"] .card-grid-handle-group .card-grid-handle:last-child {
    border-left-width: 1px;
}

/* In RTL the grid toolbar docks to the top-right, so cell handles move to the
   top-left to stay out of its way. */
[dir="rtl"] .card-grid-cell > .card-grid-handles {
    right: auto;
    left: -0.625rem;
}

.dark .card-grid-handle {
    background-color: rgb(55 65 81);
    color: rgb(209 213 219);
    border-color: rgb(75 85 99);
    box-shadow: 0 1px 3px rgb(0 0 0 / 0.4), 0 1px 2px rgb(0 0 0 / 0.3);
}

.dark .card-grid-handle:hover {
    background-color: rgb(75 85 99);
    color: white;
    border-color: rgb(107 114 128);
}

.dark .card-grid-handle--delete:hover {
    background-color: rgb(127 29 29);
    color: rgb(254 202 202);
    border-color: rgb(185 28 28);
}

.dark .card-grid-handle-group {
    box-shadow: 0 1px 3px rgb(0 0 0 / 0.4), 0 1px 2px rgb(0 0 0 / 0.3);
}

/* -------------------------------------------------------------------------- */
/* Cell                                                                       */
/* -------------------------------------------------------------------------- */

.card-grid-cell {
    position: relative;
    border: 1px solid rgb(229 231 235);
    border-radius: 0.5rem;
    padding: 1rem;
    background-color: rgb(249 250 251);
    min-height: 4rem;
}

.dark .card-grid-cell {
    border-color: rgb(55 65 81);
    background-color: rgb(17 24 39);
}

/* Editor-only: outline cells with a dashed border and no fill, so the
   structure is visible while editing without obscuring the content. */
.fi-fo-rich-editor .card-grid-cell,
.ProseMirror .card-grid-cell {
    border: 1px dashed rgb(148 163 184);
    background-color: transparent;
}

.dark .fi-fo-rich-editor .card-grid-cell,
.dark .ProseMirror .card-grid-cell {
    border-color: rgb(71 85 105);
}

.card-grid-cell-content {
    min-height: 1.5rem;
}

/* -------------------------------------------------------------------------- */
/* Drag and drop feedback                                                     */
/* -------------------------------------------------------------------------- */

.card-grid-cell--dragging {
    opacity: 0.4;
}

.card-grid-cell--drop-target {
    outline: 2px solid rgb(56 189 248);
    outline-offset: 2px;
}

/* -------------------------------------------------------------------------- */
/* Modal                                                                      */
/* -------------------------------------------------------------------------- */

.card-grid-modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background-color: rgb(0 0 0 / 0.3);
}

.dark .card-grid-modal-overlay {
    background-color: rgb(0 0 0 / 0.6);
}

.card-grid-modal {
    position: fixed;
    top: 50%;
    left: 50%;
    width: min(24rem, calc(100vw - 2rem));
    max-height: calc(100vh - 2rem);
    overflow-y: auto;
    padding: 1.25rem;
    border-radius: 0.75rem;
    background-color: white;
    box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
}

.dark .card-grid-modal {
    background-color: rgb(31 41 55);
    box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.5), 0 4px 6px -4px rgb(0 0 0 / 0.5);
}

.card-grid-modal__title {
    margin: 0 0 1rem;
    font-size: 1rem;
    font-weight: 600;
    color: rgb(17 24 39);
}

.dark .card-grid-modal__title {
    color: rgb(243 244 246);
}

/* A row groups two or more fields on a single line. */
.card-grid-modal__row {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 0.875rem;
}

.card-grid-modal__row .card-grid-modal__field {
    flex: 1;
    margin-bottom: 0;
}

.card-grid-modal__field {
    display: block;
    margin-bottom: 0.875rem;
}

.card-grid-modal__label {
    display: block;
    margin-bottom: 0.375rem;
    font-size: 0.875rem;
    font-weight: 500;
    color: rgb(55 65 81);
}

.dark .card-grid-modal__label {
    color: rgb(209 213 219);
}

.card-grid-modal__input {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid rgb(209 213 219);
    border-radius: 0.5rem;
    font-size: 0.875rem;
    color: rgb(17 24 39);
    background-color: white;
    box-sizing: border-box;
}

.dark .card-grid-modal__input {
    border-color: rgb(75 85 99);
    color: rgb(243 244 246);
    background-color: rgb(55 65 81);
}

.card-grid-modal__input:focus {
    outline: 2px solid rgb(56 189 248);
    outline-offset: -1px;
}

.card-grid-modal__help {
    display: block;
    margin-top: 0.25rem;
    font-size: 0.75rem;
    line-height: 1.4;
    color: rgb(107 114 128);
}

.dark .card-grid-modal__help {
    color: rgb(156 163 175);
}

.card-grid-modal__message {
    margin: 0 0 1rem;
    font-size: 0.875rem;
    line-height: 1.5;
    color: rgb(75 85 99);
}

.dark .card-grid-modal__message {
    color: rgb(209 213 219);
}

.card-grid-modal__actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 1.25rem;
}

.card-grid-modal__cancel,
.card-grid-modal__apply {
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    border: 1px solid transparent;
}

.card-grid-modal__cancel {
    color: rgb(55 65 81);
    background-color: white;
    border-color: rgb(209 213 219);
}

.dark .card-grid-modal__cancel {
    color: rgb(209 213 219);
    background-color: rgb(55 65 81);
    border-color: rgb(75 85 99);
}

.card-grid-modal__cancel:hover {
    background-color: rgb(243 244 246);
}

.dark .card-grid-modal__cancel:hover {
    background-color: rgb(75 85 99);
}

.card-grid-modal__apply {
    color: white;
    background-color: rgb(56 189 248);
}

.card-grid-modal__apply:hover {
    background-color: rgb(14 165 233);
}

.card-grid-modal__apply--danger {
    color: white;
    background-color: rgb(220 38 38);
}

.card-grid-modal__apply--danger:hover {
    background-color: rgb(185 28 28);
}
