@layer cinder.tokens, cinder.foundation, cinder.components, cinder.utilities;
@layer cinder.components {
  /* ========================================
 * ERROR STATE
 * ======================================== */

  /* The default layout is the centred page state: it fills the region it is given and centres
   * its content both ways, with 8px of extra bottom padding for optical centre. */
  .cinder-error-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    inline-size: 100%;
    /* Fills a region of known height, as the empty state does; in an auto-height parent it is
       ignored and the padding alone spaces it. */
    min-block-size: 100%;
    box-sizing: border-box;
    padding-block: var(--cinder-space-2) var(--cinder-space-4);
    padding-inline: var(--cinder-space-6);
  }

  /* An opt-in readable width: the state stops at about 70 characters, so a row in a wide column
   * keeps its action beside its title. Off by default. */
  .cinder-error-state[data-cinder-measure] {
    max-inline-size: 70ch;
  }

  /* A bordered state draws its own box, as the empty state does, for a body with no container,
   * and pads itself the way the empty state does, so the two read as one family when the box is
   * not in a region of known height. */
  .cinder-error-state[data-cinder-bordered] {
    border: var(--cinder-border-width) solid var(--cinder-border);
    border-radius: var(--cinder-radius-lg);
    background: var(--cinder-surface);
    padding-block: var(--cinder-space-12) calc(var(--cinder-space-12) + var(--cinder-space-2));
    padding-inline: var(--cinder-space-8);
  }

  .cinder-error-state[data-cinder-variant='flat'] {
    align-items: flex-start;
    min-block-size: 0;
    padding: 0;
  }

  .cinder-error-state-body {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    min-inline-size: 0;
    max-inline-size: 100%;
  }

  .cinder-error-state[data-cinder-variant='flat'] .cinder-error-state-body {
    align-items: flex-start;
    text-align: start;
  }

  /* The disc: 48px, red for a failure and neutral for a state where nothing failed. */
  .cinder-error-state-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    inline-size: var(--cinder-space-12);
    block-size: var(--cinder-space-12);
    border-radius: var(--cinder-radius-full);
    background: var(--cinder-status-danger-background);
    color: var(--cinder-status-danger-text);
  }

  .cinder-error-state-icon > svg {
    inline-size: 1.375rem;
    block-size: 1.375rem;
  }

  .cinder-error-state[data-cinder-tone='neutral'] .cinder-error-state-icon {
    background: var(--cinder-surface-inset);
    color: var(--cinder-text-muted);
  }

  .cinder-error-state[data-cinder-tone='warning'] .cinder-error-state-icon {
    background: var(--cinder-status-warning-background);
    color: var(--cinder-status-warning-text);
  }

  /* Compact fits a list pane: less padding, a smaller neutral disc, and quieter type. */
  .cinder-error-state[data-cinder-size='compact'] {
    padding-block: var(--cinder-space-6);
    padding-inline: var(--cinder-space-4);
  }

  .cinder-error-state[data-cinder-size='compact'] .cinder-error-state-icon {
    inline-size: var(--cinder-space-10);
    block-size: var(--cinder-space-10);
    background: var(--cinder-surface-inset);
    color: var(--cinder-text-muted);
  }

  .cinder-error-state[data-cinder-size='compact'] .cinder-error-state-icon > svg {
    inline-size: var(--cinder-space-5);
    block-size: var(--cinder-space-5);
  }

  .cinder-error-state[data-cinder-size='compact'] .cinder-error-state-title {
    margin-block-start: var(--cinder-space-2);
    font-size: var(--cinder-text-base);
  }

  .cinder-error-state[data-cinder-size='compact'] .cinder-error-state-action {
    margin-block-start: var(--cinder-space-2);
  }

  .cinder-error-state-title {
    margin: 0;
    margin-block-start: var(--cinder-space-3-5);
    font-size: var(--cinder-text-lg);
    font-weight: var(--cinder-font-semibold);
    line-height: var(--cinder-leading-normal);
    color: var(--cinder-text-default);
  }

  .cinder-error-state[data-cinder-variant='flat'] .cinder-error-state-title {
    margin-block-start: 0;
    font-size: var(--cinder-text-base);
    font-weight: var(--cinder-font-medium);
    line-height: var(--cinder-leading-snug);
  }

  .cinder-error-state[data-cinder-variant='flat'][data-cinder-tone='danger']
    .cinder-error-state-title {
    color: var(--cinder-status-danger-text);
  }

  .cinder-error-state[data-cinder-variant='flat'][data-cinder-tone='warning']
    .cinder-error-state-title {
    color: var(--cinder-status-warning-text);
  }

  /* The sentence holds a 380px measure however wide the region is. */
  .cinder-error-state-description,
  .cinder-error-state-remediation {
    margin: 0;
    margin-block-start: var(--cinder-space-1);
    max-inline-size: 23.75rem;
    font-size: var(--cinder-text-sm);
    line-height: var(--cinder-leading-normal);
    color: var(--cinder-text-muted);
  }

  /* The next step reads as its own line, a little apart from the sentence that says what failed. */
  .cinder-error-state-remediation {
    margin-block-start: var(--cinder-space-2);
  }

  .cinder-error-state[data-cinder-variant='flat'] .cinder-error-state-description,
  .cinder-error-state[data-cinder-variant='flat'] .cinder-error-state-remediation {
    max-inline-size: none;
  }

  .cinder-error-state-status {
    margin: 0;
    margin-block-start: var(--cinder-space-2);
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-normal);
    color: var(--cinder-text-subtle);
    font-variant-numeric: tabular-nums;
  }

  .cinder-error-state-action {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: var(--cinder-space-2);
    margin-block-start: var(--cinder-space-4);
  }

  /* The flat state reads from the start, but its action row spans the state and ends at the
   * trailing content edge, the primary last. */
  .cinder-error-state[data-cinder-variant='flat'] .cinder-error-state-body {
    align-self: stretch;
  }

  .cinder-error-state[data-cinder-variant='flat'] .cinder-error-state-action {
    align-self: stretch;
    justify-content: flex-end;
    margin-block-start: var(--cinder-space-3);
  }

  /* Codes and fault text sit behind a quiet disclosure, never in the plain-words sentence. */
  .cinder-error-state-details {
    margin-block-start: var(--cinder-space-3);
    max-inline-size: 23.75rem;
    font-size: var(--cinder-text-xs);
  }

  /* Retry comes before Details. Keep the shared disclosure target out of Retry's press area by
     extending into the detail that follows instead. The face and the ErrorState rhythm stay put. */
  .cinder-error-state-details-summary {
    --_cinder-disclosure-hit-block-end: min(0px, calc(1lh - var(--_cinder-disclosure-hit-target)));
    --_cinder-disclosure-hit-block-start: 0px;
  }

  .cinder-error-state[data-cinder-variant='flat'] .cinder-error-state-details {
    max-inline-size: none;
  }

  .cinder-error-state-detail {
    margin: 0;
    margin-block-start: var(--cinder-space-1);
    font-family: var(--cinder-font-mono);
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-normal);
    color: var(--cinder-text-muted);
    overflow-wrap: anywhere;
    text-align: start;
  }

  /* A row is the failure of one card among cards that are live: a small glyph, the title and the
   * description as one sentence, and a secondary retry at the end. It takes a grouped list row's
   * insets (16px at the start, 8px at the end), so its retry ends on the same edge as a list row's
   * button. A narrow row drops the retry under the words; the row is its own inline-size
   * container for that. */
  .cinder-error-state[data-cinder-variant='row'] {
    container-type: inline-size;
    container-name: cinder-error-state;
    align-items: stretch;
    min-block-size: 0;
    padding: 0;
  }

  .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-body {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--cinder-space-2) var(--cinder-space-3);
    inline-size: 100%;
    padding-block: var(--cinder-space-3);
    padding-inline: var(--cinder-space-4) var(--cinder-space-2);
    text-align: start;
  }

  .cinder-error-state-glyph {
    display: flex;
    flex-shrink: 0;
    color: var(--cinder-status-danger-text);
  }

  /* In a row the glyph sits on the title's first line, not in the middle of however many lines the
     words take: it starts at the top and is centred on one line of the title's height. */
  .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-glyph {
    align-self: flex-start;
    margin-block-start: calc((1lh - var(--cinder-space-4)) / 2);
  }

  .cinder-error-state-glyph > svg {
    inline-size: var(--cinder-space-4);
    block-size: var(--cinder-space-4);
  }

  .cinder-error-state[data-cinder-tone='neutral'] .cinder-error-state-glyph {
    color: var(--cinder-text-muted);
  }

  .cinder-error-state[data-cinder-tone='warning'] .cinder-error-state-glyph {
    color: var(--cinder-status-warning-text);
  }

  /* The row's glyph and words: one start-aligned group, so the row's centring (the body's
   * align-items) places the group and the retry on one centre line, and the glyph stays on the
   * title's first line however many lines the words take. */
  .cinder-error-state-lead {
    display: flex;
    align-items: flex-start;
    gap: var(--cinder-space-3);
    flex: 1 1 12rem;
    min-inline-size: 0;
    font-size: var(--cinder-text-base);
    line-height: var(--cinder-leading-snug);
    color: var(--cinder-text-default);
  }

  .cinder-error-state-text {
    flex: 1 1 auto;
    min-inline-size: 0;
  }

  .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-title,
  .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-description {
    display: block;
    margin: 0;
    max-inline-size: none;
    font-size: inherit;
    font-weight: var(--cinder-font-normal);
    line-height: inherit;
    color: inherit;
  }

  /* The title is the row's heading: the centred page title's semibold weight (the title and the
   * sentence under it are one size, so weight and a small gap carry the hierarchy), on a line of
   * its own, so it never runs on into the sentence under it. */
  .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-title {
    margin-block-end: var(--cinder-space-1);
    font-weight: var(--cinder-font-semibold);
  }

  /* A title that is the whole sentence has nothing under it to space from, so the gap would only
   * push the title and glyph above the action's centre line. */
  .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-title:last-child {
    margin-block-end: 0;
  }

  /* A bordered row is a quiet hairline in its tone's border token, never the centred box's grey
   * panel: the row fills a card's width and its own words carry the weight. The status border
   * tokens are drawn for a soft fill and read at 1.9:1 (light) to 4.7:1 (dark) on the page
   * surface, so the hairline mixes each toward transparent, per scheme, to about 1.5:1 against
   * that surface in both: danger 65% / 32%, warning 65% / 43% (light / dark). The neutral token
   * is already 1.4:1. */
  .cinder-error-state[data-cinder-variant='row'][data-cinder-bordered] {
    /* cinder-allow-raw-color: structural-pattern — a theme-arm mix amount of the danger border token, not a colour literal */
    --_cinder-error-state-edge: light-dark(
      color-mix(in oklch, var(--cinder-status-danger-border) 65%, transparent),
      color-mix(in oklch, var(--cinder-status-danger-border) 32%, transparent)
    );

    border-color: var(--_cinder-error-state-edge);
    border-radius: var(--cinder-radius-md);
    background: none;
    padding: 0;
  }

  .cinder-error-state[data-cinder-variant='row'][data-cinder-bordered][data-cinder-tone='warning'] {
    /* cinder-allow-raw-color: structural-pattern — a theme-arm mix amount of the warning border token, not a colour literal */
    --_cinder-error-state-edge: light-dark(
      color-mix(in oklch, var(--cinder-status-warning-border) 65%, transparent),
      color-mix(in oklch, var(--cinder-status-warning-border) 43%, transparent)
    );
  }

  .cinder-error-state[data-cinder-variant='row'][data-cinder-bordered][data-cinder-tone='neutral'] {
    /* Nothing failed, so the box is the quietest hairline the page draws, the weight its list rows
       use, not a fourth border weight. */
    --_cinder-error-state-edge: var(--cinder-border-muted);
  }

  /* The next step is not part of the sentence: it takes its own line under it, in the same type. */
  .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-remediation {
    display: block;
    margin: 0;
    margin-block-start: var(--cinder-space-1);
    max-inline-size: none;
    font-size: inherit;
    line-height: inherit;
    color: inherit;
  }

  .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-action {
    margin-block-start: 0;
    justify-content: flex-end;
  }

  .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-details {
    flex-basis: 100%;
    max-inline-size: none;
  }

  /* A narrow row: 28rem (448px; the row has no padding of its own, so that is its width). The retry
   * keeps the inline end whenever it has room beside the words, and a full-width row on a phone
   * still drops it to a line of its own, flush with the row's trailing edge (the owner's
   * trailing-edge actions, 2026-11-13), as a narrow EmptyState row does. */
  @container cinder-error-state (max-width: 28rem) {
    .cinder-error-state[data-cinder-variant='row'] .cinder-error-state-action {
      flex-basis: 100%;
      justify-content: flex-end;
    }
  }

  @media (forced-colors: active) {
    .cinder-error-state-icon {
      border: 1px solid CanvasText;
    }
  }
}
