/* ==========================================================================
   Equation Editor — full-canvas LaTeX mode + inline cell math.
   Mirrors the Node Editor / Lab Mode canvas shape (see nodeEditor.css) so a
   third mode does not read as a different application.
   ========================================================================== */

/* ── Canvas shell ───────────────────────────────────────────────────────── */

/* `flex: 1` rather than `height: 100%`. The centre panel is a flex column with
   other children in it, so a 100%-tall child asks for the whole panel and then
   relies on flex-shrink to claw it back — which happens to work, until a
   sibling stops being hidden. A flex item sized by its share is the same
   result stated correctly, and it is what the Node Editor already does. */
.equation-canvas {
    display: none;                 /* enableEquationEditor() flips this to flex */
    flex-direction: column;
    width: 100%;
    flex: 1 1 auto;
    min-height: 0;
    background: var(--panel-bg, #1e293b);
    color: var(--text, #e2e8f0);
    overflow: hidden;
}

/* Equation mode owns the center pane. The table-specific header must not
   compete for vertical space or leave controls that act on a hidden table. */
body.equation-editor-active .center-header { display: none !important; }

.equation-canvas-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border-color, rgba(148, 163, 184, 0.2));
    font-size: 13px;
    font-weight: 600;
    flex-shrink: 0;
}

.equation-canvas-header > span:first-child {
    display: flex;
    align-items: center;
    gap: 7px;
}

.equation-canvas-header-actions {
    margin-left: auto;
    display: flex;
    gap: 6px;
    overflow-x: auto;
}

.equation-canvas-body {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: 260px 1fr;
}

/* ── Left rail: the equation list ───────────────────────────────────────── */

.eq-list {
    border-right: 1px solid var(--border-color, rgba(148, 163, 184, 0.2));
    overflow-y: auto;
    padding: 8px;
    min-height: 0;
}

.eq-item {
    position: relative;
    padding: 8px 26px 8px 10px;
    margin-bottom: 6px;
    border: 1px solid transparent;
    border-radius: 7px;
    cursor: pointer;
    background: rgba(148, 163, 184, 0.06);
}

.eq-item:hover { background: rgba(148, 163, 184, 0.12); }
.eq-item.active { border-color: #2563eb; background: rgba(37, 99, 235, 0.14); }

.eq-item-head {
    display: flex;
    align-items: baseline;
    gap: 6px;
    margin-bottom: 5px;
}

.eq-item-name {
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.eq-badge {
    margin-left: auto;
    flex-shrink: 0;
    font-size: 9px;
    letter-spacing: 0.03em;
    padding: 1px 5px;
    border-radius: 7px;
    background: rgba(148, 163, 184, 0.2);
    opacity: 0.85;
}

/* The thumbnail is a preview, not a reading surface: it scrolls rather than
   wrapping, because a wrapped equation is a misleading picture of one. */
.eq-item-thumb {
    font-size: 13px;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 2px;
}

.eq-item-del {
    position: absolute;
    top: 5px;
    right: 5px;
    border: none;
    background: none;
    color: inherit;
    opacity: 0.35;
    cursor: pointer;
    font-size: 15px;
    line-height: 1;
    padding: 2px 4px;
}

.eq-item-del:hover { opacity: 1; color: #f87171; }

/* ── Right pane: the equation, then its source, then the palette ─────────── */
/* `minmax(0, 1fr)` on the equation, not `1fr`. A bare `1fr` keeps a min-content
   floor, so a tall expression — a summation with limits above and below, say —
   grows the row past the pane instead of scrolling inside it, and the rows
   under it get pushed out of the panel. On a phone that is exactly what
   clipped the sigma's lower limit. */

.eq-work {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    min-height: 0;
    min-width: 0;
    overflow: hidden;
}

.eq-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px 0;
}

.eq-meta label { font-size: 11px; opacity: 0.7; }

.eq-meta input {
    flex: 1;
    min-width: 0;
    background: rgba(148, 163, 184, 0.1);
    border: 1px solid var(--border-color, rgba(148, 163, 184, 0.25));
    border-radius: 6px;
    color: inherit;
    font-size: 12px;
    padding: 5px 8px;
}

/* The source field is a disclosure, closed by default. It used to sit open
   above the preview showing the same TeX the preview then swapped itself for
   on click — two editors for one string, and the render you were correcting
   vanished the moment you went to correct it. */
.eq-source-wrap { padding: 0 12px 8px; min-height: 0; display: flex; flex-direction: column; }

.eq-source-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    width: 100%;
    padding: 6px 8px;
    border: 1px solid transparent;
    border-radius: 6px;
    background: rgba(148, 163, 184, 0.08);
    color: inherit;
    font: inherit;
    font-size: 11px;
    opacity: 0.8;
    cursor: pointer;
    text-align: left;
}

.eq-source-label:hover { opacity: 1; border-color: var(--border-color, rgba(148, 163, 184, 0.25)); }
.eq-source-label span { font-size: 9px; letter-spacing: 0.06em; text-transform: uppercase; opacity: 0.7; }
.eq-source-label::after { content: '\25B8'; margin-left: 2px; transition: transform 0.12s ease; }
.eq-source-wrap.eq-source-open .eq-source-label::after { transform: rotate(90deg); }

#eqSource { display: none; }
.eq-source-wrap.eq-source-open #eqSource { display: block; }

#eqSource {
    width: 100%;
    min-height: 110px;
    max-height: 30vh;
    margin-top: 6px;
    resize: vertical;
    background: rgba(15, 23, 42, 0.55);
    border: 1px solid var(--border-color, rgba(148, 163, 184, 0.25));
    border-radius: 7px;
    color: inherit;
    padding: 9px 11px;
    font: 13px/1.6 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    tab-size: 2;
}

#eqSource:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.22);
}

/* The error belongs to the source field and is padded by it. It stays visible
   with the field closed, because a message you have to open a panel to see is
   a message you will not see. */
.eq-error {
    display: none;
    margin: 6px 0 0;
    padding: 6px 9px;
    border-radius: 6px;
    background: rgba(248, 113, 113, 0.12);
    border: 1px solid rgba(248, 113, 113, 0.35);
    color: #fca5a5;
    font: 11px/1.45 ui-monospace, Menlo, Consolas, monospace;
    white-space: pre-wrap;
}

/* KaTeX renders on a transparent ground and inherits colour, so a preview on
   the dark panel would be dark-on-dark. It gets its own light surface — the
   same decision the artifact previews make, and for the same reason: this is
   what the equation will look like in a document. */
.eq-preview {
    position: relative;            /* the caret is measured against this box */
    margin: 10px 12px;
    padding: 18px 14px;
    border-radius: 8px;
    background: #ffffff;
    color: #111827;
    border: 1px solid rgba(148, 163, 184, 0.25);
    overflow: auto;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: text;
}

.eq-preview:focus { outline: none; }
.eq-preview.eq-focus { border-color: #2563eb; box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.22); }

/* The equation is typeset, not laid out as text, so it must not be selected as
   text — a drag across it would otherwise highlight glyphs the caret model
   knows nothing about. */
.eq-preview .katex { user-select: none; }

/* An expression the source map could not instrument still renders; it just has
   no anchors to put a caret on. The dotted rule says the box is showing rather
   than editing, instead of leaving a caret that would not move. */
.eq-preview.eq-unanchored { border-style: dashed; cursor: default; }
.eq-preview.eq-unanchored .eq-caret { display: none; }

.eq-caret {
    position: absolute;
    width: 2px;
    background: #111827;
    pointer-events: none;
    animation: eq-caret-blink 1.1s steps(1) infinite;
}

@keyframes eq-caret-blink { 50% { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
    .eq-caret { animation: none; }
    .eq-source-label::after { transition: none; }
}

.eq-preview.eq-math-error { background: #fff7f7; border-color: rgba(248, 113, 113, 0.5); }

.eq-empty {
    opacity: 0.5;
    font-size: 12px;
    text-align: center;
    padding: 18px 10px;
}

.eq-raw-tex {
    margin: 0;
    font: 12px/1.5 ui-monospace, Menlo, Consolas, monospace;
    white-space: pre-wrap;
    word-break: break-word;
    opacity: 0.85;
}

/* ── Symbol palette ─────────────────────────────────────────────────────── */

.eq-palette {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    padding: 8px 12px 12px;
    border-top: 1px solid var(--border-color, rgba(148, 163, 184, 0.2));
    max-height: 34vh;
    overflow-y: auto;
}

.eq-pal-group { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }

.eq-pal-label {
    width: 100%;
    font-size: 9px;
    font-weight: 700;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    opacity: 0.5;
    margin-bottom: 2px;
}

.eq-pal-btn {
    min-width: 30px;
    height: 28px;
    padding: 0 7px;
    border: 1px solid var(--border-color, rgba(148, 163, 184, 0.25));
    border-radius: 6px;
    background: rgba(148, 163, 184, 0.08);
    color: inherit;
    cursor: pointer;
    font-size: 13px;
    line-height: 1;
}

.eq-pal-btn:hover { background: rgba(37, 99, 235, 0.22); border-color: #2563eb; }
.eq-pal-btn:active { transform: translateY(1px); }

/* ── Inline cell math ───────────────────────────────────────────────────── */

/* Math sits on the cell's own baseline and inherits its colour, so a table of
   formulas reads as a table rather than as a strip of pasted images. */
.eq-math {
    display: inline-block;
    vertical-align: middle;
    max-width: 100%;
    overflow-x: auto;
    color: inherit;
}

.eq-math .katex { color: inherit; }

/* A cell whose TeX does not compile is marked, never hidden: the source is
   still shown (renderLatex falls back to it) and the underline says the tool
   could not typeset it rather than pretending it did. */
.eq-math-error {
    text-decoration: underline wavy rgba(248, 113, 113, 0.8);
    text-underline-offset: 3px;
}

@media (max-width: 900px) {
    .equation-canvas-header { align-items: flex-start; }
    .equation-canvas-header-actions { width: 100%; margin-left: 0; }
    .equation-canvas-body { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
    .eq-list { display: flex; gap: 6px; padding: 6px; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--border-color, rgba(148, 163, 184, 0.2)); }
    .eq-item { min-width: 150px; margin: 0; }
    .eq-preview { min-height: 34vh; margin: 8px; padding: 12px; }
    .eq-source-wrap { padding: 0 8px 8px; }
    .eq-source-wrap #eqSource { min-height: 92px; max-height: 24vh; }
    .eq-palette { flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; max-height: none; padding: 8px; }
    .eq-pal-group { min-width: max-content; }
    .eq-pal-btn { min-width: 42px; height: 42px; font-size: 16px; }
}

@media (max-width: 560px) {
    .equation-canvas-header { flex-wrap: wrap; }
    .equation-canvas-header-actions .btn { white-space: nowrap; }
    .eq-meta { padding: 6px 8px 0; }
}
