/* ==========================================================================
   mb.bes.is/Regex — the regex tester's own stylesheet.

   RegexLayout links this after app.css, on /Regex only. Like git.css it
   overrides the base outright: this is a workbench of two editors and three
   panels, not a column of prose, so the measure, the fluid type and the
   generous spacing all go.

   Unlike the rest of the site it spends colour, because here colour carries
   the answer: which stretch of text is which match, and which group of the
   pattern caught which part of it. The pattern's own syntax stays in the
   site's grayscale idiom of weight, slope and a wash; the hues are kept for
   matches and groups, and every one comes in a light and a dark shade.

   1. Tokens          the dense scale, match and group colours
   2. Base            what app.css set for prose, undone
   3. Frame           page, status bar, the two columns, footer
   4. Blocks          the bordered box every section is
   5. Editors         the textarea-over-a-div overlay
   6. Pattern         syntax and error marks in the expression
   7. Test string     match and group marks
   8. Panels          explanation, match information, reference
   9. Tools           tabs, replacement, output
   ========================================================================== */


/* 1. Tokens ================================================================ */

html {
    font-size: 100%;
}

.rx {
    font-size: 0.875rem;
    line-height: 1.5;

    --g0: 0.125rem;
    --g1: 0.25rem;
    --g2: 0.5rem;
    --g3: 1rem;

    --g-line: light-dark(#d5d5d5, #2b2b2b);
    --g-fill: light-dark(#f1f1f1, #131313);
    --g-dim: light-dark(#6b6b6b, #909090);
    --g-wash: light-dark(rgb(0 0 0 / 8%), rgb(255 255 255 / 10%));
    --g-mark: light-dark(#ffe98a, #5a4a00);

    /* Matches alternate between two shades of one hue, so two matches that touch still read as
       two. */
    --m0: light-dark(#b9d6ff, #1f4472);
    --m1: light-dark(#dce9ff, #15304f);

    /* One hue per group, recycled past six. Opaque, because a group's colour replaces the match's
       under it rather than mixing with it. */
    --g-0: light-dark(#bde6b5, #1f5121);
    --g-1: light-dark(#ffd3a1, #613710);
    --g-2: light-dark(#dfc6ff, #48286f);
    --g-3: light-dark(#b0e4df, #124c47);
    --g-4: light-dark(#ffc2da, #63203e);
    --g-5: light-dark(#efe08f, #534811);
}

.g0 { --group: var(--g-0); }
.g1 { --group: var(--g-1); }
.g2 { --group: var(--g-2); }
.g3 { --group: var(--g-3); }
.g4 { --group: var(--g-4); }
.g5 { --group: var(--g-5); }


/* 2. Base ================================================================== */

.rx :is(h1, h2, h3, h4, h5, h6) {
    margin-block: 0;
    font-size: 1em;
    line-height: inherit;
    text-wrap: nowrap;
}

.rx p {
    margin-block: 0;
}

.rx :is(ul, ol) {
    margin-block: 0;
    padding-inline-start: 0;
    list-style: none;
}

.rx pre {
    margin-block: 0;
}

.rx a {
    color: inherit;
    text-decoration: none;
}

.rx a:hover,
.rx a:focus-visible {
    background-color: transparent;
    color: inherit;
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 0.15em;
}

.rx :is(button, input, textarea) {
    font: inherit;
    border-radius: 0;
}

.rx button {
    min-inline-size: 0;
    padding: 0 var(--g2);
    border-color: var(--g-line);
}

.rx label {
    display: inline;
}

.rx code {
    font: inherit;
}


/* 3. Frame ================================================================= */

.rx {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: var(--g2);
    max-inline-size: 110rem;
    inline-size: 100%;
    margin-inline: auto;
    padding: var(--g2);
}

/* The status line, inverted like the git browser's. */
.rx-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--g1) var(--g3);
    padding: var(--g1) var(--g2);
    background-color: var(--color);
    color: var(--background);
}

.rx-bar :focus-visible {
    outline-color: var(--background);
}

.rx-crumbs {
    display: flex;
    gap: var(--g1);
}

.rx-crumbs .sep {
    opacity: 60%;
}

.rx-actions {
    display: flex;
    align-items: center;
    gap: var(--g3);
}

.rx-bar button {
    padding: 0;
    border: none;
    background: none;
    color: inherit;
}

.rx-bar button:hover {
    background: none;
    color: inherit;
    text-decoration: underline;
}

.rx-actions > *::before {
    content: "[";
    opacity: 50%;
}

.rx-actions > *::after {
    content: "]";
    opacity: 50%;
}

/* The editors on the left, what they produce on the right. Below about a laptop's width the
   panels drop under the editors, where they are still next to nothing they depend on. */
.rx-main {
    flex: 1;
    display: grid;
    grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
    align-items: start;
    gap: var(--g2);
    border: none;
    padding-block: 0;
}

@media (width < 60rem) {
    .rx-main {
        grid-template-columns: minmax(0, 1fr);
    }
}

.rx-work,
.rx-side {
    display: flex;
    flex-direction: column;
    gap: var(--g2);
    min-inline-size: 0;
}

.rx-foot {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: var(--g1) var(--g3);
    padding-block-start: var(--g2);
    border-block-start: 1px solid var(--g-line);
    color: var(--g-dim);
}


/* 4. Blocks ================================================================ */

.block {
    border: 1px solid var(--g-line);
    min-inline-size: 0;
}

.block-head {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--g1) var(--g2);
    padding: var(--g1) var(--g2);
    background-color: var(--g-fill);
    border-block-end: 1px solid var(--g-line);
    font-weight: 700;
}

.block-note {
    font-weight: 400;
    color: var(--g-dim);
}

.empty {
    padding: var(--g2);
    color: var(--g-dim);
}


/* 5. Editors =============================================================== */

/* The textarea and the div that draws its highlighting share one grid cell. The div is in flow
   and sets the height; the textarea stretches to it and never has anything to scroll. For the two
   to wrap at exactly the same place, everything that moves a glyph is set here, on both. */
.overlay {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    position: relative;
}

.overlay > .backdrop,
.overlay > textarea {
    grid-area: 1 / 1;
    margin: 0;
    border: 0;
    padding: var(--g2);
    font: inherit;
    line-height: inherit;
    letter-spacing: normal;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: normal;
    tab-size: 4;
    min-inline-size: 0;
}

.overlay > .backdrop {
    pointer-events: none;
    color: var(--color);
}

/* The textarea is only there for the caret, the selection and the keyboard. Its own text is
   transparent so the backdrop's copy, highlighted, is what shows through. */
.overlay > textarea {
    display: block;
    inline-size: 100%;
    min-block-size: 0;
    resize: none;
    overflow: hidden;
    background: transparent;
    color: transparent;
    caret-color: var(--color);
}

.overlay > textarea::selection {
    background-color: var(--color);
    color: var(--background);
}

.overlay > textarea:focus-visible {
    outline: none;
}

.overlay:focus-within {
    outline: 2px solid var(--color);
    outline-offset: -2px;
}

.text-box {
    min-block-size: 16lh;
}

.text-box > .backdrop {
    min-block-size: 16lh;
}


/* 6. Pattern =============================================================== */

/* /pattern/flags on one line, the way it is written in code. */
.rx-expression {
    display: flex;
    /* Baseline, so the slashes and flags sit on the pattern's first line however many it wraps to. */
    align-items: baseline;
    padding-inline: var(--g2) 0;
}

.rx-expression .delim {
    padding-block: var(--g2);
    color: var(--g-dim);
    user-select: none;
    -webkit-user-select: none;
}

.pattern-box {
    flex: 1;
    min-inline-size: 0;
    font-size: 1.15em;
}

.pattern-box > .backdrop,
.pattern-box > textarea {
    padding-inline: var(--g1);
}

/* Flags are toggles that read as the letters they add. They stay on the closing slash's line even
   on a phone, where the pattern wraps instead. */
.flags {
    display: flex;
    flex: none;
    padding-inline: var(--g1) var(--g2);
}

.flags button {
    padding: 0 var(--g0);
    border: none;
    background: none;
    color: var(--g-dim);
}

.flags button[aria-pressed="true"] {
    color: var(--color);
    font-weight: 700;
}

.flags button:hover {
    background-color: var(--g-fill);
    color: var(--color);
}

/* Each flag explains itself on hover and on focus, straight away, rather than through a title
   attribute: those take a second to appear, never appear for the keyboard or a touch screen, and
   the letters alone mean nothing until you have learnt them. Anchored on the right edge of the
   whole group rather than of each letter, because the flags sit at the right edge of their column
   and on a phone a tip hung off the left-most one would run off the screen. */
.flags {
    position: relative;
}

.flag .tip {
    position: absolute;
    inset-block-start: calc(100% + var(--g1));
    inset-inline-end: 0;
    z-index: 1;
    display: none;
    inline-size: max-content;
    max-inline-size: min(34ch, 80vw);
    padding: var(--g1) var(--g2);
    background-color: var(--color);
    color: var(--background);
    font-size: 0.875rem;
    font-weight: 400;
    white-space: normal;
    pointer-events: none;
}

.flag:hover .tip,
.flag:focus-within .tip {
    display: grid;
    gap: var(--g0);
}

.flag .tip code {
    opacity: 70%;
}

.rx-error {
    padding: var(--g1) var(--g2);
    border-block-start: 1px solid var(--g-line);
    color: var(--error);
}

/* The language: anchors, quantifiers, alternation. */
.t-meta {
    font-weight: 700;
}

/* An escape is a character spelled out, so it is the character, quietly. */
.t-escape {
    font-weight: 700;
    color: var(--g-dim);
}

/* A class stands for a set of characters, which is a value like a string is. */
.t-set {
    background-color: var(--g-wash);
}

.t-look {
    font-weight: 700;
    color: var(--g-dim);
}

/* A capturing group's parens carry the group's colour, the same one its text gets below. */
.t-group {
    font-weight: 700;
    background-color: var(--group);
}

.t-ref {
    text-decoration-line: underline;
    text-decoration-thickness: 2px;
    text-decoration-color: var(--group, var(--error));
    text-underline-offset: 0.2em;
}

.t-error {
    text-decoration-line: underline;
    text-decoration-style: wavy;
    text-decoration-color: var(--error);
    text-decoration-skip-ink: none;
}

/* Where the line of the explanation under the pointer is. Last, so it wins over a wash. */
.backdrop .hl {
    background-color: var(--g-mark);
}


/* 7. Test string =========================================================== */

.hit.m0 {
    background-color: var(--m0);
}

.hit.m1 {
    background-color: var(--m1);
}

.hit:is(.g0, .g1, .g2, .g3, .g4, .g5) {
    background-color: var(--group);
}

/* An empty match has no text to colour, so it is a bar between two characters. It must not take
   up any room of its own, or every line after it would wrap differently from the textarea's. */
.hit.empty {
    position: relative;
}

.hit.empty::before {
    content: "";
    position: absolute;
    inset-block: 0;
    inset-inline-start: -1px;
    inline-size: 2px;
    background-color: var(--info);
}

/* The match the caret is in, or the one picked from the list. */
.hit.sel {
    text-decoration-line: underline;
    text-decoration-thickness: 2px;
    text-underline-offset: 0.25em;
    text-decoration-skip-ink: none;
}

.hit.empty.sel::before {
    background-color: var(--color);
}


/* 8. Panels ================================================================ */

.explain,
.matches {
    max-block-size: 60vh;
    overflow: auto;
}

/* -- Explanation ----------------------------------------------------------- */

.explain {
    padding-block: var(--g1);
}

.explain ul ul {
    margin-inline-start: calc(var(--g2) + 1px);
    padding-inline-start: var(--g2);
    border-inline-start: 1px solid var(--g-line);
}

.explain .line {
    display: flex;
    gap: var(--g2);
    padding: var(--g0) var(--g2);
    cursor: default;
}

.explain .line:hover {
    background-color: var(--g-fill);
}

.explain .line.here {
    background-color: var(--g-mark);
}

.explain .line code {
    flex: 0 1 auto;
    max-inline-size: 45%;
    padding-inline: var(--g1);
    background-color: var(--group, var(--g-wash));
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    font-weight: 700;
}

.explain .line span {
    flex: 1;
    min-inline-size: 0;
}

.explain .line.bad span {
    color: var(--error);
}

/* -- Match information ----------------------------------------------------- */

.matches table {
    inline-size: 100%;
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
}

.matches :is(th, td) {
    padding: var(--g0) var(--g2);
    text-align: start;
    vertical-align: baseline;
    font-weight: 400;
}

.matches th {
    text-wrap: nowrap;
    color: var(--g-dim);
}

.matches tbody + tbody tr:first-child > * {
    border-block-start: 1px solid var(--g-line);
}

.matches tbody.sel {
    background-color: var(--g-fill);
}

.matches .whole th {
    color: inherit;
}

.matches .num {
    text-wrap: nowrap;
    color: var(--g-dim);
    text-align: end;
}

.matches .val {
    inline-size: 100%;
}

/* Block, not inline-block: an inline-block that scrolls takes its baseline from its bottom edge,
   which lifts a one-line value above the rest of its row. */
.matches .val code {
    display: block;
    max-block-size: 6lh;
    overflow: auto;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.missing {
    color: var(--g-dim);
    font-style: italic;
}

.pick {
    border: none;
    font-weight: 700;
    padding: 0 var(--g1);
}

.pick.m0 {
    background-color: var(--m0);
}

.pick.m1 {
    background-color: var(--m1);
}

.pick:hover {
    background-color: var(--color);
    color: var(--background);
}

.swatch {
    display: inline-block;
    inline-size: 0.8em;
    block-size: 0.8em;
    margin-inline-end: var(--g1);
    background-color: var(--group);
    vertical-align: -0.05em;
}

/* -- Reference ------------------------------------------------------------- */

.reference > summary {
    cursor: pointer;
    list-style: none;
}

.reference > summary::-webkit-details-marker {
    display: none;
}

.reference > summary::marker {
    content: "";
}

.reference > summary h2::before {
    content: "+ ";
}

.reference[open] > summary h2::before {
    content: "- ";
}

.reference:not([open]) > summary {
    border-block-end: none;
}

.reference h3 {
    padding: var(--g2) var(--g2) var(--g0);
    color: var(--g-dim);
}

.reference ul {
    padding-inline: var(--g2);
}

.reference li {
    display: grid;
    grid-template-columns: 14ch minmax(0, 1fr);
    gap: var(--g2);
    align-items: baseline;
}

.reference li > code,
.reference .insert {
    justify-self: start;
    padding: 0 var(--g1);
    font-weight: 700;
    background-color: var(--g-wash);
    border: none;
    text-align: start;
    overflow-wrap: anywhere;
}

.reference .insert:hover {
    background-color: var(--color);
    color: var(--background);
}

.reference ul:last-child {
    padding-block-end: var(--g2);
}


/* 9. Tools ================================================================= */

.rx-tabs {
    display: flex;
    gap: var(--g1);
}

.rx-tabs button {
    border: none;
    background: none;
    font-weight: 700;
    padding-inline: var(--g1);
}

.rx-tabs button::before {
    content: "[";
    opacity: 50%;
}

.rx-tabs button::after {
    content: "]";
    opacity: 50%;
}

.rx-tabs button[aria-selected="true"] {
    background-color: var(--color);
    color: var(--background);
}

.rx-tabs button[aria-selected="true"]::before,
.rx-tabs button[aria-selected="true"]::after {
    opacity: 100%;
}

.rx .rx-replacement {
    display: flex;
    align-items: baseline;
    gap: var(--g2);
    padding: var(--g1) var(--g2);
    border-block-end: 1px solid var(--g-line);
    color: var(--g-dim);
}

.rx .rx-replacement[hidden] {
    display: none;
}

.rx-replacement input {
    flex: 1;
    min-inline-size: 0;
    padding: var(--g0) var(--g1);
    border: 1px solid var(--g-line);
    color: var(--color);
}

.rx-output {
    min-block-size: 3lh;
    max-block-size: 40vh;
    overflow: auto;
    padding: var(--g2);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.parts {
    counter-reset: part -1;
}

.parts li {
    counter-increment: part;
    display: grid;
    grid-template-columns: 4ch minmax(0, 1fr);
    gap: var(--g2);
}

.parts li::before {
    content: counter(part);
    color: var(--g-dim);
    text-align: end;
}

.parts code {
    justify-self: start;
    background-color: var(--g-wash);
    white-space: pre-wrap;
}
