/* CMS (layouts/cms). Every stylesheet loads on every page, so all of this is scoped to body.cms.
   Same palette, type and .button as the site, with dark grey fields. */

body.cms {
    --cms-line: rgb(255 255 255 / 0.15);
    --cms-field-line: rgb(255 255 255 / 0.3);
    --cms-muted: rgb(255 255 255 / 0.6);
    --cms-danger: #E5484D;
    --cms-published: #4DA995;
    --cms-draft: #DCB63D;

    min-height: 100svh;
    color: var(--white);
    font-family: "neue-haas-grotesk-text", sans-serif;
    font-weight: 500;

    a:hover {
        text-decoration: underline;
    }

    a.button:hover {
        text-decoration: none;
    }

    code {
        font-family: ui-monospace, monospace;
        font-size: 0.9em;
    }
}

.cms {
    /* Colour bar and header stick to the top together. A sticky element can't leave its parent, so the wrapper
       around both is what sticks, not each of them. */
    .cms-header-wrapper {
        position: sticky;
        top: 0;
        z-index: 10;
        background-color: black;
    }

    /* The site's bar (shared/_colour_bar, application.css), always shown, thin and in the flow */
    section.colour-bar {
        position: static;
        width: auto;
        height: 0.5rem;
        transform: none;
        transition: none;
    }

    /* Header */
    .cms-header {
        /* Undo the site's fixed, scroll-revealed header (application.css) */
        transform: none;
        transition: none;
        width: auto;
        max-width: none;
        height: auto;
        flex-direction: row;
        position: static;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 1rem 2.5rem;
        padding: 1rem 0;
        background-color: black;
        border-bottom: 1px solid var(--cms-line);
    }

    .cms-brand {
        display: flex;
        align-items: baseline;
        gap: 0.75rem;

        &:hover {
            text-decoration: none;
        }
    }

    .cms-nav {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 0.25rem;

        a {
            padding: 0.4rem 0.75rem;
            background-color: var(--dark-grey);

            &:hover {
                text-decoration: none;
                background-color: var(--white);
                color: black;
            }

            &[aria-current="page"] {
                background-color: var(--white);
                color: black;
            }
        }
    }

    .cms-account {
        margin-left: auto;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 1rem;
    }

    /* Main */
    .cms-main {
        padding: 2rem 0 4rem;
        display: flex;
        flex-direction: column;
        gap: 1.5rem;
    }

    .cms-page-header {
        display: flex;
        flex-wrap: wrap;
        align-items: flex-end;
        justify-content: space-between;
        gap: 1rem;
        padding-bottom: 1.5rem;
        border-bottom: 1px solid var(--cms-line);
    }

    .cms-actions {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 1rem;
    }

    .cms-muted, .cms-hint {
        color: var(--cms-muted);
    }

    .cms-hint {
        font-size: 0.9rem;
        font-weight: 500;
    }

    .cms-strong {
        font-size: 1.1rem;
    }

    .cms-empty {
        padding: 2rem;
        text-align: center;
        color: var(--cms-muted);
        border: 1px dashed var(--cms-line);
    }

    .cms-section-title {
        margin-top: 1rem;
    }

    .cms-visually-hidden {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
    }

    /* Flash and errors */
    .cms-flash {
        padding: 0.75rem 1rem;
        background-color: var(--white);
        color: black;
        border-left: 0.5rem solid var(--cms-published);

        &.is-alert {
            border-left-color: var(--cms-danger);
        }
    }

    .cms-errors {
        padding: 1rem;
        border: 2px solid var(--cms-danger);

        ul {
            margin-top: 0.5rem;
            padding-left: 1.25rem;
            list-style: disc;
        }

        p {
            font-size: 1rem;
        }
    }

    /* Buttons and links */
    .cms-link {
        background: none;
        border: none;
        padding: 0;
        color: inherit;
        font: inherit;
        cursor: pointer;
        text-decoration: underline;
        text-underline-offset: 0.2rem;

        &.is-danger {
            color: var(--cms-danger);
        }
    }

    .cms-icon-button {
        width: 2rem;
        height: 2rem;
        background-color: var(--dark-grey);
        color: var(--white);
        border: none;
        font: inherit;
        cursor: pointer;

        &:hover:not(:disabled) {
            background-color: var(--white);
            color: black;
        }

        &:disabled {
            opacity: 0.3;
            cursor: default;
        }
    }

    form.button_to, .button_to {
        display: inline;
    }

    /* Status, chips, swatches, icons */
    .cms-status {
        display: inline-flex;
        align-items: center;
        gap: 0.4rem;

        &::before {
            content: "";
            width: 0.6rem;
            height: 0.6rem;
            border-radius: 50%;
            background-color: currentColor;
        }

        &.is-published { color: var(--cms-published); }
        &.is-draft { color: var(--cms-draft); }
    }

    .cms-chips {
        display: inline-flex;
        flex-wrap: wrap;
        gap: 0.25rem;
    }

    .cms-chip {
        padding: 0.25rem 0.4rem;
        background-color: var(--dark-grey);
        white-space: nowrap;

        /* Characteristic colour, with black text on light colours and white on dark */
        &[style*="--chip-colour"] {
            background-color: var(--chip-colour);
            color: oklch(from var(--chip-colour) clamp(0, (0.62 - l) * 1000, 1) 0 0);
        }
    }

    .cms-swatch {
        display: inline-block;
        flex: none;
        width: 1rem;
        height: 1rem;
        border: 1px solid var(--cms-line);
        vertical-align: middle;
    }

    .cms-icon {
        width: 1.25rem;
        height: 1.25rem;
        object-fit: contain;
    }

    /* Filters */
    .cms-filters {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 0.5rem 1rem;

        input[type="search"] {
            flex: 1 1 16rem;
            max-width: 24rem;
        }
    }

    /* Tables: stacked cards on phones */
    .cms-table {
        width: 100%;
        border-collapse: collapse;

        th {
            text-align: left;
            font-weight: 500;
            color: var(--cms-muted);
        }

        th, td {
            padding: 0.75rem 1rem 0.75rem 0;
            border-bottom: 1px solid var(--cms-line);
            vertical-align: middle;
        }

        tbody tr {
            position: relative;
        }

        tbody tr:hover {
            background-color: rgb(255 255 255 / 0.04);
        }

        a:hover {
            text-decoration: none;
        }

        /* The row's edit link covers the whole row, other links and tooltips sit above it */
        .cms-row-link::after {
            content: "";
            position: absolute;
            inset: 0;
        }

        tbody tr:has(.cms-row-link) {
            cursor: pointer;
        }

        a:not(.cms-row-link), [title] {
            position: relative;
            z-index: 1;
        }

        @media (width < 50rem) {
            thead {
                display: none;
            }

            tr {
                display: block;
                padding: 0.75rem 0;
                border-bottom: 1px solid var(--cms-line);
            }

            td {
                display: flex;
                gap: 1rem;
                padding: 0.2rem 0;
                border: none;

                &::before {
                    content: attr(data-label);
                    flex: 0 0 8rem;
                    color: var(--cms-muted);
                }
            }
        }
    }

    /* Forms */
    .cms-form {
        display: flex;
        flex-direction: column;
        gap: 1.5rem;
    }

    .cms-narrow { max-width: 40rem; }
    .cms-medium { max-width: 60rem; }

    .cms-columns {
        display: grid;
        grid-template-columns: minmax(0, 2fr) minmax(18rem, 1fr);
        gap: 2rem;
        align-items: start;

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

    .cms-column-main, .cms-column-side {
        display: flex;
        flex-direction: column;
        gap: 1.5rem;
    }

    .cms-field {
        display: flex;
        flex-direction: column;
        gap: 0.4rem;
    }

    label, legend {
        font-weight: 600;
    }

    input:is([type="text"], [type="email"], [type="password"], [type="number"], [type="search"]), textarea, select {
        width: 100%;
        padding: 0.6rem 0.75rem;
        background-color: var(--dark-grey);
        color: var(--white);
        border: 1px solid var(--cms-field-line);
        border-radius: 0;
        font: inherit;
        font-weight: 500;

        &::placeholder {
            color: var(--cms-muted);
        }

        &:focus {
            outline: none;
            border-color: var(--cms-published);
        }
    }

    /* Own chevron so the gap from the right edge is the same in every browser (Safari ignores padding on the native arrow) */
    select {
        appearance: none;
        padding-right: 2.5rem;
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='white' stroke-width='1.5'/%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-position: right 0.85rem center;
        background-size: 0.75rem auto;
    }

    .cms-filters select {
        width: auto;
    }

    textarea {
        resize: vertical;
    }

    input[type="file"] {
        font: inherit;
        font-weight: 500;
    }

    input[type="color"] {
        width: 3rem;
        height: 2.25rem;
        padding: 0;
        border: none;
        background: none;
        cursor: pointer;
    }

    input:is([type="checkbox"], [type="radio"]) {
        accent-color: var(--cms-published);
        width: 1rem;
        height: 1rem;
    }

    .cms-choice {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-weight: 500;
        cursor: pointer;

        .cms-hint {
            margin-left: auto;
        }
    }

    .cms-inline {
        display: flex;
        align-items: center;
        gap: 1rem;
    }

    .cms-panel {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        padding: 1rem;
        background-color: var(--dark-grey);
        border: none;

        legend {
            float: left;
            width: 100%;
            margin-bottom: 0.25rem;
            color: var(--cms-muted);
        }
    }

    .cms-preview {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;

        img, video {
            max-width: 100%;
            max-height: 20rem;
            width: fit-content;
            object-fit: contain;
            background-color: black;
        }

        &.is-icon img {
            width: 2.5rem;
            height: 2.5rem;
        }
    }

    /* Rich text editor: dark like the fields. Lexxy's ink scale runs dark to light, so flip it. */
    lexxy-editor {
        --lexxy-editor-padding: 1rem;
        --lexxy-color-ink: var(--white);
        --lexxy-color-ink-medium: oklch(80% 0 0);
        --lexxy-color-ink-light: oklch(60% 0 0);
        --lexxy-color-ink-lighter: oklch(35% 0 0);
        --lexxy-color-ink-lightest: oklch(28% 0 0);
        --lexxy-color-ink-inverted: var(--dark-grey);
        --lexxy-color-link: var(--cms-published);
        --lexxy-color-selected: oklch(35% 0.05 254);
        --lexxy-color-selected-hover: oklch(40% 0.07 254);
        background-color: var(--dark-grey);
        color: var(--white);
        border: 1px solid var(--cms-field-line);
        border-radius: 0;

        &:focus-within {
            border-color: var(--cms-published);
        }

        /* Lexxy's own rules are :where() (no specificity), so the site's reset beats them */
        .lexxy-editor__content {
            padding: var(--lexxy-editor-padding);
        }

        .lexxy-editor__toolbar-button {
            background-color: transparent;
        }

        /* Only the formatting the site styles (pages.css, typography.css): italic, link, quote, plus undo/redo.
           Add a button back here once the site has a style for it. */
        lexxy-toolbar :is(button[name="bold"], button[name="strikethrough"], button[name="underline"],
                          button[name="code"], button[name="unordered-list"], button[name="ordered-list"],
                          button[name="table"], button[name="divider"]),
        lexxy-toolbar lexxy-toolbar-dropdown:has(> button[name="format"]),
        lexxy-highlight-dropdown {
            display: none;
        }

        font-family: "neue-haas-grotesk-text", serif;
        font-weight: 500;
        min-height: 12rem;
    }

    /* Page blocks */
    .cms-block {
        display: flex;
        flex-direction: column;
        gap: 1rem;
        padding: 1rem;
        background-color: var(--dark-grey);
        border: none;
        border-left: 0.25rem solid var(--white);
        scroll-margin-top: 6rem;

        &:target {
            border-left-color: var(--cms-published);
        }

        &:has(input[name$="[_destroy]"]:checked) {
            opacity: 0.5;
            border-left-color: var(--cms-danger);
        }

        legend {
            float: left;
            color: var(--cms-muted);
        }
    }

    .cms-block-controls {
        display: flex;
        align-items: center;
        gap: 0.25rem;

        .cms-choice {
            margin-left: auto;
        }
    }

    .cms-add-blocks {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 1.5rem;
        padding: 1rem;
        border: 1px dashed var(--cms-line);
    }

    /* Save bar stays in reach on long forms */
    .cms-savebar {
        position: sticky;
        bottom: 0;
        z-index: 5;
        display: flex;
        align-items: center;
        gap: 1.5rem;
        padding: 1rem 0;
        background-color: black;
        border-top: 1px solid var(--cms-line);
    }

    /* Delete */
    .cms-danger {
        max-width: 40rem;
        margin-top: 2rem;
        padding: 1rem;
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        align-items: flex-start;
        border: 1px solid var(--cms-danger);

        p {
            font-size: 1rem;
        }
    }

    /* Sign in */
    .cms-signin {
        width: 100%;
        max-width: 24rem;
        margin: 4rem auto;
        display: flex;
        flex-direction: column;
        gap: 2rem;
    }
}
