/*
 * Braille translator (feldwaldwiese/braille-translator)
 *
 * Only what the element needs to work: the 2×3 dot grid, raised/empty dots (drawn as
 * borders, so they stay visible in forced colors mode and in print), cells side
 * by side, and the [hidden]/.offscreen helpers. Everything is wrapped in :where(), so it
 * has no specificity – any rule in the site stylesheet wins without !important.
 *
 * The look (box around the form, font sizes, borders, spacing, colors) belongs in the
 * site stylesheet. The form uses Contao's markup (.widget.widget-text, label, input.text,
 * .widget.widget-submit, button.submit), so the site's form styles apply. Sizes and dot
 * colors can also be set via custom properties, e.g.
 *
 *   .content-braille-translator { --braille-dot-color: #a10086; }
 */

:where(.content-braille-translator) {
    --braille-cell-width: 36px;
    --braille-cell-height: 57px;
    --braille-dot-size: 13px;          /* raised dot */
    --braille-dot-empty-size: 4px;     /* position without a raised dot */
    --braille-dot-color: currentColor;
    --braille-dot-empty-color: currentColor;
    --braille-gap: 25px;               /* between the output items */
}

/*
 * The dot size is built from border + content width (see below); a site-wide
 * "* { box-sizing: border-box }" would shrink it, so this one rule needs real specificity.
 */
.content-braille-translator .braille-dot {
    box-sizing: content-box;
}

/* Script-controlled visibility must win over display rules of the site (.widget etc.) */
.content-braille-translator [hidden] {
    display: none !important;
}

/* Fallback for sites without an .offscreen class */
:where(.content-braille-translator .offscreen) {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

:where(.content-braille-translator .braille-output) {
    display: flex;
    flex-wrap: wrap;
    gap: var(--braille-gap);
}

:where(.content-braille-translator .braille-output-item) {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    width: min-content;                /* as wide as the cell(s); long sign labels wrap */
    min-width: var(--braille-cell-width);
}

:where(.content-braille-translator .braille-cells) {
    display: flex;
    justify-content: center;
    gap: calc(var(--braille-gap) / 3);
}

/* 2 columns × 3 rows, filled column by column: dots 1–3 left, 4–6 right */
:where(.content-braille-translator .braille-cell) {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-auto-flow: column;
    width: var(--braille-cell-width);
    height: var(--braille-cell-height);
}

/*
 * Dots are drawn as borders, not backgrounds: backgrounds disappear in forced colors mode
 * (Windows contrast themes), when printing without background graphics and with user
 * stylesheets. A 0×0 element with a round border of half the size is a filled circle.
 */
:where(.content-braille-translator .braille-dot) {
    place-self: center;
    width: 0;
    height: 0;
    border-radius: 50%;
    border: calc(var(--braille-dot-empty-size) / 2) solid var(--braille-dot-empty-color);
}

:where(.content-braille-translator .braille-dot--raised) {
    border-width: calc(var(--braille-dot-size) / 2);
    border-color: var(--braille-dot-color);
}

/*
 * Browsers round border widths down to whole pixels, so a dot with an odd size (13px) would
 * become 1px smaller. On screen (not in forced colors mode) the missing pixel is filled by
 * the content box with a background; the border alone still draws the dot everywhere else.
 */
@supports (width: round(down, 1.5px, 1px)) {
    @media screen and (forced-colors: none) {
        :where(.content-braille-translator .braille-dot) {
            border-width: round(down, calc(var(--braille-dot-empty-size) / 2), 1px);
            width: calc(var(--braille-dot-empty-size) - 2 * round(down, calc(var(--braille-dot-empty-size) / 2), 1px));
            height: calc(var(--braille-dot-empty-size) - 2 * round(down, calc(var(--braille-dot-empty-size) / 2), 1px));
            background: var(--braille-dot-empty-color);
        }

        :where(.content-braille-translator .braille-dot--raised) {
            border-width: round(down, calc(var(--braille-dot-size) / 2), 1px);
            width: calc(var(--braille-dot-size) - 2 * round(down, calc(var(--braille-dot-size) / 2), 1px));
            height: calc(var(--braille-dot-size) - 2 * round(down, calc(var(--braille-dot-size) / 2), 1px));
            background: var(--braille-dot-color);
        }
    }
}

:where(.content-braille-translator .braille-char) {
    display: block;
    min-height: 1.4em;
    text-align: center;
    white-space: pre;
}

/* "Zahlzeichen" / "Buchstabenzeichen" must fit under one cell */
:where(.content-braille-translator .braille-output-item--number-sign .braille-char,
       .content-braille-translator .braille-output-item--letter-sign .braille-char) {
    font-size: .6em;
    line-height: 1.2;
    white-space: normal;
    hyphens: manual;
}

:where(.content-braille-translator .braille-output-item--unsupported .braille-cell) {
    outline: 1px dashed currentColor;
    outline-offset: 2px;
}
