pb-formula {
    position: relative;
}

pb-formula::part(latex-command) {
    position: absolute;
    z-index: 1;
    bottom: 100%;
    left: 0;
    margin: 0;
    padding: 3px 6px;
    border: 1px solid #d5d2c9;
    border-radius: var(--bebb-control-radius, 5px);
    background: #fff;
    color: var(--bernoulli-red, #ba1e1a);
    font: 13px/1.4 var(--bebb-font-ui, sans-serif);
    opacity: 0;
    pointer-events: none;
    cursor: pointer;
}

pb-formula:hover::part(latex-command),
pb-formula:focus-within::part(latex-command) {
    opacity: 1;
    pointer-events: auto;
}

:host(.bebb-register-links-hidden) pb-formula::part(latex-command) {
    display: none;
}

.bebb-formula-dialog[open] {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.bebb-formula-dialog {
    position: fixed;
    inset: auto;
    top: 16px;
    left: 16px;
    z-index: 10000;
    box-sizing: border-box;
    align-items: stretch;
    justify-content: normal;
    width: 360px;
    min-width: min(360px, calc(100vw - 2rem));
    max-width: calc(100vw - 2rem);
    height: fit-content;
    min-height: min(180px, calc(100dvh - 2rem));
    max-height: calc(100dvh - 2rem);
    margin: 0;
    padding: 1.25rem;
    border: 0;
    border-radius: var(--bebb-control-radius, 5px);
    background: #fff;
    color: #2d2b26;
    box-shadow: 0 4px 16px #0002;
    backdrop-filter: none;
    font: 16px/1.5 var(--bebb-font-ui, sans-serif);
    overflow: auto;
    resize: both;
}

.bebb-formula-dialog > :is(header, footer) {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-shrink: 0;
    margin: 0;
    padding: 0;
    background: transparent;
}

.bebb-formula-dialog > header {
    cursor: move;
    touch-action: none;
    user-select: none;
}

.bebb-formula-dialog h2 {
    flex: 1;
    min-width: 0;
    overflow-wrap: anywhere;
    margin: 0;
    color: inherit;
    font: 18px/1.5 var(--bebb-font-ui, sans-serif);
}

.bebb-formula-dialog footer [role="status"] {
    min-width: 0;
    overflow-wrap: anywhere;
}

.bebb-formula-dialog select {
    flex: 0 1 auto;
    width: auto;
    min-width: 7rem;
    height: 32px;
    margin: 0;
    padding: 0.2rem 2rem 0.2rem 0.5rem;
    border: 0;
    border-radius: var(--bebb-control-radius, 5px);
    background-color: #f5f5f5;
    color: inherit;
    font: inherit;
}

.bebb-formula-dialog__notice {
    margin: 0;
    font-size: 14px;
}

.bebb-formula-dialog [hidden] {
    display: none;
}

.bebb-formula-dialog textarea {
    box-sizing: border-box;
    flex: 1 1 auto;
    width: 100%;
    min-height: 3rem;
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 0.75rem;
    border: 0;
    border-radius: var(--bebb-control-radius, 5px);
    background: #f5f5f5;
    color: inherit;
    font: 14px/1.5 Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
    white-space: pre;
    overflow-wrap: normal;
    resize: none;
}

.bebb-formula-dialog textarea:focus {
    outline: none;
    box-shadow: none;
}

.bebb-formula-dialog button {
    display: grid;
    place-items: center;
    flex: 0 0 32px;
    width: 32px;
    height: 32px;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: var(--bebb-control-radius, 5px);
    background: #f5f5f5;
    color: inherit;
    font: 24px/1 var(--bebb-font-ui, sans-serif);
    cursor: pointer;
}

.bebb-formula-dialog button:hover:not(:disabled) {
    background: var(--bebb-control-hover-background, #e3e3e3);
}

.bebb-formula-dialog button:disabled {
    opacity: 0.5;
    cursor: default;
}

pb-formula::part(latex-command):focus-visible,
.bebb-formula-dialog :is(button, select, h2):focus-visible {
    outline: 2px solid var(--bebb-control-focus-color, #393939);
    outline-offset: 2px;
}

@media (max-height: 360px) {
    .bebb-formula-dialog {
        gap: 0.5rem;
        padding: 0.75rem;
    }
}
