:root {
    --tnx-markdown-code-bg: #212121;
    --tnx-markdown-code-text: #eeffff;
    --tnx-markdown-code-border: rgba(255, 255, 255, 0.14);
    --tnx-markdown-marker-bg: var(--marker-color);
    --tnx-markdown-marker-text: #fefce8;
    --tnx-markdown-selection-bg: rgba(128, 203, 196, 0.3);
    --tnx-markdown-link-color: #2563eb;
}

html[data-theme="dark"],
html.dark {
    --tnx-markdown-marker-bg: var(--marker-color);
    --tnx-markdown-marker-text: #111827;
    --tnx-markdown-link-color: #60a5fa;
}

.tnx-markdown-textmarker,
.tnx-markdown-editable-preview mark,
.entry-editable-markdown-preview mark,
.interactive-table-editable-markdown-preview mark,
.emailbot-editable-markdown-preview mark,
.tnx-markdown-cm-textmarker {
    padding: 0.05em 0.2em;
    border-radius: 0.25em;
    background: var(--tnx-markdown-marker-bg) !important;
    color: var(--tnx-markdown-marker-text) !important;
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}

.tnx-markdown-cm-textmarker {
    font-weight: inherit !important;
}

/*
 * A checkbox line's real <input> (readonly / static renders) and its CodeMirror source-view
 * token highlight, plus the "checked" strikethrough treatment shared by every render path. Pure
 * text-decoration/opacity - never padding/width - so it stays safe inside the character-aligned
 * editable overlay too.
 */
.tnx-markdown-checkbox {
    accent-color: var(--link-color, var(--tnx-markdown-link-color, #2563eb)) !important;
    vertical-align: middle;
    margin: 0 0.35em 0 0;
    cursor: default;
}

/* Checked dims the checkbox control itself too - never struck through, just lower opacity. */
.tnx-markdown-checkbox:checked {
    opacity: 0.5;
}

.tnx-markdown-checkbox-line {
    display: block;
}

.tnx-markdown-checkbox-text--done,
.tnx-markdown-editable-checkbox-text--done,
.tnx-markdown-cm-checkbox-text--done {
    text-decoration: line-through;
    opacity: 0.5;
}

.tnx-markdown-cm-checkbox {
    padding: 0.05em 0.15em;
    border-radius: 0.25em;
    background: var(--link-color, var(--tnx-markdown-link-color, #2563eb)) !important;
}

.tnx-markdown-cm-checkbox--checked {
    opacity: 0.5;
}

.tnx-markdown-inline-code,
code:not(pre code),
.tnx-markdown-editable-preview code,
.entry-editable-markdown-preview code,
.interactive-table-editable-markdown-preview code,
.emailbot-editable-markdown-preview code,
.js-markdown-value code:not(pre code),
.markdown-readonly code:not(pre code),
.emailbot-markdown code:not(pre code),
.chat-bubble code:not(pre code),
.tnx-markdown-rendered code:not(pre code),
.tnx-markdown-cm-code {
    display: inline;
    padding: 0.08em 0.3em;
    border: 1px solid var(--tnx-markdown-code-border) !important;
    border-radius: 4px;
    background: var(--tnx-markdown-code-bg) !important;
    color: var(--tnx-markdown-code-text) !important;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace !important;
    font-size: 0.94em;
    font-weight: 400;
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}

/*
 * Inside the editable overlay every character must line up with the transparent textarea
 * underneath, so code and textmarker keep the field's own font metrics and add no width of
 * their own: no padding, no border (a zero-layout inset shadow draws the code box instead) and
 * the highlight is exactly as tall as its line.
 */
.tnx-markdown-editable-preview code,
.tnx-markdown-editable-preview mark,
.entry-editable-markdown-preview code,
.entry-editable-markdown-preview mark,
.interactive-table-editable-markdown-preview code,
.interactive-table-editable-markdown-preview mark,
.emailbot-editable-markdown-preview code,
.emailbot-editable-markdown-preview mark {
    padding: 0 !important;
    border: 0 !important;
    font: inherit !important;
    font-family: inherit !important;
    font-size: inherit !important;
    letter-spacing: inherit !important;
    line-height: inherit !important;
    vertical-align: baseline;
    border-radius: 0.2em;
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}

.tnx-markdown-editable-preview code,
.entry-editable-markdown-preview code,
.interactive-table-editable-markdown-preview code,
.emailbot-editable-markdown-preview code {
    box-shadow: inset 0 0 0 1px var(--tnx-markdown-code-border);
}

/*
 * The literal [] / [x] characters are kept as-is in the overlay for the same character-alignment
 * reason as code/textmarker above - no padding or border, background colour only - so this reuses
 * the same "mark" tokens rather than looking like a real checkbox control.
 */
.tnx-markdown-editable-checkbox {
    padding: 0 !important;
    border: 0 !important;
    border-radius: 0.2em;
    background: var(--link-color, var(--tnx-markdown-link-color, #2563eb));
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}

/* Checked dims the token itself too - never struck through, just lower opacity. */
.tnx-markdown-editable-checkbox--checked {
    opacity: 0.5;
}

/*
 * The overlay link stays out of hit testing (pointer-events:none) exactly like the bold /
 * marker / code decorations, so a click, a drag-select or a double-click always reaches the
 * transparent field underneath and behaves natively. It looks like an ordinary link (see the
 * global `a` rule in style.css): link colour, no underline and a 20% opacity drop on hover.
 */
.tnx-markdown-editable-link {
    pointer-events: none;
    color: var(--link-color, var(--tnx-markdown-link-color, #2563eb));
    text-decoration: none;
    transition: opacity 0.3s;
}

.tnx-markdown-editable-link.tnx-markdown-editable-link--hover {
    opacity: var(--tnx-link-hover-opacity, 0.8);
}

/*
 * The [] label is deliberately NOT visually upper-cased with text-transform: that changes glyph
 * widths, so a mixed-case label ("Amazon" -> "AMAZON") would push every character after it out of
 * sync with the transparent textarea underneath and land the caret several characters off. Auto
 * inserted and shared links already carry an upper-case label in their source, and the read-only
 * render upper-cases it there; the __label span is kept only as a structural hook.
 */

/*
 * The hover bridge flips this on for the length of one synchronous elementFromPoint lookup so the
 * otherwise pointer-events:none overlay (and its links) can be hit-tested, then flips it back.
 */
.tnx-markdown-hovertest,
.tnx-markdown-hovertest .tnx-markdown-editable-link {
    pointer-events: auto;
}

/* The "double-click to open the link" hover card (the native title never shows on a
 * pointer-events:none link), styled like the other floating cards. */
.tnx-markdown-link-hint {
    position: fixed;
    z-index: 100050;
    max-width: 260px;
    padding: 4px 9px;
    border: 3px solid var(--border-color, var(--border-subtle, #d1d5db));
    border-radius: 10px;
    background: var(--border-color, var(--border-subtle, #d1d5db));
    color: var(--text-color, var(--text, #111827));
    box-shadow: 0 0 0 2px var(--shadow-color, rgba(15, 23, 42, 0.16));
    font-size: 0.8rem;
    line-height: 1.3;
    white-space: nowrap;
    pointer-events: none;
}

.tnx-markdown-link-hint[hidden] {
    display: none !important;
}

/*
 * A force-bold field keeps its bold styling in the overlay too, and a human primary one (bold AND
 * accent) keeps its accent, even through inline code / marks whose own rules would otherwise reset
 * the weight or the colour. The two are separate because a field can be bold without being accent:
 * an important or a warning note is, and painting its overlay accent would flip its colour every
 * time the raw text is shown for a drag-select.
 */
.tnx-markdown-editable-content--bold,
.tnx-markdown-editable-content--bold * {
    font-weight: bold !important;
}

.tnx-markdown-editable-content--accent,
.tnx-markdown-editable-content--accent * {
    color: var(--accent-color) !important;
}

.tnx-markdown-preview-host {
    position: relative !important;
}

.tnx-markdown-editable-preview {
    position: absolute;
    z-index: 2;
    box-sizing: border-box;
    overflow: hidden;
    pointer-events: none;
    color: var(--text-color, var(--text, #111827));
}

.tnx-markdown-editable-preview[hidden] {
    display: none !important;
}

.tnx-markdown-editable-preview__content {
    min-width: 100%;
    min-height: 100%;
    box-sizing: border-box;
    white-space: pre-wrap;
    word-break: break-word;
    overflow-wrap: anywhere;
}

.tnx-markdown-editable-marker {
    font-weight: inherit;
    font-style: inherit;
}

/* Calculation-note results are a visual editing aid only. The raw note stays untouched so saving,
 * copying and caret positions remain exact. Every negative result is red and only the final result
 * after an equals sign is bold. */
.tnx-markdown-calculation-result--negative {
    color: #e53935 !important;
}

.tnx-markdown-calculation-result--final {
    font-weight: 700;
}

.tnx-markdown-editable-input.tnx-markdown-editable-active {
    color: transparent !important;
    -webkit-text-fill-color: transparent !important;
    caret-color: var(--text-color, var(--text, #111827)) !important;
}

.tnx-markdown-editable-input.tnx-markdown-native-pointer-interaction,
.entry-editable-markdown-shell--active > .tnx-markdown-editable-input.tnx-markdown-native-pointer-interaction,
.interactive-table-editable-markdown-shell--active > .tnx-markdown-editable-input.tnx-markdown-native-pointer-interaction,
.emailbot-editable-markdown-shell--active > .tnx-markdown-editable-input.tnx-markdown-native-pointer-interaction {
    color: var(--text-color, var(--text, #111827)) !important;
    -webkit-text-fill-color: var(--text-color, var(--text, #111827)) !important;
}

.tnx-markdown-editable-preview--native-pointer-interaction,
.entry-editable-markdown-shell.tnx-markdown-shell--native-pointer-interaction > .entry-editable-markdown-preview,
.interactive-table-editable-markdown-shell.tnx-markdown-shell--native-pointer-interaction > .interactive-table-editable-markdown-preview,
.emailbot-editable-markdown-shell.tnx-markdown-shell--native-pointer-interaction > .emailbot-editable-markdown-preview {
    visibility: hidden !important;
}

/* Calculation notes must keep the same rendered character layer while drag-selecting. Swapping
 * back to the proportional raw values changes the width of bold results and makes the text jump. */
.tnx-entry-note-dialog.is-calculation .tnx-markdown-editable-preview--native-pointer-interaction {
    visibility: visible !important;
}

.tnx-entry-note-dialog.is-calculation .tnx-entry-note-textarea.tnx-markdown-native-pointer-interaction {
    color: transparent !important;
    -webkit-text-fill-color: transparent !important;
}

.tnx-markdown-editable-input.tnx-markdown-editable-active::selection,
.entry-editable-markdown-shell--active > .form-control::selection,
.interactive-table-editable-markdown-shell--active > .interactive-table-input::selection,
.emailbot-editable-markdown-shell--active > textarea::selection {
    background: var(--tnx-markdown-selection-bg) !important;
    color: transparent !important;
    -webkit-text-fill-color: transparent !important;
}

/*
 * While the user is actively drag-selecting, the rendered preview is hidden and the native field
 * is the only text layer. Its selected source therefore becomes visible for that interaction only.
 * Once the preview returns, keeping the source transparent prevents italic (and every other format)
 * from being painted twice while the selection remains active.
 */
.tnx-markdown-editable-input.tnx-markdown-native-pointer-interaction::selection,
.entry-editable-markdown-shell--active.tnx-markdown-shell--native-pointer-interaction > .form-control::selection,
.interactive-table-editable-markdown-shell--active.tnx-markdown-shell--native-pointer-interaction > .interactive-table-input::selection,
.emailbot-editable-markdown-shell--active.tnx-markdown-shell--native-pointer-interaction > textarea::selection {
    background: var(--tnx-markdown-selection-bg) !important;
    color: var(--text-color, var(--text, #111827)) !important;
    -webkit-text-fill-color: var(--text-color, var(--text, #111827)) !important;
}

.tnx-entry-note-dialog.is-calculation .tnx-entry-note-textarea.tnx-markdown-native-pointer-interaction::selection {
    color: transparent !important;
    -webkit-text-fill-color: transparent !important;
}

/*
 * A human primary field is bold + accent, so it must stay accent while the raw text is shown for a
 * drag-select and while characters are selected - otherwise it flashes to the normal text colour
 * ("jumps") every time text is selected to apply formatting. The marker class is set on the field
 * itself (see MarkdownRuntime.js) and never read off an ancestor, so a normal field nested inside a
 * human primary block keeps its own colour instead of flashing accent for the drag.
 */
.tnx-markdown-editable-input--human-primary.tnx-markdown-editable-input.tnx-markdown-native-pointer-interaction {
    color: var(--accent-color) !important;
    -webkit-text-fill-color: var(--accent-color) !important;
}

.tnx-markdown-editable-input--human-primary.tnx-markdown-editable-input.tnx-markdown-editable-active {
    caret-color: var(--accent-color) !important;
}

.tnx-markdown-editable-input--human-primary.tnx-markdown-editable-input.tnx-markdown-editable-active.tnx-markdown-native-pointer-interaction::selection {
    color: var(--accent-color) !important;
    -webkit-text-fill-color: var(--accent-color) !important;
}

.tnx-markdown-selection-toolbar {
    position: fixed;
    z-index: 100050;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    width: auto;
    max-width: calc(100vw - 16px);
    padding: 3px;
    border: 3px solid var(--border-color, var(--border-subtle, #d1d5db));
    border-radius: 12px;
    background: var(--border-color, var(--border-subtle, #d1d5db));
    box-shadow: 0 0 0 2px var(--shadow-color, rgba(15, 23, 42, 0.16));
}

.tnx-markdown-selection-toolbar[hidden] {
    display: none !important;
}

.tnx-markdown-selection-action,
.tnx-markdown-selection-action:visited {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    flex: 0 0 36px;
    aspect-ratio: 1 / 1;
    box-sizing: border-box;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--input-bg, var(--bg-alt, #fff));
    color: var(--text-color, var(--text, #111827));
    box-shadow: 0 1px 2px var(--shadow-color, rgba(15, 23, 42, 0.16));
    font-size: 1rem;
    line-height: 1;
    text-decoration: none;
    cursor: pointer;
    opacity: 0.5;
    transition: opacity 0.2s ease;
}

/* Inactive action: 0.5, dropping another 20% to 0.4 on hover. */
.tnx-markdown-selection-action:hover,
.tnx-markdown-selection-action:focus-visible {
    opacity: 0.4 !important;
}

/* Active action (the selection is already fully wrapped in this format): 1, dropping 20% to 0.8 on hover. */
.tnx-markdown-selection-action--active {
    opacity: 1 !important;
}

.tnx-markdown-selection-action--active:hover,
.tnx-markdown-selection-action--active:focus-visible {
    opacity: 0.8 !important;
}

@media (max-width: 520px) {
    .tnx-markdown-selection-toolbar {
        gap: 4px;
    }

    .tnx-markdown-selection-action,
    .tnx-markdown-selection-action:visited {
        width: 34px;
        height: 34px;
        min-width: 34px;
        min-height: 34px;
        flex-basis: 34px;
    }
}
