/**
 * TipTap Editor — Admin Styles
 *
 * Styles pour l'editeur TipTap integre dans EasyAdmin.
 * Utilise les couleurs du theme ($bw-blue) via CSS custom properties.
 */

// ─── Editor Wrapper ──────────────────────────────────────────────────────────

.tiptap-wrapper {
    border: 1px solid #dee2e6;
    border-radius: 0.5rem;
    overflow: hidden;
    background: #fff;
    margin-bottom: 0.5rem;
}

// ─── Toolbar ─────────────────────────────────────────────────────────────────

.tiptap-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 2px;
    padding: 0.4rem 0.5rem;
    border-bottom: 1px solid #dee2e6;
    background: #f8f9fa;
}

.tiptap-toolbar-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: #495057;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 600;
    line-height: 1;
    transition: background 0.15s, color 0.15s;
    padding: 0;

    &:hover {
        background: #e9ecef;
        color: #212529;
    }

    &.is-active {
        background: #0455C0;
        color: #fff;
    }

    i {
        font-size: 0.85rem;
    }
}

.tiptap-toolbar-sep {
    display: inline-block;
    width: 1px;
    height: 20px;
    background: #dee2e6;
    margin: 0 4px;
}

// ─── Editor Content Area ─────────────────────────────────────────────────────

.tiptap-editor-content {
    min-height: 400px;
    max-height: 70vh;
    overflow-y: auto;
    padding: 1.25rem 1.5rem;

    .ProseMirror {
        min-height: 350px;
        outline: none;
        font-size: 1rem;
        line-height: 1.75;
        color: #212529;

        > * + * {
            margin-top: 0.75em;
        }

        // Placeholder
        p.is-editor-empty:first-child::before {
            content: attr(data-placeholder);
            float: left;
            color: #adb5bd;
            pointer-events: none;
            height: 0;
            font-style: italic;
        }

        // Headings
        h2 {
            font-size: 1.5rem;
            font-weight: 700;
            margin-top: 1.5em;
            color: #1d2226;
        }

        h3 {
            font-size: 1.25rem;
            font-weight: 600;
            margin-top: 1.25em;
            color: #1d2226;
        }

        h4 {
            font-size: 1.1rem;
            font-weight: 600;
            margin-top: 1em;
        }

        // Lists
        ul, ol {
            padding-left: 1.5rem;
        }

        li {
            margin-bottom: 0.25em;
        }

        // Blockquote
        blockquote {
            border-left: 3px solid #0455C0;
            padding-left: 1rem;
            margin-left: 0;
            margin-right: 0;
            color: #6c757d;
            font-style: italic;
        }

        // Code blocks
        pre {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 0.5rem;
            padding: 1rem;
            font-family: 'Courier New', Courier, monospace;
            font-size: 0.875rem;
            overflow-x: auto;
            line-height: 1.5;

            code {
                background: none;
                color: inherit;
                padding: 0;
                border-radius: 0;
                font-size: inherit;
            }
        }

        // Inline code
        code {
            background: #f1f3f5;
            border-radius: 3px;
            padding: 0.15em 0.35em;
            font-size: 0.9em;
            color: #e83e8c;
        }

        // Horizontal rule
        hr {
            border: none;
            border-top: 2px solid #dee2e6;
            margin: 2rem 0;
        }

        // Images
        img {
            max-width: 100%;
            height: auto;
            border-radius: 0.375rem;
            margin: 0.5rem 0;
            cursor: default;

            &.ProseMirror-selectednode {
                outline: 2px solid #0455C0;
                outline-offset: 2px;
            }
        }

        // Links
        a {
            color: #0455C0;
            text-decoration: underline;
            cursor: pointer;
        }

        // YouTube embeds
        div[data-youtube-video] {
            position: relative;
            padding-bottom: 56.25%;
            height: 0;
            overflow: hidden;
            border-radius: 0.5rem;
            margin: 1rem 0;

            iframe {
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                border: 0;
            }

            &.ProseMirror-selectednode {
                outline: 2px solid #0455C0;
                outline-offset: 2px;
            }
        }

        // Highlight
        mark, .block-highlight {
            background: rgba(245, 158, 11, 0.3);
            padding: 0.1em 0.2em;
            border-radius: 2px;
        }

        // Callouts dans l'editeur
        div[data-callout] {
            display: flex;
            gap: 0.75rem;
            padding: 0.75rem 1rem;
            margin: 0.75rem 0;
            border-radius: 0.5rem;
            border-left: 4px solid;

            &[data-callout="info"] {
                background: rgba(59, 130, 246, 0.08);
                border-left-color: #3b82f6;
            }
            &[data-callout="warning"] {
                background: rgba(245, 158, 11, 0.08);
                border-left-color: #f59e0b;
            }
            &[data-callout="success"] {
                background: rgba(34, 197, 94, 0.08);
                border-left-color: #22c55e;
            }
            &[data-callout="danger"] {
                background: rgba(239, 68, 68, 0.08);
                border-left-color: #ef4444;
            }
        }

        // Colonnes dans l'editeur
        div[data-columns] {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 1.5rem;
            margin: 0.75rem 0;
            padding: 0.5rem;
            border: 1px dashed #dee2e6;
            border-radius: 0.5rem;
        }

        div[data-column] {
            padding: 0.5rem;
            border: 1px dashed #e9ecef;
            border-radius: 0.375rem;
            min-height: 3rem;
        }

        // Tables
        .tableWrapper {
            overflow-x: auto;
            margin: 1rem 0;
        }

        table {
            border-collapse: collapse;
            width: 100%;
            table-layout: fixed;
            overflow: hidden;

            th, td {
                border: 1px solid #dee2e6;
                padding: 0.5rem 0.75rem;
                vertical-align: top;
                min-width: 80px;
                position: relative;

                > * {
                    margin: 0;
                }
            }

            th {
                background: #f1f3f5;
                font-weight: 600;
                color: #1d2226;
            }

            td {
                background: #fff;
            }

            // Selection visuelle des cellules
            .selectedCell::after {
                content: '';
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                background: rgba(4, 85, 192, 0.12);
                pointer-events: none;
                z-index: 1;
            }

            // Poignee de redimensionnement des colonnes
            .column-resize-handle {
                position: absolute;
                right: -2px;
                top: 0;
                bottom: -2px;
                width: 4px;
                background: #0455C0;
                pointer-events: none;
                z-index: 2;
            }
        }

        &.resize-cursor {
            cursor: col-resize;
        }

        // Gapcursor
        .ProseMirror-gapcursor {
            &::after {
                border-top: 1px solid #0455C0;
            }
        }
    }
}

// ─── Responsive Preview Modes ────────────────────────────────────────────────

.tiptap-editor-content.tiptap-preview-tablet {
    .ProseMirror {
        max-width: 768px;
        margin: 0 auto;
        border-left: 1px dashed #dee2e6;
        border-right: 1px dashed #dee2e6;
        padding-left: 1rem;
        padding-right: 1rem;
    }
}

.tiptap-editor-content.tiptap-preview-mobile {
    .ProseMirror {
        max-width: 375px;
        margin: 0 auto;
        border-left: 1px dashed #dee2e6;
        border-right: 1px dashed #dee2e6;
        padding-left: 0.75rem;
        padding-right: 0.75rem;
        font-size: 0.9rem;
    }
}

// ─── Status Bar (character count + autosave) ────────────────────────────────

.tiptap-status-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.25rem 0.75rem;
    border-top: 1px solid #dee2e6;
    background: #f8f9fa;
    min-height: 1.75rem;
}

.tiptap-char-count {
    font-size: 0.75rem;
    color: #6c757d;
}

.tiptap-autosave-indicator {
    font-size: 0.75rem;
    color: #6c757d;
    opacity: 0;
    transition: opacity 0.3s ease;

    &.visible {
        opacity: 1;
    }
}

// ─── Media Modal ─────────────────────────────────────────────────────────────

.tiptap-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tiptap-modal-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
}

.tiptap-modal-dialog {
    position: relative;
    width: 90%;
    max-width: 800px;
    max-height: 80vh;
    background: #fff;
    border-radius: 0.75rem;
    box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
}

.tiptap-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #dee2e6;

    h3 {
        margin: 0;
        font-size: 1.1rem;
        font-weight: 600;
    }
}

.tiptap-modal-close {
    border: none;
    background: none;
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    color: #6c757d;
    padding: 0;

    &:hover {
        color: #212529;
    }
}

.tiptap-modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem;
}

.tiptap-media-loading,
.tiptap-media-empty {
    text-align: center;
    padding: 2rem;
    color: #6c757d;
    font-size: 0.95rem;
}

.tiptap-media-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 0.75rem;
}

.tiptap-media-item {
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 0.5rem;
    overflow: hidden;
    transition: border-color 0.15s, transform 0.15s;
    background: #f8f9fa;

    &:hover {
        border-color: #0455C0;
        transform: scale(1.03);
    }

    img {
        width: 100%;
        height: 100px;
        object-fit: cover;
        display: block;
    }

    .tiptap-media-name {
        display: block;
        padding: 0.25rem 0.5rem;
        font-size: 0.7rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        color: #495057;
    }
}

.tiptap-modal-footer {
    padding: 0.75rem 1.25rem;
    border-top: 1px solid #dee2e6;
}

.tiptap-url-insert {
    display: flex;
    gap: 0.5rem;

    input {
        flex: 1;
    }
}

// ─── Link Picker ────────────────────────────────────────────────────────────

.tiptap-link-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    width: 100%;
    border: none;
    background: transparent;
    padding: 0.45rem 0.75rem;
    font-size: 0.85rem;
    color: #212529;
    cursor: pointer;
    border-radius: 0.25rem;
    text-align: left;

    &:hover, &.is-selected {
        background: #e8ecf1;
    }

    &__title {
        font-weight: 500;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    &__url {
        font-size: 0.7rem;
        color: #6c757d;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 200px;
    }
}

// ─── Callout Menu (dropdown) ─────────────────────────────────────────────────

.tiptap-callout-menu {
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 0.5rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    padding: 0.25rem;
    min-width: 160px;

    &__item {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        width: 100%;
        border: none;
        background: transparent;
        padding: 0.5rem 0.75rem;
        font-size: 0.875rem;
        color: #212529;
        cursor: pointer;
        border-radius: 0.375rem;
        text-align: left;

        &:hover {
            background: #f0f0f0;
        }

        i {
            font-size: 1rem;
        }
    }
}

// ─── Table Context Bar (under toolbar) ──────────────────────────────────────

.tiptap-table-contextbar {
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 0.3rem 0.5rem;
    background: #eef2ff;
    border-bottom: 1px solid #c7d2fe;
    flex-wrap: wrap;

    &__btn {
        display: inline-flex;
        align-items: center;
        gap: 0.3rem;
        border: none;
        background: transparent;
        padding: 0.3rem 0.5rem;
        font-size: 0.75rem;
        color: #495057;
        cursor: pointer;
        border-radius: 0.25rem;
        white-space: nowrap;
        line-height: 1;

        &:hover {
            background: #dbeafe;
            color: #212529;
        }

        &.is-danger {
            color: #dc3545;

            &:hover {
                background: #fdf0f0;
                color: #b02a37;
            }
        }

        i {
            font-size: 0.7rem;
        }

        span {
            font-size: 0.7rem;
        }
    }

    &__sep {
        display: inline-block;
        width: 1px;
        height: 18px;
        background: #c7d2fe;
        margin: 0 2px;
    }
}

// ─── Slash Menu (/ commands) ─────────────────────────────────────────────────

.tiptap-slash-menu {
    position: absolute;
    z-index: 100;
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 0.5rem;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    padding: 0.25rem;
    min-width: 200px;
    max-height: 320px;
    overflow-y: auto;
}

.tiptap-slash-item {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    width: 100%;
    border: none;
    background: transparent;
    padding: 0.5rem 0.75rem;
    font-size: 0.875rem;
    color: #212529;
    cursor: pointer;
    border-radius: 0.375rem;
    text-align: left;

    &:hover,
    &.active {
        background: #e8ecf1;
    }

    i {
        font-size: 0.875rem;
        width: 1.25rem;
        text-align: center;
        color: #6366f1;
    }
}

.tiptap-slash-empty {
    padding: 0.75rem;
    font-size: 0.8125rem;
    color: #94a3b8;
    text-align: center;
}

// ─── Document Modal & Card ──────────────────────────────────────────────────

.tiptap-doc-list {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.tiptap-doc-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
    padding: 0.5rem 0.75rem;
    background: #fff;
    border: 1px solid transparent;
    border-radius: 6px;
    text-align: left;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s;

    &:hover {
        background: #f1f3f5;
        border-color: #ced4da;
    }

    &__icon {
        flex: 0 0 auto;
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.1rem;
        color: #495057;
    }

    &__body {
        display: flex;
        flex-direction: column;
        gap: 0.1rem;
        flex: 1;
        min-width: 0;
    }

    &__name {
        font-weight: 500;
        color: #212529;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    &__meta {
        font-size: 0.75rem;
        color: #6c757d;
        text-transform: uppercase;
        letter-spacing: 0.03em;
    }
}

// Carte document dans l'editeur ProseMirror (rendu inline du node)
.tiptap-editor-content {
    .block-document {
        display: flex;
        align-items: center;
        gap: 0.85rem;
        padding: 0.65rem 0.9rem;
        margin: 0.75rem 0;
        background: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        text-decoration: none;
        color: #212529;
        cursor: pointer;
        transition: background 0.15s, border-color 0.15s;

        &:hover {
            background: #e9ecef;
            border-color: #adb5bd;
        }

        &__icon {
            flex: 0 0 auto;
            width: 36px;
            height: 36px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.5rem;
            color: #495057;
        }

        &__body {
            display: flex;
            flex-direction: column;
            gap: 0.15rem;
            flex: 1;
            min-width: 0;
        }

        &__name {
            font-weight: 500;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        &__meta {
            font-size: 0.75rem;
            color: #6c757d;
            text-transform: uppercase;
            letter-spacing: 0.03em;
        }

        &__action {
            flex: 0 0 auto;
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #6c757d;
        }
    }
}

// ─── Inline icons (img src="/icons/...") ─────────────────────────────────────

.tiptap-editor-content img[src^="/icons/"] {
    display: inline-block;
    width: 1.2em;
    height: 1.2em;
    vertical-align: -0.15em;
    margin: 0 0.15em;
}

// ─── Responsive ──────────────────────────────────────────────────────────────

@media (max-width: 768px) {
    .tiptap-toolbar {
        gap: 1px;
        padding: 0.3rem;
    }

    .tiptap-toolbar-btn {
        width: 28px;
        height: 28px;
        font-size: 0.75rem;
    }

    .tiptap-editor-content {
        min-height: 300px;
        padding: 1rem;
    }

    .tiptap-modal-dialog {
        width: 95%;
        max-height: 90vh;
    }

    .tiptap-media-grid {
        grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    }
}
