/**
 * Rich Editor CSS
 * Custom contenteditable-based editor styling
 * Matches gtanexus.com design system
 */

/* ========================================
   Editor Container
   ======================================== */

.rich-editor {
    border: 1px solid #d1d5db;
    border-radius: 0.75rem;
    overflow: hidden;
    background: #ffffff;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.rich-editor:focus-within {
    border-color: #ec4899;
    box-shadow: 0 0 0 3px rgba(236, 72, 153, 0.1);
}

/* ========================================
   Toolbar
   ======================================== */

.rich-editor-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.25rem;
    padding: 0.5rem;
    background: #f9fafb;
    border-bottom: 1px solid #e5e7eb;
    user-select: none;
}

.rich-editor-toolbar .toolbar-group {
    display: flex;
    align-items: center;
    gap: 0.125rem;
}

.rich-editor-toolbar .toolbar-group + .toolbar-group {
    margin-left: 0.5rem;
    padding-left: 0.5rem;
    border-left: 1px solid #e5e7eb;
}

/* Toolbar Buttons */
.rich-editor-toolbar button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    padding: 0.375rem;
    border: none;
    border-radius: 0.375rem;
    background: transparent;
    color: #374151;
    cursor: pointer;
    transition: all 0.15s ease;
}

.rich-editor-toolbar button:hover {
    background: #fce7f3;
    color: #be185d;
}

.rich-editor-toolbar button.active {
    background: #fce7f3;
    color: #be185d;
}

.rich-editor-toolbar button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.rich-editor-toolbar button svg {
    width: 1rem;
    height: 1rem;
    flex-shrink: 0;
}

/* Separator */
.rich-editor-toolbar .toolbar-separator {
    width: 1px;
    height: 1.5rem;
    background: #e5e7eb;
    margin: 0 0.25rem;
}

/* ========================================
   Editable Content Area
   ======================================== */

.rich-editor-content {
    min-height: 200px;
    max-height: 70vh;
    padding: 1rem;
    overflow-y: auto;
    outline: none;
    font-size: 0.95rem;
    line-height: 1.7;
    color: #1f2937;
    background: #ffffff;
}

/* Placeholder */
.rich-editor-content:empty::before {
    content: attr(data-placeholder);
    color: #9ca3af;
    pointer-events: none;
    position: absolute;
}

.rich-editor-content[data-placeholder]:empty {
    position: relative;
}

/* Content Styling */
.rich-editor-content p,
.rich-editor-content div:not(.youtube-embed):not([class]) {
    margin: 0 0 1em 0;
    min-height: 1.5em; /* Ensure empty paragraphs have height */
}

.rich-editor-content p:last-child,
.rich-editor-content > div:last-child {
    margin-bottom: 0;
}

/* Empty line spacing */
.rich-editor-content br {
    display: block;
    content: "";
    margin-bottom: 0.5em;
}

.rich-editor-content h2 {
    font-size: 1.5rem;
    font-weight: 700;
    margin: 1.5em 0 0.75em 0;
    color: #111827;
}

.rich-editor-content h3 {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 1.25em 0 0.5em 0;
    color: #1f2937;
}

.rich-editor-content h2:first-child,
.rich-editor-content h3:first-child {
    margin-top: 0;
}

.rich-editor-content strong {
    font-weight: 600;
}

.rich-editor-content em {
    font-style: italic;
}

.rich-editor-content u {
    text-decoration: underline;
}

.rich-editor-content s {
    text-decoration: line-through;
}

.rich-editor-content a {
    color: #db2777;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.rich-editor-content a:hover {
    color: #be185d;
}

/* Lists */
.rich-editor-content ul,
.rich-editor-content ol {
    margin: 0 0 1em 0;
    padding-left: 1.5em;
}

.rich-editor-content ul {
    list-style-type: disc;
}

.rich-editor-content ol {
    list-style-type: decimal;
}

.rich-editor-content li {
    margin-bottom: 0.25em;
}

.rich-editor-content li:last-child {
    margin-bottom: 0;
}

/* Blockquote */
.rich-editor-content blockquote {
    margin: 1em 0;
    padding: 0.75rem 1rem;
    border-left: 4px solid #ec4899;
    background: #fdf2f8;
    color: #831843;
    border-radius: 0 0.5rem 0.5rem 0;
}

/* Code */
.rich-editor-content code {
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 0.875em;
    padding: 0.125em 0.375em;
    background: #f3f4f6;
    border-radius: 0.25rem;
    color: #be185d;
}

.rich-editor-content pre {
    margin: 1em 0;
    padding: 1rem;
    background: #1f2937;
    border-radius: 0.5rem;
    overflow-x: auto;
}

.rich-editor-content pre code {
    background: transparent;
    padding: 0;
    color: #e5e7eb;
    font-size: 0.875rem;
    line-height: 1.5;
}

/* Images */
.rich-editor-content img {
    max-width: 100%;
    height: auto;
    border-radius: 0.5rem;
    margin: 0.5em 0;
}

.rich-editor-content figure {
    margin: 1em 0;
    text-align: center;
}

.rich-editor-content figure img {
    margin: 0;
}

.rich-editor-content figcaption {
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: #6b7280;
    font-style: italic;
}

/* YouTube Embeds */
.rich-editor-content .youtube-embed {
    position: relative;
    width: 100%;
    max-width: 100%;
    margin: 1em 0;
    padding-bottom: 56.25%; /* 16:9 */
    height: 0;
    overflow: hidden;
    border-radius: 0.75rem;
    background: #000;
}

.rich-editor-content .youtube-embed iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: none;
    border-radius: 0.75rem;
}

/* ========================================
   HTML View (Admin)
   ======================================== */

.rich-editor-html {
    display: none;
    width: 100%;
    min-height: 200px;
    max-height: 70vh;
    padding: 1rem;
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 0.875rem;
    line-height: 1.5;
    background: #1f2937;
    color: #e5e7eb;
    border: none;
    resize: vertical;
    outline: none;
    tab-size: 2;
}

.rich-editor-html:focus {
    outline: none;
}

/* When HTML mode is active */
.rich-editor.html-mode .rich-editor-content {
    display: none;
}

.rich-editor.html-mode .rich-editor-html {
    display: block;
}

.rich-editor.html-mode .rich-editor-toolbar button[data-command="htmlView"] {
    background: #be185d;
    color: #ffffff;
}

/* ========================================
   Dark Mode
   ======================================== */

.dark .rich-editor {
    border-color: #374151;
    background: #111827;
}

.dark .rich-editor:focus-within {
    border-color: #ec4899;
    box-shadow: 0 0 0 3px rgba(236, 72, 153, 0.2);
}

.dark .rich-editor-toolbar {
    background: #1f2937;
    border-color: #374151;
}

.dark .rich-editor-toolbar .toolbar-group + .toolbar-group {
    border-color: #374151;
}

.dark .rich-editor-toolbar .toolbar-separator {
    background: #374151;
}

.dark .rich-editor-toolbar button {
    color: #d1d5db;
}

.dark .rich-editor-toolbar button:hover {
    background: rgba(236, 72, 153, 0.2);
    color: #f9a8d4;
}

.dark .rich-editor-toolbar button.active {
    background: rgba(236, 72, 153, 0.2);
    color: #f9a8d4;
}

.dark .rich-editor-content {
    background: #111827;
    color: #e5e7eb;
}

.dark .rich-editor-content:empty::before {
    color: #6b7280;
}

.dark .rich-editor-content h2,
.dark .rich-editor-content h3 {
    color: #f3f4f6;
}

.dark .rich-editor-content a {
    color: #f472b6;
}

.dark .rich-editor-content a:hover {
    color: #f9a8d4;
}

.dark .rich-editor-content blockquote {
    background: rgba(236, 72, 153, 0.1);
    border-left-color: #ec4899;
    color: #f9a8d4;
}

.dark .rich-editor-content code {
    background: #374151;
    color: #f472b6;
}

.dark .rich-editor-content pre {
    background: #0f172a;
}

.dark .rich-editor-content figcaption {
    color: #9ca3af;
}

.dark .rich-editor-html {
    background: #0f172a;
    color: #e5e7eb;
}

/* ========================================
   Responsive
   ======================================== */

@media (max-width: 640px) {
    .rich-editor-toolbar {
        padding: 0.375rem;
        gap: 0.125rem;
    }

    .rich-editor-toolbar .toolbar-group + .toolbar-group {
        margin-left: 0.25rem;
        padding-left: 0.25rem;
    }

    .rich-editor-toolbar button {
        width: 1.75rem;
        height: 1.75rem;
        padding: 0.25rem;
    }

    .rich-editor-toolbar button svg {
        width: 0.875rem;
        height: 0.875rem;
    }

    .rich-editor-content {
        padding: 0.75rem;
        font-size: 0.9rem;
    }

    .rich-editor-html {
        padding: 0.75rem;
        font-size: 0.8rem;
    }
}

/* ========================================
   Focus States & Accessibility
   ======================================== */

.rich-editor-toolbar button:focus-visible {
    outline: 2px solid #ec4899;
    outline-offset: 1px;
}

.rich-editor-content:focus-visible {
    outline: none;
}

/* Selected content highlight */
.rich-editor-content ::selection {
    background: rgba(236, 72, 153, 0.3);
}

.dark .rich-editor-content ::selection {
    background: rgba(236, 72, 153, 0.4);
}

/* ========================================
   Loading State
   ======================================== */

.rich-editor.loading {
    opacity: 0.6;
    pointer-events: none;
}

.rich-editor.loading::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ========================================
   Error State
   ======================================== */

.rich-editor.error {
    border-color: #ef4444;
}

.rich-editor.error:focus-within {
    border-color: #ef4444;
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
}
