/* CSR Template editor components: the collapsible sections and bindings cards, plus the
   upload dialog form. Split out of clinical-ai-wc.css so this page owns its styles, per
   review on PR #1085.

   Rules whose selector also matches the CSR Linkage editor stay in clinical-ai-wc.css:
   that page belongs to CAI-387 on develop and its cascade should not move with this one. */

/* The upload dialog skips .cai-labelled-form (see LibraryTlfCsrTemplateUploadDialog), so its
   section titles miss the rule above and fall back to the library default — 1rem, Title Case.
   Match .tlf-gov-lbl so WORKBOOK / LIFECYCLE read the same as the embedded GOVERNANCE band. */
[data-testid="library-tlf-csr-template-upload-form"] > .wc-ui-section > .wc-ui-section-title {
    font-size: 0.6875rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--nwc-text-secondary);
    line-height: 1;
}

/* ── CSR Template editor — collapsible sections, bindings as a grid ────────
   These structural classes shipped with no CSS, so a section and its nested
   bindings rendered as one flat run of identically-indented fields with no
   divider, and every field of every binding stayed in the document. Sections
   now collapse to a header row and bindings are a fixed-height grid, so a
   section's height no longer grows with its binding count. Reuses the same
   card border, header band, and label typography as the .wc-ui-section rule
   above. */
.csr-template-editor-form .csr-tpl-sections-toolbar {
    display: flex;
    justify-content: flex-end;
    gap: 0.375rem;
    padding: 0 var(--csr-card-pad) 0.875rem;
}

/* The visible control is the widget's inner shadow-DOM button, which paints its own background,
   padding and radius — styling the host alone left these looking like default buttons. Drive the
   widget's own custom properties instead, same approach as the row controls below. */
.csr-template-editor-form .csr-tpl-toolbar-btn {
    border: 0.0625rem solid var(--nwc-ui-border);
    background: var(--nwc-ui-surface);
    color: var(--nwc-text-secondary);
    border-radius: 0.25rem;
    padding: 0.25rem 0.625rem;
    font-size: 0.75rem;
    cursor: pointer;
    line-height: 1.4;
}

.csr-template-editor-form .csr-tpl-toolbar-btn:hover:not(:disabled) {
    background: var(--nwc-ui-surface-soft);
    color: var(--nwc-text-primary);
}

.csr-template-editor-form .csr-tpl-toolbar-btn:disabled {
    opacity: 0.4;
    cursor: default;
}

.csr-template-editor-form .csr-tpl-sections-cards {
    display: grid;
    gap: 0.75rem;
}

.csr-template-editor-form .csr-tpl-section-card {
    background: var(--nwc-ui-surface);
    border: 0.0625rem solid var(--nwc-ui-border);
    border-radius: 0.5rem;
    overflow: hidden;
}

/* Whole header is the collapse target; the move/remove cluster stops propagation in Java. */
.csr-template-editor-form .csr-tpl-section-hdr {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem var(--csr-card-pad);
    background: var(--nwc-ui-surface-soft);
    border-bottom: 0.0625rem solid var(--nwc-ui-border);
    cursor: pointer;
    user-select: none;
}

.csr-template-editor-form .csr-tpl-section-hdr:hover {
    background: var(--nwc-ui-surface-soft);
}

/* nph-button's shadow DOM sizes icon-only padding from --nwc-button-icon-only-padding, not from
   the host's own box model — overriding width/height alone (as below) leaves that shadow padding
   in place, so the chevron rendered low/off-center against csr-tpl-section-title next to it.
   Zeroing the variable plus forcing flex-centering on the host is what keeps it aligned. */
.csr-template-editor-form .csr-tpl-section-chevron {
    --nwc-button-icon-only-padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 1rem;
    height: 1rem;
    color: var(--nwc-text-muted);
}

.csr-template-editor-form .csr-tpl-section-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.8125rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--cai-body);
    line-height: 1.3;
}

/* CAI-555: a subsection sits indented under its parent, one step per level. */
.csr-template-editor-form .csr-tpl-section-card {
    --csr-tpl-level-indent: 1.5rem;
}

.csr-template-editor-form .csr-tpl-section-card[data-level="1"] { margin-inline-start: calc(var(--csr-tpl-level-indent) * 1); }
.csr-template-editor-form .csr-tpl-section-card[data-level="2"] { margin-inline-start: calc(var(--csr-tpl-level-indent) * 2); }
.csr-template-editor-form .csr-tpl-section-card[data-level="3"] { margin-inline-start: calc(var(--csr-tpl-level-indent) * 3); }
.csr-template-editor-form .csr-tpl-section-card[data-level="4"] { margin-inline-start: calc(var(--csr-tpl-level-indent) * 4); }
.csr-template-editor-form .csr-tpl-section-card[data-level="5"] { margin-inline-start: calc(var(--csr-tpl-level-indent) * 5); }
.csr-template-editor-form .csr-tpl-section-card[data-level="6"] { margin-inline-start: calc(var(--csr-tpl-level-indent) * 6); }
.csr-template-editor-form .csr-tpl-section-card[data-level="7"] { margin-inline-start: calc(var(--csr-tpl-level-indent) * 7); }
.csr-template-editor-form .csr-tpl-section-card[data-level="8"] { margin-inline-start: calc(var(--csr-tpl-level-indent) * 8); }

/* The section's ICH E3 mapping: a tag beside the title, not part of its name. */
.csr-template-editor-form .csr-tpl-section-e3 {
    flex-shrink: 0;
    border: 0.0625rem solid var(--cai-rule);
    border-radius: 0.25rem;
    background: var(--cai-inset);
    padding: 0.0625rem 0.375rem;
    font-size: 0.6875rem;
    color: var(--cai-muted);
    line-height: 1.5;
    white-space: nowrap;
}

.csr-template-editor-form .csr-tpl-section-count {
    flex-shrink: 0;
    border: 0.0625rem solid var(--nwc-ui-border);
    border-radius: 1rem;
    background: var(--nwc-ui-surface);
    padding: 0.0625rem 0.5rem;
    font-size: 0.6875rem;
    color: var(--nwc-text-secondary);
    line-height: 1.5;
}

/* Compound selector so a later same-specificity rule can never re-show it. */
.csr-template-editor-form .csr-tpl-section-body.csr-tpl-section-body--hidden {
    display: none;
}

/* Bindings sit one level in, behind a left rule. */
.csr-template-editor-form .csr-tpl-bindings {
    margin: 0 var(--csr-card-pad) 0.875rem;
    padding-left: 0.875rem;
    border-left: 0.125rem solid var(--nwc-ui-border);
}

.csr-template-editor-form .csr-tpl-bindings-hdr {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.5rem 0 0.375rem;
}

.csr-template-editor-form .csr-tpl-bindings-lbl {
    font-size: 0.6875rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--nwc-text-muted);
    line-height: 1;
}

/* Binding cards mirror the section cards one level down — same chevron/header/hide
   mechanics, lighter band and smaller label so the nesting stays legible. */
.csr-template-editor-form .csr-tpl-binding-cards {
    display: grid;
    gap: 0.375rem;
}

.csr-template-editor-form .csr-tpl-binding-card {
    background: var(--nwc-ui-surface);
    border: 0.0625rem solid var(--nwc-ui-border);
    border-radius: 0.375rem;
    overflow: hidden;
}

.csr-template-editor-form .csr-tpl-binding-hdr {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 0.75rem;
    background: var(--nwc-ui-surface);
    border-bottom: 0.0625rem solid var(--nwc-ui-border);
    cursor: pointer;
    user-select: none;
}

.csr-template-editor-form .csr-tpl-binding-hdr:hover {
    background: var(--nwc-ui-surface-soft);
}

/* Same nph-button icon-padding fix as .csr-tpl-section-chevron above. */
.csr-template-editor-form .csr-tpl-binding-chevron {
    --nwc-button-icon-only-padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 0.875rem;
    height: 0.875rem;
    color: var(--nwc-text-muted);
}

.csr-template-editor-form .csr-tpl-binding-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.625rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--nwc-text-muted);
    line-height: 1.3;
}

.csr-template-editor-form .csr-tpl-binding-meta {
    flex-shrink: 0;
    font-size: 0.6875rem;
    color: var(--nwc-text-secondary);
    line-height: 1.4;
    white-space: nowrap;
}

.csr-template-editor-form .csr-tpl-binding-body {
    padding: 0.5rem 0.75rem 0.75rem;
    display: grid;
    gap: 0.25rem;
}

/* Compound selector, and placed after the base rule: as a single class this lost the
   cascade to .csr-tpl-binding-body's display:grid at equal specificity, so bindings
   rendered permanently expanded no matter what the chevron said. */
.csr-template-editor-form .csr-tpl-binding-body.csr-tpl-binding-body--hidden {
    display: none;
}

/* Name and Heading run long, so they take two of the three columns; every other field keeps
   one, which leaves all the column gutters aligned down the card. */
.csr-template-editor-form .csr-tpl-span-2 {
    grid-column: span 2;
}

/* Empty trailing cell that holds a column open so the fields beside it are not stretched. */
.csr-template-editor-form .csr-tpl-form-spacer {
    min-width: 0;
}

/* Row controls were unstyled native buttons next to an already-styled remove "×".
   Match .linked-tpl-del so the cluster reads as one set. */
.csr-template-editor-form .csr-tpl-row-actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 0.25rem;
    flex-shrink: 0;
}

.csr-template-editor-form .csr-tpl-move-btn {
    width: 1.5rem;
    height: 1.5rem;
    flex-shrink: 0;
    border: 0.0625rem solid transparent;
    background: transparent;
    color: var(--nwc-text-muted);
    cursor: pointer;
    border-radius: 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.875rem;
    line-height: 1;
}

.csr-template-editor-form .csr-tpl-move-btn:hover:not(:disabled) {
    background: var(--nwc-ui-surface-soft);
    color: var(--nwc-text-primary);
    border-color: var(--nwc-ui-border);
}

.csr-template-editor-form .csr-tpl-move-btn:disabled {
    opacity: 0.35;
    cursor: default;
}

/* Base .linked-tpl-footer margin-top is 0.25rem, tight enough here that "+ Add Section" read as
   part of the section card above it. Match the 0.75rem gap already used between section cards
   (.csr-tpl-sections-cards) so toolbar→card, card→card, and card→button read as one rhythm. */
.csr-template-editor-form .csr-tpl-sections-body > .linked-tpl-footer {
    margin-top: 0.75rem;
}

/* Moved out of clinical-ai-wc.css per review on PR #1085: these are CSR Template
   rules only. The .csr-linkage-editor-form halves stay in the shared sheet so this
   page's styles no longer alter the CSR Linkage editor's layout. */

/* ── CSR Template / CSR Linkage editors — same card-based form pattern ────── */
.csr-template-editor-form {
    --csr-card-pad: 2.25rem;
    /* Was 100% — with every field on its own full-width row that gave the widest possible box
       to a 6-character E3 Ref. Sections/bindings fields now sit in .cai-form-grid-2/-3 rows, so
       a real cap keeps those rows (and Template Details/Governance, which share --csr-content-box)
       from stretching arbitrarily wide on a large monitor without leaving the old dead band. */
    --csr-content-max: 76rem;
    --csr-content-box: calc(var(--csr-content-max) + (2 * var(--csr-card-pad)));
    display: flex;
    flex-direction: column;
    gap: 1rem;
    width: 100%;
}

.csr-template-editor-form.cai-labelled-form .wc-ui-section {
    background: var(--nwc-ui-surface);
    border: 0.0625rem solid var(--nwc-ui-border);
    border-radius: 0.5rem;
    padding: 0;
    gap: 0;
    overflow: hidden;
}

.csr-template-editor-form.cai-labelled-form .wc-ui-section > .wc-ui-section-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0;
    padding: 0.625rem var(--csr-card-pad);
    background: var(--nwc-ui-surface-soft);
    border-bottom: 0.0625rem solid var(--nwc-ui-border);
    font-size: 0.6875rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--nwc-text-secondary);
    line-height: 1.3;
}

/* Kind/Lifecycle are nph-combobox and Design Summary is nph-textarea; the shared
   .cai-labelled-form rule only uncaps nph-input, so these kept a 28.7rem shadow-DOM cap
   inside a 49.7rem column. Mirrors the .deriv-spec-editor-form rule above. */
.csr-template-editor-form.cai-labelled-form nph-combobox,
.csr-template-editor-form.cai-labelled-form nph-textarea {
    --label-width: 0;
    --label-input-gap: 0;
    --input-max-width: 100%;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    box-sizing: border-box;
}

/* Three fields per row, reusing the app's .cai-form-grid-3. The label column stays at the
   shared 10.5rem — narrowing it clipped "Source Kind" and "Resolution Status" to "Sourc"
   and "Resolution". Row gap is tightened since these rows sit inside a card. */
.csr-template-editor-form .cai-form-grid-3 {
    gap: 0.5rem 1.25rem;
}

/* Horizontal inset comes from the Card body padding below, not from these inner wrappers —
   stacking both gave the two EditorCard surfaces a 3.5rem inset against Governance's 2.25rem. */
.csr-template-editor-form .cai-editor-card {
    --wc-ui-card-body-padding: 0.375rem var(--csr-card-pad) 0.875rem;
    max-width: var(--csr-content-box);
    box-sizing: border-box;
}

.csr-template-editor-form.cai-labelled-form .wc-ui-field {
    grid-template-columns: 10.5rem minmax(0, 1fr);
}

.csr-template-editor-form.cai-labelled-form .wc-ui-field > :not(.wc-ui-label) {
    max-width: none;
}

.csr-template-editor-form .tlf-gov-section {
    --wc-ui-card-header-padding: 0.625rem var(--csr-card-pad);
    --wc-ui-card-body-padding: 1rem var(--csr-card-pad);
    max-width: var(--csr-content-box);
    box-sizing: border-box;
}

.csr-template-editor-form .linked-tpl-grid,
.csr-template-editor-form .linked-tpl-footer {
    padding-left: var(--csr-card-pad);
    padding-right: var(--csr-card-pad);
    max-width: var(--csr-content-box);
    box-sizing: border-box;
}

.csr-template-editor-form .linked-tpl-grid {
    padding-top: 0.375rem;
}

/* Below the card's comfortable width for a 3-up row, stack to one field per line instead of
   letting columns get cramped. 2-up (Template Details) drops to one column a bit later, since a
   pair of fields keeps enough room for longer values down to a narrower viewport. */
@media (max-width: 56rem) {
    .csr-template-editor-form .cai-form-grid-3 {
        grid-template-columns: 1fr;
    }

    .csr-template-editor-form .csr-tpl-span-2 {
        grid-column: span 1;
    }
}

@media (max-width: 40rem) {
    .csr-template-editor-form .cai-form-grid-2 {
        grid-template-columns: 1fr;
    }
}

/* Format profile card: the settings beside a sample page. The page's size, margins, font, sizes,
   colours and alignment come from custom properties the card sets from the profile, so the sample
   follows every edit without rebuilding its DOM. */
.csr-template-editor-form .csr-format-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 1.5rem;
    align-items: start;
}

.csr-template-editor-form .csr-format-settings {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    min-width: 0;
}

.csr-template-editor-form .csr-format-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.csr-template-editor-form .csr-format-group-title,
.csr-template-editor-form .csr-format-preview-title {
    font-size: 0.6875rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--cai-muted);
}

.csr-template-editor-form .csr-format-unsaved,
.csr-template-editor-form .csr-format-import-hint,
.csr-template-editor-form .csr-format-import-status {
    color: var(--cai-muted);
    font-size: 0.8125rem;
}

.csr-template-editor-form .csr-format-headings {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.csr-template-editor-form .csr-format-heading-row {
    display: grid;
    grid-template-columns: 6rem repeat(3, minmax(0, 1fr));
    gap: 0.5rem 1rem;
    align-items: center;
    padding-bottom: 0.5rem;
    border-bottom: 0.0625rem solid var(--cai-rule);
}

.csr-template-editor-form .csr-format-heading-row .wc-ui-field {
    grid-template-columns: minmax(0, auto) minmax(0, 1fr);
}

.csr-template-editor-form .csr-format-heading-level {
    font-weight: 600;
}

.csr-template-editor-form .csr-format-heading-flags {
    grid-column: 2 / -1;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1.5rem;
}

.csr-template-editor-form .csr-format-import-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    align-items: center;
}

.csr-template-editor-form .csr-format-import-status--error {
    color: var(--cai-err);
}

.csr-template-editor-form .csr-format-import-notes {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    margin: 0;
    padding-left: 1.25rem;
    font-size: 0.8125rem;
    list-style: disc;
}

.csr-template-editor-form .csr-format-import-note::marker {
    color: var(--cai-muted);
}

.csr-template-editor-form .csr-format-preview {
    position: sticky;
    top: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.csr-template-editor-form .csr-format-preview-page {
    position: relative;
    box-sizing: border-box;
    width: var(--csr-format-page-width);
    height: var(--csr-format-page-height);
    padding: var(--csr-format-margin);
    overflow: hidden;
    background: var(--cai-card);
    border: 0.0625rem solid var(--cai-rule);
    box-shadow: var(--cai-shadow-sm);
    font-family: var(--csr-format-font);
    color: var(--csr-format-text-color);
    line-height: 1.15;
}

.csr-template-editor-form .csr-format-sample-title {
    font-size: var(--csr-format-title-size);
    font-weight: 700;
    text-align: center;
}

.csr-template-editor-form .csr-format-sample-subtitle {
    font-size: var(--csr-format-subtitle-size);
    text-align: center;
    margin-bottom: 0.8em;
}

.csr-template-editor-form .csr-format-sample-h1,
.csr-template-editor-form .csr-format-sample-h2,
.csr-template-editor-form .csr-format-sample-h3,
.csr-template-editor-form .csr-format-sample-h4,
.csr-template-editor-form .csr-format-sample-h5 {
    margin-bottom: 0.4em;
}

.csr-template-editor-form .csr-format-sample-h1 {
    font-size: var(--csr-format-h1-size);
    color: var(--csr-format-h1-color);
    font-weight: var(--csr-format-h1-weight);
    text-transform: var(--csr-format-h1-transform);
}

.csr-template-editor-form .csr-format-sample-h2 {
    font-size: var(--csr-format-h2-size);
    color: var(--csr-format-h2-color);
    font-weight: var(--csr-format-h2-weight);
    text-transform: var(--csr-format-h2-transform);
}

.csr-template-editor-form .csr-format-sample-h3 {
    font-size: var(--csr-format-h3-size);
    color: var(--csr-format-h3-color);
    font-weight: var(--csr-format-h3-weight);
    text-transform: var(--csr-format-h3-transform);
}

.csr-template-editor-form .csr-format-sample-h4 {
    font-size: var(--csr-format-h4-size);
    color: var(--csr-format-h4-color);
    font-weight: var(--csr-format-h4-weight);
    text-transform: var(--csr-format-h4-transform);
}

.csr-template-editor-form .csr-format-sample-h5 {
    font-size: var(--csr-format-h5-size);
    color: var(--csr-format-h5-color);
    font-weight: var(--csr-format-h5-weight);
    text-transform: var(--csr-format-h5-transform);
}

.csr-template-editor-form .csr-format-sample-body {
    font-size: var(--csr-format-body-size);
    text-align: var(--csr-format-align);
    margin-bottom: 0.8em;
}

.csr-template-editor-form .csr-format-sample-caption {
    font-size: var(--csr-format-caption-size);
    font-weight: 700;
    margin-bottom: 0.3em;
}

.csr-template-editor-form .csr-format-sample-table {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.5em;
    font-size: var(--csr-format-table-size);
    text-align: left;
    border-top: 0.0625rem solid currentcolor;
    border-bottom: 0.0625rem solid currentcolor;
    padding: 0.3em 0;
}

.csr-template-editor-form .csr-format-sample-link {
    margin-left: 0.25em;
    color: var(--csr-format-link-color);
    text-decoration: underline;
}

.csr-template-editor-form .csr-format-sample-note {
    font-size: var(--csr-format-note-size);
    margin-top: 0.3em;
}

.csr-template-editor-form .csr-format-sample-header,
.csr-template-editor-form .csr-format-sample-footer {
    position: absolute;
    left: var(--csr-format-margin);
    right: var(--csr-format-margin);
    font-size: var(--csr-format-footer-size);
}

.csr-template-editor-form .csr-format-sample-header {
    top: var(--csr-format-header-footer-distance);
    text-align: right;
}

.csr-template-editor-form .csr-format-sample-footer {
    bottom: var(--csr-format-header-footer-distance);
    text-align: center;
}

@media (max-width: 72rem) {
    .csr-template-editor-form .csr-format-layout {
        grid-template-columns: minmax(0, 1fr);
    }

    .csr-template-editor-form .csr-format-preview {
        position: static;
    }
}
