.prompt-editor-page { box-sizing: border-box; }
.prompt-editor-page *, .prompt-editor-page *::before, .prompt-editor-page *::after { box-sizing: border-box; }

.prompt-editor-page {
    font-family: var(--app-font-primary, "Geist", sans-serif);
    font-size: 14px;
    line-height: 22px;
    color: var(--app-text-primary, rgba(17, 24, 39, 0.87));
    /* No own padding in the in-flow (narrow) mode: the ancestors provide
     * 10px (.app-content) + 1rem (.ui-page-content) = the same 26px the
     * full-height mode restates as padding, so the margins don't jump when
     * the viewport crosses the 1400px breakpoint. */
    max-width: 1800px;
}

.prompt-editor-page .pe-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}

.prompt-editor-page .pe-header h2 {
    font-size: 20px;
    font-weight: 600;
    line-height: 28px;
    margin: 0;
}

.prompt-editor-page .header-actions {
    display: flex;
    gap: 12px;
    align-items: center;
}

.prompt-editor-page .header-actions a {
    color: var(--app-text-secondary, #919191);
    text-decoration: none;
    font-size: 14px;
    font-weight: 400;
}

.prompt-editor-page .header-actions a:hover {
    color: var(--app-text-primary, rgba(17, 24, 39, 0.87));
}

.prompt-editor-page .toolbar {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    align-items: center;
}

/* Buttons — aligned with the eval page (`.eval-btn*`) so the Prompt
 * Editor and Setup & Run share one visual language. Padding, radius,
 * font, and transition all match; only the CSS class names differ
 * because this page uses the shorter `.btn*` convention. */
.prompt-editor-page .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 9px 16px;
    border-radius: 8px;
    border: 1px solid hsla(0, 0%, 40%, 0.15);
    background-color: var(--app-bg-primary, #fff);
    color: var(--app-text-primary);
    font-family: inherit;
    font-size: 14px;
    font-weight: 500;
    line-height: 22px;
    cursor: pointer;
    transition: background-color 0.15s ease;
    text-decoration: none;
}

.prompt-editor-page .btn:hover {
    background-color: var(--app-bg-hover, #f5f5f5);
}

.prompt-editor-page .btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.prompt-editor-page .btn-primary {
    background-color: var(--app-action-primary, #212121);
    color: #f5f5f5;
    border: none;
}

.prompt-editor-page .btn-primary:hover {
    background-color: var(--app-action-hover, #141b9b);
}

.prompt-editor-page .btn-secondary {
    background-color: var(--app-bg-primary, #fff);
    color: var(--app-text-primary);
    border: 1px solid hsla(0, 0%, 40%, 0.15);
}

.prompt-editor-page .btn-secondary:hover {
    background-color: var(--app-bg-hover, #f5f5f5);
}

.prompt-editor-page .status {
    font-size: 13px;
    color: var(--app-text-secondary, #919191);
    margin-left: 8px;
}

.prompt-editor-page .status.saved {
    color: #16a34a;
}

.prompt-editor-page .status.error {
    color: var(--app-text-alert, #dc172e);
}

.prompt-editor-page .editor-container {
    border: 1px solid var(--app-color-border, #dee2e6);
    border-radius: 6px;
    overflow: hidden;
}

.prompt-editor-page .cm-editor {
    font-size: 14px;
    min-height: 500px;
}

/* In wide layout the editor column is bounded by the parent flex
 * chain and CodeMirror handles its own scrolling via .cm-scroller.
 *
 * Drop the 500px floor (it would otherwise force the editor to be
 * taller than its container) and absolutely-position cm-editor inside
 * a relative editor-container. We tried `flex: 1; min-height: 0`
 * first, but `.cm-editor` is itself a flex container whose intrinsic
 * content size (`.cm-content`) wins over the flex-shrink rule —
 * cm-editor refused to shrink below its content. Absolute positioning
 * bypasses the flex/intrinsic-size conflict: `inset: 0` makes
 * cm-editor exactly fill editor-container regardless of content
 * height, and CodeMirror's internal `.cm-scroller` handles scrolling
 * within the bounded area. */
@media (min-width: 1400px) {
    .prompt-editor-page .editor-container {
        position: relative;
    }
    /* `!important` is required because CodeMirror 6 injects an inline
     * stylesheet at runtime with `position: relative !important;
     * display: flex !important;` on the cm-editor host class
     * (`.ͼ1`). Without `!important` here, our position would lose
     * the cascade despite having higher selector specificity. */
    .prompt-editor-page .cm-editor {
        position: absolute !important;
        inset: 0;
        min-height: 0;
    }
}

.prompt-editor-page .cm-editor .cm-scroller {
    font-family: "JetBrains Mono", "Fira Code", "Consolas", monospace;
}

.prompt-editor-page .cm-editor.cm-focused {
    outline: none;
}

/* Syntax highlighting is handled by oneDark theme via standard CodeMirror tags:
   heading = #! sections, meta = ##? conditionals, atom = inject names,
   processingInstruction = inject delimiters, string = TellEm scripts, comment = comments */

/* Layout — single column by default, two columns on wide viewports.
 * On wide viewports the whole page is constrained to the viewport
 * height with `overflow: hidden`, so only the individual columns
 * (and the CodeMirror editor inside) scroll. The pe-editor-toolbar
 * above the editor is a fixed-height row inside editor-panel, which
 * keeps Save / Preview changes visible while the user scrolls the
 * prompt body. */
.prompt-editor-page .layout {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.prompt-editor-page .editor-panel {
    flex: 1;
    min-width: 0;
}

.prompt-editor-page .side-panel {
    min-width: 0;
}

@media (min-width: 1400px) {
    /* Fill the parent (`.app-content`, which is `position: relative`
     * in central-ui.css) via absolute positioning. This is the most
     * reliable way to bound the page without depending on the parent's
     * flex configuration or percentage-height resolution. With
     * `inset: 0` the prompt-editor-page exactly fills the padding box
     * of `.app-content`; combined with `overflow: hidden`, nothing
     * inside can ever push past the parent and trigger an outer
     * scrollbar. */
    .prompt-editor-page {
        position: absolute;
        inset: 0;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        /* inset: 0 anchors to .app-content's padding box, swallowing both its
         * 10px padding and .ui-page-content's 1rem — the spacing every other
         * page gets. Restate the sum on all four sides so the edit mode
         * aligns with the rest of the admin instead of hugging the edges
         * (ReleaseWeek: margins in the prompts widget edit mode). */
        padding: calc(10px + 1rem);
    }

    .prompt-editor-page .pe-header {
        flex-shrink: 0;
    }

    .prompt-editor-page .layout {
        flex: 1;
        flex-direction: row;
        min-height: 0;
        margin-bottom: 0;
    }

    /* ── Editor column ──────────────────────────────────────────
     * Becomes a flex column itself: a fixed-height toolbar at the
     * top, and a flex-filling editor-container below. The
     * CodeMirror widget inside `editor-container` is set to
     * `height: 100%`, which makes its internal `.cm-scroller`
     * the actual scroll target. From the user's perspective the
     * column has its own scrollbar (CM6 renders one inside cm-editor)
     * while the toolbar above stays pinned without needing
     * `position: sticky`. */
    .prompt-editor-page .editor-panel {
        display: flex;
        flex-direction: column;
        flex: 1;
        min-width: 0;
        min-height: 0;
        overflow: hidden;
    }

    .prompt-editor-page .pe-editor-toolbar {
        flex-shrink: 0;
    }

    .prompt-editor-page .editor-container {
        flex: 1;
        min-height: 0;
        /* `display: flex` and the cm-editor `flex: 1` rule live in
         * the cm-editor block above. */
    }

    /* ── Side column ────────────────────────────────────────────
     * Same flex-column structure: tab bar at the top, the active
     * tab content below as a flex-filling scroll container.
     * Inactive tab-content elements stay in the DOM but are hidden
     * via the existing `:not(.active)` rule pattern. */
    .prompt-editor-page .side-panel {
        display: flex;
        flex-direction: column;
        width: 420px;
        flex-shrink: 0;
        min-width: 0;
        min-height: 0;
        overflow: hidden;
    }

    .prompt-editor-page .side-panel .tab-bar {
        flex-shrink: 0;
    }

    .prompt-editor-page .side-panel .tab-content {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
    }

    .prompt-editor-page .side-panel .tab-content:not(.active) {
        display: none;
    }
}

/* Tabs */
.prompt-editor-page .tab-bar {
    display: flex;
    gap: 40px;
    padding: 0;
    margin-bottom: 8px;
}

.prompt-editor-page .tab-btn {
    padding: 5px 0;
    border: none;
    border-bottom: 3px solid transparent;
    border-radius: 0;
    background: none;
    cursor: pointer;
    font-family: inherit;
    font-size: 14px;
    font-weight: 400;
    color: var(--app-text-primary, rgba(17, 24, 39, 0.87));
}

.prompt-editor-page .tab-btn:hover {
    color: var(--app-text-primary, rgba(17, 24, 39, 0.87));
    background: none;
    border-color: transparent;
}

.prompt-editor-page .tab-btn.active {
    border-bottom-color: #212121;
    font-weight: 600;
    color: var(--app-text-primary, rgba(17, 24, 39, 0.87));
    background: none;
}

.prompt-editor-page .tab-content {
    display: none;
}

.prompt-editor-page .tab-content.active {
    display: block;
}

/* Docs list */
.prompt-editor-page .docs-list {
    max-height: calc(100vh - 260px);
    overflow-y: auto;
}

/* In wide layout the side-panel's `.tab-content` is the scroll
 * container (set in the layout block above with `overflow-y: auto`).
 * The pre-existing `max-height: calc(100vh - 260px)` on `.docs-list`
 * predates that restructuring and now clamps the list to a smaller
 * height than the tab-content it lives in — leaving the tab column
 * with a scrollbar that doesn't reach the bottom. Drop the
 * inner constraint and let the list grow to its content; the
 * tab-content above it handles the actual scrolling. */
@media (min-width: 1400px) {
    .prompt-editor-page .docs-list {
        max-height: none;
        overflow: visible;
    }
}

.prompt-editor-page .doc-item {
    padding: 6px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    margin-bottom: 2px;
}

.prompt-editor-page .doc-item:hover {
    background: var(--app-bg-hover, #f5f5f5);
}

.prompt-editor-page .doc-item .doc-name {
    font-family: "JetBrains Mono", monospace;
    color: #c2410c;
    font-weight: 500;
}

.prompt-editor-page .doc-item .doc-desc {
    color: var(--app-text-secondary, #919191);
    font-size: 12px;
    margin-top: 2px;
}

.prompt-editor-page .doc-item .doc-example {
    font-family: "JetBrains Mono", monospace;
    color: #b45309;
    font-size: 12px;
    margin-top: 2px;
}

/* Test panel */

.prompt-editor-page .test-form {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.prompt-editor-page .test-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.prompt-editor-page .test-row label {
    width: 60px;
    font-size: 13px;
    color: var(--app-text-secondary, #919191);
    flex-shrink: 0;
}

.prompt-editor-page .test-row input {
    flex: 1;
    padding: 4px 8px;
    border: 1px solid var(--app-color-border, #dee2e6);
    border-radius: 4px;
    background: var(--app-bg-primary, #fff);
    color: var(--app-text-primary, #212529);
    font-size: 13px;
    font-family: inherit;
}

.prompt-editor-page .test-row input:focus {
    outline: none;
    border-color: var(--app-action-hover, #141b9b);
}

.prompt-editor-page .test-results {
    margin-top: 12px;
}

.prompt-editor-page .test-results summary {
    cursor: pointer;
    font-size: 13px;
    color: var(--app-text-secondary, #919191);
    padding: 4px 0;
}

.prompt-editor-page .test-results pre {
    background: var(--app-bg-secondary, #f9f9f9);
    border: 1px solid var(--app-color-border, #dee2e6);
    border-radius: 4px;
    padding: 10px;
    font-size: 12px;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 400px;
    overflow-y: auto;
    margin: 4px 0 8px 0;
    color: var(--app-text-primary, #212529);
}

/* Critique section */
.prompt-editor-page .critique-section {
    margin-top: 12px;
    border-top: 1px solid var(--app-color-border, #dee2e6);
    padding-top: 12px;
}

.prompt-editor-page .critique-answer {
    background: var(--app-bg-secondary, #f9f9f9);
    border: 1px solid var(--app-color-border, #dee2e6);
    border-radius: 4px;
    padding: 10px;
    font-size: 13px;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 400px;
    overflow-y: auto;
    margin: 8px 0 0 0;
    color: var(--app-text-primary, #212529);
}

/* Helper tab */
.prompt-editor-page .helper-messages {
    max-height: calc(100vh - 340px);
    min-height: 120px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 4px 0;
}

.prompt-editor-page .helper-msg {
    font-size: 13px;
    padding: 6px 10px;
    border-radius: 6px;
    white-space: pre-wrap;
    word-break: break-word;
    max-width: 90%;
}

.prompt-editor-page .helper-msg.user {
    align-self: flex-end;
    background: var(--app-action-hover, #141b9b);
    color: var(--app-text-white, #fff);
}

.prompt-editor-page .helper-msg.assistant {
    align-self: flex-start;
    background: var(--app-bg-secondary, #f9f9f9);
    border: 1px solid var(--app-color-border, #dee2e6);
    color: var(--app-text-primary, #212529);
}

.prompt-editor-page .helper-input {
    display: flex;
    gap: 6px;
    margin-top: 8px;
}

.prompt-editor-page .helper-input textarea {
    flex: 1;
    min-width: 0;
    padding: 6px 10px;
    border: 1px solid var(--app-color-border, #dee2e6);
    border-radius: 4px;
    background: var(--app-bg-primary, #fff);
    color: var(--app-text-primary, #212529);
    font-size: 13px;
    font-family: inherit;
    resize: vertical;
}

.prompt-editor-page .helper-input textarea:focus {
    outline: none;
    border-color: var(--app-action-hover, #141b9b);
}

.prompt-editor-page .helper-msg.edits {
    align-self: stretch;
    background: var(--app-bg-secondary, #f9f9f9);
    border: 1px solid var(--app-color-border, #dee2e6);
    color: var(--app-text-primary, #212529);
    max-width: 100%;
}

.prompt-editor-page .helper-edit-item {
    margin: 6px 0;
    font-family: "JetBrains Mono", monospace;
    font-size: 12px;
}

.prompt-editor-page .helper-edit-old,
.prompt-editor-page .helper-edit-new {
    padding: 2px 4px;
    border-radius: 2px;
    white-space: pre-wrap;
    word-break: break-word;
}

.prompt-editor-page .helper-edit-arrow {
    text-align: center;
    color: var(--app-text-secondary, #919191);
    font-size: 12px;
}

.prompt-editor-page .helper-diff-removed {
    background: #fcc;
    text-decoration: line-through;
}

.prompt-editor-page .helper-diff-inserted {
    background: #cfc;
}

.prompt-editor-page .helper-accept-btn {
    margin-top: 8px;
}

/* ── Preview Changes: split-button + dropdown + overlay ─────────────────── */

.prompt-editor-page .pe-editor-toolbar {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 16px;
}

.prompt-editor-page .pe-toolbar-help {
    font-size: 13px;
    color: var(--app-text-secondary, #919191);
    margin-right: auto;
}

/* Split-button + dropdown — visually identical to `.eval-split-btn*` /
 * `.eval-dropdown*` on the Setup & Run page. The class namespace is
 * different (`pe-*` rather than `eval-*`) to keep the two pages'
 * selectors scoped, but every size, color, and transition is aligned. */

.prompt-editor-page .pe-split-btn {
    position: relative;
    display: inline-flex;
}

.prompt-editor-page .pe-split-btn__trigger {
    display: inline-flex;
    align-items: center;
    gap: 0;
}

.prompt-editor-page .pe-split-btn__arrow {
    font-size: 14px;
    line-height: 1;
    margin-left: 4px;
    padding: 9px 12px 9px 10px;
    margin-top: -9px;
    margin-bottom: -9px;
    margin-right: -16px;
    border-left: 1px solid hsla(0, 0%, 40%, 0.2);
    cursor: pointer;
}

.prompt-editor-page .pe-split-btn__trigger.btn-secondary .pe-split-btn__arrow {
    border-left-color: hsla(0, 0%, 40%, 0.15);
}

.prompt-editor-page .pe-dropdown__menu {
    display: none;
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 4px;
    min-width: 280px;
    max-width: 480px;
    background: var(--app-bg-primary, #fff);
    border: 1px solid hsla(0, 0%, 40%, 0.15);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    z-index: 100;
    padding: 4px;
    flex-direction: column;
}

.prompt-editor-page .pe-split-btn--open .pe-dropdown__menu {
    display: flex;
}

.prompt-editor-page .pe-dropdown__revisions {
    display: flex;
    flex-direction: column;
}

.prompt-editor-page .pe-dropdown__item {
    display: block;
    width: 100%;
    padding: 9px 12px;
    border: none;
    border-radius: 6px;
    background: none;
    color: var(--app-text-primary);
    font-family: inherit;
    font-size: 14px;
    font-weight: 500;
    line-height: 22px;
    text-align: left;
    cursor: pointer;
    transition: background-color 0.15s ease;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.prompt-editor-page .pe-dropdown__item:hover,
.prompt-editor-page .pe-dropdown__item:focus {
    background-color: var(--app-bg-hover, #f5f5f5);
    outline: none;
}

.prompt-editor-page .pe-dropdown__item--current {
    font-weight: 600;
}

.prompt-editor-page .pe-dropdown__item--default {
    font-style: italic;
}

.prompt-editor-page .pe-dropdown__divider {
    height: 1px;
    background: hsla(0, 0%, 40%, 0.15);
    margin: 4px 0;
}

.prompt-editor-page .pe-dropdown__empty,
.prompt-editor-page .pe-dropdown__loading {
    padding: 9px 12px;
    color: var(--app-text-secondary, #6c7086);
    font-style: italic;
    font-size: 13px;
}

.prompt-editor-page .pe-dropdown__expand {
    display: block;
    width: 100%;
    padding: 9px 12px;
    background: transparent;
    border: none;
    border-top: 1px solid hsla(0, 0%, 40%, 0.15);
    border-radius: 0 0 6px 6px;
    text-align: center;
    font-family: inherit;
    font-size: 13px;
    font-weight: 500;
    color: var(--app-link, #2563eb);
    cursor: pointer;
    margin-top: 4px;
    transition: background-color 0.15s ease;
}

.prompt-editor-page .pe-dropdown__expand:hover {
    background-color: var(--app-bg-hover, #f5f5f5);
}

/* Overlay panel — floats over the editor area when a diff is requested. */
.prompt-editor-page .pe-preview-overlay {
    position: fixed;
    top: 20%;
    left: 50%;
    transform: translateX(-50%);
    width: min(900px, 92vw);
    max-height: 70vh;
    background: var(--app-bg, #ffffff);
    border: 1px solid var(--app-border, #e0e0e0);
    border-radius: 6px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
    z-index: 1500;
    display: flex;
    flex-direction: column;
}

.prompt-editor-page .pe-preview-overlay[hidden] {
    display: none;
}

.prompt-editor-page .pe-preview-overlay__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    border-bottom: 1px solid var(--app-border, #e0e0e0);
    background: var(--app-bg-soft, #f8fafc);
}

.prompt-editor-page .pe-preview-overlay__title {
    font-weight: 600;
    font-family: var(--app-mono, ui-monospace, Consolas, monospace);
    font-size: 13px;
    color: var(--app-text, #1f2937);
}

.prompt-editor-page .pe-preview-overlay__close {
    background: transparent;
    border: none;
    font-size: 1.4em;
    line-height: 1;
    cursor: pointer;
    color: var(--app-text-secondary, #6c7086);
    padding: 2px 8px;
}

.prompt-editor-page .pe-preview-overlay__close:hover {
    color: var(--app-text, #1f2937);
}

.prompt-editor-page .pe-preview-overlay__body {
    flex: 1;
    overflow: auto;
    padding: 0;
}

.prompt-editor-page #pe-preview-overlay-segments {
    margin: 0;
    padding: 12px 16px;
    font-family: var(--app-mono, ui-monospace, Consolas, monospace);
    font-size: 12px;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
    background: transparent;
    color: var(--app-text, #1f2937);
}

.prompt-editor-page .pe-preview-overlay__footer {
    padding: 6px 16px;
    border-top: 1px solid var(--app-border, #e0e0e0);
    font-size: 13px;
    color: var(--app-text-secondary, #6c7086);
    background: var(--app-bg-soft, #f8fafc);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.prompt-editor-page .pe-preview-overlay__restore {
    font-size: 13px;
    padding: 4px 10px;
}

.prompt-editor-page .status.warning {
    color: #b45309;
    font-weight: 600;
}

/* Segment classes — mirror prompt-history/history.css so both pages share
 * the same visual language. Kept local here (rather than a shared stylesheet)
 * because the editor and history pages already live in separate modules. */
.prompt-editor-page .ph-diff-equal {
    /* Unstyled; <pre> handles font and whitespace. */
}

.prompt-editor-page .ph-diff-ins {
    background: rgba(34, 197, 94, 0.20);
    color: #166534;
    border-radius: 2px;
    padding: 0 1px;
}

.prompt-editor-page .ph-diff-del {
    background: rgba(239, 68, 68, 0.20);
    color: #991b1b;
    text-decoration: line-through;
    border-radius: 2px;
    padding: 0 1px;
}

/* ── Helper intro + markdown-rendered replies ───────────────── */

.prompt-editor-page .helper-intro {
    margin: 0 0 8px;
    font-size: 13px;
    color: var(--app-text-secondary, #919191);
}

/* Rendered markdown: typography comes from the shared .dlg-md rules in
   dlg-markdown.css. Only the pre-wrap meant for literal bubbles must be
   overridden here, since .helper-msg's selector outweighs .dlg-md. */
.prompt-editor-page .helper-msg--md {
    white-space: normal;
}
