:root {
    color-scheme: dark;
    --page: #2a2a35;
    --surface: #1e1e26;
    --surface-2: #2f2f3d;
    --surface-3: #3c3c4b;
    --hover: #4f4f6a;
    --text: #dcdcdc;
    --muted: #9a9aaa;
    --accent: #8cb4e0;
    --warn: #e6b450;
    --danger: #f06a6a;
}

* {
    box-sizing: border-box;
}

body {
    background: var(--page);
    color: var(--text);
    font-family:
        system-ui,
        -apple-system,
        'Segoe UI',
        Roboto,
        'Helvetica Neue',
        Arial,
        sans-serif;
    margin: 0;
}

.muted {
    color: var(--muted);
    font-size: 0.85rem;
}

code {
    background: var(--surface-3);
    border-radius: 3px;
    padding: 0 0.3em;
}

.app {
    display: grid;
    gap: 0.75rem;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100vh;
    height: 100dvh;
    padding: 0.75rem 1rem 1rem;
}

/* top bar */

.topbar {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1.5rem;
    justify-content: space-between;
}

.topbar h1 {
    font-size: 1.1rem;
    margin: 0;
}

.controls {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
}

.controls label {
    align-items: center;
    color: var(--muted);
    display: flex;
    font-size: 0.85rem;
    gap: 0.4rem;
}

select,
input[type='number'],
input[type='text'] {
    background: var(--surface);
    border: 1px solid var(--hover);
    border-radius: 4px;
    color: var(--text);
    font: inherit;
    font-size: 0.85rem;
    padding: 0.25rem 0.4rem;
}

.btn {
    background: var(--surface-3);
    border: 0;
    border-radius: 4px;
    color: var(--text);
    cursor: pointer;
    font: inherit;
    font-size: 0.85rem;
    padding: 0.3rem 0.7rem;
}

.btn:hover {
    background: var(--hover);
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

/* layout */

.layout {
    display: grid;
    gap: 1rem;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    min-height: 0;
}

.pane {
    background: var(--surface);
    border-radius: 8px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.pane-title {
    align-items: baseline;
    background: var(--surface-3);
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0.8rem;
}

.pane-title h2 {
    font-size: 1rem;
    margin: 0;
}

/* toolbar */

.toolbar {
    align-items: center;
    background: var(--surface-2);
    display: flex;
    flex-wrap: wrap;
    gap: 0.2rem;
    padding: 0.4rem;
}

.toolbar-divider {
    background: var(--hover);
    height: 1.4rem;
    margin: 0 0.25rem;
    width: 1px;
}

.tb {
    align-items: center;
    background: none;
    border: 0;
    border-radius: 4px;
    color: var(--text);
    cursor: pointer;
    display: inline-flex;
    font: inherit;
    font-size: 0.85rem;
    height: 2rem;
    justify-content: center;
    min-width: 2rem;
    padding: 0 0.3rem;
}

.tb:hover {
    background: var(--hover);
}

.tb.wide {
    padding: 0 0.5rem;
}

.tb.bold {
    font-weight: 700;
}

.tb.italic {
    font-style: italic;
}

.tb.underline {
    text-decoration: underline;
}

.tb.strike {
    text-decoration: line-through;
}

.color-popover {
    background: var(--surface-2);
    border-top: 1px solid var(--surface);
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem;
    padding: 0.4rem;
}

.color-popover[hidden] {
    display: none;
}

.swatch {
    border: 2px solid var(--hover);
    border-radius: 50%;
    cursor: pointer;
    height: 1.5rem;
    padding: 0;
    width: 1.5rem;
}

.swatch:hover {
    border-color: var(--text);
}

/* text area */

/* the pre shows the colored text; the textarea sits on top with invisible text */

.editor-wrap {
    flex: 1;
    min-height: 8rem;
    position: relative;
}

#input,
#backdrop {
    border: 0;
    font: inherit;
    inset: 0;
    letter-spacing: normal;
    line-height: 1.6;
    margin: 0;
    overflow: auto;
    overflow-wrap: anywhere;
    padding: 0.9rem;
    position: absolute;
    scrollbar-gutter: stable;
    tab-size: 2;
    white-space: pre-wrap;
}

#input {
    background: transparent;
    caret-color: var(--text);
    color: transparent;
    resize: none;
}

#input:focus {
    outline: none;
}

#input::placeholder {
    color: var(--muted);
}

#backdrop {
    color: var(--text);
    overflow: hidden;
    pointer-events: none;
}

.tk-tag {
    color: var(--accent);
}

.tk-param {
    color: var(--warn);
}

.tk-url {
    color: #7fd1b9;
}

.tk-color {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
}

.tk-black {
    background: #aaa;
}

/* preview pane */

#preview {
    flex: 1;
    min-height: 0;
}

.warnings {
    background: var(--surface-2);
    border-top: 1px solid var(--surface);
    font-size: 0.85rem;
    max-height: 30%;
    overflow: auto;
    padding: 0.4rem 0.8rem;
}

.warnings summary {
    cursor: pointer;
}

.warnings.has-warnings summary {
    color: var(--warn);
}

.warnings ul {
    list-style: none;
    margin: 0.4rem 0 0.2rem;
    padding: 0;
}

.warning-link {
    background: none;
    border: 0;
    color: var(--text);
    cursor: pointer;
    font: inherit;
    padding: 0.15rem 0;
    text-align: left;
}

.warning-link:hover {
    color: var(--accent);
    text-decoration: underline;
}

/* inline images panel */

.inlines {
    background: var(--surface-2);
    border-top: 1px solid var(--surface);
    max-height: 40%;
    overflow: auto;
    padding: 0.4rem 0.8rem;
}

.inlines.dragging {
    outline: 2px dashed var(--accent);
    outline-offset: -4px;
}

.inlines summary {
    cursor: pointer;
}

.inlines-body p {
    margin: 0.5rem 0;
}

.inlines-actions {
    align-items: center;
    display: flex;
    gap: 0.6rem;
}

.inline-list {
    list-style: none;
    margin: 0.6rem 0 0.2rem;
    padding: 0;
}

.inline-row {
    align-items: center;
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.4rem;
}

.inline-row img {
    background: var(--surface);
    border-radius: 4px;
    height: 40px;
    object-fit: cover;
    width: 40px;
}

.inline-row input[type='number'] {
    width: 6rem;
}

.inline-row input[type='text'] {
    flex: 1;
    min-width: 0;
}

.inline-row input.invalid {
    border-color: var(--danger);
}

/* small screens stack the two panes */

@media (max-width: 900px) {
    .app {
        height: auto;
    }

    .layout {
        grid-template-columns: minmax(0, 1fr);
    }

    .pane {
        min-height: 70vh;
    }

    .editor-wrap {
        min-height: 40vh;
    }

    #preview {
        min-height: 40vh;
    }
}
