/*
  UI kit classes en-* (components in Shared/ render exactly these). Ported from CampaignManager's components.css with
  its rules: one main action per screen, colour carries meaning, 40px controls (32px small), a modal is the native
  <dialog>, a message is always an Alert.
*/
@layer kit {
  /* ── Headings ── */
  .en-h2 {
    font-size: 1.25rem;
    font-weight: 600;
    line-height: 1.3;
  }

  .en-h3 {
    font-size: 1.0625rem;
    font-weight: 600;
    line-height: 1.35;
  }

  .en-section-title {
    font-size: 0.8125rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--primary-500);
  }

  /* ── Buttons ── primary — the one main action; secondary — neutral; outline — edit in a row; error — irreversible
     confirmation in a dialog; ghost — close, chevrons. */
  .en-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: var(--tap);
    padding: 0.5rem 1rem;
    font-size: 0.9375rem;
    font-weight: 600;
    line-height: 1.25;
    white-space: nowrap;
    color: var(--primary-800);
    background: var(--white);
    border: 1px solid transparent;
    border-radius: var(--radius);
    box-shadow: var(--shadow-card);
    text-decoration: none;
    user-select: none;
    transition: background-color 150ms ease, border-color 150ms ease, color 150ms ease;
  }

  .en-btn:focus-visible {
    outline: 2px solid var(--accent-500);
    outline-offset: 2px;
  }

  .en-btn:disabled,
  .en-btn[aria-disabled="true"] {
    background: var(--primary-200);
    border-color: var(--primary-200);
    color: var(--primary-500);
    opacity: 0.7;
    box-shadow: none;
    cursor: not-allowed;
  }

  .en-btn-sm {
    min-height: var(--tap-sm);
    padding: 0.25rem 0.75rem;
    font-size: 0.875rem;
  }

  .en-btn-lg {
    min-height: 3rem;
    padding: 0.625rem 1.5rem;
    font-size: 1rem;
  }

  .en-btn-icon {
    width: var(--tap);
    padding: 0;
    gap: 0;
  }

  .en-btn-sm.en-btn-icon {
    width: var(--tap-sm);
  }

  .en-btn-primary { background: var(--primary-800); color: var(--white); }
  .en-btn-primary:hover:not(:disabled) { background: var(--primary-900); color: var(--white); }

  .en-btn-accent { background: var(--accent-600); color: var(--white); }
  .en-btn-accent:hover:not(:disabled) { background: var(--accent-700); color: var(--white); }

  .en-btn-secondary { background: var(--white); color: var(--primary-800); border-color: var(--primary-300); }
  .en-btn-secondary:hover:not(:disabled) { background: var(--primary-50); border-color: var(--primary-400); color: var(--primary-900); }

  .en-btn-success { background: var(--success-600); color: var(--white); }
  .en-btn-success:hover:not(:disabled) { background: var(--success-700); color: var(--white); }

  .en-btn-error { background: var(--error-600); color: var(--white); }
  .en-btn-error:hover:not(:disabled) { background: var(--error-700); color: var(--white); }

  .en-btn-warning { background: var(--warning-500); color: var(--white); }
  .en-btn-warning:hover:not(:disabled) { background: var(--warning-600); color: var(--white); }

  .en-btn-outline { background: var(--white); color: var(--accent-700); border-color: var(--accent-200); }
  .en-btn-outline:hover:not(:disabled) { background: var(--accent-50); border-color: var(--accent-400); color: var(--accent-800); }

  .en-btn-outline-error { background: var(--white); color: var(--error-700); border-color: var(--error-200); }
  .en-btn-outline-error:hover:not(:disabled) { background: var(--error-50); border-color: var(--error-500); color: var(--error-800); }

  .en-btn-ghost { background: transparent; color: var(--primary-600); box-shadow: none; }
  .en-btn-ghost:hover:not(:disabled) { background: var(--primary-100); color: var(--primary-900); }

  /* A toggled button (Hide text, Loop): light with an accent frame, never the dark main-button fill. */
  .en-btn.en-btn-on {
    color: var(--accent-800);
    background: var(--accent-50);
    border-color: var(--accent-600);
    box-shadow: inset 0 0 0 1px var(--accent-600);
  }

  .en-btn-reason {
    font-size: 0.8125rem;
    line-height: 1.35;
    color: var(--primary-600);
  }

  /* ── Segmented choice ── */
  .en-segmented {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 0.25rem;
  }

  .en-segment {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
    min-height: var(--tap-sm);
    min-width: var(--tap-sm);
    padding: 0.25rem 0.75rem;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.25;
    color: var(--primary-700);
    background: var(--white);
    border: 1px solid var(--primary-300);
    border-radius: 0.375rem;
  }

  @media (width < 30rem) {
    .en-segment { padding: 0.25rem 0.5rem; }
  }

  .en-segment:hover:not(:disabled) { background: var(--primary-50); }

  .en-segment[aria-checked="true"] {
    color: var(--accent-800);
    font-weight: 600;
    background: var(--accent-50);
    border-color: var(--accent-600);
    box-shadow: inset 0 0 0 1px var(--accent-600);
  }

  .en-segment:disabled { opacity: 0.55; cursor: not-allowed; }

  .en-segment:focus-visible {
    outline: 2px solid var(--accent-500);
    outline-offset: 2px;
  }

  .en-segment-count {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--primary-500);
  }

  /* ── Fields ── 16px text on phones: a smaller font makes iPhone Safari zoom the page on focus. */
  .en-input {
    display: block;
    width: 100%;
    min-height: var(--tap);
    padding: 0.5rem 0.75rem;
    font-size: 1rem;
    line-height: 1.5;
    color: var(--primary-900);
    background: var(--primary-50);
    border: 1px solid var(--primary-300);
    border-radius: 0.375rem;
    transition: border-color 150ms ease, box-shadow 150ms ease, background-color 150ms ease;
  }

  @media (width >= 48rem) {
    .en-input { font-size: 0.9375rem; }
  }

  .en-input::placeholder { color: var(--primary-400); }
  .en-input:hover:not(:disabled) { border-color: var(--primary-400); background: var(--white); }

  .en-input:focus {
    outline: none;
    background: var(--white);
    border-color: var(--accent-500);
    box-shadow: var(--focus-ring);
  }

  .en-input:disabled { background: var(--primary-100); color: var(--primary-400); cursor: not-allowed; }
  textarea.en-input { min-height: 5rem; resize: vertical; }
  .en-input-sm { min-height: var(--tap-sm); padding: 0.25rem 0.5rem; }
  textarea.en-input-sm { min-height: 2.5rem; }

  .en-checkbox {
    width: 1.25rem;
    height: 1.25rem;
    accent-color: var(--accent-600);
    flex-shrink: 0;
    cursor: pointer;
  }

  /* A checkbox with its label: the whole row is the tap target. */
  .en-tap-row {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    min-height: var(--tap-sm);
    cursor: pointer;
  }

  .en-field {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 0;
  }

  .en-field-label {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--primary-700);
  }

  .en-field-hint { font-weight: 400; color: var(--primary-500); }
  .en-field-required { margin-left: 0.125rem; color: var(--error-600); }
  .en-field-note { font-size: 0.8125rem; line-height: 1.35; color: var(--primary-600); }
  .en-field-error { font-size: 0.8125rem; line-height: 1.35; color: var(--error-700); }
  .en-field-invalid .en-input { border-color: var(--error-500); }

  .en-field-row {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 0.75rem;
  }

  .en-field-row > .en-field { flex: 1 1 12rem; }

  /* ── Card ── One card, never cards inside cards: inside, blocks are separated by rules (en-list rows). */
  .en-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    min-width: 0;
  }

  .en-card-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    border-bottom: 1px solid var(--border);
  }

  .en-card-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--primary-800);
  }

  .en-card-count {
    padding: 0.05rem 0.45rem;
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--primary-600);
    background: var(--primary-100);
    border-radius: 999px;
  }

  .en-card-body { padding: 1rem; }

  .en-card-footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border);
  }

  /* Rows inside a card: a rule between them, the whole row a link or a button when it opens something. */
  .en-list > * + * { border-top: 1px solid var(--border); }

  .en-list-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-height: 3rem;
    padding: 0.5rem 1rem;
    color: inherit;
    text-decoration: none;
  }

  a.en-list-item:hover,
  button.en-list-item:hover {
    background: var(--primary-50);
    color: inherit;
  }

  button.en-list-item {
    width: 100%;
    text-align: left;
    background: none;
    border: none;
  }

  /* ── Badge ── */
  .en-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.0625rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 600;
    line-height: 1.25rem;
    white-space: nowrap;
    border: 1px solid transparent;
    border-radius: 999px;
  }

  .en-badge-neutral { background: var(--primary-100); color: var(--primary-700); border-color: var(--primary-200); }
  .en-badge-accent { background: var(--accent-100); color: var(--accent-800); border-color: var(--accent-200); }
  .en-badge-success { background: var(--success-100); color: var(--success-800); border-color: var(--success-200); }
  .en-badge-warning { background: var(--warning-100); color: var(--warning-800); border-color: var(--warning-200); }
  .en-badge-error { background: var(--error-100); color: var(--error-800); border-color: var(--error-200); }

  /* ── Alert ── The only way to show a message; carries its own bottom margin (class mb-0 removes it). */
  .en-alert {
    display: flex;
    align-items: flex-start;
    gap: 0.625rem;
    margin-bottom: 0.75rem;
    padding: 0.75rem 1rem;
    font-size: 0.875rem;
    line-height: 1.5;
    border: 1px solid transparent;
    border-radius: var(--radius);
  }

  .en-alert-title { font-weight: 600; line-height: 1.3; }
  .en-alert-title + .en-alert-body { margin-top: 0.125rem; }
  .en-alert-icon { flex-shrink: 0; line-height: 1.3rem; }

  .en-alert-close {
    flex-shrink: 0;
    margin: -0.5rem -0.5rem -0.5rem 0;
    color: inherit;
  }

  .en-alert-neutral { background: var(--primary-50); color: var(--primary-900); border-color: var(--primary-200); }
  .en-alert-accent { background: var(--accent-50); color: var(--accent-900); border-color: var(--accent-200); }
  .en-alert-accent .en-alert-icon { color: var(--accent-600); }
  .en-alert-success { background: var(--success-50); color: var(--success-900); border-color: var(--success-200); }
  .en-alert-success .en-alert-icon { color: var(--success-600); }
  .en-alert-warning { background: var(--warning-50); color: var(--warning-900); border-color: var(--warning-200); }
  .en-alert-warning .en-alert-icon { color: var(--warning-600); }
  .en-alert-error { background: var(--error-50); color: var(--error-900); border-color: var(--error-200); }
  .en-alert-error .en-alert-icon { color: var(--error-600); }

  /* ── Spinner ── the only one. */
  .en-spinner {
    display: inline-block;
    width: 2rem;
    height: 2rem;
    border: 4px solid var(--accent-500);
    border-right-color: transparent;
    border-radius: 999px;
    animation: en-spin 0.8s linear infinite;
  }

  .en-spinner-sm { width: 1rem; height: 1rem; border-width: 2px; }

  @keyframes en-spin {
    to { transform: rotate(360deg); }
  }

  .en-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    padding: 2.5rem 0;
    color: var(--primary-500);
  }

  /* ── Empty state ── */
  .en-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 1.5rem;
    text-align: center;
  }

  .en-empty-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.75rem;
    height: 2.75rem;
    margin-bottom: 0.75rem;
    font-size: 1.125rem;
    color: var(--accent-600);
    background: var(--accent-50);
    border-radius: 999px;
  }

  .en-empty-compact {
    padding: 0.75rem 1rem;
    font-size: 0.875rem;
    color: var(--primary-600);
  }

  /* ── Stat tile ── */
  .en-stat {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
    min-width: 0;
  }

  .en-stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    line-height: 1.1;
    font-variant-numeric: tabular-nums;
    color: var(--primary-900);
  }

  .en-stat-label {
    font-size: 0.8125rem;
    color: var(--primary-600);
  }

  /* ── Modal ── the native <dialog> in the top layer: focus, Esc and the backdrop come from the browser. */
  .en-modal {
    width: calc(100% - 2rem);
    max-height: calc(100dvh - 2rem);
    margin: auto;
    padding: 0;
    color: var(--primary-900);
    background: var(--white);
    border: none;
    border-radius: 1rem;
    box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25);
    overflow: hidden;
  }

  .en-modal[open] {
    display: flex;
    flex-direction: column;
    animation: en-modal-in 150ms ease-out;
  }

  .en-modal::backdrop { background: rgb(15 23 42 / 0.45); }
  .en-modal-sm { max-width: 28rem; }
  .en-modal-md { max-width: 36rem; }
  .en-modal-lg { max-width: 48rem; }

  @media (width >= 40rem) {
    .en-modal { max-height: calc(100dvh - 5rem); }
  }

  .en-modal-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-shrink: 0;
    padding: 0.5rem 0.5rem 0.5rem 1.25rem;
    border-bottom: 1px solid var(--border);
  }

  .en-modal-title {
    flex: 1;
    min-width: 0;
    font-size: 1.0625rem;
    font-weight: 600;
  }

  .en-modal-body {
    flex: 1 1 auto;
    padding: 1.25rem;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .en-modal-footer {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 0.5rem;
    flex-shrink: 0;
    padding: 0.75rem 1.25rem;
    background: var(--primary-50);
    border-top: 1px solid var(--border);
  }

  /* «Cancel» on the left, the main action on the right. */
  .en-modal-footer > .en-btn-secondary:first-of-type:not(:last-of-type) { margin-right: auto; }
  .en-modal-footer-error { flex: 1 0 100%; }
  .en-modal-footer-error > .en-alert { margin-bottom: 0; }

  .en-modal-footer-hint {
    flex: 1 0 100%;
    font-size: 0.8125rem;
    text-align: right;
    color: var(--primary-600);
  }

  @keyframes en-modal-in {
    from { opacity: 0; transform: scale(0.97); }
    to { opacity: 1; transform: scale(1); }
  }

  /* Delete lives at the end of an edit form, separated from Save. */
  .en-danger-zone {
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
  }

  /* ── Toasts ── Alerts stacked at the bottom centre; above the tab bar on phones. */
  .en-toasts {
    position: fixed;
    z-index: 70;
    left: 50%;
    bottom: calc(1rem + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    width: min(28rem, calc(100vw - 2rem));
    pointer-events: none;
  }

  @media (width < 48rem) {
    .en-toasts { bottom: calc(var(--tabbar-height) + env(safe-area-inset-bottom) + 0.75rem); }
  }

  .en-toasts > * {
    pointer-events: auto;
    margin-bottom: 0;
    box-shadow: 0 4px 12px -2px rgb(0 0 0 / 0.15);
  }

  /* ── Progress bar ── */
  .en-progress {
    height: 0.375rem;
    overflow: hidden;
    background: var(--primary-200);
    border-radius: 999px;
  }

  .en-progress > span {
    display: block;
    height: 100%;
    background: var(--accent-500);
    border-radius: inherit;
    transition: width 200ms ease;
  }

  /* An audio element styled to the page width; the browser draws the controls. */
  .en-audio {
    width: 100%;
    height: 2.5rem;
  }
}
