/* Custom Trix Editor Styling */
/* This file contains custom Trix styles that cannot use Tailwind @apply directives */

/* ==========================================================================
   Trix Editor Selection Styling - Show text color through selection
   Following Google Docs UX pattern
   ========================================================================== */

/* Semi-transparent selection to show underlying text color */
trix-editor::selection,
trix-editor *::selection {
    background: rgba(51, 102, 204, 0.3) !important;
    color: inherit !important;
}

/* Explicitly style links to ensure they look like links and clean up on unlink */
trix-editor a,
.trix-content a,
.dark trix-editor a,
.dark .trix-content a {
    color: #2563eb !important;
    /* Standard Blue */
    /* Robust Underline Strategy: Use border-bottom to guarantee visibility */
    text-decoration: none !important;
    border-bottom: 1px solid #2563eb !important;
    padding-bottom: 0px !important;
    cursor: pointer;
}

trix-editor a:visited,
.trix-content a:visited,
.dark trix-editor a:visited,
.dark .trix-content a:visited {
    color: #2563eb !important;
}

/* ==========================================================================
   Underline Styling - Fix for Tailwind Preflight Reset
   ========================================================================== */
trix-editor u,
.trix-content u {
    text-decoration: underline !important;
}

trix-editor::-moz-selection,
trix-editor *::-moz-selection {
    background: rgba(51, 102, 204, 0.3) !important;
    color: inherit !important;
}

/* Dark mode selection */
.dark trix-editor::selection,
.dark trix-editor *::selection {
    background: rgba(66, 133, 244, 0.4) !important;
    color: inherit !important;
}

.dark trix-editor::-moz-selection,
.dark trix-editor *::-moz-selection {
    background: rgba(66, 133, 244, 0.4) !important;
    color: inherit !important;
}


/* Trix Editor Sticky Toolbar */
trix-toolbar {
    position: sticky !important;
    top: 0 !important;
    z-index: 20 !important;
    background-color: #ffffff !important;
    border: 1px solid #d1d5db !important;
    border-radius: 0.5rem 0.5rem 0 0 !important;
    padding: 0.75rem !important;
    box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1) !important;
    -webkit-transform: translateZ(0) !important;
    transform: translateZ(0) !important;
    backface-visibility: hidden !important;
}

@media (max-width: 1024px) {
    trix-toolbar {
        top: 0 !important;
        padding: 0.5rem !important;
    }
}

/* Toolbar Buttons — applies to both Trix default (.trix-button) and custom (.trix-btn) */
trix-toolbar .trix-button,
trix-toolbar .trix-btn {
    background-color: #f9fafb !important;
    border: 1px solid #e5e7eb !important;
    border-radius: 6px !important;
    transition: all 0.15s ease !important;
    position: relative !important;
    min-width: 2.5rem !important;
    min-height: 2.5rem !important;
    padding: 0.5rem !important;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #374151;
    outline: none !important;
}

trix-toolbar .trix-button:focus,
trix-toolbar .trix-btn:focus,
trix-toolbar .trix-button:focus-visible,
trix-toolbar .trix-btn:focus-visible {
    outline: none !important;
    box-shadow: 0 0 0 2px hsl(var(--primary) / 0.3) !important;
}

trix-toolbar .trix-button:hover,
trix-toolbar .trix-btn:hover {
    background-color: #f3f4f6 !important;
    border-color: #d1d5db !important;
    transform: translateY(-1px) !important;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
}

trix-toolbar .trix-button:active,
trix-toolbar .trix-btn:active {
    transform: translateY(0) !important;
    box-shadow: none !important;
}

trix-toolbar .trix-button.trix-active {
    background-color: #10b981 !important;
    border-color: #059669 !important;
    color: white !important;
}

trix-toolbar .trix-button.trix-active::before {
    filter: brightness(0) invert(1) !important;
}

/* Markdown Toggle Button */
trix-toolbar .trix-md-toggle {
    font-weight: 700;
    font-size: 0.75rem;
    letter-spacing: -0.02em;
}

trix-toolbar .trix-md-toggle .trix-md-label {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

trix-toolbar .trix-md-toggle.active {
    background-color: #10b981 !important;
    border-color: #059669 !important;
    color: white !important;
}

.dark trix-toolbar .trix-btn.trix-md-toggle.active {
    background-color: #2dd4bf !important;
    border-color: #14b8a6 !important;
    color: #000000 !important;
}

/* Toolbar Dropdowns (.trix-select) */
trix-toolbar .trix-select {
    background-color: #f9fafb !important;
    border: 1px solid #e5e7eb !important;
    border-radius: 6px !important;
    padding: 0.375rem 0.5rem !important;
    font-size: 0.8125rem !important;
    color: #374151 !important;
    cursor: pointer;
    min-height: 2.5rem !important;
    transition: border-color 0.15s ease !important;
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23374151' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.5rem center;
    padding-right: 1.75rem !important;
}

trix-toolbar .trix-select:hover {
    border-color: #d1d5db !important;
}

trix-toolbar .trix-select:focus {
    outline: none !important;
    border-color: hsl(var(--primary)) !important;
    box-shadow: 0 0 0 2px hsl(var(--primary) / 0.2) !important;
}

/* Dark Mode — Toolbar */
.dark trix-toolbar {
    background-color: #18181b !important;
    border-color: #3f3f46 !important;
}

.dark trix-toolbar .trix-button,
.dark trix-toolbar .trix-btn {
    background-color: #27272a !important;
    border: 1px solid #3f3f46 !important;
    color: #fafafa !important;
}

.dark trix-toolbar .trix-button::before {
    filter: invert(1) brightness(1.8) !important;
}

.dark trix-toolbar .trix-button:hover,
.dark trix-toolbar .trix-btn:hover {
    background-color: #3f3f46 !important;
    border-color: #52525b !important;
    color: #fafafa !important;
    transform: translateY(-1px) !important;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4) !important;
}

.dark trix-toolbar .trix-button.trix-active {
    background-color: hsl(var(--primary)) !important;
    border-color: hsl(var(--primary)) !important;
    color: #000000 !important;
}

.dark trix-toolbar .trix-button.trix-active::before {
    filter: brightness(0) invert(1) !important;
}

/* Dark Mode — Toolbar Dropdowns */
.dark trix-toolbar .trix-select {
    background-color: #27272a !important;
    border-color: #3f3f46 !important;
    color: #fafafa !important;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23a1a1aa' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
}

.dark trix-toolbar .trix-select:hover {
    border-color: #52525b !important;
}

.dark trix-toolbar .trix-select option {
    background-color: #18181b;
    color: #fafafa;
}

.dark trix-editor {
    background-color: #18181b !important;
    color: #fafafa !important;
    border-color: #3f3f46 !important;
}

/* Toolbar Layout — button row, sections, separators */
.trix-button-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
}

.trix-section {
    display: inline-flex;
    align-items: center;
    gap: 2px;
}

.trix-separator {
    width: 1px;
    height: 1.5rem;
    background: #d1d5db;
    margin: 0 4px;
    flex-shrink: 0;
}

.dark .trix-separator {
    background: #3f3f46;
}

/* ==========================================================================
   Trix Link Dialog
   ========================================================================== */

/* Light mode base improvements */
trix-toolbar .trix-dialog {
    background: #ffffff;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    padding: 1rem;
    z-index: 50;
}

trix-toolbar .trix-input--dialog {
    font-size: 0.875rem;
    padding: 0.625rem 0.875rem;
    border: 1px solid #d1d5db;
    border-radius: 0.375rem;
    background-color: #ffffff;
    color: #1f2937;
    min-width: 280px;
}

trix-toolbar .trix-input--dialog:focus {
    outline: none;
    border-color: #10b981;
    box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.2);
}

trix-toolbar .trix-button--dialog {
    font-size: 0.875rem;
    padding: 0.625rem 1rem;
    border-radius: 0.375rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
}

/* Link button styling */
trix-toolbar .trix-dialog__link-fields .trix-button-group .trix-button--dialog {
    background-color: #047857;
    /* Emerald-700 for AAA contrast with white */
    color: white;
    border: none;
    margin-left: 0.5rem;
}

trix-toolbar .trix-dialog__link-fields .trix-button-group .trix-button--dialog:hover {
    background-color: #065f46;
    /* Emerald-800 */
}

/* Unlink button - secondary style */
trix-toolbar .trix-dialog__link-fields .trix-button-group .trix-button--dialog:last-child {
    background-color: #f3f4f6;
    color: #374151;
    border: 1px solid #d1d5db;
}

trix-toolbar .trix-dialog__link-fields .trix-button-group .trix-button--dialog:last-child:hover {
    background-color: #e5e7eb;
}

/* Dark Mode Link Dialog */
.dark trix-toolbar .trix-dialog {
    background: #18181b;
    border-color: #3f3f46;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}

.dark trix-toolbar .trix-input--dialog {
    background-color: #09090b;
    border-color: #3f3f46;
    color: #fafafa;
}

.dark trix-toolbar .trix-input--dialog::placeholder {
    color: #a1a1aa;
}

.dark trix-toolbar .trix-input--dialog:focus {
    border-color: hsl(var(--primary));
    box-shadow: 0 0 0 3px hsl(var(--primary) / 0.25);
    outline: none;
}

.dark trix-toolbar .trix-input--dialog:invalid,
.dark trix-toolbar .trix-input--dialog:focus:invalid {
    background-color: #09090b;
    border-color: hsl(var(--destructive));
    color: #fafafa;
    box-shadow: none;
}

/* Ensure decent contrast even if browser forces something */
trix-toolbar .trix-input--dialog:invalid {
    background-color: #ffffff;
    box-shadow: none;
}

.dark trix-toolbar .trix-dialog__link-fields .trix-button-group .trix-button--dialog {
    background-color: hsl(var(--primary));
    color: #000000;
}

.dark trix-toolbar .trix-dialog__link-fields .trix-button-group .trix-button--dialog:last-child {
    background-color: #27272a;
    color: #fafafa;
    border-color: #3f3f46;
}

.dark trix-toolbar .trix-dialog__link-fields .trix-button-group .trix-button--dialog:last-child:hover {
    background-color: #3f3f46;
}

/* ==========================================================================
   Color Picker & Highlight Picker Dropdowns
   Portaled to document.body by JS so position:fixed is always
   relative to the viewport — no containing-block issues from
   ancestor transform/filter/will-change.
   ========================================================================== */
.color-picker-dropdown,
.highlight-picker-dropdown {
    position: fixed;
    z-index: 9999;
    background: #ffffff;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    padding: 0.75rem;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    min-width: 240px;
    max-width: calc(100vw - 20px);
    max-height: 80vh;
    overflow-y: auto;
}

.color-picker-dropdown .color-picker-header,
.highlight-picker-dropdown .color-picker-header {
    font-size: 0.6875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #64748b;
    margin-bottom: 0.5rem;
}

.color-picker-dropdown .color-grid-label,
.highlight-picker-dropdown .color-grid-label {
    font-size: 0.6875rem;
    font-weight: 500;
    color: #94a3b8;
    margin: 0.5rem 0 0.375rem;
}

.color-picker-dropdown .color-grid,
.highlight-picker-dropdown .color-grid {
    display: grid;
    grid-template-columns: repeat(10, 1fr);
    gap: 3px;
}

.color-picker-dropdown .color-swatch,
.highlight-picker-dropdown .color-swatch {
    width: 22px;
    height: 22px;
    border-radius: 3px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    cursor: pointer;
    transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.color-picker-dropdown .color-swatch:hover,
.highlight-picker-dropdown .color-swatch:hover {
    transform: scale(1.2);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    z-index: 1;
    position: relative;
}

.color-picker-dropdown .custom-color-btn,
.highlight-picker-dropdown .custom-color-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    padding: 0.5rem;
    margin-top: 0.5rem;
    border: 1px solid #e5e7eb;
    border-radius: 0.375rem;
    background: #f9fafb;
    color: #374151;
    font-size: 0.8125rem;
    cursor: pointer;
    transition: background 0.15s;
}

.color-picker-dropdown .custom-color-btn:hover,
.highlight-picker-dropdown .custom-color-btn:hover {
    background: #f1f5f9;
}

/* Dark mode */
.dark .color-picker-dropdown,
.dark .highlight-picker-dropdown {
    background: #18181b;
    border-color: #3f3f46;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}

.dark .color-picker-dropdown .color-picker-header,
.dark .highlight-picker-dropdown .color-picker-header {
    color: #a1a1aa;
}

.dark .color-picker-dropdown .color-grid-label,
.dark .highlight-picker-dropdown .color-grid-label {
    color: #71717a;
}

.dark .color-picker-dropdown .color-swatch,
.dark .highlight-picker-dropdown .color-swatch {
    border-color: rgba(255, 255, 255, 0.08);
}

.dark .color-picker-dropdown .custom-color-btn,
.dark .highlight-picker-dropdown .custom-color-btn {
    background: #27272a;
    border-color: #3f3f46;
    color: #fafafa;
}

.dark .color-picker-dropdown .custom-color-btn:hover,
.dark .highlight-picker-dropdown .custom-color-btn:hover {
    background: #3f3f46;
}

/* ==========================================================================
   Color Indicator Bars - Ensure Visibility
   ========================================================================== */

/* Ensure color picker buttons have proper positioning for indicators */
#color-picker-toggle,
#highlight-picker-toggle {
    position: relative !important;
    overflow: visible !important;
}

/* Color indicator bars — !important needed to override JS-injected inline styles */
#text-color-indicator {
    position: absolute !important;
    bottom: 3px !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    width: 14px !important;
    height: 3px !important;
    border-radius: 1px !important;
    z-index: 10 !important;
    pointer-events: none !important;
    background: #374151 !important;
    box-shadow: none !important;
}

#highlight-color-indicator {
    position: absolute !important;
    bottom: 3px !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    width: 14px !important;
    height: 3px !important;
    border-radius: 1px !important;
    z-index: 10 !important;
    pointer-events: none !important;
    background: #fbbf24 !important;
}

/* Highlight picker specific - 5 column grid for highlight colors */
.highlight-picker-dropdown .color-grid {
    grid-template-columns: repeat(5, 1fr) !important;
    gap: 8px !important;
}

.highlight-picker-dropdown .color-swatch {
    width: 28px !important;
    height: 28px !important;
    border-radius: 4px !important;
}

/* ==========================================================================
   Semantic HTML Paragraph Styles for Trix Content
   ========================================================================== */

/* Paragraph styling in both editor and rendered content */
trix-editor p,
.trix-content p {
    margin: 0 0 1em 0;
    /* Standard paragraph spacing */
    min-height: 1em;
    /* Prevent empty paragraph collapse */
}

/* Remove bottom margin from last paragraph */
trix-editor p:last-child,
.trix-content p:last-child {
    margin-bottom: 0;
}

/* Empty paragraph placeholder (for cursor visibility in editor) */
trix-editor p:empty::before {
    content: '\00a0';
    /* Non-breaking space */
    visibility: hidden;
}

/* Ensure semantic block elements have proper margins */
.trix-content article,
.trix-content section,
.trix-content figure {
    margin: 1.5em 0;
}

.trix-content figcaption {
    font-size: 0.875em;
    color: #6b7280;
    margin-top: 0.5em;
    text-align: center;
}

.dark .trix-content figcaption {
    color: #a1a1aa;
}

/* ==========================================================================
   Public Content RTL Support (Mirrors Trix Editor Styles)
   ========================================================================== */

/* Blockquotes are styled in tailwind/application.css using CSS variables.
   Only RTL border-flipping rules remain here. */

/* Code Blocks - Standard code block styling (GitHub/Medium style) */
trix-editor pre,
.trix-content pre {
    display: block;
    width: 100%;
    vertical-align: top;
    font-family: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
    font-size: 0.875em;
    line-height: 1.7;
    padding: 1rem 1.25rem;
    white-space: pre;
    background-color: #f6f8fa;
    border: 1px solid #d1d5db;
    overflow-x: auto;
    border-radius: 0.5rem;
    margin: 1.5rem 0;
    color: #1f2937;
}


.dark trix-editor pre,
.dark .trix-content pre {
    background-color: #09090b;
    color: #fafafa;
    border: 1px solid #3f3f46;
}

/* Default: LTR/Left alignment on container only - do not force on all children */
.trix-content {
    text-align: left;
    direction: ltr;
}

/* Standard RTL blocks */
.trix-content *[dir="rtl"] {
    direction: rtl;
    text-align: right;
    unicode-bidi: isolate;
    /* Font family handled in fonts.css */
}

/* ==========================================================================
   BASE HEADING STYLES - Apply to ALL headings regardless of language
   These provide the foundation that RTL-specific rules can build upon
   ========================================================================== */

/* H1 - Extra Large (English default) */
trix-editor h1,
.trix-content h1 {
    font-size: 2.25rem !important;
    font-weight: 700 !important;
    line-height: 1.2 !important;
    margin: 1.5rem 0 1rem 0 !important;
}

/* H2 - Large */
trix-editor h2,
.trix-content h2 {
    font-size: 1.875rem !important;
    font-weight: 700 !important;
    line-height: 1.3 !important;
    margin: 1.25rem 0 0.875rem 0 !important;
}

/* H3 - Medium-Large */
trix-editor h3,
.trix-content h3 {
    font-size: 1.5rem !important;
    font-weight: 600 !important;
    line-height: 1.4 !important;
    margin: 1rem 0 0.75rem 0 !important;
}

/* H4 - Medium */
trix-editor h4,
.trix-content h4 {
    font-size: 1.25rem !important;
    font-weight: 600 !important;
    line-height: 1.5 !important;
    margin: 0.875rem 0 0.625rem 0 !important;
}

/* H5 - Small */
trix-editor h5,
.trix-content h5 {
    font-size: 1.125rem !important;
    font-weight: 600 !important;
    line-height: 1.5 !important;
    margin: 0.75rem 0 0.5rem 0 !important;
}

/* H6 - Extra Small */
trix-editor h6,
.trix-content h6 {
    font-size: 1rem !important;
    font-weight: 600 !important;
    line-height: 1.5 !important;
    margin: 0.75rem 0 0.5rem 0 !important;
}

/* ==========================================================================
   RTL Headings - Comprehensive Rules for All Scenarios

   Handles ALL possible HTML structures Trix can create:
   1. <h1 dir="rtl">...</h1>
   2. <h1 style="direction: rtl">...</h1>
   3. <h1><span dir="rtl">...</span></h1>
   4. <h1><span style="direction: rtl">...</span></h1>

   Default font: Al Qalam (can be overridden by user font selection)
   ========================================================================== */

/* Case 1: Heading element itself has dir="rtl" or style="direction: rtl" */
.trix-content h1[dir="rtl"],
.trix-content h2[dir="rtl"],
.trix-content h3[dir="rtl"],
.trix-content h4[dir="rtl"],
.trix-content h5[dir="rtl"],
.trix-content h6[dir="rtl"],
trix-editor h1[dir="rtl"],
trix-editor h2[dir="rtl"],
trix-editor h3[dir="rtl"],
trix-editor h4[dir="rtl"],
trix-editor h5[dir="rtl"],
trix-editor h6[dir="rtl"],
.trix-content h1[style*="direction:rtl"],
.trix-content h1[style*="direction: rtl"],
.trix-content h2[style*="direction:rtl"],
.trix-content h2[style*="direction: rtl"],
.trix-content h3[style*="direction:rtl"],
.trix-content h3[style*="direction: rtl"],
.trix-content h4[style*="direction:rtl"],
.trix-content h4[style*="direction: rtl"],
.trix-content h5[style*="direction:rtl"],
.trix-content h5[style*="direction: rtl"],
.trix-content h6[style*="direction:rtl"],
.trix-content h6[style*="direction: rtl"],
trix-editor h1[style*="direction:rtl"],
trix-editor h1[style*="direction: rtl"],
trix-editor h2[style*="direction:rtl"],
trix-editor h2[style*="direction: rtl"],
trix-editor h3[style*="direction:rtl"],
trix-editor h3[style*="direction: rtl"],
trix-editor h4[style*="direction:rtl"],
trix-editor h4[style*="direction: rtl"],
trix-editor h5[style*="direction:rtl"],
trix-editor h5[style*="direction: rtl"],
trix-editor h6[style*="direction:rtl"],
trix-editor h6[style*="direction: rtl"] {
    font-family: 'Al Qalam', 'Scheherazade New', 'Amiri', 'Noto Naskh Arabic', serif;
    direction: rtl !important;
    text-align: right !important;
    unicode-bidi: isolate;
}

/* Case 2: Heading contains RTL child elements (using :has selector) */
.trix-content h1:has([dir="rtl"]),
.trix-content h1:has([style*="direction:rtl"]),
.trix-content h1:has([style*="direction: rtl"]),
.trix-content h2:has([dir="rtl"]),
.trix-content h2:has([style*="direction:rtl"]),
.trix-content h2:has([style*="direction: rtl"]),
.trix-content h3:has([dir="rtl"]),
.trix-content h3:has([style*="direction:rtl"]),
.trix-content h3:has([style*="direction: rtl"]),
trix-editor h1:has([dir="rtl"]),
trix-editor h1:has([style*="direction:rtl"]),
trix-editor h1:has([style*="direction: rtl"]),
trix-editor h2:has([dir="rtl"]),
trix-editor h2:has([style*="direction:rtl"]),
trix-editor h2:has([style*="direction: rtl"]),
trix-editor h3:has([dir="rtl"]),
trix-editor h3:has([style*="direction:rtl"]),
trix-editor h3:has([style*="direction: rtl"]) {
    font-family: 'Al Qalam', 'Scheherazade New', 'Amiri', 'Noto Naskh Arabic', serif;
    direction: rtl !important;
    text-align: right !important;
    unicode-bidi: isolate;
}

/* ==========================================================================
   RTL Heading Sizes - Larger for Arabic readability
   These sizes apply to ALL RTL heading variations
   ========================================================================== */

/* H1 - Arabic (Extra Large) */
.trix-content h1[dir="rtl"],
.trix-content h1[style*="direction:rtl"],
.trix-content h1[style*="direction: rtl"],
.trix-content h1:has([dir="rtl"]),
.trix-content h1:has([style*="direction:rtl"]),
.trix-content h1:has([style*="direction: rtl"]),
trix-editor h1[dir="rtl"],
trix-editor h1[style*="direction:rtl"],
trix-editor h1[style*="direction: rtl"],
trix-editor h1:has([dir="rtl"]),
trix-editor h1:has([style*="direction:rtl"]),
trix-editor h1:has([style*="direction: rtl"]) {
    font-size: 2.5rem !important;
    line-height: 1.4 !important;
    font-weight: bold;
}

/* H2 - Arabic (Large) */
.trix-content h2[dir="rtl"],
.trix-content h2[style*="direction:rtl"],
.trix-content h2[style*="direction: rtl"],
.trix-content h2:has([dir="rtl"]),
.trix-content h2:has([style*="direction:rtl"]),
.trix-content h2:has([style*="direction: rtl"]),
trix-editor h2[dir="rtl"],
trix-editor h2[style*="direction:rtl"],
trix-editor h2[style*="direction: rtl"],
trix-editor h2:has([dir="rtl"]),
trix-editor h2:has([style*="direction:rtl"]),
trix-editor h2:has([style*="direction: rtl"]) {
    font-size: 2rem !important;
    line-height: 1.5 !important;
    font-weight: bold;
}

/* H3 - Arabic (Medium-Large) */
.trix-content h3[dir="rtl"],
.trix-content h3[style*="direction:rtl"],
.trix-content h3[style*="direction: rtl"],
.trix-content h3:has([dir="rtl"]),
.trix-content h3:has([style*="direction:rtl"]),
.trix-content h3:has([style*="direction: rtl"]),
trix-editor h3[dir="rtl"],
trix-editor h3[style*="direction:rtl"],
trix-editor h3[style*="direction: rtl"],
trix-editor h3:has([dir="rtl"]),
trix-editor h3:has([style*="direction:rtl"]),
trix-editor h3:has([style*="direction: rtl"]) {
    font-size: 1.75rem !important;
    line-height: 1.6 !important;
    font-weight: bold;
}

/* H4/H5/H6 - Arabic (Medium) */
.trix-content h4[dir="rtl"],
.trix-content h4[style*="direction:rtl"],
.trix-content h4[style*="direction: rtl"],
.trix-content h4:has([dir="rtl"]),
.trix-content h4:has([style*="direction:rtl"]),
.trix-content h4:has([style*="direction: rtl"]),
.trix-content h5[dir="rtl"],
.trix-content h5[style*="direction:rtl"],
.trix-content h5[style*="direction: rtl"],
.trix-content h5:has([dir="rtl"]),
.trix-content h5:has([style*="direction:rtl"]),
.trix-content h5:has([style*="direction: rtl"]),
.trix-content h6[dir="rtl"],
.trix-content h6[style*="direction:rtl"],
.trix-content h6[style*="direction: rtl"],
.trix-content h6:has([dir="rtl"]),
.trix-content h6:has([style*="direction:rtl"]),
.trix-content h6:has([style*="direction: rtl"]),
trix-editor h4[dir="rtl"],
trix-editor h4[style*="direction:rtl"],
trix-editor h4[style*="direction: rtl"],
trix-editor h4:has([dir="rtl"]),
trix-editor h4:has([style*="direction:rtl"]),
trix-editor h4:has([style*="direction: rtl"]),
trix-editor h5[dir="rtl"],
trix-editor h5[style*="direction:rtl"],
trix-editor h5[style*="direction: rtl"],
trix-editor h5:has([dir="rtl"]),
trix-editor h5:has([style*="direction:rtl"]),
trix-editor h5:has([style*="direction: rtl"]),
trix-editor h6[dir="rtl"],
trix-editor h6[style*="direction:rtl"],
trix-editor h6[style*="direction: rtl"],
trix-editor h6:has([dir="rtl"]),
trix-editor h6:has([style*="direction:rtl"]),
trix-editor h6:has([style*="direction: rtl"]) {
    font-size: 1.25rem !important;
    line-height: 1.6 !important;
    font-weight: bold;
}

/* ==========================================================================
   Direct RTL Span Targeting Inside Headings
   Trix creates: <h1><span style="direction: rtl; font-family: ...;">Arabic</span></h1>
   IMPORTANT: Only set font-family here, NOT font-size
   The heading element itself controls the size via h1, h2, h3 rules above
   ========================================================================== */

/* RTL spans inside headings — default Arabic font (no font-size, heading controls size) */
trix-editor h1 [style*="direction: rtl"]:not([style*="font-family"]),
trix-editor h1 [style*="direction:rtl"]:not([style*="font-family"]),
trix-editor h2 [style*="direction: rtl"]:not([style*="font-family"]),
trix-editor h2 [style*="direction:rtl"]:not([style*="font-family"]),
trix-editor h3 [style*="direction: rtl"]:not([style*="font-family"]),
trix-editor h3 [style*="direction:rtl"]:not([style*="font-family"]),
trix-editor h4 [style*="direction: rtl"]:not([style*="font-family"]),
trix-editor h4 [style*="direction:rtl"]:not([style*="font-family"]),
trix-editor h5 [style*="direction: rtl"]:not([style*="font-family"]),
trix-editor h5 [style*="direction:rtl"]:not([style*="font-family"]),
trix-editor h6 [style*="direction: rtl"]:not([style*="font-family"]),
trix-editor h6 [style*="direction:rtl"]:not([style*="font-family"]),
.trix-content h1 [style*="direction: rtl"]:not([style*="font-family"]),
.trix-content h1 [style*="direction:rtl"]:not([style*="font-family"]),
.trix-content h2 [style*="direction: rtl"]:not([style*="font-family"]),
.trix-content h2 [style*="direction:rtl"]:not([style*="font-family"]),
.trix-content h3 [style*="direction: rtl"]:not([style*="font-family"]),
.trix-content h3 [style*="direction:rtl"]:not([style*="font-family"]),
.trix-content h4 [style*="direction: rtl"]:not([style*="font-family"]),
.trix-content h4 [style*="direction:rtl"]:not([style*="font-family"]),
.trix-content h5 [style*="direction: rtl"]:not([style*="font-family"]),
.trix-content h5 [style*="direction:rtl"]:not([style*="font-family"]),
.trix-content h6 [style*="direction: rtl"]:not([style*="font-family"]),
.trix-content h6 [style*="direction:rtl"]:not([style*="font-family"]) {
    /* Only set font-family - heading element controls size */
    font-family: 'Al Qalam', 'Scheherazade New', 'Amiri', 'Noto Naskh Arabic', serif;
    /* Inherit size from parent heading */
    font-size: inherit !important;
    line-height: inherit !important;
}

/* RTL Blockquotes - flip border to right side.
   Trix stores direction via inline style (style="direction: rtl"), not dir attribute.
   Match both "direction: rtl" (with space) and "direction:rtl" (without). */
.trix-content blockquote:has(*[style*="direction: rtl"]),
.trix-content blockquote:has(*[style*="direction:rtl"]),
trix-editor blockquote:has(*[style*="direction: rtl"]),
trix-editor blockquote:has(*[style*="direction:rtl"]) {
    border-left: none;
    border-right: 4px solid var(--color-primary);
    border-radius: 0.5rem 0 0 0.5rem;
    text-align: right;
}

/* Hide scrollbar utility */
.no-scrollbar::-webkit-scrollbar {
    display: none;
}

.no-scrollbar {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

/* ==========================================================================
   Rich Text Gallery Layout Enhancements
   (Base attachment styles are in actiontext.css)
   ========================================================================== */

/* Gallery layout improvements */
.trix-content .attachment-gallery {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    margin: 1.5rem 0;
}

.trix-content .attachment-gallery .attachment {
    margin: 0;
    flex: 1 1 calc(33.333% - 1rem);
    min-width: 200px;
}

.trix-content .attachment-gallery .attachment img {
    width: 100%;
    height: auto;
    object-fit: cover;
    aspect-ratio: 4/3;
}

/* Two-item gallery */
.trix-content .attachment-gallery.attachment-gallery--2 .attachment {
    flex: 1 1 calc(50% - 0.5rem);
}

/* Four-item gallery */
.trix-content .attachment-gallery.attachment-gallery--4 .attachment {
    flex: 1 1 calc(50% - 0.5rem);
}

/* Responsive gallery adjustments */
@media (max-width: 640px) {
    .trix-content .attachment-gallery .attachment {
        flex: 1 1 100%;
        min-width: 100%;
    }

    .trix-content .attachment-gallery .attachment img {
        aspect-ratio: 16/9;
    }
}

/* ==========================================================================
   Mobile Trix Toolbar Optimization
   ========================================================================== */

/* Tablet and below - Wrapping toolbar */
@media (max-width: 768px) {
    trix-toolbar {
        top: 0 !important;
        border-radius: 0 !important;
        padding: 0.25rem 0.5rem !important;
        overflow: visible !important;
        max-width: 100vw !important;
    }

    .trix-button-row {
        gap: 3px !important;
    }

    .trix-separator {
        width: 1px !important;
        height: 20px !important;
        margin: 0 2px !important;
    }

    trix-toolbar .trix-button,
    trix-toolbar .trix-btn {
        min-width: 36px !important;
        min-height: 36px !important;
        padding: 4px !important;
    }

    trix-toolbar .trix-select {
        min-height: 36px !important;
        font-size: 0.75rem !important;
        padding: 0.25rem 1.5rem 0.25rem 0.375rem !important;
    }

    /* Font dropdown narrower on mobile */
    trix-toolbar .font-dropdown {
        max-width: 100px !important;
    }
}

/* ========================================================
   Custom Attachment Containers (Editor + Public Pages)

   DOM STRUCTURE:
     Editor: <figure class="attachment attachment--content" data-trix-attachment='...'>
     Public: <figure class="attachment attachment--content"> via _content_attachment.html.erb

   Content HTML in the editor is sanitized by Trix's HTMLSanitizer
   (only style/href/src/width/height/class/data-trix-* survive).
   On public pages, ActionText's own sanitizer applies (configured
   in config/initializers/action_text.rb).
   ======================================================== */

/* Block-level, full-width figures for custom attachments — public pages */
.trix-content figure.attachment--content {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0.5rem 0;
}

/* Block-level, full-width figures for custom attachments — editor */
trix-editor figure[data-trix-attachment*="vnd.table"],
trix-editor figure[data-trix-attachment*="vnd.mermaid"] {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    cursor: pointer;
    position: relative;
    transition: outline 0.15s ease;
    border-radius: var(--radius, 0.75rem);
    margin: 0.5rem 0;
}

/* Content HTML is a direct child of the figure — make it block/full-width */
trix-editor figure[data-trix-attachment*="vnd.table"] > * {
    display: block !important;
    width: 100% !important;
}

trix-editor figure[data-trix-attachment*="vnd.mermaid"] > * {
    display: block !important;
}

/* Mermaid diagrams use base64 <img> tags to bypass Trix's HTMLSanitizer
   which strips SVG attributes (d, viewBox, fill, stroke, etc.) */
trix-editor figure[data-trix-attachment*="vnd.mermaid"] img {
    max-width: 100% !important;
    max-height: 450px !important;
    width: auto !important;
    height: auto !important;
    object-fit: contain !important;
    display: block !important;
    margin: 0 auto !important;
}

/* Hide the drag handle on custom block attachments.
   The figure itself IS the .attachment element, so ::before is on the figure. */
trix-editor figure[data-trix-attachment*="vnd.table"]::before,
trix-editor figure[data-trix-attachment*="vnd.mermaid"]::before {
    display: none !important;
    content: none !important;
    opacity: 0 !important;
}

/* Click-to-edit hover outline */
trix-editor figure[data-trix-attachment*="vnd.table"]:hover,
trix-editor figure[data-trix-attachment*="vnd.mermaid"]:hover {
    outline: 2px solid hsl(var(--primary));
    outline-offset: 4px;
}

/* Click-to-edit tooltip — positioned top-left */
trix-editor figure[data-trix-attachment*="vnd.table"]:hover::after,
trix-editor figure[data-trix-attachment*="vnd.mermaid"]:hover::after {
    content: "Click to edit";
    position: absolute;
    top: 0.5rem;
    left: 0.5rem;
    background: rgba(0, 0, 0, 0.75);
    color: #ffffff;
    padding: 0.25rem 0.625rem;
    border-radius: calc(var(--radius, 0.75rem) - 4px);
    font-size: 0.75rem;
    font-weight: 500;
    pointer-events: none;
    z-index: 5;
}

/* Hide Trix's built-in attachment toolbar for custom attachments.
   Click-to-edit opens the modal; delete is inside the modal footer. */
trix-editor figure[data-trix-attachment*="vnd.table"] .attachment__toolbar,
trix-editor figure[data-trix-attachment*="vnd.mermaid"] .attachment__toolbar {
    display: none !important;
}

/* ========================================================
   Horizontal Rule Attachment Styles
   Trix has no native <hr> support, so horizontal rules are
   inserted as custom attachments with content type vnd.hr+html.
   ======================================================== */

trix-editor figure[data-trix-attachment*="vnd.hr"] {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0.25rem 0;
    cursor: default;
}

trix-editor figure[data-trix-attachment*="vnd.hr"] > * {
    display: block !important;
    width: 100% !important;
}

trix-editor .trix-hr hr,
.trix-content .trix-hr hr {
    border: none;
    border-top: 2px solid #d1d5db;
    margin: 0.5rem 0;
}

.dark trix-editor .trix-hr hr,
.dark .trix-content .trix-hr hr {
    border-top-color: #3f3f46;
}

/* Hide drag handle, toolbar, and hover effects on HR attachments */
trix-editor figure[data-trix-attachment*="vnd.hr"]::before {
    display: none !important;
    content: none !important;
    opacity: 0 !important;
}

trix-editor figure[data-trix-attachment*="vnd.hr"] .attachment__toolbar {
    display: none !important;
}

trix-editor figure[data-trix-attachment*="vnd.hr"]:hover {
    outline: none;
}

/* ========================================================
   Table Attachment Styles (Editor + Public Display)
   Uses border-collapse: collapse with explicit borders.
   Bordered vs non-bordered is controlled by the
   .trix-table--bordered class toggling cell borders.
   ======================================================== */

trix-editor .trix-table-wrapper,
.trix-content .trix-table-wrapper {
    overflow-x: auto;
    margin: 1rem 0;
    border-radius: var(--radius, 0.75rem);
    border: 1px solid #cbd5e1;
    background: #ffffff;
}

.dark trix-editor .trix-table-wrapper,
.dark .trix-content .trix-table-wrapper {
    border-color: #3f3f46;
    background: #18181b;
}

trix-editor .trix-table,
.trix-content .trix-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
    line-height: 1.5;
    color: #1e293b;
}

.dark trix-editor .trix-table,
.dark .trix-content .trix-table {
    color: #fafafa;
}

/* Header cells — centered, bold, distinct background */
trix-editor .trix-table th,
.trix-content .trix-table th {
    background-color: #e2e8f0;
    font-weight: 600;
    text-align: center;
    padding: 0.625rem 0.875rem;
    border: none;
    border-bottom: 2px solid #cbd5e1;
    color: #0f172a;
    letter-spacing: 0.01em;
}

.dark trix-editor .trix-table th,
.dark .trix-content .trix-table th {
    background-color: #27272a;
    color: #fafafa;
    border-bottom: 2px solid #3f3f46;
    font-weight: 700;
}

/* Body cells — no borders by default (clean look) */
trix-editor .trix-table td,
.trix-content .trix-table td {
    padding: 0.625rem 0.875rem;
    border: none;
    color: #1e293b;
    background-color: transparent;
}

.dark trix-editor .trix-table td,
.dark .trix-content .trix-table td {
    background-color: #18181b;
    color: #fafafa;
}

/* Striped rows */
trix-editor .trix-table--striped tbody tr:nth-child(even) td,
.trix-content .trix-table--striped tbody tr:nth-child(even) td {
    background-color: #e9eef4;
}

.dark trix-editor .trix-table--striped tbody tr:nth-child(even) td,
.dark .trix-content .trix-table--striped tbody tr:nth-child(even) td {
    background-color: #27272a;
}

/* Bordered: visible grid lines on ALL cells (th and td). */
trix-editor .trix-table--bordered th,
trix-editor .trix-table--bordered td,
.trix-content .trix-table--bordered th,
.trix-content .trix-table--bordered td {
    border: 1px solid #cbd5e1;
}

.dark trix-editor .trix-table--bordered th,
.dark trix-editor .trix-table--bordered td,
.dark .trix-content .trix-table--bordered th,
.dark .trix-content .trix-table--bordered td {
    border: 1px solid #3f3f46;
}

/* Bordered wrapper uses matching border color */
trix-editor .trix-table-wrapper:has(.trix-table--bordered),
.trix-content .trix-table-wrapper:has(.trix-table--bordered) {
    border-color: #cbd5e1;
}

.dark trix-editor .trix-table-wrapper:has(.trix-table--bordered),
.dark .trix-content .trix-table-wrapper:has(.trix-table--bordered) {
    border-color: #3f3f46;
}

/* ========================================================
   Mermaid Diagram Attachment Styles
   Constrained to reasonable max-height so diagrams don't
   overflow the editor. Uses object-fit: contain to scale
   down while preserving aspect ratio.
   ======================================================== */

trix-editor .mermaid-diagram,
.trix-content .mermaid-diagram {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    margin: 1rem 0;
    padding: 1.25rem;
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: var(--radius, 0.75rem);
    min-height: 80px;
    max-height: 500px;
    overflow: auto;
}

.dark trix-editor .mermaid-diagram,
.dark .trix-content .mermaid-diagram {
    background: #18181b;
    border-color: #3f3f46;
}

/* Mermaid content: <img> in editor (base64 data URL), inline SVG on public pages.
   Constrain height so large diagrams scale down instead of overflowing. */
trix-editor .mermaid-diagram img,
trix-editor .mermaid-diagram svg,
.trix-content .mermaid-diagram img,
.trix-content .mermaid-diagram svg {
    max-width: 100% !important;
    max-height: 450px !important;
    width: auto !important;
    height: auto !important;
    object-fit: contain;
    display: block;
    margin: 0 auto;
}

/* Modal Overlay & Container */
.trix-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 60;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 5vh 1.5rem 1.5rem;
    animation: trix-overlay-in 0.2s ease-out;
}

@keyframes trix-overlay-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes trix-modal-in {
    from { opacity: 0; transform: scale(0.97) translateY(6px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

.trix-modal {
    background: #ffffff;
    border: 1px solid #cbd5e1;
    border-radius: var(--radius, 0.75rem);
    box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.25);
    width: 100%;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    z-index: 65;
    animation: trix-modal-in 0.2s ease-out;
    overflow: hidden;
}

.dark .trix-modal {
    background: #18181b;
    border-color: #3f3f46;
}

/* Modal Header / Body / Footer */
.trix-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #e2e8f0;
    flex-shrink: 0;
}

.dark .trix-modal-header {
    border-bottom-color: #3f3f46;
}

.trix-modal-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: #1e293b;
    margin: 0;
}

.dark .trix-modal-title {
    color: #fafafa;
}

.trix-modal-close {
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid transparent;
    background: transparent;
    color: #94a3b8;
    border-radius: calc(var(--radius, 0.75rem) - 4px);
    cursor: pointer;
    transition: all 0.15s;
    outline: none;
}

.trix-modal-close:hover {
    background: #f1f5f9;
    color: #1e293b;
}

.dark .trix-modal-close {
    color: #a1a1aa;
}

.dark .trix-modal-close:hover {
    background: #27272a;
}

.trix-modal-body {
    padding: 1.5rem;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
}

.trix-modal-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    border-top: 1px solid #e2e8f0;
    flex-shrink: 0;
}

.dark .trix-modal-footer {
    border-top-color: #3f3f46;
}

.trix-modal-footer-left {
    flex-shrink: 0;
}

.trix-modal-footer-right {
    display: flex;
    gap: 0.75rem;
}

/* Modal Buttons */
.trix-modal-btn {
    padding: 0.5rem 1rem;
    border-radius: calc(var(--radius, 0.75rem) - 2px);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    border: 1px solid transparent;
    line-height: 1.25rem;
    outline: none;
}

.trix-modal-btn:focus,
.trix-modal-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px hsl(var(--primary) / 0.3);
}

.trix-modal-btn--cancel {
    background: #f8fafc;
    color: #374151;
    border-color: #cbd5e1;
}

.dark .trix-modal-btn--cancel {
    background: #27272a;
    color: #fafafa;
    border-color: #3f3f46;
}

.trix-modal-btn--cancel:hover {
    background: #e2e8f0;
    color: #1e293b;
}

.dark .trix-modal-btn--cancel:hover {
    background: #3f3f46;
}

.trix-modal-btn--submit {
    background: hsl(var(--primary));
    color: hsl(var(--primary-foreground));
    border-color: hsl(var(--primary));
}

.trix-modal-btn--submit:hover {
    filter: brightness(1.1);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.trix-modal-btn--submit:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    filter: none;
    box-shadow: none;
}

.trix-modal-btn--delete {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    background: transparent;
    color: #dc2626;
    border-color: transparent;
}

.trix-modal-btn--delete:hover {
    background: #fef2f2;
    color: #b91c1c;
}

.dark .trix-modal-btn--delete {
    color: #f87171;
}

.dark .trix-modal-btn--delete:hover {
    background: rgba(239, 68, 68, 0.15);
    color: #fca5a5;
}

.trix-modal-btn--delete svg {
    flex-shrink: 0;
}

/* Table Editor Modal Controls */
.trix-table-controls {
    padding-bottom: 1rem;
    border-bottom: 1px solid #e2e8f0;
    margin-bottom: 1rem;
}

.dark .trix-table-controls {
    border-bottom-color: #3f3f46;
}

.trix-table-controls-row {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    margin-bottom: 0.75rem;
    flex-wrap: wrap;
}

.trix-table-control-group {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.trix-table-label {
    font-size: 0.8125rem;
    font-weight: 500;
    color: #64748b;
}

.dark .trix-table-label {
    color: #a1a1aa;
}

.trix-table-stepper {
    display: flex;
    align-items: center;
    border: 1px solid #cbd5e1;
    border-radius: calc(var(--radius, 0.75rem) - 4px);
    overflow: hidden;
}

.dark .trix-table-stepper {
    border-color: #3f3f46;
}

.trix-table-stepper-btn {
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: #f1f5f9;
    color: #334155;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    outline: none;
    transition: all 0.15s;
    line-height: 1;
}

.dark .trix-table-stepper-btn {
    background: #27272a;
    color: #fafafa;
}

.trix-table-stepper-btn:hover {
    background: #e0f2fe;
    color: hsl(var(--primary));
}

.dark .trix-table-stepper-btn:hover {
    background: #3f3f46;
    color: hsl(var(--primary));
}

.trix-table-stepper-value {
    width: 2.5rem;
    text-align: center;
    font-size: 0.875rem;
    font-weight: 600;
    color: #1e293b;
    border-left: 1px solid #cbd5e1;
    border-right: 1px solid #cbd5e1;
    line-height: 2rem;
    background: #ffffff;
}

.dark .trix-table-stepper-value {
    border-left-color: #3f3f46;
    border-right-color: #3f3f46;
    background: #09090b;
    color: #fafafa;
}

.trix-table-checkbox {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    font-size: 0.8125rem;
    color: #64748b;
    cursor: pointer;
    transition: color 0.15s;
}

.dark .trix-table-checkbox {
    color: #a1a1aa;
}

.trix-table-checkbox:hover {
    color: #1e293b;
}

.dark .trix-table-checkbox:hover {
    color: #fafafa;
}

.trix-table-checkbox input[type="checkbox"] {
    width: 1rem;
    height: 1rem;
    border-radius: 0.25rem;
    accent-color: hsl(var(--primary));
    cursor: pointer;
}

/* Table Grid (Editable cells in modal) */
.trix-table-grid-wrapper {
    overflow-x: auto;
    border-radius: calc(var(--radius, 0.75rem) - 4px);
}

.trix-table-grid {
    width: max-content;
    min-width: 100%;
    border-collapse: collapse;
    border: 1px solid #cbd5e1;
    border-radius: calc(var(--radius, 0.75rem) - 4px);
}

.dark .trix-table-grid {
    border-color: #3f3f46;
}

.trix-table-cell {
    padding: 0.5rem 0.75rem;
    border: 1px dashed #cbd5e1;
    min-width: 100px;
    font-size: 0.875rem;
    outline: none;
    color: #1e293b;
    background: #ffffff;
    transition: box-shadow 0.15s, border-color 0.15s, background 0.15s;
}

.dark .trix-table-cell {
    background: #18181b;
    color: #fafafa;
    border-color: #3f3f46;
}

.trix-table-cell:focus {
    background: #f0fdfa;
    box-shadow: inset 0 0 0 2px hsl(var(--primary));
    border-color: transparent;
    position: relative;
    z-index: 1;
}

.dark .trix-table-cell:focus {
    background: rgba(16, 185, 129, 0.1);
}

.trix-table-grid th.trix-table-cell {
    background: #e2e8f0;
    font-weight: 600;
    text-align: center;
    color: #0f172a;
    border-bottom: 2px solid #cbd5e1;
}

.dark .trix-table-grid th.trix-table-cell {
    background: #27272a;
    color: #fafafa;
    border-bottom: 2px solid #3f3f46;
    font-weight: 700;
}

.trix-table-grid--bordered .trix-table-cell {
    border: 1px solid #cbd5e1 !important;
}

.dark .trix-table-grid--bordered .trix-table-cell {
    border: 1px solid #3f3f46 !important;
}

/* Striped rows — high-contrast alternating bands */
.trix-table-grid--striped tbody tr:nth-child(even) .trix-table-cell {
    background: #eef1f6 !important;
}

.dark .trix-table-grid--striped tbody tr:nth-child(even) .trix-table-cell {
    background: #27272a !important;
}

.trix-table-grid:not(.trix-table-grid--has-header) .trix-table-cell {
    font-weight: normal;
}

/* ========================================================
   Mermaid Editor: Templates
   ======================================================== */

.trix-mermaid-templates {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.trix-mermaid-templates-label {
    font-size: 0.8125rem;
    font-weight: 500;
    color: #64748b;
    margin-right: 0.25rem;
}

.dark .trix-mermaid-templates-label {
    color: #a1a1aa;
}

.trix-mermaid-template-btn {
    padding: 0.3rem 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: 999px;
    background: #f1f5f9;
    color: #475569;
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
}

.dark .trix-mermaid-template-btn {
    background: #27272a;
    border-color: #3f3f46;
    color: #a1a1aa;
}

.trix-mermaid-template-btn:hover {
    background: #e0f2fe;
    border-color: hsl(var(--primary));
    color: hsl(var(--primary));
}

.dark .trix-mermaid-template-btn:hover {
    background: rgba(16, 185, 129, 0.15);
    border-color: hsl(var(--primary));
    color: hsl(var(--primary));
}

/* ========================================================
   Mermaid Editor: Split Pane Layout
   ======================================================== */

.trix-mermaid-split {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    height: 400px;
}

@media (max-width: 640px) {
    .trix-mermaid-split {
        grid-template-columns: 1fr;
    }
}

.trix-mermaid-source-pane,
.trix-mermaid-preview-pane {
    display: flex;
    flex-direction: column;
    border: 1px solid #cbd5e1;
    border-radius: calc(var(--radius, 0.75rem) - 4px);
    overflow: hidden;
}

.dark .trix-mermaid-source-pane,
.dark .trix-mermaid-preview-pane {
    border-color: #3f3f46;
}

.trix-mermaid-pane-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.375rem 0.75rem;
    background: #e2e8f0;
    border-bottom: 1px solid #cbd5e1;
    font-size: 0.6875rem;
    font-weight: 600;
    color: #64748b;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    flex-shrink: 0;
}

.dark .trix-mermaid-pane-header {
    background: #27272a;
    border-bottom-color: #3f3f46;
    color: #a1a1aa;
}

/* Zoom controls in mermaid preview header */
.trix-mermaid-zoom-controls {
    display: flex;
    align-items: center;
    gap: 2px;
}

.trix-mermaid-zoom-btn {
    width: 1.375rem;
    height: 1.375rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #cbd5e1;
    border-radius: 3px;
    background: #ffffff;
    color: #475569;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    outline: none;
    transition: background 0.1s;
    padding: 0;
    line-height: 1;
}

.trix-mermaid-zoom-btn:hover {
    background: #f1f5f9;
    border-color: #94a3b8;
}

.dark .trix-mermaid-zoom-btn {
    background: #18181b;
    border-color: #3f3f46;
    color: #a1a1aa;
}

.dark .trix-mermaid-zoom-btn:hover {
    background: #3f3f46;
    color: #fafafa;
}

.trix-mermaid-zoom-level {
    font-size: 0.625rem;
    font-weight: 500;
    color: #64748b;
    min-width: 2rem;
    text-align: center;
    font-variant-numeric: tabular-nums;
    text-transform: none;
    letter-spacing: normal;
}

.dark .trix-mermaid-zoom-level {
    color: #71717a;
}

.trix-mermaid-textarea {
    flex: 1;
    padding: 0.75rem;
    border: none;
    outline: none;
    resize: none;
    font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', 'SF Mono', 'Consolas', monospace;
    font-size: 0.8125rem;
    line-height: 1.6;
    color: #1e293b;
    background: #ffffff;
    tab-size: 4;
}

.dark .trix-mermaid-textarea {
    background: #09090b;
    color: #fafafa;
}

.trix-mermaid-textarea::placeholder {
    color: #94a3b8;
}

.dark .trix-mermaid-textarea::placeholder {
    color: #a1a1aa;
}

.trix-mermaid-preview {
    flex: 1;
    padding: 1rem;
    background: #ffffff;
    overflow: auto;
}

.dark .trix-mermaid-preview {
    background: #09090b;
}

.trix-mermaid-rendered {
    min-width: min-content;
}

.trix-mermaid-rendered svg {
    display: block;
    height: auto;
}

.trix-mermaid-loading,
.trix-mermaid-placeholder {
    color: hsl(var(--muted-foreground));
    font-size: 0.875rem;
}

.trix-mermaid-placeholder {
    font-style: italic;
}

.trix-mermaid-error {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    color: hsl(var(--destructive));
    font-size: 0.8125rem;
    line-height: 1.4;
    padding: 0.75rem;
    background: hsl(var(--destructive) / 0.1);
    border: 1px solid hsl(var(--destructive) / 0.3);
    border-radius: calc(var(--radius, 0.75rem) - 4px);
    max-width: 100%;
    word-break: break-word;
}

.trix-mermaid-error svg {
    flex-shrink: 0;
    margin-top: 0.125rem;
}

/* Modal + Editor mobile responsiveness */
@media (max-width: 640px) {
    .trix-modal-overlay {
        padding: 0;
    }

    .trix-modal {
        max-height: 100vh;
        border-radius: 0;
        border: none;
    }

    .trix-modal-body {
        padding: 1rem;
    }

    .trix-table-controls-row {
        gap: 1rem;
    }

    .trix-mermaid-split {
        height: 300px;
    }

    .trix-modal-footer {
        padding: 0.75rem 1rem;
    }
}

/* ========================================================
   Diagram Viewer — Expand/Zoom Modal for Public Pages
   Full-screen overlay with zoom, pan, and pinch-zoom.
   Triggered by Stimulus: diagram_viewer_controller.
   ======================================================== */

/* Expand button overlay on mermaid diagrams in .trix-content */
.trix-content .diagram-expand-btn {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #d1d5db;
    border-radius: 0.375rem;
    background: rgba(255, 255, 255, 0.9);
    color: #374151;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s ease, background 0.15s ease;
    z-index: 2;
    backdrop-filter: blur(4px);
}

.dark .trix-content .diagram-expand-btn {
    background: rgba(24, 24, 27, 0.9);
    border-color: #3f3f46;
    color: #a1a1aa;
}

.trix-content .mermaid-diagram:hover .diagram-expand-btn {
    opacity: 1;
}

.trix-content .diagram-expand-btn:hover {
    background: rgba(255, 255, 255, 1);
    border-color: #9ca3af;
}

.dark .trix-content .diagram-expand-btn:hover {
    background: #27272a;
    border-color: #52525b;
}

/* Full-screen viewer overlay */
.diagram-viewer-overlay {
    position: fixed;
    inset: 0;
    z-index: 70;
    display: flex;
    flex-direction: column;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    opacity: 0;
    transition: opacity 0.2s ease;
}

.diagram-viewer-overlay.diagram-viewer-visible {
    opacity: 1;
}

/* Header bar */
.diagram-viewer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1.25rem;
    background: rgba(0, 0, 0, 0.4);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    flex-shrink: 0;
}

.diagram-viewer-title {
    color: #e5e7eb;
    font-size: 0.875rem;
    font-weight: 500;
    letter-spacing: 0.01em;
}

.diagram-viewer-controls {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.diagram-viewer-ctrl {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 0.375rem;
    background: rgba(255, 255, 255, 0.08);
    color: #d1d5db;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.diagram-viewer-ctrl:hover {
    background: rgba(255, 255, 255, 0.15);
    border-color: rgba(255, 255, 255, 0.25);
    color: #fff;
}

.diagram-viewer-close {
    margin-left: 0.5rem;
}

.diagram-viewer-zoom-level {
    color: #9ca3af;
    font-size: 0.75rem;
    font-variant-numeric: tabular-nums;
    min-width: 3rem;
    text-align: center;
    user-select: none;
}

/* Canvas area — fills remaining space */
.diagram-viewer-canvas {
    flex: 1;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    position: relative;
}

.diagram-viewer-canvas:active {
    cursor: grabbing;
}

/* Content wrapper — transformed by JS */
.diagram-viewer-content {
    transition: transform 0.1s ease-out;
    transform-origin: center center;
    user-select: none;
    -webkit-user-select: none;
}

.diagram-viewer-content img,
.diagram-viewer-content svg {
    display: block;
    pointer-events: none;
    filter: drop-shadow(0 4px 24px rgba(0, 0, 0, 0.4));
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 0.75rem;
    padding: 1.5rem;
}

.dark .diagram-viewer-content img,
.dark .diagram-viewer-content svg {
    background: #18181b;
    border-color: #3f3f46;
    filter: drop-shadow(0 4px 24px rgba(0, 0, 0, 0.6));
}