/*
 * Lexical Editor — Symposium v2
 * ================================
 * Styles for the Lexical rich-text editor (edit & view modes).
 *
 * ALL values use variables.css tokens exclusively — zero hardcoded colors.
 * The editor automatically adapts to every theme:
 * dark, liquid-glass, sakina-purple, deep-blue, dark-brown, dark-green,
 * dark-red, and all pastel variants.
 *
 * Class prefix: lex-
 *
 * Sections:
 *  1.  Editor Wrapper
 *  2.  Toolbar
 *  3.  Editor Area + Placeholder
 *  4.  Editor Content
 *  5.  Floating Toolbar
 *  6.  Slash Command Menu
 *  7.  Footer (word count)
 *  8.  Loading & Fallback
 *  9.  View Wrapper (read-only)
 *  10. Lexical Node Styles (shared edit + view)
 *  11. Responsive
 *  12. Dark / Colored Theme Overrides
 *  13. Liquid Glass Theme
 */

/* ============================================================
   1. Editor Wrapper
   ============================================================ */

.lex-editor-wrapper {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    /* border: var(--border-width) solid var(--border-color); */
    border-radius: var(--radius-xl);
    overflow: visible;          /* allow float toolbar to overflow */
    background: var(--main-background-color);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
    position: relative;
}

/* Clip overflow only on the inner area, not the whole wrapper */
.lex-editor-wrapper > .lex-toolbar,
.lex-editor-wrapper > .lex-footer {
    /* overflow: hidden; */
}

.lex-editor-wrapper:focus-within {
    border-color: var(--color-ink-lighter);
    box-shadow: var(--shadow-sm);
}

/* Unsaved state — subtle left border accent */
.lex-editor-wrapper[data-unsaved="true"] {
    border-left-color: var(--color-accent);
}

/* ============================================================
   2. Toolbar
   ============================================================ */

.lex-toolbar {
    display: flex;
    align-items: center;
    overflow: auto;
    gap: var(--space-1);
    padding: var(--space-2) var(--space-3);
    border-bottom: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    flex-shrink: 0;
    transition: opacity var(--transition-fast);
    scrollbar-width: none;
}

.lex-toolbar-group {
    display: flex;
    align-items: center;
    gap: 2px;
}

.lex-toolbar-sep {
    width: var(--border-width);
    height: 18px;
    background: var(--border-color);
    margin: 0 var(--space-1);
    flex-shrink: 0;
}

.lex-toolbar-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
    height: 28px;
    padding: 0 var(--space-1);
    border-radius: var(--radius-md);
    border: var(--border-width) solid transparent;
    background: transparent;
    color: var(--color-ink-light);
    font-size: var(--text-sm);
    font-family: var(--font-sans);
    cursor: pointer;
    transition:
        background var(--transition-fast),
        color var(--transition-fast),
        border-color var(--transition-fast),
        opacity var(--transition-fast);
    line-height: 1;
    user-select: none;
}

.lex-toolbar-btn:hover:not(:disabled) {
    background: var(--main-background-color-third);
    color: var(--color-ink);
    border-color: var(--border-color);
}

.lex-toolbar-btn.is-active {
    background: var(--color-ink);
    color: var(--main-background-color);
    border-color: var(--color-ink);
}

.lex-toolbar-btn.is-active:hover {
    background: var(--color-ink-light);
    border-color: var(--color-ink-light);
}

.lex-toolbar-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.lex-toolbar-btn svg {
    display: block;
    pointer-events: none;
}

/* Inline format label decorators */
.lex-btn-u  { text-decoration: underline; font-weight: var(--weight-semibold); }
.lex-btn-code { font-family: var(--font-mono); font-size: var(--text-xs); letter-spacing: -0.03em; }

/* ============================================================
   3. Editor Area + Placeholder
   ============================================================ */

.lex-editor-area {
    position: relative;
    flex: 1;
    height: 100%;
    min-height: 0;
    /* Clip the editor content but allow positioned children (float, slash) to overflow */
    overflow: visible;
}

/* Placeholder shown when editor is empty */
.lex-placeholder {
    display: none;
    position: absolute;
    top: var(--space-4);
    left: var(--space-5);
    right: var(--space-5);
    pointer-events: none;
    user-select: none;
    color: var(--color-ink-lighter);
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--leading-relaxed);
    z-index: 1;
}

.lex-placeholder kbd {
    display: inline-flex;
    align-items: center;
    height: 16px;
    padding: 0 4px;
    border-radius: var(--radius-sm);
    border: var(--border-width) solid var(--border-color);
    background: var(--main-background-color-secondary);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--color-ink-lighter);
    vertical-align: middle;
}

/* ============================================================
   4. Editor Content (contenteditable)
   ============================================================ */

.lex-editor-content {
    position: relative;
    z-index: 2;
    min-height: 600px;
    max-height: 100%;
    overflow-y: auto;
    padding: var(--space-4) var(--space-5);
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--leading-relaxed);
    color: var(--color-ink);
    background: var(--main-background-color);
    outline: none;
    caret-color: var(--color-accent);
    scrollbar-width: thin;
    scrollbar-color: var(--main-background-color-third) var(--main-background-color);
}

.side-panel-body.side-panel-body-lexical-editor .lex-editor-wrapper,
.side-panel-body.side-panel-body-lexical-editor .lex-editor-area,
.side-panel-body.side-panel-body-lexical-editor .lex-editor-content {
    flex: 1;
    min-height: 0;
}

.side-panel-body.side-panel-body-lexical-editor .lex-editor-content {
    height: 100%;
        max-width: 800px;
    margin-top: 2rem;
    margin-inline: auto;
}

.lex-editor-content:focus { outline: none; }

.lex-editor-content[data-lex-empty]::before {
    content: attr(data-placeholder);
    position: absolute;
    top: var(--space-4);
    left: var(--space-5);
    right: var(--space-5);
    color: var(--color-ink-lighter);
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--leading-relaxed);
    pointer-events: none;
    user-select: none;
}

/* ============================================================
   5. Floating Toolbar
   ============================================================ */

.lex-float-toolbar {
    position: absolute;
    z-index: var(--z-dropdown);
    display: flex;
    align-items: center;
    gap: 2px;
    padding: var(--space-1) var(--space-2);
    background: var(--main-background-color);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-md);
    pointer-events: auto;
    /* Animated appearance */
    animation: lex-float-appear 120ms ease forwards;
}

.lex-float-toolbar[hidden] {
    display: none !important;
}

@keyframes lex-float-appear {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}

.lex-float-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    border-radius: var(--radius-md);
    border: var(--border-width) solid transparent;
    background: transparent;
    color: var(--color-ink-light);
    font-size: var(--text-sm);
    font-family: var(--font-sans);
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast);
    line-height: 1;
    user-select: none;
}

.lex-float-btn:hover {
    background: var(--main-background-color-secondary);
    color: var(--color-ink);
}

.lex-float-btn.is-active {
    background: var(--color-ink);
    color: var(--main-background-color);
    border-radius: var(--radius-md);
}

/* ============================================================
   6. Slash Command Menu
   ============================================================ */

.lex-slash-menu {
    position: absolute;
    z-index: var(--z-dropdown);
    width: 240px;
    max-height: 280px;
    overflow-y: auto;
    background: var(--main-background-color);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: var(--space-1);
    scrollbar-width: thin;
    scrollbar-color: var(--main-background-color-third) transparent;
    animation: lex-slash-appear 120ms ease forwards;
}

.lex-slash-menu[hidden] {
    display: none !important;
}

@keyframes lex-slash-appear {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(0); }
}

.lex-slash-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    width: 100%;
    padding: var(--space-2) var(--space-2);
    border-radius: var(--radius-md);
    border: none;
    background: transparent;
    color: var(--color-ink);
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    text-align: left;
    cursor: pointer;
    transition: background var(--transition-fast);
}

.lex-slash-item:hover,
.lex-slash-item.is-selected {
    background: var(--main-background-color-secondary);
}

.lex-slash-item-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: var(--radius-md);
    background: var(--main-background-color-secondary);
    color: var(--color-ink-light);
    flex-shrink: 0;
}

.lex-slash-item.is-selected .lex-slash-item-icon {
    background: var(--main-background-color-third);
    color: var(--color-ink);
}

.lex-slash-item-text {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.lex-slash-item-label {
    font-weight: var(--weight-medium);
    color: var(--color-ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lex-slash-item-desc {
    font-size: var(--text-xs);
    color: var(--color-ink-lighter);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lex-slash-item-kbd {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    height: 18px;
    padding: 0 var(--space-1);
    border-radius: var(--radius-sm);
    border: var(--border-width) solid var(--border-color);
    background: var(--main-background-color-secondary);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--color-ink-lighter);
    font-weight: var(--weight-normal);
}

/* ============================================================
   7. Footer (word count)
   ============================================================ */

.lex-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: var(--space-1) var(--space-3);
    /* background: var(--main-background-color-secondary); */
    border-top: var(--border-width) solid var(--border-color);
    border-radius: 0 0 var(--radius-xl) var(--radius-xl);
    flex-shrink: 0;
}

.lex-word-count {
    font-size: var(--text-xs);
    color: var(--color-ink-lighter);
    font-family: var(--font-sans);
    font-variant-numeric: tabular-nums;
    user-select: none;
}

/* ============================================================
   8. Loading & Fallback
   ============================================================ */

/* Edit-mode loading: absolute overlay inside .lex-editor-area */
.lex-editor-loading {
    display: none;   /* shown as flex via JS */
    position: absolute;
    inset: 0;
    z-index: 10;
    align-items: center;
    justify-content: center;
    background: var(--main-background-color);
    border-radius: 0;  /* inherits from parent clip */
}

/* View-mode loading: inline (view wrapper is not position:relative) */
.lex-view-loading {
    display: none;
    align-items: center;
    justify-content: center;
    min-height: 80px;
    padding: var(--space-6);
    background: transparent;
}

.lex-loading-dots {
    display: flex;
    gap: var(--space-1);
    align-items: center;
}

.lex-loading-dots span {
    width: 5px;
    height: 5px;
    border-radius: var(--radius-full);
    background: var(--color-ink-lighter);
    animation: lex-dot-pulse 1.2s infinite ease-in-out both;
}

.lex-loading-dots span:nth-child(1) { animation-delay: -0.32s; }
.lex-loading-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes lex-dot-pulse {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40%           { transform: scale(1);   opacity: 1; }
}

/* Textarea fallback */
.lex-fallback-textarea {
    display: block;
    width: 100%;
    min-height: 220px;
    padding: var(--space-4) var(--space-5);
    font-family: var(--font-mono);
    font-size: var(--text-sm);
    line-height: var(--leading-relaxed);
    color: var(--color-ink);
    background: var(--main-background-color);
    border: none;
    resize: vertical;
    outline: none;
    box-sizing: border-box;
}

/* ============================================================
   9. View Wrapper (read-only)
   ============================================================ */

.lex-view-wrapper {
    width: 100%;
    position: relative;
}

.lex-view-content {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--leading-relaxed);
    color: var(--color-ink);
    word-break: break-word;
    min-height: 1em;
    outline: none;
}

.lex-view-content .lex-root { cursor: default; }
.lex-view-content a.lex-link { cursor: pointer; }

/* Legacy HTML content from pre-markdown cards */
.lex-view-legacy { /* uses view-content-rich from tiptap.css */ }

/* Plain text fallback */
.lex-view-fallback {
    white-space: pre-wrap;
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--leading-relaxed);
    color: var(--color-ink);
}

/* ============================================================
   10. Lexical Node Styles (shared edit + view)
   ============================================================ */

.lex-root { outline: none; position: relative; }

/* Paragraphs */
.lex-paragraph { margin: 0 0 var(--space-2); position: relative; }
.lex-paragraph:last-child { margin-bottom: 0; }

/* ── Headings ────────────────────────────────────────────── */
.lex-h1 {
    font-size: var(--text-2xl);
    font-weight: var(--weight-semibold);
    line-height: var(--leading-tight);
    margin: var(--space-5) 0 var(--space-3);
    color: var(--color-ink);
}
.lex-h2 {
    font-size: var(--text-xl);
    font-weight: var(--weight-semibold);
    line-height: var(--leading-tight);
    margin: var(--space-4) 0 var(--space-2);
    color: var(--color-ink);
}
.lex-h3 {
    font-size: var(--text-lg);
    font-weight: var(--weight-semibold);
    line-height: var(--leading-tight);
    margin: var(--space-4) 0 var(--space-2);
    color: var(--color-ink);
}
.lex-h4, .lex-h5, .lex-h6 {
    font-size: var(--text-base);
    font-weight: var(--weight-semibold);
    margin: var(--space-3) 0 var(--space-2);
    color: var(--color-ink);
}
.lex-root > .lex-h1:first-child,
.lex-root > .lex-h2:first-child,
.lex-root > .lex-h3:first-child { margin-top: 0; }

/* ── Text formats ────────────────────────────────────────── */
.lex-bold                   { font-weight: var(--weight-semibold); }
.lex-italic                 { font-style: italic; }
.lex-underline              { text-decoration: underline; text-underline-offset: 2px; }
.lex-strikethrough          { text-decoration: line-through; }
.lex-underline-strikethrough{ text-decoration: underline line-through; }
.lex-subscript              { font-size: 0.75em; vertical-align: sub; }
.lex-superscript            { font-size: 0.75em; vertical-align: super; }
.lex-highlight {
    background: color-mix(in srgb, var(--color-cleanup) 25%, transparent);
    border-radius: var(--radius-sm);
    padding: 0 2px;
}

/* ── Inline code ─────────────────────────────────────────── */
.lex-inline-code {
    font-family: var(--font-mono);
    font-size: 0.875em;
    background: var(--main-background-color-secondary);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.1em 0.35em;
    color: var(--color-ink);
}

/* ── Code blocks ─────────────────────────────────────────── */
.lex-code-block {
    display: block;
    font-family: var(--font-mono);
    font-size: var(--text-sm);
    background: var(--color-ink);
    color: var(--main-background-color);
    padding: var(--space-4);
    border-radius: var(--radius-lg);
    overflow-x: auto;
    margin: var(--space-3) 0;
    line-height: var(--leading-relaxed);
    tab-size: 2;
    white-space: pre;
}

/* Syntax tokens */
.lex-tok-comment, .lex-tok-prolog, .lex-tok-doctype, .lex-tok-cdata {
    color: color-mix(in srgb, var(--main-background-color) 60%, var(--color-ink-lighter));
}
.lex-tok-punctuation { color: color-mix(in srgb, var(--main-background-color) 80%, gray); }
.lex-tok-keyword, .lex-tok-atrule, .lex-tok-builtin { color: var(--color-target); }
.lex-tok-string, .lex-tok-char, .lex-tok-attr, .lex-tok-inserted { color: var(--color-operation); }
.lex-tok-boolean, .lex-tok-constant, .lex-tok-number { color: var(--color-cleanup); }
.lex-tok-deleted  { color: var(--color-error); }
.lex-tok-selector, .lex-tok-class, .lex-tok-class-name { color: var(--color-query); }
.lex-tok-function { color: var(--color-agent); }
.lex-tok-regex, .lex-tok-important, .lex-tok-variable { color: var(--color-output); }
.lex-tok-operator, .lex-tok-entity, .lex-tok-url { color: var(--color-ink-light); }
.lex-tok-namespace { opacity: 0.7; }

/* ── Blockquote ──────────────────────────────────────────── */
.lex-quote {
    border-left: 3px solid var(--color-accent-subtle);
    padding-left: var(--space-4);
    margin: var(--space-3) 0;
    color: var(--color-ink-light);
    font-style: italic;
}

/* ── Lists ───────────────────────────────────────────────── */
.lex-ul, .lex-ol { padding-left: var(--space-6); margin: var(--space-2) 0; }
.lex-ul { list-style-type: disc; }
.lex-ol { list-style-type: decimal; }

.lex-listitem { margin: var(--space-1) 0; line-height: var(--leading-relaxed); }
.lex-nested-listitem { list-style: none; }

.lex-listitem-checked,
.lex-listitem-unchecked {
    position: relative;
    padding-left: var(--space-6);
    list-style: none;
}

.lex-listitem-checked::before,
.lex-listitem-unchecked::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 14px;
    height: 14px;
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--main-background-color);
}

.lex-listitem-checked::before {
    background: var(--color-ink);
    border-color: var(--color-ink);
}

.lex-listitem-checked::after {
    content: '';
    position: absolute;
    left: 3px;
    top: 50%;
    transform: translateY(-60%) rotate(45deg);
    width: 4px;
    height: 8px;
    border-right: 2px solid var(--main-background-color);
    border-bottom: 2px solid var(--main-background-color);
}

/* ── Links ───────────────────────────────────────────────── */
.lex-link {
    color: var(--color-target);
    text-decoration: underline;
    text-underline-offset: 2px;
    cursor: pointer;
}
.lex-link:hover { opacity: 0.85; }

/* ── Images ──────────────────────────────────────────────── */
.lex-image {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-md);
    display: block;
    margin: var(--space-3) 0;
}

/* ── Horizontal rule ─────────────────────────────────────── */
.lex-hr {
    border: none;
    border-top: var(--border-width) solid var(--border-color);
    margin: var(--space-4) 0;
}

/* ── Tables ──────────────────────────────────────────────── */
.lex-table {
    border-collapse: collapse;
    width: 100%;
    margin: var(--space-4) 0;
    font-size: var(--text-sm);
    table-layout: fixed;
}
.lex-table-cell,
.lex-table-cell-header {
    border: var(--border-width) solid var(--border-color);
    padding: var(--space-2) var(--space-3);
    vertical-align: top;
    min-width: 60px;
}
.lex-table-cell-header {
    background: var(--main-background-color-secondary);
    font-weight: var(--weight-semibold);
    text-align: left;
}
.lex-table-cell.lex-table-cell-selected {
    background: color-mix(in srgb, var(--color-target) 12%, transparent);
}

/* ── Mark ────────────────────────────────────────────────── */
.lex-mark { background: color-mix(in srgb, var(--color-cleanup) 20%, transparent); border-bottom: 1px solid var(--color-cleanup); }
.lex-mark-overlap { background: color-mix(in srgb, var(--color-query) 15%, transparent); }

/* ============================================================
   11. Responsive
   ============================================================ */

@media (max-width: 480px) {
    .lex-toolbar { gap: 2px; padding: var(--space-1) var(--space-2); }
    .lex-toolbar-btn { min-width: 26px; height: 26px; font-size: var(--text-xs); }
    .lex-editor-content { padding: var(--space-3) var(--space-3); }
    .lex-placeholder { top: var(--space-3); left: var(--space-3); right: var(--space-3); }
    .lex-slash-menu { width: calc(100vw - var(--space-8)); }
}

/* ============================================================
   12. Dark / Colored Theme Overrides
   (Code block adjusts since --color-ink is light in dark themes)
   ============================================================ */

html[data-theme="dark"] .lex-code-block,
html[data-theme="sakina-purple"] .lex-code-block,
html[data-theme="deep-blue"] .lex-code-block,
html[data-theme="dark-brown"] .lex-code-block,
html[data-theme="dark-green"] .lex-code-block,
html[data-theme="dark-red"] .lex-code-block {
    background: var(--main-background-color-secondary);
    color: var(--color-ink);
    border: var(--border-width) solid var(--border-color);
}

/* Floating toolbar on dark backgrounds */
html[data-theme="dark"] .lex-float-toolbar,
html[data-theme="sakina-purple"] .lex-float-toolbar,
html[data-theme="deep-blue"] .lex-float-toolbar,
html[data-theme="dark-brown"] .lex-float-toolbar,
html[data-theme="dark-green"] .lex-float-toolbar,
html[data-theme="dark-red"] .lex-float-toolbar {
    box-shadow: var(--shadow-lg);
}

/* Slash menu on dark backgrounds */
html[data-theme="dark"] .lex-slash-menu,
html[data-theme="sakina-purple"] .lex-slash-menu,
html[data-theme="deep-blue"] .lex-slash-menu,
html[data-theme="dark-brown"] .lex-slash-menu,
html[data-theme="dark-green"] .lex-slash-menu,
html[data-theme="dark-red"] .lex-slash-menu {
    box-shadow: var(--shadow-lg);
}

/* ============================================================
   13. Liquid Glass Theme
   ============================================================ */

html[data-theme="liquid-glass"] .lex-editor-wrapper {
    backdrop-filter: blur(10px) saturate(160%);
    -webkit-backdrop-filter: blur(10px) saturate(160%);
    background: rgba(255, 255, 255, 0.6);
}

html[data-theme="liquid-glass"] .lex-toolbar {
    background: rgba(245, 245, 245, 0.7);
}

html[data-theme="liquid-glass"] .lex-float-toolbar,
html[data-theme="liquid-glass"] .lex-slash-menu {
    backdrop-filter: blur(12px) saturate(180%);
    -webkit-backdrop-filter: blur(12px) saturate(180%);
    background: rgba(255, 255, 255, 0.85);
}
