/* Keep long service messages readable, including an unbroken filename/token. */
dialog :is(.error, .field-error, .recovery-error, .account-flow-error, .upload-file-status.is-error, .deck-file-meta.is-error) {
  overflow-wrap: anywhere;
}

@media (min-width: 601px) and (not ((orientation: landscape) and (pointer: coarse) and (max-height: 500px))) {
  /* Only error states reflow. The existing error-free frames stay unchanged. */
  #recovery-modal[data-step]:has(.recovery-error:not(:empty)),
  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) {
    height: fit-content;
    min-height: 0;
    max-height: calc(100dvh - 36px);
    overflow: auto;
  }

  #recovery-modal[data-step]:has(.recovery-error:not(:empty)) .recovery-header,
  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) .account-flow-header {
    position: relative;
    inset: auto;
  }

  #recovery-modal[data-step]:has(.recovery-error:not(:empty)) .recovery-body,
  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) > :is(form, div) {
    position: relative;
    inset: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 100%;
    height: auto;
    margin: 0;
    padding: 24px;
  }

  #recovery-modal[data-step]:has(.recovery-error:not(:empty)) form {
    position: static;
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin: 0;
  }

  #recovery-modal[data-step]:has(.recovery-error:not(:empty)) :is(.recovery-body > p, .recovery-body > button, form > label, #recovery-email, form > .recovery-error, .recovery-primary, .recovery-code-control, .recovery-resend, .recovery-resend-button, .recovery-password-control, .recovery-help),
  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) > :is(form, div) > :is(p, h3, img, label, div, button) {
    position: relative;
    inset: auto;
    flex-shrink: 0;
    width: 100%;
    height: auto;
    margin: 0;
  }

  #recovery-modal[data-step]:has(.recovery-error:not(:empty)) .recovery-password-control,
  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) .account-password-control {
    height: 51px;
  }

  #recovery-modal[data-step]:has(.recovery-error:not(:empty)) .recovery-code-control {
    align-self: center;
    max-width: 403px;
    height: 58px;
  }
  #recovery-modal[data-step]:has(.recovery-error:not(:empty)) .recovery-code-cells {
    width: 100%;
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
  #recovery-modal[data-step]:has(.recovery-error:not(:empty)) .recovery-code-cells span { width: 100%; }

  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) .account-code-inputs {
    align-self: center;
    max-width: 420px;
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) .account-code-inputs :is(input, span) { width: 100%; }

  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) .account-flow-actions {
    display: flex;
    gap: 12px;
  }
  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) .account-flow-actions > button {
    position: relative;
    inset: auto;
    flex: 1;
    min-width: 0;
    height: auto;
    min-height: 46px;
    padding: 8px;
  }
  #account-flow-modal[data-flow]:has(.account-flow-error:not(:empty)) .account-verified-message img {
    position: static;
    flex-shrink: 0;
  }
  #account-flow-modal[data-flow="delete-confirm"]:has(.account-flow-error:not(:empty)) .account-delete-confirm {
    display: grid;
    grid-template-columns: 24px minmax(0, 1fr);
  }
  #account-flow-modal[data-flow="delete-confirm"]:has(.account-flow-error:not(:empty)) .account-delete-confirm > img { width: 24px; }
  #account-flow-modal[data-flow="delete-confirm"]:has(.account-flow-error:not(:empty)) .account-delete-confirm > :is(p, div) { grid-column: 1 / -1; }



  #modal[data-kind="pitch-deck-pdf"]:has(.upload-error:not(:empty), .upload-file-status.is-error, .deck-file-meta.is-error) {
    height: fit-content;
    min-height: 0;
    max-height: calc(100dvh - 36px);
    overflow: auto;
  }
  #modal[data-kind="pitch-deck-pdf"]:has(.upload-error:not(:empty), .upload-file-status.is-error) .upload-form {
    position: relative;
    inset: auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
    width: 100%;
    height: auto;
  }
  #modal[data-kind="pitch-deck-pdf"]:has(.upload-error:not(:empty), .upload-file-status.is-error) :is(.upload-file-status, .upload-error, .upload-progress, .upload-actions) {
    position: relative;
    inset: auto;
    height: auto;
    margin: 0;
  }
  #modal[data-kind="pitch-deck-pdf"] :is(.upload-error:not(:empty), .upload-file-status.is-error, .deck-file-meta.is-error) {
    height: auto;
    overflow: visible;
    white-space: normal;
    text-overflow: clip;
  }
  #modal[data-kind="pitch-deck-pdf"]:has(.upload-error:not(:empty)) .upload-file-status { display: none; }
  #modal[data-kind="pitch-deck-pdf"]:has(.deck-file-meta.is-error) .uploaded-file { height: auto; min-height: 77px; }

  @media (min-width: 601px) {
    #modal[data-kind="create-profile"]:has(form > .error:not(:empty)) {
      height: fit-content;
      min-height: 0;
      max-height: calc(100dvh - 36px);
      overflow: auto;
    }
    #modal[data-kind="create-profile"]:has(form > .error:not(:empty)) form {
      position: static;
      display: block;
      width: 100%;
      margin: 0;
      /* The existing header occupies 66px before the tail at 377.25px. */
      padding: 311.25px 0 0;
    }
    #modal[data-kind="create-profile"]:has(form > .error:not(:empty)) :is(.signup-policy, form > .error, form > .primary, .modal-bottom) {
      position: relative;
      inset: auto;
      width: 100%;
      height: auto;
      margin: 0 0 12px;
    }
    #modal[data-kind="create-profile"]:has(form > .error:not(:empty)) .modal-bottom { padding: 0; }
  }


  /* Field validation needs flow between fields, as well as below the form.
     Keep the existing duplicate-email-only layout and error-free frames. */
  #modal:is([data-kind="sign-in"], [data-kind="create-profile"]):has(.field-error:not(:empty):not([data-duplicate-email])) {
    height: fit-content;
    min-height: 0;
    max-height: calc(var(--auth-viewport-height, 100dvh) - 36px);
    overflow: auto;
  }
  #modal:is([data-kind="sign-in"], [data-kind="create-profile"]):has(.field-error:not(:empty):not([data-duplicate-email])) form[data-auth-form] {
    position: static;
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
    margin: 0;
    padding: 0;
  }
  /* Include the field IDs so legacy ID-specific mobile offsets also reset. */
  #modal:is([data-kind="sign-in"], [data-kind="create-profile"]):has(.field-error:not(:empty):not([data-duplicate-email])) :is(form[data-auth-form] > *, .signup-name-field, .signup-name-field > *, .modal-bottom, #auth-email, #auth-password, #auth-email-error, #auth-password-error) {
    position: relative;
    inset: auto;
    translate: none;
    width: 100%;
    margin: 0;
  }
  #modal:is([data-kind="sign-in"], [data-kind="create-profile"]):has(.field-error:not(:empty):not([data-duplicate-email])) :is(label, .signup-name-row, .signup-policy, .modal-bottom, .field-error) { height: auto; }
  #modal:is([data-kind="sign-in"], [data-kind="create-profile"]):has(.field-error:not(:empty):not([data-duplicate-email])) .field-error:empty,
  #modal:is([data-kind="sign-in"], [data-kind="create-profile"]):has(.field-error:not(:empty):not([data-duplicate-email])) form > .error:empty { display: none; }
  #modal:is([data-kind="sign-in"], [data-kind="create-profile"]):has(.field-error:not(:empty):not([data-duplicate-email])) .modal-bottom { margin: 16px 0; padding: 0; }
  #modal[data-kind="create-profile"]:has(.field-error:not(:empty):not([data-duplicate-email])) .signup-name-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    align-items: start;
  }
  #modal[data-kind="create-profile"]:has(.field-error:not(:empty):not([data-duplicate-email])) .signup-name-field { display: grid; gap: 4px; }

  @media (min-width: 601px) {
    #modal[data-kind="sign-in"]:has(.field-error:not(:empty)) { padding-inline: 24px; }
    #modal[data-kind="sign-in"]:has(.field-error:not(:empty)) > h2 { margin-inline: -24px; }
    #modal[data-kind="create-profile"]:has(.field-error:not(:empty):not([data-duplicate-email])) form[data-auth-form] {
      /* Keep the name row below the existing Google button and divider. */
      padding-top: 105px;
    }
  }
}
