: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: #854d0e;
    --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: #fef08a;
    --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;
}

.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 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 (a human primary) keeps its bold + accent styling in the overlay too, even
 * through inline code / marks whose own rules would otherwise reset the weight or colour.
 */
.tnx-markdown-editable-content--bold,
.tnx-markdown-editable-content--bold * {
    font-weight: bold !important;
    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;
}

.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;
}

.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: var(--text-color, var(--text, #111827)) !important;
    -webkit-text-fill-color: var(--text-color, var(--text, #111827)) !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.
 */
.human-primary .tnx-markdown-editable-input.tnx-markdown-native-pointer-interaction,
.interactive-table-cell--human-primary .tnx-markdown-editable-input.tnx-markdown-native-pointer-interaction {
    color: var(--accent-color) !important;
    -webkit-text-fill-color: var(--accent-color) !important;
}

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

.human-primary .tnx-markdown-editable-input.tnx-markdown-editable-active::selection,
.interactive-table-cell--human-primary .tnx-markdown-editable-input.tnx-markdown-editable-active::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;
    }
}
