.message-templates-view {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
  min-height: 0;
  height: 100%;
  box-sizing: border-box;
  padding: 12px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  color: var(--text-primary);
}

.message-template-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  min-width: 0;
}

.message-template-heading h2,
.message-template-form h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}

.message-template-filters {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 6px;
  min-width: 0;
}

.message-template-filter-scopes {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 8px;
  min-height: 0;
}
.message-template-scope-tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.message-template-scope-tabs > button { flex: 1; }
.message-template-scope-tabs > button[aria-pressed=true] { background: var(--brand-soft); border-color: var(--brand); }
.message-template-filter-trigger > .record-action-label { display: none; }
.message-template-filter-count { font-size: 12px; font-weight: 500; color: var(--brand); }
.message-template-filter-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; min-width: 0; }
.message-template-filter-fields > label { display: grid; gap: 4px; min-width: 0; font-size: 13px; }
.message-template-filter-popover { position: fixed; z-index: 1000; box-sizing: border-box; width: min(460px, calc(100vw - 20px)); padding: 10px; overflow-y: auto; border: 1px solid var(--border-default); border-radius: var(--radius-md); background: var(--surface); color: var(--text-primary); box-shadow: var(--shadow-sm); font: inherit; scrollbar-width: thin; scrollbar-color: var(--scrollbar-thumb, #a9bdb8) transparent; }
.message-template-filter-popover-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.message-template-filter-popover-title { margin: 0; font-size: 14px; font-weight: 600; }
.message-template-filter-popover-close { display: inline-flex; align-items: center; justify-content: center; flex: 0 0 34px; width: 34px; height: 34px; padding: 6px; border: 1px solid var(--border-default); border-radius: 6px; background: var(--surface); color: var(--text-secondary); cursor: pointer; }
.message-template-filter-popover-close svg { width: 18px; height: 18px; stroke: currentColor; stroke-width: 1.7; fill: none; }
.message-template-filter-popover-actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 6px; margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border-subtle); }
.message-template-filter-popover :is(input:not([type=checkbox]),select) { box-sizing: border-box; min-width: 0; width: 100%; min-height: 34px; padding: 6px 8px; border: 1px solid var(--border-default); border-radius: 6px; background: var(--surface); color: var(--text-primary); font: inherit; }
.message-template-filter-popover :is(button,input,select):focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.message-template-filter-popover [hidden] { display: none; }

.message-templates-view input:not([type="checkbox"]),
.message-templates-view select,
.message-templates-view textarea {
  box-sizing: border-box;
  min-width: 0;
  width: 100%;
  min-height: 34px;
  padding: 6px 8px;
  border: 1px solid var(--border-default, #dce5e3);
  border-radius: 6px;
  background: #fff;
  color: var(--text-primary, #172b29);
  font: inherit;
}

.message-template-records,
.message-template-field-group,
.message-template-configuration {
  display: grid;
  align-content: start;
  gap: 8px;
  min-width: 0;
}

.message-template-heading-copy { display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0; }
.message-template-heading, .message-template-filters, .message-templates-view > .message-template-feedback { flex: 0 0 auto; }
.message-template-records { flex: 1 1 0; min-height: 0; overflow-y: auto; padding: 2px; }
.message-template-creation { flex: 1 1 0; min-height: 0; min-width: 0; }
.message-template-form.catalog-editor-form { display: grid; grid-template-rows: auto minmax(0,1fr) auto; align-items: stretch; height: 100%; min-height: 0; min-width: 0; gap: 8px; margin: 0; box-sizing: border-box; }
.message-template-form > h3 { grid-row: 1; }
.message-template-form > .message-template-layout { grid-row: 2; display: grid; grid-template-rows: auto minmax(0,1fr); gap: 8px; min-width: 0; min-height: 0; }
.message-template-layout > .message-template-identification { grid-row: 1; }
.message-template-layout > .message-template-workspace { grid-row: 2; }
.message-template-form > .message-template-footer { grid-row: 3; }
.message-template-identification { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); align-items: start; gap: 6px 10px; min-width: 0; }
.message-template-identification > label { display: grid; gap: 4px; min-width: 0; font-size: 13px; font-weight: 500; }
.message-template-form .message-template-workspace { display: grid; grid-template-columns: minmax(0,13fr) minmax(0,7fr); grid-template-rows: minmax(0,1fr); align-items: stretch; align-content: stretch; gap: 10px; min-height: 0; min-width: 0; }
.message-template-configuration { min-height: 0; overflow-y: auto; padding: 2px; container: template-configuration / inline-size; }
.message-template-configuration-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 0; }
.message-template-configuration-heading h3 { margin: 0; font-size: 14px; }
.message-template-content-region { display: flex; flex-direction: column; min-height: 0; min-width: 0; gap: 6px; }
.message-template-content-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex: 0 0 auto; }
.message-template-content-heading h3 { font-size: 14px; min-width: 0; }
.message-template-content-region .advanced-rich-editor { flex: 1 1 0; min-height: 0; }
.message-template-content-region .advanced-rich-editor.is-scrollable .rich-editor-content { padding: 10px; }
.message-template-content-region .advanced-rich-editor .rich-editor-feedback { text-align: right; padding: 2px 8px 4px; }
.message-template-footer { display: grid; gap: 6px; padding-top: 8px; border-top: 1px solid var(--border-subtle); min-width: 0; }
.message-template-footer > .message-template-feedback { max-block-size: min(100px, 18dvh); overflow-y: auto; }
.message-template-footer > .catalog-command { justify-self: end; white-space: normal; }
.message-template-footer .entity-form-actions { display: flex; align-items: flex-end; justify-content: flex-end; flex-wrap: nowrap; gap: 7px; margin: 0; }
.message-template-footer .entity-form-actions .catalog-command { min-width: 0; width: auto; white-space: nowrap; }
.message-template-mobile-regions { display: none; }
.message-template-empty { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 6px; padding: 18px 10px; border-radius: var(--radius-sm); background: var(--surface-subtle); box-sizing: border-box; }
.message-template-empty > svg { width: 26px; height: 26px; fill: none; stroke: var(--text-muted); stroke-width: 1.5; }
.message-template-empty h3, .message-template-empty p { margin: 0; }
.message-template-empty h3 { font-size: 14px; font-weight: 500; }
.message-template-empty p { color: var(--text-secondary); font-size: 13px; }

.message-template-record {
  padding: 6px 10px;
  border: 1px solid var(--border-default, #dce5e3);
  border-radius: var(--radius-sm, 8px);
  background: #fff;
  min-width: 0;
}

.message-template-record-copy {
  display: grid;
  gap: 2px;
  min-width: 0;
  overflow-wrap: anywhere;
}
.message-template-record-line { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 8px; min-width: 0; }
.message-template-record-copy > strong { font-size: 13px; font-weight: 600; line-height: 1.3; }

.message-template-record-meta,
.message-template-help,
.message-template-feedback {
  margin: 0;
  overflow-wrap: anywhere;
  color: var(--text-secondary, #536563);
  font-size: 12px;
  line-height: 1.4;
}

.message-template-feedback:empty { display: none; }
.message-template-feedback.form-error { color: var(--danger, #8e2430); }

.message-template-form-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  min-width: 0;
}
.message-template-record-actions { display: flex; align-items: center; justify-content: flex-end; flex-wrap: nowrap; gap: 4px; min-width: 0; }
.message-template-record-actions > .catalog-command { box-sizing: border-box; flex: 0 0 36px; width: 36px; height: 36px; min-height: 36px; padding: 6px; }

.message-template-creation:empty { display: none; }
.message-template-field-group { margin: 0; padding: 8px 0 0; border: 0; border-top: 1px solid var(--border-subtle); }
.message-template-group-title { display: flex; align-items: center; gap: 4px; margin: 0; font-size: 13px; font-weight: 500; }
.message-template-availability { gap: 4px; }
.message-template-availability-options { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; min-width: 0; }
.message-template-availability-options > .message-template-check { flex: 0 0 auto; align-items: center; min-height: 30px; white-space: nowrap; }
.message-template-visibility-switch { display: inline-flex; flex: 0 0 auto; gap: 2px; padding: 2px; border: 1px solid var(--border-default); border-radius: 6px; min-width: 0; }
.message-templates-view .message-template-visibility-switch > button { display: inline-flex; align-items: center; gap: 4px; min-width: 0; min-height: 28px; padding: 3px 6px; border-color: transparent; border-radius: 4px; box-shadow: none; font-size: 12px; font-weight: 400; }
.message-template-visibility-switch > button[aria-pressed=true] { color: var(--brand-hover); background: var(--brand-soft); border-color: var(--brand); }
.message-template-visibility-switch > button:hover:not(:disabled) { background: var(--surface-subtle); }
.message-template-visibility-mark { display: inline-flex; flex: 0 0 auto; align-items: center; gap: 4px; padding: 2px 3px; border-radius: 4px; color: var(--text-secondary); font-size: 12px; font-weight: 400; line-height: 1.3; white-space: nowrap; }
.message-template-visibility-mark svg, .message-template-choice-scope svg { flex: 0 0 14px; width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 1.7; }
.message-template-visibility-mark > span { min-width: 0; }
.message-template-choice-scope { display: flex; align-items: center; gap: 4px; font-size: 11px; color: var(--text-secondary); }
.message-template-delete-confirmation { inset: auto; margin: 0; height: fit-content; width: min(390px, calc(100vw - 20px)); overflow-wrap: anywhere; }
.message-template-delete-confirmation .message-template-filter-popover-header p { margin: 6px 0 0; font-size: 13px; line-height: 1.4; }
.message-template-delete-confirmation .message-template-filter-popover-actions > button { min-width: 0; white-space: normal; font-weight: 400; }
.message-template-field-group > label:not(.message-template-check) { display: grid; gap: 4px; font-size: 13px; }
.message-template-context-group { gap: 4px 6px; padding: 0; border: 0; }
.message-template-resolution-options { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; grid-column: 1 / -1; min-width: 0; }
.message-template-resolution-options > .message-template-check { flex: 1 0 auto; align-items: center; min-height: 28px; white-space: nowrap; }
.message-template-context-group > .message-template-group-title { grid-column: 1 / -1; color: var(--text-secondary); font-size: 12px; }
.message-template-check { display: flex; align-items: flex-start; gap: 6px; min-width: 0; font-size: 13px; line-height: 1.4; overflow-wrap: anywhere; }
.message-template-check input { order: -1; width: 16px; height: 16px; flex: 0 0 16px; margin: 0; }
.message-template-option { display: flex; align-items: flex-start; gap: 4px; min-width: 0; }
.message-template-option > label { flex: 1; }
.message-template-automatic-option { grid-column: 1 / -1; padding-inline-start: 10px; border-inline-start: 2px solid var(--border-subtle); }
.message-templates-view .message-template-info { flex: 0 0 26px; width: 26px; min-height: 26px; padding: 4px; border-color: transparent; color: var(--text-secondary); vertical-align: middle; }
.message-template-form .catalog-scope-heading > .message-template-info { margin-inline-start: 4px; }
.message-template-info .record-action-label { display: none; }
.message-template-form textarea { resize: vertical; height: 36px; min-height: 36px; max-height: 70px; overflow-y: auto; }
.message-template-form .message-template-associations { display: flex; flex-direction: column; gap: 6px; padding-top: 8px; }
.message-template-associations > .catalog-association-controls { grid-template-columns: minmax(0,1fr); gap: 6px; }
.message-template-associations .catalog-association-control { padding: 6px 8px; }
.message-template-associations .catalog-scope-heading { display: flex; align-items: center; }
.message-template-associations .catalog-selection-chip { max-width: min(220px, calc(100% - 36px)); }
.message-template-associations > .catalog-association-options { display: block; }
.message-template-associations .catalog-association-options-panel { gap: 6px; padding: 8px; }
.message-template-associations .catalog-association-options-panel > label { display: grid; gap: 4px; min-width: 0; font-size: 12px; }
.message-template-associations .catalog-choice-page { max-block-size: clamp(68px, calc(var(--catalog-viewport-height,100vh) * .22), 204px); }
.message-template-form .message-template-associations [hidden] { display: none; }
.message-template-form .message-template-category-scope > .catalog-scope-header { grid-template-columns: minmax(0,1fr) auto 36px; align-items: center; gap: 4px; }
.message-template-category-scope .catalog-scope-header > .catalog-scope-title-line { grid-row: 1; grid-column: 1; }
.message-template-category-scope .catalog-scope-header > .catalog-scope-copy { grid-row: 2; grid-column: 1 / -1; }
.message-template-category-scope .catalog-scope-header > .catalog-scope-toggle { grid-row: 1; grid-column: 3; align-self: center; }
.message-template-category-lock { grid-row: 1; grid-column: 2; display: inline-flex; align-items: center; gap: 4px; min-width: 0; color: var(--text-secondary); font-size: 12px; font-weight: 400; white-space: nowrap; }
.message-template-category-lock-compact { display: none; }
.message-templates-view .message-template-category-lock-help { flex: 0 0 26px; margin: 0; }
@container template-configuration (max-width: 499px) {
  .message-template-category-lock-text { display: none; }
  .message-template-category-lock-compact { display: inline; }
}
@container template-configuration (max-width: 399px) {
  .message-template-category-lock-compact { display: none; }
}
@container template-configuration (min-width: 400px) {
  .message-template-context-group { grid-template-columns: repeat(2,minmax(0,1fr)); }
}
.message-template-rich-preview { flex: 1 1 0; min-height: 0; min-width: 0; overflow-y: auto; padding: 12px; border: 1px solid var(--border-default); border-radius: 8px; background: var(--surface); }
.message-template-rich-preview > .rich-text-output { max-height: none; overflow: visible; }

.message-templates-view :is(button, input, select, textarea):focus-visible,
.message-template-popover :is(button, input):focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* Compatibility mount is a live region only; its action lives in the toolbar. */
.message-template-picker { position: absolute; width: 1px; height: 1px; margin: 0; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
.message-template-automatic-feedback { padding: 0 10px 5px; }
.message-template-popover { position: fixed; z-index: 100; inset: auto; display: flex; flex-direction: column; box-sizing: border-box; width: 340px; max-width: calc(100vw - 20px); height: fit-content; min-height: 0; margin: 0; padding: 8px; gap: 6px; overflow-y: auto; border: 1px solid var(--border-default); border-radius: 8px; box-shadow: var(--shadow-sm); background: var(--surface); color: var(--text-primary); font: 400 13px/1.35 Aptos, Calibri, "Segoe UI", sans-serif; scrollbar-width: thin; scrollbar-color: var(--scrollbar-thumb, #a9bdb8) transparent; }
.message-template-popover::backdrop { background: transparent; backdrop-filter: none; }
.message-template-popover-header { display: flex; flex: 0 0 auto; align-items: center; justify-content: space-between; gap: 6px; min-width: 0; }
.message-template-popover-title { margin: 0; color: var(--text-primary); font: 500 13px/1.35 Aptos, Calibri, "Segoe UI", sans-serif; }
.message-template-popover-close, .message-template-search-clear { display: inline-flex; flex: 0 0 28px; align-items: center; justify-content: center; box-sizing: border-box; width: 28px; height: 28px; min-height: 28px; padding: 4px; border: 1px solid transparent; border-radius: 5px; background: transparent; color: var(--text-secondary); box-shadow: none; cursor: pointer; }
.message-template-popover-close:hover, .message-template-search-clear:hover { background: var(--surface-subtle); border-color: var(--border-default); }
.message-template-popover svg { flex: 0 0 17px; width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.message-template-popover-body { display: flex; flex: 1 1 auto; flex-direction: column; gap: 6px; min-width: 0; min-height: 0; }
.message-template-search { display: flex; flex: 0 0 auto; align-items: center; gap: 5px; min-width: 0; min-height: 32px; padding: 0 6px; border: 1px solid var(--border-default); border-radius: 5px; background: var(--surface); color: var(--text-muted); }
.message-template-search:focus-within { border-color: var(--brand); }
.message-template-search > input { box-sizing: border-box; flex: 1 1 0; min-width: 0; width: 100%; height: 30px; min-height: 30px; margin: 0; padding: 3px 0; border: 0; border-radius: 0; background: transparent; color: var(--text-primary); box-shadow: none; outline: 0; font: inherit; }
.message-template-search > input::-webkit-search-cancel-button { -webkit-appearance: none; }
.message-template-popover .message-template-search > input:focus-visible { outline: 0; }
.message-template-search:has(> input:focus-visible) { outline: 2px solid var(--brand); outline-offset: 2px; }
.message-template-search-clear { flex-basis: 24px; width: 24px; height: 26px; min-height: 26px; padding: 3px; }
.message-template-choice-list { display: grid; flex: 0 1 auto; align-content: start; gap: 2px; min-width: 0; min-height: 0; max-height: min(320px, 45dvh); overflow-y: auto; padding: 2px; scroll-padding-block: 4px; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: var(--scrollbar-thumb, #a9bdb8) transparent; }
.message-template-choice-list:empty { display: none; }
.message-template-choice-list:focus-visible { outline: 2px solid var(--brand); outline-offset: -2px; }
.message-template-choice { display: grid; gap: 2px; box-sizing: border-box; min-width: 0; width: 100%; padding: 6px; border: 1px solid transparent; border-radius: 5px; background: var(--surface); color: var(--text-primary); text-align: start; font: inherit; overflow-wrap: anywhere; cursor: pointer; }
.message-template-choice:hover { border-color: var(--border-default); background: var(--surface-subtle); }
.message-template-choice strong { font-size: 13px; font-weight: 500; }
.message-template-choice span { display: -webkit-box; overflow: hidden; -webkit-line-clamp: 2; -webkit-box-orient: vertical; font-size: 12px; color: var(--text-secondary); }
.message-template-choice:disabled { opacity: .65; cursor: wait; }
.message-template-popover .message-template-feedback { flex: 0 0 auto; margin: 0; padding: 6px 2px; text-align: center; font-size: 12px; font-weight: 400; }
.message-template-popover .message-template-feedback.has-error { color: var(--danger); }
.message-template-popover .message-template-retry { flex: 0 0 auto; align-self: center; min-height: 30px; padding: 4px 7px; font-size: 12px; font-weight: 400; }
.message-template-popover-actions:empty, .message-template-popover [hidden] { display: none; }

.message-templates-view [hidden] { display: none; }

@media (max-width: 480px) {
  .message-templates-view { padding: 10px; gap: 8px; }
  .message-template-identification { grid-template-columns: minmax(0,1fr); }
  .message-templates-view input:not([type=checkbox]), .message-templates-view select, .message-templates-view textarea { font-size: 16px; }
  .message-template-filter-scopes { grid-template-columns: minmax(0, 1fr); }
  .message-template-record { padding: 6px 8px; }
  .message-template-visibility-mark > span { display: none; }
  .message-template-filter-popover :is(input:not([type=checkbox]),select) { font-size: 16px; }
  .message-template-filter-fields { grid-template-columns: minmax(0,1fr); }
  .message-template-popover .message-template-search > input { font-size: 16px; }
  .message-template-popover-close { width: 32px; height: 32px; flex-basis: 32px; }
}

@media (max-width: 900px) {
  .message-template-form > .message-template-layout { grid-template-rows: auto auto minmax(0,1fr); }
  .message-template-layout > .message-template-mobile-regions { grid-row: 2; }
  .message-template-layout > .message-template-workspace { grid-row: 3; }
  .message-template-mobile-regions { display: flex; gap: 6px; }
  .message-template-mobile-regions button { flex: 1; min-width: 0; padding-inline: 6px; }
  .message-template-mobile-regions button[aria-pressed=true] { border-color: var(--brand); background: var(--brand-soft); color: var(--brand-hover); }
  .message-template-form .message-template-workspace { grid-template-columns: minmax(0,1fr); }
  .message-template-form:not([data-region=content]) .message-template-content-region,
  .message-template-form:not([data-region=configuration]) .message-template-configuration { display: none; }
}

@media (max-height: 560px) {
  .message-template-form > .message-template-layout { display: flex; flex-direction: column; overflow-y: auto; scrollbar-width: thin; scrollbar-color: var(--scrollbar-thumb, #a9bdb8) transparent; }
  .message-template-layout > :is(.message-template-identification,.message-template-mobile-regions) { flex: 0 0 auto; }
  .message-template-layout > .message-template-workspace { flex: 1 0 260px; min-block-size: 260px; }
  .message-template-content-region { overflow-y: auto; scrollbar-width: thin; }
  .message-template-content-region .advanced-rich-editor.is-scrollable .rich-editor-content { min-block-size: 96px; }
}

/* On phones the form, including its actions, owns one continuous scroll area. */
@media (max-width: 767px), (max-width: 959px) and (max-height: 500px) and (pointer: coarse) and (orientation: landscape) {
  .app-shell.catalog-shell:has(.message-template-form) {
    position: fixed;
    inset: auto;
    top: var(--catalog-viewport-top, 0px);
    left: var(--catalog-viewport-left, 0px);
    width: var(--catalog-viewport-width, 100vw);
  }
  .message-template-form.catalog-editor-form { display: flex; flex-direction: column; overflow-y: auto; overscroll-behavior-y: contain; scroll-padding-block: 8px; }
  .message-template-form > h3,
  .message-template-form > .message-template-footer { flex: 0 0 auto; position: static; }
  .message-template-form > .message-template-layout { display: flex; flex: 1 0 auto; flex-direction: column; min-height: auto; overflow: visible; }
  .message-template-form .message-template-workspace { display: flex; flex: 1 0 auto; flex-direction: column; min-block-size: 0; overflow: visible; }
  .message-template-form .message-template-content-region { flex: 1 0 auto; overflow: visible; }
  .message-template-form .message-template-configuration { flex: 0 0 auto; overflow: visible; }
  .message-template-content-region .advanced-rich-editor.is-scrollable { flex: 1 0 auto; }
  .message-template-content-region .advanced-rich-editor.is-scrollable .rich-editor-content { flex: 1 0 auto; min-block-size: 8em; overflow: visible; }
  .message-template-form .message-template-rich-preview { flex: 0 0 auto; overflow: visible; }
}

